firearmactionattachmagazine.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. Magazine magazine;
  199. #ifdef SERVER
  200. magazine = Magazine.Cast(target.GetObject());
  201. #else
  202. magazine = Magazine.Cast(player.GetWeaponManager().GetPreparedMagazine());
  203. #endif
  204. if (!magazine)
  205. return false;
  206. MagazineStorage mag = MagazineStorage.Cast(magazine);
  207. if (!mag)
  208. return false;
  209. Weapon weapon = Weapon.Cast(item);
  210. bool isLoadedMag = false;
  211. for (int i = 0, count = weapon.GetMuzzleCount(); i < count; ++i)
  212. isLoadedMag |= ( mag == weapon.GetMagazine( i ) );
  213. return !isLoadedMag;
  214. }
  215. override ActionData CreateActionData()
  216. {
  217. AttachMagazineActionData action_data = new AttachMagazineActionData;
  218. return action_data;
  219. }
  220. override void WriteToContext(ParamsWriteContext ctx, ActionData action_data)
  221. {
  222. super.WriteToContext(ctx, action_data);
  223. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  224. action_data_am.m_ilOldMagazine.WriteToContext(ctx);
  225. }
  226. override bool ReadFromContext(ParamsReadContext ctx, out ActionReciveData action_recive_data )
  227. {
  228. if (!action_recive_data)
  229. {
  230. action_recive_data = new AttachMagazineActionReciveData;
  231. }
  232. super.ReadFromContext(ctx, action_recive_data);
  233. InventoryLocation il = new InventoryLocation;
  234. if (!il.ReadFromContext(ctx))
  235. return false;
  236. AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
  237. recive_data_am.m_ilOldMagazine = il;
  238. return true;
  239. }
  240. override void HandleReciveData(ActionReciveData action_recive_data, ActionData action_data)
  241. {
  242. AttachMagazineActionReciveData recive_data_am = AttachMagazineActionReciveData.Cast(action_recive_data);
  243. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  244. action_data.m_MainItem = action_recive_data.m_MainItem;
  245. if (!action_recive_data.m_Target)
  246. {
  247. action_data.m_Target = new ActionTarget(NULL, NULL, -1, vector.Zero, 0);
  248. }
  249. else
  250. {
  251. action_data.m_Target = action_recive_data.m_Target;
  252. }
  253. action_data_am.m_ilOldMagazine = recive_data_am.m_ilOldMagazine;
  254. }
  255. override bool Post_SetupAction( ActionData action_data )
  256. {
  257. if ( !GetGame().IsDedicatedServer() )
  258. {
  259. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  260. int muzzle_index = wpn.GetCurrentMuzzle();
  261. AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
  262. am_action_data.m_oldMagazine = wpn.GetMagazine(muzzle_index);
  263. ActionTarget newTarget = new ActionTarget(action_data.m_Player.GetWeaponManager().GetPreparedMagazine(), null, -1, vector.Zero, -1);
  264. action_data.m_Target = newTarget;
  265. InventoryLocation new_il = new InventoryLocation;
  266. if (!action_data.m_Player.GetWeaponManager().PrepareInventoryLocationForMagazineSwap(wpn, Magazine.Cast(action_data.m_Target.GetObject()), new_il))
  267. {
  268. return false;
  269. }
  270. am_action_data.m_ilOldMagazine = new_il;
  271. }
  272. return true;
  273. }
  274. override bool InventoryReservation( ActionData action_data)
  275. {
  276. if( (IsLocal() || !UseAcknowledgment()) && IsInstant() )
  277. return true;
  278. bool success = true;
  279. InventoryLocation oldMagTargetInventoryLocation = NULL;
  280. InventoryLocation targetInventoryLocation = NULL;
  281. InventoryLocation handInventoryLocation = NULL;
  282. PlayerBase player = action_data.m_Player;
  283. AttachMagazineActionData am_action_data = AttachMagazineActionData.Cast(action_data);
  284. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  285. int muzzle_index = wpn.GetCurrentMuzzle();
  286. Magazine new_mag = Magazine.Cast(action_data.m_Target.GetObject());
  287. if (am_action_data.m_oldMagazine)
  288. {
  289. oldMagTargetInventoryLocation = new InventoryLocation;
  290. if ( action_data.m_Player.GetInventory().HasInventoryReservation( am_action_data.m_oldMagazine, am_action_data.m_ilOldMagazine) )
  291. {
  292. success = false;
  293. }
  294. else
  295. {
  296. player.GetInventory().AddInventoryReservationEx(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine,GameInventory.c_InventoryReservationTimeoutMS);
  297. }
  298. }
  299. if (success)
  300. {
  301. targetInventoryLocation = new InventoryLocation;
  302. targetInventoryLocation.SetAttachment( wpn, new_mag, InventorySlots.MAGAZINE);
  303. if ( action_data.m_Player.GetInventory().HasInventoryReservation( new_mag, targetInventoryLocation) )
  304. {
  305. success = false;
  306. if (am_action_data.m_oldMagazine)
  307. {
  308. player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
  309. }
  310. }
  311. else
  312. {
  313. action_data.m_Player.GetInventory().AddInventoryReservationEx( new_mag, targetInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
  314. }
  315. }
  316. if (success)
  317. {
  318. handInventoryLocation = new InventoryLocation;
  319. handInventoryLocation.SetHands(action_data.m_Player, wpn);
  320. if ( action_data.m_Player.GetInventory().HasInventoryReservation( wpn, handInventoryLocation) )
  321. {
  322. if (am_action_data.m_oldMagazine)
  323. {
  324. player.GetInventory().ClearInventoryReservation(am_action_data.m_oldMagazine,am_action_data.m_ilOldMagazine);
  325. }
  326. player.GetInventory().ClearInventoryReservation(new_mag, targetInventoryLocation);
  327. success = false;
  328. }
  329. else
  330. {
  331. action_data.m_Player.GetInventory().AddInventoryReservationEx( wpn, handInventoryLocation, GameInventory.c_InventoryReservationTimeoutMS);
  332. }
  333. }
  334. if ( success )
  335. {
  336. if (am_action_data.m_ilOldMagazine)
  337. action_data.m_ReservedInventoryLocations.Insert(am_action_data.m_ilOldMagazine);
  338. if (targetInventoryLocation)
  339. action_data.m_ReservedInventoryLocations.Insert(targetInventoryLocation);
  340. if (handInventoryLocation)
  341. action_data.m_ReservedInventoryLocations.Insert(handInventoryLocation);
  342. }
  343. return success;
  344. }
  345. override void Start( ActionData action_data )
  346. {
  347. super.Start( action_data );
  348. AttachMagazineActionData action_data_am = AttachMagazineActionData.Cast(action_data);
  349. Weapon_Base wpn = Weapon_Base.Cast(action_data.m_MainItem);
  350. Magazine mag = Magazine.Cast(action_data.m_Target.GetObject());
  351. ClearInventoryReservationEx(action_data);
  352. if ( action_data.m_Player.GetWeaponManager().CanAttachMagazine(wpn,mag,false) )
  353. action_data.m_Player.GetWeaponManager().AttachMagazine(mag, this);
  354. else
  355. action_data.m_Player.GetWeaponManager().SwapMagazineEx(mag, action_data_am.m_ilOldMagazine, this);
  356. InventoryReservation(action_data);
  357. }
  358. override bool HasTarget()
  359. {
  360. return true;
  361. }
  362. override bool HasProgress()
  363. {
  364. return false;
  365. }
  366. override typename GetInputType()
  367. {
  368. return ContinuousWeaponManipulationActionInput;
  369. }
  370. override void CreateConditionComponents()
  371. {
  372. m_ConditionItem = new CCINonRuined();
  373. m_ConditionTarget = new CCTSelf;
  374. }
  375. };