Client LuaCsForBarotrauma
NPCFollowAction.cs
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 namespace Barotrauma
5 {
10  {
11  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the NPC(s) that should follow the target.")]
12  public Identifier NPCTag { get; set; }
13 
14  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the target. Can be any type of entity: if it's a static one like a device or a hull, the NPC will just stay at the position of that target.")]
15  public Identifier TargetTag { get; set; }
16 
17  [Serialize(true, IsPropertySaveable.Yes, description: "Should the NPC start or stop following the target?")]
18  public bool Follow { get; set; }
19 
20  [Serialize(-1, IsPropertySaveable.Yes, description: "Maximum number of NPCs to target (e.g. you could choose to only make a specific number of security officers follow the player.)")]
21  public int MaxTargets { get; set; }
22 
23  [Serialize(true, IsPropertySaveable.Yes, description: "The event actions reset when a GoTo action makes the event jump to a different point. Should the NPC stop following the target when the event resets?")]
24  public bool AbandonOnReset { get; set; }
25 
26  private bool isFinished = false;
27 
28  public NPCFollowAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
29 
30 
31  private IEnumerable<Character> affectedNpcs;
32  private Entity target;
33 
34  public override void Update(float deltaTime)
35  {
36  if (isFinished) { return; }
37 
38  target = ParentEvent.GetTargets(TargetTag).FirstOrDefault();
39  if (target == null) { return; }
40 
41  int targetCount = 0;
42  affectedNpcs = ParentEvent.GetTargets(NPCTag).Where(c => c is Character).Select(c => c as Character);
43  foreach (var npc in affectedNpcs)
44  {
45  if (npc.Removed) { continue; }
46  if (npc.AIController is not HumanAIController humanAiController) { continue; }
47 
48  if (Follow)
49  {
50  var newObjective = new AIObjectiveGoTo(target, npc, humanAiController.ObjectiveManager, repeat: true)
51  {
52  OverridePriority = 100.0f,
53  IsFollowOrder = true
54  };
55  humanAiController.ObjectiveManager.AddObjective(newObjective);
56  humanAiController.ObjectiveManager.WaitTimer = 0.0f;
57  }
58  else
59  {
60  foreach (var objective in humanAiController.ObjectiveManager.Objectives)
61  {
62  if (objective is AIObjectiveGoTo goToObjective && goToObjective.Target == target)
63  {
64  goToObjective.Abandon = true;
65  }
66  }
67  }
68  targetCount++;
69  if (MaxTargets > -1 && targetCount >= MaxTargets)
70  {
71  break;
72  }
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 && target != null && AbandonOnReset)
85  {
86  foreach (var npc in affectedNpcs)
87  {
88  if (npc.Removed || npc.AIController is not HumanAIController humanAiController) { continue; }
89  foreach (var goToObjective in humanAiController.ObjectiveManager.GetActiveObjectives<AIObjectiveGoTo>())
90  {
91  if (goToObjective.Target == target)
92  {
93  goToObjective.Abandon = true;
94  }
95  }
96  }
97  target = null;
98  affectedNpcs = null;
99  }
100  isFinished = false;
101  }
102 
103  public override string ToDebugString()
104  {
105  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(NPCFollowAction)} -> (NPCTag: {NPCTag.ColorizeObject()}, TargetTag: {TargetTag.ColorizeObject()}, Follow: {Follow.ColorizeObject()})";
106  }
107  }
108 }
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
Makes an NPC follow or stop following a specific target.
override string ToDebugString()
Rich test to display in debugdraw
override bool IsFinished(ref string goTo)
Has the action finished.
NPCFollowAction(ScriptedEvent parentEvent, ContentXElement element)
override void Update(float deltaTime)
IEnumerable< Entity > GetTargets(Identifier tag)