SharedMaterialStorageSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Linq;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.Interaction.Components;
  4. using Content.Shared.Mobs;
  5. using Content.Shared.Stacks;
  6. using Content.Shared.Whitelist;
  7. using JetBrains.Annotations;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Timing;
  10. using Robust.Shared.Utility;
  11. using Content.Shared.Research.Components;
  12. namespace Content.Shared.Materials;
  13. /// <summary>
  14. /// This handles storing materials and modifying their amounts
  15. /// <see cref="MaterialStorageComponent"/>
  16. /// </summary>
  17. public abstract class SharedMaterialStorageSystem : EntitySystem
  18. {
  19. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  20. [Dependency] private readonly IGameTiming _timing = default!;
  21. [Dependency] private readonly IPrototypeManager _prototype = default!;
  22. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  23. /// <summary>
  24. /// Default volume for a sheet if the material's entity prototype has no material composition.
  25. /// </summary>
  26. private const int DefaultSheetVolume = 100;
  27. /// <inheritdoc/>
  28. public override void Initialize()
  29. {
  30. base.Initialize();
  31. SubscribeLocalEvent<MaterialStorageComponent, MapInitEvent>(OnMapInit);
  32. SubscribeLocalEvent<MaterialStorageComponent, InteractUsingEvent>(OnInteractUsing);
  33. SubscribeLocalEvent<MaterialStorageComponent, TechnologyDatabaseModifiedEvent>(OnDatabaseModified);
  34. }
  35. public override void Update(float frameTime)
  36. {
  37. base.Update(frameTime);
  38. var query = EntityQueryEnumerator<InsertingMaterialStorageComponent>();
  39. while (query.MoveNext(out var uid, out var inserting))
  40. {
  41. if (_timing.CurTime < inserting.EndTime)
  42. continue;
  43. _appearance.SetData(uid, MaterialStorageVisuals.Inserting, false);
  44. RemComp(uid, inserting);
  45. }
  46. }
  47. private void OnMapInit(EntityUid uid, MaterialStorageComponent component, MapInitEvent args)
  48. {
  49. _appearance.SetData(uid, MaterialStorageVisuals.Inserting, false);
  50. }
  51. /// <summary>
  52. /// Gets the volume of a specified material contained in this storage.
  53. /// </summary>
  54. /// <param name="uid"></param>
  55. /// <param name="material"></param>
  56. /// <param name="component"></param>
  57. /// <returns>The volume of the material</returns>
  58. [PublicAPI]
  59. public int GetMaterialAmount(EntityUid uid, MaterialPrototype material, MaterialStorageComponent? component = null)
  60. {
  61. return GetMaterialAmount(uid, material.ID, component);
  62. }
  63. /// <summary>
  64. /// Gets the volume of a specified material contained in this storage.
  65. /// </summary>
  66. /// <param name="uid"></param>
  67. /// <param name="material"></param>
  68. /// <param name="component"></param>
  69. /// <returns>The volume of the material</returns>
  70. public int GetMaterialAmount(EntityUid uid, string material, MaterialStorageComponent? component = null)
  71. {
  72. if (!Resolve(uid, ref component))
  73. return 0; //you have nothing
  74. return component.Storage.GetValueOrDefault(material, 0);
  75. }
  76. /// <summary>
  77. /// Gets the total volume of all materials in the storage.
  78. /// </summary>
  79. /// <param name="uid"></param>
  80. /// <param name="component"></param>
  81. /// <returns>The volume of all materials in the storage</returns>
  82. public int GetTotalMaterialAmount(EntityUid uid, MaterialStorageComponent? component = null)
  83. {
  84. if (!Resolve(uid, ref component))
  85. return 0;
  86. return component.Storage.Values.Sum();
  87. }
  88. /// <summary>
  89. /// Tests if a specific amount of volume will fit in the storage.
  90. /// </summary>
  91. /// <param name="uid"></param>
  92. /// <param name="volume"></param>
  93. /// <param name="component"></param>
  94. /// <returns>If the specified volume will fit</returns>
  95. public bool CanTakeVolume(EntityUid uid, int volume, MaterialStorageComponent? component = null)
  96. {
  97. if (!Resolve(uid, ref component))
  98. return false;
  99. return component.StorageLimit == null || GetTotalMaterialAmount(uid, component) + volume <= component.StorageLimit;
  100. }
  101. /// <summary>
  102. /// Checks if the specified material can be changed by the specified volume.
  103. /// </summary>
  104. /// <param name="uid"></param>
  105. /// <param name="materialId"></param>
  106. /// <param name="volume"></param>
  107. /// <param name="component"></param>
  108. /// <returns>If the amount can be changed</returns>
  109. public bool CanChangeMaterialAmount(EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null)
  110. {
  111. if (!Resolve(uid, ref component))
  112. return false;
  113. if (!CanTakeVolume(uid, volume, component))
  114. return false;
  115. if (component.MaterialWhiteList == null ? false : !component.MaterialWhiteList.Contains(materialId))
  116. return false;
  117. var amount = component.Storage.GetValueOrDefault(materialId);
  118. return amount + volume >= 0;
  119. }
  120. /// <summary>
  121. /// Checks if the specified materials can be changed by the specified volumes.
  122. /// </summary>
  123. /// <param name="entity"></param>
  124. /// <param name="materials"></param>
  125. /// <returns>If the amount can be changed</returns>
  126. public bool CanChangeMaterialAmount(Entity<MaterialStorageComponent?> entity, Dictionary<string,int> materials)
  127. {
  128. if (!Resolve(entity, ref entity.Comp))
  129. return false;
  130. foreach (var (material, amount) in materials)
  131. {
  132. if (!CanChangeMaterialAmount(entity, material, amount, entity.Comp))
  133. return false;
  134. }
  135. return true;
  136. }
  137. /// <summary>
  138. /// Changes the amount of a specific material in the storage.
  139. /// Still respects the filters in place.
  140. /// </summary>
  141. /// <param name="uid"></param>
  142. /// <param name="materialId"></param>
  143. /// <param name="volume"></param>
  144. /// <param name="component"></param>
  145. /// <param name="dirty"></param>
  146. /// <returns>If it was successful</returns>
  147. public bool TryChangeMaterialAmount(EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null, bool dirty = true)
  148. {
  149. if (!Resolve(uid, ref component))
  150. return false;
  151. if (!CanChangeMaterialAmount(uid, materialId, volume, component))
  152. return false;
  153. var existing = component.Storage.GetOrNew(materialId);
  154. existing += volume;
  155. if (existing == 0)
  156. component.Storage.Remove(materialId);
  157. else
  158. component.Storage[materialId] = existing;
  159. var ev = new MaterialAmountChangedEvent();
  160. RaiseLocalEvent(uid, ref ev);
  161. if (dirty)
  162. Dirty(uid, component);
  163. return true;
  164. }
  165. /// <summary>
  166. /// Changes the amount of a specific material in the storage.
  167. /// Still respects the filters in place.
  168. /// </summary>
  169. /// <param name="entity"></param>
  170. /// <param name="materials"></param>
  171. /// <returns>If the amount can be changed</returns>
  172. public bool TryChangeMaterialAmount(Entity<MaterialStorageComponent?> entity, Dictionary<string,int> materials)
  173. {
  174. if (!Resolve(entity, ref entity.Comp))
  175. return false;
  176. if (!CanChangeMaterialAmount(entity, materials))
  177. return false;
  178. foreach (var (material, amount) in materials)
  179. {
  180. if (!TryChangeMaterialAmount(entity, material, amount, entity.Comp, false))
  181. return false;
  182. }
  183. Dirty(entity, entity.Comp);
  184. return true;
  185. }
  186. /// <summary>
  187. /// Tries to set the amount of material in the storage to a specific value.
  188. /// Still respects the filters in place.
  189. /// </summary>
  190. /// <param name="uid">The entity to change the material storage on.</param>
  191. /// <param name="materialId">The ID of the material to change.</param>
  192. /// <param name="volume">The stored material volume to set the storage to.</param>
  193. /// <param name="component">The storage component on <paramref name="uid"/>. Resolved automatically if not given.</param>
  194. /// <returns>True if it was successful (enough space etc).</returns>
  195. public bool TrySetMaterialAmount(
  196. EntityUid uid,
  197. string materialId,
  198. int volume,
  199. MaterialStorageComponent? component = null)
  200. {
  201. if (!Resolve(uid, ref component))
  202. return false;
  203. var curAmount = GetMaterialAmount(uid, materialId, component);
  204. var delta = volume - curAmount;
  205. return TryChangeMaterialAmount(uid, materialId, delta, component);
  206. }
  207. /// <summary>
  208. /// Tries to insert an entity into the material storage.
  209. /// </summary>
  210. public virtual bool TryInsertMaterialEntity(EntityUid user,
  211. EntityUid toInsert,
  212. EntityUid receiver,
  213. MaterialStorageComponent? storage = null,
  214. MaterialComponent? material = null,
  215. PhysicalCompositionComponent? composition = null)
  216. {
  217. if (!Resolve(receiver, ref storage))
  218. return false;
  219. if (!Resolve(toInsert, ref material, ref composition, false))
  220. return false;
  221. if (_whitelistSystem.IsWhitelistFail(storage.Whitelist, toInsert))
  222. return false;
  223. if (HasComp<UnremoveableComponent>(toInsert))
  224. return false;
  225. // Material Whitelist checked implicitly by CanChangeMaterialAmount();
  226. var multiplier = TryComp<StackComponent>(toInsert, out var stackComponent) ? stackComponent.Count : 1;
  227. var totalVolume = 0;
  228. foreach (var (mat, vol) in composition.MaterialComposition)
  229. {
  230. if (!CanChangeMaterialAmount(receiver, mat, vol * multiplier, storage))
  231. return false;
  232. totalVolume += vol * multiplier;
  233. }
  234. if (!CanTakeVolume(receiver, totalVolume, storage))
  235. return false;
  236. foreach (var (mat, vol) in composition.MaterialComposition)
  237. {
  238. TryChangeMaterialAmount(receiver, mat, vol * multiplier, storage);
  239. }
  240. var insertingComp = EnsureComp<InsertingMaterialStorageComponent>(receiver);
  241. insertingComp.EndTime = _timing.CurTime + storage.InsertionTime;
  242. if (!storage.IgnoreColor)
  243. {
  244. _prototype.TryIndex<MaterialPrototype>(composition.MaterialComposition.Keys.First(), out var lastMat);
  245. insertingComp.MaterialColor = lastMat?.Color;
  246. }
  247. _appearance.SetData(receiver, MaterialStorageVisuals.Inserting, true);
  248. Dirty(receiver, insertingComp);
  249. var ev = new MaterialEntityInsertedEvent(material);
  250. RaiseLocalEvent(receiver, ref ev);
  251. return true;
  252. }
  253. /// <summary>
  254. /// Broadcasts an event that will collect a list of which materials
  255. /// are allowed to be inserted into the materialStorage.
  256. /// </summary>
  257. /// <param name="uid"></param>
  258. /// <param name="component"></param>
  259. public void UpdateMaterialWhitelist(EntityUid uid, MaterialStorageComponent? component = null)
  260. {
  261. if (!Resolve(uid, ref component, false))
  262. return;
  263. var ev = new GetMaterialWhitelistEvent(uid);
  264. RaiseLocalEvent(uid, ref ev);
  265. component.MaterialWhiteList = ev.Whitelist;
  266. Dirty(uid, component);
  267. }
  268. private void OnInteractUsing(EntityUid uid, MaterialStorageComponent component, InteractUsingEvent args)
  269. {
  270. if (args.Handled || !component.InsertOnInteract)
  271. return;
  272. args.Handled = TryInsertMaterialEntity(args.User, args.Used, uid, component);
  273. }
  274. private void OnDatabaseModified(Entity<MaterialStorageComponent> ent, ref TechnologyDatabaseModifiedEvent args)
  275. {
  276. UpdateMaterialWhitelist(ent);
  277. }
  278. public int GetSheetVolume(MaterialPrototype material)
  279. {
  280. if (material.StackEntity == null)
  281. return DefaultSheetVolume;
  282. var proto = _prototype.Index<EntityPrototype>(material.StackEntity);
  283. if (!proto.TryGetComponent<PhysicalCompositionComponent>(out var composition, EntityManager.ComponentFactory))
  284. return DefaultSheetVolume;
  285. return composition.MaterialComposition.FirstOrDefault(kvp => kvp.Key == material.ID).Value;
  286. }
  287. }