1
0

StorageSystem.Fill.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Linq;
  2. using Content.Server.Spawners.Components;
  3. using Content.Server.Storage.Components;
  4. using Content.Shared.Item;
  5. using Content.Shared.Prototypes;
  6. using Content.Shared.Storage;
  7. using Content.Shared.Storage.Components;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Utility;
  10. namespace Content.Server.Storage.EntitySystems;
  11. public sealed partial class StorageSystem
  12. {
  13. private void OnStorageFillMapInit(EntityUid uid, StorageFillComponent component, MapInitEvent args)
  14. {
  15. if (component.Contents.Count == 0)
  16. return;
  17. if (TryComp<StorageComponent>(uid, out var storageComp))
  18. {
  19. FillStorage((uid, component, storageComp));
  20. }
  21. else if (TryComp<EntityStorageComponent>(uid, out var entityStorageComp))
  22. {
  23. FillEntityStorage((uid, component, entityStorageComp));
  24. }
  25. else
  26. {
  27. Log.Error($"StorageFillComponent couldn't find any StorageComponent ({uid})");
  28. }
  29. }
  30. private void FillStorage(Entity<StorageFillComponent?, StorageComponent?> entity)
  31. {
  32. var (uid, component, storage) = entity;
  33. if (!Resolve(uid, ref component, ref storage))
  34. return;
  35. var coordinates = Transform(uid).Coordinates;
  36. var spawnItems = EntitySpawnCollection.GetSpawns(component.Contents, Random);
  37. var items = new List<Entity<ItemComponent>>();
  38. foreach (var spawnPrototype in spawnItems)
  39. {
  40. var ent = Spawn(spawnPrototype, coordinates);
  41. // No, you are not allowed to fill a container with entity spawners.
  42. DebugTools.Assert(!_prototype.Index<EntityPrototype>(spawnPrototype)
  43. .HasComponent(typeof(RandomSpawnerComponent)));
  44. if (!TryComp<ItemComponent>(ent, out var itemComp))
  45. {
  46. Log.Error($"Tried to fill {ToPrettyString(entity)} with non-item {spawnPrototype}.");
  47. Del(ent);
  48. continue;
  49. }
  50. items.Add((ent, itemComp));
  51. }
  52. // we order the items from biggest to smallest to try and reduce poor placement in the grid.
  53. var sortedItems = items
  54. .OrderByDescending(x => ItemSystem.GetItemShape(x.Comp).GetArea());
  55. ClearCantFillReasons();
  56. foreach (var ent in sortedItems)
  57. {
  58. if (Insert(uid, ent, out _, out var reason, storageComp: storage, playSound: false))
  59. continue;
  60. if (CantFillReasons.Count > 0)
  61. {
  62. var reasons = string.Join(", ", CantFillReasons.Select(s => Loc.GetString(s)));
  63. if (reason == null)
  64. reason = reasons;
  65. else
  66. reason += $", {reasons}";
  67. }
  68. Log.Error($"Tried to StorageFill {ToPrettyString(ent)} inside {ToPrettyString(uid)} but can't. reason: {reason}");
  69. ClearCantFillReasons();
  70. Del(ent);
  71. }
  72. }
  73. private void FillEntityStorage(Entity<StorageFillComponent?, EntityStorageComponent?> entity)
  74. {
  75. var (uid, component, entityStorageComp) = entity;
  76. if (!Resolve(uid, ref component, ref entityStorageComp))
  77. return;
  78. var coordinates = Transform(uid).Coordinates;
  79. var spawnItems = EntitySpawnCollection.GetSpawns(component.Contents, Random);
  80. foreach (var item in spawnItems)
  81. {
  82. // No, you are not allowed to fill a container with entity spawners.
  83. DebugTools.Assert(!_prototype.Index<EntityPrototype>(item)
  84. .HasComponent(typeof(RandomSpawnerComponent)));
  85. var ent = Spawn(item, coordinates);
  86. // handle depending on storage component, again this should be unified after ECS
  87. if (entityStorageComp != null && EntityStorage.Insert(ent, uid, entityStorageComp))
  88. continue;
  89. Log.Error($"Tried to StorageFill {item} inside {ToPrettyString(uid)} but can't.");
  90. Del(ent);
  91. }
  92. }
  93. }