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.

202 lines
6.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Client's C_BaseCombatCharacter entity
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include "cbase.h"
  14. #include "c_basecombatcharacter.h"
  15. #include "c_cs_player.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. #if defined( CBaseCombatCharacter )
  19. #undef CBaseCombatCharacter
  20. #endif
  21. //-----------------------------------------------------------------------------
  22. // Purpose:
  23. //-----------------------------------------------------------------------------
  24. C_BaseCombatCharacter::C_BaseCombatCharacter()
  25. {
  26. for ( int i=0; i < m_iAmmo.Count(); i++ )
  27. m_iAmmo.Set( i, 0 );
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. C_BaseCombatCharacter::~C_BaseCombatCharacter()
  33. {
  34. }
  35. /*
  36. //-----------------------------------------------------------------------------
  37. // Purpose: Returns the amount of ammunition of the specified type the character's carrying
  38. //-----------------------------------------------------------------------------
  39. int C_BaseCombatCharacter::GetAmmoCount( char *szName ) const
  40. {
  41. return GetAmmoCount( g_pGameRules->GetAmmoDef()->Index(szName) );
  42. }
  43. */
  44. //-----------------------------------------------------------------------------
  45. // Purpose: Overload our muzzle flash and send it to any actively held weapon
  46. //-----------------------------------------------------------------------------
  47. void C_BaseCombatCharacter::DoMuzzleFlash()
  48. {
  49. // Our weapon takes our muzzle flash command
  50. C_BaseCombatWeapon *pWeapon = GetActiveWeapon();
  51. if ( pWeapon )
  52. {
  53. pWeapon->DoMuzzleFlash();
  54. //NOTENOTE: We do not chain to the base here
  55. }
  56. else
  57. {
  58. BaseClass::DoMuzzleFlash();
  59. }
  60. }
  61. void C_BaseCombatCharacter::OnDataChanged( DataUpdateType_t updateType )
  62. {
  63. BaseClass::OnDataChanged( updateType );
  64. // view weapon model cache monitoring
  65. // NOTE: expected to be updated ONLY once per frame for the primary player ONLY!
  66. // the expectation is that there is ONLY one customer that requires view models
  67. // otherwise the lower level code will thrash as it tries to maintain a single player's view model inventory
  68. {
  69. const char *viewWeapons[MAX_WEAPONS];
  70. int nNumViewWeapons = 0;
  71. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  72. if ( pLocalPlayer == this && !pLocalPlayer->IsObserver() )
  73. {
  74. // want to know what this player's weapon inventory is to keep all of these in cache
  75. for ( int i = 0; i < MAX_WEAPONS; i++ )
  76. {
  77. C_BaseCombatWeapon *pWeapon = GetWeapon( i );
  78. if ( !pWeapon )
  79. continue;
  80. viewWeapons[nNumViewWeapons] = pWeapon->GetViewModel();
  81. nNumViewWeapons++;
  82. }
  83. }
  84. else if ( pLocalPlayer && pLocalPlayer->GetObserverMode() == OBS_MODE_IN_EYE && pLocalPlayer->GetObserverTarget() == this )
  85. {
  86. // once spectating, PURPOSELY only the active view weapon gets tracked
  87. // cycling through spectators is the more common pattern and tracking just the active weapon prevents massive cache thrashing
  88. // otherwise maintaining the observer targets inventories would needlessly thrash the cache as the player rapidly cycles
  89. C_BaseCombatWeapon *pWeapon = pLocalPlayer->GetActiveWeapon();
  90. if ( pWeapon )
  91. {
  92. viewWeapons[nNumViewWeapons] = pWeapon->GetViewModel();
  93. nNumViewWeapons++;
  94. }
  95. }
  96. if ( nNumViewWeapons )
  97. {
  98. // view model weapons are subject to a cache policy that needs to be kept accurate for a SINGLE Player
  99. modelinfo->UpdateViewWeaponModelCache( viewWeapons, nNumViewWeapons );
  100. }
  101. }
  102. // world weapon model cache monitoring
  103. // world weapons have a much looser cache policy and just needs to monitor the important set of world weapons
  104. {
  105. const char *worldWeapons[MAX_WEAPONS];
  106. int nNumWorldWeapons = 0;
  107. // want to track any world models that are active weapons
  108. // the world weapons lying on the ground are the ones that become LRU purge candidates
  109. C_BasePlayer *pPlayer = ToBasePlayer( this );
  110. if ( pPlayer )
  111. {
  112. C_BaseCombatWeapon *pWeapon = pPlayer->GetActiveWeapon();
  113. if ( pWeapon )
  114. {
  115. worldWeapons[nNumWorldWeapons] = pWeapon->GetWorldModel();
  116. nNumWorldWeapons++;
  117. }
  118. }
  119. C_CSPlayer *pCSPlayer = C_CSPlayer::GetLocalCSPlayer();
  120. if ( pCSPlayer == this )
  121. {
  122. int weaponEntIndex = pCSPlayer->GetTargetedWeapon();
  123. if ( weaponEntIndex > 0 ) //0 is a valid entity index, but will never be used for a weapon
  124. {
  125. C_BaseEntity *pEnt = cl_entitylist->GetEnt( weaponEntIndex );
  126. C_BaseCombatWeapon *pWeapon = dynamic_cast< C_BaseCombatWeapon * >( pEnt );
  127. if ( pWeapon )
  128. {
  129. worldWeapons[nNumWorldWeapons] = pWeapon->GetWorldModel();
  130. nNumWorldWeapons++;
  131. }
  132. }
  133. }
  134. if ( nNumWorldWeapons )
  135. {
  136. modelinfo->TouchWorldWeaponModelCache( worldWeapons, nNumWorldWeapons );
  137. }
  138. }
  139. }
  140. bool C_BaseCombatCharacter::HasEverBeenInjured( void ) const
  141. {
  142. return ( m_flTimeOfLastInjury != 0.0f );
  143. }
  144. float C_BaseCombatCharacter::GetTimeSinceLastInjury( void ) const
  145. {
  146. return gpGlobals->curtime - m_flTimeOfLastInjury;
  147. }
  148. IMPLEMENT_CLIENTCLASS(C_BaseCombatCharacter, DT_BaseCombatCharacter, CBaseCombatCharacter);
  149. // Only send active weapon index to local player
  150. BEGIN_RECV_TABLE_NOBASE( C_BaseCombatCharacter, DT_BCCLocalPlayerExclusive )
  151. RecvPropTime( RECVINFO( m_flNextAttack ) ),
  152. END_RECV_TABLE();
  153. BEGIN_RECV_TABLE_NOBASE( C_BaseCombatCharacter, DT_BCCNonLocalPlayerExclusive )
  154. #if defined( CSTRIKE15 )
  155. // In CS:GO send active weapon index to all players except local
  156. RecvPropArray3( RECVINFO_ARRAY(m_hMyWeapons), RecvPropEHandle( RECVINFO( m_hMyWeapons[0] ) ) ),
  157. #endif
  158. END_RECV_TABLE();
  159. BEGIN_RECV_TABLE(C_BaseCombatCharacter, DT_BaseCombatCharacter)
  160. RecvPropDataTable( "bcc_localdata", 0, 0, &REFERENCE_RECV_TABLE(DT_BCCLocalPlayerExclusive) ),
  161. RecvPropDataTable( "bcc_nonlocaldata", 0, 0, &REFERENCE_RECV_TABLE(DT_BCCNonLocalPlayerExclusive) ),
  162. RecvPropInt( RECVINFO( m_LastHitGroup ) ),
  163. RecvPropEHandle( RECVINFO( m_hActiveWeapon ) ),
  164. RecvPropTime( RECVINFO( m_flTimeOfLastInjury ) ),
  165. RecvPropInt( RECVINFO( m_nRelativeDirectionOfLastInjury ) ),
  166. RecvPropArray3( RECVINFO_ARRAY(m_hMyWeapons), RecvPropEHandle( RECVINFO( m_hMyWeapons[0] ) ) ),
  167. #ifdef INVASION_CLIENT_DLL
  168. RecvPropInt( RECVINFO( m_iPowerups ) ),
  169. #endif
  170. END_RECV_TABLE()
  171. BEGIN_PREDICTION_DATA( C_BaseCombatCharacter )
  172. DEFINE_PRED_ARRAY( m_iAmmo, FIELD_INTEGER, MAX_AMMO_TYPES, FTYPEDESC_INSENDTABLE ),
  173. DEFINE_PRED_FIELD( m_flNextAttack, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
  174. DEFINE_PRED_FIELD( m_hActiveWeapon, FIELD_EHANDLE, FTYPEDESC_INSENDTABLE ),
  175. DEFINE_PRED_ARRAY( m_hMyWeapons, FIELD_EHANDLE, MAX_WEAPONS, FTYPEDESC_INSENDTABLE ),
  176. END_PREDICTION_DATA()