Team Fortress 2 Source Code as on 22/4/2020
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.

295 lines
8.1 KiB

  1. //========= Copyright 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)
  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->GetFloat();
  96. return 0;
  97. }
  98. else
  99. {
  100. return m_AmmoType[nAmmoIndex].pMaxCarry;
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. // Input :
  106. // Output :
  107. //-----------------------------------------------------------------------------
  108. int CAmmoDef::DamageType(int nAmmoIndex)
  109. {
  110. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  111. return 0;
  112. return m_AmmoType[nAmmoIndex].nDamageType;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Purpose:
  116. //-----------------------------------------------------------------------------
  117. int CAmmoDef::Flags(int nAmmoIndex)
  118. {
  119. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  120. return 0;
  121. return m_AmmoType[nAmmoIndex].nFlags;
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. // Input :
  126. // Output :
  127. //-----------------------------------------------------------------------------
  128. int CAmmoDef::MinSplashSize(int nAmmoIndex)
  129. {
  130. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  131. return 4;
  132. return m_AmmoType[nAmmoIndex].nMinSplashSize;
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. // Input :
  137. // Output :
  138. //-----------------------------------------------------------------------------
  139. int CAmmoDef::MaxSplashSize(int nAmmoIndex)
  140. {
  141. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  142. return 8;
  143. return m_AmmoType[nAmmoIndex].nMaxSplashSize;
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Purpose:
  147. // Input :
  148. // Output :
  149. //-----------------------------------------------------------------------------
  150. int CAmmoDef::TracerType(int nAmmoIndex)
  151. {
  152. if (nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex)
  153. return 0;
  154. return m_AmmoType[nAmmoIndex].eTracerType;
  155. }
  156. float CAmmoDef::DamageForce(int nAmmoIndex)
  157. {
  158. if ( nAmmoIndex < 1 || nAmmoIndex >= m_nAmmoIndex )
  159. return 0;
  160. return m_AmmoType[nAmmoIndex].physicsForceImpulse;
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose: Create an Ammo type with the name, decal, and tracer.
  164. // Does not increment m_nAmmoIndex because the functions below do so and
  165. // are the only entry point.
  166. //-----------------------------------------------------------------------------
  167. bool CAmmoDef::AddAmmoType(char const* name, int damageType, int tracerType, int nFlags, int minSplashSize, int maxSplashSize )
  168. {
  169. if (m_nAmmoIndex == MAX_AMMO_TYPES)
  170. return false;
  171. int len = strlen(name);
  172. m_AmmoType[m_nAmmoIndex].pName = new char[len+1];
  173. Q_strncpy(m_AmmoType[m_nAmmoIndex].pName, name,len+1);
  174. m_AmmoType[m_nAmmoIndex].nDamageType = damageType;
  175. m_AmmoType[m_nAmmoIndex].eTracerType = tracerType;
  176. m_AmmoType[m_nAmmoIndex].nMinSplashSize = minSplashSize;
  177. m_AmmoType[m_nAmmoIndex].nMaxSplashSize = maxSplashSize;
  178. m_AmmoType[m_nAmmoIndex].nFlags = nFlags;
  179. return true;
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Purpose: Add an ammo type with it's damage & carrying capability specified via cvars
  183. //-----------------------------------------------------------------------------
  184. void CAmmoDef::AddAmmoType(char const* name, int damageType, int tracerType,
  185. char const* plr_cvar, char const* npc_cvar, char const* carry_cvar,
  186. float physicsForceImpulse, int nFlags, int minSplashSize, int maxSplashSize)
  187. {
  188. if ( AddAmmoType( name, damageType, tracerType, nFlags, minSplashSize, maxSplashSize ) == false )
  189. return;
  190. if (plr_cvar)
  191. {
  192. m_AmmoType[m_nAmmoIndex].pPlrDmgCVar = cvar->FindVar(plr_cvar);
  193. if (!m_AmmoType[m_nAmmoIndex].pPlrDmgCVar)
  194. {
  195. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,plr_cvar);
  196. }
  197. m_AmmoType[m_nAmmoIndex].pPlrDmg = USE_CVAR;
  198. }
  199. if (npc_cvar)
  200. {
  201. m_AmmoType[m_nAmmoIndex].pNPCDmgCVar = cvar->FindVar(npc_cvar);
  202. if (!m_AmmoType[m_nAmmoIndex].pNPCDmgCVar)
  203. {
  204. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,npc_cvar);
  205. }
  206. m_AmmoType[m_nAmmoIndex].pNPCDmg = USE_CVAR;
  207. }
  208. if (carry_cvar)
  209. {
  210. m_AmmoType[m_nAmmoIndex].pMaxCarryCVar= cvar->FindVar(carry_cvar);
  211. if (!m_AmmoType[m_nAmmoIndex].pMaxCarryCVar)
  212. {
  213. Msg("ERROR: Ammo (%s) found no CVar named (%s)\n",name,carry_cvar);
  214. }
  215. m_AmmoType[m_nAmmoIndex].pMaxCarry = USE_CVAR;
  216. }
  217. m_AmmoType[m_nAmmoIndex].physicsForceImpulse = physicsForceImpulse;
  218. m_nAmmoIndex++;
  219. }
  220. //-----------------------------------------------------------------------------
  221. // Purpose: Add an ammo type with it's damage & carrying capability specified via integers
  222. //-----------------------------------------------------------------------------
  223. void CAmmoDef::AddAmmoType(char const* name, int damageType, int tracerType,
  224. int plr_dmg, int npc_dmg, int carry, float physicsForceImpulse,
  225. int nFlags, int minSplashSize, int maxSplashSize )
  226. {
  227. if ( AddAmmoType( name, damageType, tracerType, nFlags, minSplashSize, maxSplashSize ) == false )
  228. return;
  229. m_AmmoType[m_nAmmoIndex].pPlrDmg = plr_dmg;
  230. m_AmmoType[m_nAmmoIndex].pNPCDmg = npc_dmg;
  231. m_AmmoType[m_nAmmoIndex].pMaxCarry = carry;
  232. m_AmmoType[m_nAmmoIndex].physicsForceImpulse = physicsForceImpulse;
  233. m_nAmmoIndex++;
  234. }
  235. //-----------------------------------------------------------------------------
  236. // Purpose: Constructor
  237. // Input :
  238. // Output :
  239. //-----------------------------------------------------------------------------
  240. CAmmoDef::CAmmoDef(void)
  241. {
  242. // Start with an index of 1. Client assumes 0 is an invalid ammo type
  243. m_nAmmoIndex = 1;
  244. memset( m_AmmoType, 0, sizeof( m_AmmoType ) );
  245. }
  246. CAmmoDef::~CAmmoDef( void )
  247. {
  248. for ( int i = 1; i < MAX_AMMO_TYPES; i++ )
  249. {
  250. delete[] m_AmmoType[ i ].pName;
  251. }
  252. }