weaponchambering.c 45 KB

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