summaryrefslogtreecommitdiff
path: root/game/client/dod/dod_hud_deathstats.cpp
blob: 90fa0647815b9aecfaa232a15e366d9b45a8bcdb (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "dod_hud_deathstats.h"
#include "vgui_controls/AnimationController.h"
#include "iclientmode.h"
#include <vgui_controls/Label.h>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include "hud_macros.h"
#include "c_dod_playerresource.h"
#include "c_dod_team.h"
#include "c_dod_player.h"

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

extern ConVar cl_deathicon_width;
extern ConVar cl_deathicon_height;

//DECLARE_HUDELEMENT( CDODDeathStatsPanel );

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CDODDeathStatsPanel::CDODDeathStatsPanel( const char *pElementName )
: EditablePanel( NULL, "DeathStats" ), CHudElement( pElementName )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );
	SetVisible( false );
	SetAlpha( 0 );
	SetScheme( "ClientScheme" );

	m_pAttackerHistoryLabel = new vgui::Label( this, "AttackerDmgLabel", "..." );
	m_pSummaryLabel = new vgui::Label( this, "LifeSummaryLabel", "..." );

	memset( &m_DeathRecord, 0, sizeof(m_DeathRecord) );

	LoadControlSettings("Resource/UI/DeathStats.res");	
}

void CDODDeathStatsPanel::OnScreenSizeChanged( int iOldWide, int iOldTall )
{
	LoadControlSettings( "resource/UI/DeathStats.res" );
}

void CDODDeathStatsPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	SetBgColor( GetSchemeColor("TransparentLightBlack", pScheme) );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CDODDeathStatsPanel::Reset()
{
	Hide();
}

void CDODDeathStatsPanel::Init()
{
	Hide();

	ListenForGameEvent( "player_death" );

	m_iMaterialTexture = vgui::surface()->CreateNewTextureID();

	CHudElement::Init();
}

void CDODDeathStatsPanel::VidInit( void )
{
	m_iconD_skull	= gHUD.GetIcon( "d_skull_dod" );
	m_pIconKill		= gHUD.GetIcon( "stats_kill" );
	m_pIconWounded	= gHUD.GetIcon( "stats_wounded" );
	m_pIconCap		= gHUD.GetIcon( "stats_cap" );
	m_pIconDefended	= gHUD.GetIcon( "stats_defended" );
}

//-----------------------------------------------------------------------------
// Purpose: Handle player_death message
//-----------------------------------------------------------------------------
void CDODDeathStatsPanel::FireGameEvent( IGameEvent * event)
{
	if ( Q_strcmp( "player_death", event->GetName() ) == 0 )
	{	
		C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();

		if ( !pLocalPlayer )
			return;

		if ( !g_PR )
			return;

		int victim = engine->GetPlayerForUserID( event->GetInt("userid") );

		// only deal with deathnotices where we die
		if ( victim != pLocalPlayer->entindex() )
		{
			return;
		}

		int killer = engine->GetPlayerForUserID( event->GetInt("attacker") );
		
		const char *killedwith = event->GetString( "weapon" );

		char fullkilledwith[128];
		if ( killedwith && *killedwith )
		{
			Q_snprintf( fullkilledwith, sizeof(fullkilledwith), "d_%s", killedwith );
		}
		else
		{
			fullkilledwith[0] = 0;
		}

		// Get the names of the players
		const char *killer_name = g_PR->GetPlayerName( killer );
		const char *victim_name = g_PR->GetPlayerName( victim );

		if ( !killer_name )
			killer_name = "";
		if ( !victim_name )
			victim_name = "";

		m_DeathRecord.Killer.iEntIndex = killer;
		m_DeathRecord.Victim.iEntIndex = victim;
		Q_strncpy( m_DeathRecord.Killer.szName, killer_name, MAX_PLAYER_NAME_LENGTH );
		Q_strncpy( m_DeathRecord.Victim.szName, victim_name, MAX_PLAYER_NAME_LENGTH );
		m_DeathRecord.bSuicide = ( !killer || killer == victim );

		// Try and find the death identifier in the icon list
		m_DeathRecord.iconDeath = gHUD.GetIcon( fullkilledwith );

		if ( !m_DeathRecord.iconDeath )
		{
			// Can't find it, so use the default skull & crossbones icon
			m_DeathRecord.iconDeath = m_iconD_skull;
		}

		// Info we get from this message:
		// - who killed us with what

		// Info that is networked to the local player
		// - the hitgroups we hit the guy who killed us with
		// - life kills
		// - life woundings
		// - life caps
		// - life defenses

		Show();
	}	
}

const char *szHitgroupNames[] = 
{
	"head",
	"chest",
	"arm",
	"leg"
};

void CDODDeathStatsPanel::Paint( void )
{
	int deathNoticeWidth = 0;

	if ( m_DeathRecord.Victim.iEntIndex > 0 )
		deathNoticeWidth = DrawDeathNoticeItem( m_iDeathNoticeX, m_iDeathNoticeY );

	const int minWidth = XRES(160);

	int panelWidth = ( deathNoticeWidth < minWidth ? minWidth : deathNoticeWidth );

	int wide, tall;

	// set width of summary label to death notice width
	m_pSummaryLabel->GetSize( wide, tall );
	m_pSummaryLabel->SetSize( panelWidth, tall );

	C_DODPlayer *pLocalPlayer = C_DODPlayer::GetLocalDODPlayer();

	if ( !pLocalPlayer )
		return;

	char buf[512];
	buf[0] = '\0';

	if ( !m_DeathRecord.bSuicide )
	{
		bool bStart = true;
		bool bHit = false;

		for ( int i=0;i<4;i++ )
		{
			if ( pLocalPlayer->m_iHitsOnAttacker[i] > 0 )
			{
				bHit = true;
				if ( bStart )
				{
					Q_snprintf( buf, sizeof(buf), "You hit %s %i %s in the %s\n",
						m_DeathRecord.Killer.szName,
						pLocalPlayer->m_iHitsOnAttacker[i],
						pLocalPlayer->m_iHitsOnAttacker[i] == 1 ? "time" : "times",
						szHitgroupNames[i] );

					bStart = false;
				}
				else
				{
					Q_snprintf( buf, sizeof(buf), "%s and %i %s in the %s\n",
						buf,
						pLocalPlayer->m_iHitsOnAttacker[i],
						pLocalPlayer->m_iHitsOnAttacker[i] == 1 ? "time" : "times",
						szHitgroupNames[i] );
				}
			}
		}
	}	

	Q_strncat( buf, "\n", sizeof(buf), COPY_ALL_CHARACTERS );

	if ( pLocalPlayer->m_iPerLifeKills )
	{
		Q_snprintf( buf, sizeof(buf), "%sKills: %i\n\n",
			buf,
			pLocalPlayer->m_iPerLifeKills );
	}

	if ( pLocalPlayer->m_iPerLifeWounded )
	{
		Q_snprintf( buf, sizeof(buf), "%sWounded: %i\n\n",
			buf,
			pLocalPlayer->m_iPerLifeWounded );
	}

	if ( pLocalPlayer->m_iPerLifeCaptures )
	{
		Q_snprintf( buf, sizeof(buf), "%sFlag Captures: %i\n\n",
			buf,
			pLocalPlayer->m_iPerLifeCaptures );
	}

	if ( pLocalPlayer->m_iPerLifeDefenses )
	{
		Q_snprintf( buf, sizeof(buf), "%sFlag Defenses: %i\n\n",
			buf,
			pLocalPlayer->m_iPerLifeDefenses );
	}

	m_pAttackerHistoryLabel->SetText( buf );
	m_pAttackerHistoryLabel->SizeToContents();

	int panel_h = YRES(160);

	SetSize( panelWidth, panel_h );
}

float GetScale( int nIconWidth, int nIconHeight, int nWidth, int nHeight );

int CDODDeathStatsPanel::DrawDeathNoticeItem( int x, int y )
{
	// Get the team numbers for the players involved
	int iKillerTeam = TEAM_UNASSIGNED;
	int iVictimTeam = TEAM_UNASSIGNED;

	if( g_PR )
	{
		iKillerTeam = g_PR->GetTeam( m_DeathRecord.Killer.iEntIndex );
		iVictimTeam = g_PR->GetTeam( m_DeathRecord.Victim.iEntIndex );
	}	

	wchar_t victim[ 256 ];
	wchar_t killer[ 256 ];

	g_pVGuiLocalize->ConvertANSIToUnicode( m_DeathRecord.Victim.szName, victim, sizeof( victim ) );
	g_pVGuiLocalize->ConvertANSIToUnicode( m_DeathRecord.Killer.szName, killer, sizeof( killer ) );

	// Get the local position for this notice
	int len = UTIL_ComputeStringWidth( m_hTextFont, victim );

	int iconWide;
	int iconTall;

	CHudTexture *icon = m_DeathRecord.iconDeath;

	Assert( icon );

	if ( !icon )
		return 0;

	if( icon->bRenderUsingFont )
	{
		iconWide = surface()->GetCharacterWidth( icon->hFont, icon->cCharacterInFont );
		iconTall = surface()->GetFontTall( icon->hFont );
	}
	else
	{
		float scale = GetScale( icon->Width(), icon->Height(), XRES(cl_deathicon_width.GetInt()), YRES(cl_deathicon_height.GetInt()) );
		iconWide = (int)( scale * (float)icon->Width() );
		iconTall = (int)( scale * (float)icon->Height() );
	}

	int spacerX = XRES(5);

	//int x = xRight - len - spacerX - iconWide - XRES(10);

	surface()->DrawSetTextFont( m_hTextFont );
	int iFontTall = vgui::surface()->GetFontTall( m_hTextFont );
	int yText = y + ( iconTall - iFontTall ) / 2;

	int boxWidth = len + iconWide + spacerX;
	int boxHeight = MIN( iconTall, m_flLineHeight );	
	int boxBorder = XRES(2);

	// Only draw killers name if it wasn't a suicide
	if ( !m_DeathRecord.bSuicide )
	{
		int nameWidth = UTIL_ComputeStringWidth( m_hTextFont, killer ) + spacerX;	// gap

		//x -= nameWidth;
		boxWidth += nameWidth;

		Panel::DrawBox( x-boxBorder,
			y-boxBorder,
			boxWidth+2*boxBorder,
			boxHeight+2*boxBorder,
			m_ActiveBackgroundColor,
			1.0 );

		Color c = g_PR->GetTeamColor( iKillerTeam );
		surface()->DrawSetTextColor( c );

		// Draw killer's name			
		surface()->DrawSetTextPos( x, yText );
		const wchar_t *p = killer;
		while ( *p )
		{
			surface()->DrawUnicodeChar( *p++ );
		}
		surface()->DrawGetTextPos( x, yText );

		x += spacerX;
	}
	else
	{
		Panel::DrawBox( x-boxBorder,
			y-boxBorder,
			boxWidth+2*boxBorder,
			boxHeight+2*boxBorder,
			m_ActiveBackgroundColor,
			1.0 );
	}

	Color iconColor( 255, 80, 0, 255 );

	// Draw death weapon
	//If we're using a font char, this will ignore iconTall and iconWide
	icon->DrawSelf( x, y, iconWide, iconTall, iconColor );
	x += iconWide + spacerX;

	Color c = g_PR->GetTeamColor( iVictimTeam );
	surface()->DrawSetTextColor( c );

	// Draw victims name
	surface()->DrawSetTextFont( m_hTextFont );	//reset the font, draw icon can change it
	surface()->DrawSetTextPos( x, yText );
	const wchar_t *p = victim;
	while ( *p )
	{
		surface()->DrawUnicodeChar( *p++ );
	}

	return boxWidth;
}