Counter Strike : Global Offensive Source Code
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.

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