DayZ 1.24
Loading...
Searching...
No Matches
FirearmActionAttachMagazine.c
Go to the documentation of this file.
6{
9}
10
12{
13 //-----------------------------------------------------
14 // Action events and methods
15 //-----------------------------------------------------
17 {
18 m_Text = "#attach";
19 }
20
21 override int GetActionCategory()
22 {
23 return AC_SINGLE_USE;
24 }
25
26 override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item) //condition for action
27 {
28 if (!super.ActionCondition(player, target, item))
29 return false;
30
31 HumanCommandWeapons hcw = player.GetCommandModifier_Weapons();
32 Magazine mag = Magazine.Cast(target.GetObject());
34 return mag && (player.GetWeaponManager().CanAttachMagazine(wpn, mag) || player.GetWeaponManager().CanSwapMagazine(wpn, mag)) && (!hcw || hcw.GetRunningAction() != WeaponActions.RELOAD);
35 }
36
38 {
39 return true;
40 }
41
47
48
50 {
51 super.WriteToContext(ctx, action_data);
52
54
55 action_data_am.m_ilOldMagazine.WriteToContext(ctx);
56 }
57
59 {
62
63 super.ReadFromContext(ctx, action_recive_data);
64
66 if (!il.ReadFromContext(ctx))
67 return false;
68
70 recive_data_am.m_ilOldMagazine = il;
71
72 return true;
73 }
74
87
89 {
90 if (!GetGame().IsDedicatedServer())
91 {
92 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
93 int muzzle_index = wpn.GetCurrentMuzzle();
94
96 am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
97
99
100 if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
101 return false;
102 am_action_data.m_ilOldMagazine = new_il;
103 }
104 return true;
105 }
106
108 {
109 if ((IsLocal() || !UseAcknowledgment()) && IsInstant())
110 return true;
111
112 bool success = true;
116
117 PlayerBase player = action_data.m_Player;
119
120 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
121 int muzzle_index = wpn.GetCurrentMuzzle();
122
123 Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
124
125 if (am_action_data.m_oldMagazine)
126 {
128 if (action_data.m_Player.GetInventory().HasInventoryReservation(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine))
129 success = false;
130 else
131 player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine, GameInventory.c_InventoryReservationTimeoutMS);
132 }
133
134 if (success)
135 {
137 targetInventoryLocation.SetAttachment(wpn, new_mag, InventorySlots.MAGAZINE);
138 if (action_data.m_Player.GetInventory().HasInventoryReservation(new_mag, targetInventoryLocation))
139 {
140 success = false;
141 if (am_action_data.m_oldMagazine)
142 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine);
143 }
144 else
145 action_data.m_Player.GetInventory().AddInventoryReservationEx(new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
146 }
147
148 if (success)
149 {
151 handInventoryLocation.SetHands(action_data.m_Player, wpn);
152
153 if (action_data.m_Player.GetInventory().HasInventoryReservation(wpn, handInventoryLocation))
154 {
155 if (am_action_data.m_oldMagazine)
156 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine);
157 player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
158 success = false;
159 }
160 else
161 action_data.m_Player.GetInventory().AddInventoryReservationEx(wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
162 }
163
164 if (success)
165 {
166 if (am_action_data.m_ilOldMagazine)
167 action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
168
170 action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
171
173 action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
174 }
175
176 return success;
177 }
178
180 {
181 super.Start(action_data);
183 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
184 Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
186 if (action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn, mag, false))
187 action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
188 else
189 action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
191 }
192
194 {
195 return true;
196 }
197
199 {
200 return true;
201 }
202};
203
204
206{
210
211 override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item) //condition for action
212 {
213 if (!super.ActionCondition(player, target, item))
214 return false;
215
216 HumanCommandWeapons hcw = player.GetCommandModifier_Weapons();
217 if (hcw && hcw.GetRunningAction() == WeaponActions.RELOAD)
218 return false;
219
220 MagazineStorage mag = MagazineStorage.Cast(player.GetWeaponManager().GetPreparedMagazine());
221
222 if (!mag)
223 return false;
224
225 Weapon weapon = Weapon.Cast(item);
226 bool isLoadedMag = false;
227
228 for (int i = 0, count = weapon.GetMuzzleCount(); i < count; ++i)
229 isLoadedMag |= (mag == weapon.GetMagazine(i));
230
231 return !isLoadedMag;
232 }
233
239
240
242 {
243 super.WriteToContext(ctx, action_data);
244
246
247 action_data_am.m_ilOldMagazine.WriteToContext(ctx);
248 }
249
251 {
254
255 super.ReadFromContext(ctx, action_recive_data);
256
258 if (!il.ReadFromContext(ctx))
259 return false;
260
262 recive_data_am.m_ilOldMagazine = il;
263
264 return true;
265 }
266
268 {
271
272 action_data.m_MainItem = action_recive_data.m_MainItem;
273 if (!action_recive_data.m_Target)
274 action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
275 else
276 action_data.m_Target = action_recive_data.m_Target;
277 action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
278 }
279
281 {
282 if (!GetGame().IsDedicatedServer())
283 {
284 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
285 int muzzle_index = wpn.GetCurrentMuzzle();
286
288 am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
289
290 ActionTarget newTarget = new ActionTarget(action_data.m_Player.GetWeaponManager().GetPreparedMagazine(), null, -1, vector.Zero, -1);
291 action_data.m_Target = newTarget;
292
294
295 if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
296 return false;
297 am_action_data.m_ilOldMagazine = new_il;
298 }
299 return true;
300 }
301
303 {
304 if ((IsLocal() || !UseAcknowledgment()) && IsInstant())
305 return true;
306
307 bool success = true;
311
312 PlayerBase player = action_data.m_Player;
314
315 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
316 int muzzle_index = wpn.GetCurrentMuzzle();
317
318 Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
319
320 if (am_action_data.m_oldMagazine)
321 {
323 if (action_data.m_Player.GetInventory().HasInventoryReservation(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine))
324 success = false;
325 else
326 player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine, GameInventory.c_InventoryReservationTimeoutMS);
327 }
328
329 if (success)
330 {
332 targetInventoryLocation.SetAttachment(wpn, new_mag, InventorySlots.MAGAZINE);
333 if (action_data.m_Player.GetInventory().HasInventoryReservation(new_mag, targetInventoryLocation))
334 {
335 success = false;
336 if (am_action_data.m_oldMagazine)
337 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine);
338 }
339 else
340 action_data.m_Player.GetInventory().AddInventoryReservationEx(new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
341 }
342
343 if (success)
344 {
346 handInventoryLocation.SetHands(action_data.m_Player, wpn);
347
348 if (action_data.m_Player.GetInventory().HasInventoryReservation(wpn, handInventoryLocation))
349 {
350 if (am_action_data.m_oldMagazine)
351 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine);
352 player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
353 success = false;
354 }
355 else
356 action_data.m_Player.GetInventory().AddInventoryReservationEx(wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
357 }
358
359 if (success)
360 {
361 if (am_action_data.m_ilOldMagazine)
362 action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
363
365 action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
366
368 action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
369 }
370
371 return success;
372 }
373
375 {
376 super.Start(action_data);
378 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
379 Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
381 if (action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn, mag, false))
382 action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
383 else
384 action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
386 }
387
388 override bool HasTarget()
389 {
390 return true;
391 }
392
393 override bool HasProgress()
394 {
395 return false;
396 }
397
398 override typename GetInputType()
399 {
401 }
402
404 {
407 }
408};
const int AC_SINGLE_USE
Definition _constants.c:2
string m_Text
Definition ActionBase.c:49
bool UseAcknowledgment()
Definition ActionBase.c:821
ref CCIBase m_ConditionItem
Definition ActionBase.c:55
ref CCTBase m_ConditionTarget
Definition ActionBase.c:56
void ClearInventoryReservationEx(ActionData action_data)
Definition ActionBase.c:790
bool IsLocal()
Definition ActionBase.c:222
bool IsInstant()
Definition ActionBase.c:228
class ActionTargets ActionTarget
Magazine m_oldMagazine
AttachMagazineActionReciveData m_ilOldMagazine
override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data)
override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
override bool InventoryReservation(ActionData action_data)
override bool ActionConditionContinue(ActionData action_data)
override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
override void Start(ActionData action_data)
override bool Post_SetupAction(ActionData action_data)
override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
override void Start(ActionData action_data)
override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
override bool Post_SetupAction(ActionData action_data)
override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
override bool InventoryReservation(ActionData action_data)
override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data)
script counterpart to engine's class Inventory
Definition Inventory.c:79
const int c_InventoryReservationTimeoutMS
reservations
Definition Inventory.c:687
InventoryLocation.
provides access to slot configuration
Serialization general interface. Serializer API works with:
Definition Serializer.c:56
script counterpart to engine's class Weapon
static const vector Zero
Definition EnConvert.c:110
proto native CGame GetGame()
WeaponActions
actions
Definition human.c:796