DayZ Scripts
v1.21.156300 ยท Jun 20, 2023
 
Loading...
Searching...
No Matches
FirearmActionAttachMagazine.c
Go to the documentation of this file.
1class AttachMagazineActionReciveData : ActionReciveData
2{
4}
5class AttachMagazineActionData : ActionData
6{
8 Magazine m_oldMagazine;
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());
33 Weapon_Base wpn = Weapon_Base.Cast(item);
34 return mag && (player.GetWeaponManager().CanAttachMagazine(wpn,mag) || player.GetWeaponManager().CanSwapMagazine(wpn,mag)) && (!hcw || hcw.GetRunningAction() != WeaponActions.RELOAD);
35 }
36
37 override bool ActionConditionContinue( ActionData action_data )
38 {
39 return true;
40 }
41
43 {
44 AttachMagazineActionData action_data = new AttachMagazineActionData;
45 return action_data;
46 }
47
48
49 override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
50 {
51 super.WriteToContext(ctx, action_data);
52
53 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
54
55 action_data_am.m_ilOldMagazine.WriteToContext(ctx);
56 }
57
58 override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
59 {
60 if (!action_recive_data)
61 {
62 action_recive_data = new AttachMagazineActionReciveData;
63 }
64
65 super.ReadFromContext(ctx, action_recive_data);
66
68 if (!il.ReadFromContext(ctx))
69 return false;
70
71 AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
72 recive_data_am.m_ilOldMagazine = il;
73
74 return true;
75 }
76
77 override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
78 {
79 AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
80 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
81
82 action_data.m_MainItem = action_recive_data.m_MainItem;
83 if (!action_recive_data.m_Target)
84 {
85 action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
86 }
87 else
88 {
89 action_data.m_Target = action_recive_data.m_Target;
90 }
91 action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
92 }
93
94 override bool Post_SetupAction( ActionData action_data )
95 {
96 if ( !GetGame().IsDedicatedServer() )
97 {
98 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
99 int muzzle_index = wpn.GetCurrentMuzzle();
100
101 AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
102 am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
103
105
106 if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
107 {
108 return false;
109 }
110 am_action_data.m_ilOldMagazine = new_il;
111 }
112 return true;
113 }
114
115 override bool InventoryReservation( ActionData action_data)
116 {
117 if( (IsLocal() || !UseAcknowledgment()) && IsInstant() )
118 return true;
119
120 bool success = true;
121 InventoryLocation oldMagTargetInventoryLocation = NULL;
122 InventoryLocation targetInventoryLocation = NULL;
123 InventoryLocation handInventoryLocation = NULL;
124
125 PlayerBase player = action_data.m_Player;
126 AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
127
128 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
129 int muzzle_index = wpn.GetCurrentMuzzle();
130
131 Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
132
133 if (am_action_data.m_oldMagazine)
134 {
135 oldMagTargetInventoryLocation = new InventoryLocation;
136 if ( action_data.m_Player.GetInventory().HasInventoryReservation( am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine) )
137 {
138 success = false;
139 }
140 else
141 {
142 player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine,GameInventory.c_InventoryReservationTimeoutMS);
143 }
144 }
145
146 if (success)
147 {
148 targetInventoryLocation = new InventoryLocation;
149 targetInventoryLocation.SetAttachment( wpn, new_mag, InventorySlots.MAGAZINE);
150 if ( action_data.m_Player.GetInventory().HasInventoryReservation( new_mag, targetInventoryLocation) )
151 {
152 success = false;
153 if (am_action_data.m_oldMagazine)
154 {
155 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
156 }
157 }
158 else
159 {
160 action_data.m_Player.GetInventory().AddInventoryReservationEx( new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
161 }
162 }
163
164 if (success)
165 {
166 handInventoryLocation = new InventoryLocation;
167 handInventoryLocation.SetHands(action_data.m_Player, wpn);
168
169 if ( action_data.m_Player.GetInventory().HasInventoryReservation( wpn, handInventoryLocation) )
170 {
171 if (am_action_data.m_oldMagazine)
172 {
173 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
174 }
175 player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
176 success = false;
177 }
178 else
179 {
180 action_data.m_Player.GetInventory().AddInventoryReservationEx( wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
181 }
182 }
183
184 if ( success )
185 {
186 if (am_action_data.m_ilOldMagazine)
187 action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
188
189 if (targetInventoryLocation)
190 action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
191
192 if (handInventoryLocation)
193 action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
194 }
195
196 return success;
197 }
198
199 override void Start( ActionData action_data )
200 {
201 super.Start( action_data );
202 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
203 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
204 Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
205 ClearInventoryReservationEx(action_data);
206 if ( action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn,mag,false) )
207 action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
208 else
209 action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
210 InventoryReservation(action_data);
211 }
212
214 {
215 return true;
216 }
217
219 {
220 return true;
221 }
222};
223
224
226{
228 {
229 }
230
231 override bool ActionCondition( PlayerBase player, ActionTarget target, ItemBase item ) //condition for action
232 {
233 if (!super.ActionCondition( player, target, item ))
234 return false;
235
236 HumanCommandWeapons hcw = player.GetCommandModifier_Weapons();
237 if (hcw && hcw.GetRunningAction() == WeaponActions.RELOAD)
238 return false;
239
240 MagazineStorage mag = MagazineStorage.Cast(player.GetWeaponManager().GetPreparedMagazine());
241
242 if (!mag)
243 return false;
244
245 Weapon weapon = Weapon.Cast(item);
246 bool isLoadedMag = false;
247
248 for (int i = 0, count = weapon.GetMuzzleCount(); i < count; ++i)
249 isLoadedMag |= ( mag == weapon.GetMagazine( i ) );
250
251 return !isLoadedMag;
252 }
253
255 {
256 AttachMagazineActionData action_data = new AttachMagazineActionData;
257 return action_data;
258 }
259
260
261 override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
262 {
263 super.WriteToContext(ctx, action_data);
264
265 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
266
267 action_data_am.m_ilOldMagazine.WriteToContext(ctx);
268 }
269
270 override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
271 {
272 if (!action_recive_data)
273 {
274 action_recive_data = new AttachMagazineActionReciveData;
275 }
276
277 super.ReadFromContext(ctx, action_recive_data);
278
280 if (!il.ReadFromContext(ctx))
281 return false;
282
283 AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
284 recive_data_am.m_ilOldMagazine = il;
285
286 return true;
287 }
288
289 override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
290 {
291 AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
292 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
293
294 action_data.m_MainItem = action_recive_data.m_MainItem;
295 if (!action_recive_data.m_Target)
296 {
297 action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
298 }
299 else
300 {
301 action_data.m_Target = action_recive_data.m_Target;
302 }
303 action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
304 }
305
306 override bool Post_SetupAction( ActionData action_data )
307 {
308 if ( !GetGame().IsDedicatedServer() )
309 {
310 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
311 int muzzle_index = wpn.GetCurrentMuzzle();
312
313 AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
314 am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
315
316 ActionTarget newTarget = new ActionTarget(action_data.m_Player.GetWeaponManager().GetPreparedMagazine(), null, -1, vector.Zero, -1);
317 action_data.m_Target = newTarget;
318
320
321 if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
322 {
323 return false;
324 }
325 am_action_data.m_ilOldMagazine = new_il;
326 }
327 return true;
328 }
329
330 override bool InventoryReservation( ActionData action_data)
331 {
332 if( (IsLocal() || !UseAcknowledgment()) && IsInstant() )
333 return true;
334
335 bool success = true;
336 InventoryLocation oldMagTargetInventoryLocation = NULL;
337 InventoryLocation targetInventoryLocation = NULL;
338 InventoryLocation handInventoryLocation = NULL;
339
340 PlayerBase player = action_data.m_Player;
341 AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
342
343 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
344 int muzzle_index = wpn.GetCurrentMuzzle();
345
346 Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
347
348 if (am_action_data.m_oldMagazine)
349 {
350 oldMagTargetInventoryLocation = new InventoryLocation;
351 if ( action_data.m_Player.GetInventory().HasInventoryReservation( am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine) )
352 {
353 success = false;
354 }
355 else
356 {
357 player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine,GameInventory.c_InventoryReservationTimeoutMS);
358 }
359 }
360
361 if (success)
362 {
363 targetInventoryLocation = new InventoryLocation;
364 targetInventoryLocation.SetAttachment( wpn, new_mag, InventorySlots.MAGAZINE);
365 if ( action_data.m_Player.GetInventory().HasInventoryReservation( new_mag, targetInventoryLocation) )
366 {
367 success = false;
368 if (am_action_data.m_oldMagazine)
369 {
370 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
371 }
372 }
373 else
374 {
375 action_data.m_Player.GetInventory().AddInventoryReservationEx( new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
376 }
377 }
378
379 if (success)
380 {
381 handInventoryLocation = new InventoryLocation;
382 handInventoryLocation.SetHands(action_data.m_Player, wpn);
383
384 if ( action_data.m_Player.GetInventory().HasInventoryReservation( wpn, handInventoryLocation) )
385 {
386 if (am_action_data.m_oldMagazine)
387 {
388 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
389 }
390 player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
391 success = false;
392 }
393 else
394 {
395 action_data.m_Player.GetInventory().AddInventoryReservationEx( wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
396 }
397 }
398
399 if ( success )
400 {
401 if (am_action_data.m_ilOldMagazine)
402 action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
403
404 if (targetInventoryLocation)
405 action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
406
407 if (handInventoryLocation)
408 action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
409 }
410
411 return success;
412 }
413
414 override void Start( ActionData action_data )
415 {
416 super.Start( action_data );
417 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
418 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
419 Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
420 ClearInventoryReservationEx(action_data);
421 if ( action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn,mag,false) )
422 action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
423 else
424 action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
425 InventoryReservation(action_data);
426 }
427
428 override bool HasTarget()
429 {
430 return true;
431 }
432
433 override bool HasProgress()
434 {
435 return false;
436 }
437
438 override typename GetInputType()
439 {
441 }
442
444 {
447 }
448};
const int AC_SINGLE_USE
Definition _constants.c:2
bool UseAcknowledgment()
Definition ActionBase.c:892
ref CCIBase m_ConditionItem
Definition ActionBase.c:55
ref CCTBase m_ConditionTarget
Definition ActionBase.c:56
void ClearInventoryReservationEx(ActionData action_data)
Definition ActionBase.c:861
bool IsLocal()
Definition ActionBase.c:244
bool IsInstant()
Definition ActionBase.c:250
protected string m_Text
Definition ActionBase.c:49
class ActionTargets ActionTarget
Magazine m_oldMagazine
AttachMagazineActionReciveData m_ilOldMagazine
ItemBase m_MainItem
Definition ActionBase.c:28
PlayerBase m_Player
Definition ActionBase.c:33
ref array< ref InventoryLocation > m_ReservedInventoryLocations
Definition ActionBase.c:35
ref ActionTarget m_Target
Definition ActionBase.c:32
override bool Post_SetupAction(ActionData action_data)
override bool ActionConditionContinue(ActionData action_data)
override void Start(ActionData action_data)
override bool InventoryReservation(ActionData action_data)
override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data)
override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
override bool Post_SetupAction(ActionData action_data)
override void Start(ActionData action_data)
override bool InventoryReservation(ActionData action_data)
override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data)
override bool ActionCondition(PlayerBase player, ActionTarget target, ItemBase item)
override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
script counterpart to engine's class Inventory
Definition Inventory.c:77
const int c_InventoryReservationTimeoutMS
reservations
Definition Inventory.c:683
proto native int GetRunningAction()
returns -1 when no action is running or RELOAD,MECHANISM, ....
InventoryLocation.
proto native void SetHands(notnull EntityAI parent, EntityAI e)
sets current inventory location type to Hands
bool ReadFromContext(ParamsReadContext ctx)
proto native void SetAttachment(notnull EntityAI parent, EntityAI e, int slotId)
sets current inventory location type to Attachment with slot id set to <slotId>
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:798