weaponchambering.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. // load 1 bullet
  2. class WeaponChambering_Start extends WeaponStartAction
  3. {
  4. override void OnEntry (WeaponEventBase e)
  5. {
  6. super.OnEntry(e);
  7. if (e)
  8. {
  9. m_weapon.SelectionBulletHide();
  10. m_weapon.ForceSyncSelectionState();
  11. }
  12. }
  13. override bool IsWaitingForActionFinish()
  14. {
  15. return true;
  16. }
  17. };
  18. class WeaponChambering_Base extends WeaponStateBase
  19. {
  20. float m_damage;
  21. string m_type;
  22. string m_magazineType;
  23. Magazine m_srcMagazine; /// source of the cartridge
  24. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  25. {
  26. if (!super.SaveCurrentFSMState(ctx))
  27. return false;
  28. if (!ctx.Write(m_damage))
  29. {
  30. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.SaveCurrentFSMState: cannot write m_damage for weapon=" + m_weapon);
  31. return false;
  32. }
  33. if (!ctx.Write(m_type))
  34. {
  35. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.SaveCurrentFSMState: cannot write m_type for weapon=" + m_weapon);
  36. return false;
  37. }
  38. if (!ctx.Write(m_magazineType))
  39. {
  40. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.SaveCurrentFSMState: cannot write m_magazineType for weapon=" + m_weapon);
  41. return false;
  42. }
  43. if (!ctx.Write(m_srcMagazine))
  44. {
  45. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.SaveCurrentFSMState: cannot write m_srcMagazine for weapon=" + m_weapon);
  46. return false;
  47. }
  48. return true;
  49. }
  50. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  51. {
  52. if (!super.LoadCurrentFSMState(ctx, version))
  53. return false;
  54. if (!ctx.Read(m_damage))
  55. {
  56. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.LoadCurrentFSMState: cannot read m_damage for weapon=" + m_weapon);
  57. return false;
  58. }
  59. if (!ctx.Read(m_type))
  60. {
  61. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.LoadCurrentFSMState: cannot read m_type for weapon=" + m_weapon);
  62. return false;
  63. }
  64. if (!ctx.Read(m_magazineType))
  65. {
  66. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.LoadCurrentFSMState: cannot read m_magazineType for weapon=" + m_weapon);
  67. return false;
  68. }
  69. if (!ctx.Read(m_srcMagazine))
  70. {
  71. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.LoadCurrentFSMState: cannot read m_srcMagazine for weapon=" + m_weapon);
  72. return false;
  73. }
  74. return true;
  75. }
  76. };
  77. class WeaponChambering_Cartridge extends WeaponChambering_Base
  78. {
  79. override bool IsWaitingForActionFinish () { return true; }
  80. override void OnEntry (WeaponEventBase e)
  81. {
  82. super.OnEntry(e);
  83. if (e)
  84. {
  85. if (m_srcMagazine)
  86. {
  87. m_magazineType = m_srcMagazine.GetType();
  88. if (m_srcMagazine.ServerAcquireCartridge(m_damage, m_type))
  89. {
  90. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, ok - cartridge acquired: dmg=" + m_damage + " type=" + m_type); }
  91. m_weapon.SelectionBulletShow();
  92. m_weapon.ShowBullet(m_weapon.GetCurrentMuzzle());
  93. m_weapon.EffectBulletShow( m_weapon.GetCurrentMuzzle(), m_damage, m_type);
  94. m_weapon.SetWeaponOpen(false);
  95. }
  96. else
  97. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, error - cannot take cartridge from magazine");
  98. }
  99. else
  100. {
  101. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, error - no magazine to load from (m_srcMagazine=NULL)");
  102. }
  103. }
  104. }
  105. override void OnAbort(WeaponEventBase e)
  106. {
  107. int mi = m_weapon.GetCurrentMuzzle();
  108. string magazineTypeName;
  109. if (m_magazineType.Length() > 0)
  110. magazineTypeName = m_magazineType;
  111. else
  112. magazineTypeName = m_weapon.GetChamberAmmoTypeName(mi);
  113. if ( GetGame().IsServer() )
  114. {
  115. if (DayZPlayerUtils.HandleDropCartridge(e.m_player, m_damage, m_type, magazineTypeName))
  116. {
  117. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, ok - aborting, chambering cartridge dropped to ground"); }
  118. }
  119. else
  120. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, error - cannot abort removal from wpn (of old mag)");
  121. }
  122. m_weapon.EffectBulletHide(mi);
  123. m_weapon.SelectionBulletHide(); // force hide on abort
  124. m_magazineType = string.Empty;
  125. m_type = string.Empty;
  126. super.OnAbort(e);
  127. }
  128. override void OnExit (WeaponEventBase e)
  129. {
  130. int mi = m_weapon.GetCurrentMuzzle();
  131. //if ( m_weapon.IsChamberFiredOut(mi) )
  132. // m_weapon.EjectCasing(mi);
  133. if (m_weapon.PushCartridgeToChamber(mi, m_damage, m_type))
  134. {
  135. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, ok - loaded chamber"); }
  136. }
  137. else
  138. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, error - cannot load chamber chamber!"); }
  139. m_weapon.SetCharged(true);
  140. m_magazineType = string.Empty;
  141. m_type = string.Empty;
  142. super.OnExit(e);
  143. }
  144. };
  145. class WeaponChambering_Cartridge_ChambToMag extends WeaponChambering_Cartridge
  146. {
  147. override void OnExit (WeaponEventBase e)
  148. {
  149. float ammoDamage;
  150. string ammoTypeName;
  151. int mi = m_weapon.GetCurrentMuzzle();
  152. if (m_weapon.IsChamberFull(mi))
  153. {
  154. m_weapon.PopCartridgeFromChamber(mi, ammoDamage, ammoTypeName);
  155. if (m_weapon.PushCartridgeToInternalMagazine(mi, ammoDamage, ammoTypeName))
  156. {
  157. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, ok - loaded chamber"); }
  158. }
  159. else
  160. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, error - cannot load chamber chamber!"); }
  161. }
  162. m_weapon.SetWeaponOpen(false);
  163. super.OnExit(e);
  164. }
  165. }
  166. //-----------MAGNUM-----------
  167. class WeaponChambering_MultiMuzzleMagnum extends WeaponChambering_Cartridge
  168. {
  169. override bool IsWaitingForActionFinish () { return false; }
  170. override void OnEntry(WeaponEventBase e)
  171. {
  172. super.OnEntry(e);
  173. /*for(int i = 0; i < m_weapon.GetMuzzleCount(); i++ )
  174. {
  175. if(!m_weapon.IsChamberFull(i))
  176. {
  177. m_weapon.ShowBullet(i);
  178. m_weapon.EffectBulletShow(i, m_damage, m_type);
  179. return;
  180. }
  181. }*/
  182. }
  183. override void OnExit(WeaponEventBase e)
  184. {
  185. m_weapon.SelectionBulletHide();
  186. int muzzle = m_weapon.GetCurrentMuzzle();
  187. if (!m_weapon.IsChamberFull(muzzle))
  188. {
  189. if (m_weapon.PushCartridgeToChamber(muzzle, m_damage, m_type))
  190. {
  191. Magnum_Cylinder cylinder = Magnum_Cylinder.Cast(m_weapon.GetAttachmentByType(Magnum_Cylinder));
  192. if (cylinder)
  193. {
  194. string bullet = "bullet";
  195. string bullet_nose = "bullet_nose";
  196. if (muzzle > 0)
  197. {
  198. bullet = string.Format("bullet_" + ( muzzle + 1 ));
  199. bullet_nose = string.Format("bullet_nose_" + ( muzzle + 1 ));
  200. }
  201. cylinder.ShowSelection(bullet);
  202. cylinder.ShowSelection(bullet_nose);
  203. }
  204. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_MultiMuzzleMagnum, ok - loaded chamber"); }
  205. }
  206. else
  207. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_MultiMuzzleMagnum, error - cannot load chamber chamber!"); }
  208. m_type = string.Empty;
  209. return;
  210. }
  211. else
  212. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_MultiMuzzleMagnum, error - cannot load chamber chamber!"); }
  213. //super.OnExit(e);
  214. }
  215. }
  216. //----------------------------
  217. class WeaponChambering_MultiMuzzle extends WeaponChambering_Cartridge
  218. {
  219. override bool IsWaitingForActionFinish () { return true; }
  220. override void OnEntry(WeaponEventBase e)
  221. {
  222. super.OnEntry(e);
  223. for(int i = 0; i < m_weapon.GetMuzzleCount(); i++ )
  224. {
  225. if(!m_weapon.IsChamberFull(i))
  226. {
  227. m_weapon.ShowBullet(i);
  228. m_weapon.EffectBulletShow(i, m_damage, m_type);
  229. return;
  230. }
  231. }
  232. }
  233. override void OnExit (WeaponEventBase e)
  234. {
  235. for(int i = 0; i < m_weapon.GetMuzzleCount(); i++ )
  236. {
  237. if(!m_weapon.IsChamberFull(i))
  238. {
  239. if (m_weapon.PushCartridgeToChamber(i, m_damage, m_type))
  240. {
  241. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, ok - loaded chamber"); }
  242. }
  243. else
  244. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge, error - cannot load chamber chamber!"); }
  245. m_type = string.Empty;
  246. return;
  247. }
  248. }
  249. super.OnExit(e);
  250. }
  251. }
  252. class WeaponChambering_MultiMuzzle_W4T extends WeaponChambering_MultiMuzzle
  253. {
  254. override bool IsWaitingForActionFinish () { return true; }
  255. };
  256. class WeaponChambering_Cartridge_InnerMag extends WeaponChambering_Base
  257. {
  258. override void OnEntry (WeaponEventBase e)
  259. {
  260. super.OnEntry(e);
  261. if (e)
  262. {
  263. if (m_srcMagazine)
  264. {
  265. m_magazineType = m_srcMagazine.GetType();
  266. if (m_srcMagazine.ServerAcquireCartridge(m_damage, m_type))
  267. {
  268. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, ok - cartridge acquired: dmg=" + m_damage + " type=" + m_type); }
  269. }
  270. else
  271. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, error - cannot take cartridge from magazine");
  272. }
  273. else
  274. {
  275. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, error - no magazine to load from (m_srcMagazine=NULL)");
  276. }
  277. m_weapon.SelectionBulletShow();
  278. m_weapon.EffectBulletShow(m_weapon.GetCurrentMuzzle(),m_damage,m_type);
  279. }
  280. }
  281. override void OnAbort(WeaponEventBase e)
  282. {
  283. int mi = m_weapon.GetCurrentMuzzle();
  284. string magazineTypeName;
  285. if (m_magazineType.Length() > 0)
  286. magazineTypeName = m_magazineType;
  287. else
  288. magazineTypeName = m_weapon.GetChamberAmmoTypeName(mi);
  289. if ( !GetGame().IsMultiplayer() || GetGame().IsServer() )
  290. {
  291. if (DayZPlayerUtils.HandleDropCartridge(e.m_player, m_damage, m_type, magazineTypeName))
  292. {
  293. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, ok - aborting, chambering cartridge dropped to ground"); }
  294. }
  295. else
  296. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, error - cannot abort removal from wpn (of old mag)");
  297. }
  298. m_weapon.SelectionBulletHide(); // force hide on abort
  299. m_weapon.EffectBulletHide(m_weapon.GetCurrentMuzzle());
  300. m_magazineType = string.Empty;
  301. m_type = string.Empty;
  302. super.OnAbort(e);
  303. }
  304. override void OnExit (WeaponEventBase e)
  305. {
  306. float ammoDamage;
  307. string ammoTypeName;
  308. int mi = m_weapon.GetCurrentMuzzle();
  309. if (!m_weapon.IsInternalMagazineFull(mi))
  310. {
  311. if (m_weapon.PushCartridgeToInternalMagazine(mi, m_damage, m_type))
  312. {
  313. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, ok - loaded chamber"); }
  314. }
  315. else
  316. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering_Cartridge_InnerMag, error - cannot load chamber chamber!"); }
  317. }
  318. m_magazineType = string.Empty;
  319. super.OnExit(e);
  320. }
  321. };
  322. class WeaponChambering_W4T extends WeaponStateBase
  323. {
  324. override bool IsWaitingForActionFinish () { return true; }
  325. };
  326. class WeaponChambering extends WeaponStateBase
  327. {
  328. WeaponActions m_action;
  329. int m_actionType;
  330. Magazine m_srcMagazine; /// source of the cartridge
  331. ref InventoryLocation m_srcMagazinePrevLocation;
  332. ref WeaponStateBase m_start;
  333. ref WeaponEjectCasing m_eject;
  334. ref WeaponChambering_Cartridge m_chamber;
  335. ref WeaponChambering_W4T m_w4t;
  336. ref WeaponCharging_CK m_onCK;
  337. void WeaponChambering (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
  338. {
  339. m_action = action;
  340. m_actionType = actionType;
  341. // setup nested state machine
  342. m_start = new WeaponChambering_Start(m_weapon, this, m_action, m_actionType);
  343. m_chamber = new WeaponChambering_Cartridge(m_weapon, this);
  344. m_w4t = new WeaponChambering_W4T(m_weapon, this);
  345. m_eject = new WeaponEjectCasing(m_weapon, this);
  346. m_onCK = new WeaponCharging_CK(m_weapon, this);
  347. // events
  348. WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
  349. WeaponEventAnimBulletInChamber __bc_ = new WeaponEventAnimBulletInChamber;
  350. WeaponEventAnimBulletShow __bs_ = new WeaponEventAnimBulletShow;
  351. WeaponEventAnimBulletEject __be_ = new WeaponEventAnimBulletEject;
  352. WeaponEventAnimCocked __ck_ = new WeaponEventAnimCocked;
  353. m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
  354. m_fsm.AddTransition(new WeaponTransition(m_start , __be_, m_eject));
  355. m_fsm.AddTransition(new WeaponTransition(m_start , __ck_, m_onCK));
  356. m_fsm.AddTransition(new WeaponTransition(m_start , __bs_, m_chamber));
  357. m_fsm.AddTransition(new WeaponTransition(m_onCK , __be_, m_eject));
  358. m_fsm.AddTransition(new WeaponTransition(m_onCK , __bs_, m_chamber));
  359. m_fsm.AddTransition(new WeaponTransition(m_eject , __bs_, m_chamber));
  360. m_fsm.AddTransition(new WeaponTransition(m_chamber, __bc_, m_w4t));
  361. m_fsm.AddTransition(new WeaponTransition(m_w4t , _fin_, null));
  362. // Safety exits
  363. m_fsm.AddTransition(new WeaponTransition(m_chamber, _fin_, null));
  364. m_fsm.AddTransition(new WeaponTransition(m_eject , _fin_, null));
  365. m_fsm.AddTransition(new WeaponTransition(m_start , _fin_, null));
  366. m_fsm.SetInitialState(m_start);
  367. }
  368. override void OnEntry (WeaponEventBase e)
  369. {
  370. if (e != NULL)
  371. {
  372. m_srcMagazine = e.m_magazine;
  373. if (m_srcMagazine != NULL)
  374. {
  375. InventoryLocation newSrc = new InventoryLocation;
  376. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(newSrc);
  377. m_srcMagazinePrevLocation = newSrc;
  378. // move to LH
  379. InventoryLocation lhand = new InventoryLocation;
  380. lhand.SetAttachment(e.m_player, m_srcMagazine, InventorySlots.LEFTHAND);
  381. if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
  382. {
  383. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - ammo pile removed from inv (inv->LHand)"); }
  384. }
  385. else
  386. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, error - cannot remove ammo pile from inv");
  387. m_chamber.m_srcMagazine = m_srcMagazine;
  388. }
  389. else
  390. {
  391. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering m_srcMagazine = NULL"); }
  392. }
  393. }
  394. else
  395. {
  396. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering (e=NULL), m_srcMagazine=" + m_srcMagazine.ToString()); }
  397. }
  398. super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
  399. }
  400. override void OnAbort (WeaponEventBase e)
  401. {
  402. bool done = false;
  403. if (m_srcMagazine)
  404. {
  405. e.m_player.GetInventory().ClearInventoryReservationEx( m_srcMagazine , m_srcMagazinePrevLocation );
  406. InventoryLocation leftHandIl = new InventoryLocation;
  407. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(leftHandIl);
  408. if (leftHandIl.IsValid())
  409. {
  410. if (m_srcMagazinePrevLocation && m_srcMagazinePrevLocation.IsValid())
  411. {
  412. if (vector.DistanceSq(m_srcMagazinePrevLocation.GetPos(),leftHandIl.GetPos()) < WeaponManager.MAX_DROP_MAGAZINE_DISTANCE_SQ)
  413. {
  414. if (GameInventory.LocationCanMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  415. {
  416. if (GameInventory.LocationSyncMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  417. {
  418. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - ammo pile removed from left hand to previous location (LHand->inv) - abort"); }
  419. done = true;
  420. }
  421. }
  422. }
  423. }
  424. if( !done)
  425. {
  426. InventoryLocation il = new InventoryLocation;
  427. e.m_player.GetInventory().FindFreeLocationFor( m_srcMagazine, FindInventoryLocationType.CARGO, il );
  428. if(!il || !il.IsValid())
  429. {
  430. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_srcMagazine))
  431. {
  432. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - no inventory space for ammo pile - dropped to ground - abort"); }
  433. }
  434. else
  435. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, error - cannot drop ammo pile from left hand after not found inventory space for ammo pile - abort");
  436. }
  437. else
  438. {
  439. if (GameInventory.LocationSyncMoveEntity(leftHandIl, il))
  440. {
  441. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - ammo pile removed from left hand (LHand->inv) - abort"); }
  442. }
  443. else
  444. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, error - cannot remove ammo pile from wpn - abort");
  445. }
  446. }
  447. }
  448. }
  449. super.OnAbort(e);
  450. m_srcMagazine = NULL;
  451. m_chamber.m_srcMagazine = NULL;
  452. m_srcMagazinePrevLocation = NULL;
  453. }
  454. override void OnExit (WeaponEventBase e)
  455. {
  456. bool done = false;
  457. if (m_srcMagazine)
  458. {
  459. e.m_player.GetInventory().ClearInventoryReservationEx( m_srcMagazine , m_srcMagazinePrevLocation );
  460. InventoryLocation leftHandIl = new InventoryLocation;
  461. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(leftHandIl);
  462. if (leftHandIl.IsValid())
  463. {
  464. if (m_srcMagazinePrevLocation && m_srcMagazinePrevLocation.IsValid())
  465. {
  466. if (vector.DistanceSq(m_srcMagazinePrevLocation.GetPos(), leftHandIl.GetPos()) < WeaponManager.MAX_DROP_MAGAZINE_DISTANCE_SQ)
  467. {
  468. if (GameInventory.LocationCanMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  469. {
  470. if (GameInventory.LocationSyncMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  471. {
  472. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - ammo pile removed from left hand to previous location (LHand->inv) - exit"); }
  473. done = true;
  474. }
  475. }
  476. }
  477. }
  478. if( !done)
  479. {
  480. InventoryLocation il = new InventoryLocation;
  481. e.m_player.GetInventory().FindFreeLocationFor( m_srcMagazine, FindInventoryLocationType.CARGO, il );
  482. if(!il || !il.IsValid())
  483. {
  484. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_srcMagazine))
  485. {
  486. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - no inventory space for ammo pile - dropped to ground - exit"); }
  487. }
  488. else
  489. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, error - cannot drop ammo pile from left hand after not found inventory space for ammo pile - exit");
  490. }
  491. else
  492. {
  493. if (GameInventory.LocationSyncMoveEntity(leftHandIl, il))
  494. {
  495. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, ok - ammo pile removed from left hand (LHand->inv) - exit"); }
  496. }
  497. else
  498. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering, error - cannot remove ammo pile from wpn - exit");
  499. }
  500. }
  501. }
  502. }
  503. super.OnExit(e);
  504. m_srcMagazine = NULL;
  505. m_chamber.m_srcMagazine = NULL;
  506. m_srcMagazinePrevLocation = NULL;
  507. }
  508. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  509. {
  510. if (!super.SaveCurrentFSMState(ctx))
  511. return false;
  512. if (!ctx.Write(m_srcMagazine))
  513. {
  514. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.SaveCurrentFSMState: cannot save m_srcMagazine for weapon=" + m_weapon);
  515. return false;
  516. }
  517. if (!OptionalLocationWriteToContext(m_srcMagazinePrevLocation, ctx))
  518. {
  519. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.SaveCurrentFSMState: cannot write m_srcMagazinePrevLocation for weapon=" + m_weapon);
  520. return false;
  521. }
  522. return true;
  523. }
  524. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  525. {
  526. if (!super.LoadCurrentFSMState(ctx, version))
  527. return false;
  528. if (!ctx.Read(m_srcMagazine))
  529. {
  530. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.LoadCurrentFSMState: cannot read m_srcMagazine for weapon=" + m_weapon);
  531. return false;
  532. }
  533. if (!OptionalLocationReadFromContext(m_srcMagazinePrevLocation, ctx))
  534. {
  535. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponChambering.LoadCurrentFSMState: cannot read m_srcMagazinePrevLocation for weapon=" + m_weapon);
  536. return false;
  537. }
  538. return true;
  539. }
  540. };
  541. //----------------------------------------------
  542. //----------------------------------------------
  543. //----------------------------------------------
  544. class WeaponEndAction extends WeaponStartAction
  545. {
  546. override bool IsWaitingForActionFinish()
  547. {
  548. return true;
  549. }
  550. }
  551. class ChamberMultiBullet extends WeaponStateBase
  552. {
  553. WeaponActions m_action;
  554. int m_startActionType;
  555. int m_endActionType;
  556. Magazine m_srcMagazine; /// source of the cartridge
  557. ref InventoryLocation m_srcMagazinePrevLocation;
  558. ref WeaponStateBase m_start;
  559. ref WeaponEjectCasingMultiMuzzle m_eject;
  560. ref WeaponChambering_Base m_chamber;
  561. ref WeaponChambering_Base m_chamber_end;
  562. ref LoopedChambering_Wait4ShowBullet2 m_w4sb2;
  563. ref WeaponEndAction m_endLoop;
  564. ref BulletShow_W4T m_showB;
  565. ref BulletShow2_W4T m_showB2;
  566. void ChamberMultiBullet (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int startActionType = -1, int endActionType = -1)
  567. {
  568. m_action = action;
  569. m_startActionType = startActionType;
  570. m_endActionType = endActionType;
  571. // setup nested state machine
  572. m_start = new WeaponChambering_Start(m_weapon, this, m_action, m_startActionType);
  573. m_eject = new WeaponEjectCasingMultiMuzzle(m_weapon, this);
  574. m_chamber = new WeaponChambering_MultiMuzzle_W4T(m_weapon, this);
  575. m_chamber_end = new WeaponChambering_MultiMuzzle_W4T(m_weapon, this);
  576. m_w4sb2 = LoopedChambering_Wait4ShowBullet2(m_weapon, this);
  577. m_showB = new BulletShow_W4T(m_weapon, this);
  578. m_showB2= new BulletShow2_W4T(m_weapon, this);
  579. m_endLoop = new WeaponEndAction(m_weapon, this, m_action, m_endActionType); // @NOTE: termination playing action - dummy?
  580. // events
  581. WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
  582. WeaponEventContinuousLoadBulletStart __lS_ = new WeaponEventContinuousLoadBulletStart;
  583. WeaponEventContinuousLoadBulletEnd __lE_ = new WeaponEventContinuousLoadBulletEnd;
  584. WeaponEventAnimBulletShow __bs_ = new WeaponEventAnimBulletShow;
  585. WeaponEventAnimBulletShow2 _bs2_ = new WeaponEventAnimBulletShow2;
  586. WeaponEventAnimBulletHide __bh_ = new WeaponEventAnimBulletHide;
  587. WeaponEventAnimBulletEject __be_ = new WeaponEventAnimBulletEject;
  588. WeaponEventAnimBulletInChamber __bc_ = new WeaponEventAnimBulletInChamber;
  589. m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
  590. m_fsm.AddTransition(new WeaponTransition(m_start , __be_, m_eject));
  591. m_fsm.AddTransition(new WeaponTransition(m_start , __bs_, m_chamber));
  592. m_fsm.AddTransition(new WeaponTransition(m_eject , __bs_, m_chamber));
  593. m_fsm.AddTransition(new WeaponTransition(m_chamber, __bc_, m_w4sb2, NULL, new GuardAnd(new GuardAnd(new WeaponGuardHasAmmoInLoopedState(m_chamber), new WeaponGuardChamberMultiHasRoomBulltet(m_weapon)),new WeaponGuardWeaponManagerWantContinue())));
  594. m_fsm.AddTransition(new WeaponTransition(m_chamber, __bc_, m_endLoop));
  595. m_fsm.AddTransition(new WeaponTransition(m_w4sb2, __bs_, m_chamber));
  596. m_fsm.AddTransition(new WeaponTransition(m_w4sb2 , _fin_, NULL));
  597. m_fsm.AddTransition(new WeaponTransition(m_chamber , _fin_, NULL));
  598. m_fsm.AddTransition(new WeaponTransition(m_endLoop , _fin_, NULL));
  599. // Safety exits
  600. m_fsm.AddTransition(new WeaponTransition(m_eject , _fin_, null));
  601. m_fsm.AddTransition(new WeaponTransition(m_start , _fin_, null));
  602. m_fsm.SetInitialState(m_start);
  603. }
  604. override void OnEntry (WeaponEventBase e)
  605. {
  606. if (e != NULL)
  607. {
  608. m_srcMagazine = e.m_magazine;
  609. if (m_srcMagazine != NULL)
  610. {
  611. InventoryLocation newSrc = new InventoryLocation;
  612. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(newSrc);
  613. m_srcMagazinePrevLocation = newSrc;
  614. // move to LH
  615. InventoryLocation lhand = new InventoryLocation;
  616. lhand.SetAttachment(e.m_player, m_srcMagazine, InventorySlots.LEFTHAND);
  617. if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
  618. {
  619. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - ammo pile removed from inv (inv->LHand)"); }
  620. }
  621. else
  622. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, error - cannot remove ammo pile from inv");
  623. m_chamber.m_srcMagazine = m_srcMagazine;
  624. }
  625. else
  626. {
  627. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet m_srcMagazine = NULL"); }
  628. }
  629. }
  630. else
  631. {
  632. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet (e=NULL), m_srcMagazine=" + m_srcMagazine.ToString()); }
  633. }
  634. super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
  635. }
  636. override void OnExit (WeaponEventBase e)
  637. {
  638. bool done = false;
  639. if (m_srcMagazine)
  640. {
  641. e.m_player.GetInventory().ClearInventoryReservationEx( m_srcMagazine , m_srcMagazinePrevLocation );
  642. InventoryLocation leftHandIl = new InventoryLocation;
  643. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(leftHandIl);
  644. if (leftHandIl.IsValid())
  645. {
  646. if (m_srcMagazinePrevLocation && m_srcMagazinePrevLocation.IsValid())
  647. {
  648. if (vector.DistanceSq(m_srcMagazinePrevLocation.GetPos(), leftHandIl.GetPos()) < WeaponManager.MAX_DROP_MAGAZINE_DISTANCE_SQ)
  649. {
  650. if (GameInventory.LocationCanMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  651. {
  652. if (GameInventory.LocationSyncMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  653. {
  654. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - ammo pile removed from left hand to previous location (LHand->inv) - exit"); }
  655. done = true;
  656. }
  657. }
  658. }
  659. }
  660. if( !done)
  661. {
  662. InventoryLocation il = new InventoryLocation;
  663. e.m_player.GetInventory().FindFreeLocationFor( m_srcMagazine, FindInventoryLocationType.CARGO, il );
  664. if(!il || !il.IsValid())
  665. {
  666. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_srcMagazine))
  667. {
  668. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - no inventory space for ammo pile - dropped to ground - exit"); }
  669. }
  670. else
  671. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, error - cannot drop ammo pile from left hand after not found inventory space for ammo pile - exit");
  672. }
  673. else
  674. {
  675. if (GameInventory.LocationSyncMoveEntity(leftHandIl, il))
  676. {
  677. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - ammo pile removed from left hand (LHand->inv) - exit"); }
  678. }
  679. else
  680. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, error - cannot remove ammo pile from wpn - exit");
  681. }
  682. }
  683. }
  684. }
  685. super.OnExit(e);
  686. m_srcMagazine = NULL;
  687. m_chamber.m_srcMagazine = NULL;
  688. m_srcMagazinePrevLocation = NULL;
  689. }
  690. override void OnAbort (WeaponEventBase e)
  691. {
  692. bool done = false;
  693. if (m_srcMagazine)
  694. {
  695. e.m_player.GetInventory().ClearInventoryReservationEx( m_srcMagazine , m_srcMagazinePrevLocation );
  696. InventoryLocation leftHandIl = new InventoryLocation;
  697. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(leftHandIl);
  698. if (leftHandIl.IsValid())
  699. {
  700. if (m_srcMagazinePrevLocation && m_srcMagazinePrevLocation.IsValid())
  701. {
  702. if (vector.DistanceSq(m_srcMagazinePrevLocation.GetPos(), leftHandIl.GetPos()) < WeaponManager.MAX_DROP_MAGAZINE_DISTANCE_SQ)
  703. {
  704. if (GameInventory.LocationCanMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  705. {
  706. if (GameInventory.LocationSyncMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  707. {
  708. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - ammo pile removed from left hand to previous location (LHand->inv) - abort"); }
  709. done = true;
  710. }
  711. }
  712. }
  713. }
  714. if( !done)
  715. {
  716. InventoryLocation il = new InventoryLocation;
  717. e.m_player.GetInventory().FindFreeLocationFor( m_srcMagazine, FindInventoryLocationType.CARGO, il );
  718. if(!il || !il.IsValid())
  719. {
  720. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_srcMagazine))
  721. {
  722. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - no inventory space for ammo pile - dropped to ground - abort"); }
  723. }
  724. else
  725. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, error - cannot drop ammo pile from left hand after not found inventory space for ammo pile - abort");
  726. }
  727. else
  728. {
  729. if (GameInventory.LocationSyncMoveEntity(leftHandIl, il))
  730. {
  731. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, ok - ammo pile removed from left hand (LHand->inv) - abort"); }
  732. }
  733. else
  734. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet, error - cannot remove ammo pile from wpn - abort");
  735. }
  736. }
  737. }
  738. }
  739. super.OnAbort(e);
  740. m_srcMagazine = NULL;
  741. m_chamber.m_srcMagazine = NULL;
  742. m_srcMagazinePrevLocation = NULL;
  743. }
  744. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  745. {
  746. if (!super.SaveCurrentFSMState(ctx))
  747. return false;
  748. if (!ctx.Write(m_srcMagazine))
  749. {
  750. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet.SaveCurrentFSMState: cannot save m_srcMagazine for weapon=" + m_weapon);
  751. return false;
  752. }
  753. if (!OptionalLocationWriteToContext(m_srcMagazinePrevLocation, ctx))
  754. {
  755. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet.SaveCurrentFSMState: cannot write m_srcMagazinePrevLocation for weapon=" + m_weapon);
  756. return false;
  757. }
  758. return true;
  759. }
  760. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  761. {
  762. if (!super.LoadCurrentFSMState(ctx, version))
  763. return false;
  764. if (!ctx.Read(m_srcMagazine))
  765. {
  766. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet.LoadCurrentFSMState: cannot read m_srcMagazine for weapon=" + m_weapon);
  767. return false;
  768. }
  769. if (!OptionalLocationReadFromContext(m_srcMagazinePrevLocation, ctx))
  770. {
  771. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " ChamberMultiBullet.LoadCurrentFSMState: cannot read m_srcMagazinePrevLocation for weapon=" + m_weapon);
  772. return false;
  773. }
  774. return true;
  775. }
  776. };
  777. //------------------------------------------------------
  778. //------------------ROTATE------------------------------
  779. //------------------------------------------------------
  780. class WeaponCylinderRotate extends WeaponStateBase
  781. {
  782. bool FindNextFreeMuzzle(int currentMuzzle, out int nextMuzzle)
  783. {
  784. nextMuzzle = currentMuzzle;
  785. int nMuzzles = m_weapon.GetMuzzleCount();
  786. for (int i = 0; i < nMuzzles; ++i)
  787. {
  788. --nextMuzzle;
  789. nextMuzzle = Math.WrapInt(nextMuzzle, 0, nMuzzles);
  790. if (m_weapon.IsChamberEmpty(nextMuzzle))
  791. return true;
  792. }
  793. return false;
  794. }
  795. override void OnEntry(WeaponEventBase e)
  796. {
  797. int nextMuzzle;
  798. if (FindNextFreeMuzzle(m_weapon.GetCurrentMuzzle(), nextMuzzle))
  799. {
  800. Magnum_Base magnum = Magnum_Base.Cast(m_weapon);
  801. magnum.SetCylinderRotationAnimationPhase(magnum.GetCylinderRotation(nextMuzzle));
  802. m_weapon.SetCurrentMuzzle(nextMuzzle);
  803. }
  804. else
  805. {
  806. Print("WTF");
  807. }
  808. super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
  809. }
  810. };
  811. //------------------------------------------------------
  812. //------------------MAGNUM------------------------------
  813. //------------------------------------------------------
  814. class WeaponMagnumChambering extends WeaponStateBase
  815. {
  816. WeaponActions m_action;
  817. int m_startActionType;
  818. int m_endActionType;
  819. Magazine m_srcMagazine; /// source of the cartridge
  820. ref InventoryLocation m_srcMagazinePrevLocation;
  821. ref WeaponStateBase m_start;
  822. ref WeaponEjectAllMuzzles m_eject;
  823. ref WeaponCylinderRotate m_rotate;
  824. ref WeaponChambering_Base m_chamber;
  825. ref LoopedChambering_Wait4ShowBullet2 m_w4sb2;
  826. ref WeaponStartAction m_endLoop;
  827. ref BulletHide_W4T m_hideB;
  828. void WeaponMagnumChambering(Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int startActionType = -1, int endActionType = -1)
  829. {
  830. m_action = action;
  831. m_startActionType = startActionType;
  832. m_endActionType = endActionType;
  833. // setup nested state machine
  834. m_start = new WeaponChambering_Start(m_weapon, this, m_action, m_startActionType);
  835. m_eject = new WeaponEjectAllMuzzles(m_weapon, this);
  836. m_rotate = new WeaponCylinderRotate(m_weapon, this);
  837. m_chamber = new WeaponChambering_MultiMuzzleMagnum(m_weapon, this);
  838. m_w4sb2 = LoopedChambering_Wait4ShowBullet2(m_weapon, this);
  839. m_hideB = new BulletHide_W4T(m_weapon, this);
  840. m_endLoop = new LoopedChambering_EndLoop(m_weapon, this, m_action, m_endActionType); // @NOTE: termination playing action - dummy?
  841. // events
  842. WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
  843. WeaponEventContinuousLoadBulletStart __lS_ = new WeaponEventContinuousLoadBulletStart;
  844. WeaponEventContinuousLoadBulletEnd __lE_ = new WeaponEventContinuousLoadBulletEnd;
  845. WeaponEventCylinderRotate __cr_ = new WeaponEventCylinderRotate;
  846. WeaponEventAnimBulletShow __bs_ = new WeaponEventAnimBulletShow;
  847. WeaponEventAnimBulletHide __bh_ = new WeaponEventAnimBulletHide;
  848. WeaponEventAnimBulletEject __be_ = new WeaponEventAnimBulletEject;
  849. WeaponEventAnimBulletInMagazine __bM_ = new WeaponEventAnimBulletInMagazine;
  850. WeaponEventAnimBulletShow2 _bs2_ = new WeaponEventAnimBulletShow2;
  851. m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
  852. m_fsm.AddTransition(new WeaponTransition(m_start, __be_, m_eject));
  853. m_fsm.AddTransition(new WeaponTransition(m_start, __cr_, m_rotate));
  854. m_fsm.AddTransition(new WeaponTransition(m_eject, __cr_, m_rotate));
  855. m_fsm.AddTransition(new WeaponTransition(m_rotate, __be_, m_eject));
  856. m_fsm.AddTransition(new WeaponTransition(m_eject, __bs_, m_chamber));
  857. m_fsm.AddTransition(new WeaponTransition(m_rotate, __bs_, m_chamber));
  858. m_fsm.AddTransition(new WeaponTransition(m_chamber, __bM_, m_w4sb2, null, new GuardAnd(new GuardAnd(new WeaponGuardHasAmmoInLoopedState(m_chamber), new WeaponGuardChamberMultiHasRoomBulltet(m_weapon)),new WeaponGuardWeaponManagerWantContinue())));
  859. m_fsm.AddTransition(new WeaponTransition(m_chamber, __bM_, m_endLoop));
  860. //m_fsm.AddTransition(new WeaponTransition(m_rotate, __bh_, m_chamber));
  861. //m_fsm.AddTransition(new WeaponTransition(m_w4sb2, __bh_, m_hideB));
  862. m_fsm.AddTransition(new WeaponTransition(m_w4sb2, __cr_, m_rotate));
  863. m_fsm.AddTransition(new WeaponTransition(m_endLoop, _fin_, null));
  864. // Safety exits
  865. m_fsm.AddTransition(new WeaponTransition(m_w4sb2, _fin_, null));
  866. m_fsm.AddTransition(new WeaponTransition(m_chamber, _fin_, null));
  867. m_fsm.AddTransition(new WeaponTransition(m_rotate, _fin_, null));
  868. m_fsm.AddTransition(new WeaponTransition(m_eject , _fin_, null));
  869. m_fsm.AddTransition(new WeaponTransition(m_start , _fin_, null));
  870. m_fsm.SetInitialState(m_start);
  871. }
  872. override void OnEntry(WeaponEventBase e)
  873. {
  874. if (e != NULL)
  875. {
  876. m_srcMagazine = e.m_magazine;
  877. if (m_srcMagazine != NULL)
  878. {
  879. InventoryLocation newSrc = new InventoryLocation;
  880. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(newSrc);
  881. m_srcMagazinePrevLocation = newSrc;
  882. // move to LH
  883. InventoryLocation lhand = new InventoryLocation;
  884. lhand.SetAttachment(e.m_player, m_srcMagazine, InventorySlots.LEFTHAND);
  885. if (GameInventory.LocationSyncMoveEntity(newSrc, lhand))
  886. {
  887. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - ammo pile removed from inv (inv->LHand)"); }
  888. }
  889. else
  890. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, error - cannot remove ammo pile from inv");
  891. m_chamber.m_srcMagazine = m_srcMagazine;
  892. }
  893. else
  894. {
  895. Print("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering m_srcMagazine = NULL");
  896. }
  897. }
  898. else
  899. {
  900. Print("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering (e=NULL), m_srcMagazine=" + m_srcMagazine.ToString());
  901. }
  902. super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
  903. }
  904. override void OnExit(WeaponEventBase e)
  905. {
  906. bool done = false;
  907. if (m_srcMagazine)
  908. {
  909. e.m_player.GetInventory().ClearInventoryReservationEx( m_srcMagazine , m_srcMagazinePrevLocation );
  910. InventoryLocation leftHandIl = new InventoryLocation;
  911. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(leftHandIl);
  912. if (leftHandIl.IsValid())
  913. {
  914. if (m_srcMagazinePrevLocation && m_srcMagazinePrevLocation.IsValid())
  915. {
  916. if (vector.DistanceSq(m_srcMagazinePrevLocation.GetPos(), leftHandIl.GetPos()) < WeaponManager.MAX_DROP_MAGAZINE_DISTANCE_SQ)
  917. {
  918. if (GameInventory.LocationCanMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  919. {
  920. if (GameInventory.LocationSyncMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  921. {
  922. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - ammo pile removed from left hand to previous location (LHand->inv) - exit"); }
  923. done = true;
  924. }
  925. }
  926. }
  927. }
  928. if ( !done)
  929. {
  930. InventoryLocation il = new InventoryLocation;
  931. e.m_player.GetInventory().FindFreeLocationFor( m_srcMagazine, FindInventoryLocationType.CARGO, il );
  932. if (!il || !il.IsValid())
  933. {
  934. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_srcMagazine))
  935. {
  936. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - no inventory space for ammo pile - dropped to ground - exit"); }
  937. }
  938. else
  939. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, error - cannot drop ammo pile from left hand after not found inventory space for ammo pile - exit");
  940. }
  941. else
  942. {
  943. if (GameInventory.LocationSyncMoveEntity(leftHandIl, il))
  944. {
  945. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - ammo pile removed from left hand (LHand->inv) - exit"); }
  946. }
  947. else
  948. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, error - cannot remove ammo pile from wpn - exit");
  949. }
  950. }
  951. }
  952. }
  953. super.OnExit(e);
  954. m_srcMagazine = NULL;
  955. m_chamber.m_srcMagazine = NULL;
  956. m_srcMagazinePrevLocation = NULL;
  957. }
  958. override void OnAbort(WeaponEventBase e)
  959. {
  960. bool done = false;
  961. if (m_srcMagazine)
  962. {
  963. e.m_player.GetInventory().ClearInventoryReservationEx( m_srcMagazine , m_srcMagazinePrevLocation );
  964. InventoryLocation leftHandIl = new InventoryLocation;
  965. m_srcMagazine.GetInventory().GetCurrentInventoryLocation(leftHandIl);
  966. if (leftHandIl.IsValid())
  967. {
  968. if (m_srcMagazinePrevLocation && m_srcMagazinePrevLocation.IsValid())
  969. {
  970. if (vector.DistanceSq(m_srcMagazinePrevLocation.GetPos(), leftHandIl.GetPos()) < WeaponManager.MAX_DROP_MAGAZINE_DISTANCE_SQ)
  971. {
  972. if (GameInventory.LocationCanMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  973. {
  974. if (GameInventory.LocationSyncMoveEntity(leftHandIl,m_srcMagazinePrevLocation))
  975. {
  976. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - ammo pile removed from left hand to previous location (LHand->inv) - abort"); }
  977. done = true;
  978. }
  979. }
  980. }
  981. }
  982. if ( !done)
  983. {
  984. InventoryLocation il = new InventoryLocation;
  985. e.m_player.GetInventory().FindFreeLocationFor( m_srcMagazine, FindInventoryLocationType.CARGO, il );
  986. if (!il || !il.IsValid())
  987. {
  988. if (DayZPlayerUtils.HandleDropMagazine(e.m_player, m_srcMagazine))
  989. {
  990. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - no inventory space for ammo pile - dropped to ground - abort"); }
  991. }
  992. else
  993. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " LoopedChambering, error - cannot drop ammo pile from left hand after not found inventory space for ammo pile - abort");
  994. }
  995. else
  996. {
  997. if (GameInventory.LocationSyncMoveEntity(leftHandIl, il))
  998. {
  999. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering, ok - ammo pile removed from left hand (LHand->inv) - abort"); }
  1000. }
  1001. else
  1002. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " LoopedChambering, error - cannot remove ammo pile from wpn - abort");
  1003. }
  1004. }
  1005. }
  1006. }
  1007. super.OnAbort(e);
  1008. m_srcMagazine = NULL;
  1009. m_chamber.m_srcMagazine = NULL;
  1010. m_srcMagazinePrevLocation = NULL;
  1011. }
  1012. override bool SaveCurrentFSMState(ParamsWriteContext ctx)
  1013. {
  1014. if (!super.SaveCurrentFSMState(ctx))
  1015. return false;
  1016. if (!ctx.Write(m_srcMagazine))
  1017. {
  1018. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering.SaveCurrentFSMState: cannot save m_srcMagazine for weapon=" + m_weapon);
  1019. return false;
  1020. }
  1021. if (!OptionalLocationWriteToContext(m_srcMagazinePrevLocation, ctx))
  1022. {
  1023. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering.SaveCurrentFSMState: cannot write m_srcMagazinePrevLocation for weapon=" + m_weapon);
  1024. return false;
  1025. }
  1026. return true;
  1027. }
  1028. override bool LoadCurrentFSMState(ParamsReadContext ctx, int version)
  1029. {
  1030. if (!super.LoadCurrentFSMState(ctx, version))
  1031. return false;
  1032. if (!ctx.Read(m_srcMagazine))
  1033. {
  1034. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering.LoadCurrentFSMState: cannot read m_srcMagazine for weapon=" + m_weapon);
  1035. return false;
  1036. }
  1037. if (!OptionalLocationReadFromContext(m_srcMagazinePrevLocation, ctx))
  1038. {
  1039. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponMagnumChambering.LoadCurrentFSMState: cannot read m_srcMagazinePrevLocation for weapon=" + m_weapon);
  1040. return false;
  1041. }
  1042. return true;
  1043. }
  1044. };