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.

389 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef TAKEDAMAGEINFO_H
  8. #define TAKEDAMAGEINFO_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "networkvar.h" // todo: change this when DECLARE_CLASS is moved into a better location.
  13. // Used to initialize m_flBaseDamage to something that we know pretty much for sure
  14. // hasn't been modified by a user.
  15. #define BASEDAMAGE_NOT_SPECIFIED FLT_MAX
  16. class CBaseEntity;
  17. class CTakeDamageInfo
  18. {
  19. public:
  20. DECLARE_CLASS_NOBASE( CTakeDamageInfo );
  21. CTakeDamageInfo();
  22. CTakeDamageInfo( CBaseEntity *pInflictor, CBaseEntity *pAttacker, float flDamage, int bitsDamageType, int iKillType = 0, int iObjectsPenetrated = 0 );
  23. CTakeDamageInfo( CBaseEntity *pInflictor, CBaseEntity *pAttacker, CBaseEntity *pWeapon, float flDamage, int bitsDamageType, int iKillType = 0, int iObjectsPenetrated = 0 );
  24. CTakeDamageInfo( CBaseEntity *pInflictor, CBaseEntity *pAttacker, const Vector &damageForce, const Vector &damagePosition, float flDamage, int bitsDamageType, int iKillType = 0, Vector *reportedPosition = NULL, int iObjectsPenetrated = 0 );
  25. CTakeDamageInfo( CBaseEntity *pInflictor, CBaseEntity *pAttacker, CBaseEntity *pWeapon, const Vector &damageForce, const Vector &damagePosition, float flDamage, int bitsDamageType, int iKillType = 0, Vector *reportedPosition = NULL, int iObjectsPenetrated = 0 );
  26. // Inflictor is the weapon or rocket (or player) that is dealing the damage.
  27. CBaseEntity* GetInflictor() const;
  28. void SetInflictor( CBaseEntity *pInflictor );
  29. // Weapon is the weapon that did the attack.
  30. // For hitscan weapons, it'll be the same as the inflictor. For projectile weapons, the projectile
  31. // is the inflictor, and this contains the weapon that created the projectile.
  32. CBaseEntity* GetWeapon() const;
  33. void SetWeapon( CBaseEntity *pWeapon );
  34. // Attacker is the character who originated the attack (like a player or an AI).
  35. CBaseEntity* GetAttacker() const;
  36. void SetAttacker( CBaseEntity *pAttacker );
  37. float GetDamage() const;
  38. void SetDamage( float flDamage );
  39. float GetMaxDamage() const;
  40. void SetMaxDamage( float flMaxDamage );
  41. void ScaleDamage( float flScaleAmount );
  42. void AddDamage( float flAddAmount );
  43. void SubtractDamage( float flSubtractAmount );
  44. float GetBaseDamage() const;
  45. bool BaseDamageIsValid() const;
  46. Vector GetDamageForce() const;
  47. void SetDamageForce( const Vector &damageForce );
  48. void ScaleDamageForce( float flScaleAmount );
  49. Vector GetDamagePosition() const;
  50. void SetDamagePosition( const Vector &damagePosition );
  51. Vector GetReportedPosition() const;
  52. void SetReportedPosition( const Vector &reportedPosition );
  53. int GetDamageType() const;
  54. void SetDamageType( int bitsDamageType );
  55. void AddDamageType( int bitsDamageType );
  56. int GetDamageCustom( void ) const;
  57. void SetDamageCustom( int iDamageCustom );
  58. int GetDamageStats( void ) const;
  59. void SetDamageStats( int iDamageStats );
  60. int GetAmmoType() const;
  61. void SetAmmoType( int iAmmoType );
  62. const char * GetAmmoName() const;
  63. float GetRadius() const;
  64. void SetRadius( float fRadius );
  65. int GetObjectsPenetrated() const; // Returns number of objects this damage has passed through
  66. void SetObjectsPenetrated( int iObjectrPenetrated );
  67. int GetDamagedOtherPlayers() const { return m_iDamagedOtherPlayers; }
  68. void SetDamagedOtherPlayers( int iVal ) { m_iDamagedOtherPlayers = iVal; }
  69. void Set( CBaseEntity *pInflictor, CBaseEntity *pAttacker, float flDamage, int bitsDamageType, int iKillType = 0, int iObjectsPenetrated = 0);
  70. void Set( CBaseEntity *pInflictor, CBaseEntity *pAttacker, CBaseEntity *pWeapon, float flDamage, int bitsDamageType, int iKillType = 0, int iObjectsPenetrated = 0 );
  71. void Set( CBaseEntity *pInflictor, CBaseEntity *pAttacker, const Vector &damageForce, const Vector &damagePosition, float flDamage, int bitsDamageType, int iKillType = 0, Vector *reportedPosition = NULL, int iObjectsPenetrated = 0 );
  72. void Set( CBaseEntity *pInflictor, CBaseEntity *pAttacker, CBaseEntity *pWeapon, const Vector &damageForce, const Vector &damagePosition, float flDamage, int bitsDamageType, int iKillType = 0, Vector *reportedPosition = NULL, int iObjectsPenetrated = 0 );
  73. void AdjustPlayerDamageInflictedForSkillLevel();
  74. void AdjustPlayerDamageTakenForSkillLevel();
  75. // Given a damage type (composed of the #defines above), fill out a string with the appropriate text.
  76. // For designer debug output.
  77. static void DebugGetDamageTypeString(unsigned int DamageType, char *outbuf, int outbuflength );
  78. //private:
  79. void CopyDamageToBaseDamage();
  80. uint32 GetBulletID() const { return m_uiBulletID; }
  81. uint8 GetRecoilIndex() const { return m_uiRecoilIndex; }
  82. void SetBulletID( uint32 bulletid, uint8 uiRecoilIndex )
  83. {
  84. m_uiBulletID = bulletid;
  85. m_uiRecoilIndex = uiRecoilIndex;
  86. }
  87. protected:
  88. void Init( CBaseEntity *pInflictor, CBaseEntity *pAttacker, CBaseEntity *pWeapon, const Vector &damageForce, const Vector &damagePosition, const Vector &reportedPosition, float flDamage, int bitsDamageType, int iKillType, int iObjectsPenetrated );
  89. Vector m_vecDamageForce;
  90. Vector m_vecDamagePosition;
  91. Vector m_vecReportedPosition; // Position players are told damage is coming from
  92. EHANDLE m_hInflictor;
  93. EHANDLE m_hAttacker;
  94. EHANDLE m_hWeapon;
  95. float m_flDamage;
  96. float m_flMaxDamage;
  97. float m_flBaseDamage; // The damage amount before skill leve adjustments are made. Used to get uniform damage forces.
  98. int m_bitsDamageType;
  99. int m_iDamageCustom;
  100. int m_iDamageStats;
  101. int m_iAmmoType; // AmmoType of the weapon used to cause this damage, if any
  102. float m_flRadius;
  103. int m_iDamagedOtherPlayers;
  104. int m_iObjectsPenetrated; // Number of objects penetrated
  105. uint32 m_uiBulletID;
  106. uint8 m_uiRecoilIndex;
  107. DECLARE_SIMPLE_DATADESC();
  108. };
  109. //-----------------------------------------------------------------------------
  110. // Purpose: Multi damage. Used to collect multiple damages in the same frame (i.e. shotgun pellets)
  111. //-----------------------------------------------------------------------------
  112. class CMultiDamage : public CTakeDamageInfo
  113. {
  114. DECLARE_CLASS( CMultiDamage, CTakeDamageInfo );
  115. public:
  116. CMultiDamage();
  117. bool IsClear( void ) { return (m_hTarget == nullptr); }
  118. CBaseEntity *GetTarget() const;
  119. void SetTarget( CBaseEntity *pTarget );
  120. void Init( CBaseEntity *pTarget, CBaseEntity *pInflictor, CBaseEntity *pAttacker, CBaseEntity *pWeapon, const Vector &damageForce, const Vector &damagePosition, const Vector &reportedPosition, float flDamage, int bitsDamageType, int iKillType, int iObjectsPenetrated );
  121. protected:
  122. EHANDLE m_hTarget;
  123. DECLARE_SIMPLE_DATADESC();
  124. };
  125. extern CMultiDamage g_MultiDamage;
  126. // Multidamage accessors
  127. void ClearMultiDamage( void );
  128. void ApplyMultiDamage( void );
  129. void AddMultiDamage( const CTakeDamageInfo &info, CBaseEntity *pEntity );
  130. //-----------------------------------------------------------------------------
  131. // Purpose: Utility functions for physics damage force calculation
  132. //-----------------------------------------------------------------------------
  133. float ImpulseScale( float flTargetMass, float flDesiredSpeed );
  134. void CalculateExplosiveDamageForce( CTakeDamageInfo *info, const Vector &vecDir, const Vector &vecForceOrigin, float flScale = 1.0 );
  135. void CalculateBulletDamageForce( CTakeDamageInfo *info, int iBulletType, const Vector &vecBulletDir, const Vector &vecForceOrigin, float flScale = 1.0 );
  136. void CalculateMeleeDamageForce( CTakeDamageInfo *info, const Vector &vecMeleeDir, const Vector &vecForceOrigin, float flScale = 1.0 );
  137. void GuessDamageForce( CTakeDamageInfo *info, const Vector &vecForceDir, const Vector &vecForceOrigin, float flScale = 1.0 );
  138. // -------------------------------------------------------------------------------------------------- //
  139. // Inlines.
  140. // -------------------------------------------------------------------------------------------------- //
  141. inline CBaseEntity* CTakeDamageInfo::GetInflictor() const
  142. {
  143. return m_hInflictor;
  144. }
  145. inline void CTakeDamageInfo::SetInflictor( CBaseEntity *pInflictor )
  146. {
  147. m_hInflictor = pInflictor;
  148. }
  149. inline CBaseEntity* CTakeDamageInfo::GetAttacker() const
  150. {
  151. return m_hAttacker;
  152. }
  153. inline void CTakeDamageInfo::SetAttacker( CBaseEntity *pAttacker )
  154. {
  155. m_hAttacker = pAttacker;
  156. }
  157. inline CBaseEntity* CTakeDamageInfo::GetWeapon() const
  158. {
  159. return m_hWeapon;
  160. }
  161. inline void CTakeDamageInfo::SetWeapon( CBaseEntity *pWeapon )
  162. {
  163. m_hWeapon = pWeapon;
  164. }
  165. inline float CTakeDamageInfo::GetDamage() const
  166. {
  167. return m_flDamage;
  168. }
  169. inline void CTakeDamageInfo::SetDamage( float flDamage )
  170. {
  171. m_flDamage = flDamage;
  172. }
  173. inline float CTakeDamageInfo::GetMaxDamage() const
  174. {
  175. return m_flMaxDamage;
  176. }
  177. inline void CTakeDamageInfo::SetMaxDamage( float flMaxDamage )
  178. {
  179. m_flMaxDamage = flMaxDamage;
  180. }
  181. inline void CTakeDamageInfo::ScaleDamage( float flScaleAmount )
  182. {
  183. m_flDamage *= flScaleAmount;
  184. }
  185. inline void CTakeDamageInfo::AddDamage( float flAddAmount )
  186. {
  187. m_flDamage += flAddAmount;
  188. }
  189. inline void CTakeDamageInfo::SubtractDamage( float flSubtractAmount )
  190. {
  191. m_flDamage -= flSubtractAmount;
  192. }
  193. inline float CTakeDamageInfo::GetBaseDamage() const
  194. {
  195. if( BaseDamageIsValid() )
  196. return m_flBaseDamage;
  197. // No one ever specified a base damage, so just return damage.
  198. return m_flDamage;
  199. }
  200. inline bool CTakeDamageInfo::BaseDamageIsValid() const
  201. {
  202. return (m_flBaseDamage != BASEDAMAGE_NOT_SPECIFIED);
  203. }
  204. inline Vector CTakeDamageInfo::GetDamageForce() const
  205. {
  206. return m_vecDamageForce;
  207. }
  208. inline void CTakeDamageInfo::SetDamageForce( const Vector &damageForce )
  209. {
  210. m_vecDamageForce = damageForce;
  211. }
  212. inline void CTakeDamageInfo::ScaleDamageForce( float flScaleAmount )
  213. {
  214. m_vecDamageForce *= flScaleAmount;
  215. }
  216. inline Vector CTakeDamageInfo::GetDamagePosition() const
  217. {
  218. return m_vecDamagePosition;
  219. }
  220. inline void CTakeDamageInfo::SetDamagePosition( const Vector &damagePosition )
  221. {
  222. m_vecDamagePosition = damagePosition;
  223. }
  224. inline Vector CTakeDamageInfo::GetReportedPosition() const
  225. {
  226. return m_vecReportedPosition;
  227. }
  228. inline void CTakeDamageInfo::SetReportedPosition( const Vector &reportedPosition )
  229. {
  230. m_vecReportedPosition = reportedPosition;
  231. }
  232. inline int CTakeDamageInfo::GetDamageType() const
  233. {
  234. return m_bitsDamageType;
  235. }
  236. inline void CTakeDamageInfo::SetDamageType( int bitsDamageType )
  237. {
  238. m_bitsDamageType = bitsDamageType;
  239. }
  240. inline void CTakeDamageInfo::AddDamageType( int bitsDamageType )
  241. {
  242. m_bitsDamageType |= bitsDamageType;
  243. }
  244. inline int CTakeDamageInfo::GetDamageCustom() const
  245. {
  246. return m_iDamageCustom;
  247. }
  248. inline void CTakeDamageInfo::SetDamageCustom( int iDamageCustom )
  249. {
  250. m_iDamageCustom = iDamageCustom;
  251. }
  252. inline int CTakeDamageInfo::GetDamageStats() const
  253. {
  254. return m_iDamageCustom;
  255. }
  256. inline void CTakeDamageInfo::SetDamageStats( int iDamageCustom )
  257. {
  258. m_iDamageCustom = iDamageCustom;
  259. }
  260. inline int CTakeDamageInfo::GetAmmoType() const
  261. {
  262. return m_iAmmoType;
  263. }
  264. inline void CTakeDamageInfo::SetAmmoType( int iAmmoType )
  265. {
  266. m_iAmmoType = iAmmoType;
  267. }
  268. inline void CTakeDamageInfo::CopyDamageToBaseDamage()
  269. {
  270. m_flBaseDamage = m_flDamage;
  271. }
  272. inline float CTakeDamageInfo::GetRadius() const
  273. {
  274. return m_flRadius;
  275. }
  276. inline void CTakeDamageInfo::SetRadius( float flRadius )
  277. {
  278. m_flRadius = flRadius;
  279. }
  280. inline int CTakeDamageInfo::GetObjectsPenetrated() const
  281. {
  282. return m_iObjectsPenetrated;
  283. }
  284. inline void CTakeDamageInfo::SetObjectsPenetrated( int iObjectsPenetrated )
  285. {
  286. m_iObjectsPenetrated = iObjectsPenetrated;
  287. }
  288. // -------------------------------------------------------------------------------------------------- //
  289. // Inlines.
  290. // -------------------------------------------------------------------------------------------------- //
  291. inline CBaseEntity *CMultiDamage::GetTarget() const
  292. {
  293. return m_hTarget;
  294. }
  295. inline void CMultiDamage::SetTarget( CBaseEntity *pTarget )
  296. {
  297. m_hTarget = pTarget;
  298. }
  299. #endif // TAKEDAMAGEINFO_H