MedibotSystem.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Content.Shared.Chemistry.EntitySystems;
  2. using Content.Shared.Damage;
  3. using Content.Shared.DoAfter;
  4. using Content.Shared.Emag.Components;
  5. using Content.Shared.Emag.Systems;
  6. using Content.Shared.Interaction;
  7. using Content.Shared.Mobs;
  8. using Content.Shared.Mobs.Components;
  9. using Content.Shared.NPC.Components;
  10. using Content.Shared.Popups;
  11. using Robust.Shared.Audio.Systems;
  12. using Robust.Shared.Serialization;
  13. using System.Diagnostics.CodeAnalysis;
  14. namespace Content.Shared.Silicons.Bots;
  15. /// <summary>
  16. /// Handles emagging medibots and provides api.
  17. /// </summary>
  18. public sealed class MedibotSystem : EntitySystem
  19. {
  20. [Dependency] private readonly SharedAudioSystem _audio = default!;
  21. [Dependency] private readonly EmagSystem _emag = default!;
  22. [Dependency] private SharedInteractionSystem _interaction = default!;
  23. [Dependency] private SharedSolutionContainerSystem _solutionContainer = default!;
  24. [Dependency] private SharedPopupSystem _popup = default!;
  25. [Dependency] private SharedDoAfterSystem _doAfter = default!;
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. SubscribeLocalEvent<EmaggableMedibotComponent, GotEmaggedEvent>(OnEmagged);
  30. SubscribeLocalEvent<MedibotComponent, UserActivateInWorldEvent>(OnInteract);
  31. SubscribeLocalEvent<MedibotComponent, MedibotInjectDoAfterEvent>(OnInject);
  32. }
  33. private void OnEmagged(EntityUid uid, EmaggableMedibotComponent comp, ref GotEmaggedEvent args)
  34. {
  35. if (!_emag.CompareFlag(args.Type, EmagType.Interaction))
  36. return;
  37. if (_emag.CheckFlag(uid, EmagType.Interaction))
  38. return;
  39. if (!TryComp<MedibotComponent>(uid, out var medibot))
  40. return;
  41. foreach (var (state, treatment) in comp.Replacements)
  42. {
  43. medibot.Treatments[state] = treatment;
  44. }
  45. args.Handled = true;
  46. }
  47. private void OnInteract(Entity<MedibotComponent> medibot, ref UserActivateInWorldEvent args)
  48. {
  49. if (!CheckInjectable(medibot!, args.Target, true)) return;
  50. _doAfter.TryStartDoAfter(new DoAfterArgs(EntityManager, args.User, 2f, new MedibotInjectDoAfterEvent(), args.User, args.Target)
  51. {
  52. BlockDuplicate = true,
  53. BreakOnMove = true,
  54. });
  55. }
  56. private void OnInject(EntityUid uid, MedibotComponent comp, ref MedibotInjectDoAfterEvent args)
  57. {
  58. if (args.Cancelled) return;
  59. if (args.Target is { } target)
  60. TryInject(uid, target);
  61. }
  62. /// <summary>
  63. /// Get a treatment for a given mob state.
  64. /// </summary>
  65. /// <remarks>
  66. /// This only exists because allowing other execute would allow modifying the dictionary, and Read access does not cover TryGetValue.
  67. /// </remarks>
  68. public bool TryGetTreatment(MedibotComponent comp, MobState state, [NotNullWhen(true)] out MedibotTreatment? treatment)
  69. {
  70. return comp.Treatments.TryGetValue(state, out treatment);
  71. }
  72. /// <summary>
  73. /// Checks if the target can be injected.
  74. /// </summary>
  75. public bool CheckInjectable(Entity<MedibotComponent?> medibot, EntityUid target, bool manual = false)
  76. {
  77. if (!Resolve(medibot, ref medibot.Comp, false)) return false;
  78. if (HasComp<NPCRecentlyInjectedComponent>(target))
  79. {
  80. _popup.PopupClient(Loc.GetString("medibot-recently-injected"), medibot, medibot);
  81. return false;
  82. }
  83. if (!TryComp<MobStateComponent>(target, out var mobState)) return false;
  84. if (!TryComp<DamageableComponent>(target, out var damageable)) return false;
  85. if (!_solutionContainer.TryGetInjectableSolution(target, out _, out _)) return false;
  86. if (mobState.CurrentState != MobState.Alive && mobState.CurrentState != MobState.Critical)
  87. {
  88. _popup.PopupClient(Loc.GetString("medibot-target-dead"), medibot, medibot);
  89. return false;
  90. }
  91. var total = damageable.TotalDamage;
  92. if (total == 0 && !HasComp<EmaggedComponent>(medibot))
  93. {
  94. _popup.PopupClient(Loc.GetString("medibot-target-healthy"), medibot, medibot);
  95. return false;
  96. }
  97. if (!TryGetTreatment(medibot.Comp, mobState.CurrentState, out var treatment) || !treatment.IsValid(total) && !manual) return false;
  98. return true;
  99. }
  100. /// <summary>
  101. /// Tries to inject the target.
  102. /// </summary>
  103. public bool TryInject(Entity<MedibotComponent?> medibot, EntityUid target)
  104. {
  105. if (!Resolve(medibot, ref medibot.Comp, false)) return false;
  106. if (!_interaction.InRangeUnobstructed(medibot.Owner, target)) return false;
  107. if (!TryComp<MobStateComponent>(target, out var mobState)) return false;
  108. if (!TryGetTreatment(medibot.Comp, mobState.CurrentState, out var treatment)) return false;
  109. if (!_solutionContainer.TryGetInjectableSolution(target, out var injectable, out _)) return false;
  110. EnsureComp<NPCRecentlyInjectedComponent>(target);
  111. _solutionContainer.TryAddReagent(injectable.Value, treatment.Reagent, treatment.Quantity, out _);
  112. _popup.PopupEntity(Loc.GetString("hypospray-component-feel-prick-message"), target, target);
  113. _popup.PopupClient(Loc.GetString("medibot-target-injected"), medibot, medibot);
  114. _audio.PlayPredicted(medibot.Comp.InjectSound, medibot, medibot);
  115. return true;
  116. }
  117. }
  118. [Serializable, NetSerializable]
  119. public sealed partial class MedibotInjectDoAfterEvent : SimpleDoAfterEvent { }