summaryrefslogtreecommitdiff
path: root/game/client/tf/tf_hud_spectator_extras.cpp
blob: 9e57814004ae1a976ee305d0413f1e173b20d6f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "hud.h"
#include "c_team.h"
#include "tf_shareddefs.h"
#include "tf_gamerules.h"
#include "iclientmode.h"
#include "c_playerresource.h"
#include "c_tf_playerresource.h"
#include "tf_hud_target_id.h"
#include "c_baseobject.h"
#include "tf_hud_spectator_extras.h"

#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>

using namespace vgui;

ConVar tf_spec_xray_disable( "tf_spec_xray_disable", "0", FCVAR_ARCHIVE, "Disable the spectator xray mode." );
ConVar tf_enable_glows_after_respawn( "tf_enable_glows_after_respawn", "1", FCVAR_ARCHIVE, "Enable teammate glow effects after respawn." );

DECLARE_HUDELEMENT( CTFHudSpectatorExtras );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFHudSpectatorExtras::CTFHudSpectatorExtras( const char *pszElementName ) : CHudElement( pszElementName ), EditablePanel( NULL, "HudSpectatorExtras" )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetHiddenBits( 0 );

	vgui::ivgui()->AddTickSignal( GetVPanel(), 100 );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFHudSpectatorExtras::ShouldDraw( void )
{
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudSpectatorExtras::Reset( void )
{
	if ( !g_PR )
		return;

	FOR_EACH_VEC( m_vecEntitiesToDraw, i )
	{
		int nEntIndex = m_vecEntitiesToDraw[i].m_nEntIndex;
		if ( IsPlayerIndex( nEntIndex ) && !g_PR->IsConnected( nEntIndex ) )
			continue;

		CBaseCombatCharacter *pEnt = dynamic_cast< CBaseCombatCharacter* >( cl_entitylist->GetEnt( nEntIndex ) );
		if ( !pEnt )
			continue;

		if ( pEnt->IsClientSideGlowEnabled() )
		{
			pEnt->SetClientSideGlowEnabled( false );
		}
	}

	m_vecEntitiesToDraw.Purge();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudSpectatorExtras::RemoveEntity( int nRemove )
{
	FOR_EACH_VEC( m_vecEntitiesToDraw, i )
	{
		if ( m_vecEntitiesToDraw[i].m_nEntIndex == nRemove )
		{
			m_vecEntitiesToDraw.Remove( i );
			return;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudSpectatorExtras::OnTick()
{
	BaseClass::OnTick();

	if ( !g_PR )
		return;

	if ( TFGameRules() && TFGameRules()->ShowMatchSummary() )
	{
		Reset();
		return;
	}

	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !pLocalPlayer )
		return;

	int nLocalPlayerTeam = pLocalPlayer->GetTeamNumber();
	bool bIsHLTV = engine->IsHLTV();

	if ( tf_spec_xray_disable.GetBool() || ( !bIsHLTV && ( nLocalPlayerTeam < TEAM_SPECTATOR ) ) )
	{
		Reset();
		return;
	}

	if ( nLocalPlayerTeam >= FIRST_GAME_TEAM )
	{
		if ( pLocalPlayer->IsAlive() && !pLocalPlayer->m_Shared.InCond( TF_COND_TEAM_GLOWS ) )
		{
			if ( m_vecEntitiesToDraw.Count() > 0 )
			{
				Reset();
			}
			return;
		}
	}

	if ( bIsHLTV || 
		( tf_spec_xray.GetBool() && ( ( nLocalPlayerTeam == TEAM_SPECTATOR ) || ( pLocalPlayer->GetObserverMode() > OBS_MODE_FREEZECAM ) || ( pLocalPlayer->m_Shared.InCond( TF_COND_TEAM_GLOWS ) && tf_enable_glows_after_respawn.GetBool() ) ) ) )
	{
		bool bShowEveryone = ( bIsHLTV || 
							   ( ( nLocalPlayerTeam == TEAM_SPECTATOR ) && tf_spec_xray.GetBool() ) ||
							   ( ( nLocalPlayerTeam >= FIRST_GAME_TEAM ) && ( pLocalPlayer->GetObserverMode() > OBS_MODE_FREEZECAM ) && ( tf_spec_xray.GetInt() > 1 ) ) );

		// loop through the players
		for ( int i = 1; i <= gpGlobals->maxClients; i++ )
		{
			if ( !g_PR->IsConnected( i ) )
			{
				RemoveEntity( i );
				continue;
			}

			CTFPlayer *pPlayer = ToTFPlayer( UTIL_PlayerByIndex( i ) );
			if ( !pPlayer || ( pPlayer == pLocalPlayer ) )
			{
				RemoveEntity( i );
				continue;
			}

			int nPlayerTeamNumber = pPlayer->GetTeamNumber();
			
			// remove the entities we don't want to draw anymore
			if ( pPlayer->IsDormant() ||
				( nPlayerTeamNumber < FIRST_GAME_TEAM ) ||
				( !pPlayer->IsAlive() ) ||
				( pPlayer->m_Shared.IsStealthed() && ( nLocalPlayerTeam >= FIRST_GAME_TEAM ) && ( nPlayerTeamNumber != nLocalPlayerTeam ) ) ||
				( !bShowEveryone && !pPlayer->IsPlayerClass( TF_CLASS_SPY ) && ( nPlayerTeamNumber != nLocalPlayerTeam ) ) ||
				( !bShowEveryone && pPlayer->IsPlayerClass( TF_CLASS_SPY ) && !pPlayer->m_Shared.InCond( TF_COND_DISGUISED ) && ( nPlayerTeamNumber != nLocalPlayerTeam ) ) ||
				( !bShowEveryone && pPlayer->IsPlayerClass( TF_CLASS_SPY ) && pPlayer->m_Shared.InCond( TF_COND_DISGUISED ) && ( nPlayerTeamNumber != nLocalPlayerTeam ) && ( pPlayer->m_Shared.GetDisguiseTeam() != nLocalPlayerTeam ) ) )
			{
				if ( pPlayer->IsClientSideGlowEnabled() )
				{
					pPlayer->SetClientSideGlowEnabled( false );
				}
				RemoveEntity( i );
				continue;
			}

			// passed all of the tests, so make sure they're in the list
			int nVecIndex = -1;
			FOR_EACH_VEC( m_vecEntitiesToDraw, nTemp )
			{
				if ( m_vecEntitiesToDraw[nTemp].m_nEntIndex == i )
				{
					nVecIndex = nTemp;
					break;
				}
			}
			if ( nVecIndex == -1 )
			{
				nVecIndex = m_vecEntitiesToDraw.AddToTail();
			}

			// set the player index
			m_vecEntitiesToDraw[nVecIndex].m_nEntIndex = i;

			// don't draw their name if we're currently spectating them, but we still want them to glow
			m_vecEntitiesToDraw[nVecIndex].m_bDrawName = true;
			if ( !pLocalPlayer->IsAlive() )
			{
				CSpectatorTargetID *pSpecTargetID = (CSpectatorTargetID *)GET_HUDELEMENT( CSpectatorTargetID );
				if ( ( pLocalPlayer->GetObserverTarget() == pPlayer ) || ( pSpecTargetID && pSpecTargetID->GetTargetIndex() == i ) )
				{
					m_vecEntitiesToDraw[nVecIndex].m_bDrawName = false;

					// if we're in chase mode, just remove them entirely
					if ( pLocalPlayer->GetObserverMode() == OBS_MODE_CHASE )
					{
						if ( pPlayer->IsClientSideGlowEnabled() )
						{
							pPlayer->SetClientSideGlowEnabled( false );
						}
						RemoveEntity( i );
						continue;
					}
				}
			}

			// disguised Spy?
			C_TFPlayer *pDisguiseTarget = NULL;
			if ( !bIsHLTV && ( nLocalPlayerTeam >= FIRST_GAME_TEAM ) )
			{
				if ( pPlayer->IsPlayerClass( TF_CLASS_SPY ) && pPlayer->m_Shared.InCond( TF_COND_DISGUISED ) && ( nPlayerTeamNumber != nLocalPlayerTeam ) )
				{
					pDisguiseTarget = ToTFPlayer( pPlayer->m_Shared.GetDisguiseTarget() );
				}
			}

			// use actual name or disguised name?
			int nNameIndex = pDisguiseTarget ? pDisguiseTarget->entindex() : i;
			g_pVGuiLocalize->ConvertANSIToUnicode( g_PR->GetPlayerName( nNameIndex ), m_vecEntitiesToDraw[nVecIndex].m_wszName, sizeof( m_vecEntitiesToDraw[nVecIndex].m_wszName ) );
			m_vecEntitiesToDraw[nVecIndex].m_nNameWidth = UTIL_ComputeStringWidth( m_hNameFont, m_vecEntitiesToDraw[nVecIndex].m_wszName );

			m_vecEntitiesToDraw[nVecIndex].m_nOffset = ( VEC_HULL_MAX_SCALED( pPlayer ).z );

			// use actual health or disguised health?
			float flHealth = 1.0f;

			if ( pDisguiseTarget )
			{
				flHealth = (float)( pPlayer->m_Shared.GetDisguiseHealth() ) / (float)( pPlayer->m_Shared.GetDisguiseMaxHealth() );
			}
			else
			{
				flHealth = (float)( pPlayer->GetHealth() ) / (float)( pPlayer->GetMaxHealth() );
			}
			// don't show buffed health for this simple bar
			if ( flHealth > 1.0f )
			{
				flHealth = 1.0f;
			}
			m_vecEntitiesToDraw[nVecIndex].m_flHealth = flHealth;

 			// what color should we use?
			float r, g, b;
			pPlayer->GetGlowEffectColor( &r, &g, &b );
			m_vecEntitiesToDraw[nVecIndex].m_clrGlowColor = Color( r * 255, g * 255, b * 255, 255 );

			if ( !pPlayer->IsClientSideGlowEnabled() )
			{
				pPlayer->SetClientSideGlowEnabled( true );
			}
		}

		// loop through the buildings
		for ( int nCount = 0; nCount < IBaseObjectAutoList::AutoList().Count(); nCount++ )
		{
			bool bDraw = false;
			C_BaseObject *pObject = static_cast<C_BaseObject*>( IBaseObjectAutoList::AutoList()[nCount] );
			if ( !pObject->IsDormant() && !pObject->IsMapPlaced() && !pObject->IsEffectActive( EF_NODRAW ) )
			{
				if ( bShowEveryone || ( ( nLocalPlayerTeam >= FIRST_GAME_TEAM ) && ( nLocalPlayerTeam == pObject->GetTeamNumber() ) ) )
				{
					bDraw = true;
				}
			}

			if ( bDraw )
			{
				int nVecIndex = -1;
				FOR_EACH_VEC( m_vecEntitiesToDraw, nTemp )
				{
					if ( m_vecEntitiesToDraw[nTemp].m_nEntIndex == pObject->entindex() )
					{
						nVecIndex = nTemp;
						break;
					}
				}
				if ( nVecIndex == -1 )
				{
					nVecIndex = m_vecEntitiesToDraw.AddToTail();
				}

				// set the player index
				m_vecEntitiesToDraw[nVecIndex].m_nEntIndex = pObject->entindex();

				// don't draw the name if we're currently spectating this building, but we still want it to glow
				m_vecEntitiesToDraw[nVecIndex].m_bDrawName = true;
				if ( pLocalPlayer->GetObserverTarget() == pObject )
				{
					m_vecEntitiesToDraw[nVecIndex].m_bDrawName = false;
				}

				if ( pObject->GetType() == OBJ_TELEPORTER )
				{
					m_vecEntitiesToDraw[nVecIndex].m_nOffset = 30;
				}
				else if ( pObject->GetType() == OBJ_DISPENSER )
				{
					m_vecEntitiesToDraw[nVecIndex].m_nOffset = 70;
				}
				else
				{
					switch ( pObject->GetUpgradeLevel() )
					{
					case 1:
						m_vecEntitiesToDraw[nVecIndex].m_nOffset = 50;
						break;
					case 2:
						m_vecEntitiesToDraw[nVecIndex].m_nOffset = 65;
						break;
					case 3:
					default:
						m_vecEntitiesToDraw[nVecIndex].m_nOffset = 80;
						break;
					}
				}

				pObject->GetTargetIDString( m_vecEntitiesToDraw[nVecIndex].m_wszName, sizeof( m_vecEntitiesToDraw[nVecIndex].m_wszName ), true );
				m_vecEntitiesToDraw[nVecIndex].m_nNameWidth = UTIL_ComputeStringWidth( m_hNameFont, m_vecEntitiesToDraw[nVecIndex].m_wszName );

				float flHealth = 1.0f;
				flHealth = (float)( pObject->GetHealth() ) / (float)( pObject->GetMaxHealth() );
				if ( flHealth > 1.0f )
				{
					flHealth = 1.0f;
				}
				m_vecEntitiesToDraw[nVecIndex].m_flHealth = flHealth;

				// what color should we use?
				float r, g, b;
				pObject->GetGlowEffectColor( &r, &g, &b );
				m_vecEntitiesToDraw[nVecIndex].m_clrGlowColor = Color( r * 255, g * 255, b * 255, 255 );

				if ( !pObject->IsClientSideGlowEnabled() )
				{
					pObject->SetClientSideGlowEnabled( true );
				}
			}
			else
			{
				if ( pObject->IsClientSideGlowEnabled() )
				{
					pObject->SetClientSideGlowEnabled( false );
				}
				RemoveEntity( pObject->entindex() );
			}
		}
	}
	else
	{
		Reset();
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFHudSpectatorExtras::Paint()
{
	BaseClass::Paint();

	if ( !g_PR )
		return;

	if ( tf_spec_xray_disable.GetBool() )
		return;

	int nNameOffset = 35;
	int nHealthWidth = 70;
	int nHealthHeight = 6;

	FOR_EACH_VEC( m_vecEntitiesToDraw, i )
	{
		if ( !m_vecEntitiesToDraw[i].m_bDrawName )
			continue;

		int nEntIndex = m_vecEntitiesToDraw[i].m_nEntIndex;
		if ( IsPlayerIndex( nEntIndex ) && !g_PR->IsConnected( nEntIndex ) )
			continue;

		C_BaseEntity *pEnt = cl_entitylist->GetEnt( nEntIndex );
		if ( !pEnt )
			continue;

		Vector vecPos = pEnt->GetAbsOrigin();
		vecPos.z += m_vecEntitiesToDraw[i].m_nOffset;

		int iX, iY;
		Vector vecWorld( vecPos.x, vecPos.y, vecPos.z );
		if ( GetVectorInHudSpace( vecWorld, iX, iY ) )
		{
 			// draw the name
 			vgui::surface()->DrawSetTextFont( m_hNameFont );
			vgui::surface()->DrawSetTextPos( iX - ( m_vecEntitiesToDraw[i].m_nNameWidth / 2 ), iY - nNameOffset );
			vgui::surface()->DrawSetTextColor( m_vecEntitiesToDraw[i].m_clrGlowColor );
			vgui::surface()->DrawPrintText( m_vecEntitiesToDraw[i].m_wszName, wcslen( m_vecEntitiesToDraw[i].m_wszName ), vgui::FONT_DRAW_NONADDITIVE );

			int xHealthPos = iX - 35;
			int yHealthPos = iY - 10;

			// draw the health bar background
			vgui::surface()->DrawSetColor( Color( 127, 127, 127, 255 ) );
			vgui::surface()->DrawFilledRect( xHealthPos, yHealthPos, xHealthPos + nHealthWidth, yHealthPos + nHealthHeight );

			// draw the health bar
			vgui::surface()->DrawSetColor( m_vecEntitiesToDraw[i].m_clrGlowColor );
			vgui::surface()->DrawFilledRect( xHealthPos, yHealthPos, xHealthPos + ( nHealthWidth * m_vecEntitiesToDraw[i].m_flHealth ), yHealthPos + nHealthHeight );
		}
	}
}