Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AIUtils.c
Go to the documentation of this file.
1 // gets a string from value given in port
2 // basically workaround that we can't cast void from GetVariableIn into type of that variable
3 //------------------------------------------------------------------------------------------------
4 static string SCR_AIGetStringFromPort(Node node, string port)
5 {
6  const string UNASSIGNED_VARIABLE = "Unassigned";
7  if ( node.GetVariableType(true, port) == string )
8  {
9  string value;
10  if(!node.GetVariableIn(port, value))
11  value = UNASSIGNED_VARIABLE;
12  return value;
13  }
14  else if ( node.GetVariableType(true, port) == int )
15  {
16  int value;
17  if(!node.GetVariableIn(port, value))
18  return UNASSIGNED_VARIABLE;
19  return value.ToString();
20  }
21  else if ( node.GetVariableType(true, port) == float )
22  {
23  float value;
24  if(!node.GetVariableIn(port, value))
25  return UNASSIGNED_VARIABLE;
26  return value.ToString();
27  }
28  else if ( node.GetVariableType(true, port) == bool )
29  {
30  bool value;
31  if(!node.GetVariableIn(port, value))
32  return UNASSIGNED_VARIABLE;
33  return value.ToString();
34  }
35  else if ( node.GetVariableType(true, port) == vector )
36  {
37  vector value;
38  if(!node.GetVariableIn(port, value))
39  return UNASSIGNED_VARIABLE;
40  return value.ToString();
41  }
42  else if ( node.GetVariableType(true, port).IsInherited(Managed) )
43  {
44  Managed value;
45  if(!node.GetVariableIn(port, value))
46  return UNASSIGNED_VARIABLE;
47  if (value)
48  return value.ToString();
49  else
50  return "NULL";
51  }
52  return "";
53 };
54 
55 // use this for generating random numbers for AI, no need to create another instance
56 //------------------------------------------------------------------------------------------------------------------------------------------------------------
57 static ref RandomGenerator s_AIRandomGenerator = new RandomGenerator;
58 
59 // use this for interpretting relevant character stance based on threat level
60 //------------------------------------------------------------------------------------------------
61 static ECharacterStance GetStanceFromThreat(EAIThreatState threatState)
62 {
63  switch (threatState)
64  {
65  case EAIThreatState.THREATENED: return ECharacterStance.PRONE;
66  case EAIThreatState.ALERTED: return ECharacterStance.CROUCH;
67  case EAIThreatState.VIGILANT: return ECharacterStance.STAND;
68  case EAIThreatState.SAFE: return ECharacterStance.STAND;
69  default: return ECharacterStance.STAND;
70  }
71  return ECharacterStance.STAND;
72 };
73 
74 //------------------------------------------------------------------------------------------------------------------------------------------------------------
75 // Compartments on vehicles
77 {
78  //--------------------------------------------------------------------------------------------
79  static ECompartmentType CompartmentClassToType(typename type)
80  {
81  if (type.IsInherited(CargoCompartmentSlot))
82  return ECompartmentType.Cargo;
83  else if (type.IsInherited(PilotCompartmentSlot))
84  return ECompartmentType.Pilot;
85  else if (type.IsInherited(TurretCompartmentSlot))
86  return ECompartmentType.Turret;
87  return -1;
88  }
89 
90  //--------------------------------------------------------------------------------------------
91  static bool FindAvailableCompartmentInVehicles (array<IEntity> vehicles, ECompartmentType roleInVehicle, out BaseCompartmentSlot compartmentOut, out IEntity vehicleOut)
92  {
93  foreach (IEntity vehicle: vehicles)
94  {
95  if (!vehicle)
96  continue;
97  BaseCompartmentManagerComponent compartmentMan = BaseCompartmentManagerComponent.Cast(vehicle.FindComponent(BaseCompartmentManagerComponent));
98  if (!Vehicle.Cast(vehicle) || !compartmentMan)
99  return false;
100  ref array<BaseCompartmentSlot> compartments = {};
101  compartmentMan.GetCompartments(compartments);
102  foreach (BaseCompartmentSlot compartment: compartments)
103  {
104  if (SCR_AICompartmentHandling.CompartmentClassToType(compartment.Type()) == roleInVehicle)
105  {
106  if (!compartment.GetOccupant() && compartment.IsCompartmentAccessible())
107  {
108  compartmentOut = compartment;
109  vehicleOut = vehicle;
110  return true;
111  }
112  }
113  }
114  }
115  return false;
116  }
117 
118  //--------------------------------------------------------------------------------------------
119  static bool IsInCompartment(notnull AIAgent agent)
120  {
121  IEntity controlledEntity = agent.GetControlledEntity();
122  if (!controlledEntity)
123  return false;
124 
125  ChimeraCharacter character = ChimeraCharacter.Cast(controlledEntity);
126  if (!character)
127  return false;
128 
129  return character.IsInVehicle();
130  }
131 }
132 
133 //------------------------------------------------------------------------------------------------------------------------------------------------------------
134 // Weapon Handling
135 class SCR_AIWeaponHandling
136 {
137  //--------------------------------------------------------------------------------------------
138  static int GetCurrentMuzzleId(BaseWeaponManagerComponent weapMgr)
139  {
140  BaseWeaponComponent weaponComp = weapMgr.GetCurrentWeapon();
141 
142  if (!weaponComp)
143  return -1;
144 
145  // Find muzzle ID
146  array<BaseMuzzleComponent> muzzles = {};
147  weaponComp.GetMuzzlesList(muzzles);
148  BaseMuzzleComponent currentMuzzle = weaponComp.GetCurrentMuzzle();
149 
150  if (!currentMuzzle || muzzles.IsEmpty())
151  return -1;
152 
153  int currentMuzzleId = muzzles.Find(currentMuzzle);
154  return currentMuzzleId;
155  }
156 
157  //--------------------------------------------------------------------------------------------
158  static BaseMagazineComponent GetCurrentMagazineComponent(BaseWeaponManagerComponent weapMgr)
159  {
160  BaseWeaponComponent weaponComp = weapMgr.GetCurrentWeapon();
161 
162  if (!weaponComp)
163  return null;
164 
165  BaseMuzzleComponent currentMuzzle = weaponComp.GetCurrentMuzzle();
166  if (!currentMuzzle)
167  return null;
168 
169  return currentMuzzle.GetMagazine();
170  }
171 
172  //--------------------------------------------------------------------------------------------
173  static bool IsCurrentMuzzleChambered(BaseWeaponManagerComponent weapMgr)
174  {
175  BaseWeaponComponent weaponComp = weapMgr.GetCurrentWeapon();
176 
177  if (!weaponComp)
178  return false;
179 
180  BaseMuzzleComponent currentMuzzle = weaponComp.GetCurrentMuzzle();
181  if (!currentMuzzle)
182  return false;
183 
184  return currentMuzzle.IsCurrentBarrelChambered();
185  }
186 
187  //--------------------------------------------------------------------------------------------
189  static BaseWeaponComponent GetCurrentWeaponComponent(BaseWeaponManagerComponent weapMgr)
190  {
191  return weapMgr.GetCurrentWeapon();
192  }
193 
194  //--------------------------------------------------------------------------------------------
195  static void StartMuzzleSwitch(CharacterControllerComponent controller, int newMuzzleId)
196  {
197  controller.SetMuzzle(newMuzzleId);
198  }
199 
200  //--------------------------------------------------------------------------------------------
201  static void StartMagazineSwitchCharacter(CharacterControllerComponent controller, BaseMagazineComponent newMagazineComp)
202  {
203  controller.ReloadWeaponWith(newMagazineComp.GetOwner());
204  }
205 
206  //--------------------------------------------------------------------------------------------
207  static void StartMagazineSwitchTurret(TurretControllerComponent controller, BaseMagazineComponent newMagazineComp)
208  {
209  controller.DoReloadWeaponWith(newMagazineComp.GetOwner());
210  }
211 
212  //--------------------------------------------------------------------------------------------
213  static void StartWeaponSwitchCharacter(CharacterControllerComponent controller, BaseWeaponComponent newWeaponComp)
214  {
215  controller.TryEquipRightHandItem(newWeaponComp.GetOwner(), EEquipItemType.EEquipTypeWeapon, false);
216  }
217 
218  //--------------------------------------------------------------------------------------------
219  static void StartWeaponSwitchTurret(TurretControllerComponent controller, BaseWeaponComponent newWeaponComp, IEntity turretOperator)
220  {
221  // Find weapon slot which has this weapon
222  IEntity newWeaponEntity = newWeaponComp.GetOwner();
223  BaseWeaponManagerComponent weaponMgr = controller.GetWeaponManager();
224  if (!weaponMgr)
225  return;
226 
227  WeaponSlotComponent newWeaponSlot;
228  array<WeaponSlotComponent> slots = {};
229  weaponMgr.GetWeaponsSlots(slots);
230  foreach (WeaponSlotComponent slot : slots)
231  {
232  if (slot.GetWeaponEntity() == newWeaponEntity)
233  newWeaponSlot = slot;
234  }
235 
236  if (!newWeaponSlot)
237  {
238  // There is no slot which has this weapon
239  return;
240  }
241 
242  controller.SelectWeapon(turretOperator, newWeaponSlot);
243  }
244 }
245 
247 {
248  static bool IsAlive(IEntity entity)
249  {
250  if (!entity)
251  return false;
252 
253  DamageManagerComponent damageManager;
254  ChimeraCharacter character = ChimeraCharacter.Cast(entity);
255  if (character)
256  damageManager = character.GetDamageManager();
257  else
258  damageManager = DamageManagerComponent.Cast(entity.FindComponent(DamageManagerComponent));
259 
260  if (!damageManager)
261  return true;
262 
263  return damageManager.GetState() != EDamageState.DESTROYED;
264  }
265 
266  // This method abstracts away the internals of damage system for usage in various AI behaviors.
267  static bool IsCharacterWounded(IEntity entity)
268  {
269  ChimeraCharacter character = ChimeraCharacter.Cast(entity);
270  if (!character)
271  return false;
272 
273  DamageManagerComponent damageManager = character.GetDamageManager();
274  if (!damageManager)
275  return false;
276 
277  return damageManager.IsDamagedOverTime(EDamageType.BLEEDING);
278  }
279 }
SCR_AIDamageHandling
Definition: SCR_AIUtils.c:246
ECharacterStance
ECharacterStance
Definition: ECharacterStance.c:12
EEquipItemType
EEquipItemType
Definition: EEquipItemType.c:12
ECompartmentType
ECompartmentType
Definition: ECompartmentType.c:7
EDamageState
EDamageState
Definition: EDamageState.c:12
PilotCompartmentSlot
Definition: PilotCompartmentSlot.c:12
WeaponSlotComponent
Definition: WeaponSlotComponent.c:12
TurretCompartmentSlot
Definition: TurretCompartmentSlot.c:12
BaseWeaponComponent
Definition: BaseWeaponComponent.c:12
CargoCompartmentSlot
Definition: CargoCompartmentSlot.c:12
TurretControllerComponent
Definition: TurretControllerComponent.c:12
type
EDamageType type
Definition: SCR_DestructibleTreeV2.c:32
EDamageType
EDamageType
Definition: EDamageType.c:12
GetCurrentMuzzleId
class SCR_AICompartmentHandling GetCurrentMuzzleId(BaseWeaponManagerComponent weapMgr)
Definition: SCR_AIUtils.c:138
BaseMuzzleComponent
Definition: BaseMuzzleComponent.c:12
DamageManagerComponent
Definition: DamageManagerComponent.c:12
SCR_AICompartmentHandling
Definition: SCR_AIUtils.c:76