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.

164 lines
5.6 KiB

  1. //========= Copyright 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_iScore), SendPropInt( SENDINFO_ARRAY(m_iScore), 12 ) ),
  19. SendPropArray3( SENDINFO_ARRAY3(m_iDeaths), SendPropInt( SENDINFO_ARRAY(m_iDeaths), 12 ) ),
  20. SendPropArray3( SENDINFO_ARRAY3(m_bConnected), SendPropInt( SENDINFO_ARRAY(m_bConnected), 1, SPROP_UNSIGNED ) ),
  21. SendPropArray3( SENDINFO_ARRAY3(m_iTeam), SendPropInt( SENDINFO_ARRAY(m_iTeam), 4 ) ),
  22. SendPropArray3( SENDINFO_ARRAY3(m_bAlive), SendPropInt( SENDINFO_ARRAY(m_bAlive), 1, SPROP_UNSIGNED ) ),
  23. SendPropArray3( SENDINFO_ARRAY3(m_iHealth), SendPropInt( SENDINFO_ARRAY(m_iHealth), -1, SPROP_VARINT | SPROP_UNSIGNED ) ),
  24. SendPropArray3( SENDINFO_ARRAY3(m_iAccountID), SendPropInt( SENDINFO_ARRAY(m_iAccountID), 32, SPROP_UNSIGNED ) ),
  25. SendPropArray3( SENDINFO_ARRAY3(m_bValid), SendPropInt( SENDINFO_ARRAY(m_bValid), 1, SPROP_UNSIGNED ) ),
  26. END_SEND_TABLE()
  27. BEGIN_DATADESC( CPlayerResource )
  28. // DEFINE_ARRAY( m_iPing, FIELD_INTEGER, MAX_PLAYERS+1 ),
  29. // DEFINE_ARRAY( m_iPacketloss, FIELD_INTEGER, MAX_PLAYERS+1 ),
  30. // DEFINE_ARRAY( m_iScore, FIELD_INTEGER, MAX_PLAYERS+1 ),
  31. // DEFINE_ARRAY( m_iDeaths, FIELD_INTEGER, MAX_PLAYERS+1 ),
  32. // DEFINE_ARRAY( m_bConnected, FIELD_INTEGER, MAX_PLAYERS+1 ),
  33. // DEFINE_FIELD( m_flNextPingUpdate, FIELD_FLOAT ),
  34. // DEFINE_ARRAY( m_iTeam, FIELD_INTEGER, MAX_PLAYERS+1 ),
  35. // DEFINE_ARRAY( m_bAlive, FIELD_INTEGER, MAX_PLAYERS+1 ),
  36. // DEFINE_ARRAY( m_iHealth, FIELD_INTEGER, MAX_PLAYERS+1 ),
  37. // DEFINE_FIELD( m_nUpdateCounter, FIELD_INTEGER ),
  38. // Function Pointers
  39. DEFINE_FUNCTION( ResourceThink ),
  40. END_DATADESC()
  41. LINK_ENTITY_TO_CLASS( player_manager, CPlayerResource );
  42. CPlayerResource *g_pPlayerResource;
  43. //-----------------------------------------------------------------------------
  44. // Purpose:
  45. //-----------------------------------------------------------------------------
  46. void CPlayerResource::Spawn( void )
  47. {
  48. for ( int i=0; i < MAX_PLAYERS+1; i++ )
  49. {
  50. Init( i );
  51. }
  52. SetThink( &CPlayerResource::ResourceThink );
  53. SetNextThink( gpGlobals->curtime );
  54. m_nUpdateCounter = 0;
  55. }
  56. void CPlayerResource::Init( int iIndex )
  57. {
  58. m_iPing.Set( iIndex, 0 );
  59. m_iScore.Set( iIndex, 0 );
  60. m_iDeaths.Set( iIndex, 0 );
  61. m_bConnected.Set( iIndex, 0 );
  62. m_iTeam.Set( iIndex, 0 );
  63. m_bAlive.Set( iIndex, 0 );
  64. m_iHealth.Set( iIndex, 0 );
  65. m_iAccountID.Set( iIndex, 0 );
  66. m_bValid.Set( iIndex, 0 );
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose: The Player resource is always transmitted to clients
  70. //-----------------------------------------------------------------------------
  71. int CPlayerResource::UpdateTransmitState()
  72. {
  73. // ALWAYS transmit to all clients.
  74. return SetTransmitState( FL_EDICT_ALWAYS );
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Purpose: Wrapper for the virtual GrabPlayerData Think function
  78. //-----------------------------------------------------------------------------
  79. void CPlayerResource::ResourceThink( void )
  80. {
  81. m_nUpdateCounter++;
  82. UpdatePlayerData();
  83. SetNextThink( gpGlobals->curtime + 0.1f );
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. //-----------------------------------------------------------------------------
  88. void CPlayerResource::UpdatePlayerData( void )
  89. {
  90. for ( int i = 1; i <= MAX_PLAYERS; i++ )
  91. {
  92. CBasePlayer *pPlayer = (CBasePlayer*)UTIL_PlayerByIndex( i );
  93. if ( pPlayer && pPlayer->IsConnected() )
  94. {
  95. UpdateConnectedPlayer( i, pPlayer );
  96. }
  97. else
  98. {
  99. UpdateDisconnectedPlayer( i );
  100. }
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. void CPlayerResource::UpdateConnectedPlayer( int iIndex, CBasePlayer *pPlayer )
  107. {
  108. m_iScore.Set( iIndex, pPlayer->FragCount() );
  109. m_iDeaths.Set( iIndex, pPlayer->DeathCount() );
  110. m_bConnected.Set( iIndex, 1 );
  111. m_iTeam.Set( iIndex, pPlayer->GetTeamNumber() );
  112. m_bAlive.Set( iIndex, pPlayer->IsAlive()?1:0 );
  113. m_iHealth.Set( iIndex, MAX( 0, pPlayer->GetHealth() ) );
  114. m_bValid.Set( iIndex, 1 );
  115. // Don't update ping / packetloss every time
  116. if ( !(m_nUpdateCounter%20) )
  117. {
  118. // update ping all 20 think ticks = (20*0.1=2seconds)
  119. int ping, packetloss;
  120. UTIL_GetPlayerConnectionInfo( iIndex, ping, packetloss );
  121. // calc avg for scoreboard so it's not so jittery
  122. ping = 0.8f * m_iPing.Get( iIndex ) + 0.2f * ping;
  123. m_iPing.Set( iIndex, ping );
  124. // m_iPacketloss.Set( iSlot, packetloss );
  125. }
  126. CSteamID steamID;
  127. pPlayer->GetSteamID( &steamID );
  128. m_iAccountID.Set( iIndex, steamID.GetAccountID() );
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Purpose:
  132. //-----------------------------------------------------------------------------
  133. void CPlayerResource::UpdateDisconnectedPlayer( int iIndex )
  134. {
  135. m_bConnected.Set( iIndex, 0 );
  136. m_iAccountID.Set( iIndex, 0 );
  137. m_bValid.Set( iIndex, 0 );
  138. }