Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AIGetSmartActionsState.c
Go to the documentation of this file.
1 class SCR_AIGetSmartActionsState : AITaskScripted
2 {
3  static const string WAYPOINT_ENTITY_IN = "WaypointEntityIn";
4 
5  private EAIWaypointCompletionType m_eCompletionType;
6  private int m_iNumberOfSALeft = 0;
7  private ENodeResult m_eSACompletedResult = ENodeResult.RUNNING;
8  private bool m_bLeaderWasSuccessful;
9  private bool m_bIsRegisteredTags = false;
10 
11  private SCR_SmartActionWaypoint m_prevWaypoint;
12  private ref array<string> m_arrayOfTags;
13  private AIAgent m_leaderAgent;
14 
15  private IEntity m_SmartActionEntity;
16  private SCR_SmartActionWaypoint m_waypoint;
17  private string m_smartActionTag;
18  private ref array<Managed> m_smartActionComponents;
19 
20  //------------------------------------------------------------------------------------------------
21  protected static ref TStringArray s_aVarsIn = {
22  WAYPOINT_ENTITY_IN
23  };
24  override TStringArray GetVariablesIn()
25  {
26  return s_aVarsIn;
27  }
28 
29  //------------------------------------------------------------------------------------------------
30  protected override void OnInit(AIAgent owner)
31  {
32  m_smartActionComponents = new array<Managed>;
33  m_arrayOfTags = new array<string>;
34  }
35 
36  //------------------------------------------------------------------------------------------------
37  override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
38  {
39  AIWaypoint targetWaypoint;
40  GetVariableIn("WaypointEntityIn", targetWaypoint);
41 
42  if (!targetWaypoint)
43  {
44  NodeError(this, owner, "Waypoint parameter is not of correct type");
45  return ENodeResult.FAIL;
46  };
47 
48  AIGroup group = AIGroup.Cast(owner);
49  if (group)
50  m_leaderAgent = group.GetLeaderAgent();
51  else m_leaderAgent = owner;
52 
53  m_waypoint = SCR_SmartActionWaypoint.Cast(targetWaypoint);
54  // is the waypoint new or same as in prev. check?
55  if (m_prevWaypoint != m_waypoint)
56  {
57  if (m_waypoint)
58  {
59  if (m_prevWaypoint && m_smartActionComponents)
60  UnregisterSmartActionsWithTag(m_smartActionTag,m_smartActionComponents);
61 
62  m_eCompletionType = targetWaypoint.GetCompletionType();
63  m_waypoint.GetSmartActionEntity(m_SmartActionEntity, m_smartActionTag);
64  if (!m_SmartActionEntity || m_smartActionTag.IsEmpty())
65  NodeError(this, owner, "Waypoint is not set properly");
66 
67  FindAllSmartActionComponents(m_SmartActionEntity,m_smartActionComponents);
68  m_iNumberOfSALeft = RegisterSmartActionsWithTag(m_smartActionTag, m_smartActionComponents);
69  m_prevWaypoint = m_waypoint;
70  }
71  };
72 
73  switch (m_eCompletionType)
74  {
75  case EAIWaypointCompletionType.All:
76  {
77  // we want all of the SA to be completed, not all were yet reported back but one has failed now
78  if (m_eSACompletedResult == ENodeResult.FAIL)
79  {
80  ResetVariables();
81  return ENodeResult.FAIL;
82  }
83  else if (m_iNumberOfSALeft == 0)
84  {
85  ResetVariables();
86  return ENodeResult.SUCCESS;
87  }
88  break;
89  }
90  case EAIWaypointCompletionType.Any:
91  {
92  // we succeed the node the first time the SA completes
93  if (m_eSACompletedResult == ENodeResult.SUCCESS)
94  {
95  ResetVariables();
96  return ENodeResult.SUCCESS;
97  }
98  else if (m_iNumberOfSALeft == 0)
99  {
100  ResetVariables();
101  return ENodeResult.FAIL;
102  }
103  break;
104  }
105  case EAIWaypointCompletionType.Leader:
106  {
107  if (m_bLeaderWasSuccessful)
108  {
109  ResetVariables();
110  return ENodeResult.SUCCESS;
111  }
112  else if (m_iNumberOfSALeft == 0)
113  {
114  ResetVariables();
115  return ENodeResult.FAIL;
116  }
117  break;
118  }
119  }
120  return ENodeResult.RUNNING;
121  }
122 
123  //------------------------------------------------------------------------------------------------
124  override void OnAbort(AIAgent owner, Node nodeCausingAbort)
125  {
126  ResetVariables();
127  }
128 
129  //------------------------------------------------------------------------------------------------
130  int FindAllSmartActionComponents(IEntity entityContainingSmartActions, out array<Managed> smartActionComponents)
131  {
132  entityContainingSmartActions.FindComponents(SCR_AISmartActionComponent,smartActionComponents);
133  IEntity child = entityContainingSmartActions.GetChildren();
134  while (child)
135  {
136  array<Managed> smartActionComponentsOfOneEntity = {};
137  child.FindComponents(SCR_AISmartActionComponent,smartActionComponentsOfOneEntity);
138  smartActionComponents.InsertAll(smartActionComponentsOfOneEntity);
139  child = child.GetSibling();
140  }
141  return smartActionComponents.Count();
142  }
143 
144  //------------------------------------------------------------------------------------------------
145  int RegisterSmartActionsWithTag(string tag, array<Managed> smartActionComponents)
146  {
147  int numberResult;
148  foreach (Managed managedComp : smartActionComponents)
149  {
151  comp.GetTags(m_arrayOfTags);
152  if (m_arrayOfTags.Contains(tag))
153  {
154  numberResult ++;
155  comp.GetOnActionEnd().Insert(OnSmartActionEnd);
156  comp.GetOnActionFailed().Insert(OnSmartActionFailed);
157  }
158  }
159  m_bIsRegisteredTags = true;
160  return numberResult;
161  }
162 
163  //------------------------------------------------------------------------------------------------
164  void UnregisterSmartActionsWithTag(string tag, array<Managed> smartActionComponents)
165  {
166  if (!m_bIsRegisteredTags)
167  return;
168 
169  foreach (Managed managedComp : smartActionComponents)
170  {
171  if (managedComp)
172  {
174  comp.GetTags(m_arrayOfTags);
175  if (m_arrayOfTags.Contains(tag))
176  {
177  comp.GetOnActionEnd().Remove(OnSmartActionEnd);
178  comp.GetOnActionFailed().Remove(OnSmartActionFailed);
179  }
180  }
181  }
182  m_arrayOfTags.Clear();
183  m_smartActionComponents = null;
184  m_bIsRegisteredTags = false;
185  }
186 
187  //------------------------------------------------------------------------------------------------
188  void OnSmartActionEnd(AIAgent user)
189  {
190  m_iNumberOfSALeft = Math.Max(m_iNumberOfSALeft - 1,0);
191  if (user == m_leaderAgent)
192  m_bLeaderWasSuccessful = true;
193  m_eSACompletedResult = ENodeResult.SUCCESS;
194  }
195 
196  //------------------------------------------------------------------------------------------------
197  void OnSmartActionFailed(AIAgent user)
198  {
199  m_iNumberOfSALeft = Math.Max(m_iNumberOfSALeft - 1,0);
200  m_eSACompletedResult = ENodeResult.FAIL;
201  }
202 
203  //------------------------------------------------------------------------------------------------
204  private void ResetVariables()
205  {
206  UnregisterSmartActionsWithTag(m_smartActionTag, m_smartActionComponents);
207  m_iNumberOfSALeft = 0;
208  m_eSACompletedResult = ENodeResult.RUNNING;
209  m_bLeaderWasSuccessful = false;
210  m_leaderAgent = null;
211  m_waypoint = null;
212  }
213 
214  //------------------------------------------------------------------------------------------------
215  protected override bool VisibleInPalette()
216  {
217  return true;
218  }
219 
220  //------------------------------------------------------------------------------------------------
221  override bool CanReturnRunning()
222  {
223  return true;
224  }
225 
226  //------------------------------------------------------------------------------------------------
227  protected override string GetOnHoverDescription()
228  {
229  return "Node that checks if smart actions from given waypoint have been completed depending on settings of the waypoint, returns SUCCESS if they were.";
230  }
231 };
SCR_AISmartActionComponent
Definition: SCR_AISmartActionComponent.c:9
NodeError
ENodeResult NodeError(Node node, AIAgent owner, string msg)
Error call to be used in scripted BT nodes.
Definition: NodeError.c:3
SCR_SmartActionWaypoint
Definition: SCR_SmartActionWaypoint.c:4
SCR_AIGetSmartActionsState
Definition: SCR_AIGetSmartActionsState.c:1