ActionOnInteractSystem.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Linq;
  2. using Content.Shared.Actions;
  3. using Content.Shared.Interaction;
  4. using Robust.Shared.Random;
  5. using Robust.Shared.Timing;
  6. namespace Content.Server.Actions;
  7. /// <summary>
  8. /// This System handled interactions for the <see cref="ActionOnInteractComponent"/>.
  9. /// </summary>
  10. public sealed class ActionOnInteractSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly IGameTiming _timing = default!;
  14. [Dependency] private readonly SharedActionsSystem _actions = default!;
  15. [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<ActionOnInteractComponent, ActivateInWorldEvent>(OnActivate);
  20. SubscribeLocalEvent<ActionOnInteractComponent, AfterInteractEvent>(OnAfterInteract);
  21. SubscribeLocalEvent<ActionOnInteractComponent, MapInitEvent>(OnMapInit);
  22. }
  23. private void OnMapInit(EntityUid uid, ActionOnInteractComponent component, MapInitEvent args)
  24. {
  25. if (component.Actions == null)
  26. return;
  27. var comp = EnsureComp<ActionsContainerComponent>(uid);
  28. foreach (var id in component.Actions)
  29. {
  30. _actionContainer.AddAction(uid, id, comp);
  31. }
  32. }
  33. private void OnActivate(EntityUid uid, ActionOnInteractComponent component, ActivateInWorldEvent args)
  34. {
  35. if (args.Handled || !args.Complex)
  36. return;
  37. if (component.ActionEntities is not {} actionEnts)
  38. {
  39. if (!TryComp<ActionsContainerComponent>(uid, out var actionsContainerComponent))
  40. return;
  41. actionEnts = actionsContainerComponent.Container.ContainedEntities.ToList();
  42. }
  43. var options = GetValidActions<InstantActionComponent>(actionEnts);
  44. if (options.Count == 0)
  45. return;
  46. var (actId, act) = _random.Pick(options);
  47. _actions.PerformAction(args.User, null, actId, act, act.Event, _timing.CurTime, false);
  48. args.Handled = true;
  49. }
  50. private void OnAfterInteract(EntityUid uid, ActionOnInteractComponent component, AfterInteractEvent args)
  51. {
  52. if (args.Handled)
  53. return;
  54. if (component.ActionEntities is not {} actionEnts)
  55. {
  56. if (!TryComp<ActionsContainerComponent>(uid, out var actionsContainerComponent))
  57. return;
  58. actionEnts = actionsContainerComponent.Container.ContainedEntities.ToList();
  59. }
  60. // First, try entity target actions
  61. if (args.Target != null)
  62. {
  63. var entOptions = GetValidActions<EntityTargetActionComponent>(actionEnts, args.CanReach);
  64. for (var i = entOptions.Count - 1; i >= 0; i--)
  65. {
  66. var action = entOptions[i];
  67. if (!_actions.ValidateEntityTarget(args.User, args.Target.Value, action))
  68. entOptions.RemoveAt(i);
  69. }
  70. if (entOptions.Count > 0)
  71. {
  72. var (entActId, entAct) = _random.Pick(entOptions);
  73. if (entAct.Event != null)
  74. {
  75. entAct.Event.Target = args.Target.Value;
  76. }
  77. _actions.PerformAction(args.User, null, entActId, entAct, entAct.Event, _timing.CurTime, false);
  78. args.Handled = true;
  79. return;
  80. }
  81. }
  82. // Then EntityWorld target actions
  83. var entWorldOptions = GetValidActions<EntityWorldTargetActionComponent>(actionEnts, args.CanReach);
  84. for (var i = entWorldOptions.Count - 1; i >= 0; i--)
  85. {
  86. var action = entWorldOptions[i];
  87. if (!_actions.ValidateEntityWorldTarget(args.User, args.Target, args.ClickLocation, action))
  88. entWorldOptions.RemoveAt(i);
  89. }
  90. if (entWorldOptions.Count > 0)
  91. {
  92. var (entActId, entAct) = _random.Pick(entWorldOptions);
  93. if (entAct.Event != null)
  94. {
  95. entAct.Event.Entity = args.Target;
  96. entAct.Event.Coords = args.ClickLocation;
  97. }
  98. _actions.PerformAction(args.User, null, entActId, entAct, entAct.Event, _timing.CurTime, false);
  99. args.Handled = true;
  100. return;
  101. }
  102. // else: try world target actions
  103. var options = GetValidActions<WorldTargetActionComponent>(component.ActionEntities, args.CanReach);
  104. for (var i = options.Count - 1; i >= 0; i--)
  105. {
  106. var action = options[i];
  107. if (!_actions.ValidateWorldTarget(args.User, args.ClickLocation, action))
  108. options.RemoveAt(i);
  109. }
  110. if (options.Count == 0)
  111. return;
  112. var (actId, act) = _random.Pick(options);
  113. if (act.Event != null)
  114. {
  115. act.Event.Target = args.ClickLocation;
  116. }
  117. _actions.PerformAction(args.User, null, actId, act, act.Event, _timing.CurTime, false);
  118. args.Handled = true;
  119. }
  120. private List<(EntityUid Id, T Comp)> GetValidActions<T>(List<EntityUid>? actions, bool canReach = true) where T : BaseActionComponent
  121. {
  122. var valid = new List<(EntityUid Id, T Comp)>();
  123. if (actions == null)
  124. return valid;
  125. foreach (var id in actions)
  126. {
  127. if (!_actions.TryGetActionData(id, out var baseAction) ||
  128. baseAction as T is not { } action ||
  129. !_actions.ValidAction(action, canReach))
  130. {
  131. continue;
  132. }
  133. valid.Add((id, action));
  134. }
  135. return valid;
  136. }
  137. }