Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AiSelectFireMode.c
Go to the documentation of this file.
1 class SCR_AISelectFireMode: AITaskScripted
2 {
3  [Attribute("0", UIWidgets.ComboBox, "Try set Firemode", "", ParamEnumArray.FromEnum(EWeaponFiremodeType) )]
4  protected EWeaponFiremodeType m_FiremodeType;
5 
6  //-----------------------------------------------------------------------------------------------------------------------------
7  override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
8  {
9  GenericEntity controlledEntity = GenericEntity.Cast(owner.GetControlledEntity());
10  if (!controlledEntity)
11  return ENodeResult.FAIL;
12 
13  //--------------------------------------------------------------------------
14  // Resolve which weapon manager to use
15 
16  BaseWeaponManagerComponent wpnManagerComponent;
17  CharacterControllerComponent controller;
18 
19  CompartmentAccessComponent compAccess = CompartmentAccessComponent.Cast(controlledEntity.FindComponent(CompartmentAccessComponent));
20  if (!compAccess)
21  return ENodeResult.FAIL;
22 
23  BaseCompartmentSlot compartment = compAccess.GetCompartment();
24  if (compartment)
25  {
26  wpnManagerComponent = BaseWeaponManagerComponent.Cast(compartment.GetOwner().FindComponent(BaseWeaponManagerComponent));
27  if (wpnManagerComponent)
28  {
29  // Return success if in turret. Turret weapons have no fire modes anyway.
30  return ENodeResult.SUCCESS;
31  }
32  }
33  else
34  {
35  controller = CharacterControllerComponent.Cast(controlledEntity.FindComponent(CharacterControllerComponent));
36  if (controller)
37  wpnManagerComponent = controller.GetWeaponManagerComponent();
38  }
39 
40  if (!wpnManagerComponent || !controller)
41  return ENodeResult.FAIL;
42 
43  BaseWeaponComponent wpnComponent = wpnManagerComponent.GetCurrent();
44  if (!wpnComponent)
45  return ENodeResult.FAIL;
46 
47  // Bail if we already have the desired fire mode
48  if (wpnComponent.GetCurrentFireModeType() == m_FiremodeType)
49  return ENodeResult.SUCCESS;
50 
51  BaseMuzzleComponent muzzle = wpnComponent.GetCurrentMuzzle();
52 
53  // Fail if there is no muzzle, it is probably a grenade
54  if (!muzzle)
55  return ENodeResult.FAIL;
56 
57  //--------------------------------------------------------------------------
58  // Find available firemodes
59 
60  array<BaseFireMode> fireModes = {};
61  muzzle.GetFireModesList(fireModes);
62 
63  int fireModesCount = fireModes.Count();
64  int semiAutoId = -1, autoId = -1, burstId = -1, manualId = -1, safetyId = -1;
65  for (int i = 0; i<fireModesCount; i++)
66  {
67  switch (fireModes[i].GetFiremodeType())
68  {
69  case EWeaponFiremodeType.Semiauto :
70  {
71  semiAutoId = i;
72  break;
73  }
74  case EWeaponFiremodeType.Auto :
75  {
76  autoId = i;
77  break;
78  }
79  case EWeaponFiremodeType.Burst :
80  {
81  burstId = i;
82  break;
83  }
84  case EWeaponFiremodeType.Safety :
85  {
86  safetyId = i;
87  break;
88  }
89  case EWeaponFiremodeType.Manual:
90  {
91  manualId = i;
92  break;
93  }
94  }
95  }
96 
97  //--------------------------------------------------------------------------
98  // Try to best suitable firemode
99  // If a non-safety firemode is requested, we will still try to find any non-safety firemode,
100  // to ensure weapon is not stuck in safety
101 
102  int bestFm = -1;
103  switch (m_FiremodeType)
104  {
105  case EWeaponFiremodeType.Auto:
106  {
107  // auto > burst > semi > manual
108  if (autoId != -1)
109  bestFm = autoId;
110  else if (burstId != -1)
111  bestFm = burstId;
112  else if (semiAutoId != -1)
113  bestFm = semiAutoId;
114  else if (manualId != -1)
115  bestFm = manualId;
116 
117  break;
118  }
119 
120  case EWeaponFiremodeType.Burst:
121  {
122  // burst > auto > semi > manual
123  if (burstId != -1)
124  bestFm = burstId;
125  else if (autoId != -1)
126  bestFm = autoId;
127  else if (semiAutoId != -1)
128  bestFm = semiAutoId;
129  else if (manualId != -1)
130  bestFm = manualId;
131 
132  break;
133  }
134 
135  case EWeaponFiremodeType.Semiauto:
136  {
137  // semi > manual > burst > auto
138  if (semiAutoId != -1)
139  bestFm = semiAutoId;
140  else if (manualId != -1)
141  bestFm = manualId;
142  else if (burstId != -1)
143  bestFm = burstId;
144  else if (autoId != -1)
145  bestFm = autoId;
146 
147  break;
148  }
149 
150  case EWeaponFiremodeType.Safety:
151  {
152  // safety
153  if (safetyId != -1)
154  bestFm = safetyId;
155 
156  break;
157  }
158  }
159 
160  //--------------------------------------------------------------------------
161  // Set found firemode or report error
162 
163  if (bestFm == -1)
164  {
165  // We couldn't find desired firemode or adequate replacement, log a warning
166 
167  WeaponSlotComponent weaponSlotComp = WeaponSlotComponent.Cast(wpnComponent);
168  IEntity weaponEntity;
169  if (weaponSlotComp)
170  weaponEntity = weaponSlotComp.GetWeaponEntity();
171  else
172  weaponEntity = wpnComponent.GetOwner();
173 
174  string prefabName;
175  if (weaponEntity)
176  prefabName = weaponEntity.GetPrefabData().GetPrefabName();
177 
178  string str = string.Format("SCR_AISelectFireMode: proper fire mode was not found: %1. Weapon prefab: %2",
179  typename.EnumToString(EWeaponFiremodeType, m_FiremodeType),
180  prefabName);
181 
182  Print(str, LogLevel.WARNING);
183 
184  return ENodeResult.FAIL;
185  }
186 
187  controller.SetFireMode(bestFm);
188  return ENodeResult.SUCCESS;
189  }
190 
191  override protected string GetNodeMiddleText()
192  {
193  return "Selects a firemode closest to provided one.";
194  }
195 
196  override bool VisibleInPalette() {return true;}
197 };
SCR_AISelectFireMode
Definition: SCR_AiSelectFireMode.c:1
EWeaponFiremodeType
EWeaponFiremodeType
Definition: EWeaponFiremodeType.c:12
WeaponSlotComponent
Definition: WeaponSlotComponent.c:12
GenericEntity
SCR_GenericBoxEntityClass GenericEntity
Attribute
typedef Attribute
Post-process effect of scripted camera.
BaseWeaponComponent
Definition: BaseWeaponComponent.c:12
BaseMuzzleComponent
Definition: BaseMuzzleComponent.c:12