2023-12-04 18:04:39 -05:00
|
|
|
using System.Linq;
|
2023-10-16 16:54:10 +11:00
|
|
|
using Content.Server.Spawners.Components;
|
2022-01-29 22:45:40 +11:00
|
|
|
using Content.Server.Storage.Components;
|
2023-12-04 18:04:39 -05:00
|
|
|
using Content.Shared.Item;
|
2023-10-16 16:54:10 +11:00
|
|
|
using Content.Shared.Prototypes;
|
2022-03-28 09:58:13 -07:00
|
|
|
using Content.Shared.Storage;
|
2023-09-11 21:20:46 +10:00
|
|
|
using Content.Shared.Storage.Components;
|
2023-10-16 16:54:10 +11:00
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
using Robust.Shared.Utility;
|
2022-01-29 22:45:40 +11:00
|
|
|
|
|
|
|
|
namespace Content.Server.Storage.EntitySystems;
|
|
|
|
|
|
|
|
|
|
public sealed partial class StorageSystem
|
|
|
|
|
{
|
|
|
|
|
private void OnStorageFillMapInit(EntityUid uid, StorageFillComponent component, MapInitEvent args)
|
|
|
|
|
{
|
2023-09-11 21:20:46 +10:00
|
|
|
if (component.Contents.Count == 0)
|
|
|
|
|
return;
|
2022-07-13 19:11:59 -04:00
|
|
|
|
2023-12-04 18:04:39 -05:00
|
|
|
if (TryComp<StorageComponent>(uid, out var storageComp))
|
|
|
|
|
{
|
|
|
|
|
FillStorage((uid, component, storageComp));
|
|
|
|
|
}
|
|
|
|
|
else if (TryComp<EntityStorageComponent>(uid, out var entityStorageComp))
|
|
|
|
|
{
|
|
|
|
|
FillEntityStorage((uid, component, entityStorageComp));
|
|
|
|
|
}
|
|
|
|
|
else
|
2022-01-29 22:45:40 +11:00
|
|
|
{
|
2023-09-11 21:20:46 +10:00
|
|
|
Log.Error($"StorageFillComponent couldn't find any StorageComponent ({uid})");
|
2023-12-04 18:04:39 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FillStorage(Entity<StorageFillComponent?, StorageComponent?> entity)
|
|
|
|
|
{
|
|
|
|
|
var (uid, component, storage) = entity;
|
|
|
|
|
|
|
|
|
|
if (!Resolve(uid, ref component, ref storage))
|
2022-01-29 22:45:40 +11:00
|
|
|
return;
|
2023-12-04 18:04:39 -05:00
|
|
|
|
|
|
|
|
var coordinates = Transform(uid).Coordinates;
|
|
|
|
|
|
|
|
|
|
var spawnItems = EntitySpawnCollection.GetSpawns(component.Contents, Random);
|
|
|
|
|
|
|
|
|
|
var items = new List<Entity<ItemComponent>>();
|
|
|
|
|
foreach (var spawnPrototype in spawnItems)
|
|
|
|
|
{
|
|
|
|
|
var ent = Spawn(spawnPrototype, coordinates);
|
|
|
|
|
|
|
|
|
|
// No, you are not allowed to fill a container with entity spawners.
|
|
|
|
|
DebugTools.Assert(!_prototype.Index<EntityPrototype>(spawnPrototype)
|
|
|
|
|
.HasComponent(typeof(RandomSpawnerComponent)));
|
|
|
|
|
|
|
|
|
|
if (!TryComp<ItemComponent>(ent, out var itemComp))
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"Tried to fill {ToPrettyString(entity)} with non-item {spawnPrototype}.");
|
|
|
|
|
Del(ent);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
items.Add((ent, itemComp));
|
2022-01-29 22:45:40 +11:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 18:04:39 -05:00
|
|
|
// we order the items from biggest to smallest to try and reduce poor placement in the grid.
|
|
|
|
|
var sortedItems = items
|
|
|
|
|
.OrderByDescending(x => ItemSystem.GetItemShape(x.Comp).GetArea());
|
|
|
|
|
|
|
|
|
|
foreach (var ent in sortedItems)
|
|
|
|
|
{
|
|
|
|
|
if (Insert(uid, ent, out _, out var reason, storageComp: storage, playSound: false))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Log.Error($"Tried to StorageFill {ToPrettyString(ent)} inside {ToPrettyString(uid)} but can't. reason: {reason}");
|
|
|
|
|
Del(ent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FillEntityStorage(Entity<StorageFillComponent?, EntityStorageComponent?> entity)
|
|
|
|
|
{
|
|
|
|
|
var (uid, component, entityStorageComp) = entity;
|
|
|
|
|
|
|
|
|
|
if (!Resolve(uid, ref component, ref entityStorageComp))
|
|
|
|
|
return;
|
|
|
|
|
|
2022-01-29 22:45:40 +11:00
|
|
|
var coordinates = Transform(uid).Coordinates;
|
|
|
|
|
|
2023-09-11 21:20:46 +10:00
|
|
|
var spawnItems = EntitySpawnCollection.GetSpawns(component.Contents, Random);
|
2022-03-28 09:58:13 -07:00
|
|
|
foreach (var item in spawnItems)
|
2022-01-29 22:45:40 +11:00
|
|
|
{
|
2023-10-16 16:54:10 +11:00
|
|
|
// No, you are not allowed to fill a container with entity spawners.
|
|
|
|
|
DebugTools.Assert(!_prototype.Index<EntityPrototype>(item)
|
|
|
|
|
.HasComponent(typeof(RandomSpawnerComponent)));
|
2023-12-04 18:04:39 -05:00
|
|
|
var ent = Spawn(item, coordinates);
|
2022-01-29 22:45:40 +11:00
|
|
|
|
2022-04-28 06:11:15 -06:00
|
|
|
// handle depending on storage component, again this should be unified after ECS
|
2023-10-16 16:54:10 +11:00
|
|
|
if (entityStorageComp != null && EntityStorage.Insert(ent, uid, entityStorageComp))
|
2023-09-11 21:20:46 +10:00
|
|
|
continue;
|
2022-04-28 06:11:15 -06:00
|
|
|
|
2023-12-04 18:04:39 -05:00
|
|
|
Log.Error($"Tried to StorageFill {item} inside {ToPrettyString(uid)} but can't.");
|
|
|
|
|
Del(ent);
|
2022-03-27 16:04:58 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-29 22:45:40 +11:00
|
|
|
}
|