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.

150 lines
5.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Entity that propagates general data needed by clients for every player.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "player.h"
  9. #include "player_resource.h"
  10. #include <coordsize.h>
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. // Datatable
  14. IMPLEMENT_SERVERCLASS_ST_NOBASE(CPlayerResource, DT_PlayerResource)
  15. // SendPropArray( SendPropString( SENDINFO(m_szName[0]) ), SENDARRAYINFO(m_szName) ),
  16. SendPropArray3( SENDINFO_ARRAY3(m_iPing), SendPropInt( SENDINFO_ARRAY(m_iPing), 10, SPROP_UNSIGNED ) ),
  17. // SendPropArray( SendPropInt( SENDINFO_ARRAY(m_iPacketloss), 7, SPROP_UNSIGNED ), m_iPacketloss ),
  18. SendPropArray3( SENDINFO_ARRAY3(m_iKills), SendPropInt( SENDINFO_ARRAY(m_iKills), 16 ) ),
  19. SendPropArray3( SENDINFO_ARRAY3(m_iAssists), SendPropInt( SENDINFO_ARRAY(m_iAssists), 16 ) ),
  20. SendPropArray3( SENDINFO_ARRAY3(m_iDeaths), SendPropInt( SENDINFO_ARRAY(m_iDeaths), 12 ) ),
  21. SendPropArray3( SENDINFO_ARRAY3(m_bConnected), SendPropInt( SENDINFO_ARRAY(m_bConnected), 1, SPROP_UNSIGNED ) ),
  22. SendPropArray3( SENDINFO_ARRAY3(m_iTeam), SendPropInt( SENDINFO_ARRAY(m_iTeam), 4 ) ),
  23. SendPropArray3( SENDINFO_ARRAY3(m_iPendingTeam), SendPropInt( SENDINFO_ARRAY(m_iPendingTeam), 4 ) ),
  24. SendPropArray3( SENDINFO_ARRAY3(m_bAlive), SendPropInt( SENDINFO_ARRAY(m_bAlive), 1, SPROP_UNSIGNED ) ),
  25. SendPropArray3( SENDINFO_ARRAY3(m_iHealth), SendPropInt( SENDINFO_ARRAY(m_iHealth), 10 ) ),
  26. SendPropArray3( SENDINFO_ARRAY3(m_iCoachingTeam), SendPropInt( SENDINFO_ARRAY(m_iCoachingTeam), 4 ) ),
  27. END_SEND_TABLE()
  28. BEGIN_DATADESC( CPlayerResource )
  29. // DEFINE_ARRAY( m_iPing, FIELD_INTEGER, MAX_PLAYERS+1 ),
  30. // DEFINE_ARRAY( m_iPacketloss, FIELD_INTEGER, MAX_PLAYERS+1 ),
  31. // DEFINE_ARRAY( m_iKills, FIELD_INTEGER, MAX_PLAYERS+1 ),
  32. // DEFINE_ARRAY( m_iDeaths, FIELD_INTEGER, MAX_PLAYERS+1 ),
  33. // DEFINE_ARRAY( m_bConnected, FIELD_INTEGER, MAX_PLAYERS+1 ),
  34. // DEFINE_FIELD( m_flNextPingUpdate, FIELD_FLOAT ),
  35. // DEFINE_ARRAY( m_iTeam, FIELD_INTEGER, MAX_PLAYERS+1 ),
  36. // DEFINE_ARRAY( m_bAlive, FIELD_INTEGER, MAX_PLAYERS+1 ),
  37. // DEFINE_ARRAY( m_iHealth, FIELD_INTEGER, MAX_PLAYERS+1 ),
  38. // DEFINE_FIELD( m_nUpdateCounter, FIELD_INTEGER ),
  39. // Function Pointers
  40. DEFINE_FUNCTION( ResourceThink ),
  41. END_DATADESC()
  42. LINK_ENTITY_TO_CLASS( player_manager, CPlayerResource );
  43. CPlayerResource *g_pPlayerResource;
  44. //-----------------------------------------------------------------------------
  45. // Purpose:
  46. //-----------------------------------------------------------------------------
  47. void CPlayerResource::Spawn( void )
  48. {
  49. for ( int i=0; i < MAX_PLAYERS+1; i++ )
  50. {
  51. m_iPing.Set( i, 0 );
  52. m_iKills.Set( i, 0 );
  53. m_iAssists.Set( i, 0 );
  54. m_iDeaths.Set( i, 0 );
  55. m_bConnected.Set( i, 0 );
  56. m_iTeam.Set( i, 0 );
  57. m_iPendingTeam.Set( i, 0 );
  58. m_bAlive.Set( i, 0 );
  59. m_iCoachingTeam.Set( i, 0 );
  60. }
  61. SetThink( &CPlayerResource::ResourceThink );
  62. SetNextThink( gpGlobals->curtime );
  63. m_nUpdateCounter = 0;
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Purpose: The Player resource is always transmitted to clients
  67. //-----------------------------------------------------------------------------
  68. int CPlayerResource::UpdateTransmitState()
  69. {
  70. // ALWAYS transmit to all clients.
  71. return SetTransmitState( FL_EDICT_ALWAYS );
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Wrapper for the virtual GrabPlayerData Think function
  75. //-----------------------------------------------------------------------------
  76. void CPlayerResource::ResourceThink( void )
  77. {
  78. m_nUpdateCounter++;
  79. UpdatePlayerData();
  80. SetNextThink( gpGlobals->curtime + 0.1f );
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose:
  84. //-----------------------------------------------------------------------------
  85. void CPlayerResource::UpdatePlayerData( void )
  86. {
  87. for ( int i = 1; i <= gpGlobals->maxClients; i++ )
  88. {
  89. CBasePlayer *pPlayer = (CBasePlayer*)UTIL_PlayerByIndex( i );
  90. if ( pPlayer && pPlayer->IsConnected() )
  91. {
  92. m_iKills.Set( i, pPlayer->FragCount() );
  93. m_iAssists.Set(i, pPlayer->AssistsCount() );
  94. m_iDeaths.Set( i, pPlayer->DeathCount() );
  95. m_bConnected.Set( i, 1 );
  96. m_iTeam.Set( i, pPlayer->GetTeamNumber() );
  97. m_iPendingTeam.Set( i, pPlayer->GetPendingTeamNumber() );
  98. m_bAlive.Set( i, pPlayer->IsAlive()?1:0 );
  99. m_iHealth.Set(i, MAX( 0, pPlayer->GetHealth() ) );
  100. m_iCoachingTeam.Set( i, pPlayer->GetCoachingTeam() );
  101. // Don't update ping / packetloss everytime
  102. if ( !(m_nUpdateCounter%20) )
  103. {
  104. // update ping all 20 think ticks = (20*0.1=2seconds)
  105. int ping, packetloss;
  106. UTIL_GetPlayerConnectionInfo( i, ping, packetloss );
  107. // calc avg for scoreboard so it's not so jittery
  108. ping = 0.8f * m_iPing.Get(i) + 0.2f * ping;
  109. m_iPing.Set( i, ping );
  110. // m_iPacketloss.Set( i, packetloss );
  111. }
  112. }
  113. else
  114. {
  115. m_bConnected.Set( i, 0 );
  116. }
  117. }
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose: Exposes smooth player ping that this PR accumulates over time
  121. //-----------------------------------------------------------------------------
  122. int CPlayerResource::GetPlayerSmoothPing( int iClientIndex )
  123. {
  124. if ( ( iClientIndex > 0 ) && ( iClientIndex <= gpGlobals->maxClients ) )
  125. return m_iPing.Get( iClientIndex );
  126. else
  127. return 0;
  128. }