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.

261 lines
7.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Client side C_TFTeam class
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "cbase.h"
  8. #include "engine/IEngineSound.h"
  9. #include "hud.h"
  10. #include "recvproxy.h"
  11. #include "c_tf_team.h"
  12. #include "c_tf_player.h"
  13. #include "tf_shareddefs.h"
  14. #include "tf_gamerules.h"
  15. #include "c_tf_playerresource.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. //-----------------------------------------------------------------------------
  19. // Purpose: RecvProxy that converts the Player's object UtlVector to entindexes
  20. //-----------------------------------------------------------------------------
  21. void RecvProxy_TeamObjectList( const CRecvProxyData *pData, void *pStruct, void *pOut )
  22. {
  23. C_TFTeam *pPlayer = (C_TFTeam*)pStruct;
  24. CBaseHandle *pHandle = (CBaseHandle*)(&(pPlayer->m_aObjects[pData->m_iElement]));
  25. RecvProxy_IntToEHandle( pData, pStruct, pHandle );
  26. }
  27. void RecvProxyArrayLength_TeamObjects( void *pStruct, int objectID, int currentArrayLength )
  28. {
  29. C_TFTeam *pPlayer = (C_TFTeam*)pStruct;
  30. if ( pPlayer->m_aObjects.Count() != currentArrayLength )
  31. {
  32. pPlayer->m_aObjects.SetSize( currentArrayLength );
  33. }
  34. }
  35. IMPLEMENT_CLIENTCLASS_DT( C_TFTeam, DT_TFTeam, CTFTeam )
  36. RecvPropInt( RECVINFO( m_nFlagCaptures ) ),
  37. RecvPropInt( RECVINFO( m_iRole ) ),
  38. RecvPropArray2(
  39. RecvProxyArrayLength_TeamObjects,
  40. RecvPropInt( "team_object_array_element", 0, SIZEOF_IGNORE, 0, RecvProxy_TeamObjectList ),
  41. MAX_PLAYERS * MAX_OBJECTS_PER_PLAYER,
  42. 0,
  43. "team_object_array" ),
  44. RecvPropEHandle( RECVINFO( m_hLeader ) ),
  45. END_RECV_TABLE()
  46. #define TEAM_THINK_RATE 0.5f
  47. //-----------------------------------------------------------------------------
  48. // Purpose:
  49. //-----------------------------------------------------------------------------
  50. C_TFTeam::C_TFTeam()
  51. {
  52. m_nFlagCaptures = 0;
  53. m_bUsingCustomTeamName = false;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Purpose:
  57. //-----------------------------------------------------------------------------
  58. C_TFTeam::~C_TFTeam()
  59. {
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. void C_TFTeam::OnDataChanged( DataUpdateType_t updateType )
  65. {
  66. BaseClass::OnDataChanged( updateType );
  67. if ( updateType == DATA_UPDATE_CREATED )
  68. {
  69. SetNextClientThink( gpGlobals->curtime + TEAM_THINK_RATE );
  70. }
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose:
  74. //-----------------------------------------------------------------------------
  75. char* C_TFTeam::Get_Name( void )
  76. {
  77. // Use Get_Localized_Name() instead
  78. AssertMsg( false, "Use Get_Localized_Name() instead" );
  79. return "";
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Purpose:
  83. //-----------------------------------------------------------------------------
  84. void C_TFTeam::ClientThink()
  85. {
  86. BaseClass::ClientThink();
  87. UpdateTeamName();
  88. SetNextClientThink( gpGlobals->curtime + TEAM_THINK_RATE );
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose:
  92. //-----------------------------------------------------------------------------
  93. void C_TFTeam::UpdateTeamName( void )
  94. {
  95. m_bUsingCustomTeamName = false;
  96. const wchar_t *pwzName = NULL;
  97. if ( TFGameRules() && TFGameRules()->IsInTournamentMode() && ( ( m_iTeamNum == TF_TEAM_RED ) || ( m_iTeamNum == TF_TEAM_BLUE ) ) )
  98. {
  99. if ( TFGameRules()->IsCompetitiveMode() )
  100. {
  101. if ( g_TF_PR && ( g_TF_PR->HasPremadeParties() || g_TF_PR->GetEventTeamStatus() ) )
  102. {
  103. wchar_t wszTempName[MAX_TEAM_NAME_LENGTH];
  104. wchar_t *pFormat = g_pVGuiLocalize->Find( "#TF_Team_PartyLeader" );
  105. if ( !pFormat )
  106. {
  107. pFormat = L"%s";
  108. }
  109. if ( g_TF_PR->GetEventTeamStatus() )
  110. {
  111. // GetEventTeamStatus() returns a value in the following range
  112. // enum WarMatch
  113. // {
  114. // NOPE = 0;
  115. // INVADERS_ARE_PYRO = 1;
  116. // INVADERS_ARE_HEAVY = 2;
  117. // };
  118. const char *pszTeamName = ( m_iTeamNum == TF_TEAM_BLUE ) ?
  119. ( g_TF_PR->GetEventTeamStatus() == 1 ? "#TF_Pyro" : "#TF_HWGuy" ) :
  120. ( g_TF_PR->GetEventTeamStatus() == 1 ? "#TF_HWGuy" : "#TF_Pyro" );
  121. wchar_t *pwzWarTeam = g_pVGuiLocalize->Find( pszTeamName );
  122. V_swprintf_safe( m_wzTeamname, pFormat, pwzWarTeam );
  123. m_bUsingCustomTeamName = true;
  124. return;
  125. }
  126. else
  127. {
  128. int iPlayerIndex = ( m_iTeamNum == TF_TEAM_RED ) ? g_TF_PR->GetPartyLeaderRedTeamIndex() : g_TF_PR->GetPartyLeaderBlueTeamIndex();
  129. if ( g_TF_PR->IsConnected( iPlayerIndex ) )
  130. {
  131. g_pVGuiLocalize->ConvertANSIToUnicode( UTIL_SafeName( g_TF_PR->GetPlayerName( iPlayerIndex ) ), wszTempName, sizeof( wszTempName ) );
  132. V_swprintf_safe( m_wzTeamname, pFormat, wszTempName );
  133. m_bUsingCustomTeamName = true;
  134. return;
  135. }
  136. }
  137. }
  138. }
  139. else
  140. {
  141. const char *pTemp = ( m_iTeamNum == TF_TEAM_BLUE ) ? mp_tournament_blueteamname.GetString() : mp_tournament_redteamname.GetString();
  142. if ( pTemp && pTemp[0] )
  143. {
  144. g_pVGuiLocalize->ConvertANSIToUnicode( pTemp, m_wzTeamname, sizeof( m_wzTeamname ) );
  145. return;
  146. }
  147. }
  148. }
  149. if ( m_iTeamNum == TF_TEAM_BLUE )
  150. {
  151. pwzName = g_pVGuiLocalize->Find( "#TF_BlueTeam_Name" );
  152. if ( !pwzName )
  153. {
  154. pwzName = L"BLU";
  155. }
  156. }
  157. else if ( m_iTeamNum == TF_TEAM_RED )
  158. {
  159. if ( TFGameRules() && TFGameRules()->IsMannVsMachineMode() )
  160. {
  161. pwzName = g_pVGuiLocalize->Find( "#TF_Defenders" );
  162. if ( !pwzName )
  163. {
  164. pwzName = L"DEFENDERS";
  165. }
  166. }
  167. else
  168. {
  169. pwzName = g_pVGuiLocalize->Find( "#TF_RedTeam_Name" );
  170. if ( !pwzName )
  171. {
  172. pwzName = L"RED";
  173. }
  174. }
  175. }
  176. else if ( m_iTeamNum == TEAM_SPECTATOR )
  177. {
  178. pwzName = g_pVGuiLocalize->Find( "#TF_Spectators" );
  179. if ( !pwzName )
  180. {
  181. pwzName = L"SPECTATORS";
  182. }
  183. }
  184. V_wcscpy_safe( m_wzTeamname, pwzName ? pwzName : L"" );
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Get the C_TFTeam for the specified team number
  188. //-----------------------------------------------------------------------------
  189. C_TFTeam *GetGlobalTFTeam( int iTeamNumber )
  190. {
  191. for ( int i = 0; i < g_Teams.Count(); i++ )
  192. {
  193. if ( g_Teams[i]->GetTeamNumber() == iTeamNumber )
  194. return ( dynamic_cast< C_TFTeam* >( g_Teams[i] ) );
  195. }
  196. return NULL;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Purpose:
  200. //-----------------------------------------------------------------------------
  201. int C_TFTeam::GetNumObjects( int iObjectType )
  202. {
  203. // Asking for a count of a specific object type?
  204. if ( iObjectType > 0 )
  205. {
  206. int iCount = 0;
  207. for ( int i = 0; i < GetNumObjects(); i++ )
  208. {
  209. CBaseObject *pObject = GetObject(i);
  210. if ( pObject && pObject->GetType() == iObjectType )
  211. {
  212. iCount++;
  213. }
  214. }
  215. return iCount;
  216. }
  217. return m_aObjects.Count();
  218. }
  219. //-----------------------------------------------------------------------------
  220. // Purpose:
  221. //-----------------------------------------------------------------------------
  222. CBaseObject *C_TFTeam::GetObject( int num )
  223. {
  224. Assert( num >= 0 && num < m_aObjects.Count() );
  225. return m_aObjects[ num ];
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Purpose:
  229. //-----------------------------------------------------------------------------
  230. C_BasePlayer *C_TFTeam::GetTeamLeader( void )
  231. {
  232. return m_hLeader.Get();
  233. }