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.

324 lines
9.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "cs_hud_chat.h"
  8. #include "c_cs_player.h"
  9. #include "c_cs_playerresource.h"
  10. #include "hud_macros.h"
  11. #include "text_message.h"
  12. #include "vguicenterprint.h"
  13. #include "vgui/ILocalize.h"
  14. #include "engine/IEngineSound.h"
  15. #include "radio_status.h"
  16. #include "bot/shared_util.h"
  17. #include "ihudlcd.h"
  18. #include "voice_status.h"
  19. #include <vgui/IScheme.h>
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. // [hpe:jason] Do not disable this element! It needs to be around to forward various HudChat messages to the new Scaleform UI
  23. DECLARE_HUDELEMENT_FLAGS( CHudChat, HUDELEMENT_SS_FULLSCREEN_ONLY );
  24. //=====================
  25. //CHudChatLine
  26. //=====================
  27. CHudChatLine::CHudChatLine( vgui::Panel *parent, const char *panelName ) : CBaseHudChatLine( parent, panelName )
  28. {
  29. m_text = NULL;
  30. }
  31. void CHudChatLine::ApplySchemeSettings(vgui::IScheme *pScheme)
  32. {
  33. BaseClass::ApplySchemeSettings( pScheme );
  34. }
  35. //=====================
  36. //CHudChatInputLine
  37. //=====================
  38. void CHudChatInputLine::ApplySchemeSettings(vgui::IScheme *pScheme)
  39. {
  40. BaseClass::ApplySchemeSettings(pScheme);
  41. vgui::HFont hFont = pScheme->GetFont( "ChatFont" );
  42. m_pPrompt->SetFont( hFont );
  43. m_pInput->SetFont( hFont );
  44. m_pInput->SetFgColor( pScheme->GetColor( "Chat.TypingText", pScheme->GetColor( "Panel.FgColor", Color( 255, 255, 255, 255 ) ) ) );
  45. }
  46. //=====================
  47. //CHudChat
  48. //=====================
  49. CHudChat::CHudChat( const char *pElementName ) : BaseClass( pElementName )
  50. {
  51. }
  52. void CHudChat::CreateChatInputLine( void )
  53. {
  54. m_pChatInput = new CHudChatInputLine( this, "ChatInputLine" );
  55. m_pChatInput->SetVisible( false );
  56. }
  57. void CHudChat::CreateChatLines( void )
  58. {
  59. m_ChatLine = new CHudChatLine( this, "ChatLine1" );
  60. m_ChatLine->SetVisible( false );
  61. }
  62. void CHudChat::Init( void )
  63. {
  64. BaseClass::Init();
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Overrides base reset to not cancel chat at round restart
  68. //-----------------------------------------------------------------------------
  69. void CHudChat::Reset( void )
  70. {
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose: Reads in a player's Radio text from the server
  74. //-----------------------------------------------------------------------------
  75. bool CHudChat::MsgFunc_RadioText( const CCSUsrMsg_RadioText &msg )
  76. {
  77. int client = msg.client();
  78. wchar_t szBuf[6][128];
  79. wchar_t *msg_text = ReadLocalizedString( msg.msg_name().c_str(), szBuf[0], sizeof( szBuf[0] ), false );
  80. // keep reading strings and using C format strings for subsituting the strings into the localised text string
  81. ReadChatTextString ( msg.params(0).c_str(), szBuf[1], sizeof( szBuf[1] ) ); // player name
  82. ReadLocalizedString( msg.params(1).c_str(), szBuf[2], sizeof( szBuf[2] ), true ); // location
  83. ReadLocalizedString( msg.params(2).c_str(), szBuf[3], sizeof( szBuf[3] ), true ); // radio text
  84. ReadLocalizedString( msg.params(3).c_str(), szBuf[4], sizeof( szBuf[4] ), true ); // unused :(
  85. if ( V_strcmp( msg.params( 3 ).c_str(), "auto" ) != 0 && ( GetClientVoiceMgr()->IsPlayerBlocked( client ) || GetClientVoiceMgr()->ShouldHideCommunicationFromPlayer( client ) ) )
  86. return false;
  87. g_pVGuiLocalize->ConstructString( szBuf[5], sizeof( szBuf[5] ), msg_text, 4, szBuf[1], szBuf[2], szBuf[3], szBuf[4] );
  88. char ansiString[512];
  89. g_pVGuiLocalize->ConvertUnicodeToANSI( ConvertCRtoNL( szBuf[5] ), ansiString, sizeof( ansiString ) );
  90. ChatPrintf( client, CHAT_FILTER_TEAMCHANGE, "%s", ansiString );
  91. CLocalPlayerFilter filter;
  92. C_BaseEntity::EmitSound( filter, SOUND_FROM_LOCAL_PLAYER, "HudChat.Message" );
  93. return true;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose: Reads in a player's Chat text from the server
  97. //-----------------------------------------------------------------------------
  98. bool CHudChat::MsgFunc_SayText2( const CCSUsrMsg_SayText2 &msg )
  99. {
  100. // Got message during connection
  101. if ( !GetCSResources() )
  102. return true;
  103. if ( CDemoPlaybackParameters_t const *pParameters = engine->GetDemoPlaybackParameters() )
  104. {
  105. if ( pParameters->m_bAnonymousPlayerIdentity )
  106. return true; // cannot print potentially personal details
  107. }
  108. int client = msg.ent_idx();
  109. bool bWantsToChat = msg.chat() != 0;
  110. wchar_t szBuf[6][256];
  111. char untranslated_msg_text[256];
  112. wchar_t *msg_text = ReadLocalizedString( msg.msg_name().c_str(), szBuf[0], sizeof( szBuf[0] ), false, untranslated_msg_text, sizeof( untranslated_msg_text ) );
  113. // keep reading strings and using C format strings for subsituting the strings into the localised text string
  114. ReadChatTextString ( msg.params(0).c_str(), szBuf[1], sizeof( szBuf[1] ) ); // player name
  115. ReadChatTextString ( msg.params(1).c_str(), szBuf[2], sizeof( szBuf[2] ), true ); // location
  116. ReadLocalizedString( msg.params(2).c_str(), szBuf[3], sizeof( szBuf[3] ), true ); // radio text
  117. ReadLocalizedString( msg.params(3).c_str(), szBuf[4], sizeof( szBuf[4] ), true ); // unused :(
  118. if ( V_strcmp( msg.params( 3 ).c_str(), "auto" ) != 0 && ( GetClientVoiceMgr()->IsPlayerBlocked( client ) || GetClientVoiceMgr()->ShouldHideCommunicationFromPlayer( client ) ) )
  119. bWantsToChat = false;
  120. CEconQuestDefinition *pQuestDef = CSGameRules()->GetActiveAssassinationQuest();
  121. if ( pQuestDef && GetCSResources()->IsAssassinationTarget( client ) )
  122. {
  123. extern const char* Helper_GetLocalPlayerAssassinationQuestLocToken( const CEconQuestDefinition *pQuest );
  124. if ( const char* szToken = Helper_GetLocalPlayerAssassinationQuestLocToken( pQuestDef ) )
  125. V_wcscpy_safe( szBuf[ 1 ], g_pVGuiLocalize->Find( szToken ) );
  126. }
  127. g_pVGuiLocalize->ConstructString( szBuf[5], sizeof( szBuf[5] ), msg_text, 4, szBuf[1], szBuf[2], szBuf[3], szBuf[4] );
  128. char ansiString[512];
  129. g_pVGuiLocalize->ConvertUnicodeToANSI( ConvertCRtoNL( szBuf[5] ), ansiString, sizeof( ansiString ) );
  130. if ( bWantsToChat )
  131. {
  132. int iFilter = CHAT_FILTER_NONE;
  133. bool playChatSound = true;
  134. if ( client > 0 && g_PR && (g_PR->GetTeam( client ) != g_PR->GetTeam( GetLocalPlayerIndex() )) )
  135. {
  136. iFilter = CHAT_FILTER_PUBLICCHAT;
  137. if ( !( iFilter & GetFilterFlags() ) )
  138. {
  139. playChatSound = false;
  140. }
  141. }
  142. // print raw chat text
  143. ChatPrintf( client, iFilter, "%s", ansiString );
  144. Msg( "%s\n", RemoveColorMarkup(ansiString) );
  145. if ( playChatSound )
  146. {
  147. CLocalPlayerFilter filter;
  148. C_BaseEntity::EmitSound( filter, SOUND_FROM_LOCAL_PLAYER, "HudChat.Message" );
  149. }
  150. }
  151. else
  152. {
  153. if ( !GetClientVoiceMgr()->IsPlayerBlocked( client ) && !GetClientVoiceMgr()->ShouldHideCommunicationFromPlayer( client ) )
  154. {
  155. // print raw chat text
  156. ChatPrintf( client, GetFilterForString( untranslated_msg_text), "%s", ansiString );
  157. }
  158. }
  159. return true;
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Purpose:
  163. //-----------------------------------------------------------------------------
  164. int CHudChat::GetChatInputOffset( void )
  165. {
  166. if ( m_pChatInput->IsVisible() )
  167. {
  168. return m_iFontHeight;
  169. }
  170. else
  171. {
  172. return 0;
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Purpose: Reads in an Audio message from the server (wav file to be played
  177. // via the player's voice, i.e. for bot chatter)
  178. //-----------------------------------------------------------------------------
  179. bool CHudChat::MsgFunc_RawAudio( const CCSUsrMsg_RawAudio &msg )
  180. {
  181. int pitch = msg.pitch();
  182. int playerIndex = msg.entidx();
  183. float feedbackDuration = msg.duration();
  184. const char *szString = msg.voice_filename().c_str();
  185. EmitSound_t ep;
  186. ep.m_nChannel = CHAN_VOICE;
  187. ep.m_pSoundName = szString;
  188. ep.m_flVolume = 1.0f;
  189. ep.m_SoundLevel = SNDLVL_NORM;
  190. ep.m_nPitch = pitch;
  191. CLocalPlayerFilter filter;
  192. C_BaseEntity::EmitSound( filter, SOUND_FROM_LOCAL_PLAYER, ep );
  193. if ( feedbackDuration > 0.0f )
  194. {
  195. //Flash them on the radar
  196. C_CSPlayer *pPlayer = static_cast<C_CSPlayer*>( cl_entitylist->GetEnt(playerIndex) );
  197. if ( pPlayer )
  198. {
  199. // Create the flashy above player's head
  200. RadioManager()->UpdateVoiceStatus( playerIndex, feedbackDuration );
  201. }
  202. }
  203. return true;
  204. }
  205. //-----------------------------------------------------------------------------
  206. Color CHudChat::GetClientColor( int clientIndex )
  207. {
  208. if ( clientIndex == 0 ) // console msg
  209. {
  210. return g_ColorGreen;
  211. }
  212. else if( g_PR )
  213. {
  214. switch ( g_PR->GetTeam( clientIndex ) )
  215. {
  216. case 2 : return g_ColorRed;
  217. case 3 : return g_ColorBlue;
  218. default : return g_ColorGrey;
  219. }
  220. }
  221. return g_ColorYellow;
  222. }
  223. //-----------------------------------------------------------------------------
  224. // Purpose:
  225. //-----------------------------------------------------------------------------
  226. Color CHudChat::GetTextColorForClient( TextColor colorNum, int clientIndex )
  227. {
  228. Color c;
  229. switch ( colorNum )
  230. {
  231. case COLOR_PLAYERNAME:
  232. c = GetClientColor( clientIndex );
  233. break;
  234. case COLOR_LOCATION:
  235. c = g_ColorDarkGreen;
  236. break;
  237. case COLOR_ACHIEVEMENT:
  238. {
  239. vgui::IScheme *pSourceScheme = vgui::scheme()->GetIScheme( vgui::scheme()->GetScheme( "SourceScheme" ) );
  240. if ( pSourceScheme )
  241. {
  242. c = pSourceScheme->GetColor( "SteamLightGreen", GetBgColor() );
  243. }
  244. else
  245. {
  246. c = g_ColorGrey;
  247. }
  248. }
  249. break;
  250. default:
  251. c = g_ColorYellow;
  252. }
  253. return Color( c[0], c[1], c[2], 255 );
  254. }
  255. int CHudChat::GetFilterForString( const char *pString )
  256. {
  257. int iFilter = BaseClass::GetFilterForString( pString );
  258. if ( iFilter == CHAT_FILTER_NONE )
  259. {
  260. if ( !Q_stricmp( pString, "#CStrike_Name_Change" ) )
  261. {
  262. return CHAT_FILTER_NAMECHANGE;
  263. }
  264. }
  265. return iFilter;
  266. }