summaryrefslogtreecommitdiff
path: root/game/client/tf/tf_tips.cpp
blob: 479c5d633005da203709d38679433400e88a1732 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Rich Presence support
//
//=====================================================================================//

#include "cbase.h"
#include "tf_tips.h"
#include "tier3/tier3.h"
#include <vgui/ILocalize.h>
#include "cdll_util.h"
#include "fmtstr.h"
#include "tf_gamerules.h"
#include "tf_hud_statpanel.h"
#include "filesystem.h"

//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CTFTips::CTFTips() : CAutoGameSystem( "CTFTips" )
{
	Q_memset( m_iTipCount, 0, sizeof( m_iTipCount ) );
	m_iTipCountAll = 0;
	m_iCurrentClassTip = 0;
	m_bInited = false;
}

//-----------------------------------------------------------------------------
// Purpose: Initializer
//-----------------------------------------------------------------------------
bool CTFTips::Init()
{
	if ( !m_bInited )
	{
		// count how many tips there are for each class and in total
		m_iTipCountAll = 0;
		for ( int iClass = TF_FIRST_NORMAL_CLASS; iClass <= TF_LAST_NORMAL_CLASS; iClass++ )
		{
			// tip count per class is stored in resource file
			wchar_t *wzTipCount = g_pVGuiLocalize->Find( CFmtStr( "Tip_%d_Count", iClass ) );
			int iClassTipCount = wzTipCount ? _wtoi( wzTipCount ) : 0;
			m_iTipCount[iClass] = iClassTipCount;
			m_iTipCountAll += iClassTipCount;

			m_iArenaTipCount = g_pVGuiLocalize->Find( "Tip_arena_Count" ) ? _wtoi( g_pVGuiLocalize->Find( "Tip_arena_Count" ) ) : 0;	
		}

		// Captain Canteen mascot parts!
		m_CaptainCanteenBody.RemoveAll();
		m_CaptainCanteenMisc.RemoveAll();
		m_CaptainCanteenHat.RemoveAll();

		KeyValues *kv = new KeyValues( "cpncntn" );
		KeyValues::AutoDelete autodelete_key( kv );

		if ( kv->LoadFromFile( g_pFullFileSystem, "scripts/cpncntn.txt", "MOD" ) )
		{
			for ( KeyValues *pSubKey = kv->GetFirstTrueSubKey(); pSubKey; pSubKey = pSubKey->GetNextTrueSubKey() )
			{
				// Figure out which bucket it goes in
				CUtlVector< CaptainCanteenAsset_t > *pAssetBucket = NULL;

				if ( V_stricmp( pSubKey->GetName(), "body" ) == 0 )
				{
					pAssetBucket = &m_CaptainCanteenBody;
				}
				else if ( V_stricmp( pSubKey->GetName(), "misc" ) == 0 )
				{
					pAssetBucket = &m_CaptainCanteenMisc;
				}
				else if ( V_stricmp( pSubKey->GetName(), "hat" ) == 0 )
				{
					pAssetBucket = &m_CaptainCanteenHat;
				}

				if ( pAssetBucket )
				{
					// Added more assets to that bucket
					for ( KeyValues *pAssetKey = pSubKey->GetFirstSubKey(); pAssetKey; pAssetKey = pAssetKey->GetNextKey() )
					{
						int nNewAsset = pAssetBucket->AddToTail();
						V_strncpy( (*pAssetBucket)[ nNewAsset ].szImage, pAssetKey->GetName(), sizeof( (*pAssetBucket)[ nNewAsset ].szImage ) );
						(*pAssetBucket)[ nNewAsset ].flRarity = ( 1.0f / pAssetKey->GetFloat() );
					}
				}
			}
		}

		m_bInited = true;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Returns a random tip, selected from tips for all classes, 
//          fills in iClassUsed with the class the tip is for
//-----------------------------------------------------------------------------
const wchar_t *CTFTips::GetRandomTip( int &iClassUsed )
{
	Init();

	// Chance of reminding players about the Abuse Reporter.
	// The chance is very high for the first 20 hours of play since newbies get picked on a lot.
	int abuseHintChance = 3;

	if ( CTFStatPanel::GetTotalHoursPlayed() < 20.0f )
	{
		abuseHintChance = 33;
	}

	if ( RandomInt( 1, 100 ) <= abuseHintChance )
	{
		iClassUsed = RandomInt( TF_FIRST_NORMAL_CLASS, TF_LAST_NORMAL_CLASS );
		return GetAbuseReportTip();
	}

	// pick a random tip
	int iTip = RandomInt( 0, m_iTipCountAll-1 );
	// walk through each class until we find the class this tip lands in
	for ( int iClass = TF_FIRST_NORMAL_CLASS; iClass <= TF_LAST_NORMAL_CLASS; iClass++ )
	{
		Assert( iTip >= 0 );
		int iClassTipCount = m_iTipCount[iClass]; 
		if ( iTip < iClassTipCount )
		{
			iClassUsed = iClass; 

			// return the tip
			return GetTip( iClass, iTip+1 );
		}
		iTip -= iClassTipCount;
	}
	Assert( false );	// shouldn't hit this

	iClassUsed = TF_CLASS_UNDEFINED;
	return L"";
}

//-----------------------------------------------------------------------------
// Purpose: Returns the next tip for specified class
//-----------------------------------------------------------------------------
const wchar_t *CTFTips::GetNextClassTip( int iClass )
{
	int iTipClass = TF_CLASS_UNDEFINED;

	// OK to call this function with TF_CLASS_UNDEFINED or TF_CLASS_RANDOM, just return a random tip for any class in that case
	if ( iClass < TF_FIRST_NORMAL_CLASS || iClass > TF_LAST_NORMAL_CLASS )
		return GetRandomTip( iTipClass );

	if ( TFGameRules() && TFGameRules()->IsInArenaMode() == true )
	{
		return GetArenaTip();
	}
	
	int iClassTipCount = m_iTipCount[iClass];
	Assert( 0 != iClassTipCount );
	if ( 0 == iClassTipCount )
		return L"";
	// wrap the tip index to the valid range for this class
	if ( m_iCurrentClassTip >= iClassTipCount )
	{
		m_iCurrentClassTip %= iClassTipCount;
	}

	// return the tip
	const wchar_t *wzTip = GetTip( iClass, m_iCurrentClassTip+1 );
	m_iCurrentClassTip++;

	return wzTip;
}

//-----------------------------------------------------------------------------
// Purpose: Returns specified tip index for arena
//-----------------------------------------------------------------------------
const wchar_t *CTFTips::GetArenaTip( void )
{
	int iClassTipCount = m_iArenaTipCount;
	Assert( 0 != iClassTipCount );

	if ( 0 == iClassTipCount )
		return L"";

	// wrap the tip index to the valid range for this class
	if ( m_iCurrentClassTip >= iClassTipCount )
	{
		m_iCurrentClassTip %= iClassTipCount;
	}

	// return the tip
	const wchar_t *wzFmt = g_pVGuiLocalize->Find( CFmtStr( "#Tip_arena_%d", m_iCurrentClassTip+1 ) );
	static wchar_t wzTip[512] = L"";

	// replace any commands with their bound keys
	UTIL_ReplaceKeyBindings( wzFmt, 0, wzTip, sizeof( wzTip ) );

	m_iCurrentClassTip++;

	return wzTip;

}

//-----------------------------------------------------------------------------
// Purpose: Returns specified tip index for specified class
//-----------------------------------------------------------------------------
const wchar_t *CTFTips::GetTip( int iClass, int iTip )
{
	static wchar_t wzTip[512] = L"";

	// get the tip
	const wchar_t *wzFmt = g_pVGuiLocalize->Find( CFmtStr( "#Tip_%d_%d", iClass, iTip ) );
	// replace any commands with their bound keys
	UTIL_ReplaceKeyBindings( wzFmt, 0, wzTip, sizeof( wzTip ) );

	return wzTip;
}

//-----------------------------------------------------------------------------
// Purpose: Returns tip about using the Abuse Reporter
//-----------------------------------------------------------------------------
const wchar_t *CTFTips::GetAbuseReportTip( void )
{
	static wchar_t wzAbuseTip[512] = L"";

	// replace any commands with their bound keys
	const wchar_t *wzFmt = g_pVGuiLocalize->Find( "Tip_Abuse_Report" );
	UTIL_ReplaceKeyBindings( wzFmt, 0, wzAbuseTip, sizeof( wzAbuseTip ) );

	return wzAbuseTip;
}

//-----------------------------------------------------------------------------
// Purpose: Returns tip related to MvM
//-----------------------------------------------------------------------------
const wchar_t *CTFTips::GetRandomMvMTip( int &iClassUsed )
{
	Init();

	static wchar_t wzMvMTip[512] = L"";
	static int iPrevMvMClass = -1;

	iClassUsed = RandomInt( TF_FIRST_NORMAL_CLASS, TF_LAST_NORMAL_CLASS );

	if ( iClassUsed == iPrevMvMClass )
	{
		iClassUsed = ( iClassUsed + 1 ) % TF_LAST_NORMAL_CLASS;
	}
	iPrevMvMClass = iClassUsed;

	// Get Tip Count
	CFmtStr fmtTipCount("Tip_MvM_%d_Count", iClassUsed );
	int iMvMTipCount = g_pVGuiLocalize->Find( fmtTipCount ) ? _wtoi( g_pVGuiLocalize->Find( fmtTipCount ) ) : 1;

	int iTip = RandomInt( 1, iMvMTipCount );

	// replace any commands with their bound keys
	const wchar_t *wzFmt = g_pVGuiLocalize->Find( CFmtStr( "#Tip_MvM_%d_%d", iClassUsed, iTip ) );
	UTIL_ReplaceKeyBindings( wzFmt, 0, wzMvMTip, sizeof( wzMvMTip ) );

	iPrevMvMClass = iClassUsed;
	return wzMvMTip;
}

void CTFTips::GetRandomCaptainCanteenImages( const char **ppchBody, const char **ppchMisc, const char **ppchHat )
{
	// Select and copy over all 3 parts
	*ppchBody = GetRandomCaptainCanteenAsset( &m_CaptainCanteenBody );
	*ppchMisc = GetRandomCaptainCanteenAsset( &m_CaptainCanteenMisc );
	*ppchHat = GetRandomCaptainCanteenAsset( &m_CaptainCanteenHat );
}

const char *CTFTips::GetRandomCaptainCanteenAsset( CUtlVector< CaptainCanteenAsset_t > *pAssetBucket )
{
	// If there's nothing in the bucket, bail out
	if ( pAssetBucket->Count() <= 0 )
	{
		return "";
	}

	if ( pAssetBucket->Count() == 1 )
	{
		// Easy out if there's only 1 choice
		return (*pAssetBucket)[ 0 ].szImage;
	}

	// Get the total scale of selection possibilities
	float flSelectionTotal = 0.0f;

	for ( int i = 0; i < pAssetBucket->Count(); ++i )
	{
		flSelectionTotal += (*pAssetBucket)[ i ].flRarity;
	}

	if ( flSelectionTotal > 0.0f )
	{
		// Pick a number from 0 to 1
		float flRand = RandomFloat();

		// Loop through to figure out which asset we've chosen
		float flCurrentPosition = 0.0f;

		for ( int i = 0; i < pAssetBucket->Count(); ++i )
		{
			flCurrentPosition += (*pAssetBucket)[ i ].flRarity / flSelectionTotal;

			if ( flRand <= flCurrentPosition )
			{
				// We landed on this random asset
				return (*pAssetBucket)[ i ].szImage;
			}
		}
	}

	// Something went wrong... just return the first choice
	return (*pAssetBucket)[ 0 ].szImage;
}


// global instance
CTFTips g_TFTips;