SubdermalImplantSystem.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using Content.Server.Cuffs;
  2. using Content.Server.Forensics;
  3. using Content.Server.Humanoid;
  4. using Content.Server.Implants.Components;
  5. using Content.Server.Store.Components;
  6. using Content.Server.Store.Systems;
  7. using Content.Shared.Cuffs.Components;
  8. using Content.Shared.Forensics;
  9. using Content.Shared.Forensics.Components;
  10. using Content.Shared.Humanoid;
  11. using Content.Shared.Implants;
  12. using Content.Shared.Implants.Components;
  13. using Content.Shared.Interaction;
  14. using Content.Shared.Physics;
  15. using Content.Shared.Popups;
  16. using Content.Shared.Preferences;
  17. using Robust.Shared.Audio.Systems;
  18. using Robust.Shared.Map;
  19. using Robust.Shared.Physics;
  20. using Robust.Shared.Physics.Components;
  21. using Robust.Shared.Random;
  22. using System.Numerics;
  23. using Content.Shared.Movement.Pulling.Components;
  24. using Content.Shared.Movement.Pulling.Systems;
  25. using Content.Server.IdentityManagement;
  26. using Content.Shared.DetailExaminable;
  27. using Content.Shared.Store.Components;
  28. using Robust.Shared.Collections;
  29. using Robust.Shared.Map.Components;
  30. namespace Content.Server.Implants;
  31. public sealed class SubdermalImplantSystem : SharedSubdermalImplantSystem
  32. {
  33. [Dependency] private readonly CuffableSystem _cuffable = default!;
  34. [Dependency] private readonly HumanoidAppearanceSystem _humanoidAppearance = default!;
  35. [Dependency] private readonly IRobustRandom _random = default!;
  36. [Dependency] private readonly MetaDataSystem _metaData = default!;
  37. [Dependency] private readonly StoreSystem _store = default!;
  38. [Dependency] private readonly SharedAudioSystem _audio = default!;
  39. [Dependency] private readonly SharedPopupSystem _popup = default!;
  40. [Dependency] private readonly SharedTransformSystem _xform = default!;
  41. [Dependency] private readonly ForensicsSystem _forensicsSystem = default!;
  42. [Dependency] private readonly PullingSystem _pullingSystem = default!;
  43. [Dependency] private readonly EntityLookupSystem _lookupSystem = default!;
  44. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  45. [Dependency] private readonly IdentitySystem _identity = default!;
  46. private EntityQuery<PhysicsComponent> _physicsQuery;
  47. private HashSet<Entity<MapGridComponent>> _targetGrids = [];
  48. public override void Initialize()
  49. {
  50. base.Initialize();
  51. _physicsQuery = GetEntityQuery<PhysicsComponent>();
  52. SubscribeLocalEvent<SubdermalImplantComponent, UseFreedomImplantEvent>(OnFreedomImplant);
  53. SubscribeLocalEvent<StoreComponent, ImplantRelayEvent<AfterInteractUsingEvent>>(OnStoreRelay);
  54. SubscribeLocalEvent<SubdermalImplantComponent, ActivateImplantEvent>(OnActivateImplantEvent);
  55. SubscribeLocalEvent<SubdermalImplantComponent, UseScramImplantEvent>(OnScramImplant);
  56. SubscribeLocalEvent<SubdermalImplantComponent, UseDnaScramblerImplantEvent>(OnDnaScramblerImplant);
  57. }
  58. private void OnStoreRelay(EntityUid uid, StoreComponent store, ImplantRelayEvent<AfterInteractUsingEvent> implantRelay)
  59. {
  60. var args = implantRelay.Event;
  61. if (args.Handled)
  62. return;
  63. // can only insert into yourself to prevent uplink checking with renault
  64. if (args.Target != args.User)
  65. return;
  66. if (!TryComp<CurrencyComponent>(args.Used, out var currency))
  67. return;
  68. // same as store code, but message is only shown to yourself
  69. if (!_store.TryAddCurrency((args.Used, currency), (uid, store)))
  70. return;
  71. args.Handled = true;
  72. var msg = Loc.GetString("store-currency-inserted-implant", ("used", args.Used));
  73. _popup.PopupEntity(msg, args.User, args.User);
  74. }
  75. private void OnFreedomImplant(EntityUid uid, SubdermalImplantComponent component, UseFreedomImplantEvent args)
  76. {
  77. if (!TryComp<CuffableComponent>(component.ImplantedEntity, out var cuffs) || cuffs.Container.ContainedEntities.Count < 1)
  78. return;
  79. _cuffable.Uncuff(component.ImplantedEntity.Value, cuffs.LastAddedCuffs, cuffs.LastAddedCuffs);
  80. args.Handled = true;
  81. }
  82. private void OnActivateImplantEvent(EntityUid uid, SubdermalImplantComponent component, ActivateImplantEvent args)
  83. {
  84. args.Handled = true;
  85. }
  86. private void OnScramImplant(EntityUid uid, SubdermalImplantComponent component, UseScramImplantEvent args)
  87. {
  88. if (component.ImplantedEntity is not { } ent)
  89. return;
  90. if (!TryComp<ScramImplantComponent>(uid, out var implant))
  91. return;
  92. // We need stop the user from being pulled so they don't just get "attached" with whoever is pulling them.
  93. // This can for example happen when the user is cuffed and being pulled.
  94. if (TryComp<PullableComponent>(ent, out var pull) && _pullingSystem.IsPulled(ent, pull))
  95. _pullingSystem.TryStopPull(ent, pull);
  96. // Check if the user is pulling anything, and drop it if so
  97. if (TryComp<PullerComponent>(ent, out var puller) && TryComp<PullableComponent>(puller.Pulling, out var pullable))
  98. _pullingSystem.TryStopPull(puller.Pulling.Value, pullable);
  99. var xform = Transform(ent);
  100. var targetCoords = SelectRandomTileInRange(xform, implant.TeleportRadius);
  101. if (targetCoords != null)
  102. {
  103. _xform.SetCoordinates(ent, targetCoords.Value);
  104. _audio.PlayPvs(implant.TeleportSound, ent);
  105. args.Handled = true;
  106. }
  107. }
  108. private EntityCoordinates? SelectRandomTileInRange(TransformComponent userXform, float radius)
  109. {
  110. var userCoords = userXform.Coordinates.ToMap(EntityManager, _xform);
  111. _targetGrids.Clear();
  112. _lookupSystem.GetEntitiesInRange(userCoords, radius, _targetGrids);
  113. Entity<MapGridComponent>? targetGrid = null;
  114. if (_targetGrids.Count == 0)
  115. return null;
  116. // Give preference to the grid the entity is currently on.
  117. // This does not guarantee that if the probability fails that the owner's grid won't be picked.
  118. // In reality the probability is higher and depends on the number of grids.
  119. if (userXform.GridUid != null && TryComp<MapGridComponent>(userXform.GridUid, out var gridComp))
  120. {
  121. var userGrid = new Entity<MapGridComponent>(userXform.GridUid.Value, gridComp);
  122. if (_random.Prob(0.5f))
  123. {
  124. _targetGrids.Remove(userGrid);
  125. targetGrid = userGrid;
  126. }
  127. }
  128. if (targetGrid == null)
  129. targetGrid = _random.GetRandom().PickAndTake(_targetGrids);
  130. EntityCoordinates? targetCoords = null;
  131. do
  132. {
  133. var valid = false;
  134. var range = (float) Math.Sqrt(radius);
  135. var box = Box2.CenteredAround(userCoords.Position, new Vector2(range, range));
  136. var tilesInRange = _mapSystem.GetTilesEnumerator(targetGrid.Value.Owner, targetGrid.Value.Comp, box, false);
  137. var tileList = new ValueList<Vector2i>();
  138. while (tilesInRange.MoveNext(out var tile))
  139. {
  140. tileList.Add(tile.GridIndices);
  141. }
  142. while (tileList.Count != 0)
  143. {
  144. var tile = tileList.RemoveSwap(_random.Next(tileList.Count));
  145. valid = true;
  146. foreach (var entity in _mapSystem.GetAnchoredEntities(targetGrid.Value.Owner, targetGrid.Value.Comp,
  147. tile))
  148. {
  149. if (!_physicsQuery.TryGetComponent(entity, out var body))
  150. continue;
  151. if (body.BodyType != BodyType.Static ||
  152. !body.Hard ||
  153. (body.CollisionLayer & (int) CollisionGroup.MobMask) == 0)
  154. continue;
  155. valid = false;
  156. break;
  157. }
  158. if (valid)
  159. {
  160. targetCoords = new EntityCoordinates(targetGrid.Value.Owner,
  161. _mapSystem.TileCenterToVector(targetGrid.Value, tile));
  162. break;
  163. }
  164. }
  165. if (valid || _targetGrids.Count == 0) // if we don't do the check here then PickAndTake will blow up on an empty set.
  166. break;
  167. targetGrid = _random.GetRandom().PickAndTake(_targetGrids);
  168. } while (true);
  169. return targetCoords;
  170. }
  171. private void OnDnaScramblerImplant(EntityUid uid, SubdermalImplantComponent component, UseDnaScramblerImplantEvent args)
  172. {
  173. if (component.ImplantedEntity is not { } ent)
  174. return;
  175. if (TryComp<HumanoidAppearanceComponent>(ent, out var humanoid))
  176. {
  177. var newProfile = HumanoidCharacterProfile.RandomWithSpecies(humanoid.Species);
  178. _humanoidAppearance.LoadProfile(ent, newProfile, humanoid);
  179. _metaData.SetEntityName(ent, newProfile.Name, raiseEvents: false); // raising events would update ID card, station record, etc.
  180. // If the entity has the respecive components, then scramble the dna and fingerprint strings
  181. _forensicsSystem.RandomizeDNA(ent);
  182. _forensicsSystem.RandomizeFingerprint(ent);
  183. RemComp<DetailExaminableComponent>(ent); // remove MRP+ custom description if one exists
  184. _identity.QueueIdentityUpdate(ent); // manually queue identity update since we don't raise the event
  185. _popup.PopupEntity(Loc.GetString("scramble-implant-activated-popup"), ent, ent);
  186. }
  187. args.Handled = true;
  188. QueueDel(uid);
  189. }
  190. }