2021-02-11 01:13:03 -08:00
|
|
|
using System;
|
2020-09-21 12:47:52 +02:00
|
|
|
using System.Collections.Generic;
|
2021-07-22 11:56:55 +02:00
|
|
|
using Content.Shared.Storage;
|
2020-09-21 12:47:52 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Log;
|
2021-03-15 13:25:18 +01:00
|
|
|
using Robust.Shared.Prototypes;
|
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;
|
2021-03-26 20:55:14 +01:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
2020-09-21 12:47:52 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Storage.Components
|
2020-09-21 12:47:52 +02:00
|
|
|
{
|
|
|
|
|
[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-07-25 08:41:50 -07:00
|
|
|
[DataField("contents")] private List<EntitySpawnEntry> _contents = new();
|
2020-09-21 12:47:52 +02:00
|
|
|
|
2021-07-25 08:41:50 -07:00
|
|
|
public IReadOnlyList<EntitySpawnEntry> 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)
|
|
|
|
|
{
|
2021-07-22 11:56:55 +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++)
|
|
|
|
|
{
|
2021-07-22 11:56:55 +02:00
|
|
|
storage.Insert(
|
|
|
|
|
Owner.EntityManager.SpawnEntity(storageItem.PrototypeId, Owner.Transform.Coordinates));
|
2020-10-05 21:28:06 +02:00
|
|
|
}
|
2021-07-22 11:56:55 +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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|