Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
9.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Base combat character with no AI
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #include "cbase.h"
  10. #include "ammodef.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //-----------------------------------------------------------------------------
  14. // Purpose: Return a pointer to the Ammo at the Index passed in
  15. //-----------------------------------------------------------------------------
  16. Ammo_t *CAmmoDef::GetAmmoOfIndex(int nAmmoIndex)
  17. {
  18. if ( nAmmoIndex >= m_nAmmoIndex )
  19. return NULL;
  20. return &m_AmmoType[ nAmmoIndex ];
  21. }
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. // Input :
  25. // Output :
  26. //-----------------------------------------------------------------------------
  27. int CAmmoDef::Index(const char *psz)
  28. {
  29. int i;
  30. if (!psz)
  31. return -1;
  32. for (i = 1; i < m_nAmmoIndex; i++)
  33. {
  34. if (stricmp( psz, m_AmmoType[i].pName ) == 0)
  35. return i;
  36. }
  37. return -1;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Purpose:
  41. // Input :
  42. // Output :
  43. //-----------------------------------------------------------------------------
  44. int CAmmoDef::PlrDamage(int nAmmoIndex)
  45. {
  46. if ( nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex )
  47. return 0;
  48. if ( m_AmmoType[nAmmoIndex].pPlrDmg == USE_CVAR )
  49. {
  50. if ( m_AmmoType[nAmmoIndex].pPlrDmgCVar )
  51. {
  52. return m_AmmoType[nAmmoIndex].pPlrDmgCVar->GetFloat();
  53. }
  54. return 0;
  55. }
  56. else
  57. {
  58. return m_AmmoType[nAmmoIndex].pPlrDmg;
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. // Input :
  64. // Output :
  65. //-----------------------------------------------------------------------------
  66. int CAmmoDef::NPCDamage(int nAmmoIndex)
  67. {
  68. if ( nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex )
  69. return 0;
  70. if ( m_AmmoType[nAmmoIndex].pNPCDmg == USE_CVAR )
  71. {
  72. if ( m_AmmoType[nAmmoIndex].pNPCDmgCVar )
  73. {
  74. return m_AmmoType[nAmmoIndex].pNPCDmgCVar->GetFloat();
  75. }
  76. return 0;
  77. }
  78. else
  79. {
  80. return m_AmmoType[nAmmoIndex].pNPCDmg;
  81. }
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose:
  85. // Input :
  86. // Output :
  87. //-----------------------------------------------------------------------------
  88. int CAmmoDef::MaxCarry(int nAmmoIndex, const CBaseCombatCharacter *owner)
  89. {
  90. if ( nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex )
  91. return 0;
  92. if ( m_AmmoType[nAmmoIndex].pMaxCarry == USE_CVAR )
  93. {
  94. if ( m_AmmoType[nAmmoIndex].pMaxCarryCVar )
  95. return m_AmmoType[nAmmoIndex].pMaxCarryCVar->GetInt();
  96. return 0;
  97. }
  98. else
  99. {
  100. return m_AmmoType[nAmmoIndex].pMaxCarry;
  101. }
  102. }
  103. bool CAmmoDef::CanCarryInfiniteAmmo(int nAmmoIndex)
  104. {
  105. if ( nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex )
  106. return false;
  107. int maxCarry = m_AmmoType[nAmmoIndex].pMaxCarry;
  108. if ( maxCarry == USE_CVAR )
  109. {
  110. if ( m_AmmoType[nAmmoIndex].pMaxCarryCVar )
  111. {
  112. maxCarry = m_AmmoType[nAmmoIndex].pMaxCarryCVar->GetInt();
  113. }
  114. }
  115. return maxCarry == INFINITE_AMMO ? true : false;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Purpose:
  119. // Input :
  120. // Output :
  121. //-----------------------------------------------------------------------------
  122. int CAmmoDef::DamageType(int nAmmoIndex)
  123. {
  124. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  125. return 0;
  126. return m_AmmoType[nAmmoIndex].nDamageType;
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Purpose:
  130. //-----------------------------------------------------------------------------
  131. int CAmmoDef::Flags(int nAmmoIndex)
  132. {
  133. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  134. return 0;
  135. return m_AmmoType[nAmmoIndex].nFlags;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose:
  139. // Input :
  140. // Output :
  141. //-----------------------------------------------------------------------------
  142. int CAmmoDef::MinSplashSize(int nAmmoIndex)
  143. {
  144. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  145. return 4;
  146. return m_AmmoType[nAmmoIndex].nMinSplashSize;
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose:
  150. // Input :
  151. // Output :
  152. //-----------------------------------------------------------------------------
  153. int CAmmoDef::MaxSplashSize(int nAmmoIndex)
  154. {
  155. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  156. return 8;
  157. return m_AmmoType[nAmmoIndex].nMaxSplashSize;
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Purpose:
  161. // Input :
  162. // Output :
  163. //-----------------------------------------------------------------------------
  164. int CAmmoDef::TracerType(int nAmmoIndex)
  165. {
  166. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  167. return 0;
  168. return m_AmmoType[nAmmoIndex].eTracerType;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose: Figure out the damage force for a certian ammo type.
  172. // Input :
  173. // Output :
  174. //-----------------------------------------------------------------------------
  175. float CAmmoDef::DamageForce(int nAmmoIndex)
  176. {
  177. const float DEFAULT_IMPULSE = 1.0f;
  178. if ( nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex )
  179. return DEFAULT_IMPULSE;
  180. if ( m_AmmoType[nAmmoIndex].pPhysicsForceImpulse == USE_CVAR )
  181. {
  182. if ( m_AmmoType[nAmmoIndex].pPhysicsForceImpulseCVar )
  183. return m_AmmoType[nAmmoIndex].pPhysicsForceImpulseCVar->GetFloat();
  184. return DEFAULT_IMPULSE;
  185. }
  186. else
  187. {
  188. return (float)m_AmmoType[nAmmoIndex].pPhysicsForceImpulse;
  189. }
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Purpose: Create an Ammo type with the name, decal, and tracer.
  193. // Does not increment m_nAmmoIndex because the functions below do so and
  194. // are the only entry point.
  195. //-----------------------------------------------------------------------------
  196. bool CAmmoDef::AddAmmoType(char const* name, int damageType, int tracerType, int nFlags, int minSplashSize, int maxSplashSize )
  197. {
  198. if (m_nAmmoIndex == MAX_AMMO_TYPES)
  199. return false;
  200. int len = strlen(name);
  201. m_AmmoType[m_nAmmoIndex].pName = new char[len+1];
  202. Q_strncpy(m_AmmoType[m_nAmmoIndex].pName, name,len+1);
  203. m_AmmoType[m_nAmmoIndex].nDamageType = damageType;
  204. m_AmmoType[m_nAmmoIndex].eTracerType = tracerType;
  205. m_AmmoType[m_nAmmoIndex].nMinSplashSize = minSplashSize;
  206. m_AmmoType[m_nAmmoIndex].nMaxSplashSize = maxSplashSize;
  207. m_AmmoType[m_nAmmoIndex].nFlags = nFlags;
  208. return true;
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Purpose: Add an ammo type with it's damage & carrying capability specified via cvars
  212. //-----------------------------------------------------------------------------
  213. void CAmmoDef::AddAmmoType(char const* name, int damageType, int tracerType,
  214. char const* plr_cvar, char const* npc_cvar, char const* carry_cvar,
  215. char const* impulse_cvar, int nFlags, int minSplashSize, int maxSplashSize)
  216. {
  217. if ( AddAmmoType( name, damageType, tracerType, nFlags, minSplashSize, maxSplashSize ) == false )
  218. return;
  219. if (plr_cvar)
  220. {
  221. m_AmmoType[m_nAmmoIndex].pPlrDmgCVar = cvar->FindVar(plr_cvar);
  222. if (!m_AmmoType[m_nAmmoIndex].pPlrDmgCVar)
  223. {
  224. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,plr_cvar);
  225. }
  226. m_AmmoType[m_nAmmoIndex].pPlrDmg = USE_CVAR;
  227. }
  228. if (npc_cvar)
  229. {
  230. m_AmmoType[m_nAmmoIndex].pNPCDmgCVar = cvar->FindVar(npc_cvar);
  231. if (!m_AmmoType[m_nAmmoIndex].pNPCDmgCVar)
  232. {
  233. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,npc_cvar);
  234. }
  235. m_AmmoType[m_nAmmoIndex].pNPCDmg = USE_CVAR;
  236. }
  237. if (carry_cvar)
  238. {
  239. m_AmmoType[m_nAmmoIndex].pMaxCarryCVar= cvar->FindVar(carry_cvar);
  240. if (!m_AmmoType[m_nAmmoIndex].pMaxCarryCVar)
  241. {
  242. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,carry_cvar);
  243. }
  244. m_AmmoType[m_nAmmoIndex].pMaxCarry = USE_CVAR;
  245. }
  246. if (impulse_cvar)
  247. {
  248. m_AmmoType[m_nAmmoIndex].pPhysicsForceImpulseCVar = cvar->FindVar(impulse_cvar);
  249. if (!m_AmmoType[m_nAmmoIndex].pPhysicsForceImpulseCVar)
  250. {
  251. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,impulse_cvar);
  252. }
  253. else
  254. {
  255. // By default, use whatever value is in the convar at load time.
  256. // To enable real-time updating of the ammo impulse values, execute "tweak_ammo_impulse" which will set pPhysicsForceImpulse to USE_CVAR.
  257. m_AmmoType[m_nAmmoIndex].pPhysicsForceImpulse = m_AmmoType[m_nAmmoIndex].pPhysicsForceImpulseCVar->GetInt();
  258. }
  259. }
  260. m_nAmmoIndex++;
  261. }
  262. //-----------------------------------------------------------------------------
  263. // Purpose: Add an ammo type with it's damage & carrying capability specified via integers
  264. //-----------------------------------------------------------------------------
  265. void CAmmoDef::AddAmmoType(char const* name, int damageType, int tracerType,
  266. int plr_dmg, int npc_dmg, int carry, int impulse,
  267. int nFlags, int minSplashSize, int maxSplashSize )
  268. {
  269. if ( AddAmmoType( name, damageType, tracerType, nFlags, minSplashSize, maxSplashSize ) == false )
  270. return;
  271. m_AmmoType[m_nAmmoIndex].pPlrDmg = plr_dmg;
  272. m_AmmoType[m_nAmmoIndex].pNPCDmg = npc_dmg;
  273. m_AmmoType[m_nAmmoIndex].pMaxCarry = carry;
  274. m_AmmoType[m_nAmmoIndex].pPhysicsForceImpulse = impulse;
  275. m_nAmmoIndex++;
  276. }
  277. //-----------------------------------------------------------------------------
  278. // Purpose: Constructor
  279. // Input :
  280. // Output :
  281. //-----------------------------------------------------------------------------
  282. CAmmoDef::CAmmoDef(void)
  283. {
  284. // Start with an index of 1. Client assumes 0 is an invalid ammo type
  285. m_nAmmoIndex = 1;
  286. memset( m_AmmoType, 0, sizeof( m_AmmoType ) );
  287. }
  288. CAmmoDef::~CAmmoDef( void )
  289. {
  290. for ( int i = 1; i < MAX_AMMO_TYPES; i++ )
  291. {
  292. delete[] m_AmmoType[ i ].pName;
  293. }
  294. }