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.

220 lines
8.4 KiB

  1. //========= Copyright � 1996-2003, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose: Attributable entities contain one of these, which handles game specific handling:
  4. // - Save / Restore
  5. // - Networking
  6. // - Attribute providers
  7. // - Application of attribute effects
  8. //
  9. //=============================================================================
  10. #ifndef ATTRIBUTE_MANAGER_H
  11. #define ATTRIBUTE_MANAGER_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "econ_item_view.h"
  16. #include "ihasattributes.h"
  17. // Provider types
  18. enum attributeprovidertypes_t
  19. {
  20. PROVIDER_GENERIC,
  21. PROVIDER_WEAPON,
  22. };
  23. float CollateAttributeValues( const CEconItemAttribute *pAttrib1, const CEconItemAttribute *pAttrib2 );
  24. //-----------------------------------------------------------------------------
  25. // Macros for hooking the application of attributes
  26. #define CALL_ATTRIB_HOOK( vartype, retval, hookName, who, itemlist ) \
  27. retval = CAttributeManager::AttribHookValue<vartype>( retval, #hookName, static_cast<const CBaseEntity*>( who ), itemlist, true );
  28. #define CALL_ATTRIB_HOOK_INT( retval, hookName ) CALL_ATTRIB_HOOK( int, retval, hookName, this, NULL )
  29. #define CALL_ATTRIB_HOOK_FLOAT( retval, hookName ) CALL_ATTRIB_HOOK( float, retval, hookName, this, NULL )
  30. #define CALL_ATTRIB_HOOK_INT_ON_OTHER( other, retval, hookName ) CALL_ATTRIB_HOOK( int, retval, hookName, other, NULL )
  31. #define CALL_ATTRIB_HOOK_FLOAT_ON_OTHER( other, retval, hookName ) CALL_ATTRIB_HOOK( float, retval, hookName, other, NULL )
  32. #define CALL_ATTRIB_HOOK_BOOL( hookName ) ( 0 != CAttributeManager::AttribHookValue<int>( 0, #hookName, static_cast<const CBaseEntity*>( this ), NULL, true ) )
  33. #define CALL_ATTRIB_HOOK_BOOL_ON_OTHER( other, hookName ) ( 0 != CAttributeManager::AttribHookValue<int>( 0, #hookName, static_cast<const CBaseEntity*>( other ), NULL, true ) )
  34. template< class T > T AttributeConvertFromFloat( float flValue );
  35. template<> float AttributeConvertFromFloat<float>( float flValue );
  36. template<> int AttributeConvertFromFloat<int>( float flValue );
  37. //-----------------------------------------------------------------------------
  38. // Purpose: Base Attribute manager.
  39. // This class knows how to apply attribute effects that have been
  40. // provided to its owner by other entities, but doesn't contain attributes itself.
  41. //-----------------------------------------------------------------------------
  42. class CAttributeManager
  43. {
  44. DECLARE_CLASS_NOBASE( CAttributeManager );
  45. public:
  46. DECLARE_DATADESC();
  47. DECLARE_EMBEDDED_NETWORKVAR();
  48. virtual ~CAttributeManager() {}
  49. // Call this inside your entity's Spawn()
  50. virtual void InitializeAttributes( CBaseEntity *pEntity );
  51. CBaseEntity *GetOuter( void ) { return m_hOuter.Get(); }
  52. //--------------------------------------------------------
  53. // Attribute providers.
  54. // Other entities that are providing attributes to this entity (i.e. weapons being carried by a player)
  55. void ProvideTo( CBaseEntity *pProvider );
  56. void StopProvidingTo( CBaseEntity *pProvider );
  57. protected:
  58. // Not to be called directly. Use ProvideTo() or StopProvidingTo() above.
  59. void AddProvider( CBaseEntity *pProvider );
  60. void RemoveProvider( CBaseEntity *pProvider );
  61. public:
  62. int GetNumProviders( void ) { return m_Providers.Count(); }
  63. CBaseEntity *GetProvider( int iIndex );
  64. // Return true if this entity is providing attributes to the specified entity
  65. bool IsProvidingTo( CBaseEntity *pEntity );
  66. // Return true if this entity is being provided attributes by the specified entity
  67. bool IsBeingProvidedToBy( CBaseEntity *pEntity );
  68. // Provider types are used to prevent specified providers supplying to certain initiators
  69. void SetProviderType( attributeprovidertypes_t tType ) { m_ProviderType = tType; }
  70. attributeprovidertypes_t GetProviderType( void ) { return m_ProviderType; }
  71. //--------------------------------------------------------
  72. // Attribute hook. Use the CALL_ATTRIB_HOOK macros above.
  73. template <class T> static T AttribHookValue( T TValue, const char *pszAttribHook, const CBaseEntity *pEntity, CUtlVector<CBaseEntity*> *pItemList = NULL, bool bIsGlobalConstString = false )
  74. {
  75. // Do we have a hook?
  76. if ( pszAttribHook == NULL || pszAttribHook[0] == '\0' )
  77. return TValue;
  78. // Verify that we have an entity, at least as "this"
  79. if ( pEntity == NULL )
  80. return TValue;
  81. IHasAttributes *pAttribInterface = dynamic_cast<IHasAttributes*>( (CBaseEntity*) pEntity );
  82. Assert(pAttribInterface); // If you hit this, you've probably got a hook incorrectly setup, because the entity it's hooking on doesn't know about attributes.
  83. if ( pAttribInterface && pAttribInterface->GetAttributeManager() )
  84. {
  85. string_t iszAttribHook = bIsGlobalConstString ? AllocPooledStringConstant(pszAttribHook) : AllocPooledString(pszAttribHook);
  86. float flValue = pAttribInterface->GetAttributeManager()->ApplyAttributeFloatWrapper( static_cast<float>( TValue ), (CBaseEntity*) pEntity, iszAttribHook, pItemList );
  87. TValue = AttributeConvertFromFloat<T>( flValue );
  88. }
  89. return TValue;
  90. }
  91. virtual float ApplyAttributeFloat( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook = NULL_STRING, CUtlVector<CBaseEntity*> *pItemList = NULL );
  92. //--------------------------------------------------------
  93. // Networking
  94. #ifdef CLIENT_DLL
  95. virtual void OnPreDataChanged( DataUpdateType_t updateType );
  96. virtual void OnDataChanged( DataUpdateType_t updateType );
  97. #endif
  98. //--------------------------------------------------------
  99. // memory handling
  100. void *operator new( size_t stAllocateBlock );
  101. void *operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine );
  102. protected:
  103. CUtlVector<EHANDLE> m_Providers;
  104. CNetworkVarForDerived( int, m_iReapplyProvisionParity );
  105. CNetworkVarForDerived( EHANDLE, m_hOuter );
  106. bool m_bPreventLoopback;
  107. CNetworkVarForDerived( attributeprovidertypes_t, m_ProviderType );
  108. public:
  109. void ClearCache( void );
  110. private:
  111. virtual float ApplyAttributeFloatWrapper( float flValue, CBaseEntity *pInitiator, string_t pszAttribHook, CUtlVector<CBaseEntity*> *pItemList = NULL );
  112. private:
  113. // Cached attribute results
  114. // We cache off requests for data, and wipe the cache whenever our providers change.
  115. struct cached_attribute_float_t
  116. {
  117. float flIn;
  118. string_t iAttribHook;
  119. float flOut;
  120. };
  121. CUtlVector<cached_attribute_float_t> m_CachedResults;
  122. #ifdef CLIENT_DLL
  123. public:
  124. // Data received from the server
  125. int m_iOldReapplyProvisionParity;
  126. #endif
  127. };
  128. //-----------------------------------------------------------------------------
  129. // Purpose: This is an attribute manager that also knows how to contain attributes.
  130. //-----------------------------------------------------------------------------
  131. class CAttributeContainer : public CAttributeManager
  132. {
  133. public:
  134. DECLARE_DATADESC();
  135. DECLARE_CLASS( CAttributeContainer, CAttributeManager );
  136. DECLARE_EMBEDDED_NETWORKVAR();
  137. virtual void InitializeAttributes( CBaseEntity *pEntity );
  138. //--------------------------------------------------------
  139. // Attribute hook. Use the CALL_ATTRIB_HOOK macros above.
  140. virtual float ApplyAttributeFloat( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook = NULL_STRING, CUtlVector<CBaseEntity*> *pItemList = NULL ) OVERRIDE;
  141. CEconItemView *GetItem( void ) { return m_Item.Get(); }
  142. const CEconItemView *GetItem( void ) const { return m_Item.Get(); }
  143. void SetItem( CEconItemView *pItem )
  144. {
  145. m_Item.CopyFrom( *pItem );
  146. }
  147. private:
  148. CNetworkVarEmbedded( CEconItemView, m_Item );
  149. };
  150. //-----------------------------------------------------------------------------
  151. // Purpose: An attribute manager that uses a player's shared attributes.
  152. //-----------------------------------------------------------------------------
  153. #if defined( TF_DLL ) || defined( CSTRIKE_DLL )
  154. class CAttributeContainerPlayer : public CAttributeManager
  155. {
  156. public:
  157. DECLARE_DATADESC();
  158. DECLARE_CLASS( CAttributeContainerPlayer, CAttributeManager );
  159. DECLARE_EMBEDDED_NETWORKVAR();
  160. virtual void InitializeAttributes( CBaseEntity *pEntity );
  161. virtual float ApplyAttributeFloat( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook = NULL_STRING, CUtlVector<CBaseEntity*> *pItemList = NULL ) OVERRIDE;
  162. CBasePlayer* GetPlayer( void ) { return m_hPlayer; }
  163. void SetPlayer( CBasePlayer *pPlayer ) { m_hPlayer = pPlayer; }
  164. private:
  165. CNetworkHandle( CBasePlayer, m_hPlayer );
  166. };
  167. #endif
  168. #ifdef CLIENT_DLL
  169. EXTERN_RECV_TABLE( DT_AttributeManager );
  170. EXTERN_RECV_TABLE( DT_AttributeContainer );
  171. #else
  172. EXTERN_SEND_TABLE( DT_AttributeManager );
  173. EXTERN_SEND_TABLE( DT_AttributeContainer );
  174. #endif
  175. #endif // ATTRIBUTE_MANAGER_H