Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AIPerformSmartHealing.c
Go to the documentation of this file.
1 class SCR_AIPerformSmartHealing : AITaskScripted
2 {
3  // instead of class-local enum :(
4  private const int USAGE_NONE = 0;
5  private const int USAGE_WAITING_TO_START = 1;
6  private const int USAGE_IN_PROGRESS = 2;
7  private const int USAGE_DONE_SUCCESS = 3;
8  private const int USAGE_DONE_FAILURE = 4;
9 
10  protected const float TIME_OF_HEALING_MS = 12000.0; // how long the node waits before FAILS (time to trigger and execute healing action)
11  protected const string PORT_USER_ACTION = "UserAction";
12  protected const string PORT_TARGET_ENTITY = "TargetEntity";
13  protected const string PORT_ITEM = "ItemToUse";
14 
15  protected typename m_sUserAction;
16  protected IEntity m_targetEntity, m_item;
17  protected SCR_CharacterControllerComponent m_targetContrComp;
18  protected bool m_bAborted;
19 
20  private int m_iItemUsageInProgress;
21  private float m_fTimeout_ms;
22 
23  [Attribute( defvalue: "SCR_BandageUserAction", uiwidget: UIWidgets.EditBox, desc: "Insert UserAction class name" )]
24  protected string m_userActionString;
25 
26  override void OnEnter(AIAgent owner)
27  {
28  m_fTimeout_ms = 0;
29  m_bAborted = false;
30  m_iItemUsageInProgress = USAGE_NONE;
31  }
32 
33  //------------------------------------------------------------------------------------------------
34  override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
35  {
36  switch (m_iItemUsageInProgress)
37  {
38  case USAGE_NONE:
39  {
40 
41  string userActionString;
42 
43  if (!GetVariableIn(PORT_USER_ACTION, userActionString))
44  userActionString = m_userActionString;
45  GetVariableIn(PORT_ITEM, m_item);
46 
47  IEntity controlledEntity = owner.GetControlledEntity();
48  if (!controlledEntity)
49  return ENodeResult.FAIL;
50 
51  if (!GetVariableIn(PORT_TARGET_ENTITY, m_targetEntity))
52  m_targetEntity = controlledEntity;
53 
54  m_sUserAction = userActionString.ToType();
55  if (m_sUserAction == typename.Empty)
56  return ENodeResult.FAIL;
57 
58  ChimeraCharacter character = ChimeraCharacter.Cast(m_targetEntity);
59  if (character)
60  {
61  SCR_CharacterDamageManagerComponent damageMan = SCR_CharacterDamageManagerComponent.Cast(character.GetDamageManager());
62  if (!damageMan && !damageMan.CanBeHealed())
63  return ENodeResult.FAIL;
64  }
65  character = ChimeraCharacter.Cast(controlledEntity);
66  m_targetContrComp = SCR_CharacterControllerComponent.Cast(character.GetCharacterController());
67  if (!m_targetContrComp)
68  return ENodeResult.FAIL;
69 
70  array<BaseUserAction> outActions = {};
71  SCR_HealingUserAction action;
72  GetActions(m_targetEntity, outActions);
73  foreach (BaseUserAction baseAction : outActions)
74  {
75  // Check if action is healing action type
76  action = SCR_HealingUserAction.Cast(baseAction);
77  if (!action)
78  continue;
79 
80  // Find the healing action on the correct hitzone group
81  if (action.GetUserActionGroup() != GetTargetGroup(m_targetEntity))
82  continue;
83 
84  // If healing action is of the desired healing-type, and can be performed, PerformAction
85  if (action && m_sUserAction == action.Type() && action.CanBePerformedScript(controlledEntity))
86  {
87  m_targetContrComp.m_OnItemUseBeganInvoker.Insert(OnItemUseBegan);
88  m_targetContrComp.m_OnItemUseEndedInvoker.Insert(OnItemUseEnded);
89  m_iItemUsageInProgress = USAGE_WAITING_TO_START;
90  action.PerformAction(m_targetEntity, controlledEntity);
91  return ENodeResult.RUNNING;
92  }
93  }
94  return ENodeResult.FAIL;
95  };
96  case USAGE_WAITING_TO_START:
97  {
98  float worldTime = GetGame().GetWorld().GetWorldTime();
99  if (m_fTimeout_ms == 0)
100  m_fTimeout_ms = worldTime + TIME_OF_HEALING_MS;
101  //else if (worldTime > m_fTimeout_ms)
102  //return ENodeResult.FAIL;
103  return ENodeResult.RUNNING;
104  };
105  case USAGE_IN_PROGRESS:
106  {
107  float worldTime = GetGame().GetWorld().GetWorldTime();
108  if (m_fTimeout_ms == 0)
109  m_fTimeout_ms = worldTime + TIME_OF_HEALING_MS;
110  //else if (worldTime > m_fTimeout_ms)
111  //return ENodeResult.FAIL;
112  return ENodeResult.RUNNING;
113  };
114  case USAGE_DONE_SUCCESS:
115  {
116  return ENodeResult.SUCCESS;
117  };
118  case USAGE_DONE_FAILURE:
119  {
120  return ENodeResult.FAIL;
121  };
122  };
123  return ENodeResult.FAIL;
124  }
125 
126  //------------------------------------------------------------------------------------------------
127  override void OnAbort(AIAgent owner, Node nodeCausingAbort)
128  {
129  if (m_bAborted)
130  return;
131 
132  if (m_targetContrComp)
133  {
134  m_targetContrComp.m_OnItemUseBeganInvoker.Remove(OnItemUseBegan);
135  m_targetContrComp.m_OnItemUseEndedInvoker.Remove(OnItemUseEnded);
136  if (m_iItemUsageInProgress < USAGE_DONE_SUCCESS) // we are ending before the action has finished
137  m_targetContrComp.GetInputContext().SetCancelItemAction(true);
138  }
139  m_bAborted = true;
140  }
141 
142  //------------------------------------------------------------------------------------------------
143  void OnItemUseBegan(IEntity item, ItemUseParameters animParams)
144  {
145  if (item != m_item)
146  return;
147  m_iItemUsageInProgress = USAGE_IN_PROGRESS;
148  }
149 
150  //------------------------------------------------------------------------------------------------
151  void OnItemUseEnded(IEntity item, bool actionCompleted, ItemUseParameters animParams)
152  {
153  if (item != m_item)
154  return;
155 
156  if (actionCompleted)
157  m_iItemUsageInProgress = USAGE_DONE_SUCCESS;
158  else
159  m_iItemUsageInProgress = USAGE_DONE_FAILURE;
160  }
161 
162  //------------------------------------------------------------------------------------------------
163  protected void GetActions(IEntity targetEntity, notnull out array<BaseUserAction> outActions)
164  {
165  if (!targetEntity)
166  return;
167 
168  ActionsManagerComponent actionOnEntity = ActionsManagerComponent.Cast(targetEntity.FindComponent(ActionsManagerComponent));
169 
170  if (!actionOnEntity)
171  return;
172 
173  actionOnEntity.GetActionsList(outActions);
174  }
175 
176  //------------------------------------------------------------------------------------------------
177  protected ECharacterHitZoneGroup GetTargetGroup(IEntity targetEntity)
178  {
179  ChimeraCharacter char = ChimeraCharacter.Cast(targetEntity);
180  if (!char)
181  return null;
182 
184  if (!damageMan)
185  return null;
186 
187  array<ECharacterHitZoneGroup> limbs = {};
188  damageMan.GetAllLimbs(limbs);
189  SCR_CharacterHitZone charHitZone = SCR_CharacterHitZone.Cast(damageMan.GetMostDOTHitZone(EDamageType.BLEEDING, false, limbs));
190  if (!charHitZone)
191  return null;
192 
193  return charHitZone.GetHitZoneGroup();
194  }
195 
196  //------------------------------------------------------------------------------------------------
197  protected static ref TStringArray s_aVarsIn = {
198  PORT_USER_ACTION,
199  PORT_TARGET_ENTITY,
200  PORT_ITEM
201  };
202 
203  //------------------------------------------------------------------------------------------------
204  override TStringArray GetVariablesIn()
205  {
206  return s_aVarsIn;
207  }
208 
209  //------------------------------------------------------------------------------------------------
210  protected override bool VisibleInPalette()
211  {
212  return true;
213  }
214 
215  //------------------------------------------------------------------------------------------------
216  override bool CanReturnRunning() { return true; }
217 
218  //------------------------------------------------------------------------------------------------
219  protected override string GetOnHoverDescription()
220  {
221  return "Uses smart healing action and is running until it finishes.";
222  }
223 };
ECharacterHitZoneGroup
ECharacterHitZoneGroup
Definition: SCR_CharacterDamageManagerComponent.c:1
SCR_CharacterHitZone
Definition: SCR_CharacterHitZone.c:55
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_HealingUserAction
Definition: SCR_HealingUserAction.c:1
ItemUseParameters
Definition: ItemUseParameters.c:15
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
func
func
Definition: SCR_AIThreatSystem.c:5
SCR_CharacterControllerComponent
Definition: SCR_CharacterControllerComponent.c:35
m_fTimeout_ms
float m_fTimeout_ms
Definition: SCR_AITalkRequest.c:27
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_CharacterDamageManagerComponent
Definition: SCR_CharacterDamageManagerComponent.c:18
GetDamageManager
proto external SCR_DamageManagerComponent GetDamageManager()
Returns component which handles damage.
SCR_AIPerformSmartHealing
Definition: SCR_AIPerformSmartHealing.c:1
BaseUserAction
Definition: BaseUserAction.c:12
EDamageType
EDamageType
Definition: EDamageType.c:12