Client LuaCsForBarotrauma
CombatAction.cs
1 using Microsoft.Xna.Framework;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace Barotrauma
6 {
11  {
12  [Serialize(AIObjectiveCombat.CombatMode.Offensive, IsPropertySaveable.Yes, description: $"What kind of combat mode should the NPC switch to (Defensive, Offensive, Arrest, Retreat, None)?")]
14 
15  [Serialize(false, IsPropertySaveable.Yes, description: "Did this NPC start the fight (as an aggressor)? Attacking instigators doesn't reduce reputation or trigger outpost security.")]
16  public bool IsInstigator { get; set; }
17 
18  [Serialize(AIObjectiveCombat.CombatMode.None, IsPropertySaveable.Yes, description: "How do guards react to this character attacking others?")]
20 
21  [Serialize(AIObjectiveCombat.CombatMode.None, IsPropertySaveable.Yes, description: "How do other NPCs react to this character attacking others?")]
23 
24  [Serialize("", IsPropertySaveable.Yes, description: "The tag of the NPC to switch to combat mode.")]
25  public Identifier NPCTag { get; set; }
26 
27  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the character the NPC should attack.")]
28  public Identifier EnemyTag { get; set; }
29 
30  [Serialize(120.0f, IsPropertySaveable.Yes, description: "How long it takes for the NPC to \"cool down\" (stop attacking).")]
31  public float CoolDown { get; set; }
32 
33  private bool isFinished = false;
34 
35 
36  private IEnumerable<Character> affectedNpcs = null;
37 
38  public CombatAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
39 
40  public override void Update(float deltaTime)
41  {
42  if (isFinished) { return; }
43 
44  affectedNpcs = ParentEvent.GetTargets(NPCTag).Where(e => e is Character).Select(e => e as Character);
45 
46  foreach (var npc in affectedNpcs)
47  {
48  if (npc.Removed) { continue; }
49  if (npc.AIController is not HumanAIController humanAiController) { continue; }
50 
51  Character enemy = null;
52  float closestDist = float.MaxValue;
53  foreach (Entity target in ParentEvent.GetTargets(EnemyTag))
54  {
55  if (target is not Character character) { continue; }
56  float dist = Vector2.DistanceSquared(npc.WorldPosition, target.WorldPosition);
57  if (dist < closestDist)
58  {
59  enemy = character;
60  closestDist = dist;
61  }
62  }
63  if (enemy == null) { continue; }
64 
65  npc.CombatAction = this;
66 
67  var objectiveManager = humanAiController.ObjectiveManager;
68  foreach (var goToObjective in objectiveManager.GetActiveObjectives<AIObjectiveGoTo>())
69  {
70  goToObjective.Abandon = true;
71  }
72  objectiveManager.AddObjective(new AIObjectiveCombat(npc, enemy, CombatMode, objectiveManager, coolDown: CoolDown));
73  }
74  isFinished = true;
75  }
76 
77  public override bool IsFinished(ref string goTo)
78  {
79  return isFinished;
80  }
81 
82  public override void Reset()
83  {
84  if (affectedNpcs != null)
85  {
86  foreach (var npc in affectedNpcs)
87  {
88  if (npc.Removed || npc.AIController is not HumanAIController humanAiController) { continue; }
89  foreach (var combatObjective in humanAiController.ObjectiveManager.GetActiveObjectives<AIObjectiveCombat>())
90  {
91  combatObjective.Abandon = true;
92  }
93  }
94  affectedNpcs = null;
95  }
96  isFinished = false;
97  }
98 
99  public override string ToDebugString()
100  {
101  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(CombatAction)} -> (Cooldown: {CoolDown.ColorizeObject()}, CombatMode: {CombatMode.ColorizeObject()}, NPCTag: {NPCTag.ColorizeObject()}, EnemyTag: {EnemyTag.ColorizeObject()})";
102  }
103  }
104 }
Makes an NPC switch to a combat state (with options for different kinds of behaviors,...
Definition: CombatAction.cs:11
AIObjectiveCombat.CombatMode WitnessReaction
Definition: CombatAction.cs:22
override void Update(float deltaTime)
Definition: CombatAction.cs:40
override bool IsFinished(ref string goTo)
Has the action finished.
Definition: CombatAction.cs:77
AIObjectiveCombat.CombatMode GuardReaction
Definition: CombatAction.cs:19
AIObjectiveCombat.CombatMode CombatMode
Definition: CombatAction.cs:13
CombatAction(ScriptedEvent parentEvent, ContentXElement element)
Definition: CombatAction.cs:38
override string ToDebugString()
Rich test to display in debugdraw
Definition: CombatAction.cs:99
override void Reset()
Definition: CombatAction.cs:82
virtual Vector2 WorldPosition
Definition: Entity.cs:49
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:106
IEnumerable< Entity > GetTargets(Identifier tag)