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.

333 lines
10 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Client side CTeam class
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "c_team.h"
  9. #include "bannedwords.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. // Purpose: RecvProxy that converts the Team's player UtlVector to entindexes
  14. //-----------------------------------------------------------------------------
  15. void RecvProxy_PlayerList( const CRecvProxyData *pData, void *pStruct, void *pOut )
  16. {
  17. C_Team *pTeam = (C_Team*)pOut;
  18. pTeam->m_aPlayers[pData->m_iElement] = pData->m_Value.m_Int;
  19. }
  20. void RecvProxyArrayLength_PlayerArray( void *pStruct, int objectID, int currentArrayLength )
  21. {
  22. C_Team *pTeam = (C_Team*)pStruct;
  23. if ( pTeam->m_aPlayers.Count() != currentArrayLength )
  24. pTeam->m_aPlayers.SetSize( currentArrayLength );
  25. }
  26. IMPLEMENT_CLIENTCLASS_DT_NOBASE(C_Team, DT_Team, CTeam)
  27. RecvPropInt( RECVINFO( m_iTeamNum ) ),
  28. RecvPropInt( RECVINFO( m_bSurrendered ) ),
  29. RecvPropInt( RECVINFO( m_scoreTotal ) ),
  30. RecvPropInt( RECVINFO( m_scoreFirstHalf ) ),
  31. RecvPropInt( RECVINFO( m_scoreSecondHalf) ),
  32. RecvPropInt( RECVINFO( m_scoreOvertime ) ),
  33. RecvPropInt( RECVINFO( m_iClanID ) ),
  34. RecvPropString( RECVINFO(m_szTeamname)),
  35. RecvPropString( RECVINFO(m_szClanTeamname)),
  36. RecvPropString( RECVINFO(m_szTeamFlagImage)),
  37. RecvPropString( RECVINFO(m_szTeamLogoImage)),
  38. RecvPropString( RECVINFO( m_szTeamMatchStat ) ),
  39. RecvPropInt( RECVINFO( m_nGGLeaderEntIndex_CT ) ),
  40. RecvPropInt( RECVINFO( m_nGGLeaderEntIndex_T ) ),
  41. RecvPropInt( RECVINFO( m_numMapVictories ) ),
  42. RecvPropArray2(
  43. RecvProxyArrayLength_PlayerArray,
  44. RecvPropInt( "player_array_element", 0, SIZEOF_IGNORE, 0, RecvProxy_PlayerList ),
  45. MAX_PLAYERS,
  46. 0,
  47. "player_array"
  48. )
  49. END_RECV_TABLE()
  50. BEGIN_PREDICTION_DATA( C_Team )
  51. DEFINE_PRED_ARRAY( m_szTeamname, FIELD_CHARACTER, MAX_TEAM_NAME_LENGTH, FTYPEDESC_PRIVATE ),
  52. DEFINE_PRED_ARRAY( m_szClanTeamname, FIELD_CHARACTER, MAX_TEAM_NAME_LENGTH, FTYPEDESC_PRIVATE ),
  53. DEFINE_PRED_ARRAY( m_szTeamFlagImage, FIELD_CHARACTER, MAX_TEAM_FLAG_ICON_LENGTH, FTYPEDESC_PRIVATE ),
  54. DEFINE_PRED_ARRAY( m_szTeamLogoImage, FIELD_CHARACTER, MAX_TEAM_LOGO_ICON_LENGTH, FTYPEDESC_PRIVATE ),
  55. DEFINE_PRED_ARRAY( m_szTeamMatchStat, FIELD_CHARACTER, MAX_PATH, FTYPEDESC_PRIVATE ),
  56. DEFINE_PRED_FIELD( m_scoreTotal, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  57. DEFINE_PRED_FIELD( m_scoreFirstHalf, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  58. DEFINE_PRED_FIELD( m_scoreSecondHalf, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  59. DEFINE_PRED_FIELD( m_scoreOvertime, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  60. DEFINE_PRED_FIELD( m_iDeaths, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  61. DEFINE_PRED_FIELD( m_iPing, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  62. DEFINE_PRED_FIELD( m_iPacketloss, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  63. DEFINE_PRED_FIELD( m_iTeamNum, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  64. DEFINE_PRED_FIELD( m_bSurrendered, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  65. DEFINE_PRED_FIELD( m_iClanID, FIELD_INTEGER, FTYPEDESC_PRIVATE ),
  66. END_PREDICTION_DATA();
  67. // Global list of client side team entities
  68. CUtlVector< C_Team * > g_Teams;
  69. //=================================================================================================
  70. // C_Team functionality
  71. //-----------------------------------------------------------------------------
  72. // Purpose:
  73. //-----------------------------------------------------------------------------
  74. C_Team::C_Team()
  75. {
  76. m_scoreTotal = 0;
  77. m_scoreFirstHalf = 0;
  78. m_scoreSecondHalf = 0;
  79. m_scoreOvertime = 0;
  80. m_iClanID = 0;
  81. memset( m_szTeamname, 0, sizeof(m_szTeamname) );
  82. memset( m_szClanTeamname, 0, sizeof(m_szClanTeamname) );
  83. memset( m_szTeamFlagImage, 0, sizeof(m_szTeamFlagImage) );
  84. memset( m_szTeamLogoImage, 0, sizeof(m_szTeamLogoImage) );
  85. memset( m_szTeamMatchStat, 0, sizeof( m_szTeamMatchStat ) );
  86. m_iDeaths = 0;
  87. m_iPing = 0;
  88. m_iPacketloss = 0;
  89. m_bSurrendered = 0;
  90. m_numMapVictories = 0;
  91. // Add myself to the global list of team entities
  92. g_Teams.AddToTail( this );
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. //-----------------------------------------------------------------------------
  97. C_Team::~C_Team()
  98. {
  99. g_Teams.FindAndRemove( this );
  100. }
  101. void C_Team::RemoveAllPlayers()
  102. {
  103. m_aPlayers.RemoveAll();
  104. }
  105. void C_Team::PreDataUpdate( DataUpdateType_t updateType )
  106. {
  107. BaseClass::PreDataUpdate( updateType );
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Gets the ith player on the team (may return NULL)
  111. //-----------------------------------------------------------------------------
  112. C_BasePlayer* C_Team::GetPlayer( int idx )
  113. {
  114. return (C_BasePlayer*)cl_entitylist->GetEnt(m_aPlayers[idx]);
  115. }
  116. int C_Team::GetTeamNumber() const
  117. {
  118. return m_iTeamNum;
  119. }
  120. //=================================================================================================
  121. // TEAM HANDLING
  122. //=================================================================================================
  123. // Purpose:
  124. //-----------------------------------------------------------------------------
  125. char *C_Team::Get_Name( void )
  126. {
  127. return m_szTeamname;
  128. }
  129. //=================================================================================================
  130. // Purpose:
  131. //-----------------------------------------------------------------------------
  132. char *C_Team::Get_ClanName( void )
  133. {
  134. if ( CDemoPlaybackParameters_t const *pParameters = engine->GetDemoPlaybackParameters() )
  135. {
  136. if ( pParameters->m_bAnonymousPlayerIdentity )
  137. return "";
  138. }
  139. g_BannedWords.CensorBannedWordsInplace( m_szClanTeamname );
  140. return m_szClanTeamname;
  141. }
  142. //=================================================================================================
  143. // Purpose:
  144. //-----------------------------------------------------------------------------
  145. char *C_Team::Get_FlagImageString( void )
  146. {
  147. if ( CDemoPlaybackParameters_t const *pParameters = engine->GetDemoPlaybackParameters() )
  148. {
  149. if ( pParameters->m_bAnonymousPlayerIdentity )
  150. return "";
  151. }
  152. return m_szTeamFlagImage;
  153. }
  154. //=================================================================================================
  155. // Purpose:
  156. //-----------------------------------------------------------------------------
  157. char *C_Team::Get_LogoImageString( void )
  158. {
  159. if ( CDemoPlaybackParameters_t const *pParameters = engine->GetDemoPlaybackParameters() )
  160. {
  161. if ( pParameters->m_bAnonymousPlayerIdentity )
  162. return "";
  163. }
  164. return m_szTeamLogoImage;
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose:
  168. //-----------------------------------------------------------------------------
  169. int C_Team::Get_Deaths( void )
  170. {
  171. return m_iDeaths;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. //-----------------------------------------------------------------------------
  176. int C_Team::Get_Ping( void )
  177. {
  178. return m_iPing;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose: Return the number of players in this team
  182. //-----------------------------------------------------------------------------
  183. int C_Team::Get_Number_Players( void )
  184. {
  185. return m_aPlayers.Count();
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose: Returns true if the specified player is on this team
  189. //-----------------------------------------------------------------------------
  190. bool C_Team::ContainsPlayer( int iPlayerIndex )
  191. {
  192. for (int i = 0; i < m_aPlayers.Count(); i++ )
  193. {
  194. if ( m_aPlayers[i] == iPlayerIndex )
  195. return true;
  196. }
  197. return false;
  198. }
  199. void C_Team::ClientThink()
  200. {
  201. }
  202. int C_Team::GetGGLeader( int nTeam )
  203. {
  204. if ( nTeam == TEAM_CT )
  205. return m_nGGLeaderEntIndex_CT;
  206. else if ( nTeam == TEAM_TERRORIST )
  207. return m_nGGLeaderEntIndex_T;
  208. return -1;
  209. }
  210. //=================================================================================================
  211. // GLOBAL CLIENT TEAM HANDLING
  212. //=================================================================================================
  213. // Purpose: Get the C_Team for the local player
  214. //-----------------------------------------------------------------------------
  215. C_Team *GetLocalTeam( void )
  216. {
  217. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  218. if ( !player )
  219. return NULL;
  220. return GetPlayersTeam( player->index );
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose: Get the C_Team for the specified team number
  224. //-----------------------------------------------------------------------------
  225. C_Team *GetGlobalTeam( int iTeamNumber )
  226. {
  227. for (int i = 0; i < g_Teams.Count(); i++ )
  228. {
  229. if ( g_Teams[i]->GetTeamNumber() == iTeamNumber )
  230. return g_Teams[i];
  231. }
  232. return NULL;
  233. }
  234. //-----------------------------------------------------------------------------
  235. // Purpose: Returns the number of teams you can access via GetGlobalTeam() (hence the +1)
  236. //-----------------------------------------------------------------------------
  237. int GetNumTeams()
  238. {
  239. return g_Teams.Count() + 1;
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Purpose: Get the team of the specified player
  243. //-----------------------------------------------------------------------------
  244. C_Team *GetPlayersTeam( int iPlayerIndex )
  245. {
  246. for (int i = 0; i < g_Teams.Count(); i++ )
  247. {
  248. if ( g_Teams[i]->ContainsPlayer( iPlayerIndex ) )
  249. return g_Teams[i];
  250. }
  251. return NULL;
  252. }
  253. //-----------------------------------------------------------------------------
  254. // Purpose: Get the team of the specified player
  255. //-----------------------------------------------------------------------------
  256. C_Team *GetPlayersTeam( C_BasePlayer *pPlayer )
  257. {
  258. return GetPlayersTeam( pPlayer->entindex() );
  259. }
  260. //-----------------------------------------------------------------------------
  261. // Purpose: Returns true if the two specified players are on the same team
  262. //-----------------------------------------------------------------------------
  263. bool ArePlayersOnSameTeam( int iPlayerIndex1, int iPlayerIndex2 )
  264. {
  265. for (int i = 0; i < g_Teams.Count(); i++ )
  266. {
  267. if ( g_Teams[i]->ContainsPlayer( iPlayerIndex1 ) && g_Teams[i]->ContainsPlayer( iPlayerIndex2 ) )
  268. return true;
  269. }
  270. return false;
  271. }
  272. //-----------------------------------------------------------------------------
  273. // Purpose: Get the number of team managers
  274. //-----------------------------------------------------------------------------
  275. int GetNumberOfTeams( void )
  276. {
  277. return g_Teams.Count();
  278. }