1
0

ActionBlockerSystem.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Content.Shared.Body.Events;
  2. using Content.Shared.Emoting;
  3. using Content.Shared.Hands;
  4. using Content.Shared.Interaction;
  5. using Content.Shared.Interaction.Components;
  6. using Content.Shared.Interaction.Events;
  7. using Content.Shared.Item;
  8. using Content.Shared.Movement.Components;
  9. using Content.Shared.Movement.Events;
  10. using Content.Shared.Speech;
  11. using Content.Shared.Throwing;
  12. using Content.Shared.Weapons.Melee;
  13. using JetBrains.Annotations;
  14. using Robust.Shared.Containers;
  15. namespace Content.Shared.ActionBlocker
  16. {
  17. /// <summary>
  18. /// Utility methods to check if a specific entity is allowed to perform an action.
  19. /// </summary>
  20. [UsedImplicitly]
  21. public sealed class ActionBlockerSystem : EntitySystem
  22. {
  23. [Dependency] private readonly SharedContainerSystem _container = default!;
  24. private EntityQuery<ComplexInteractionComponent> _complexInteractionQuery;
  25. public override void Initialize()
  26. {
  27. base.Initialize();
  28. _complexInteractionQuery = GetEntityQuery<ComplexInteractionComponent>();
  29. SubscribeLocalEvent<InputMoverComponent, ComponentStartup>(OnMoverStartup);
  30. }
  31. private void OnMoverStartup(EntityUid uid, InputMoverComponent component, ComponentStartup args)
  32. {
  33. UpdateCanMove(uid, component);
  34. }
  35. public bool CanMove(EntityUid uid, InputMoverComponent? component = null)
  36. {
  37. return Resolve(uid, ref component, false) && component.CanMove;
  38. }
  39. public bool UpdateCanMove(EntityUid uid, InputMoverComponent? component = null)
  40. {
  41. if (!Resolve(uid, ref component, false))
  42. return false;
  43. var ev = new UpdateCanMoveEvent(uid);
  44. RaiseLocalEvent(uid, ev);
  45. if (component.CanMove == ev.Cancelled)
  46. Dirty(uid, component);
  47. component.CanMove = !ev.Cancelled;
  48. return !ev.Cancelled;
  49. }
  50. /// <summary>
  51. /// Checks if a given entity is able to do specific complex interactions.
  52. /// This is used to gate manipulation to general humanoids. If a mouse shouldn't be able to do something, then it's complex.
  53. /// </summary>
  54. public bool CanComplexInteract(EntityUid user)
  55. {
  56. return _complexInteractionQuery.HasComp(user);
  57. }
  58. /// <summary>
  59. /// Raises an event directed at both the user and the target entity to check whether a user is capable of
  60. /// interacting with this entity.
  61. /// </summary>
  62. /// <remarks>
  63. /// If this is a generic interaction without a target (e.g., stop-drop-and-roll when burning), the target
  64. /// may be null. Note that this is checked by <see cref="SharedInteractionSystem"/>. In the majority of
  65. /// cases, systems that provide interactions will not need to check this themselves, though they may need to
  66. /// check other blockers like <see cref="CanPickup(EntityUid)"/>
  67. /// </remarks>
  68. /// <returns></returns>
  69. public bool CanInteract(EntityUid user, EntityUid? target)
  70. {
  71. if (!CanConsciouslyPerformAction(user))
  72. return false;
  73. var ev = new InteractionAttemptEvent(user, target);
  74. RaiseLocalEvent(user, ref ev);
  75. if (ev.Cancelled)
  76. return false;
  77. if (target == null || target == user)
  78. return true;
  79. var targetEv = new GettingInteractedWithAttemptEvent(user, target);
  80. RaiseLocalEvent(target.Value, ref targetEv);
  81. return !targetEv.Cancelled;
  82. }
  83. /// <summary>
  84. /// Can a user utilize the entity that they are currently holding in their hands.
  85. /// </summary>>
  86. /// <remarks>
  87. /// This event is automatically checked by <see cref="SharedInteractionSystem"/> for any interactions that
  88. /// involve using a held entity. In the majority of cases, systems that provide interactions will not need
  89. /// to check this themselves.
  90. /// </remarks>
  91. public bool CanUseHeldEntity(EntityUid user, EntityUid used)
  92. {
  93. var useEv = new UseAttemptEvent(user, used);
  94. RaiseLocalEvent(user, useEv);
  95. if (useEv.Cancelled)
  96. return false;
  97. var usedEv = new GettingUsedAttemptEvent(user);
  98. RaiseLocalEvent(used, usedEv);
  99. return !usedEv.Cancelled;
  100. }
  101. /// <summary>
  102. /// Whether a user conscious to perform an action.
  103. /// </summary>
  104. /// <remarks>
  105. /// This should be used when you want a much more permissive check than <see cref="CanInteract"/>
  106. /// </remarks>
  107. public bool CanConsciouslyPerformAction(EntityUid user)
  108. {
  109. var ev = new ConsciousAttemptEvent(user);
  110. RaiseLocalEvent(user, ref ev);
  111. return !ev.Cancelled;
  112. }
  113. public bool CanThrow(EntityUid user, EntityUid itemUid)
  114. {
  115. var ev = new ThrowAttemptEvent(user, itemUid);
  116. RaiseLocalEvent(user, ev);
  117. if (ev.Cancelled)
  118. return false;
  119. var itemEv = new ThrowItemAttemptEvent(user);
  120. RaiseLocalEvent(itemUid, ref itemEv);
  121. return !itemEv.Cancelled;
  122. }
  123. public bool CanSpeak(EntityUid uid)
  124. {
  125. // This one is used as broadcast
  126. var ev = new SpeakAttemptEvent(uid);
  127. RaiseLocalEvent(uid, ev, true);
  128. return !ev.Cancelled;
  129. }
  130. public bool CanDrop(EntityUid uid)
  131. {
  132. var ev = new DropAttemptEvent();
  133. RaiseLocalEvent(uid, ev);
  134. return !ev.Cancelled;
  135. }
  136. public bool CanPickup(EntityUid user, EntityUid item)
  137. {
  138. var userEv = new PickupAttemptEvent(user, item);
  139. RaiseLocalEvent(user, userEv);
  140. if (userEv.Cancelled)
  141. return false;
  142. var itemEv = new GettingPickedUpAttemptEvent(user, item);
  143. RaiseLocalEvent(item, itemEv);
  144. return !itemEv.Cancelled;
  145. }
  146. public bool CanEmote(EntityUid uid)
  147. {
  148. // This one is used as broadcast
  149. var ev = new EmoteAttemptEvent(uid);
  150. RaiseLocalEvent(uid, ev, true);
  151. return !ev.Cancelled;
  152. }
  153. public bool CanAttack(EntityUid uid, EntityUid? target = null, Entity<MeleeWeaponComponent>? weapon = null, bool disarm = false)
  154. {
  155. // If target is in a container can we attack
  156. if (target != null && _container.IsEntityInContainer(target.Value))
  157. {
  158. return false;
  159. }
  160. _container.TryGetOuterContainer(uid, Transform(uid), out var outerContainer);
  161. // If we're in a container can we attack the target.
  162. if (target != null && target != outerContainer?.Owner && _container.IsEntityInContainer(uid))
  163. {
  164. var containerEv = new CanAttackFromContainerEvent(uid, target);
  165. RaiseLocalEvent(uid, containerEv);
  166. if (!containerEv.CanAttack)
  167. return false;
  168. }
  169. var ev = new AttackAttemptEvent(uid, target, weapon, disarm);
  170. RaiseLocalEvent(uid, ev);
  171. if (ev.Cancelled)
  172. return false;
  173. if (target == null)
  174. return true;
  175. var tev = new GettingAttackedAttemptEvent(uid, weapon, disarm);
  176. RaiseLocalEvent(target.Value, ref tev);
  177. return !tev.Cancelled;
  178. }
  179. public bool CanChangeDirection(EntityUid uid)
  180. {
  181. var ev = new ChangeDirectionAttemptEvent(uid);
  182. RaiseLocalEvent(uid, ev);
  183. return !ev.Cancelled;
  184. }
  185. public bool CanShiver(EntityUid uid)
  186. {
  187. var ev = new ShiverAttemptEvent(uid);
  188. RaiseLocalEvent(uid, ref ev);
  189. return !ev.Cancelled;
  190. }
  191. public bool CanSweat(EntityUid uid)
  192. {
  193. var ev = new SweatAttemptEvent(uid);
  194. RaiseLocalEvent(uid, ref ev);
  195. return !ev.Cancelled;
  196. }
  197. }
  198. }