Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_AISwitchWeapon.c
Go to the documentation of this file.
1class SCR_AISwitchWeaponMovedCallback : ScriptedInventoryOperationCallback
2{
3 ref SCR_AISwitchWeapon m_Task;
4
5 //------------------------------------------------------------------------------------------------
6 void SCR_AISwitchWeaponMovedCallback(SCR_AISwitchWeapon task)
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
49
50 //--------------------------------------------------------------------------------------------
51 void OnMoveWeaponFinished(bool result)
52 {
53 #ifdef AI_DEBUG
54 AddDebugMessage(string.Format("OnMoveWeaponFinished: %1", result));
55 #endif
56
58 }
59
60 //--------------------------------------------------------------------------------------------
61 override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
62 {
64 return ENodeResult.FAIL;
65
66 BaseWeaponComponent newWeaponComp = null;
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();
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 return ENodeResult.RUNNING;
113 }
114 else
115 {
116 //-----------------------------------------
117 // Character weapon switching
118
119 if (m_ControlComp.IsChangingItem())
120 return ENodeResult.RUNNING;
121
122 if (currentWeaponComp == newWeaponComp) // Return success if done
123 {
124 #ifdef AI_DEBUG
125 AddDebugMessage("Weapon switch completed");
126 #endif
127 return ENodeResult.SUCCESS;
128 }
129
130 if (!m_InventoryMgr.Contains(newWeaponComp.GetOwner()))
131 {
132 #ifdef AI_DEBUG
133 AddDebugMessage("Weapon switch failed: weapon is not in inventory", LogLevel.WARNING);
134 #endif
135 return ENodeResult.FAIL;
136 }
137
138 #ifdef AI_DEBUG
139 AddDebugMessage(string.Format("StartWeaponSwitchCharacter: %1 %2 %3", newWeaponComp, newWeaponComp.GetOwner(), newWeaponComp.GetOwner().GetPrefabData().GetPrefabName()));
140 #endif
141
143 return ENodeResult.RUNNING;
144
145 array<IEntity> weapons = {};
146 m_WeaponMgrComp.GetWeaponsList(weapons);
147
148 IEntity newWeaponEnt = newWeaponComp.GetOwner();
149
150 if (!weapons.Contains(newWeaponEnt))
151 {
152 // Return fail if weapon was moved and it's still unavailable
154 return ENodeResult.FAIL;
155
156 IEntity controlledEnt = owner.GetControlledEntity();
157
160
162 return ENodeResult.FAIL;
163
165
167 m_WeaponMoveCallback.Invalidate();
168
170
171 InventoryStorageSlot slot = m_EqWeaponStorageComp.FindSuitableSlotForItem(newWeaponEnt);
172 IEntity existingEntityAtSlot = null;
173 if (slot)
174 existingEntityAtSlot = slot.GetAttachedEntity();
175
176 if (existingEntityAtSlot)
177 {
178 #ifdef AI_DEBUG
179 AddDebugMessage("Weapon is not in the weapon manager list, slot already occupied, requesting swap of weapon and existing entity in slot");
180 #endif
181 m_InventoryMgr.TrySwapItemStorages(newWeaponEnt, existingEntityAtSlot, m_WeaponMoveCallback);
182 }
183 else
184 {
185 #ifdef AI_DEBUG
186 AddDebugMessage("Weapon is not in the weapon manager list, requesting move to EquipedWeaponStorageComponent");
187 #endif
188 m_InventoryMgr.TryMoveItemToStorage(newWeaponEnt, m_EqWeaponStorageComp, -1, m_WeaponMoveCallback);
189 }
190
191 return ENodeResult.RUNNING;
192 }
193
195 SCR_AIWeaponHandling.StartWeaponSwitchCharacter(m_ControlComp, newWeaponComp);
196
197 return ENodeResult.RUNNING;
198 }
199
200 return ENodeResult.FAIL;
201 }
202
203
204 override void OnAbort(AIAgent owner, Node nodeCausingAbort)
205 {
207
209 m_WeaponMoveCallback.Invalidate();
210
212 }
213
214 //--------------------------------------------------------------------------------------------
216 override TStringArray GetVariablesIn() { return s_aVarsIn; }
217
218 static override bool VisibleInPalette() { return true; }
219}
proto external Managed FindComponent(typename typeName)
proto external EntityPrefabData GetPrefabData()
Definition Node.c:13
proto bool GetVariableIn(string name, out void val)
static const int WEAPON_STATE_IDLE
ref SCR_AISwitchWeaponMovedCallback m_WeaponMoveCallback
override TStringArray GetVariablesIn()
override void OnAbort(AIAgent owner, Node nodeCausingAbort)
static const int WEAPON_STATE_MOVED
EquipedWeaponStorageComponent m_EqWeaponStorageComp
static ref TStringArray s_aVarsIn
static const int WEAPON_STATE_MOVING
static const string PORT_WEAPON_COMPONENT
override ENodeResult EOnTaskSimulate(AIAgent owner, float dt)
void OnMoveWeaponFinished(bool result)
static override bool VisibleInPalette()
Base class for nodes which handle magazine switching.
CompartmentAccessComponent m_CompartmentAccessComp
BaseWeaponManagerComponent m_WeaponMgrComp
CharacterControllerComponent m_ControlComp
SCR_InventoryStorageManagerComponent m_InventoryMgr
ENodeResult
Definition ENodeResult.c:13
LogLevel
Enum with severity of the logging message.
Definition LogLevel.c:14
array< string > TStringArray
Definition Types.c:385