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.

358 lines
9.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: HUD Target ID element
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "hud.h"
  9. #include "hudelement.h"
  10. #include "c_cs_player.h"
  11. #include "c_playerresource.h"
  12. #include "vgui_EntityPanel.h"
  13. #include "iclientmode.h"
  14. #include "vgui/ILocalize.h"
  15. #include "c_cs_hostage.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. #define PLAYER_HINT_DISTANCE 150
  19. #define PLAYER_HINT_DISTANCE_SQ (PLAYER_HINT_DISTANCE*PLAYER_HINT_DISTANCE)
  20. extern CUtlVector< C_CHostage* > g_Hostages;
  21. static ConVar hud_showtargetpos( "hud_showtargetpos", "0", FCVAR_ARCHIVE, "0: center, 1: upper left, 2 upper right, 3: lower left, 4: lower right" );
  22. static ConVar hud_showtargetid( "hud_showtargetid", "1", FCVAR_ARCHIVE, "Enables display of target names" );
  23. //-----------------------------------------------------------------------------
  24. // Purpose:
  25. //-----------------------------------------------------------------------------
  26. class CTargetID : public CHudElement, public vgui::Panel
  27. {
  28. DECLARE_CLASS_SIMPLE( CTargetID, vgui::Panel );
  29. public:
  30. explicit CTargetID( const char *pElementName );
  31. void Init( void );
  32. virtual void ApplySchemeSettings( vgui::IScheme *scheme );
  33. virtual void Paint( void );
  34. void VidInit( void );
  35. private:
  36. Color GetColorForTargetTeam( int iTeamNumber );
  37. vgui::HFont m_hFont;
  38. int m_iLastEntIndex;
  39. float m_flLastChangeTime;
  40. Color m_cCTColor;
  41. Color m_cTerroristColor;
  42. Color m_cHostageColor;
  43. };
  44. // DECLARE_HUDELEMENT( CTargetID );
  45. using namespace vgui;
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. CTargetID::CTargetID( const char *pElementName ) :
  50. CHudElement( pElementName ), BaseClass( NULL, "TargetID" )
  51. {
  52. vgui::Panel *pParent = GetClientMode()->GetViewport();
  53. SetParent( pParent );
  54. m_hFont = g_hFontTrebuchet24;
  55. m_flLastChangeTime = 0;
  56. m_iLastEntIndex = 0;
  57. SetHiddenBits( HIDEHUD_MISCSTATUS );
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose: Setup
  61. //-----------------------------------------------------------------------------
  62. void CTargetID::Init( void )
  63. {
  64. };
  65. void CTargetID::ApplySchemeSettings( vgui::IScheme *scheme )
  66. {
  67. BaseClass::ApplySchemeSettings( scheme );
  68. m_cTerroristColor = scheme->GetColor( "T_Red", Color( 255, 64, 64, 255 ) );
  69. m_cCTColor = scheme->GetColor( "CT_Blue", Color( 255, 64, 64, 255 ) );
  70. m_cHostageColor = scheme->GetColor( "Hostage_yellow", Color( 255, 160, 0, 255 ) );
  71. m_hFont = scheme->GetFont( "TargetID", true );
  72. SetPaintBackgroundEnabled( false );
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: clear out string etc between levels
  76. //-----------------------------------------------------------------------------
  77. void CTargetID::VidInit()
  78. {
  79. CHudElement::VidInit();
  80. // set our size to the current viewport size
  81. SetSize( GetClientMode()->GetViewport()->GetWide(), GetClientMode()->GetViewport()->GetTall());
  82. m_flLastChangeTime = 0;
  83. m_iLastEntIndex = 0;
  84. }
  85. Color CTargetID::GetColorForTargetTeam( int iTeamNumber )
  86. {
  87. switch( iTeamNumber )
  88. {
  89. case TEAM_CT:
  90. return m_cCTColor;
  91. break;
  92. case TEAM_TERRORIST:
  93. return m_cTerroristColor;
  94. break;
  95. default:
  96. return m_cHostageColor;
  97. break;
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose: Draw function for the element
  102. //-----------------------------------------------------------------------------
  103. void CTargetID::Paint()
  104. {
  105. if ( hud_showtargetid.GetBool() == false )
  106. return;
  107. #define MAX_ID_STRING 256
  108. wchar_t sIDString[ MAX_ID_STRING ];
  109. sIDString[0] = 0;
  110. Color c = m_cHostageColor;
  111. C_CSPlayer *pPlayer = C_CSPlayer::GetLocalCSPlayer();
  112. if ( !pPlayer )
  113. return;
  114. // don't show target IDs when flashed
  115. if ( pPlayer->m_flFlashBangTime > (gpGlobals->curtime+0.5) )
  116. return;
  117. // [menglish] Don't show target ID's when in freezecam mode
  118. if ( pPlayer->GetObserverMode() == OBS_MODE_FREEZECAM )
  119. {
  120. return;
  121. }
  122. // Get our target's ent index
  123. int iEntIndex = pPlayer->GetIDTarget();
  124. // Didn't find one?
  125. if ( !iEntIndex )
  126. {
  127. // Check to see if we should clear our ID
  128. if ( m_flLastChangeTime && (gpGlobals->curtime > (m_flLastChangeTime + 0.5)) )
  129. {
  130. m_flLastChangeTime = 0;
  131. sIDString[0] = 0;
  132. m_iLastEntIndex = 0;
  133. }
  134. else
  135. {
  136. // Keep re-using the old one
  137. iEntIndex = m_iLastEntIndex;
  138. }
  139. }
  140. else
  141. {
  142. m_flLastChangeTime = gpGlobals->curtime;
  143. }
  144. // Is this an entindex sent by the server?
  145. if ( iEntIndex )
  146. {
  147. C_BasePlayer *pPlayer = static_cast<C_BasePlayer*>(cl_entitylist->GetEnt( iEntIndex ));
  148. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  149. const char *printFormatString = NULL;
  150. wchar_t wszPlayerName[ MAX_PLAYER_NAME_LENGTH ];
  151. wchar_t wszHealthText[ 10 ];
  152. bool bShowHealth = false;
  153. bool bShowPlayerName = false;
  154. // Some entities we always want to check, cause the text may change
  155. // even while we're looking at it
  156. // Is it a player?
  157. if ( IsPlayerIndex( iEntIndex ) )
  158. {
  159. if ( !pPlayer )
  160. {
  161. // This can happen because the object was destroyed
  162. sIDString[0] = 0;
  163. m_iLastEntIndex = 0;
  164. }
  165. else
  166. {
  167. c = GetColorForTargetTeam( pPlayer->GetTeamNumber() );
  168. bShowPlayerName = true;
  169. g_pVGuiLocalize->ConvertANSIToUnicode( pPlayer->GetPlayerName(), wszPlayerName, sizeof(wszPlayerName) );
  170. if ( pPlayer->InSameTeam(pLocalPlayer) )
  171. {
  172. printFormatString = "#Cstrike_playerid_sameteam";
  173. bShowHealth = true;
  174. }
  175. else if ( pLocalPlayer->GetTeamNumber() != TEAM_CT && pLocalPlayer->GetTeamNumber() != TEAM_TERRORIST )
  176. {
  177. printFormatString = "#Cstrike_playerid_noteam";
  178. bShowHealth = true;
  179. }
  180. else
  181. {
  182. printFormatString = "#Cstrike_playerid_diffteam";
  183. }
  184. if ( bShowHealth )
  185. {
  186. _snwprintf( wszHealthText, ARRAYSIZE(wszHealthText) - 1, L"%.0f%%", ((float)pPlayer->GetHealth() / (float)pPlayer->GetMaxHealth() ) * 100 );
  187. wszHealthText[ ARRAYSIZE(wszHealthText)-1 ] = '\0';
  188. }
  189. }
  190. }
  191. else
  192. {
  193. C_BaseEntity *pEnt = cl_entitylist->GetEnt( iEntIndex );
  194. //Hostages!
  195. //"Hostage : Health 100%"
  196. /*
  197. if( long range )
  198. {
  199. m_flDisplayHistory |= DHF_HOSTAGE_SEEN_FAR;
  200. switch ( pLocalPlayer->GetTeamNumber() )
  201. {
  202. case TERRORIST:
  203. HintMessage( "#Hint_prevent_hostage_rescue", TRUE );
  204. break;
  205. case CT:
  206. HintMessage( "#Hint_rescue_the_hostages", TRUE );
  207. break;
  208. }
  209. }
  210. else
  211. {
  212. m_flDisplayHistory |= DHF_HOSTAGE_SEEN_NEAR;
  213. m_flDisplayHistory |= DHF_HOSTAGE_SEEN_FAR; // Don't want the other msg to appear now
  214. HintMessage( "#Hint_press_use_so_hostage_will_follow", FALSE );
  215. }
  216. */
  217. C_CHostage *pHostage = NULL;
  218. for( int i=0;i<g_Hostages.Count();i++ )
  219. {
  220. // compare entity pointers
  221. if( g_Hostages[i] == pEnt )
  222. {
  223. pHostage = g_Hostages[i];
  224. break;
  225. }
  226. }
  227. if( pHostage != NULL )
  228. {
  229. c = m_cHostageColor;
  230. printFormatString = "#Cstrike_playerid_hostage";
  231. _snwprintf( wszHealthText, ARRAYSIZE(wszHealthText) - 1, L"%.0f%%", ((float)pHostage->GetHealth() / (float)pHostage->GetMaxHealth() ) * 100 );
  232. wszHealthText[ ARRAYSIZE(wszHealthText)-1 ] = '\0';
  233. bShowHealth = true;
  234. }
  235. else if ( !pEnt || !pEnt->InSameTeam(pLocalPlayer) )
  236. {
  237. // This can happen because the object was destroyed
  238. sIDString[0] = 0;
  239. m_iLastEntIndex = 0;
  240. }
  241. else
  242. {
  243. // Don't check validity if it's sent by the server
  244. c = m_cHostageColor;
  245. g_pVGuiLocalize->ConvertANSIToUnicode( pEnt->GetIDString(), sIDString, sizeof(sIDString) );
  246. m_iLastEntIndex = iEntIndex;
  247. }
  248. }
  249. if ( printFormatString )
  250. {
  251. if ( bShowPlayerName && bShowHealth )
  252. {
  253. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 2, wszPlayerName, wszHealthText );
  254. }
  255. else if ( bShowPlayerName )
  256. {
  257. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 1, wszPlayerName );
  258. }
  259. else if ( bShowHealth )
  260. {
  261. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 1, wszHealthText );
  262. }
  263. else
  264. {
  265. g_pVGuiLocalize->ConstructString( sIDString, sizeof(sIDString), g_pVGuiLocalize->Find(printFormatString), 0 );
  266. }
  267. }
  268. if ( sIDString[0] )
  269. {
  270. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  271. bool bObserverMode = pPlayer && pPlayer->IsObserver();
  272. int wide, tall;
  273. vgui::surface()->GetTextSize( m_hFont, sIDString, wide, tall );
  274. int ypos;
  275. int xpos;
  276. switch ( hud_showtargetpos.GetInt() )
  277. {
  278. case 0: // center
  279. default:
  280. xpos = (ScreenWidth() - wide) / 2;
  281. ypos = YRES(260) - tall / 2;
  282. break;
  283. case 1: // upper left
  284. xpos = XRES(10);
  285. ypos = bObserverMode ? YRES(55) : YRES(5);
  286. break;
  287. case 2: // upper right
  288. xpos = XRES(630) - wide;
  289. ypos = bObserverMode ? YRES(55) : YRES(5);
  290. break;
  291. case 3: // lower left
  292. xpos = XRES(10);
  293. ypos = bObserverMode ? YRES(415) : YRES(445) - tall;
  294. break;
  295. case 4: // lower right
  296. xpos = XRES(630) - wide;
  297. ypos = bObserverMode ? YRES(415) : YRES(410) - tall;
  298. break;
  299. }
  300. vgui::surface()->DrawSetTextFont( m_hFont );
  301. vgui::surface()->DrawSetTextPos( xpos, ypos );
  302. vgui::surface()->DrawSetTextColor( c );
  303. vgui::surface()->DrawPrintText( sIDString, wcslen(sIDString) );
  304. }
  305. }
  306. }