weaponreplacingmagandchambernext.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /**@class DetachOldMagazine
  2. * @brief detaches old magazine from weapon and stores it in left hand (LH)
  3. **/
  4. class DetachOldMagazine extends WeaponStateBase
  5. {
  6. Magazine m_oldMagazine; /// magazine that will be detached
  7. ref InventoryLocation m_newDst;
  8. void DetachOldMagazine (Weapon_Base w = NULL, WeaponStateBase parent = NULL)
  9. {
  10. m_oldMagazine = NULL;
  11. m_newDst = NULL;
  12. }
  13. override void OnEntry (WeaponEventBase e)
  14. {
  15. super.OnEntry(e);
  16. }
  17. override void OnAbort (WeaponEventBase e)
  18. {
  19. super.OnAbort(e);
  20. m_oldMagazine = NULL;
  21. m_newDst = NULL;
  22. }
  23. override void OnExit (WeaponEventBase e)
  24. {
  25. if (m_oldMagazine)
  26. {
  27. InventoryLocation il = new InventoryLocation();
  28. e.m_player.GetInventory().FindFreeLocationFor( m_oldMagazine , FindInventoryLocationType.CARGO, il );
  29. if (!m_newDst || !m_newDst.IsValid() || m_newDst.GetType() == InventoryLocationType.GROUND)
  30. {
  31. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_oldMagazine))
  32. {
  33. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - no inventory space for old magazine - dropped to ground"); }
  34. }
  35. else
  36. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine");
  37. }
  38. else
  39. {
  40. InventoryLocation oldSrc = new InventoryLocation();
  41. m_oldMagazine.GetInventory().GetCurrentInventoryLocation(oldSrc);
  42. if (GameInventory.LocationSyncMoveEntity(oldSrc, m_newDst))
  43. {
  44. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - old magazine removed from wpn (LHand->inv)"); }
  45. }
  46. else
  47. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot remove old mag from wpn");
  48. }
  49. }
  50. m_weapon.HideMagazine();
  51. m_oldMagazine = NULL;
  52. m_newDst = null;
  53. super.OnExit(e);
  54. }
  55. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  56. {
  57. if (!super.SaveCurrentFSMState(ctx))
  58. return false;
  59. if (!ctx.Write(m_oldMagazine))
  60. {
  61. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_oldMagazine for weapon=" + m_weapon);
  62. return false;
  63. }
  64. if (!OptionalLocationWriteToContext(m_newDst, ctx))
  65. {
  66. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
  67. return false;
  68. }
  69. return true;
  70. }
  71. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  72. {
  73. if (!super.LoadCurrentFSMState(ctx, version))
  74. return false;
  75. if (!ctx.Read(m_oldMagazine))
  76. {
  77. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_oldMagazine for weapon=" + m_weapon);
  78. return false;
  79. }
  80. if (!OptionalLocationReadFromContext(m_newDst, ctx))
  81. {
  82. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
  83. return false;
  84. }
  85. return true;
  86. }
  87. };
  88. /**@class OldMagazineHide
  89. * @brief hides old magazine, but keep it in LH
  90. * @note on abort, old magazine is dropped to ground
  91. **/
  92. class OldMagazineHide : MagazineHide
  93. {
  94. };
  95. /**@class SwapOldAndNewMagazine
  96. * @brief old magazine to inventory, new to left hand
  97. *
  98. * stores old magazine in left hand somewhere in inventory (or drop to ground if necessary)
  99. * and moves new magazine to left hand.
  100. * on abort new magazine is dropped to ground.
  101. **/
  102. class SwapOldAndNewMagazine extends WeaponStateBase
  103. {
  104. Magazine m_newMagazine; /// magazine that will be dropped on abort
  105. ref InventoryLocation m_newDst;
  106. override void OnEntry (WeaponEventBase e)
  107. {
  108. super.OnEntry(e);
  109. if( e )
  110. {
  111. if (!m_newMagazine || !m_newDst || !m_newDst.IsValid())
  112. {
  113. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine, error - m_newMagazine(" + m_newMagazine + ") or destination(" + InventoryLocation.DumpToStringNullSafe(m_newDst) + ") is not set ");
  114. }
  115. else
  116. {
  117. e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , m_newDst );
  118. m_weapon.ShowMagazine();
  119. InventoryLocation lhand = new InventoryLocation();
  120. lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
  121. if (GameInventory.LocationSyncMoveEntity(lhand, m_newDst))
  122. {
  123. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine, ok - new magazine removed from inv (LHand->Att)"); }
  124. }
  125. else
  126. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine, error - cannot remove new mag from LHand");
  127. }
  128. }
  129. }
  130. override void OnAbort (WeaponEventBase e)
  131. {
  132. //m_weapon.HideMagazine();
  133. m_newMagazine = NULL;
  134. m_newDst = NULL;
  135. super.OnAbort(e);
  136. }
  137. override void OnExit (WeaponEventBase e)
  138. {
  139. m_newMagazine = NULL;
  140. m_newDst = NULL;
  141. super.OnExit(e);
  142. }
  143. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  144. {
  145. if (!super.SaveCurrentFSMState(ctx))
  146. return false;
  147. if (!ctx.Write(m_newMagazine))
  148. {
  149. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
  150. return false;
  151. }
  152. if (!OptionalLocationWriteToContext(m_newDst, ctx))
  153. {
  154. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
  155. return false;
  156. }
  157. return true;
  158. }
  159. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  160. {
  161. if (!super.LoadCurrentFSMState(ctx, version))
  162. return false;
  163. if (!ctx.Read(m_newMagazine))
  164. {
  165. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
  166. return false;
  167. }
  168. if (!OptionalLocationReadFromContext(m_newDst, ctx))
  169. {
  170. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " SwapOldAndNewMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
  171. return false;
  172. }
  173. return true;
  174. }
  175. };
  176. /**@class AttachNewMagazine
  177. * @brief attach mag in LH into weapon
  178. **/
  179. class AttachNewMagazine extends WeaponStateBase
  180. {
  181. Magazine m_newMagazine; /// new magazine from inventory that will be attached
  182. ref InventoryLocation m_newDst;
  183. void AttachNewMagazine (Weapon_Base w = NULL, WeaponStateBase parent = NULL)
  184. {
  185. m_newMagazine = NULL;
  186. m_newDst = NULL;
  187. }
  188. override bool IsWaitingForActionFinish ()
  189. {
  190. return true;
  191. }
  192. override void OnEntry (WeaponEventBase e)
  193. {
  194. super.OnEntry(e);
  195. if (e)
  196. {
  197. if (m_newMagazine && m_newDst)
  198. {
  199. InventoryLocation il = new InventoryLocation();
  200. if (m_newMagazine.GetInventory().GetCurrentInventoryLocation(il))
  201. {
  202. InventoryLocation lhand = new InventoryLocation();
  203. lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
  204. if (GameInventory.LocationSyncMoveEntity(il, m_newDst))
  205. {
  206. m_weapon.ShowMagazine();
  207. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, ok - attached new magazine (LHand->dst)"); }
  208. }
  209. else
  210. {
  211. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, error - cannot attach new magazine!"); }
  212. }
  213. }
  214. else
  215. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, error - cannot get curr location");
  216. }
  217. else
  218. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, error - no magazines configured for replace (m_new=NULL)");
  219. }
  220. }
  221. override void OnAbort (WeaponEventBase e)
  222. {
  223. if (m_newMagazine && m_newDst)
  224. {
  225. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_newMagazine))
  226. {
  227. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, ok - aborting, detached magazine dropped to ground"); }
  228. }
  229. else
  230. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, error - cannot abort detaching of magazine");
  231. m_weapon.HideMagazine(); // force hide on abort
  232. }
  233. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine, aborting, but attached new magazine already in place"); }
  234. m_newMagazine = NULL;
  235. m_newDst = NULL;
  236. super.OnAbort(e);
  237. }
  238. override void OnExit (WeaponEventBase e)
  239. {
  240. m_newMagazine = NULL;
  241. m_newDst = NULL;
  242. super.OnExit(e);
  243. }
  244. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  245. {
  246. if (!super.SaveCurrentFSMState(ctx))
  247. return false;
  248. if (!ctx.Write(m_newMagazine))
  249. {
  250. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
  251. return false;
  252. }
  253. if (!OptionalLocationWriteToContext(m_newDst, ctx))
  254. {
  255. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
  256. return false;
  257. }
  258. return true;
  259. }
  260. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  261. {
  262. if (!super.LoadCurrentFSMState(ctx, version))
  263. return false;
  264. if (!ctx.Read(m_newMagazine))
  265. {
  266. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
  267. return false;
  268. }
  269. if (!OptionalLocationReadFromContext(m_newDst, ctx))
  270. {
  271. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " AttachNewMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
  272. return false;
  273. }
  274. return true;
  275. }
  276. };
  277. class AttachNewMagazine_W4T extends WeaponStateBase
  278. {
  279. override bool IsWaitingForActionFinish () { return true; }
  280. };
  281. /**@class WeaponReplacingMagAndChamberNext
  282. * @brief replace current magazine with new one
  283. *
  284. * replace magazine, i.e. eject curr magazine from weapon, attach another (store the old one)
  285. * and chamber next bullet from magazine (if possible)
  286. **/
  287. class WeaponReplacingMagAndChamberNext extends WeaponStateBase
  288. {
  289. WeaponActions m_action;
  290. int m_actionType;
  291. ref WeaponStartAction m_start;
  292. ref DetachOldMagazine m_detach;
  293. ref OldMagazineHide m_hideOld;
  294. ref SwapOldAndNewMagazine m_swapMags;
  295. ref AttachNewMagazine_W4T m_attach;
  296. ref WeaponChamberFromAttMag_W4T m_chamber;
  297. ref WeaponCharging_CK m_onCK;
  298. ref WeaponEjectCasing m_eject;
  299. // substates configuration
  300. Magazine m_oldMagazine; /// magazine that will be detached
  301. Magazine m_newMagazine; /// magazine that will be attached
  302. ref InventoryLocation m_newDst;
  303. void WeaponReplacingMagAndChamberNext (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
  304. {
  305. m_action = action;
  306. m_actionType = actionType;
  307. m_newMagazine = NULL;
  308. // setup nested state machine
  309. m_start = new WeaponStartAction(m_weapon, this, m_action, m_actionType);
  310. m_eject = new WeaponEjectCasing(m_weapon,this);
  311. m_detach = new DetachOldMagazine(m_weapon, this);
  312. m_hideOld = new OldMagazineHide(m_weapon, this);
  313. m_swapMags = new SwapOldAndNewMagazine(m_weapon, this);
  314. m_attach = new AttachNewMagazine_W4T(m_weapon, this);
  315. m_chamber = new WeaponChamberFromAttMag_W4T(m_weapon, this);
  316. m_onCK = new WeaponCharging_CK(m_weapon, this);
  317. // events
  318. WeaponEventBase __so_ = new WeaponEventAnimSliderOpen;
  319. WeaponEventBase __md_ = new WeaponEventAnimMagazineDetached;
  320. WeaponEventBase __mh_ = new WeaponEventAnimMagazineHide;
  321. WeaponEventBase __ms_ = new WeaponEventAnimMagazineShow;
  322. WeaponEventBase __ma_ = new WeaponEventAnimMagazineAttached;
  323. WeaponEventBase __ck_ = new WeaponEventAnimCocked;
  324. WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
  325. m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
  326. m_fsm.AddTransition(new WeaponTransition( m_start, __md_, m_detach));
  327. m_fsm.AddTransition(new WeaponTransition( m_start, __so_, m_eject));
  328. m_fsm.AddTransition(new WeaponTransition( m_eject, __md_, m_detach));
  329. m_fsm.AddTransition(new WeaponTransition( m_detach, __mh_, m_hideOld));
  330. m_fsm.AddTransition(new WeaponTransition( m_hideOld, __ms_, m_swapMags));
  331. m_fsm.AddTransition(new WeaponTransition(m_swapMags, __ma_, m_attach));
  332. m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_chamber, NULL, new GuardAnd(new WeaponGuardCurrentChamberEmpty(m_weapon), new WeaponGuardHasAmmo(m_weapon))));
  333. m_fsm.AddTransition(new WeaponTransition( m_attach, __ck_, m_onCK));
  334. m_fsm.AddTransition(new WeaponTransition( m_attach, _fin_, NULL));
  335. m_fsm.AddTransition(new WeaponTransition( m_chamber, _fin_, NULL));
  336. m_fsm.AddTransition(new WeaponTransition( m_onCK, _fin_, NULL));
  337. // Safety exits
  338. m_fsm.AddTransition(new WeaponTransition(m_swapMags, _fin_, null));
  339. m_fsm.AddTransition(new WeaponTransition(m_hideOld, _fin_, null));
  340. m_fsm.AddTransition(new WeaponTransition(m_detach, _fin_, null));
  341. m_fsm.AddTransition(new WeaponTransition(m_eject, _fin_, null));
  342. m_fsm.AddTransition(new WeaponTransition(m_start, _fin_, null));
  343. m_fsm.SetInitialState(m_start);
  344. }
  345. override void OnEntry (WeaponEventBase e)
  346. {
  347. if (e != NULL)
  348. {
  349. WeaponEventSwapMagazine se;
  350. if (Class.CastTo(se, e))
  351. {
  352. int mi = m_weapon.GetCurrentMuzzle();
  353. m_oldMagazine = m_weapon.GetMagazine(mi);
  354. m_newMagazine = se.m_magazine;
  355. m_newDst = se.m_dst;
  356. 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())); }
  357. InventoryLocation oldSrc = new InventoryLocation();
  358. InventoryLocation newSrc = new InventoryLocation();
  359. if (!m_newMagazine.GetInventory().GetCurrentInventoryLocation(newSrc))
  360. {
  361. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNext cannot get curr inv loc of NEW mag=" + Object.GetDebugName(m_newMagazine));
  362. }
  363. if (!m_oldMagazine.GetInventory().GetCurrentInventoryLocation(oldSrc))
  364. {
  365. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponReplacingMagAndChamberNext cannot get curr inv loc of OLD mag=" + Object.GetDebugName(m_oldMagazine));
  366. }
  367. // move to LH
  368. InventoryLocation lhand = new InventoryLocation();
  369. lhand.SetAttachment(e.m_player, m_newMagazine, InventorySlots.LEFTHAND);
  370. if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
  371. {
  372. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, ok - new magazine removed from inv (inv->LHand)"); }
  373. }
  374. else
  375. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " RemoveNewMagazineFromInventory, error - cannot new remove mag from inv");
  376. m_detach.m_oldMagazine = m_oldMagazine;
  377. m_detach.m_newDst = m_newDst;
  378. m_swapMags.m_newMagazine = m_newMagazine;
  379. oldSrc.SetItem(m_newMagazine);
  380. m_swapMags.m_newDst = oldSrc;
  381. }
  382. }
  383. super.OnEntry(e);
  384. }
  385. override void OnExit (WeaponEventBase e)
  386. {
  387. super.OnExit(e);
  388. EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
  389. Magazine mag = Magazine.Cast(leftHandItem);
  390. if(mag)
  391. {
  392. if (m_newMagazine)
  393. e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , null );
  394. if (m_oldMagazine)
  395. e.m_player.GetInventory().ClearInventoryReservationEx( m_oldMagazine , null );
  396. InventoryLocation il = new InventoryLocation();
  397. e.m_player.GetInventory().FindFreeLocationFor(mag, FindInventoryLocationType.CARGO, il);
  398. if (!il.IsValid())
  399. {
  400. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
  401. {
  402. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - no inventory space for old magazine - dropped to ground - exit"); }
  403. }
  404. else
  405. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine - exit");
  406. }
  407. else
  408. {
  409. InventoryLocation oldSrc = new InventoryLocation();
  410. mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
  411. if (GameInventory.LocationSyncMoveEntity(oldSrc, il))
  412. {
  413. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - old magazine removed from wpn (LHand->inv) - exit"); }
  414. }
  415. else
  416. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot remove old mag from wpn - exit");
  417. }
  418. }
  419. m_oldMagazine = NULL;
  420. m_newMagazine = NULL;
  421. m_newDst = NULL;
  422. }
  423. override void OnAbort(WeaponEventBase e)
  424. {
  425. super.OnAbort(e);
  426. EntityAI leftHandItem = e.m_player.GetInventory().FindAttachment(InventorySlots.LEFTHAND);
  427. Magazine mag = Magazine.Cast(leftHandItem);
  428. if (mag)
  429. {
  430. if (m_newMagazine)
  431. e.m_player.GetInventory().ClearInventoryReservationEx( m_newMagazine , null );
  432. if (m_oldMagazine)
  433. e.m_player.GetInventory().ClearInventoryReservationEx( m_oldMagazine , null );
  434. InventoryLocation il = new InventoryLocation();
  435. e.m_player.GetInventory().FindFreeLocationFor(mag, FindInventoryLocationType.CARGO, il);
  436. if (!il.IsValid())
  437. {
  438. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, mag))
  439. {
  440. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - no inventory space for old magazine - dropped to ground - abort"); }
  441. }
  442. else
  443. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot drop magazine from left hand after not found inventory space for old magazine - abort");
  444. }
  445. else
  446. {
  447. InventoryLocation oldSrc = new InventoryLocation();
  448. mag.GetInventory().GetCurrentInventoryLocation(oldSrc);
  449. if (GameInventory.LocationSyncMoveEntity(oldSrc, il))
  450. {
  451. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, ok - old magazine removed from wpn (LHand->inv) - abort"); }
  452. }
  453. else
  454. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine, error - cannot remove old mag from wpn - abort");
  455. }
  456. }
  457. }
  458. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  459. {
  460. if (!super.SaveCurrentFSMState(ctx))
  461. return false;
  462. if (!ctx.Write(m_newMagazine))
  463. {
  464. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_newMagazine for weapon=" + m_weapon);
  465. return false;
  466. }
  467. if (!ctx.Write(m_oldMagazine))
  468. {
  469. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_oldMagazine for weapon=" + m_weapon);
  470. return false;
  471. }
  472. if (!m_newDst.WriteToContext(ctx))
  473. {
  474. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.SaveCurrentFSMState: cannot write m_newDst for weapon=" + m_weapon);
  475. return false;
  476. }
  477. return true;
  478. }
  479. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  480. {
  481. if (!super.LoadCurrentFSMState(ctx, version))
  482. return false;
  483. if (!ctx.Read(m_newMagazine))
  484. {
  485. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_newMagazine for weapon=" + m_weapon);
  486. return false;
  487. }
  488. if (!ctx.Read(m_oldMagazine))
  489. {
  490. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_oldMagazine for weapon=" + m_weapon);
  491. return false;
  492. }
  493. m_newDst = new InventoryLocation;
  494. if (!m_newDst.ReadFromContext(ctx))
  495. {
  496. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " DetachOldMagazine.LoadCurrentFSMState: cannot read m_newDst for weapon=" + m_weapon);
  497. return false;
  498. }
  499. return true;
  500. }
  501. };