Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
weaponreplacingmagandchambernext.c
Go to the documentation of this file.
1
6{
7 Magazine m_oldMagazine;
8 ref InventoryLocation m_newDst;
9
10 void DetachOldMagazine (Weapon_Base w = NULL, WeaponStateBase parent = NULL)
11 {
12 m_oldMagazine = NULL;
13 m_newDst = NULL;
14 }
15
16 override void OnEntry (WeaponEventBase e)
17 {
18 super.OnEntry(e);
19 }
20
21 override void OnAbort (WeaponEventBase e)
22 {
23 super.OnAbort(e);
24 m_oldMagazine = NULL;
25 m_newDst = NULL;
26 }
27
28 override void OnExit (WeaponEventBase e)
29 {
30 if (m_oldMagazine)
31 {
33 e.m_player.GetInventory().FindFreeLocationFor( m_oldMagazine , FindInventoryLocationType.CARGO, il );
34
35 if (!m_newDst || !m_newDst.IsValid() || m_newDst.GetType() == InventoryLocationType.GROUND)
36 {
37 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_oldMagazine))
38 {
39 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - no inventory space for old magazine - dropped to ground"); }
40 }
41 else
42 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine");
43
44 }
45 else
46 {
48 m_oldMagazine.GetInventory().GetCurrentInventoryLocation(oldSrc);
49
50 if (GameInventory.LocationSyncMoveEntity(oldSrc, m_newDst))
51 {
52 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - old magazine removed from wpn (LHand->inv)"); }
53 }
54 else
55 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot remove old mag from wpn");
56 }
57 }
58
59 m_weapon.HideMagazine();
60 m_oldMagazine = NULL;
61 m_newDst = null;
62 super.OnExit(e);
63 }
64
65 override bool SaveCurrentFSMState (ParamsWriteContext ctx)
66 {
67 if (!super.SaveCurrentFSMState(ctx))
68 return false;
69
70 if (!ctx.Write(m_oldMagazine))
71 {
72 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_oldMagazine for weapon=" + m_weapon);
73 return false;
74 }
75
76
77 if (!OptionalLocationWriteToContext(m_newDst, ctx))
78 {
79 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
80 return false;
81 }
82
83 return true;
84 }
85
86 override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
87 {
88 if (!super.LoadCurrentFSMState(ctx, version))
89 return false;
90
91 if (!ctx.Read(m_oldMagazine))
92 {
93 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_oldMagazine for weapon=" + m_weapon);
94 return false;
95 }
96
97 if (!OptionalLocationReadFromContext(m_newDst, ctx))
98 {
99 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
100 return false;
101 }
102 return true;
103 }
104};
105
111{
112};
113
122{
123 Magazine m_newMagazine;
124 ref InventoryLocation m_newDst;
125
126 override void OnEntry (WeaponEventBase e)
127 {
128 super.OnEntry(e);
129
130 if( e )
131 {
132 if (!m_newMagazine || !m_newDst || !m_newDst.IsValid())
133 {
134 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine, error - m_newMagazine(" + m_newMagazine + ") or destination(" + InventoryLocation.DumpToStringNullSafe(m_newDst) + ") is not set ");
135 }
136 else
137 {
138 e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , m_newDst );
139 m_weapon.ShowMagazine();
141 lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
142
143 if (GameInventory.LocationSyncMoveEntity(lhand, m_newDst))
144 {
145 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine, ok - new magazine removed from inv (LHand->Att)"); }
146 }
147 else
148 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine, error - cannot remove new mag from LHand");
149 }
150 }
151 }
152
153 override void OnAbort (WeaponEventBase e)
154 {
155 //m_weapon.HideMagazine();
156
157 m_newMagazine = NULL;
158 m_newDst = NULL;
159
160 super.OnAbort(e);
161 }
162
163 override void OnExit (WeaponEventBase e)
164 {
165 m_newMagazine = NULL;
166 m_newDst = NULL;
167
168 super.OnExit(e);
169 }
170
171 override bool SaveCurrentFSMState (ParamsWriteContext ctx)
172 {
173 if (!super.SaveCurrentFSMState(ctx))
174 return false;
175
176 if (!ctx.Write(m_newMagazine))
177 {
178 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
179 return false;
180 }
181
182 if (!OptionalLocationWriteToContext(m_newDst, ctx))
183 {
184 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
185 return false;
186 }
187
188 return true;
189 }
190
191 override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
192 {
193 if (!super.LoadCurrentFSMState(ctx, version))
194 return false;
195
196 if (!ctx.Read(m_newMagazine))
197 {
198 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
199 return false;
200 }
201 if (!OptionalLocationReadFromContext(m_newDst, ctx))
202 {
203 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
204 return false;
205 }
206
207 return true;
208 }
209};
210
215{
216 Magazine m_newMagazine;
217 ref InventoryLocation m_newDst;
218
219 void AttachNewMagazine (Weapon_Base w = NULL, WeaponStateBase parent = NULL)
220 {
221 m_newMagazine = NULL;
222 m_newDst = NULL;
223 }
224
225 override bool IsWaitingForActionFinish ()
226 {
227 return true;
228 }
229
230 override void OnEntry (WeaponEventBase e)
231 {
232 super.OnEntry(e);
233 if (e)
234 {
235 if (m_newMagazine && m_newDst)
236 {
238 if (m_newMagazine.GetInventory().GetCurrentInventoryLocation(il))
239 {
241 lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
242 if (GameInventory.LocationSyncMoveEntity(il, m_newDst))
243 {
244 m_weapon.ShowMagazine();
245 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, ok - attached new magazine (LHand->dst)"); }
246 }
247 else
248 {
249 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, error - cannot attach new magazine!"); }
250 }
251 }
252 else
253 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, error - cannot get curr location");
254 }
255 else
256 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, error - no magazines configured for replace (m_new=NULL)");
257 }
258 }
259
260 override void OnAbort (WeaponEventBase e)
261 {
262 if (m_newMagazine && m_newDst)
263 {
264 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_newMagazine))
265 {
266 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, ok - aborting, detached magazine dropped to ground"); }
267 }
268 else
269 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, error - cannot abort detaching of magazine");
270
271 m_weapon.HideMagazine(); // force hide on abort
272 }
273
274 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, aborting, but attached new magazine already in place"); }
275 m_newMagazine = NULL;
276 m_newDst = NULL;
277
278 super.OnAbort(e);
279 }
280
281 override void OnExit (WeaponEventBase e)
282 {
283 m_newMagazine = NULL;
284 m_newDst = NULL;
285 super.OnExit(e);
286 }
287
288 override bool SaveCurrentFSMState (ParamsWriteContext ctx)
289 {
290 if (!super.SaveCurrentFSMState(ctx))
291 return false;
292
293 if (!ctx.Write(m_newMagazine))
294 {
295 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
296 return false;
297 }
298
299 if (!OptionalLocationWriteToContext(m_newDst, ctx))
300 {
301 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
302 return false;
303 }
304 return true;
305 }
306
307 override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
308 {
309 if (!super.LoadCurrentFSMState(ctx, version))
310 return false;
311
312 if (!ctx.Read(m_newMagazine))
313 {
314 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
315 return false;
316 }
317 if (!OptionalLocationReadFromContext(m_newDst, ctx))
318 {
319 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
320 return false;
321 }
322 return true;
323 }
324};
325
326class AttachNewMagazine_W4T extends WeaponStateBase
327{
328 override bool IsWaitingForActionFinish () { return true; }
329};
330
331
339{
340 WeaponActions m_action;
341 int m_actionType;
342
344 ref DetachOldMagazine m_detach;
345 ref OldMagazineHide m_hideOld;
346 ref SwapOldAndNewMagazine m_swapMags;
347 ref AttachNewMagazine_W4T m_attach;
348 ref WeaponChamberFromAttMag_W4T m_chamber;
349 ref WeaponCharging_CK m_onCK;
351
352 // substates configuration
353 Magazine m_oldMagazine;
354 Magazine m_newMagazine;
355 ref InventoryLocation m_newDst;
356
357 void WeaponReplacingMagAndChamberNext (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
358 {
359 m_action = action;
360 m_actionType = actionType;
361 m_newMagazine = NULL;
362
363 // setup nested state machine
364 m_start = new WeaponStartAction(m_weapon, this, m_action, m_actionType);
366 m_detach = new DetachOldMagazine(m_weapon, this);
367 m_hideOld = new OldMagazineHide(m_weapon, this);
368 m_swapMags = new SwapOldAndNewMagazine(m_weapon, this);
369 m_attach = new AttachNewMagazine_W4T(m_weapon, this);
370 m_chamber = new WeaponChamberFromAttMag_W4T(m_weapon, this);
371 m_onCK = new WeaponCharging_CK(m_weapon, this);
372
373 // events
374 WeaponEventBase __so_ = new WeaponEventAnimSliderOpen;
375 WeaponEventBase __md_ = new WeaponEventAnimMagazineDetached;
376 WeaponEventBase __mh_ = new WeaponEventAnimMagazineHide;
377 WeaponEventBase __ms_ = new WeaponEventAnimMagazineShow;
378 WeaponEventBase __ma_ = new WeaponEventAnimMagazineAttached;
379 WeaponEventBase __ck_ = new WeaponEventAnimCocked;
380 WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
381
382 m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
383 m_fsm.AddTransition(new WeaponTransition( m_start, __md_, m_detach));
384 m_fsm.AddTransition(new WeaponTransition( m_start, __so_, m_eject));
385 m_fsm.AddTransition(new WeaponTransition( m_eject, __md_, m_detach));
386 m_fsm.AddTransition(new WeaponTransition( m_detach, __mh_, m_hideOld));
387 m_fsm.AddTransition(new WeaponTransition( m_hideOld, __ms_, m_swapMags));
388 m_fsm.AddTransition(new WeaponTransition(m_swapMags, __ma_, m_attach));
389 m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_chamber, NULL, new GuardAnd(new WeaponGuardCurrentChamberEmpty(m_weapon), new WeaponGuardHasAmmo(m_weapon))));
390 m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_onCK));
391
392 m_fsm.AddTransition(new WeaponTransition( m_attach, _fin_, NULL));
393 m_fsm.AddTransition(new WeaponTransition( m_chamber, _fin_, NULL));
394 m_fsm.AddTransition(new WeaponTransition( m_onCK, _fin_, NULL));
395
396 // Safety exits
397 m_fsm.AddTransition(new WeaponTransition(m_swapMags, _fin_, null));
398 m_fsm.AddTransition(new WeaponTransition(m_hideOld, _fin_, null));
399 m_fsm.AddTransition(new WeaponTransition(m_detach, _fin_, null));
400 m_fsm.AddTransition(new WeaponTransition(m_eject, _fin_, null));
401 m_fsm.AddTransition(new WeaponTransition(m_start, _fin_, null));
402
403 m_fsm.SetInitialState(m_start);
404 }
405
406 override void OnEntry (WeaponEventBase e)
407 {
408 if (e != NULL)
409 {
410 WeaponEventSwapMagazine se;
411 if (Class.CastTo(se, e))
412 {
413 int mi = m_weapon.GetCurrentMuzzle();
414 m_oldMagazine = m_weapon.GetMagazine(mi);
415 m_newMagazine = se.m_magazine;
416 m_newDst = se.m_dst;
417
418 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNext, m_oldMagazine= " + m_oldMagazine + " m_newMagazine= " + m_newMagazine + " m_oldMagazineDst= " + typename.EnumToString(InventoryLocationType, se.m_dst.GetType())); }
419
422
423 if (!m_newMagazine.GetInventory().GetCurrentInventoryLocation(newSrc))
424 {
425 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNext cannot get curr inv loc of NEW mag=" + Object.GetDebugName(m_newMagazine));
426 }
427 if (!m_oldMagazine.GetInventory().GetCurrentInventoryLocation(oldSrc))
428 {
429 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNext cannot get curr inv loc of OLD mag=" + Object.GetDebugName(m_oldMagazine));
430 }
431
432 // move to LH
434 lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
435
436 if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
437 {
438 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, ok - new magazine removed from inv (inv->LHand)"); }
439 }
440 else
441 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, error - cannot new remove mag from inv");
442
443 m_detach.m_oldMagazine = m_oldMagazine;
444 m_detach.m_newDst = m_newDst;
445
446 m_swapMags.m_newMagazine = m_newMagazine;
447
448 oldSrc.SetItem(m_newMagazine);
449 m_swapMags.m_newDst = oldSrc;
450 }
451 }
452 super.OnEntry(e);
453 }
454
455 override void OnExit (WeaponEventBase e)
456 {
457 super.OnExit(e);
458
459 EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
460 Magazine mag = Magazine.Cast(leftHandItem);
461
462 if(mag)
463 {
464 if (m_newMagazine)
465 e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , null );
466 if (m_oldMagazine)
467 e.m_player.GetInventory().ClearInventoryReservationEx( m_oldMagazine , null );
468
470
471 e.m_player.GetInventory().FindFreeLocationFor(mag, FindInventoryLocationType.CARGO, il);
472
473 if (!il.IsValid())
474 {
475 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
476 {
477 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - no inventory space for old magazine - dropped to ground - exit"); }
478 }
479 else
480 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine - exit");
481
482 }
483 else
484 {
486 mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
487
489 {
490 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - old magazine removed from wpn (LHand->inv) - exit"); }
491 }
492 else
493 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot remove old mag from wpn - exit");
494 }
495 }
496
497 m_oldMagazine = NULL;
498 m_newMagazine = NULL;
499 m_newDst = NULL;
500 }
501
502 override void OnAbort(WeaponEventBase e)
503 {
504 super.OnAbort(e);
505
506 EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
507 Magazine mag = Magazine.Cast(leftHandItem);
508
509 if (mag)
510 {
511 if (m_newMagazine)
512 e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , null );
513 if (m_oldMagazine)
514 e.m_player.GetInventory().ClearInventoryReservationEx( m_oldMagazine , null );
515
517
518 e.m_player.GetInventory().FindFreeLocationFor(mag, FindInventoryLocationType.CARGO, il);
519
520 if (!il.IsValid())
521 {
522 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
523 {
524 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - no inventory space for old magazine - dropped to ground - abort"); }
525 }
526 else
527 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine - abort");
528
529 }
530 else
531 {
533 mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
534
536 {
537 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - old magazine removed from wpn (LHand->inv) - abort"); }
538 }
539 else
540 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot remove old mag from wpn - abort");
541 }
542 }
543 }
544
545 override bool SaveCurrentFSMState (ParamsWriteContext ctx)
546 {
547 if (!super.SaveCurrentFSMState(ctx))
548 return false;
549
550 if (!ctx.Write(m_newMagazine))
551 {
552 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
553 return false;
554 }
555
556 if (!ctx.Write(m_oldMagazine))
557 {
558 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_oldMagazine for weapon=" + m_weapon);
559 return false;
560 }
561
562 if (!m_newDst.WriteToContext(ctx))
563 {
564 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
565 return false;
566 }
567
568 return true;
569 }
570
571 override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
572 {
573 if (!super.LoadCurrentFSMState(ctx, version))
574 return false;
575
576 if (!ctx.Read(m_newMagazine))
577 {
578 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
579 return false;
580 }
581 if (!ctx.Read(m_oldMagazine))
582 {
583 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_oldMagazine for weapon=" + m_weapon);
584 return false;
585 }
586 m_newDst = new InventoryLocation;
587
588 if (!m_newDst.ReadFromContext(ctx))
589 {
590 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
591 return false;
592 }
593
594 return true;
595 }
596};
597
605{
606 WeaponActions m_action;
607 int m_actionType;
608
610 ref WeaponDetachingMag_StoreRemoveChamberBullet m_detach;
611 ref OldMagazineHide m_hideOld;
612 ref SwapOldAndNewMagazine m_swapMags;
613 ref AttachNewMagazine_W4T m_attach;
614 ref WeaponChamberFromAttMagOpenbolt_W4T m_chamber;
615 ref WeaponChargingOpenBolt_CK m_onCK;
616
617 // substates configuration
618 Magazine m_oldMagazine;
619 Magazine m_newMagazine;
620 ref InventoryLocation m_newDst;
621
622 void WeaponReplacingMagAndChamberNextOpenBoltCharged (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
623 {
624 m_action = action;
625 m_actionType = actionType;
626 m_newMagazine = NULL;
627
628 // setup nested state machine
629 m_start = new WeaponStartAction(m_weapon, this, m_action, m_actionType);
630 m_detach = new WeaponDetachingMag_StoreRemoveChamberBullet(m_weapon, this);
631 m_hideOld = new OldMagazineHide(m_weapon, this);
632 m_swapMags = new SwapOldAndNewMagazine(m_weapon, this);
633 m_attach = new AttachNewMagazine_W4T(m_weapon, this);
634 m_chamber = new WeaponChamberFromAttMagOpenbolt_W4T(m_weapon, this);
635 m_onCK = new WeaponChargingOpenBolt_CK(m_weapon, this);
636
637 // events
638 WeaponEventBase __so_ = new WeaponEventAnimSliderOpen;
639 WeaponEventBase __md_ = new WeaponEventAnimMagazineDetached;
640 WeaponEventBase __mh_ = new WeaponEventAnimMagazineHide;
641 WeaponEventBase __ms_ = new WeaponEventAnimMagazineShow;
642 WeaponEventBase __ma_ = new WeaponEventAnimMagazineAttached;
643 WeaponEventBase __ck_ = new WeaponEventAnimCocked;
644 WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
645
646 m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
647 m_fsm.AddTransition(new WeaponTransition( m_start, __md_, m_detach));
648 m_fsm.AddTransition(new WeaponTransition( m_detach, __mh_, m_hideOld));
649 m_fsm.AddTransition(new WeaponTransition( m_hideOld, __ms_, m_swapMags));
650 m_fsm.AddTransition(new WeaponTransition( m_swapMags, __ma_, m_chamber, NULL, new GuardAnd(new WeaponGuardWeaponCharged(m_weapon), new WeaponGuardHasAmmo(m_weapon))));
651 m_fsm.AddTransition(new WeaponTransition( m_swapMags, __ma_, m_attach));
652
653
654 m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_chamber, NULL, new WeaponGuardHasAmmo(m_weapon)));
655 m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_onCK));
656
657 m_fsm.AddTransition(new WeaponTransition( m_attach, _fin_, NULL));
658 m_fsm.AddTransition(new WeaponTransition( m_chamber, _fin_, NULL));
659 m_fsm.AddTransition(new WeaponTransition( m_onCK, _fin_, NULL));
660
661 // Safety exits
662 m_fsm.AddTransition(new WeaponTransition(m_swapMags, _fin_, null));
663 m_fsm.AddTransition(new WeaponTransition(m_hideOld, _fin_, null));
664 m_fsm.AddTransition(new WeaponTransition(m_detach, _fin_, null));
665 m_fsm.AddTransition(new WeaponTransition(m_start, _fin_, null));
666
667 m_fsm.SetInitialState(m_start);
668 }
669
670 override void OnEntry (WeaponEventBase e)
671 {
672 if (e != NULL)
673 {
674 WeaponEventSwapMagazine se;
675 if (Class.CastTo(se, e))
676 {
677 int mi = m_weapon.GetCurrentMuzzle();
678 m_oldMagazine = m_weapon.GetMagazine(mi);
679 m_newMagazine = se.m_magazine;
680 m_newDst = se.m_dst;
681
682 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, m_oldMagazine= " + m_oldMagazine + " m_newMagazine= " + m_newMagazine + " m_oldMagazineDst= " + typename.EnumToString(InventoryLocationType, se.m_dst.GetType())); }
683
686
687 if (!m_newMagazine.GetInventory().GetCurrentInventoryLocation(newSrc))
688 {
689 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged cannot get curr inv loc of NEW mag=" + Object.GetDebugName(m_newMagazine));
690 }
691 if (!m_oldMagazine.GetInventory().GetCurrentInventoryLocation(oldSrc))
692 {
693 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged cannot get curr inv loc of OLD mag=" + Object.GetDebugName(m_oldMagazine));
694 }
695
696 // move to LH
698 lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
699
700 if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
701 {
702 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, ok - new magazine removed from inv (inv->LHand)"); }
703 }
704 else
705 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, error - cannot new remove mag from inv");
706
707 m_detach.m_magazine = m_oldMagazine;
708 m_detach.m_dst = m_newDst;
709
710 m_swapMags.m_newMagazine = m_newMagazine;
711
712 oldSrc.SetItem(m_newMagazine);
713 m_swapMags.m_newDst = oldSrc;
714 }
715 }
716 super.OnEntry(e);
717 }
718
719 override void OnExit (WeaponEventBase e)
720 {
721 super.OnExit(e);
722
723 EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
724 Magazine mag = Magazine.Cast(leftHandItem);
725
726 if(mag)
727 {
728 if (m_newMagazine)
729 e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , null );
730 if (m_oldMagazine)
731 e.m_player.GetInventory().ClearInventoryReservationEx( m_oldMagazine , null );
732
734
735 e.m_player.GetInventory().FindFreeLocationFor(mag, FindInventoryLocationType.CARGO, il);
736
737 if (!il.IsValid())
738 {
739 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
740 {
741 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, ok - no inventory space for old magazine - dropped to ground - exit"); }
742 }
743 else
744 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, error - cannot drop magazine from left hand after not found inventory space for old magazine - exit");
745
746 }
747 else
748 {
750 mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
751
753 {
754 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, ok - old magazine removed from wpn (LHand->inv) - exit"); }
755 }
756 else
757 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, error - cannot remove old mag from wpn - exit");
758 }
759 }
760
761 m_oldMagazine = NULL;
762 m_newMagazine = NULL;
763 m_newDst = NULL;
764 }
765
766 override void OnAbort(WeaponEventBase e)
767 {
768 super.OnAbort(e);
769
770 EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
771 Magazine mag = Magazine.Cast(leftHandItem);
772
773 if (mag)
774 {
775 if (m_newMagazine)
776 e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , null );
777 if (m_oldMagazine)
778 e.m_player.GetInventory().ClearInventoryReservationEx( m_oldMagazine , null );
779
781
782 e.m_player.GetInventory().FindFreeLocationFor(mag, FindInventoryLocationType.CARGO, il);
783
784 if (!il.IsValid())
785 {
786 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
787 {
788 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, ok - no inventory space for old magazine - dropped to ground - abort"); }
789 }
790 else
791 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, error - cannot drop magazine from left hand after not found inventory space for old magazine - abort");
792
793 }
794 else
795 {
797 mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
798
800 {
801 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, ok - old magazine removed from wpn (LHand->inv) - abort"); }
802 }
803 else
804 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged, error - cannot remove old mag from wpn - abort");
805 }
806 }
807 }
808
809 override bool SaveCurrentFSMState (ParamsWriteContext ctx)
810 {
811 if (!super.SaveCurrentFSMState(ctx))
812 return false;
813
814 if (!ctx.Write(m_newMagazine))
815 {
816 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
817 return false;
818 }
819
820 if (!ctx.Write(m_oldMagazine))
821 {
822 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged.SaveCurrentFSMState: cannot write m_oldMagazine for weapon=" + m_weapon);
823 return false;
824 }
825
826 if (!m_newDst.WriteToContext(ctx))
827 {
828 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
829 return false;
830 }
831
832 return true;
833 }
834
835 override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
836 {
837 if (!super.LoadCurrentFSMState(ctx, version))
838 return false;
839
840 if (!ctx.Read(m_newMagazine))
841 {
842 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
843 return false;
844 }
845 if (!ctx.Read(m_oldMagazine))
846 {
847 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged.LoadCurrentFSMState: cannot read m_oldMagazine for weapon=" + m_weapon);
848 return false;
849 }
850 m_newDst = new InventoryLocation;
851
852 if (!m_newDst.ReadFromContext(ctx))
853 {
854 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNextOpenBoltCharged.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
855 return false;
856 }
857
858 return true;
859 }
860};
861
862
863
864
865
866
void wpnDebugPrint(string s)
Definition debug.c:9
attach mag in LH into weapon
Super root of all classes in Enforce script.
Definition enscript.c:11
detaches old magazine from weapon and stores it in left hand (LH)
script counterpart to engine's class Inventory
Definition inventory.c:79
static proto native bool LocationSyncMoveEntity(notnull InventoryLocation src_loc, notnull InventoryLocation dst_loc)
synchronously removes item from current inventory location and adds it to destination no anims involv...
InventoryLocation.
provides access to slot configuration
hides old magazine, but keep it in LH
Serialization general interface. Serializer API works with:
Definition serializer.c:56
old magazine to inventory, new to left hand
signalize mechanism manipulation
Definition events.c:35
weapon finite state machine
replace current magazine with new one
replace current magazine with new one for weapon charged with open position bolt
simple class starting animation action specified by m_action and m_actionType
represent weapon state base
Definition bullethide.c:2
Magazine m_oldMagazine
void Error(string err)
Messagebox with error message.
Definition endebug.c:90
class WeaponGuardIsDestroyed extends WeaponGuardBase m_weapon
Definition guards.c:604
void WeaponGuardHasAmmo(Weapon_Base w=NULL)
Definition guards.c:99
HandStateEquipped OnEntry
FindInventoryLocationType
flags for searching locations in inventory
bool OptionalLocationReadFromContext(out InventoryLocation loc, notnull ParamsReadContext ctx)
InventoryLocationType
types of Inventory Location
bool OptionalLocationWriteToContext(InventoryLocation loc, notnull ParamsWriteContext ctx)
override void OnAbort()
enum FSMTransition WeaponTransition
class WeaponChambering extends WeaponStateBase IsWaitingForActionFinish
ref WeaponStateBase m_start
class WeaponChambering_Chamber_OnEntry extends WeaponChambering_Base OnExit
class WeaponEndAction extends WeaponStartAction m_action
override bool LoadCurrentFSMState(ParamsReadContext ctx, int version)
ref WeaponChambering_Base m_chamber
ref WeaponEjectCasingMultiMuzzle m_eject
override bool SaveCurrentFSMState(ParamsWriteContext ctx)