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

#ifndef ITEM_SELECTION_CRITERIA_H
#define ITEM_SELECTION_CRITERIA_H
#ifdef _WIN32
#pragma once
#endif

// Maximum string length in item create APIs
const int k_cchCreateItemLen	= 64;	

// Operators for BAddNewItemCriteria
enum EItemCriteriaOperator
{
	k_EOperator_String_EQ = 0,				// Field is string equal to value
	k_EOperator_Not = 1,					// Logical not
	k_EOperator_String_Not_EQ = 1,			// Field is not string equal to value
	k_EOperator_Float_EQ = 2,				// Field as a float is equal to value
	k_EOperator_Float_Not_EQ = 3,			// Field as a float is not equal to value
	k_EOperator_Float_LT = 4,				// Field as a float is less than value
	k_EOperator_Float_Not_LT = 5,			// Field as a float is not less than value
	k_EOperator_Float_LTE = 6,				// Field as a float is less than or equal value
	k_EOperator_Float_Not_LTE = 7,			// Field as a float is not less than or equal value
	k_EOperator_Float_GT = 8,				// Field as a float is greater than value
	k_EOperator_Float_Not_GT = 9,			// Field as a float is not greater than value
	k_EOperator_Float_GTE = 10,				// Field as a float is greater than or equal value
	k_EOperator_Float_Not_GTE = 11,			// Field as a float is not greater than or equal value
	k_EOperator_Subkey_Contains = 12,		// Field contains value as a subkey
	k_EOperator_Subkey_Not_Contains = 13,	// Field does not contain value as a subkey

	// Must be last
	k_EItemCriteriaOperator_Count = 14,
};


EItemCriteriaOperator EItemCriteriaOperatorFromName( const char *pch );
const char *PchNameFromEItemCriteriaOperator( int eItemCriteriaOperator );

class CEconItemSchema;
class CEconItemDefinition;
class CSOItemCriteria;
class CSOItemCriteriaCondition;

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// CItemSelectionCriteria
// A class that contains all the conditions a server needs to specify what
// kind of random item they wish to generate.
//-----------------------------------------------------------------------------
class CItemSelectionCriteria
{
public:
	// Constructors and destructor
	CItemSelectionCriteria() : 
	  m_bItemLevelSet( false ),
		  m_unItemLevel( 0 ),
		  m_bQualitySet( false ),
		  m_nItemQuality( k_unItemQuality_Any ), 
		  m_unInitialInventory( 0 ),
		  m_bInitialQuantitySet( false ),
		  m_unInitialQuantity( 1 ),
		  m_bIgnoreEnabledFlag( false )
	  { 
	  }

	  CItemSelectionCriteria( const CItemSelectionCriteria &that );
	  CItemSelectionCriteria &operator=( const CItemSelectionCriteria& rhs );
	  ~CItemSelectionCriteria();

	  // Accessors and Settors
	  bool			BItemLevelSet( void ) const					{ return m_bItemLevelSet; }
	  uint32		GetItemLevel( void ) const					{ Assert( m_bItemLevelSet ); return m_unItemLevel; }
	  void			SetItemLevel( uint32 unLevel )				{ m_unItemLevel = unLevel; m_bItemLevelSet = true; }
	  bool			BQualitySet( void ) const					{ return m_bQualitySet; }
	  int32			GetQuality( void ) const					{ Assert( m_bQualitySet ); return m_nItemQuality; }
	  void			SetQuality( int32 nQuality )				{ m_nItemQuality = nQuality; m_bQualitySet = true; }
	  uint32		GetInitialInventory( void ) const			{ return m_unInitialInventory; }
	  void			SetInitialInventory( uint32 unInventory )	{ m_unInitialInventory = unInventory; }
	  bool			BInitialQuantitySet( void ) const			{ return m_bQualitySet; }
	  uint32		GetInitialQuantity( void ) const			{ Assert( m_bQualitySet ); return m_unInitialQuantity; }
	  void			SetInitialQuantity( uint32 unQuantity )		{ m_unInitialQuantity = unQuantity; m_bInitialQuantitySet = true; }
	  void			SetIgnoreEnabledFlag( bool bIgnore )		{ m_bIgnoreEnabledFlag = bIgnore; }

	  // Tags
	  void			SetTags( const char *pszTags );


	  // Add conditions to the criteria
	  class ICondition
	  {
		public:
			virtual ~ICondition() { }

			virtual bool BItemDefinitionPassesCriteria( const CEconItemDefinition *pItemDef ) const = 0;

			virtual EItemCriteriaOperator GetEOp() const { return k_EItemCriteriaOperator_Count; }
			virtual const char *GetField() const { return ""; }
			virtual const char *GetValue() const { return ""; }

			virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const { Assert( !"BSerializeToMsg() called on for unimplementing ICondition!" ); return false; }
	  };

	  bool			BAddCondition( const char *pszField, EItemCriteriaOperator eOp, float flValue, bool bRequired );
	  bool			BAddCondition( const char *pszField, EItemCriteriaOperator eOp, const char * pszValue, bool bRequired );
	  bool			BAddCondition( ICondition *pCondition );
	  int			GetConditionsCount() { return m_vecConditions.Count(); }
	  const char	*GetValueForFirstConditionOfType( EItemCriteriaOperator eType ) const;
	  const char	*GetFieldForFirstConditionOfType( EItemCriteriaOperator eType ) const;

	  // Alternate ways of initializing
	  bool			BInitFromKV( KeyValues *pKVCriteria );

	  // Serializes the criteria to and from messages
	  bool			BSerializeToMsg( CSOItemCriteria & msg ) const;
	  bool			BDeserializeFromMsg( const CSOItemCriteria & msg );

	  // Evaluates an item definition against this criteria. Returns true if 
	  // the definition passes the filter
	  bool			BEvaluate( const CEconItemDefinition* pItemDef ) const;

	  // Validation
#ifdef DBGFLAG_VALIDATE
	  void Validate( CValidator &validator, const char *pchName );
#endif

private:
	//-----------------------------------------------------------------------------
	// CItemSelectionCriteria::CCondition
	// Represents one condition of the criteria
	//-----------------------------------------------------------------------------
	class CCondition : public ICondition
	{
	public:
		CCondition( const char *pszField, EItemCriteriaOperator eOp, bool bRequired )
			: m_sField( pszField ), m_EOp( eOp ), m_bRequired( bRequired )
		{
		}

		// ICondition interface.
		virtual bool BItemDefinitionPassesCriteria( const CEconItemDefinition *pItemDef ) const OVERRIDE;

		// Serializes the condition to the message
		virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;

		// Validation
#ifdef DBGFLAG_VALIDATE
		virtual void Validate( CValidator &validator, const char *pchName );
#endif

		EItemCriteriaOperator	GetEOp( void ) const OVERRIDE { return m_EOp; }
		virtual	const char		*GetField( void ) const OVERRIDE  { return m_sField.Get(); }
		virtual	const char		*GetValue( void ) const OVERRIDE  { Assert(0); return NULL; }

	private:
		// Returns if the given KeyValues block passes this condition 
		// Performs common checks and calls BInternalEvaluate
		bool BEvaluate( KeyValues *pKVItem ) const;

	protected:
		// Returns true if applying the element's operator on m_sField of
		// pKVItem returns true. This is only called if m_pszField exists in pKVItem
		virtual bool BInternalEvaluate( KeyValues *pKVItem ) const = 0;

		// The field of the raw KeyValue form of the item definition to check
		CUtlString				m_sField;
		// The operator this clause uses
		EItemCriteriaOperator	m_EOp;
		// When true, BEvaluate returns false if m_sField does not exist in pKVItem
		bool					m_bRequired;
	};


	//-----------------------------------------------------------------------------
	// CItemSelectionCriteria::CStringCondition
	// CCondition that handles the string-based operators
	//-----------------------------------------------------------------------------
	class CStringCondition : public CCondition
	{
	public:
		CStringCondition( const char *pszField, EItemCriteriaOperator eOp, const char *pszValue, bool bRequired )
			: CCondition( pszField, eOp, bRequired ), m_sValue( pszValue )
		{
		}

		virtual ~CStringCondition( ) { }

		virtual	const char		*GetValue( void ) const OVERRIDE { return m_sValue.Get(); }

		// Validation
#ifdef DBGFLAG_VALIDATE
		virtual void Validate( CValidator &validator, const char *pchName );
#endif

	protected:
		virtual bool BInternalEvaluate( KeyValues *pKVItem ) const;
		virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;

		// The value to check against
		CUtlString		m_sValue;
	};


	//-----------------------------------------------------------------------------
	// CItemSelectionCriteria::CFloatCondition
	// CCondition that handles the float-based operators
	//-----------------------------------------------------------------------------
	class CFloatCondition : public CCondition
	{
	public:
		CFloatCondition( const char *pszField, EItemCriteriaOperator eOp, float flValue, bool bRequired )
			: CCondition( pszField, eOp, bRequired ), m_flValue( flValue )
		{
		}

		virtual ~CFloatCondition( ) { }

	protected:
		virtual bool BInternalEvaluate( KeyValues *pKVItem ) const;
		virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;

		// The value to check against
		float				m_flValue;
	};


	//-----------------------------------------------------------------------------
	// CItemSelectionCriteria::CSetCondition
	// CCondition that handles subkey checks
	//-----------------------------------------------------------------------------
	class CSetCondition : public CCondition
	{
	public:
		CSetCondition( const char *pszField, EItemCriteriaOperator eOp, const char *pszValue, bool bRequired )
			: CCondition( pszField, eOp, bRequired ), m_sValue( pszValue )
		{
		}

		virtual ~CSetCondition( ) { }

		// Validation
#ifdef DBGFLAG_VALIDATE
		virtual void Validate( CValidator &validator, const char *pchName );
#endif

	protected:
		virtual bool BInternalEvaluate( KeyValues *pKVItem ) const;

		virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;

		// The subkey to look for
		CUtlString			m_sValue;
	};

	// True if item level is specified in this criteria
	bool			m_bItemLevelSet;
	// The level of the item to generate
	uint32			m_unItemLevel;
	// True if quality is specified in this criteria
	bool			m_bQualitySet;
	// The quality of the item to generate
	int32			m_nItemQuality;
	// The initial inventory token of the item
	uint32			m_unInitialInventory;
	// True if initial quantity is specified in this criteria.
	bool			m_bInitialQuantitySet;
	// The initial quantity of the item
	uint32			m_unInitialQuantity;
	// Enforced explicit quality matching
	bool			m_bForcedQualityMatch;
	// Ignoring enabled flag (used when crafting)
	bool			m_bIgnoreEnabledFlag;

	// A list of tags
	CUtlString		m_strTags;
	CUtlVector<econ_tag_handle_t>	m_vecTags;

	// A list of the conditions
	CUtlVector<ICondition *>	m_vecConditions;
};


#endif //ITEM_SELECTION_CRITERIA_H