MedBookSystem.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using Content.Server.Body.Components;
  2. using Content.Server.Medical.Components;
  3. using Content.Server.PowerCell;
  4. using Content.Server.Temperature.Components;
  5. using Content.Shared.Traits.Assorted;
  6. using Content.Shared.Chemistry.EntitySystems;
  7. using Content.Shared.Damage;
  8. using Content.Shared.DoAfter;
  9. using Content.Shared.IdentityManagement;
  10. using Content.Shared.Interaction;
  11. using Content.Shared.Interaction.Events;
  12. using Content.Shared.Item.ItemToggle;
  13. using Content.Shared.Item.ItemToggle.Components;
  14. using Content.Shared.MedicalScanner;
  15. using Content.Shared.Mobs.Components;
  16. using Content.Shared.Popups;
  17. using Robust.Server.GameObjects;
  18. using Robust.Shared.Containers;
  19. using Robust.Shared.Timing;
  20. namespace Content.Server.Medical;
  21. public sealed class MedBookSystem : EntitySystem
  22. {
  23. [Dependency] private readonly IGameTiming _timing = default!;
  24. [Dependency] private readonly PowerCellSystem _cell = default!;
  25. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  26. [Dependency] private readonly ItemToggleSystem _toggle = default!;
  27. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainerSystem = default!;
  28. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  29. [Dependency] private readonly TransformSystem _transformSystem = default!;
  30. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  31. public override void Initialize()
  32. {
  33. SubscribeLocalEvent<MedBookComponent, AfterInteractEvent>(OnAfterInteract);
  34. SubscribeLocalEvent<MedBookComponent, MedBookDoAfterEvent>(OnDoAfter);
  35. SubscribeLocalEvent<MedBookComponent, EntGotInsertedIntoContainerMessage>(OnInsertedIntoContainer);
  36. SubscribeLocalEvent<MedBookComponent, ItemToggledEvent>(OnToggled);
  37. SubscribeLocalEvent<MedBookComponent, DroppedEvent>(OnDropped);
  38. }
  39. public override void Update(float frameTime)
  40. {
  41. var analyzerQuery = EntityQueryEnumerator<MedBookComponent, TransformComponent>();
  42. while (analyzerQuery.MoveNext(out var uid, out var component, out var transform))
  43. {
  44. //Update rate limited to 1 second
  45. if (component.NextUpdate > _timing.CurTime)
  46. continue;
  47. if (component.ScannedEntity is not { } patient)
  48. continue;
  49. if (Deleted(patient))
  50. {
  51. StopAnalyzingEntity((uid, component), patient);
  52. continue;
  53. }
  54. component.NextUpdate = _timing.CurTime + component.UpdateInterval;
  55. //Get distance between health analyzer and the scanned entity
  56. var patientCoordinates = Transform(patient).Coordinates;
  57. if (!_transformSystem.InRange(patientCoordinates, transform.Coordinates, component.MaxScanRange))
  58. {
  59. //Range too far, disable updates
  60. StopAnalyzingEntity((uid, component), patient);
  61. continue;
  62. }
  63. UpdateScannedUser(uid, patient, true);
  64. }
  65. }
  66. /// <summary>
  67. /// Trigger the doafter for scanning
  68. /// </summary>
  69. private void OnAfterInteract(Entity<MedBookComponent> uid, ref AfterInteractEvent args)
  70. {
  71. if (args.Target == null || !args.CanReach || !HasComp<MobStateComponent>(args.Target) || !_cell.HasDrawCharge(uid, user: args.User))
  72. return;
  73. var doAfterCancelled = !_doAfterSystem.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, uid.Comp.ScanDelay, new MedBookDoAfterEvent(), uid, target: args.Target, used: uid)
  74. {
  75. NeedHand = true,
  76. BreakOnMove = true,
  77. });
  78. if (args.Target == args.User || doAfterCancelled || uid.Comp.Silent)
  79. return;
  80. var msg = Loc.GetString("medbook-popup-scan-target", ("user", Identity.Entity(args.User, EntityManager)));
  81. _popupSystem.PopupEntity(msg, args.Target.Value, args.Target.Value, PopupType.Medium);
  82. }
  83. private void OnDoAfter(Entity<MedBookComponent> uid, ref MedBookDoAfterEvent args)
  84. {
  85. if (args.Handled || args.Cancelled || args.Target == null || !_cell.HasDrawCharge(uid, user: args.User))
  86. return;
  87. OpenUserInterface(args.User, uid);
  88. BeginAnalyzingEntity(uid, args.Target.Value);
  89. args.Handled = true;
  90. }
  91. /// <summary>
  92. /// Turn off when placed into a storage item or moved between slots/hands
  93. /// </summary>
  94. private void OnInsertedIntoContainer(Entity<MedBookComponent> uid, ref EntGotInsertedIntoContainerMessage args)
  95. {
  96. if (uid.Comp.ScannedEntity is { } patient)
  97. _toggle.TryDeactivate(uid.Owner);
  98. }
  99. /// <summary>
  100. /// Disable continuous updates once turned off
  101. /// </summary>
  102. private void OnToggled(Entity<MedBookComponent> ent, ref ItemToggledEvent args)
  103. {
  104. if (!args.Activated && ent.Comp.ScannedEntity is { } patient)
  105. StopAnalyzingEntity(ent, patient);
  106. }
  107. /// <summary>
  108. /// Turn off the analyser when dropped
  109. /// </summary>
  110. private void OnDropped(Entity<MedBookComponent> uid, ref DroppedEvent args)
  111. {
  112. if (uid.Comp.ScannedEntity is { } patient)
  113. _toggle.TryDeactivate(uid.Owner);
  114. }
  115. private void OpenUserInterface(EntityUid user, EntityUid analyzer)
  116. {
  117. if (!_uiSystem.HasUi(analyzer, MedBookUiKey.Key))
  118. return;
  119. _uiSystem.OpenUi(analyzer, MedBookUiKey.Key, user);
  120. }
  121. /// <summary>
  122. /// Mark the entity as having its health analyzed, and link the analyzer to it
  123. /// </summary>
  124. /// <param name="medBook">The health analyzer that should receive the updates</param>
  125. /// <param name="target">The entity to start analyzing</param>
  126. private void BeginAnalyzingEntity(Entity<MedBookComponent> medBook, EntityUid target)
  127. {
  128. //Link the health analyzer to the scanned entity
  129. medBook.Comp.ScannedEntity = target;
  130. _toggle.TryActivate(medBook.Owner);
  131. UpdateScannedUser(medBook, target, true);
  132. }
  133. /// <summary>
  134. /// Remove the analyzer from the active list, and remove the component if it has no active analyzers
  135. /// </summary>
  136. /// <param name="medBook">The health analyzer that's receiving the updates</param>
  137. /// <param name="target">The entity to analyze</param>
  138. private void StopAnalyzingEntity(Entity<MedBookComponent> medBook, EntityUid target)
  139. {
  140. //Unlink the analyzer
  141. medBook.Comp.ScannedEntity = null;
  142. _toggle.TryDeactivate(medBook.Owner);
  143. UpdateScannedUser(medBook, target, false);
  144. }
  145. /// <summary>
  146. /// Send an update for the target to the medBook
  147. /// </summary>
  148. /// <param name="medBook">The health analyzer</param>
  149. /// <param name="target">The entity being scanned</param>
  150. /// <param name="scanMode">True makes the UI show ACTIVE, False makes the UI show INACTIVE</param>
  151. public void UpdateScannedUser(EntityUid medBook, EntityUid target, bool scanMode)
  152. {
  153. if (!_uiSystem.HasUi(medBook, MedBookUiKey.Key))
  154. return;
  155. if (!HasComp<DamageableComponent>(target))
  156. return;
  157. var bodyTemperature = float.NaN;
  158. if (TryComp<TemperatureComponent>(target, out var temp))
  159. bodyTemperature = temp.CurrentTemperature;
  160. var bloodAmount = float.NaN;
  161. var bleeding = false;
  162. var unrevivable = false;
  163. if (TryComp<BloodstreamComponent>(target, out var bloodstream) &&
  164. _solutionContainerSystem.ResolveSolution(target, bloodstream.BloodSolutionName,
  165. ref bloodstream.BloodSolution, out var bloodSolution))
  166. {
  167. bloodAmount = bloodSolution.FillFraction;
  168. bleeding = bloodstream.BleedAmount > 0;
  169. }
  170. if (TryComp<UnrevivableComponent>(target, out var unrevivableComp) && unrevivableComp.Analyzable)
  171. unrevivable = true;
  172. _uiSystem.ServerSendUiMessage(medBook, MedBookUiKey.Key, new MedBookScannedUserMessage(
  173. GetNetEntity(target),
  174. bodyTemperature,
  175. bloodAmount,
  176. scanMode,
  177. bleeding,
  178. unrevivable
  179. ));
  180. }
  181. }