GunOperator.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Content.Server.NPC.Components;
  4. using Content.Shared.CombatMode;
  5. using Content.Shared.Mobs;
  6. using Content.Shared.Mobs.Components;
  7. using Robust.Shared.Audio;
  8. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Combat.Ranged;
  9. public sealed partial class GunOperator : HTNOperator, IHtnConditionalShutdown
  10. {
  11. [Dependency] private readonly IEntityManager _entManager = default!;
  12. [DataField("shutdownState")]
  13. public HTNPlanState ShutdownState { get; private set; } = HTNPlanState.TaskFinished;
  14. /// <summary>
  15. /// Key that contains the target entity.
  16. /// </summary>
  17. [DataField("targetKey", required: true)]
  18. public string TargetKey = default!;
  19. /// <summary>
  20. /// Minimum damage state that the target has to be in for us to consider attacking.
  21. /// </summary>
  22. [DataField("targetState")]
  23. public MobState TargetState = MobState.Alive;
  24. /// <summary>
  25. /// Do we require line of sight of the target before failing.
  26. /// </summary>
  27. [DataField("requireLOS")]
  28. public bool RequireLOS = false;
  29. /// <summary>
  30. /// If true, only opaque objects will block line of sight.
  31. /// </summary>
  32. [DataField("opaqueKey")]
  33. public bool UseOpaqueForLOSChecks = false;
  34. // Like movement we add a component and pass it off to the dedicated system.
  35. public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  36. CancellationToken cancelToken)
  37. {
  38. // Don't attack if they're already as wounded as we want them.
  39. if (!blackboard.TryGetValue<EntityUid>(TargetKey, out var target, _entManager))
  40. {
  41. return (false, null);
  42. }
  43. if (_entManager.TryGetComponent<MobStateComponent>(target, out var mobState) &&
  44. mobState.CurrentState > TargetState)
  45. {
  46. return (false, null);
  47. }
  48. return (true, null);
  49. }
  50. public override void Startup(NPCBlackboard blackboard)
  51. {
  52. base.Startup(blackboard);
  53. var ranged = _entManager.EnsureComponent<NPCRangedCombatComponent>(blackboard.GetValue<EntityUid>(NPCBlackboard.Owner));
  54. ranged.Target = blackboard.GetValue<EntityUid>(TargetKey);
  55. ranged.UseOpaqueForLOSChecks = UseOpaqueForLOSChecks;
  56. if (blackboard.TryGetValue<float>(NPCBlackboard.RotateSpeed, out var rotSpeed, _entManager))
  57. {
  58. ranged.RotationSpeed = new Angle(rotSpeed);
  59. }
  60. if (blackboard.TryGetValue<SoundSpecifier>("SoundTargetInLOS", out var losSound, _entManager))
  61. {
  62. ranged.SoundTargetInLOS = losSound;
  63. }
  64. }
  65. public void ConditionalShutdown(NPCBlackboard blackboard)
  66. {
  67. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  68. _entManager.System<SharedCombatModeSystem>().SetInCombatMode(owner, false);
  69. _entManager.RemoveComponent<NPCRangedCombatComponent>(owner);
  70. blackboard.Remove<EntityUid>(TargetKey);
  71. }
  72. public override HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTime)
  73. {
  74. base.Update(blackboard, frameTime);
  75. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  76. HTNOperatorStatus status;
  77. if (_entManager.TryGetComponent<NPCRangedCombatComponent>(owner, out var combat) &&
  78. blackboard.TryGetValue<EntityUid>(TargetKey, out var target, _entManager))
  79. {
  80. combat.Target = target;
  81. // Success
  82. if (_entManager.TryGetComponent<MobStateComponent>(combat.Target, out var mobState) &&
  83. mobState.CurrentState > TargetState)
  84. {
  85. status = HTNOperatorStatus.Finished;
  86. }
  87. else
  88. {
  89. switch (combat.Status)
  90. {
  91. case CombatStatus.TargetUnreachable:
  92. status = HTNOperatorStatus.Failed;
  93. break;
  94. case CombatStatus.NotInSight:
  95. if (RequireLOS)
  96. status = HTNOperatorStatus.Failed;
  97. else
  98. status = HTNOperatorStatus.Continuing;
  99. break;
  100. case CombatStatus.Normal:
  101. status = HTNOperatorStatus.Continuing;
  102. break;
  103. default:
  104. status = HTNOperatorStatus.Failed;
  105. break;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. status = HTNOperatorStatus.Failed;
  112. }
  113. // Mark it as finished to continue the plan.
  114. if (status == HTNOperatorStatus.Continuing && ShutdownState == HTNPlanState.PlanFinished)
  115. {
  116. status = HTNOperatorStatus.Finished;
  117. }
  118. return status;
  119. }
  120. }