SharedSubdermalImplantSystem.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Content.Shared.Actions;
  2. using Content.Shared.Implants.Components;
  3. using Content.Shared.Interaction;
  4. using Content.Shared.Interaction.Events;
  5. using Content.Shared.Mobs;
  6. using Content.Shared.Tag;
  7. using JetBrains.Annotations;
  8. using Robust.Shared.Containers;
  9. using Robust.Shared.Network;
  10. using System.Linq;
  11. namespace Content.Shared.Implants;
  12. public abstract class SharedSubdermalImplantSystem : EntitySystem
  13. {
  14. [Dependency] private readonly INetManager _net = default!;
  15. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  16. [Dependency] private readonly SharedContainerSystem _container = default!;
  17. [Dependency] private readonly TagSystem _tag = default!;
  18. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  19. public const string BaseStorageId = "storagebase";
  20. public override void Initialize()
  21. {
  22. SubscribeLocalEvent<SubdermalImplantComponent, EntGotInsertedIntoContainerMessage>(OnInsert);
  23. SubscribeLocalEvent<SubdermalImplantComponent, ContainerGettingRemovedAttemptEvent>(OnRemoveAttempt);
  24. SubscribeLocalEvent<SubdermalImplantComponent, EntGotRemovedFromContainerMessage>(OnRemove);
  25. SubscribeLocalEvent<ImplantedComponent, MobStateChangedEvent>(RelayToImplantEvent);
  26. SubscribeLocalEvent<ImplantedComponent, AfterInteractUsingEvent>(RelayToImplantEvent);
  27. SubscribeLocalEvent<ImplantedComponent, SuicideEvent>(RelayToImplantEvent);
  28. }
  29. private void OnInsert(EntityUid uid, SubdermalImplantComponent component, EntGotInsertedIntoContainerMessage args)
  30. {
  31. if (component.ImplantedEntity == null || _net.IsClient)
  32. return;
  33. if (!string.IsNullOrWhiteSpace(component.ImplantAction))
  34. {
  35. _actionsSystem.AddAction(component.ImplantedEntity.Value, ref component.Action, component.ImplantAction, uid);
  36. }
  37. //replace micro bomb with macro bomb
  38. if (_container.TryGetContainer(component.ImplantedEntity.Value, ImplanterComponent.ImplantSlotId, out var implantContainer) && _tag.HasTag(uid, "MacroBomb"))
  39. {
  40. foreach (var implant in implantContainer.ContainedEntities)
  41. {
  42. if (_tag.HasTag(implant, "MicroBomb"))
  43. {
  44. _container.Remove(implant, implantContainer);
  45. QueueDel(implant);
  46. }
  47. }
  48. }
  49. var ev = new ImplantImplantedEvent(uid, component.ImplantedEntity.Value);
  50. RaiseLocalEvent(uid, ref ev);
  51. }
  52. private void OnRemoveAttempt(EntityUid uid, SubdermalImplantComponent component, ContainerGettingRemovedAttemptEvent args)
  53. {
  54. if (component.Permanent && component.ImplantedEntity != null)
  55. args.Cancel();
  56. }
  57. private void OnRemove(EntityUid uid, SubdermalImplantComponent component, EntGotRemovedFromContainerMessage args)
  58. {
  59. if (component.ImplantedEntity == null || Terminating(component.ImplantedEntity.Value))
  60. return;
  61. if (component.ImplantAction != null)
  62. _actionsSystem.RemoveProvidedActions(component.ImplantedEntity.Value, uid);
  63. if (!_container.TryGetContainer(uid, BaseStorageId, out var storageImplant))
  64. return;
  65. var containedEntites = storageImplant.ContainedEntities.ToArray();
  66. foreach (var entity in containedEntites)
  67. {
  68. _transformSystem.DropNextTo(entity, uid);
  69. }
  70. }
  71. /// <summary>
  72. /// Add a list of implants to a person.
  73. /// Logs any implant ids that don't have <see cref="SubdermalImplantComponent"/>.
  74. /// </summary>
  75. public void AddImplants(EntityUid uid, IEnumerable<String> implants)
  76. {
  77. foreach (var id in implants)
  78. {
  79. AddImplant(uid, id);
  80. }
  81. }
  82. /// <summary>
  83. /// Adds a single implant to a person, and returns the implant.
  84. /// Logs any implant ids that don't have <see cref="SubdermalImplantComponent"/>.
  85. /// </summary>
  86. /// <returns>
  87. /// The implant, if it was successfully created. Otherwise, null.
  88. /// </returns>>
  89. public EntityUid? AddImplant(EntityUid uid, String implantId)
  90. {
  91. var coords = Transform(uid).Coordinates;
  92. var ent = Spawn(implantId, coords);
  93. if (TryComp<SubdermalImplantComponent>(ent, out var implant))
  94. {
  95. ForceImplant(uid, ent, implant);
  96. }
  97. else
  98. {
  99. Log.Warning($"Found invalid starting implant '{implantId}' on {uid} {ToPrettyString(uid):implanted}");
  100. Del(ent);
  101. return null;
  102. }
  103. return ent;
  104. }
  105. /// <summary>
  106. /// Forces an implant into a person
  107. /// Good for on spawn related code or admin additions
  108. /// </summary>
  109. /// <param name="target">The entity to be implanted</param>
  110. /// <param name="implant"> The implant</param>
  111. /// <param name="component">The implant component</param>
  112. public void ForceImplant(EntityUid target, EntityUid implant, SubdermalImplantComponent component)
  113. {
  114. //If the target doesn't have the implanted component, add it.
  115. var implantedComp = EnsureComp<ImplantedComponent>(target);
  116. var implantContainer = implantedComp.ImplantContainer;
  117. component.ImplantedEntity = target;
  118. _container.Insert(implant, implantContainer);
  119. }
  120. /// <summary>
  121. /// Force remove a singular implant
  122. /// </summary>
  123. /// <param name="target">the implanted entity</param>
  124. /// <param name="implant">the implant</param>
  125. [PublicAPI]
  126. public void ForceRemove(EntityUid target, EntityUid implant)
  127. {
  128. if (!TryComp<ImplantedComponent>(target, out var implanted))
  129. return;
  130. var implantContainer = implanted.ImplantContainer;
  131. _container.Remove(implant, implantContainer);
  132. QueueDel(implant);
  133. }
  134. /// <summary>
  135. /// Removes and deletes implants by force
  136. /// </summary>
  137. /// <param name="target">The entity to have implants removed</param>
  138. [PublicAPI]
  139. public void WipeImplants(EntityUid target)
  140. {
  141. if (!TryComp<ImplantedComponent>(target, out var implanted))
  142. return;
  143. var implantContainer = implanted.ImplantContainer;
  144. _container.CleanContainer(implantContainer);
  145. }
  146. //Relays from the implanted to the implant
  147. private void RelayToImplantEvent<T>(EntityUid uid, ImplantedComponent component, T args) where T : notnull
  148. {
  149. if (!_container.TryGetContainer(uid, ImplanterComponent.ImplantSlotId, out var implantContainer))
  150. return;
  151. var relayEv = new ImplantRelayEvent<T>(args);
  152. foreach (var implant in implantContainer.ContainedEntities)
  153. {
  154. if (args is HandledEntityEventArgs { Handled : true })
  155. return;
  156. RaiseLocalEvent(implant, relayEv);
  157. }
  158. }
  159. }
  160. public sealed class ImplantRelayEvent<T> where T : notnull
  161. {
  162. public readonly T Event;
  163. public ImplantRelayEvent(T ev)
  164. {
  165. Event = ev;
  166. }
  167. }
  168. /// <summary>
  169. /// Event that is raised whenever someone is implanted with any given implant.
  170. /// Raised on the the implant entity.
  171. /// </summary>
  172. /// <remarks>
  173. /// implant implant implant implant
  174. /// </remarks>
  175. [ByRefEvent]
  176. public readonly struct ImplantImplantedEvent
  177. {
  178. public readonly EntityUid Implant;
  179. public readonly EntityUid? Implanted;
  180. public ImplantImplantedEvent(EntityUid implant, EntityUid? implanted)
  181. {
  182. Implant = implant;
  183. Implanted = implanted;
  184. }
  185. }