Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
weaponattachmagazine.c
Go to the documentation of this file.
2{
3 Magazine m_newMagazine;
4 ref InventoryLocation m_newSrc;
5
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
103class 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
114class WeaponAttachMagazine extends WeaponStateBase
115{
116 WeaponActions m_action;
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);
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
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
228class WeaponAttachMagazineOpenBoltCharged extends WeaponStateBase
229{
230 WeaponActions m_action;
231 int m_actionType;
232
234 ref AttachNewMagazine m_attach;
235 ref WeaponStateBase m_attach_W;
236 ref WeaponChamberFromAttMagOpenbolt_W4T m_chamber;
237 ref WeaponChargingOpenBolt_CK m_onCK;
238
239 void WeaponAttachMagazineOpenBoltCharged (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
240 {
241 m_action = action;
242 m_actionType = actionType;
243
244 // setup nested state machine
245 m_start = new WeaponStartAction(m_weapon, this, m_action, m_actionType);
246 m_attach = new AttachNewMagazine(m_weapon, this);
247 m_attach_W = new WeaponStateBase(m_weapon, this);
248 m_chamber = new WeaponChamberFromAttMagOpenbolt_W4T(m_weapon, this);
249 m_onCK = new WeaponChargingOpenBolt_CK(m_weapon, this);
250
251
252
253 // events: MS, MA, BE, CK
254 WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
255 WeaponEventBase __ms_ = new WeaponEventAnimMagazineShow;
256 WeaponEventBase __so_ = new WeaponEventAnimSliderOpen;
257 WeaponEventBase __ma_ = new WeaponEventAnimMagazineAttached;
258 WeaponEventBase __ck_ = new WeaponEventAnimCocked;
259
260 m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
261
262 m_fsm.AddTransition(new WeaponTransition( m_start, __ms_, m_attach));
263 m_fsm.AddTransition(new WeaponTransition( m_attach, __ma_, m_chamber, NULL, new GuardAnd(new WeaponGuardWeaponCharged(m_weapon), new WeaponGuardHasAmmo(m_weapon))));
264 m_fsm.AddTransition(new WeaponTransition( m_attach, __ma_, m_attach_W));
265 m_fsm.AddTransition(new WeaponTransition( m_attach_W, __ck_, m_chamber, NULL, new WeaponGuardHasAmmo(m_weapon)));
266 m_fsm.AddTransition(new WeaponTransition( m_attach_W, __ck_, m_onCK));
267
268 m_fsm.AddTransition(new WeaponTransition( m_attach, _fin_, NULL));
269 m_fsm.AddTransition(new WeaponTransition( m_attach_W, _fin_, NULL));
270 m_fsm.AddTransition(new WeaponTransition( m_chamber, _fin_, NULL));
271 m_fsm.AddTransition(new WeaponTransition( m_onCK, _fin_, NULL));
272
273
274 // Safety exits
275 m_fsm.AddTransition(new WeaponTransition(m_start , _fin_, null));
276
277 m_fsm.SetInitialState(m_start);
278 }
279
280 override void OnEntry (WeaponEventBase e)
281 {
282 if (e)
283 {
284 Magazine mag = e.m_magazine;
285
287 mag.GetInventory().GetCurrentInventoryLocation(newSrc);
288
289 // move to LH
291 lhand.SetAttachment(e.m_player, mag, InventorySlots.LEFTHAND);
292 if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
293 {
294 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, ok - new magazine removed from inv (inv->LHand)"); }
295 }
296 else
297 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazineOpenBoltCharged, error - cannot new remove mag from inv");
298
300 il.SetAttachment(m_weapon, mag, InventorySlots.MAGAZINE);
301 m_attach.m_newMagazine = mag;
302 m_attach.m_newDst = il;
303 }
304 super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
305 }
306
307 override void OnAbort (WeaponEventBase e)
308 {
309 EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
310 Magazine mag = Magazine.Cast(leftHandItem);
311
312 if(mag)
313 {
314 e.m_player.GetInventory().ClearInventoryReservationEx( mag , null );
316 e.m_player.GetInventory().FindFreeLocationFor( mag, FindInventoryLocationType.CARGO, il );
317
318 if(!il || !il.IsValid())
319 {
320 if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
321 {
322 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, ok - no inventory space for old magazine - dropped to ground"); }
323 }
324 else
325 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazineOpenBoltCharged, error - cannot drop magazine from left hand after not found inventory space for old magazine");
326
327 }
328 else
329 {
331 mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
332
334 {
335 if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazine, ok - old magazine removed from wpn (LHand->inv)"); }
336 }
337 else
338 Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponAttachMagazineOpenBoltCharged, error - cannot remove old mag from wpn");
339 }
340 }
341 super.OnAbort(e);
342 }
343};
344
void wpnDebugPrint(string s)
Definition debug.c:9
attach mag in LH into weapon
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
Serialization general interface. Serializer API works with:
Definition serializer.c:56
signalize mechanism manipulation
Definition events.c:35
weapon finite state machine
simple class starting animation action specified by m_action and m_actionType
represent weapon state base
Definition bullethide.c:2
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
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)