Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
weaponattachmagazine.c
Go to the documentation of this file.
2 {
3  Magazine m_newMagazine;
4  ref InventoryLocation m_newSrc;
5 
6  void RemoveNewMagazineFromInventory (Weapon_Base w = NULL, WeaponStateBase parent = NULL)
7  {
8  m_newMagazine = NULL;
9  m_newSrc = NULL;
10  }
11 
12  override void OnEntry (WeaponEventBase e)
13  {
14  if(e)
15  {
16  if (!m_newSrc.IsValid())
17  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory m_newSrc=invalid, item not in bubble?");
18 
19  if (m_newMagazine && m_newSrc && m_newSrc.IsValid())
20  {
22  m_newMagazine.GetInventory().GetCurrentInventoryLocation(curr);
23 
24  if (m_newSrc.GetType() == InventoryLocationType.GROUND && curr.GetType() == InventoryLocationType.ATTACHMENT && curr.GetSlot() == InventorySlots.LEFTHAND)
25  {
26  // already in LH
27  }
28  else
29  {
31  lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
32  if (GameInventory.LocationSyncMoveEntity(m_newSrc, lhand))
33  {
34  if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, ok - new magazine removed from inv (inv->LHand)"); }
35  }
36  else
37  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, error - cannot new remove mag from inv");
38  }
39  }
40  else
41  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, error - no magazines configured for replace (m_old=m_new=NULL)");
42  }
43  super.OnEntry(e);
44  }
45 
46  override void OnAbort (WeaponEventBase e)
47  {
48  m_newMagazine = NULL;
49  m_newSrc = NULL;
50 
51  super.OnAbort(e);
52  }
53 
54  override void OnExit (WeaponEventBase e)
55  {
56  m_weapon.ShowMagazine();
57 
58  m_newMagazine = NULL;
59  m_newSrc = NULL;
60  super.OnExit(e);
61  }
62 
63  override bool SaveCurrentFSMState (ParamsWriteContext ctx)
64  {
65  if (!super.SaveCurrentFSMState(ctx))
66  return false;
67 
68  if (!ctx.Write(m_newMagazine))
69  {
70  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
71  return false;
72  }
73 
74  if (!OptionalLocationWriteToContext(m_newSrc, ctx))
75  {
76  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory.SaveCurrentFSMState: cannot write m_newSrc for weapon=" + m_weapon);
77  return false;
78  }
79  return true;
80  }
81 
82  override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
83  {
84  if (!super.LoadCurrentFSMState(ctx, version))
85  return false;
86 
87  if (!ctx.Read(m_newMagazine))
88  {
89  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
90  return false;
91  }
92 
93  if (!OptionalLocationReadFromContext(m_newSrc, ctx))
94  {
95  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory.LoadCurrentFSMState: cannot read m_newSrc for weapon=" + m_weapon);
96  return false;
97  }
98  return true;
99  }
100 };
101 
102 
103 class RemoveNewMagazineFromInventory_OnEntryShowMag extends RemoveNewMagazineFromInventory
104 {
105  override void OnEntry (WeaponEventBase e)
106  {
107  if(e)
108  m_weapon.ShowMagazine();
109  super.OnEntry(e);
110  }
111 };
112 
113 
114 class WeaponAttachMagazine extends WeaponStateBase
115 {
117  int m_actionType;
118 
120  ref AttachNewMagazine m_attach;
121  ref WeaponChamberFromAttMag_W4T m_chamber;
122  ref WeaponCharging_CK m_onCK;
124 
125  void WeaponAttachMagazine (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
126  {
127  m_action = action;
128  m_actionType = actionType;
129 
130  // setup nested state machine
131  m_start = new WeaponStartAction(m_weapon, this, m_action, m_actionType);
132  m_eject = new WeaponEjectCasing(m_weapon, this);
133  m_attach = new AttachNewMagazine(m_weapon, this);
134  m_chamber = new WeaponChamberFromAttMag_W4T(m_weapon, this);
135  m_onCK = new WeaponCharging_CK(m_weapon, this);
136 
137  // events: MS, MA, BE, CK
138  WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
139  WeaponEventBase __ms_ = new WeaponEventAnimMagazineShow;
140  WeaponEventBase __so_ = new WeaponEventAnimSliderOpen;
141  WeaponEventBase __ma_ = new WeaponEventAnimMagazineAttached;
142  WeaponEventBase __ck_ = new WeaponEventAnimCocked;
143 
144  m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
145 
146  m_fsm.AddTransition(new WeaponTransition( m_start, __ms_, m_attach));
147  m_fsm.AddTransition(new WeaponTransition( m_start, __so_, m_eject));
148  m_fsm.AddTransition(new WeaponTransition( m_eject, __ms_, m_attach));
149  m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_chamber, NULL, new GuardAnd(new WeaponGuardCurrentChamberEmpty(m_weapon), new WeaponGuardHasAmmo(m_weapon)))); // when opened, there is no __be_ event
150  m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_onCK, NULL, new GuardAnd(new WeaponGuardCurrentChamberEmpty(m_weapon), new GuardNot(new WeaponGuardHasAmmo(m_weapon)))));
151  m_fsm.AddTransition(new WeaponTransition( m_attach, _fin_, NULL));
152  m_fsm.AddTransition(new WeaponTransition( m_chamber, _fin_, NULL));
153  m_fsm.AddTransition(new WeaponTransition( m_onCK, _fin_, NULL));
154 
155  // Safety exits
156  m_fsm.AddTransition(new WeaponTransition(m_eject , _fin_, null));
157  m_fsm.AddTransition(new WeaponTransition(m_start , _fin_, null));
158 
159 
160  m_fsm.SetInitialState(m_start);
161  }
162 
163  override void OnEntry (WeaponEventBase e)
164  {
165  if (e)
166  {
167  Magazine mag = e.m_magazine;
168 
170  mag.GetInventory().GetCurrentInventoryLocation(newSrc);
171 
172  // move to LH
174  lhand.SetAttachment(e.m_player, mag, InventorySlots.LEFTHAND);
175  if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
176  {
177  if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, ok - new magazine removed from inv (inv->LHand)"); }
178  }
179  else
180  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, error - cannot new remove mag from inv");
181 
183  il.SetAttachment(m_weapon, mag, InventorySlots.MAGAZINE);
184  m_attach.m_newMagazine = mag;
185  m_attach.m_newDst = il;
186  }
187  super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
188  }
189 
190  override void OnAbort (WeaponEventBase e)
191  {
192  EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
193  Magazine mag = Magazine.Cast(leftHandItem);
194 
195  if(mag)
196  {
197  e.m_player.GetInventory().ClearInventoryReservationEx( mag , null );
199  e.m_player.GetInventory().FindFreeLocationFor( mag, FindInventoryLocationType.CARGO, il );
200 
201  if(!il || !il.IsValid())
202  {
203  if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
204  {
205  if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, ok - no inventory space for old magazine - dropped to ground"); }
206  }
207  else
208  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine");
209 
210  }
211  else
212  {
214  mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
215 
216  if (GameInventory.LocationSyncMoveEntity(oldSrc, il))
217  {
218  if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, ok - old magazine removed from wpn (LHand->inv)"); }
219  }
220  else
221  Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, error - cannot remove old mag from wpn");
222  }
223  }
224  super.OnAbort(e);
225  }
226 };
227 
LoadCurrentFSMState
override bool LoadCurrentFSMState(ParamsReadContext ctx, int version)
Definition: weaponchambering.c:867
Error
void Error(string err)
Messagebox with error message.
Definition: endebug.c:90
OnAbort
override void OnAbort()
Definition: remotedetonator.c:300
LogManager
Definition: debug.c:734
InventorySlots
provides access to slot configuration
Definition: inventoryslots.c:5
DayZPlayerUtils
private void DayZPlayerUtils()
cannot be instantiated
Definition: dayzplayerutils.c:461
OptionalLocationReadFromContext
bool OptionalLocationReadFromContext(out InventoryLocation loc, notnull ParamsReadContext ctx)
Definition: inventorylocation.c:605
OnExit
override void OnExit(HandEventBase e)
Definition: hand_states.c:28
wpnDebugPrint
void wpnDebugPrint(string s)
Definition: debug.c:9
m_eject
ref WeaponEjectCasingMultiMuzzle m_eject
Definition: weaponchambering.c:638
InventoryLocation
InventoryLocation.
Definition: inventorylocation.c:27
m_chamber
ref WeaponChambering_Base m_chamber
Definition: weaponchambering.c:639
m_action
class WeaponEndAction extends WeaponStartAction m_action
WeaponStartAction
simple class starting animation action specified by m_action and m_actionType
Definition: weaponchambering.c:2
WeaponActions
WeaponActions
actions
Definition: human.c:808
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
OptionalLocationWriteToContext
bool OptionalLocationWriteToContext(InventoryLocation loc, notnull ParamsWriteContext ctx)
Definition: inventorylocation.c:583
m_start
ref WeaponStateBase m_start
Definition: weaponchambering.c:637
SaveCurrentFSMState
override bool SaveCurrentFSMState(ParamsWriteContext ctx)
Definition: weaponchambering.c:848
InventoryLocationType
InventoryLocationType
types of Inventory Location
Definition: inventorylocation.c:3
Object
Definition: objecttyped.c:1
RemoveNewMagazineFromInventory
Definition: weaponattachmagazine.c:103
WeaponTransition
enum FSMTransition WeaponTransition
FindInventoryLocationType
FindInventoryLocationType
flags for searching locations in inventory
Definition: inventorylocation.c:15
WeaponGuardHasAmmo
void WeaponGuardHasAmmo(Weapon_Base w=NULL)
Definition: guards.c:99
WeaponEventBase
signalize mechanism manipulation
Definition: events.c:34
WeaponStateBase
represent weapon state base
Definition: bullethide.c:1
m_weapon
class WeaponGuardIsDestroyed extends WeaponGuardBase m_weapon
Definition: guards.c:583
Weapon_Base
shorthand
Definition: boltactionrifle_base.c:5
AttachNewMagazine
attach mag in LH into weapon
WeaponFSM
weapon finite state machine
WeaponEjectCasing
Definition: weaponejectcasingandchamberfromattmag.c:126
EntityAI
Definition: building.c:5
OnEntry
HandStateEquipped OnEntry
Definition: weaponchambering.c:208
GameInventory
script counterpart to engine's class Inventory
Definition: inventory.c:78