magazine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. typedef Magazine Magazine_Base;
  2. enum CartridgeType
  3. {
  4. None = 0,
  5. Pistol = 1,
  6. Intermediate = 2,
  7. FullPower = 3,
  8. Shell = 4,
  9. Arrow = 5
  10. }
  11. enum ProjectileType
  12. {
  13. None = 0,
  14. Tracer = 1,
  15. AP = 2
  16. }
  17. class AmmoData
  18. {
  19. bool m_IsValid;
  20. CartridgeType m_CartridgeType;
  21. ProjectileType m_ProjectileType;
  22. void AmmoData( string init_type )
  23. {
  24. m_IsValid = GetGame().ConfigIsExisting( "CfgMagazines " + init_type );
  25. if ( m_IsValid )
  26. {
  27. m_CartridgeType = GetGame().ConfigGetInt( "CfgMagazines " + init_type + " iconCartridge" );
  28. m_ProjectileType = GetGame().ConfigGetInt( "CfgMagazines " + init_type + " iconType" );
  29. }
  30. }
  31. }
  32. class Magazine : InventoryItemSuper
  33. {
  34. protected static ref map<string, ref AmmoData> m_AmmoData;
  35. ref array<string> m_CompatiableAmmo;
  36. ref array<float> m_ChanceToJam;
  37. protected float m_ManipulationDamage;
  38. void Magazine ()
  39. {
  40. m_ChanceToJam = new array<float>;
  41. InitReliability(m_ChanceToJam);
  42. m_ManipulationDamage = ConfigGetFloat("manipulationDamage");
  43. m_CompatiableAmmo = new array<string>;
  44. ConfigGetTextArray("ammoItems", m_CompatiableAmmo);
  45. if ( !GetGame().IsDedicatedServer() )
  46. {
  47. if ( !m_AmmoData )
  48. m_AmmoData = new map<string, ref AmmoData>;
  49. string classname = ClassName();
  50. if ( !m_AmmoData.Contains(classname) )
  51. {
  52. ref AmmoData new_data = new AmmoData( classname );
  53. if ( new_data.m_IsValid )
  54. m_AmmoData.Insert( classname, new AmmoData( classname ) );
  55. }
  56. }
  57. }
  58. //! Gets magazine ammo count
  59. proto native int GetAmmoCount();
  60. //! Sets magazine ammo count
  61. proto native void ServerSetAmmoCount(int ammoCount);
  62. proto native void LocalSetAmmoCount(int ammoCount);
  63. /**@fn AcquireCartridge
  64. * @brief acquires cartridge(damage, type) to magazine
  65. * @param[out] ammoDamage \p damage of the ammo
  66. * @param[out] cartTypeName \p type name of the ejected ammo
  67. * @return true if acquired
  68. **/
  69. proto bool LocalAcquireCartridge(out float dmg, out string cartTypeName);
  70. proto bool ServerAcquireCartridge(out float dmg, out string cartTypeName);
  71. /**@fn StoreCartridge
  72. * @brief stores cartridge(damage, type) to magazine
  73. * @param[in] ammoDamage \p damage of the cartridge
  74. * @param[in] cartTypeName \p type name of the stored cartridge
  75. * @return true if stored
  76. **/
  77. proto native bool LocalStoreCartridge(float ammoDamage, string cartTypeName);
  78. proto native bool ServerStoreCartridge(float ammoDamage, string cartTypeName);
  79. /**@fn GetCartridgeAtIndex
  80. * @brief queries cartridge(damage, type) info at specified index
  81. * @param[in] cartIndex \p index of the cartridge.
  82. * @param[out] ammoDamage \p damage of the cartridge
  83. * @param[out] cartTypeName \p type name of the cartridge
  84. * @return true if index valid
  85. **/
  86. proto bool GetCartridgeAtIndex(int cartIndex, out float dmg, out string cartTypeName);
  87. /**@fn SetCartridgeAtIndex
  88. * @brief modifies cartridge(damage, type) info at specified index
  89. * @param[in] cartIndex \p index of the cartridge.
  90. * @param[in] ammoDamage \p damage of the cartridge
  91. * @param[in] cartTypeName \p type name of the cartridge
  92. * @return true if index valid
  93. **/
  94. proto bool SetCartridgeAtIndex(int cartIndex, out float dmg, out string cartTypeName);
  95. /**@fn SetCartridgeDamageAtIndex
  96. * @brief modifies cartridge damage info at specified index
  97. * @param[in] cartIndex \p index of the cartridge.
  98. * @param[in] ammoDamage \p damage of the cartridge
  99. * @return true if index valid
  100. **/
  101. proto bool SetCartridgeDamageAtIndex(int cartIndex, float dmg);
  102. static AmmoData GetAmmoData( string classname )
  103. {
  104. if ( !m_AmmoData )
  105. m_AmmoData = new map<string, ref AmmoData>;
  106. if ( !m_AmmoData.Contains(classname) )
  107. {
  108. ref AmmoData new_data = new AmmoData( classname );
  109. if ( new_data.m_IsValid )
  110. m_AmmoData.Insert( classname, new AmmoData( classname ) );
  111. return new_data;
  112. }
  113. else
  114. {
  115. return m_AmmoData.Get( classname );
  116. }
  117. }
  118. bool IsCompatiableAmmo( ItemBase ammo )
  119. {
  120. if ( m_CompatiableAmmo && ammo )
  121. return ( m_CompatiableAmmo.Find( ammo.GetType() ) > -1 );
  122. else
  123. return false;
  124. }
  125. bool CanAddCartridges(int count)
  126. {
  127. int spc_avail = GetAmmoMax() - GetAmmoCount();
  128. return count <= spc_avail;
  129. }
  130. //! Adds magazine ammo, MP safe
  131. void ServerAddAmmoCount(int ammoCount)
  132. {
  133. ServerSetAmmoCount(GetAmmoCount() + ammoCount);
  134. }
  135. void LocalAddAmmoCount(int ammoCount)
  136. {
  137. LocalSetAmmoCount(GetAmmoCount() + ammoCount);
  138. }
  139. //! returns max rounds for this mag (returns "count" config value)
  140. int GetAmmoMax()
  141. {
  142. return m_Count;
  143. }
  144. //! set max rounds for this mag
  145. void ServerSetAmmoMax()
  146. {
  147. ServerSetAmmoCount( GetAmmoMax() );
  148. }
  149. void LocalSetAmmoMax()
  150. {
  151. LocalSetAmmoCount( GetAmmoMax() );
  152. }
  153. //! Returns if this entity is Magazine
  154. override bool IsMagazine()
  155. {
  156. return true;
  157. }
  158. override bool CanBeSplit()
  159. {
  160. if ( m_CanThisBeSplit )
  161. return ( GetAmmoCount() > 1 );
  162. return false;
  163. }
  164. bool InitReliability(out array<float> reliability_array)
  165. {
  166. if (GetGame().ConfigIsExisting("cfgMagazines " + GetType() + " Reliability ChanceToJam"))
  167. {
  168. GetGame().ConfigGetFloatArray("cfgMagazines " + GetType() + " Reliability ChanceToJam",reliability_array);
  169. return true;
  170. }
  171. return false;
  172. }
  173. float GetChanceToJam()
  174. {
  175. int level = GetHealthLevel();
  176. if (level >= 0 && level < m_ChanceToJam.Count())
  177. return m_ChanceToJam[level];
  178. else
  179. return 0.0;
  180. }
  181. override void SplitItemToInventoryLocation( notnull InventoryLocation dst )
  182. {
  183. if ( !CanBeSplit() )
  184. return;
  185. Magazine new_pile = Magazine.Cast( GameInventory.LocationCreateEntity( dst, GetType(), ECE_IN_INVENTORY, RF_DEFAULT ) );
  186. if( new_pile )
  187. {
  188. MiscGameplayFunctions.TransferItemProperties(dst.GetItem(), new_pile);
  189. new_pile.ServerSetAmmoCount(0);
  190. int quantity = GetAmmoCount();
  191. for (int i = 0; i < Math.Floor( quantity * 0.5 ); ++i)
  192. {
  193. float damage;
  194. string cartrige_name;
  195. ServerAcquireCartridge(damage, cartrige_name);
  196. new_pile.ServerStoreCartridge(damage, cartrige_name);
  197. }
  198. new_pile.SetSynchDirty();
  199. SetSynchDirty();
  200. }
  201. }
  202. override void SplitItem(PlayerBase player)
  203. {
  204. if ( !CanBeSplit() )
  205. return;
  206. Magazine new_pile = Magazine.Cast( player.CreateCopyOfItemInInventoryOrGround( this ) );
  207. if( new_pile )
  208. {
  209. new_pile.ServerSetAmmoCount(0);
  210. int quantity = this.GetAmmoCount();
  211. for (int i = 0; i < Math.Floor( quantity / 2 ); i++)
  212. {
  213. float damage;
  214. string cartrige_name;
  215. ServerAcquireCartridge(damage, cartrige_name);
  216. new_pile.ServerStoreCartridge(damage, cartrige_name);
  217. }
  218. new_pile.SetSynchDirty();
  219. SetSynchDirty();
  220. }
  221. }
  222. void ApplyManipulationDamage()
  223. {
  224. AddHealth("","Health",-m_ManipulationDamage);
  225. }
  226. override bool IsFullQuantity()
  227. {
  228. if ( GetAmmoCount() == GetAmmoMax() )
  229. {
  230. return true;
  231. }
  232. else
  233. {
  234. return false;
  235. }
  236. }
  237. override protected float GetWeightSpecialized(bool forceRecalc = false)
  238. {
  239. #ifdef DEVELOPER
  240. if (WeightDebug.m_VerbosityFlags & WeightDebugType.RECALC_FORCED)
  241. {
  242. WeightDebugData data = WeightDebug.GetWeightDebug(this);
  243. data.SetCalcDetails("TMAG: ("+GetAmmoCount()+"(Ammo count) * " + ConfigGetFloat("weightPerQuantityUnit")+"(weightPerQuantityUnit)) + " + GetConfigWeightModifiedDebugText());
  244. }
  245. #endif
  246. return GetConfigWeightModified() + (GetAmmoCount() * ConfigGetFloat("weightPerQuantityUnit"));
  247. }
  248. override bool IsCombineAll( ItemBase other_item, bool use_stack_max = false)
  249. {
  250. Magazine other_magazine = Magazine.Cast(other_item);
  251. int free_space = GetAmmoMax() - GetAmmoCount();
  252. return free_space >= other_magazine.GetAmmoCount();
  253. }
  254. override void CombineItems( ItemBase other_item, bool use_stack_max = false )
  255. {
  256. if ( !CanBeCombined(other_item) )
  257. return;
  258. if ( other_item.GetType() != GetType() )
  259. return;
  260. Magazine other_magazine;
  261. if ( Class.CastTo(other_magazine, other_item) )
  262. {
  263. //int other_item_quantity = other_magazine.GetAmmoCount();
  264. int this_free_space = GetAmmoMax() - GetAmmoCount();
  265. int numberOfTransferredBullets = 0;
  266. int currentAmount = GetAmmoCount();
  267. for (int i = 0; i < this_free_space && other_magazine.GetAmmoCount() > 0 ; i++)
  268. {
  269. float damage;
  270. string cartrige_name;
  271. other_magazine.ServerAcquireCartridge(damage, cartrige_name);
  272. if (ServerStoreCartridge(damage, cartrige_name))
  273. ++numberOfTransferredBullets;
  274. }
  275. if (GetGame().IsServer())
  276. {
  277. float resultingHealth = (currentAmount * GetHealth() + numberOfTransferredBullets * other_magazine.GetHealth()) / GetAmmoCount();
  278. SetHealth("", "", resultingHealth);
  279. }
  280. OnCombine(other_item);
  281. other_magazine.SetSynchDirty();
  282. SetSynchDirty();
  283. }
  284. }
  285. override bool CanDetachAttachment(EntityAI parent)
  286. {
  287. PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
  288. if (player)
  289. {
  290. Weapon_Base wpn = Weapon_Base.Cast(parent);
  291. if (wpn)
  292. {
  293. return player.GetWeaponManager().CanDetachMagazine(wpn,this);
  294. }
  295. }
  296. return super.CanDetachAttachment(parent);
  297. }
  298. override void OnInventoryEnter(Man player)
  299. {
  300. super.OnInventoryEnter(player);
  301. PlayerBase p = PlayerBase.Cast(player);
  302. p.GetWeaponManager().OnMagazineInventoryEnter(this);
  303. }
  304. override void OnInventoryExit(Man player)
  305. {
  306. super.OnInventoryExit(player);
  307. PlayerBase p = PlayerBase.Cast(player);
  308. p.GetWeaponManager().OnMagazineInventoryExit(this);
  309. }
  310. override void OnWasAttached( EntityAI parent, int slot_id )
  311. {
  312. super.OnWasAttached(parent, slot_id);
  313. PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
  314. Weapon_Base wpn = Weapon_Base.Cast(parent);
  315. if (wpn && player)
  316. {
  317. player.GetWeaponManager().OnMagazineAttach(this);
  318. }
  319. }
  320. override void OnWasDetached( EntityAI parent, int slot_id )
  321. {
  322. super.OnWasDetached(parent, slot_id);
  323. PlayerBase player = PlayerBase.Cast(GetHierarchyRootPlayer());
  324. Weapon_Base wpn = Weapon_Base.Cast(parent);
  325. if (wpn && player)
  326. {
  327. player.GetWeaponManager().OnMagazineDetach(this);
  328. }
  329. }
  330. override void EEHealthLevelChanged( int oldLevel, int newLevel, string zone )
  331. {
  332. super.EEHealthLevelChanged(oldLevel, newLevel, zone);
  333. float damage = 1 - GetHealthLevelValue(newLevel) + 0.001;
  334. int cartridgeCount = GetAmmoCount();
  335. for (int i = 0; i < cartridgeCount; ++i)
  336. SetCartridgeDamageAtIndex(i, damage);
  337. }
  338. override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
  339. {
  340. super.GetDebugActions(outputList);
  341. if (GetAmmoCount() > 0)
  342. {
  343. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "", FadeColors.LIGHT_GREY));
  344. outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.PRINT_BULLETS, "Print Bullets", FadeColors.LIGHT_GREY));
  345. }
  346. }
  347. override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
  348. {
  349. if (GetGame().IsServer())
  350. {
  351. if (action_id == EActions.PRINT_BULLETS)
  352. {
  353. Magazine magazine;
  354. Class.CastTo(magazine, this);
  355. for (int i = 0; i < magazine.GetAmmoCount(); i++)
  356. {
  357. float damage;
  358. string className;
  359. magazine.GetCartridgeAtIndex(i, damage, className);
  360. Debug.Log(string.Format("Bullet: %1, Damage %2", className, damage));
  361. }
  362. }
  363. }
  364. return super.OnAction(action_id, player, ctx);
  365. }
  366. override bool CanBeFSwaped()
  367. {
  368. Weapon_Base wpn = Weapon_Base.Cast(GetHierarchyParent());
  369. if (wpn)
  370. {
  371. return false;
  372. }
  373. return true;
  374. }
  375. }
  376. class MagazineStorage : Magazine
  377. {
  378. override void SetActions()
  379. {
  380. super.SetActions();
  381. AddAction(ActionLoadMagazine);
  382. AddAction(ActionEmptyMagazine);
  383. AddAction(ActionLoadMagazineQuick);
  384. }
  385. }