Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AISwitchWeapon.c
Go to the documentation of this file.
2 {
3  ref SCR_AISwitchWeapon m_Task;
4 
5  //------------------------------------------------------------------------------------------------
7  {
8  m_Task = task;
9  }
10 
11  void Invalidate() { m_Task = null; }
12 
13  //------------------------------------------------------------------------------------------------
14  override protected void OnComplete()
15  {
16  if (m_Task)
17  {
18  m_Task.OnMoveWeaponFinished(true);
19  Invalidate();
20  }
21 
22  }
23 
24  //------------------------------------------------------------------------------------------------
25  override protected void OnFailed()
26  {
27  if (m_Task)
28  {
29  m_Task.OnMoveWeaponFinished(false);
30  Invalidate();
31  }
32 
33  }
34 };
35 
36 
38 {
39  protected static const int WEAPON_STATE_IDLE = 0;
40  protected static const int WEAPON_STATE_MOVING = 1;
41  protected static const int WEAPON_STATE_MOVED = 2;
42 
43  protected static const string PORT_WEAPON_COMPONENT = "WeaponComponent";
44 
45  protected int m_iWeaponState;
46 
47  protected EquipedWeaponStorageComponent m_EqWeaponStorageComp;
48  protected ref SCR_AISwitchWeaponMovedCallback m_WeaponMoveCallback;
49 
50  //--------------------------------------------------------------------------------------------
51  void OnMoveWeaponFinished(bool result)
52  {
53  #ifdef AI_DEBUG
54  AddDebugMessage(string.Format("OnMoveWeaponFinished: %1", result));
55  #endif
56 
57  m_iWeaponState = WEAPON_STATE_MOVED;
58  }
59 
60  //--------------------------------------------------------------------------------------------
61  override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
62  {
63  if (!m_WeaponMgrComp || !m_ControlComp || !m_InventoryMgr)
64  return ENodeResult.FAIL;
65 
66  BaseWeaponComponent newWeaponComp = null;
67  GetVariableIn(PORT_WEAPON_COMPONENT, newWeaponComp);
68  if (!newWeaponComp)
69  return ENodeResult.FAIL;
70 
71  // Resolve which weapon manager to use
72  BaseCompartmentSlot compartmentSlot = m_CompartmentAccessComp.GetCompartment();
73  BaseWeaponManagerComponent weaponMgr;
74  if (compartmentSlot)
75  weaponMgr = BaseWeaponManagerComponent.Cast(compartmentSlot.GetOwner().FindComponent(BaseWeaponManagerComponent));
76  else
77  weaponMgr = m_WeaponMgrComp;
78 
79  if (!weaponMgr)
80  return ENodeResult.FAIL;
81 
82  BaseWeaponComponent currentWeaponComp = weaponMgr.GetCurrentWeapon();
83 
84  if (compartmentSlot)
85  {
86  //-----------------------------------------
87  // Turret weapon switching
88 
89  IEntity compartmentParentEntity = compartmentSlot.GetOwner();
90  TurretControllerComponent turretController = TurretControllerComponent.Cast(compartmentParentEntity.FindComponent(TurretControllerComponent));
91 
92  if (!turretController)
93  {
94  #ifdef AI_DEBUG
95  AddDebugMessage("Weapon switch failed: no turret controller on turret", LogLevel.WARNING);
96  #endif
97  return ENodeResult.FAIL;
98  }
99 
100  if (currentWeaponComp == newWeaponComp) // Return success if done
101  {
102  #ifdef AI_DEBUG
103  AddDebugMessage("Weapon switch completed");
104  #endif
105  return ENodeResult.SUCCESS;
106  }
107 
108  #ifdef AI_DEBUG
109  AddDebugMessage(string.Format("StartWeaponSwitchTurret: %1 %2 %3", newWeaponComp, newWeaponComp.GetOwner(), newWeaponComp.GetOwner().GetPrefabData().GetPrefabName()));
110  #endif
111  SCR_AIWeaponHandling.StartWeaponSwitchTurret(turretController, newWeaponComp, owner.GetControlledEntity());
112  }
113  else
114  {
115  //-----------------------------------------
116  // Character weapon switching
117 
118  if (m_ControlComp.IsChangingItem())
119  return ENodeResult.RUNNING;
120 
121  if (currentWeaponComp == newWeaponComp) // Return success if done
122  {
123  #ifdef AI_DEBUG
124  AddDebugMessage("Weapon switch completed");
125  #endif
126  return ENodeResult.SUCCESS;
127  }
128 
129  if (!m_InventoryMgr.Contains(newWeaponComp.GetOwner()))
130  {
131  #ifdef AI_DEBUG
132  AddDebugMessage("Weapon switch failed: weapon is not in inventory", LogLevel.WARNING);
133  #endif
134  return ENodeResult.FAIL;
135  }
136 
137  #ifdef AI_DEBUG
138  AddDebugMessage(string.Format("StartWeaponSwitchCharacter: %1 %2 %3", newWeaponComp, newWeaponComp.GetOwner(), newWeaponComp.GetOwner().GetPrefabData().GetPrefabName()));
139  #endif
140 
141  if (m_iWeaponState == WEAPON_STATE_MOVING)
142  return ENodeResult.RUNNING;
143 
144  array<IEntity> weapons = {};
145  m_WeaponMgrComp.GetWeaponsList(weapons);
146 
147  IEntity newWeaponEnt = newWeaponComp.GetOwner();
148 
149  if (!weapons.Contains(newWeaponEnt))
150  {
151  // Return fail if weapon was moved and it's still unavailable
152  if (m_iWeaponState == WEAPON_STATE_MOVED)
153  return ENodeResult.FAIL;
154 
155  IEntity controlledEnt = owner.GetControlledEntity();
156 
157  if (!m_EqWeaponStorageComp)
158  m_EqWeaponStorageComp = EquipedWeaponStorageComponent.Cast(controlledEnt.FindComponent(EquipedWeaponStorageComponent));
159 
160  if (!m_EqWeaponStorageComp)
161  return ENodeResult.FAIL;
162 
163  m_iWeaponState = WEAPON_STATE_MOVING;
164 
165  if (m_WeaponMoveCallback)
166  m_WeaponMoveCallback.Invalidate();
167 
168  m_WeaponMoveCallback = new SCR_AISwitchWeaponMovedCallback(this);
169 
170  InventoryStorageSlot slot = m_EqWeaponStorageComp.FindSuitableSlotForItem(newWeaponEnt);
171  IEntity existingEntityAtSlot = null;
172  if (slot)
173  existingEntityAtSlot = slot.GetAttachedEntity();
174 
175  if (existingEntityAtSlot)
176  {
177  #ifdef AI_DEBUG
178  AddDebugMessage("Weapon is not in the weapon manager list, slot already occupied, requesting swap of weapon and existing entity in slot");
179  #endif
180  m_InventoryMgr.TrySwapItemStorages(newWeaponEnt, existingEntityAtSlot, m_WeaponMoveCallback);
181  }
182  else
183  {
184  #ifdef AI_DEBUG
185  AddDebugMessage("Weapon is not in the weapon manager list, requesting move to EquipedWeaponStorageComponent");
186  #endif
187  m_InventoryMgr.TryMoveItemToStorage(newWeaponEnt, m_EqWeaponStorageComp, -1, m_WeaponMoveCallback);
188  }
189 
190  return ENodeResult.RUNNING;
191  }
192 
193  m_iWeaponState = WEAPON_STATE_IDLE;
194  SCR_AIWeaponHandling.StartWeaponSwitchCharacter(m_ControlComp, newWeaponComp);
195 
196  return ENodeResult.RUNNING;
197  }
198 
199  return ENodeResult.FAIL;
200  }
201 
202 
203  override void OnAbort(AIAgent owner, Node nodeCausingAbort)
204  {
205  m_iWeaponState = WEAPON_STATE_IDLE;
206 
207  if (m_WeaponMoveCallback)
208  m_WeaponMoveCallback.Invalidate();
209 
210  m_WeaponMoveCallback = null;
211  }
212 
213  //--------------------------------------------------------------------------------------------
214  protected static ref TStringArray s_aVarsIn = {PORT_WEAPON_COMPONENT};
215  override TStringArray GetVariablesIn() { return s_aVarsIn; }
216 
217  override bool VisibleInPalette() { return true; }
218 }
SCR_AISwitchWeaponMovedCallback
Definition: SCR_AISwitchWeapon.c:1
m_Task
SCR_EditableTaskComponentClass m_Task
Editable SCR_BaseTask.
InventoryStorageSlot
Definition: InventoryStorageSlot.c:12
ScriptedInventoryOperationCallback
Definition: ScriptedInventoryOperationCallback.c:12
SCR_AISwitchWeapon
Definition: SCR_AISwitchWeapon.c:37
BaseWeaponComponent
Definition: BaseWeaponComponent.c:12
SCR_AIWeaponHandlingBase
Base class for nodes which handle magazine switching.
Definition: SCR_AIWeaponHandlingBase.c:2
TurretControllerComponent
Definition: TurretControllerComponent.c:12