firearmactionattachmagazine.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. class AttachMagazineActionReciveData : ActionReciveData
  2. {
  3. ref InventoryLocation m_ilOldMagazine;
  4. }
  5. class AttachMagazineActionData : ActionData
  6. {
  7. ref InventoryLocation m_ilOldMagazine;
  8. Magazine m_oldMagazine;
  9. }
  10. class FirearmActionAttachMagazine : FirearmActionBase
  11. {
  12. //-----------------------------------------------------
  13. // Action events and methods
  14. //-----------------------------------------------------
  15. void FirearmActionAttachMagazine()
  16. {
  17. m_Text = "#attach";
  18. }
  19. override int GetActionCategory()
  20. {
  21. return AC_SINGLE_USE;
  22. }
  23. override bool ActionCondition( PlayerBase player, ActionTarget target, ItemBase item ) //condition for action
  24. {
  25. if (!super.ActionCondition( player, target, item ))
  26. return false;
  27. HumanCommandWeapons hcw = player.GetCommandModifier_Weapons();
  28. Magazine mag = Magazine.Cast(target.GetObject());
  29. Weapon_Base wpn = Weapon_Base.Cast(item);
  30. return mag && (player.GetWeaponManager().CanAttachMagazine(wpn,mag) || player.GetWeaponManager().CanSwapMagazine(wpn,mag)) && (!hcw || hcw.GetRunningAction() != WeaponActions.RELOAD);
  31. }
  32. override bool ActionConditionContinue( ActionData action_data )
  33. {
  34. return true;
  35. }
  36. override ActionData CreateActionData()
  37. {
  38. AttachMagazineActionData action_data = new AttachMagazineActionData;
  39. return action_data;
  40. }
  41. override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
  42. {
  43. super.WriteToContext(ctx, action_data);
  44. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  45. action_data_am.m_ilOldMagazine.WriteToContext(ctx);
  46. }
  47. override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
  48. {
  49. if (!action_recive_data)
  50. {
  51. action_recive_data = new AttachMagazineActionReciveData;
  52. }
  53. super.ReadFromContext(ctx, action_recive_data);
  54. InventoryLocation il = new InventoryLocation;
  55. if (!il.ReadFromContext(ctx))
  56. return false;
  57. AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
  58. recive_data_am.m_ilOldMagazine = il;
  59. return true;
  60. }
  61. override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
  62. {
  63. AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
  64. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  65. action_data.m_MainItem = action_recive_data.m_MainItem;
  66. if (!action_recive_data.m_Target)
  67. {
  68. action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
  69. }
  70. else
  71. {
  72. action_data.m_Target = action_recive_data.m_Target;
  73. }
  74. action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
  75. }
  76. override bool Post_SetupAction( ActionData action_data )
  77. {
  78. if ( !GetGame().IsDedicatedServer() )
  79. {
  80. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  81. int muzzle_index = wpn.GetCurrentMuzzle();
  82. AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
  83. am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
  84. InventoryLocation new_il = new InventoryLocation;
  85. if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
  86. {
  87. return false;
  88. }
  89. am_action_data.m_ilOldMagazine = new_il;
  90. }
  91. return true;
  92. }
  93. override bool InventoryReservation( ActionData action_data)
  94. {
  95. if( (IsLocal() || !UseAcknowledgment()) && IsInstant() )
  96. return true;
  97. bool success = true;
  98. InventoryLocation oldMagTargetInventoryLocation = NULL;
  99. InventoryLocation targetInventoryLocation = NULL;
  100. InventoryLocation handInventoryLocation = NULL;
  101. PlayerBase player = action_data.m_Player;
  102. AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
  103. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  104. int muzzle_index = wpn.GetCurrentMuzzle();
  105. Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
  106. if (am_action_data.m_oldMagazine)
  107. {
  108. oldMagTargetInventoryLocation = new InventoryLocation;
  109. if ( action_data.m_Player.GetInventory().HasInventoryReservation( am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine) )
  110. {
  111. success = false;
  112. }
  113. else
  114. {
  115. player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine,GameInventory.c_InventoryReservationTimeoutMS);
  116. }
  117. }
  118. if (success)
  119. {
  120. targetInventoryLocation = new InventoryLocation;
  121. targetInventoryLocation.SetAttachment( wpn, new_mag, InventorySlots.MAGAZINE);
  122. if ( action_data.m_Player.GetInventory().HasInventoryReservation( new_mag, targetInventoryLocation) )
  123. {
  124. success = false;
  125. if (am_action_data.m_oldMagazine)
  126. {
  127. player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
  128. }
  129. }
  130. else
  131. {
  132. action_data.m_Player.GetInventory().AddInventoryReservationEx( new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
  133. }
  134. }
  135. if (success)
  136. {
  137. handInventoryLocation = new InventoryLocation;
  138. handInventoryLocation.SetHands(action_data.m_Player, wpn);
  139. if ( action_data.m_Player.GetInventory().HasInventoryReservation( wpn, handInventoryLocation) )
  140. {
  141. if (am_action_data.m_oldMagazine)
  142. {
  143. player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
  144. }
  145. player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
  146. success = false;
  147. }
  148. else
  149. {
  150. action_data.m_Player.GetInventory().AddInventoryReservationEx( wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
  151. }
  152. }
  153. if ( success )
  154. {
  155. if (am_action_data.m_ilOldMagazine)
  156. action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
  157. if (targetInventoryLocation)
  158. action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
  159. if (handInventoryLocation)
  160. action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
  161. }
  162. return success;
  163. }
  164. override void Start( ActionData action_data )
  165. {
  166. super.Start( action_data );
  167. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  168. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  169. Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
  170. ClearInventoryReservationEx(action_data);
  171. if ( action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn,mag,false) )
  172. action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
  173. else
  174. action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
  175. InventoryReservation(action_data);
  176. }
  177. override bool CanBePerformedFromInventory()
  178. {
  179. return true;
  180. }
  181. override bool CanBePerformedFromQuickbar()
  182. {
  183. return true;
  184. }
  185. };
  186. class FirearmActionAttachMagazineQuick : FirearmActionBase
  187. {
  188. void FirearmActionAttachMagazineQuick()
  189. {
  190. }
  191. override bool ActionCondition( PlayerBase player, ActionTarget target, ItemBase item ) //condition for action
  192. {
  193. if (!super.ActionCondition( player, target, item ))
  194. return false;
  195. HumanCommandWeapons hcw = player.GetCommandModifier_Weapons();
  196. if (hcw && hcw.GetRunningAction() == WeaponActions.RELOAD)
  197. return false;
  198. MagazineStorage mag = MagazineStorage.Cast(player.GetWeaponManager().GetPreparedMagazine());
  199. if (!mag)
  200. return false;
  201. Weapon weapon = Weapon.Cast(item);
  202. bool isLoadedMag = false;
  203. for (int i = 0, count = weapon.GetMuzzleCount(); i < count; ++i)
  204. isLoadedMag |= ( mag == weapon.GetMagazine( i ) );
  205. return !isLoadedMag;
  206. }
  207. override ActionData CreateActionData()
  208. {
  209. AttachMagazineActionData action_data = new AttachMagazineActionData;
  210. return action_data;
  211. }
  212. override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
  213. {
  214. super.WriteToContext(ctx, action_data);
  215. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  216. action_data_am.m_ilOldMagazine.WriteToContext(ctx);
  217. }
  218. override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
  219. {
  220. if (!action_recive_data)
  221. {
  222. action_recive_data = new AttachMagazineActionReciveData;
  223. }
  224. super.ReadFromContext(ctx, action_recive_data);
  225. InventoryLocation il = new InventoryLocation;
  226. if (!il.ReadFromContext(ctx))
  227. return false;
  228. AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
  229. recive_data_am.m_ilOldMagazine = il;
  230. return true;
  231. }
  232. override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
  233. {
  234. AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
  235. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  236. action_data.m_MainItem = action_recive_data.m_MainItem;
  237. if (!action_recive_data.m_Target)
  238. {
  239. action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
  240. }
  241. else
  242. {
  243. action_data.m_Target = action_recive_data.m_Target;
  244. }
  245. action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
  246. }
  247. override bool Post_SetupAction( ActionData action_data )
  248. {
  249. if ( !GetGame().IsDedicatedServer() )
  250. {
  251. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  252. int muzzle_index = wpn.GetCurrentMuzzle();
  253. AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
  254. am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
  255. ActionTarget newTarget = new ActionTarget(action_data.m_Player.GetWeaponManager().GetPreparedMagazine(), null, -1, vector.Zero, -1);
  256. action_data.m_Target = newTarget;
  257. InventoryLocation new_il = new InventoryLocation;
  258. if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
  259. {
  260. return false;
  261. }
  262. am_action_data.m_ilOldMagazine = new_il;
  263. }
  264. return true;
  265. }
  266. override bool InventoryReservation( ActionData action_data)
  267. {
  268. if( (IsLocal() || !UseAcknowledgment()) && IsInstant() )
  269. return true;
  270. bool success = true;
  271. InventoryLocation oldMagTargetInventoryLocation = NULL;
  272. InventoryLocation targetInventoryLocation = NULL;
  273. InventoryLocation handInventoryLocation = NULL;
  274. PlayerBase player = action_data.m_Player;
  275. AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
  276. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  277. int muzzle_index = wpn.GetCurrentMuzzle();
  278. Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
  279. if (am_action_data.m_oldMagazine)
  280. {
  281. oldMagTargetInventoryLocation = new InventoryLocation;
  282. if ( action_data.m_Player.GetInventory().HasInventoryReservation( am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine) )
  283. {
  284. success = false;
  285. }
  286. else
  287. {
  288. player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine,GameInventory.c_InventoryReservationTimeoutMS);
  289. }
  290. }
  291. if (success)
  292. {
  293. targetInventoryLocation = new InventoryLocation;
  294. targetInventoryLocation.SetAttachment( wpn, new_mag, InventorySlots.MAGAZINE);
  295. if ( action_data.m_Player.GetInventory().HasInventoryReservation( new_mag, targetInventoryLocation) )
  296. {
  297. success = false;
  298. if (am_action_data.m_oldMagazine)
  299. {
  300. player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
  301. }
  302. }
  303. else
  304. {
  305. action_data.m_Player.GetInventory().AddInventoryReservationEx( new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
  306. }
  307. }
  308. if (success)
  309. {
  310. handInventoryLocation = new InventoryLocation;
  311. handInventoryLocation.SetHands(action_data.m_Player, wpn);
  312. if ( action_data.m_Player.GetInventory().HasInventoryReservation( wpn, handInventoryLocation) )
  313. {
  314. if (am_action_data.m_oldMagazine)
  315. {
  316. player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
  317. }
  318. player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
  319. success = false;
  320. }
  321. else
  322. {
  323. action_data.m_Player.GetInventory().AddInventoryReservationEx( wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
  324. }
  325. }
  326. if ( success )
  327. {
  328. if (am_action_data.m_ilOldMagazine)
  329. action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
  330. if (targetInventoryLocation)
  331. action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
  332. if (handInventoryLocation)
  333. action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
  334. }
  335. return success;
  336. }
  337. override void Start( ActionData action_data )
  338. {
  339. super.Start( action_data );
  340. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  341. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  342. Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
  343. ClearInventoryReservationEx(action_data);
  344. if ( action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn,mag,false) )
  345. action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
  346. else
  347. action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
  348. InventoryReservation(action_data);
  349. }
  350. override bool HasTarget()
  351. {
  352. return true;
  353. }
  354. override bool HasProgress()
  355. {
  356. return false;
  357. }
  358. override typename GetInputType()
  359. {
  360. return ContinuousWeaponManipulationActionInput;
  361. }
  362. override void CreateConditionComponents()
  363. {
  364. m_ConditionItem = new CCINonRuined();
  365. m_ConditionTarget = new CCTSelf;
  366. }
  367. };