summaryrefslogtreecommitdiff
path: root/game/client/tf/tf_teamstatus.cpp
blob: 432b5790045b697a80ae4671249465dc2b176814 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "iclientmode.h"
#include <vgui_controls/AnimationController.h>
#include <vgui_controls/EditablePanel.h>
#include <vgui_controls/ProgressBar.h>

#include "tf_gamerules.h"
#include "tf_logic_halloween_2014.h"
#include "c_tf_playerresource.h"
#include "tf_playerpanel.h"
#include "tf_teamstatus.h"
#include "tf_matchmaking_shared.h"
#include "tf_match_description.h"

using namespace vgui;

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"


CTFTeamStatusPlayerPanel::CTFTeamStatusPlayerPanel( vgui::Panel *parent, const char *name ) : CTFPlayerPanel( parent, name )
{
	m_pHealthBar = new vgui::ContinuousProgressBar( this, "healthbar" );
	m_pOverhealBar = new vgui::ContinuousProgressBar( this, "overhealbar" );
	m_pClassImageBG = new vgui::Panel( this, "classimagebg" );
	m_pDeathFlag = new vgui::ImagePanel( this, "DeathPanel" );

	m_iTeam = TEAM_UNASSIGNED;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatusPlayerPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatusPlayerPanel::UpdateBorder( void )
{
	// do nothing
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFTeamStatusPlayerPanel::Update( void )
{
	if ( !g_TF_PR || !TFGameRules() )
		return false;

	C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
	if ( !pLocalPlayer || ( pLocalPlayer->GetTeamNumber() < FIRST_GAME_TEAM ) )
		return false;

	bool bChanged = false;
	bool bVisible = GetTeam() >= FIRST_GAME_TEAM;
	int iRespawnWait = -1;

	if ( IsVisible() != bVisible )
	{
		SetVisible( bVisible );
		bChanged = true;
	}

	if ( bVisible && g_TF_PR )
	{
		bool bSameTeamAsLocalPlayer = ( GetTeam() == g_TF_PR->GetTeam( pLocalPlayer->entindex() ) );

		// are we connected with a class?
		Assert( TF_CLASS_UNDEFINED == 0 );
		int iClass = -1;
		bool bAlive = false;
		int iHealth = -1;
		bool bIsLocalPlayer = false;
		if ( m_iPlayerIndex > 0 )
		{
			bIsLocalPlayer = pLocalPlayer->entindex() == m_iPlayerIndex;

			iClass = g_TF_PR->GetPlayerClass( m_iPlayerIndex );

			if ( iClass != TF_CLASS_UNDEFINED )
			{
				bAlive = g_TF_PR->IsAlive( m_iPlayerIndex );
			}
			if ( bAlive )
			{
				iHealth = g_TF_PR->GetHealth( m_iPlayerIndex );
			}

			// calc respawn time remaining
			if ( !bAlive && ( iClass != TF_CLASS_UNDEFINED ) )
			{
				float flRespawnAt = g_TF_PR->GetNextRespawnTime( m_iPlayerIndex );
				iRespawnWait = ( flRespawnAt - gpGlobals->curtime );
				if ( iRespawnWait <= 0 )
					iRespawnWait = -1;
			}

			// hide class info from the other team?
			if ( !bSameTeamAsLocalPlayer )
			{
				iClass = TF_CLASS_UNDEFINED;
			}
		}

		if ( m_iTeam != GetTeam() )
		{
			m_iTeam = GetTeam();
			bChanged = true;
		}

		if ( m_pClassImageBG )
		{
			if ( m_iTeam == TF_TEAM_RED )
			{
				Color bgColor( m_ColorPortraitBGRedLocalPlayer );
				if ( !bIsLocalPlayer )
				{
					bgColor = bAlive ? m_ColorPortraitBGRed : m_ColorPortraitBGRedDead;
				}
				
				m_pClassImageBG->SetBgColor( bgColor );
			}
			else
			{
				Color bgColor( m_ColorPortraitBGBlueLocalPlayer );
				if ( !bIsLocalPlayer )
				{
					bgColor = bAlive ? m_ColorPortraitBGBlue : m_ColorPortraitBGBlueDead;
				}
				m_pClassImageBG->SetBgColor( bgColor );
			}
		}

		// update live state
		if ( m_pClassImage )
		{
			if ( m_bPrevAlive != bAlive )
			{
				bChanged = true;
				m_bPrevAlive = bAlive;
				if ( !bAlive )
				{
					m_pClassImage->SetDrawColor( ( m_iTeam == TF_TEAM_RED ) ? m_ColorPortraitBlendDeadRed : m_ColorPortraitBlendDeadBlue );
				}

				m_pDeathFlag->SetImage( ( m_iTeam == TF_TEAM_RED ) ? "../HUD/comp_player_status" : "../HUD/comp_player_status_blue" );

				if ( bAlive )
				{
					g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( this, "TeamStatus_PlayerAlive", false );
				}
				else
				{
					g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( this, "TeamStatus_PlayerDead", false );
				}
			}

			// update class image
			if ( ( m_iPrevClass != iClass ) || bChanged )
			{
				bChanged = true;
				m_iPrevClass = iClass;
				if ( iClass < 0 )
				{
					m_pClassImage->SetImage( "hud_connecting" );
				}
				else if ( iClass == TF_CLASS_UNDEFINED )
				{
					int iDeadClass = g_TF_PR->GetPlayerClassWhenKilled( m_iPlayerIndex );
					if ( !bAlive && !bSameTeamAsLocalPlayer && ( m_iTeam >= FIRST_GAME_TEAM ) && ( iDeadClass > TF_CLASS_UNDEFINED ) )
					{
						m_pClassImage->SetImage( VarArgs( "%s_alpha", ( m_iTeam == TF_TEAM_RED ) ? g_pszItemClassImagesRed[iDeadClass + 9] : g_pszItemClassImagesBlue[iDeadClass + 9] ) );
					}
					else
					{
						m_pClassImage->SetImage( "class_portraits/silhouette_alpha" );
					}
				}
				else
				{
					if ( bAlive )
					{
						m_pClassImage->SetImage( VarArgs( "%s_alpha", ( m_iTeam == TF_TEAM_RED ) ? g_pszItemClassImagesRed[iClass] : g_pszItemClassImagesBlue[iClass] ) );
					}
					else
					{
						m_pClassImage->SetImage( VarArgs( "%s_alpha", ( m_iTeam == TF_TEAM_RED ) ? g_pszItemClassImagesRed[iClass + 9] : g_pszItemClassImagesBlue[iClass + 9] ) );

					}
				}
			}
		}

		// update health indicator
		if ( m_pHealthBar && m_pOverhealBar )
		{
			if ( iHealth != m_iPrevHealth )
			{
				m_iPrevHealth = iHealth;

				if ( ( iHealth > 0 ) && bSameTeamAsLocalPlayer )
				{
					float flMaxHealth = g_TF_PR->GetMaxHealth( m_iPlayerIndex );
					float flProgress = iHealth / flMaxHealth;

					// health bar
					if ( !m_pHealthBar->IsVisible() )
					{
						m_pHealthBar->SetVisible( true );
					}
					m_pHealthBar->SetProgress( ( flProgress >= 1.0f ) ? 1.0f : flProgress );

					if ( flProgress > m_flPercentageHealthMed )
					{
						m_pHealthBar->SetFgColor( m_ColorBarHealthHigh );
					}
					else if ( flProgress > m_flPercentageHealthLow )
					{
						m_pHealthBar->SetFgColor( m_ColorBarHealthMed );
					}
					else
					{
						m_pHealthBar->SetFgColor( m_ColorBarHealthLow );
					}

					// overheal bar
					if ( flProgress > 1.0f )
					{
						if ( !m_pOverhealBar->IsVisible() )
						{
							m_pOverhealBar->SetVisible( true );
						}
						m_pOverhealBar->SetProgress( flProgress - 1.0f );
					}
					else
					{
						if ( m_pOverhealBar->IsVisible() )
						{
							m_pOverhealBar->SetVisible( false );
						}
					}
				}
				else
				{
					if ( m_pHealthBar->IsVisible() )
					{
						m_pHealthBar->SetVisible( false );
					}

					if ( m_pOverhealBar->IsVisible() )
					{
						m_pOverhealBar->SetVisible( false );
					}
				}

				bChanged = true;
			}
		}

		// update respawn time
		if ( iRespawnWait != m_iPrevRespawnWait )
		{
			m_iPrevRespawnWait = iRespawnWait;
			if ( ( iRespawnWait < 0 ) || !bSameTeamAsLocalPlayer )
			{
				SetDialogVariable( "respawntime", "" );
			}
			else
			{
				SetDialogVariable( "respawntime", VarArgs( "%d", iRespawnWait ) );
			}

			bChanged = true;
		}

		// gamerules state
		if ( TFGameRules()->State_Get() != m_iPrevState )
		{
			m_iPrevState = TFGameRules()->State_Get();
			bChanged = true;
		}
	}

	return bChanged;
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CTFTeamStatus::CTFTeamStatus( Panel *parent, const char *panelName ) : BaseClass( parent, panelName )
{
	m_pPlayerPanelKVs = NULL;
	m_bReapplyPlayerPanelKVs = false;

	Reset();

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

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFTeamStatus::~CTFTeamStatus()
{
	if ( m_pPlayerPanelKVs )
	{
		m_pPlayerPanelKVs->deleteThis();
		m_pPlayerPanelKVs = NULL;
	}
}

void SetGrowDir( CTFTeamStatus::EGrowDir* pGrowDir, const char* pszString )
{
	if ( FStrEq( pszString, "east" ) )
	{
		(*pGrowDir) = CTFTeamStatus::EGrowDir::GROW_EAST;
	}
	else if ( FStrEq( pszString, "west" ) )
	{
		(*pGrowDir) = CTFTeamStatus::EGrowDir::GROW_WEST;
	}
	else
	{
		Assert( !"Invalid direction string for team status team grow direction" );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::ApplySettings( KeyValues *inResourceData )
{
	BaseClass::ApplySettings( inResourceData );

	SetGrowDir( &m_eTeam1GrowDir, inResourceData->GetString( "team1_grow_dir" ) );
	SetGrowDir( &m_eTeam2GrowDir, inResourceData->GetString( "team2_grow_dir" ) );

	KeyValues *pItemKV = inResourceData->FindKey( "playerpanels_kv" );
	if ( pItemKV )
	{
		if ( m_pPlayerPanelKVs )
		{
			m_pPlayerPanelKVs->deleteThis();
		}
		m_pPlayerPanelKVs = new KeyValues( "playerpanels_kv" );
		pItemKV->CopySubkeys( m_pPlayerPanelKVs );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	m_bReapplyPlayerPanelKVs = true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::PerformLayout( void )
{
	BaseClass::PerformLayout();

	int iTeam1Count = 0;
	int iTeam2Count = 0;

	// Tally up how many are on each team so we can scale them appropriately down below
	for ( int i = 0; i < m_PlayerPanels.Count(); i++ )
	{
		if ( m_PlayerPanels[i]->GetTeam() == TF_TEAM_BLUE )
		{
			++iTeam1Count;
		}
		else if ( m_PlayerPanels[i]->GetTeam() == TF_TEAM_RED )
		{
			++iTeam2Count;
		}
	}

	C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();

	// Snag local player info
	int nLocalPlayerTeam = TF_TEAM_COUNT;
	int nLocalPlayerIndex = -1;
	if ( pLocalPlayer )
	{
		nLocalPlayerIndex = pLocalPlayer->entindex();
		nLocalPlayerTeam = pLocalPlayer->GetTeamNumber();
	}

	int iTeam1Processed = 0;
	int iTeam2Processed = 0;

	for ( int i = 0; i < m_PlayerPanels.Count(); i++ )
	{
		if ( m_PlayerPanels[i]->GetPlayerIndex() <= 0 )
		{
			m_PlayerPanels[i]->SetVisible( false );
			continue;
		}

		bool bIsLocalPlayerPanel = nLocalPlayerIndex == m_PlayerPanels[i]->GetPlayerIndex();
		int iTeam = m_PlayerPanels[i]->GetTeam();
		int iXPos = 0;

		// Setup vars
		EGrowDir& eGrowDir	= iTeam == TF_TEAM_BLUE ? m_eTeam1GrowDir	: m_eTeam2GrowDir;
		int& iTeamCount		= iTeam == TF_TEAM_BLUE ? iTeam1Count		: iTeam2Count;
		int& iBaseX			= iTeam == TF_TEAM_BLUE ? m_iTeam1BaseX		: m_iTeam2BaseX;
		int& iProcessed		= iTeam == TF_TEAM_BLUE ? iTeam1Processed	: iTeam2Processed;
		int& iMaxExpand		= iTeam == TF_TEAM_BLUE ? m_iTeam1MaxExpand	: m_iTeam2MaxExpand;
		const int iGap		= RemapValClamped( iTeamCount, 6, 12, m_i6v6Gap, m_i12v12Gap );

		// Local player is always the innermost panel
		int nTeamPanelIndex = bIsLocalPlayerPanel ? 0
							: iTeam == nLocalPlayerTeam ? iProcessed + 1 
							: iProcessed;

		// Setup X-position and widths
		// Use the max width if less than 6 (to fill out the space)
		int nNewWide = iTeamCount <= 6 ? m_iMaxSize : ( iMaxExpand - ( iGap * ( iTeamCount - 1 ) ) ) / iTeamCount;

		// How far each panel is apart from each other
		const int iXStep = iGap + nNewWide;
		// How many steps out this panel should be
		const int iXOffset = iXStep * nTeamPanelIndex;
		// Step out the panels by the step
		iXPos = eGrowDir == GROW_EAST ? iBaseX + iXOffset				// Align the left edge of the left-most panel on the specified point
			  : eGrowDir == GROW_WEST ? iBaseX - iXOffset - nNewWide	// Align the right edge of the right-most panel on the specified point
			  : 0;

		// This is expensive, so only do it if we need to
		if ( nNewWide != m_PlayerPanels[i]->GetWide() )
		{
			// We change the width in the KV's then reapply settings so the children will do their
			// magical proportionaltoparent layout.
			if ( m_pPlayerPanelKVs )
			{
				m_pPlayerPanelKVs->SetInt( "wide", scheme()->GetProportionalNormalizedValueEx( GetScheme(), nNewWide ) );
				m_PlayerPanels[i]->ApplySettings( m_pPlayerPanelKVs );
				m_PlayerPanels[i]->InvalidateLayout( false, true );
				m_PlayerPanels[i]->Update();
			}
		}

		if ( !bIsLocalPlayerPanel )
		{
			++iProcessed;
		}

		m_PlayerPanels[i]->SetPos( iXPos, m_PlayerPanels[i]->GetYPos() );
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::Reset()
{
	for ( int i = 0; i < m_PlayerPanels.Count(); i++ )
	{
		m_PlayerPanels[i]->Reset();
	}

	InvalidateLayout();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CTFTeamStatus::ShouldDraw( void )
{
	// Get the player and active weapon.
	C_TFPlayer *pPlayer = C_TFPlayer::GetLocalTFPlayer();

	if ( !pPlayer )
		return false;

	int iLocalTeam = g_TF_PR->GetTeam( pPlayer->entindex() );
	if ( iLocalTeam < FIRST_GAME_TEAM )
		return false;

	if ( TFGameRules() )
	{
		const IMatchGroupDescription* pMatchDesc = GetMatchGroupDescription( TFGameRules()->GetCurrentMatchGroup() );
		if ( !pMatchDesc || !pMatchDesc->m_params.m_bUseMatchHud )
			return false;

		if ( TFGameRules()->ShowMatchSummary() )
			return false; 
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::OnTick()
{
	if ( IsVisible() != ShouldDraw() )
	{
		SetVisible( ShouldDraw() );
	}

	if ( IsVisible() )
	{
		RecalculatePlayerPanels();
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CTFTeamStatusPlayerPanel *CTFTeamStatus::GetOrAddPanel( int iPanelIndex )
{
	if ( iPanelIndex < m_PlayerPanels.Count() )
	{
		return m_PlayerPanels[iPanelIndex];
	}
	Assert( iPanelIndex == m_PlayerPanels.Count() );
	CTFTeamStatusPlayerPanel *pPanel = new CTFTeamStatusPlayerPanel( this, VarArgs( "playerpanel%d", iPanelIndex ) );
	if ( m_pPlayerPanelKVs )
	{
		pPanel->ApplySettings( m_pPlayerPanelKVs );
		pPanel->InvalidateLayout( false, true );
		InvalidateLayout();
	}
	m_PlayerPanels.AddToTail( pPanel );
	return pPanel;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::RecalculatePlayerPanels( void )
{
	C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
	if ( !pPlayer || !g_TF_PR )
		return;

	int iPanel = 0;
	bool bNeedsLayout = false;
	int iLocalTeam = g_TF_PR->GetTeam( pPlayer->entindex() );
	if ( iLocalTeam >= FIRST_GAME_TEAM )
	{
		for ( int i = 1; i <= MAX_PLAYERS; i++ )
		{
			if ( !g_TF_PR->IsConnected( i ) )
				continue;

			int iTeam = g_TF_PR->GetTeam( i );
			if ( iTeam < FIRST_GAME_TEAM )
				continue;

			// Add an entry
			CTFTeamStatusPlayerPanel *pPanel = GetOrAddPanel( iPanel );

			if ( pPanel->GetPlayerIndex() != i )
			{
				bNeedsLayout = true;
			}

			pPanel->SetPlayerIndex( i );

			if ( pPanel->GetPreviousTeam() != pPanel->GetTeam() )
			{
				bNeedsLayout = true;
			}

			++iPanel;
		}
	}

	// Clear out any extra panels
	for ( int i = iPanel; i < m_PlayerPanels.Count(); i++ )
	{
		if ( m_PlayerPanels[i]->GetPlayerIndex() != 0 )
		{
			bNeedsLayout = true;
		}

		m_PlayerPanels[i]->SetPlayerIndex( 0 );
	}

	UpdatePlayerPanels();

	if ( bNeedsLayout )
	{
		InvalidateLayout();	
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTFTeamStatus::UpdatePlayerPanels( void )
{
	if ( !g_TF_PR )
		return;

	for ( int i = 0; i < m_PlayerPanels.Count(); i++ )
	{
		m_PlayerPanels[i]->Update();
	}
}