summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_upgrades_shared.cpp
blob: a5984456fbc6c6b4fd58211c9d9285aa915c7740 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:		Load item upgrade data from KeyValues
//
// $NoKeywords: $
//=============================================================================

#include "cbase.h"

#include "tf_shareddefs.h"
#include "tf_upgrades_shared.h"
#include "filesystem.h"
#include "econ_item_system.h"
#include "tf_gamerules.h"
#include "tf_item_powerup_bottle.h"


CMannVsMachineUpgradeManager g_MannVsMachineUpgrades;

CMannVsMachineUpgradeManager::CMannVsMachineUpgradeManager()
{
	SetDefLessFunc( m_AttribMap );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMannVsMachineUpgradeManager::LevelInitPostEntity()
{
	LoadUpgradesFile();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMannVsMachineUpgradeManager::LevelShutdownPostEntity()
{
	m_Upgrades.RemoveAll();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMannVsMachineUpgradeManager::ParseUpgradeBlockForUIGroup( KeyValues *pKV, int iDefaultUIGroup )
{
	if ( !pKV )
		return;

	for ( KeyValues *pData = pKV->GetFirstSubKey(); pData != NULL; pData = pData->GetNextKey() )
	{
		// Check that the expected data is there
		KeyValues *pkvAttribute = pData->FindKey( "attribute" );
		KeyValues *pkvIcon = pData->FindKey( "icon" );
		KeyValues *pkvIncrement = pData->FindKey( "increment" );
		KeyValues *pkvCap = pData->FindKey( "cap" );
		KeyValues *pkvCost = pData->FindKey( "cost" );
		if ( !pkvAttribute || !pkvIcon || !pkvIncrement || !pkvCap || !pkvCost )
		{
			Warning( "Upgrades: One or more upgrades missing attribute, icon, increment, cap, or cost value.\n" );
			return;
		}

		int index = m_Upgrades.AddToTail();

		const char *pszAttrib = pData->GetString( "attribute" );
		V_strncpy( m_Upgrades[ index ].szAttrib, pszAttrib, sizeof( m_Upgrades[ index ].szAttrib ) );
		const CEconItemSchema *pSchema = ItemSystem()->GetItemSchema();
		if ( pSchema )
		{
			// If we can't find a matching attribute, nuke this entry completely
			const CEconItemAttributeDefinition *pAttr = pSchema->GetAttributeDefinitionByName( m_Upgrades[ index ].szAttrib );
			if ( !pAttr )
			{
				Warning( "Upgrades: Invalid attribute reference! -- %s.\n", m_Upgrades[ index ].szAttrib );
				m_Upgrades.Remove( index );
				continue;
			}
			Assert( pAttr->GetAttributeType() );
			if ( !pAttr->GetAttributeType()->BSupportsGameplayModificationAndNetworking() )
			{
				Warning( "Upgrades: Invalid attribute '%s' is of a type that doesn't support networking!\n", m_Upgrades[ index ].szAttrib );
				m_Upgrades.Remove( index );
				continue;
			}
			if ( !pAttr->IsStoredAsFloat() || pAttr->IsStoredAsInteger() )
			{
				Warning( "Upgrades: Attribute reference '%s' is not stored as a float!\n", m_Upgrades[ index ].szAttrib );
				m_Upgrades.Remove( index );
				continue;
			}
		}

		V_strncpy( m_Upgrades[index].szIcon, pData->GetString( "icon" ), sizeof( m_Upgrades[ index ].szIcon ) );
		m_Upgrades[ index ].flIncrement = pData->GetFloat( "increment" );
		m_Upgrades[ index ].flCap = pData->GetFloat( "cap" );
		m_Upgrades[ index ].nCost = pData->GetInt( "cost" );
		m_Upgrades[ index ].nUIGroup = pData->GetInt( "ui_group", iDefaultUIGroup );
		m_Upgrades[ index ].nQuality = pData->GetInt( "quality", MVM_UPGRADE_QUALITY_NORMAL );
		m_Upgrades[ index ].nTier = pData->GetInt( "tier", 0 );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CMannVsMachineUpgradeManager::GetAttributeIndexByName( const char* pszAttributeName )
{
	// Already in the map?
	if( m_AttribMap.Find( pszAttributeName ) != m_AttribMap.InvalidIndex() )
	{
		return m_AttribMap.Element( m_AttribMap.Find( pszAttributeName ) );
	}

	// Not in the map.  Find it in the vector and add it to the map
	for( int i=0, nCount = m_Upgrades.Count() ; i<nCount; ++i )
	{
		// Find the index
		const char* pszAttrib = m_Upgrades[i].szAttrib;
		if( FStrEq( pszAttributeName, pszAttrib ) )
		{
			// Add to map
			m_AttribMap.Insert( pszAttributeName, i );
			// Return value
			return i;
		}
	}

	AssertMsg1( 0, "Attribute \"%s\" not found!", pszAttributeName );
	return -1;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMannVsMachineUpgradeManager::LoadUpgradesFile( void )
{
	// Determine the upgrades file to load
	const char *pszPath = "scripts/items/mvm_upgrades.txt";

	// Allow map to override
	const char *pszCustomUpgradesFile = TFGameRules()->GetCustomUpgradesFile();
	if ( TFGameRules() && pszCustomUpgradesFile && pszCustomUpgradesFile[0] )
	{
		pszPath = pszCustomUpgradesFile;
	}
#ifdef STAGING_ONLY
	else if ( TFGameRules() && !TFGameRules()->IsMannVsMachineMode() )
	{
		pszPath = "scripts/items/bountymode_upgrades.txt";
	}
#endif // STAGING_ONLY

	LoadUpgradesFileFromPath( pszPath );
}


//-----------------------------------------------------------------------------
// Purpose: Loads an upgrade file from a specific path
//-----------------------------------------------------------------------------
void CMannVsMachineUpgradeManager::LoadUpgradesFileFromPath( const char *pszPath )
{
	// Check that the path is valid
	const char *pszExtension = V_GetFileExtension( pszPath );
	if ( V_strstr( pszPath, ".." ) || V_strstr( pszPath, " " ) ||
		V_strstr( pszPath, "\r" ) || V_strstr( pszPath, "\n" ) ||
		V_strstr( pszPath, ":" ) || V_strstr( pszPath, "\\\\" ) ||
		V_IsAbsolutePath( pszPath ) ||
		pszExtension == NULL || V_strcmp( pszExtension, "txt" ) != 0 )
	{
		return;
	}

	KeyValues *pKV = new KeyValues( "Upgrades" );
	if ( !pKV->LoadFromFile( filesystem, pszPath, "MOD" ) )
	{
		Warning( "Can't open %s\n", pszPath );
		pKV->deleteThis();
		return;
	}

	m_Upgrades.RemoveAll();

	// Parse upgrades.txt
	ParseUpgradeBlockForUIGroup( pKV->FindKey( "ItemUpgrades" ), 0 );
	ParseUpgradeBlockForUIGroup( pKV->FindKey( "PlayerUpgrades" ), 1 );

	pKV->deleteThis();
}


int GetUpgradeStepData( CTFPlayer *pPlayer, int nWeaponSlot, int nUpgradeIndex, int &nCurrentStep, bool &bOverCap )
{
	if ( !pPlayer )
		return 0;

	// Get the item entity. We use the entity, not the item in the loadout, because we want
	// the dynamic attributes that have already been purchases and attached.
	CEconEntity *pEntity = NULL;
	const CEconItemView *pItemData = CTFPlayerSharedUtils::GetEconItemViewByLoadoutSlot( pPlayer, nWeaponSlot, &pEntity );

	const CMannVsMachineUpgrades *pMannVsMachineUpgrade = &( g_MannVsMachineUpgrades.m_Upgrades[ nUpgradeIndex ] );

	CEconItemAttributeDefinition *pAttribDef = ItemSystem()->GetStaticDataForAttributeByName( pMannVsMachineUpgrade->szAttrib );
	if ( !pAttribDef )
		return 0;

	// Special-case short-circuit logic for the powerup bottle. I don't know why we do this, but
	// we did before so this seems like the safest way of not breaking anything.
	const CTFPowerupBottle *pPowerupBottle = dynamic_cast< CTFPowerupBottle* >( pEntity );
	if ( pPowerupBottle )
	{
		Assert( pMannVsMachineUpgrade->nUIGroup == UIGROUP_POWERUPBOTTLE );

		nCurrentStep = ::FindAttribute( pItemData, pAttribDef )
					 ? pPowerupBottle->GetNumCharges()
					 : 0;
		bOverCap = nCurrentStep == pPowerupBottle->GetMaxNumCharges();
		
		return pPowerupBottle->GetMaxNumCharges();
	}

	Assert( pAttribDef->IsStoredAsFloat() );
	Assert( !pAttribDef->IsStoredAsInteger() );

	int nFormat = pAttribDef->GetDescriptionFormat();

	bool bPercentage = nFormat == ATTDESCFORM_VALUE_IS_PERCENTAGE || nFormat == ATTDESCFORM_VALUE_IS_INVERTED_PERCENTAGE;

	// Find the baseline value for this attribute. We start by assuming that it has no default value on
	// the item level (CEconItem) and defaulting to 100% for percentages and 0 for anything else.
	float flBase = bPercentage ? 1.0f : 0.0f;
	
	// If the item has a backing store, we pull from that to find the attribute value before any
	// gameplay-specific (CEconItemView-level) attribute modifications. If we're a player we don't have
	// any persistent backing store. This will either stomp our above value if found or leave it unchanged
	// if not found.
	if ( pItemData && pItemData->GetSOCData() )
	{
		::FindAttribute_UnsafeBitwiseCast<attrib_value_t>( pItemData->GetSOCData(), pAttribDef, &flBase );
	}

	// ...
	float flCurrentAttribValue = bPercentage ? 1.0f : 0.0f;
	
	if ( pMannVsMachineUpgrade->nUIGroup == UIGROUP_UPGRADE_ATTACHED_TO_PLAYER )
	{
		::FindAttribute_UnsafeBitwiseCast<attrib_value_t>( pPlayer->GetAttributeList(), pAttribDef, &flCurrentAttribValue );
	}
	else
	{
		Assert( pMannVsMachineUpgrade->nUIGroup == UIGROUP_UPGRADE_ATTACHED_TO_ITEM );
		if ( pItemData )
		{
			::FindAttribute_UnsafeBitwiseCast<attrib_value_t>( pItemData, pAttribDef, &flCurrentAttribValue );
		}
	}

	// ...
	const float flIncrement = pMannVsMachineUpgrade->flIncrement;
	
	// Figure out the cap value for this attribute. We start by trusting whatever is specified in our
	// upgrade config but if we're dealing with an item that specifies different properties at a level
	// before MvM upgrades (ie., the Soda Popper already specifies "Reload time decreased") then we
	// need to make sure we consider that the actual high end for UI purposes.
	const float flCap = pMannVsMachineUpgrade->flCap;

	if ( BIsAttributeValueWithDeltaOverCap( flCurrentAttribValue, flIncrement, flCap ) )
	{
		// Early out here -- we know we're over the cap already, so just fill out and return values
		// that show that.
		bOverCap = true;
		nCurrentStep = RoundFloatToInt( fabsf( ( flCurrentAttribValue - flBase ) / flIncrement ) );

		return nCurrentStep;			// Include the 0th step
	}

	// Calculate the the total number of upgrade levels and current upgrade level
	int nNumSteps = 0;
	
	// ...
	nNumSteps = RoundFloatToInt( fabsf( ( flCap - flBase ) / flIncrement ) );
	nCurrentStep = RoundFloatToInt( fabsf( ( flCurrentAttribValue - flBase ) / flIncrement ) );

	// Include the 0th step
	return nNumSteps;
}