summaryrefslogtreecommitdiff
path: root/game/shared/econ/item_selection_criteria.cpp
blob: c0f1f055172b27e2f577c705294e39d1b0750324 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: CItemSelectionCriteria, which serves as a criteria for selection
//			of a econ item
//
//=============================================================================


#include "cbase.h"
#include "item_selection_criteria.h"

#include "gcsdk/gcsystemmsgs.h"

#if defined(TF_CLIENT_DLL) || defined(TF_DLL)
#include "tf_gcmessages.h"
#endif

#include "gcsdk/enumutils.h"

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


// copied from \common\econ_item_view.h
#define AE_USE_SCRIPT_VALUE			9999		// Can't be -1, due to unsigned ints used on the backend

ENUMSTRINGS_START( EItemCriteriaOperator )
{ k_EOperator_String_EQ,		"string==" },
{ k_EOperator_String_Not_EQ,	"!string==" },
{ k_EOperator_Float_EQ,			"float==" },
{ k_EOperator_Float_Not_EQ,		"!float==" },
{ k_EOperator_Float_LT,			"float<" },
{ k_EOperator_Float_Not_LT,		"!float<" },
{ k_EOperator_Float_LTE,		"float<=" },
{ k_EOperator_Float_Not_LTE,	"!float<=" },
{ k_EOperator_Float_GT,			"float>" },
{ k_EOperator_Float_Not_GT,		"!float>" },
{ k_EOperator_Float_GTE,		"float>=" },
{ k_EOperator_Float_Not_GTE,	"!float>=" },
{ k_EOperator_Subkey_Contains,	"contains" },
{ k_EOperator_Subkey_Not_Contains, "!contains" },
ENUMSTRINGS_REVERSE( EItemCriteriaOperator, k_EItemCriteriaOperator_Count )

using namespace GCSDK;

//-----------------------------------------------------------------------------
// Purpose: Copy Constructor
//-----------------------------------------------------------------------------
CItemSelectionCriteria::CItemSelectionCriteria( const CItemSelectionCriteria &that )
{
	(*this) = that;
}


//-----------------------------------------------------------------------------
// Purpose: Operator=
//-----------------------------------------------------------------------------
CItemSelectionCriteria &CItemSelectionCriteria::operator=( const CItemSelectionCriteria &rhs )
{

	// Leverage the serialization code we already have for the copy
	CSOItemCriteria msgTemp;
	rhs.BSerializeToMsg( msgTemp );
	BDeserializeFromMsg( msgTemp );

	return *this;
}


//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CItemSelectionCriteria::~CItemSelectionCriteria( void )
{
	m_vecConditions.PurgeAndDeleteElements();
}

//-----------------------------------------------------------------------------
// Purpose: Look through our conditions and find the first of the specified type, 
//			and return the value it's looking for.
//-----------------------------------------------------------------------------
const char *CItemSelectionCriteria::GetValueForFirstConditionOfType( EItemCriteriaOperator eType ) const
{
	// Only supporting this for string conditions right now
	Assert( eType == k_EOperator_String_EQ || eType == k_EOperator_String_Not_EQ );

	FOR_EACH_VEC( m_vecConditions, i )
	{
		if ( m_vecConditions[i]->GetEOp() == eType )
			return m_vecConditions[i]->GetValue();
	}

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Look through our conditions and find the first of the specified type, 
//			and return the value it's looking for.
//-----------------------------------------------------------------------------
const char *CItemSelectionCriteria::GetFieldForFirstConditionOfType( EItemCriteriaOperator eType ) const
{
	FOR_EACH_VEC( m_vecConditions, i )
	{
		if ( m_vecConditions[i]->GetEOp() == eType )
			return m_vecConditions[i]->GetField();
	}

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Initialize from a KV structure
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BInitFromKV( KeyValues *pKVCriteria )
{
	// Read in the base fields
	if ( pKVCriteria->FindKey( "level" ) )
	{
		SetItemLevel( pKVCriteria->GetInt( "level" ) );
	}

	if ( pKVCriteria->FindKey( "quality" ) )
	{
		uint8 nQuality;
		if ( !GetItemSchema()->BGetItemQualityFromName( pKVCriteria->GetString( "quality" ), &nQuality ) )
			return false;

		SetQuality( nQuality );
	}

	if ( pKVCriteria->FindKey( "inventoryPos" ) )
	{
		SetInitialInventory( pKVCriteria->GetInt( "inventoryPos" ) );
	}

	if ( pKVCriteria->FindKey( "quantity" ) )
	{
		SetInitialQuantity( pKVCriteria->GetInt( "quantity" ) );
	}

	if ( pKVCriteria->FindKey( "ignore_enabled" ) )
	{
		SetIgnoreEnabledFlag( pKVCriteria->GetBool( "ignore_enabled" ) );
	}

	if ( pKVCriteria->FindKey( "tags" ) )
	{
		SetTags( pKVCriteria->GetString( "tags" ) );
	}

	KeyValues *pKVConditions = pKVCriteria->FindKey( "conditions", true );

	FOR_EACH_TRUE_SUBKEY( pKVConditions, pKVElement )
	{
		// Check for required fields
		if ( !pKVElement->FindKey( "field" ) ||
			!pKVElement->FindKey( "operator" ) ||
			!pKVElement->FindKey( "value" ) )
			return false;

		const char *pszField = pKVElement->GetString( "field" );
		bool bRequired = pKVElement->GetBool( "required" );
		const char *pszValue = pKVElement->GetString( "value" );

		// Get the operator
		const char *pszOperator = pKVElement->GetString( "operator" );
		EItemCriteriaOperator eOp = EItemCriteriaOperatorFromName( pszOperator );
		if ( k_EItemCriteriaOperator_Count == eOp )
			return false;

		BAddCondition( pszField, eOp, pszValue, bRequired );
	}

	return true;
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CItemSelectionCriteria::SetTags( const char *pszTags )
{
	m_vecTags.Purge();

	m_strTags = pszTags;
	CSplitString splitString( pszTags, " " );
	for ( int i=0; i<splitString.Count(); ++i )
	{
		econ_tag_handle_t tagHandle = GetItemSchema()->GetHandleForTag( splitString[i] );
		if ( !m_vecTags.HasElement( tagHandle ) )
		{
			m_vecTags.AddToTail( tagHandle );
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BAddCondition( CItemSelectionCriteria::ICondition *pCondition )
{
	CPlainAutoPtr<ICondition> pConditionPtr( pCondition );

	// Check for condition limit
	if ( UCHAR_MAX == GetConditionsCount() )
	{		
		AssertMsg( false, "Too many conditions on a a CItemSelectionCriteria. Max: 255" );
		return false;
	}

	m_vecConditions.AddToTail( pConditionPtr.Detach() );
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Adds a condition to the selection criteria
// Input:	pszField - Field to evaluate on
//			eOp - Operator to apply to the value of the field
//			flValue - The value to compare.
//			bRequired - When true, causes BEvauluate to fail if pszField doesn't
//				exist in the KV being checked.
// Output:	True if the condition was added, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BAddCondition( const char *pszField, EItemCriteriaOperator eOp, float flValue, bool bRequired )
{
	// Enforce maximum string lengths
	if ( Q_strlen( pszField ) >= k_cchCreateItemLen )
		return false;

	// Create the appropriate condition for the operator
	switch ( eOp )
	{
	case k_EOperator_Float_EQ:
	case k_EOperator_Float_Not_EQ:
	case k_EOperator_Float_LT:
	case k_EOperator_Float_Not_LT:
	case k_EOperator_Float_LTE:
	case k_EOperator_Float_Not_LTE:
	case k_EOperator_Float_GT:
	case k_EOperator_Float_Not_GT:
	case k_EOperator_Float_GTE:
	case k_EOperator_Float_Not_GTE:
		return BAddCondition( new CFloatCondition( pszField, eOp, flValue, bRequired ) );

	default:
		AssertMsg1( false, "Bad operator (%d) passed to BAddCondition. Float based operator required for this overload.", eOp );
		return false;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Adds a condition to the selection criteria
// Input:	pszField - Field to evaluate on
//			eOp - Operator to apply to the value of the field
//			pszValue - The value to compare.
//			bRequired - When true, causes BEvauluate to fail if pszField doesn't
//				exist in the KV being checked.
// Output:	True if the condition was added, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BAddCondition( const char *pszField, EItemCriteriaOperator eOp, const char * pszValue, bool bRequired )
{
	// Enforce maximum string lengths
	if ( Q_strlen( pszField ) >= k_cchCreateItemLen || Q_strlen( pszValue ) >= k_cchCreateItemLen )
		return false;

	// Create the appropriate condition for the operator
	switch ( eOp )
	{
	case k_EOperator_String_EQ:
	case k_EOperator_String_Not_EQ:
		return BAddCondition( new CStringCondition( pszField, eOp, pszValue, bRequired ) );
		return true;

	case k_EOperator_Subkey_Contains:
	case k_EOperator_Subkey_Not_Contains:
		return BAddCondition( new CSetCondition( pszField, eOp, pszValue, bRequired ) );
		return true;

	default:
		// Try the float operators
		return BAddCondition( pszField, eOp, Q_atof( pszValue ), bRequired );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Checks if a given item matches the item selection criteria
// Input:	itemDef - The item definition to evaluate against
// Output:	True is the item passes the filter, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BEvaluate( const CEconItemDefinition* pItemDef ) const
{
	// Disabled items never match
	if ( !m_bIgnoreEnabledFlag && !pItemDef->BEnabled() )
		return false;

	// Filter against level
	if ( BItemLevelSet() && (GetItemLevel() != AE_USE_SCRIPT_VALUE) &&
		( GetItemLevel() < pItemDef->GetMinLevel() || GetItemLevel() > pItemDef->GetMaxLevel() ) )
		return false;

	// Filter against quality
	if ( BQualitySet() && (GetQuality() != AE_USE_SCRIPT_VALUE) )
	{
		if ( GetQuality() != pItemDef->GetQuality() )
		{
			// Filter out item defs that have a non-any quality if we have a non-matching & non-any quality criteria
			if ( k_unItemQuality_Any != GetQuality() && k_unItemQuality_Any != pItemDef->GetQuality() )
				return false;
		}
	}

	// Filter against the additional conditions
	FOR_EACH_VEC( m_vecConditions, i )
	{
		if ( !m_vecConditions[i]->BItemDefinitionPassesCriteria( pItemDef ) )
			return false;
	}

	// Check if we have "any" tags
	if ( m_vecTags.Count() > 0 )
	{
		bool bHasTag = false;
		FOR_EACH_VEC( m_vecTags, i )
		{
			if ( pItemDef->HasEconTag( m_vecTags[i] ) )
			{
				bHasTag = true;
				break;
			}
		}

		if ( !bHasTag )
		{
			return false;
		}
	}

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Determines if the item matches this condition of the criteria
// Input:	pKVItem - Pointer to the raw KeyValues definition of the item
// Output:	True is the item matches, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::CCondition::BEvaluate( KeyValues *pKVItem ) const
{
	KeyValues *pKVField = pKVItem->FindKey( m_sField.String() );

	// Treat an empty string as a missing field as well.
	bool bIsEmptyString = false;
	if ( m_EOp == k_EOperator_String_EQ || m_EOp == k_EOperator_String_Not_EQ )
	{
		const char *pszItemVal = pKVField ? pKVField->GetString() : NULL;
		bIsEmptyString = ( pszItemVal == NULL || pszItemVal[0] == '\0' );
	}

	// Deal with missing fields
	if ( NULL == pKVField || bIsEmptyString )
	{
		if ( m_bRequired )
			return false;
		else
			return true;
	}

	// Run the operator specific check
	bool bRet = BInternalEvaluate( pKVItem );

	// If this is a "not" operator, reverse the result 
	if ( m_EOp & k_EOperator_Not )
		return !bRet;
	else
		return bRet;
}


//-----------------------------------------------------------------------------
// Purpose: Runs the operator specific check for this condition
// Input:	pKVItem - Pointer to the raw KeyValues definition of the item
// Output:	True is the item matches, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::CStringCondition::BInternalEvaluate( KeyValues *pKVItem ) const
{
	Assert( k_EOperator_String_EQ == m_EOp || k_EOperator_String_Not_EQ == m_EOp );
	if( !( k_EOperator_String_EQ == m_EOp || k_EOperator_String_Not_EQ == m_EOp ) )
		return false;

	const char *pszItemVal = pKVItem->GetString( m_sField.String() );
	return ( 0 == Q_stricmp( m_sValue.String(), pszItemVal ) );
}

bool CItemSelectionCriteria::CStringCondition::BSerializeToMsg( CSOItemCriteriaCondition & msg ) const
{
	CCondition::BSerializeToMsg( msg );
	msg.set_string_value( m_sValue.Get() );
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Runs the operator specific check for this condition
// Input:	pKVItem - Pointer to the raw KeyValues definition of the item
// Output:	True is the item matches, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::CSetCondition::BInternalEvaluate( KeyValues *pKVItem ) const
{
	Assert( k_EOperator_Subkey_Contains == m_EOp || k_EOperator_Subkey_Not_Contains == m_EOp );
	if( !( k_EOperator_Subkey_Contains == m_EOp || k_EOperator_Subkey_Not_Contains == m_EOp ) )
		return false;

	return ( NULL != pKVItem->FindKey( m_sField.String() )->FindKey( m_sValue.String() ) );
}

bool CItemSelectionCriteria::CSetCondition::BSerializeToMsg( CSOItemCriteriaCondition & msg ) const
{
	CCondition::BSerializeToMsg( msg );
	msg.set_string_value( m_sValue.Get() );
	return true;
}

 
//-----------------------------------------------------------------------------
// Purpose: Runs the operator specific check for this condition
// Input:	pKVItem - Pointer to the raw KeyValues definition of the item
// Output:	True is the item matches, false otherwise
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::CFloatCondition::BInternalEvaluate( KeyValues *pKVItem ) const
{
	float itemValue = pKVItem->GetFloat( m_sField.String() );

	switch ( m_EOp )
	{
	case k_EOperator_Float_EQ:
	case k_EOperator_Float_Not_EQ:
		return ( itemValue == m_flValue );

	case k_EOperator_Float_LT:
	case k_EOperator_Float_Not_LT:
		return ( itemValue < m_flValue );

	case k_EOperator_Float_LTE:
	case k_EOperator_Float_Not_LTE:
		return ( itemValue <= m_flValue );

	case k_EOperator_Float_GT:
	case k_EOperator_Float_Not_GT:
		return ( itemValue > m_flValue );

	case k_EOperator_Float_GTE:
	case k_EOperator_Float_Not_GTE:
		return ( itemValue >= m_flValue );

	default:
		AssertMsg1( false, "Unknown operator: %d", m_EOp );
		return false;
	}
}

bool CItemSelectionCriteria::CFloatCondition::BSerializeToMsg( CSOItemCriteriaCondition & msg ) const
{
	CCondition::BSerializeToMsg( msg );
	msg.set_float_value( m_flValue );
	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Serialize the item selection criteria to the given message
// Input:	msg - The message to serialize to.
// Output:	True if the operation was successful, false otherwise.
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BSerializeToMsg( CSOItemCriteria & msg ) const
{
	msg.set_item_level( m_unItemLevel );
	msg.set_item_quality( m_nItemQuality );
	msg.set_item_level_set( m_bItemLevelSet );
	msg.set_item_quality_set( m_bQualitySet );
	msg.set_initial_inventory( m_unInitialInventory );
	msg.set_initial_quantity( m_unInitialQuantity );
	msg.set_ignore_enabled_flag( m_bIgnoreEnabledFlag );
	msg.set_tags( m_strTags );

	FOR_EACH_VEC( m_vecConditions, i )
	{
		CSOItemCriteriaCondition *pConditionMsg = msg.add_conditions();
		m_vecConditions[i]->BSerializeToMsg( *pConditionMsg );
	}
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Deserializes the item selection criteria from the given message
// Input:	msg - The message to deserialize from.
// Output:	True if the operation was successful, false otherwise.
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::BDeserializeFromMsg( const CSOItemCriteria & msg )
{
	m_unItemLevel = msg.item_level();
	m_nItemQuality = msg.item_quality();
	m_bItemLevelSet = msg.item_level_set();
	m_bQualitySet = msg.item_quality_set();
	m_unInitialInventory = msg.initial_inventory();
	m_unInitialQuantity = msg.initial_quantity();
	m_bIgnoreEnabledFlag = msg.ignore_enabled_flag();

	SetTags( msg.tags().c_str() );

	uint32 unCount = msg.conditions_size();
	m_vecConditions.EnsureCapacity( unCount );

	for ( uint32 i = 0; i < unCount; i++ )
	{
		const CSOItemCriteriaCondition & cond = msg.conditions( i );
		EItemCriteriaOperator eOp = (EItemCriteriaOperator)cond.op();
		bool bRequired = cond.required();

		// Read the value specific to the condition and add the condition
		switch ( eOp )
		{
		case k_EOperator_Float_EQ:
		case k_EOperator_Float_Not_EQ:
		case k_EOperator_Float_LT:
		case k_EOperator_Float_Not_LT:
		case k_EOperator_Float_LTE:
		case k_EOperator_Float_Not_LTE:
		case k_EOperator_Float_GT:
		case k_EOperator_Float_Not_GT:
		case k_EOperator_Float_GTE:
		case k_EOperator_Float_Not_GTE:
			{
				if ( !BAddCondition( cond.field().c_str(), eOp, cond.float_value(), bRequired ) )	return false;
				break;
			}

		case k_EOperator_String_EQ:
		case k_EOperator_String_Not_EQ:
		case k_EOperator_Subkey_Contains:
		case k_EOperator_Subkey_Not_Contains:
			{
				if ( !BAddCondition( cond.field().c_str(), eOp, cond.string_value().c_str(), bRequired ) )	return false;
				break;
			}

		default:
			AssertMsg1( false, "Unknown operator (%d) read.", eOp );
			return false;
		}
	}


	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Serializes a condition to a message.
// Input:	msg - The message to serialize to.
// Output:	True if the operation was successful, false otherwise.
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::CCondition::BSerializeToMsg( CSOItemCriteriaCondition & msg ) const
{
	msg.set_op( m_EOp );
	msg.set_field( m_sField.String() );
	msg.set_required( m_bRequired );
	return true;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CItemSelectionCriteria::CCondition::BItemDefinitionPassesCriteria( const CEconItemDefinition *pItemDef ) const
{
	return BEvaluate( pItemDef->GetRawDefinition() );
}

// Validation
#ifdef DBGFLAG_VALIDATE

//-----------------------------------------------------------------------------
// Purpose: Run a global validation pass on all of our data structures and memory
//			allocations.
// Input:	validator -		Our global validator object
//			pchName -		Our name (typically a member var in our container)
//-----------------------------------------------------------------------------
void CItemSelectionCriteria::Validate( CValidator &validator, const char *pchName )
{
	VALIDATE_SCOPE();
	ValidateObj( m_vecConditions );
	FOR_EACH_VEC( m_vecConditions, i )
	{
		ValidatePtr( m_vecConditions[i] );
	}
}

void CItemSelectionCriteria::CCondition::Validate( CValidator &validator, const char *pchName )
{
	ValidateObj( m_sField );
}

void CItemSelectionCriteria::CStringCondition::Validate( CValidator &validator, const char *pchName )
{
	CCondition::Validate( validator, pchName );
	ValidateObj( m_sValue );
}

void CItemSelectionCriteria::CSetCondition::Validate( CValidator &validator, const char *pchName )
{
	CCondition::Validate( validator, pchName );
	ValidateObj( m_sValue );
}

#endif