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.

455 lines
18 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef ECON_ITEM_CONSTANTS_H
  7. #define ECON_ITEM_CONSTANTS_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "game_item_schema.h"
  12. #include "econ_item_constants.h"
  13. #include "localization_provider.h"
  14. #include "econ_item_interface.h"
  15. #include "econ_item.h"
  16. #if defined(CLIENT_DLL)
  17. #include "iclientrenderable.h"
  18. #endif
  19. #if defined(TF_DLL)
  20. #include "tf_item_schema.h"
  21. #endif
  22. #if defined(CLIENT_DLL)
  23. #define CEconItemView C_EconItemView
  24. #endif
  25. #if defined(GC_DLL)
  26. #error "econ_item_view.h is not intended to be built on the GC!"
  27. #endif
  28. #if defined(TF_DLL) || defined(TF_CLIENT_DLL)
  29. #define ENABLE_ATTRIBUTE_CURRENCY_TRACKING 1
  30. #else
  31. #define ENABLE_ATTRIBUTE_CURRENCY_TRACKING 0
  32. #endif
  33. class CEconItemAttribute;
  34. class CAttributeManager;
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //-----------------------------------------------------------------------------
  38. class CAttributeList
  39. {
  40. friend class CEconItemView;
  41. friend class CTFPlayer;
  42. DECLARE_CLASS_NOBASE( CAttributeList );
  43. public:
  44. DECLARE_EMBEDDED_NETWORKVAR();
  45. DECLARE_DATADESC();
  46. CAttributeList();
  47. void operator=( const CAttributeList &src );
  48. void Init();
  49. void SetManager( CAttributeManager *pManager );
  50. void IterateAttributes( class IEconItemAttributeIterator *pIterator ) const;
  51. // Remove all attributes on this item
  52. void DestroyAllAttributes( void );
  53. void AddAttribute( CEconItemAttribute *pAttribute );
  54. // Remove an attribute by name
  55. void RemoveAttribute( const CEconItemAttributeDefinition *pAttrDef );
  56. void RemoveAttributeByIndex( int iIndex );
  57. public:
  58. // Returns the attribute that matches the attribute defname
  59. const CEconItemAttribute *GetAttributeByName( const char *pszAttribDefName ) const;
  60. // Returns the attribute that matches the attribute id
  61. const CEconItemAttribute *GetAttributeByID( int iAttributeID ) const;
  62. // The only way to set the value of an attribute after its creation is through the attribute list
  63. // that contains it. This way the matching attribute manager is told one of its attributes has changed.
  64. void SetRuntimeAttributeValue( const CEconItemAttributeDefinition *pAttrDef, float flValue );
  65. #if ENABLE_ATTRIBUTE_CURRENCY_TRACKING
  66. void SetRuntimeAttributeRefundableCurrency( const CEconItemAttributeDefinition *pAttrDef, int iRefundableCurrency );
  67. int GetRuntimeAttributeRefundableCurrency( const CEconItemAttributeDefinition *pAttrDef ) const;
  68. void AdjustRuntimeAttributeRefundableCurrency( const CEconItemAttributeDefinition *pAttrDef, int iRefundableCurrencyAdjustment )
  69. {
  70. SetRuntimeAttributeRefundableCurrency( pAttrDef, GetRuntimeAttributeRefundableCurrency( pAttrDef ) + iRefundableCurrencyAdjustment );
  71. }
  72. #endif // ENABLE_ATTRIBUTE_CURRENCY_TRACKING
  73. private:
  74. void NotifyManagerOfAttributeValueChanges();
  75. // Attribute accessing
  76. int GetNumAttributes( void ) const { return m_Attributes.Count(); }
  77. CEconItemAttribute *GetAttribute( int iIndex ) { Assert( iIndex >= 0 && iIndex < m_Attributes.Count()); return &m_Attributes[iIndex]; }
  78. const CEconItemAttribute *GetAttribute( int iIndex ) const { Assert( iIndex >= 0 && iIndex < m_Attributes.Count()); return &m_Attributes[iIndex]; }
  79. // Our list of attributes
  80. CUtlVector<CEconItemAttribute> m_Attributes;
  81. CAttributeManager *m_pManager;
  82. };
  83. //-----------------------------------------------------------------------------
  84. // Purpose: An attribute that knows how to read itself from a datafile, describe itself to the user,
  85. // and serialize itself between Servers, Clients, and Steam.
  86. // Unlike the attributes created in the Game DLL, this attribute doesn't know how to actually
  87. // do anything in the game, it just knows how to describe itself.
  88. //-----------------------------------------------------------------------------
  89. class CEconItemAttribute
  90. {
  91. DECLARE_CLASS_NOBASE( CEconItemAttribute );
  92. public:
  93. DECLARE_EMBEDDED_NETWORKVAR();
  94. CEconItemAttribute();
  95. CEconItemAttribute( const attrib_definition_index_t iAttributeIndex, float flValue );
  96. CEconItemAttribute( const attrib_definition_index_t iAttributeIndex, uint32 unValue );
  97. void operator=( const CEconItemAttribute &val );
  98. // Get the index of this attribute's definition inside the script file
  99. attrib_definition_index_t GetAttribIndex( void ) const { return m_iAttributeDefinitionIndex; }
  100. void SetAttribIndex( attrib_definition_index_t iIndex ) { m_iAttributeDefinitionIndex = iIndex; }
  101. // Get the static data contained in this attribute's definition
  102. const CEconItemAttributeDefinition *GetStaticData( void ) const;
  103. // Get the float value of this attribute.
  104. //float GetValue( void ) const;
  105. #if ENABLE_ATTRIBUTE_CURRENCY_TRACKING
  106. int GetRefundableCurrency( void ) const { return m_nRefundableCurrency; }
  107. #endif // ENABLE_ATTRIBUTE_CURRENCY_TRACKING
  108. private:
  109. // The only way to set the value of an attribute after its creation is through the attribute list
  110. // that contains it. This way the matching attribute manager is told one of its attributes has changed.
  111. // Set the float value of this attribute.
  112. // Note that the value must be stored as a float!
  113. void SetValue( float flValue );
  114. // Set the value of this attribute as an unsigned integer.
  115. // Note that the value must be stored as an integer!
  116. // See CEconItemAttributeDefinition
  117. void SetIntValue( uint32 unValue );
  118. friend class CAttributeList;
  119. void Init( void );
  120. //--------------------------------------------------------
  121. private:
  122. // This is the index of the attribute into the attributes read from the data files
  123. CNetworkVar( attrib_definition_index_t, m_iAttributeDefinitionIndex );
  124. // This is the value of the attribute. Used to modify the item's variables.
  125. CNetworkVar( float, m_flValue );
  126. #if ENABLE_ATTRIBUTE_CURRENCY_TRACKING
  127. // This is the value that the attribute was first set to by an item definition
  128. CNetworkVar( int, m_nRefundableCurrency );
  129. #endif // ENABLE_ATTRIBUTE_CURRENCY_TRACKING
  130. };
  131. //-----------------------------------------------------------------------------
  132. // Purpose: An item that knows how to read itself from a datafile, describe itself to the user,
  133. // and serialize itself between Servers, Clients, and Steam.
  134. //
  135. // In the client DLL, we derive it from CDefaultClientRenderable so that
  136. // it can be passed in the pProxyData parameter of material proxies.
  137. //-----------------------------------------------------------------------------
  138. #if defined(CLIENT_DLL)
  139. class CEconItemView : public CDefaultClientRenderable, public CMaterialOverrideContainer< IEconItemInterface >
  140. #else
  141. class CEconItemView : public CMaterialOverrideContainer< IEconItemInterface >
  142. #endif
  143. {
  144. DECLARE_CLASS_NOBASE( CEconItemView );
  145. public:
  146. DECLARE_EMBEDDED_NETWORKVAR();
  147. DECLARE_DATADESC();
  148. public:
  149. CEconItemView();
  150. CEconItemView( const CEconItemView &src );
  151. ~CEconItemView();
  152. CEconItemView& operator=( const CEconItemView &src );
  153. bool operator==( const CEconItemView &other ) const;
  154. bool operator!=( const CEconItemView &other ) const { return !operator==( other ); }
  155. virtual const GameItemDefinition_t *GetItemDefinition() const
  156. {
  157. return GetStaticData();
  158. }
  159. public:
  160. // IEconItemInterface implementation.
  161. virtual itemid_t GetID() const { return GetItemID(); }
  162. virtual int32 GetQuality() const;
  163. virtual style_index_t GetStyle() const;
  164. virtual uint8 GetFlags() const;
  165. virtual eEconItemOrigin GetOrigin() const;
  166. virtual int GetQuantity() const;
  167. uint64 GetOriginalID() const { return GetSOCData() ? GetSOCData()->GetOriginalID() : 0; }
  168. virtual const char *GetCustomName() const;
  169. virtual const char *GetCustomDesc() const;
  170. virtual bool GetInUse() const { return GetSOCData() ? GetSOCData()->GetInUse() : false; }
  171. virtual void IterateAttributes( class IEconItemAttributeIterator *pIterator ) const OVERRIDE;
  172. bool IsValid( void ) const { return m_bInitialized; }
  173. void Invalidate( void ) { m_bInitialized = false; m_iItemDefinitionIndex = INVALID_ITEM_DEF_INDEX; m_iItemID = INVALID_ITEM_ID; }
  174. void InvalidateColor() { m_bColorInit = false; }
  175. void InvalidateOverrideColor() { m_bPaintOverrideInit = false; }
  176. // Initialize from the specified data
  177. // client will load SO cache as needed
  178. void Init( int iDefIndex, int iQuality, int iLevel, uint32 iAccountID = 0 );
  179. void SetInitialized( bool bInit ) { m_bInitialized = bInit; }
  180. // Get the static data contained in this item's definition
  181. GameItemDefinition_t *GetStaticData( void ) const;
  182. void SetNonSOEconItem( CEconItem* pItem ) { m_pNonSOEconItem.SetItem( pItem ); }
  183. void OnAttributeValuesChanged()
  184. {
  185. NetworkStateChanged();
  186. MarkDescriptionDirty();
  187. }
  188. private:
  189. void EnsureDescriptionIsBuilt( void ) const;
  190. void MarkDescriptionDirty( void );
  191. public:
  192. void SetGrayedOutReason( const char *pszGrayedOutReason );
  193. // Set & Get the index of this item's definition inside the script file
  194. void SetItemDefIndex( item_definition_index_t iIndex ) { m_iItemDefinitionIndex = iIndex; MarkDescriptionDirty(); }
  195. virtual item_definition_index_t GetItemDefIndex( void ) const { return m_iItemDefinitionIndex; }
  196. // Set & Get the quality & level of this item.
  197. void SetItemQuality( int iQuality ) { m_iEntityQuality = iQuality; MarkDescriptionDirty(); }
  198. int GetItemQuality( void ) const { return m_iEntityQuality; }
  199. void SetItemLevel( uint32 unLevel ) { m_iEntityLevel = unLevel; MarkDescriptionDirty(); }
  200. uint32 GetItemLevel( void ) const { return m_iEntityLevel; }
  201. int GetItemQuantity() const;
  202. #ifdef CLIENT_DLL
  203. void SetIsTradeItem( bool bIsTradeItem ) { m_bIsTradeItem = bIsTradeItem; MarkDescriptionDirty(); }
  204. void SetItemQuantity( int iQuantity ) { m_iEntityQuantity = iQuantity; MarkDescriptionDirty(); }
  205. void SetClientItemFlags( uint8 unFlags );
  206. void SetItemStyleOverride( style_index_t unNewStyleOverride );
  207. void SetItemOriginOverride( eEconItemOrigin unNewOriginOverride );
  208. #endif
  209. style_index_t GetItemStyle() const;
  210. // Access the worldwide global index of this item
  211. void SetItemID( itemid_t iIdx ) { m_iItemID = iIdx; m_iItemIDHigh = (m_iItemID >> 32); m_iItemIDLow = (m_iItemID & 0xFFFFFFFF); }
  212. #ifdef CLIENT_DLL
  213. // On the client, we need to rebuild it from the high & low networked pieces
  214. itemid_t GetItemID( void ) const { uint64 iTmp = ((((int64)m_iItemIDHigh)<<32) | m_iItemIDLow); return (itemid_t)iTmp; }
  215. #else
  216. itemid_t GetItemID( void ) const { return m_iItemID; }
  217. #endif
  218. uint32 GetAccountID( void ) const { return m_iAccountID; }
  219. void SetOverrideAccountID( uint32 nAccountID ) { m_iAccountID = nAccountID; }
  220. // Access the inventory position of this item
  221. void SetInventoryPosition( uint32 iPosition ) { m_iInventoryPosition = iPosition; }
  222. const uint32 GetInventoryPosition( void ) const { return m_iInventoryPosition; }
  223. // Return the model to use for model panels containing this item
  224. const char *GetInventoryModel( void );
  225. // Return the image to use for model panels containing this item
  226. const char *GetInventoryImage( void );
  227. bool GetInventoryImageData( int *iPosition, int *iSize );
  228. const char *GetInventoryOverlayImage( int idx );
  229. int GetInventoryOverlayImageCount( void );
  230. // Return the model to use when displaying this model on the player character model, if any
  231. const char *GetPlayerDisplayModel( int iClass, int iTeam ) const;
  232. // Return the model to use when displaying this model in the world. See the notes on this in econ_item_schema.h
  233. const char *GetWorldDisplayModel() const;
  234. const char *GetExtraWearableModel() const;
  235. const char *GetExtraWearableViewModel() const;
  236. const char *GetVisionFilteredDisplayModel() const;
  237. // Return the load-out slot that this item must be placed into
  238. int GetAnimationSlot( void ) const;
  239. // Return an int that indicates whether the item should be dropped from a dead owner.
  240. int GetDropType( void );
  241. // Remove all attributes on this item
  242. void DestroyAllAttributes( void );
  243. void InitNetworkedDynamicAttributesForDemos( void );
  244. // Items that have attributes that modify their RGB values
  245. int GetModifiedRGBValue( bool bAltColor=false );
  246. // Returns the UGC file ID of the custom texture assigned to this item. If non-zero, then it has a custom texture.
  247. uint64 GetCustomUserTextureID();
  248. CEconItem *GetSOCData( void ) const;
  249. bool IsEquipped( void ) const { return GetSOCData() && GetSOCData()->IsEquipped(); }
  250. bool IsEquippedForClass( equipped_class_t unClass ) const { return GetSOCData() && GetSOCData()->IsEquippedForClass( unClass ); }
  251. equipped_slot_t GetEquippedPositionForClass( equipped_class_t unClass ) const { return GetSOCData() ? GetSOCData()->GetEquippedPositionForClass( unClass ) : INVALID_EQUIPPED_SLOT; }
  252. // Attached particle systems
  253. int GetQualityParticleType() const;
  254. int GetSkin( int iTeam, bool bViewmodel = false ) const;
  255. public:
  256. // ...
  257. CAttributeList *GetAttributeList() { return &m_AttributeList; }
  258. const CAttributeList *GetAttributeList() const { return &m_AttributeList; }
  259. public:
  260. virtual CEconItemPaintKitDefinition *GetCustomPainkKitDefinition( void ) const { return GetItemDefinition()->GetCustomPainkKitDefinition(); }
  261. #ifdef CLIENT_DLL
  262. void SetWeaponSkinBase( ITexture* pBaseTex );
  263. void SetWeaponSkinBaseCompositor( ITextureCompositor * pTexCompositor );
  264. inline void SetWeaponSkinGeneration( RTime32 nGeneration ) { m_nWeaponSkinGeneration = nGeneration; }
  265. inline void SetWeaponSkinGenerationTeam( int iTeam ) { m_iLastGeneratedTeamSkin = iTeam; }
  266. inline void SetWeaponSkinBaseCreateFlags( uint32 flags ) { m_unWeaponSkinBaseCreateFlags = flags; }
  267. void CancelWeaponSkinComposite( );
  268. inline void SetWeaponSkinUseHighRes( bool bUseHighRes ) { m_bWeaponSkinUseHighRes = bUseHighRes; }
  269. inline void SetWeaponSkinUseLowRes( bool bUseLowRes ) { m_bWeaponSkinUseLowRes = bUseLowRes; }
  270. inline ITexture *GetWeaponSkinBase() const { return m_pWeaponSkinBase; }
  271. inline ITextureCompositor *GetWeaponSkinBaseCompositor() const { return m_pWeaponSkinBaseCompositor; }
  272. inline uint32 GetWeaponSkinBaseCreateFlags() const { return m_unWeaponSkinBaseCreateFlags; }
  273. inline RTime32 GetWeaponSkinGeneration() const { return m_nWeaponSkinGeneration; }
  274. inline int GetWeaponSkinGenerationTeam() const { return m_iLastGeneratedTeamSkin; }
  275. inline bool ShouldWeaponSkinUseHighRes() const { return m_bWeaponSkinUseHighRes; }
  276. inline bool ShouldWeaponSkinUseLowRes() const { return m_bWeaponSkinUseLowRes; }
  277. #endif // CLIENT_DLL
  278. inline int GetTeamNumber() const { return m_iTeamNumber; }
  279. inline void SetTeamNumber( int iTeamNumber ) { m_iTeamNumber = iTeamNumber; }
  280. protected:
  281. // Index of the item definition in the item script file.
  282. CNetworkVar( item_definition_index_t, m_iItemDefinitionIndex );
  283. // The quality of this item.
  284. CNetworkVar( int, m_iEntityQuality );
  285. // The level of this item.
  286. CNetworkVar( uint32, m_iEntityLevel );
  287. // The global index of this item, worldwide.
  288. itemid_t m_iItemID;
  289. CNetworkVar( uint32, m_iItemIDHigh );
  290. CNetworkVar( uint32, m_iItemIDLow );
  291. // Account ID of the person who has this in their inventory
  292. CNetworkVar( uint32, m_iAccountID );
  293. // Position inside the player's inventory
  294. CNetworkVar( uint32, m_iInventoryPosition );
  295. // This is an alternate source of data, if this item models something that isn't in the SO cache.
  296. CEconItemHandle m_pNonSOEconItem;
  297. #if defined( CLIENT_DLL )
  298. // exist on the client only
  299. bool m_bIsTradeItem;
  300. int m_iEntityQuantity;
  301. uint8 m_unClientFlags;
  302. // clients have the ability to force a style on an item view -- this is used for store previews,
  303. // character panels, etc.
  304. style_index_t m_unOverrideStyle;
  305. // clients can also force an origin on an item view -- this is used for crafting item previews
  306. eEconItemOrigin m_unOverrideOrigin;
  307. #endif
  308. bool m_bColorInit;
  309. bool m_bPaintOverrideInit;
  310. bool m_bHasPaintOverride;
  311. float m_flOverrideIndex;
  312. uint32 m_unRGB;
  313. uint32 m_unAltRGB;
  314. #ifdef CLIENT_DLL
  315. ITexture* m_pWeaponSkinBase;
  316. ITextureCompositor* m_pWeaponSkinBaseCompositor;
  317. RTime32 m_nWeaponSkinGeneration;
  318. uint32 m_unWeaponSkinBaseCreateFlags;
  319. int m_iLastGeneratedTeamSkin;
  320. bool m_bWeaponSkinUseHighRes;
  321. bool m_bWeaponSkinUseLowRes;
  322. #endif // CLIENT_DLL
  323. CNetworkVar( int, m_iTeamNumber );
  324. CNetworkVar( bool, m_bInitialized );
  325. #ifdef CLIENT_DLL // we avoid using "BUILD_ITEM_NAME_AND_DESC" to prevent everything depending on the CEconItemDescription
  326. public:
  327. // Return the single-line name of this item.
  328. const wchar_t *GetItemName( void ) const;
  329. // Return the full structure with all of our description lines.
  330. const class CEconItemDescription *GetDescription() const { EnsureDescriptionIsBuilt(); return m_pDescription; }
  331. private:
  332. mutable class CEconItemDescription *m_pDescription;
  333. mutable char *m_pszGrayedOutReason;
  334. // IClientRenderable
  335. virtual const Vector& GetRenderOrigin( void ) { return vec3_origin; }
  336. virtual const QAngle& GetRenderAngles( void ) { return vec3_angle; }
  337. virtual bool ShouldDraw( void ) { return false; }
  338. virtual bool IsTransparent( void ) { return false;}
  339. virtual const matrix3x4_t &RenderableToWorldTransform() { static matrix3x4_t mat; SetIdentityMatrix( mat ); return mat; }
  340. virtual void GetRenderBounds( Vector& mins, Vector& maxs );
  341. #endif
  342. private:
  343. CNetworkVarEmbedded( CAttributeList, m_AttributeList );
  344. CNetworkVarEmbedded( CAttributeList, m_NetworkedDynamicAttributesForDemos );
  345. // Some custom gamemodes are using server plugins to modify weapon attributes.
  346. // This variable allows them to completely set their own attributes on a weapon
  347. // and have the client and server ignore the static attributes.
  348. CNetworkVar( bool, m_bOnlyIterateItemViewAttributes );
  349. };
  350. #ifdef CLIENT_DLL
  351. bool DoesItemPassSearchFilter( const class IEconItemDescription *pDescription, const wchar_t* wszFilter );
  352. CBasePlayer *GetPlayerByAccountID( uint32 unAccountID );
  353. #endif // CLIENT_DLL
  354. #endif // ECON_ITEM_CONSTANTS_H