Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
11 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CItemSelectionCriteria, which serves as a criteria for selection
  4. // of a econ item
  5. //
  6. //=============================================================================
  7. #ifndef ITEM_SELECTION_CRITERIA_H
  8. #define ITEM_SELECTION_CRITERIA_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. // Maximum string length in item create APIs
  13. const int k_cchCreateItemLen = 64;
  14. // Operators for BAddNewItemCriteria
  15. enum EItemCriteriaOperator
  16. {
  17. k_EOperator_String_EQ = 0, // Field is string equal to value
  18. k_EOperator_Not = 1, // Logical not
  19. k_EOperator_String_Not_EQ = 1, // Field is not string equal to value
  20. k_EOperator_Float_EQ = 2, // Field as a float is equal to value
  21. k_EOperator_Float_Not_EQ = 3, // Field as a float is not equal to value
  22. k_EOperator_Float_LT = 4, // Field as a float is less than value
  23. k_EOperator_Float_Not_LT = 5, // Field as a float is not less than value
  24. k_EOperator_Float_LTE = 6, // Field as a float is less than or equal value
  25. k_EOperator_Float_Not_LTE = 7, // Field as a float is not less than or equal value
  26. k_EOperator_Float_GT = 8, // Field as a float is greater than value
  27. k_EOperator_Float_Not_GT = 9, // Field as a float is not greater than value
  28. k_EOperator_Float_GTE = 10, // Field as a float is greater than or equal value
  29. k_EOperator_Float_Not_GTE = 11, // Field as a float is not greater than or equal value
  30. k_EOperator_Subkey_Contains = 12, // Field contains value as a subkey
  31. k_EOperator_Subkey_Not_Contains = 13, // Field does not contain value as a subkey
  32. // Must be last
  33. k_EItemCriteriaOperator_Count = 14,
  34. };
  35. EItemCriteriaOperator EItemCriteriaOperatorFromName( const char *pch );
  36. const char *PchNameFromEItemCriteriaOperator( int eItemCriteriaOperator );
  37. class CEconItemSchema;
  38. class CEconItemDefinition;
  39. class CSOItemCriteria;
  40. class CSOItemCriteriaCondition;
  41. //-----------------------------------------------------------------------------
  42. //-----------------------------------------------------------------------------
  43. //-----------------------------------------------------------------------------
  44. // CItemSelectionCriteria
  45. // A class that contains all the conditions a server needs to specify what
  46. // kind of random item they wish to generate.
  47. //-----------------------------------------------------------------------------
  48. class CItemSelectionCriteria
  49. {
  50. public:
  51. // Constructors and destructor
  52. CItemSelectionCriteria() :
  53. m_bItemLevelSet( false ),
  54. m_unItemLevel( 0 ),
  55. m_bQualitySet( false ),
  56. m_nItemQuality( k_unItemQuality_Any ),
  57. m_unInitialInventory( 0 ),
  58. m_bInitialQuantitySet( false ),
  59. m_unInitialQuantity( 1 ),
  60. m_bIgnoreEnabledFlag( false )
  61. {
  62. }
  63. CItemSelectionCriteria( const CItemSelectionCriteria &that );
  64. CItemSelectionCriteria &operator=( const CItemSelectionCriteria& rhs );
  65. ~CItemSelectionCriteria();
  66. // Accessors and Settors
  67. bool BItemLevelSet( void ) const { return m_bItemLevelSet; }
  68. uint32 GetItemLevel( void ) const { Assert( m_bItemLevelSet ); return m_unItemLevel; }
  69. void SetItemLevel( uint32 unLevel ) { m_unItemLevel = unLevel; m_bItemLevelSet = true; }
  70. bool BQualitySet( void ) const { return m_bQualitySet; }
  71. int32 GetQuality( void ) const { Assert( m_bQualitySet ); return m_nItemQuality; }
  72. void SetQuality( int32 nQuality ) { m_nItemQuality = nQuality; m_bQualitySet = true; }
  73. uint32 GetInitialInventory( void ) const { return m_unInitialInventory; }
  74. void SetInitialInventory( uint32 unInventory ) { m_unInitialInventory = unInventory; }
  75. bool BInitialQuantitySet( void ) const { return m_bQualitySet; }
  76. uint32 GetInitialQuantity( void ) const { Assert( m_bQualitySet ); return m_unInitialQuantity; }
  77. void SetInitialQuantity( uint32 unQuantity ) { m_unInitialQuantity = unQuantity; m_bInitialQuantitySet = true; }
  78. void SetIgnoreEnabledFlag( bool bIgnore ) { m_bIgnoreEnabledFlag = bIgnore; }
  79. // Tags
  80. void SetTags( const char *pszTags );
  81. // Add conditions to the criteria
  82. class ICondition
  83. {
  84. public:
  85. virtual ~ICondition() { }
  86. virtual bool BItemDefinitionPassesCriteria( const CEconItemDefinition *pItemDef ) const = 0;
  87. virtual EItemCriteriaOperator GetEOp() const { return k_EItemCriteriaOperator_Count; }
  88. virtual const char *GetField() const { return ""; }
  89. virtual const char *GetValue() const { return ""; }
  90. virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const { Assert( !"BSerializeToMsg() called on for unimplementing ICondition!" ); return false; }
  91. };
  92. bool BAddCondition( const char *pszField, EItemCriteriaOperator eOp, float flValue, bool bRequired );
  93. bool BAddCondition( const char *pszField, EItemCriteriaOperator eOp, const char * pszValue, bool bRequired );
  94. bool BAddCondition( ICondition *pCondition );
  95. int GetConditionsCount() { return m_vecConditions.Count(); }
  96. const char *GetValueForFirstConditionOfType( EItemCriteriaOperator eType ) const;
  97. const char *GetFieldForFirstConditionOfType( EItemCriteriaOperator eType ) const;
  98. // Alternate ways of initializing
  99. bool BInitFromKV( KeyValues *pKVCriteria );
  100. // Serializes the criteria to and from messages
  101. bool BSerializeToMsg( CSOItemCriteria & msg ) const;
  102. bool BDeserializeFromMsg( const CSOItemCriteria & msg );
  103. // Evaluates an item definition against this criteria. Returns true if
  104. // the definition passes the filter
  105. bool BEvaluate( const CEconItemDefinition* pItemDef ) const;
  106. // Validation
  107. #ifdef DBGFLAG_VALIDATE
  108. void Validate( CValidator &validator, const char *pchName );
  109. #endif
  110. private:
  111. //-----------------------------------------------------------------------------
  112. // CItemSelectionCriteria::CCondition
  113. // Represents one condition of the criteria
  114. //-----------------------------------------------------------------------------
  115. class CCondition : public ICondition
  116. {
  117. public:
  118. CCondition( const char *pszField, EItemCriteriaOperator eOp, bool bRequired )
  119. : m_sField( pszField ), m_EOp( eOp ), m_bRequired( bRequired )
  120. {
  121. }
  122. // ICondition interface.
  123. virtual bool BItemDefinitionPassesCriteria( const CEconItemDefinition *pItemDef ) const OVERRIDE;
  124. // Serializes the condition to the message
  125. virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;
  126. // Validation
  127. #ifdef DBGFLAG_VALIDATE
  128. virtual void Validate( CValidator &validator, const char *pchName );
  129. #endif
  130. EItemCriteriaOperator GetEOp( void ) const OVERRIDE { return m_EOp; }
  131. virtual const char *GetField( void ) const OVERRIDE { return m_sField.Get(); }
  132. virtual const char *GetValue( void ) const OVERRIDE { Assert(0); return NULL; }
  133. private:
  134. // Returns if the given KeyValues block passes this condition
  135. // Performs common checks and calls BInternalEvaluate
  136. bool BEvaluate( KeyValues *pKVItem ) const;
  137. protected:
  138. // Returns true if applying the element's operator on m_sField of
  139. // pKVItem returns true. This is only called if m_pszField exists in pKVItem
  140. virtual bool BInternalEvaluate( KeyValues *pKVItem ) const = 0;
  141. // The field of the raw KeyValue form of the item definition to check
  142. CUtlString m_sField;
  143. // The operator this clause uses
  144. EItemCriteriaOperator m_EOp;
  145. // When true, BEvaluate returns false if m_sField does not exist in pKVItem
  146. bool m_bRequired;
  147. };
  148. //-----------------------------------------------------------------------------
  149. // CItemSelectionCriteria::CStringCondition
  150. // CCondition that handles the string-based operators
  151. //-----------------------------------------------------------------------------
  152. class CStringCondition : public CCondition
  153. {
  154. public:
  155. CStringCondition( const char *pszField, EItemCriteriaOperator eOp, const char *pszValue, bool bRequired )
  156. : CCondition( pszField, eOp, bRequired ), m_sValue( pszValue )
  157. {
  158. }
  159. virtual ~CStringCondition( ) { }
  160. virtual const char *GetValue( void ) const OVERRIDE { return m_sValue.Get(); }
  161. // Validation
  162. #ifdef DBGFLAG_VALIDATE
  163. virtual void Validate( CValidator &validator, const char *pchName );
  164. #endif
  165. protected:
  166. virtual bool BInternalEvaluate( KeyValues *pKVItem ) const;
  167. virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;
  168. // The value to check against
  169. CUtlString m_sValue;
  170. };
  171. //-----------------------------------------------------------------------------
  172. // CItemSelectionCriteria::CFloatCondition
  173. // CCondition that handles the float-based operators
  174. //-----------------------------------------------------------------------------
  175. class CFloatCondition : public CCondition
  176. {
  177. public:
  178. CFloatCondition( const char *pszField, EItemCriteriaOperator eOp, float flValue, bool bRequired )
  179. : CCondition( pszField, eOp, bRequired ), m_flValue( flValue )
  180. {
  181. }
  182. virtual ~CFloatCondition( ) { }
  183. protected:
  184. virtual bool BInternalEvaluate( KeyValues *pKVItem ) const;
  185. virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;
  186. // The value to check against
  187. float m_flValue;
  188. };
  189. //-----------------------------------------------------------------------------
  190. // CItemSelectionCriteria::CSetCondition
  191. // CCondition that handles subkey checks
  192. //-----------------------------------------------------------------------------
  193. class CSetCondition : public CCondition
  194. {
  195. public:
  196. CSetCondition( const char *pszField, EItemCriteriaOperator eOp, const char *pszValue, bool bRequired )
  197. : CCondition( pszField, eOp, bRequired ), m_sValue( pszValue )
  198. {
  199. }
  200. virtual ~CSetCondition( ) { }
  201. // Validation
  202. #ifdef DBGFLAG_VALIDATE
  203. virtual void Validate( CValidator &validator, const char *pchName );
  204. #endif
  205. protected:
  206. virtual bool BInternalEvaluate( KeyValues *pKVItem ) const;
  207. virtual bool BSerializeToMsg( CSOItemCriteriaCondition & msg ) const;
  208. // The subkey to look for
  209. CUtlString m_sValue;
  210. };
  211. // True if item level is specified in this criteria
  212. bool m_bItemLevelSet;
  213. // The level of the item to generate
  214. uint32 m_unItemLevel;
  215. // True if quality is specified in this criteria
  216. bool m_bQualitySet;
  217. // The quality of the item to generate
  218. int32 m_nItemQuality;
  219. // The initial inventory token of the item
  220. uint32 m_unInitialInventory;
  221. // True if initial quantity is specified in this criteria.
  222. bool m_bInitialQuantitySet;
  223. // The initial quantity of the item
  224. uint32 m_unInitialQuantity;
  225. // Enforced explicit quality matching
  226. bool m_bForcedQualityMatch;
  227. // Ignoring enabled flag (used when crafting)
  228. bool m_bIgnoreEnabledFlag;
  229. // A list of tags
  230. CUtlString m_strTags;
  231. CUtlVector<econ_tag_handle_t> m_vecTags;
  232. // A list of the conditions
  233. CUtlVector<ICondition *> m_vecConditions;
  234. };
  235. #endif //ITEM_SELECTION_CRITERIA_H