Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
firearmactionattachmagazine.c
Go to the documentation of this file.
1class AttachMagazineActionReciveData : ActionReciveData
2{
3 ref InventoryLocation m_ilOldMagazine;
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
42 override ActionData CreateActionData()
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
213 override bool CanBePerformedFromInventory()
214 {
215 return true;
216 }
217
218 override bool CanBePerformedFromQuickbar()
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 Magazine magazine;
241 #ifdef SERVER
242 magazine = Magazine.Cast(target.GetObject());
243 #else
244 magazine = Magazine.Cast(player.GetWeaponManager().GetPreparedMagazine());
245 #endif
246 if (!magazine)
247 return false;
248
249 MagazineStorage mag = MagazineStorage.Cast(magazine);
250 if (!mag)
251 return false;
252
253 Weapon weapon = Weapon.Cast(item);
254 bool isLoadedMag = false;
255
256 for (int i = 0, count = weapon.GetMuzzleCount(); i < count; ++i)
257 isLoadedMag |= ( mag == weapon.GetMagazine( i ) );
258
259 return !isLoadedMag;
260 }
261
262 override ActionData CreateActionData()
263 {
264 AttachMagazineActionData action_data = new AttachMagazineActionData;
265 return action_data;
266 }
267
268
269 override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
270 {
271 super.WriteToContext(ctx, action_data);
272
273 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
274
275 action_data_am.m_ilOldMagazine.WriteToContext(ctx);
276 }
277
278 override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
279 {
280 if (!action_recive_data)
281 {
282 action_recive_data = new AttachMagazineActionReciveData;
283 }
284
285 super.ReadFromContext(ctx, action_recive_data);
286
288 if (!il.ReadFromContext(ctx))
289 return false;
290
291 AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
292 recive_data_am.m_ilOldMagazine = il;
293
294 return true;
295 }
296
297 override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
298 {
299 AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
300 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
301
302 action_data.m_MainItem = action_recive_data.m_MainItem;
303 if (!action_recive_data.m_Target)
304 {
305 action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
306 }
307 else
308 {
309 action_data.m_Target = action_recive_data.m_Target;
310 }
311 action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
312 }
313
314 override bool Post_SetupAction( ActionData action_data )
315 {
316 if ( !GetGame().IsDedicatedServer() )
317 {
318 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
319 int muzzle_index = wpn.GetCurrentMuzzle();
320
321 AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
322 am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
323
324 ActionTarget newTarget = new ActionTarget(action_data.m_Player.GetWeaponManager().GetPreparedMagazine(), null, -1, vector.Zero, -1);
325 action_data.m_Target = newTarget;
326
328
329 if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
330 {
331 return false;
332 }
333 am_action_data.m_ilOldMagazine = new_il;
334 }
335 return true;
336 }
337
338 override bool InventoryReservation( ActionData action_data)
339 {
340 if( (IsLocal() || !UseAcknowledgment()) && IsInstant() )
341 return true;
342
343 bool success = true;
344 InventoryLocation oldMagTargetInventoryLocation = NULL;
345 InventoryLocation targetInventoryLocation = NULL;
346 InventoryLocation handInventoryLocation = NULL;
347
348 PlayerBase player = action_data.m_Player;
349 AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
350
351 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
352 int muzzle_index = wpn.GetCurrentMuzzle();
353
354 Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
355
356 if (am_action_data.m_oldMagazine)
357 {
358 oldMagTargetInventoryLocation = new InventoryLocation;
359 if ( action_data.m_Player.GetInventory().HasInventoryReservation( am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine) )
360 {
361 success = false;
362 }
363 else
364 {
365 player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine,GameInventory.c_InventoryReservationTimeoutMS);
366 }
367 }
368
369 if (success)
370 {
371 targetInventoryLocation = new InventoryLocation;
372 targetInventoryLocation.SetAttachment( wpn, new_mag, InventorySlots.MAGAZINE);
373 if ( action_data.m_Player.GetInventory().HasInventoryReservation( new_mag, targetInventoryLocation) )
374 {
375 success = false;
376 if (am_action_data.m_oldMagazine)
377 {
378 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
379 }
380 }
381 else
382 {
383 action_data.m_Player.GetInventory().AddInventoryReservationEx( new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
384 }
385 }
386
387 if (success)
388 {
389 handInventoryLocation = new InventoryLocation;
390 handInventoryLocation.SetHands(action_data.m_Player, wpn);
391
392 if ( action_data.m_Player.GetInventory().HasInventoryReservation( wpn, handInventoryLocation) )
393 {
394 if (am_action_data.m_oldMagazine)
395 {
396 player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
397 }
398 player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
399 success = false;
400 }
401 else
402 {
403 action_data.m_Player.GetInventory().AddInventoryReservationEx( wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
404 }
405 }
406
407 if ( success )
408 {
409 if (am_action_data.m_ilOldMagazine)
410 action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
411
412 if (targetInventoryLocation)
413 action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
414
415 if (handInventoryLocation)
416 action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
417 }
418
419 return success;
420 }
421
422 override void Start( ActionData action_data )
423 {
424 super.Start( action_data );
425 AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
426 Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
427 Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
428 ClearInventoryReservationEx(action_data);
429 if ( action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn,mag,false) )
430 action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
431 else
432 action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
433 InventoryReservation(action_data);
434 }
435
436 override bool HasTarget()
437 {
438 return true;
439 }
440
441 override bool HasProgress()
442 {
443 return false;
444 }
445
446 override typename GetInputType()
447 {
449 }
450
451 override void CreateConditionComponents()
452 {
455 }
456};
const int AC_SINGLE_USE
Definition _constants.c:2
string m_Text
Definition actionbase.c:58
bool UseAcknowledgment()
ref CCIBase m_ConditionItem
Definition actionbase.c:64
bool IsInstant()
Definition actionbase.c:262
void ClearInventoryReservationEx(ActionData action_data)
ref CCTBase m_ConditionTarget
Definition actionbase.c:65
bool IsLocal()
Definition actionbase.c:256
script counterpart to engine's class Inventory
Definition inventory.c:79
const int c_InventoryReservationTimeoutMS
reservations
Definition inventory.c:712
InventoryLocation.
provides access to slot configuration
Serialization general interface. Serializer API works with:
Definition serializer.c:56
script counterpart to engine's class Weapon
void Start()
Plays all elements this effects consists of.
Definition effect.c:157
Magazine m_oldMagazine
AttachMagazineActionReciveData m_ilOldMagazine
proto native CGame GetGame()