2021-03-05 01:08:38 +01:00
|
|
|
#nullable enable
|
2021-02-11 01:13:03 -08:00
|
|
|
using System;
|
2020-09-21 12:47:52 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Log;
|
2020-09-21 12:47:52 +02:00
|
|
|
using Robust.Shared.Random;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager;
|
|
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Items.Storage
|
|
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-03-05 01:08:38 +01:00
|
|
|
public sealed class StorageFillComponent : Component, IMapInit
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
|
|
|
|
public override string Name => "StorageFill";
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("contents")]
|
|
|
|
|
private List<StorageFillEntry> _contents = new();
|
2020-09-21 12:47:52 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
public IReadOnlyList<StorageFillEntry> Contents => _contents;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
|
|
|
|
void IMapInit.MapInit()
|
|
|
|
|
{
|
|
|
|
|
if (_contents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
if (!Owner.TryGetComponent(out IStorageComponent? storage))
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
|
|
|
|
Logger.Error($"StorageFillComponent couldn't find any StorageComponent ({Owner})");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-05 01:08:38 +01:00
|
|
|
|
2020-09-21 12:47:52 +02:00
|
|
|
var random = IoCManager.Resolve<IRobustRandom>();
|
|
|
|
|
|
|
|
|
|
var alreadySpawnedGroups = new List<string>();
|
|
|
|
|
foreach (var storageItem in _contents)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(storageItem.PrototypeName)) continue;
|
2020-09-25 13:49:59 +02:00
|
|
|
if (!string.IsNullOrEmpty(storageItem.GroupId) && alreadySpawnedGroups.Contains(storageItem.GroupId)) continue;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
|
|
|
|
if (storageItem.SpawnProbability != 1f &&
|
|
|
|
|
!random.Prob(storageItem.SpawnProbability))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 21:28:06 +02:00
|
|
|
for (var i = 0; i < storageItem.Amount; i++)
|
|
|
|
|
{
|
2020-10-12 01:42:53 +11:00
|
|
|
storage.Insert(Owner.EntityManager.SpawnEntity(storageItem.PrototypeName, Owner.Transform.Coordinates));
|
2020-10-05 21:28:06 +02:00
|
|
|
}
|
2020-09-25 13:49:59 +02:00
|
|
|
if (!string.IsNullOrEmpty(storageItem.GroupId)) alreadySpawnedGroups.Add(storageItem.GroupId);
|
2020-09-21 12:47:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataDefinition]
|
|
|
|
|
public struct StorageFillEntry : IPopulateDefaultValues
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("name")]
|
|
|
|
|
public string? PrototypeName;
|
|
|
|
|
|
|
|
|
|
[DataField("prob")]
|
2020-09-21 12:47:52 +02:00
|
|
|
public float SpawnProbability;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[DataField("orGroup")]
|
2020-09-21 12:47:52 +02:00
|
|
|
public string GroupId;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[DataField("amount")]
|
2020-09-21 12:47:52 +02:00
|
|
|
public int Amount;
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
public void PopulateDefaultValues()
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
Amount = 1;
|
|
|
|
|
SpawnProbability = 1;
|
2020-09-21 12:47:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|