MeleeOperator.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Combat.Melee;
  8. /// <summary>
  9. /// Attacks the specified key in melee combat.
  10. /// </summary>
  11. public sealed partial class MeleeOperator : HTNOperator, IHtnConditionalShutdown
  12. {
  13. [Dependency] private readonly IEntityManager _entManager = default!;
  14. /// <summary>
  15. /// When to shut the task down.
  16. /// </summary>
  17. [DataField("shutdownState")]
  18. public HTNPlanState ShutdownState { get; private set; } = HTNPlanState.TaskFinished;
  19. /// <summary>
  20. /// Key that contains the target entity.
  21. /// </summary>
  22. [DataField("targetKey", required: true)]
  23. public string TargetKey = default!;
  24. /// <summary>
  25. /// Minimum damage state that the target has to be in for us to consider attacking.
  26. /// </summary>
  27. [DataField("targetState")]
  28. public MobState TargetState = MobState.Alive;
  29. // Like movement we add a component and pass it off to the dedicated system.
  30. public override void Startup(NPCBlackboard blackboard)
  31. {
  32. base.Startup(blackboard);
  33. var melee = _entManager.EnsureComponent<NPCMeleeCombatComponent>(blackboard.GetValue<EntityUid>(NPCBlackboard.Owner));
  34. melee.MissChance = blackboard.GetValueOrDefault<float>(NPCBlackboard.MeleeMissChance, _entManager);
  35. melee.Target = blackboard.GetValue<EntityUid>(TargetKey);
  36. }
  37. public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  38. CancellationToken cancelToken)
  39. {
  40. // Don't attack if they're already as wounded as we want them.
  41. if (!blackboard.TryGetValue<EntityUid>(TargetKey, out var target, _entManager))
  42. {
  43. return (false, null);
  44. }
  45. if (_entManager.TryGetComponent<MobStateComponent>(target, out var mobState) &&
  46. mobState.CurrentState > TargetState)
  47. {
  48. return (false, null);
  49. }
  50. return (true, null);
  51. }
  52. public void ConditionalShutdown(NPCBlackboard blackboard)
  53. {
  54. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  55. _entManager.System<SharedCombatModeSystem>().SetInCombatMode(owner, false);
  56. _entManager.RemoveComponent<NPCMeleeCombatComponent>(owner);
  57. blackboard.Remove<EntityUid>(TargetKey);
  58. }
  59. public override void TaskShutdown(NPCBlackboard blackboard, HTNOperatorStatus status)
  60. {
  61. base.TaskShutdown(blackboard, status);
  62. ConditionalShutdown(blackboard);
  63. }
  64. public override void PlanShutdown(NPCBlackboard blackboard)
  65. {
  66. base.PlanShutdown(blackboard);
  67. ConditionalShutdown(blackboard);
  68. }
  69. public override HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTime)
  70. {
  71. base.Update(blackboard, frameTime);
  72. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  73. HTNOperatorStatus status;
  74. if (_entManager.TryGetComponent<NPCMeleeCombatComponent>(owner, out var combat) &&
  75. blackboard.TryGetValue<EntityUid>(TargetKey, out var target, _entManager) &&
  76. target != EntityUid.Invalid)
  77. {
  78. combat.Target = target;
  79. // Success
  80. if (_entManager.TryGetComponent<MobStateComponent>(target, out var mobState) &&
  81. mobState.CurrentState > TargetState)
  82. {
  83. status = HTNOperatorStatus.Finished;
  84. }
  85. else
  86. {
  87. switch (combat.Status)
  88. {
  89. case CombatStatus.TargetOutOfRange:
  90. case CombatStatus.Normal:
  91. status = HTNOperatorStatus.Continuing;
  92. break;
  93. default:
  94. status = HTNOperatorStatus.Failed;
  95. break;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. status = HTNOperatorStatus.Failed;
  102. }
  103. // Mark it as finished to continue the plan.
  104. if (status == HTNOperatorStatus.Continuing && ShutdownState == HTNPlanState.PlanFinished)
  105. {
  106. status = HTNOperatorStatus.Finished;
  107. }
  108. return status;
  109. }
  110. }