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.

274 lines
11 KiB

  1. //====== Copyright 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef BASEACHIEVEMENT_H
  7. #define BASEACHIEVEMENT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "GameEventListener.h"
  12. #include "../common/xlast_csgo/csgo.spa.h"
  13. #include "iachievementmgr.h"
  14. #define AWARD_ID_NONE ""
  15. class CAchievementMgr;
  16. class IPlayerLocal;
  17. //
  18. // Base class for achievements
  19. //
  20. class CBaseAchievement : public CGameEventListener, public IAchievement
  21. {
  22. DECLARE_CLASS_NOBASE( CBaseAchievement );
  23. public:
  24. CBaseAchievement();
  25. virtual void Init() {}
  26. virtual void ListenForEvents() {};
  27. virtual void Event_EntityKilled( CBaseEntity *pVictim, CBaseEntity *pAttacker, CBaseEntity *pInflictor, IGameEvent *event );
  28. int GetAchievementID() { return m_iAchievementID; }
  29. void SetAchievementID( int iAchievementID ) { m_iAchievementID = iAchievementID; }
  30. void SetName( const char *pszName ) { m_pszName = pszName; }
  31. const char *GetName() { return m_pszName; }
  32. void SetFlags( int iFlags );
  33. int GetFlags() { return m_iFlags; }
  34. void SetGoal( int iGoal ) { m_iGoal = iGoal; }
  35. int GetGoal() { return m_iGoal; }
  36. void SetGameDirFilter( const char *pGameDir );
  37. bool HasComponents() { return ( m_iFlags & ACH_HAS_COMPONENTS ) > 0; }
  38. void SetPointValue( int iPointValue ) { m_iPointValue = iPointValue; }
  39. int GetPointValue() { return m_iPointValue; }
  40. bool ShouldHideUntilAchieved() { return m_bHideUntilAchieved; }
  41. void SetHideUntilAchieved( bool bHide ) { m_bHideUntilAchieved = bHide; }
  42. void SetStoreProgressInSteam( bool bStoreProgressInSteam ) { m_bStoreProgressInSteam = bStoreProgressInSteam; }
  43. bool StoreProgressInSteam() { return m_bStoreProgressInSteam; }
  44. virtual bool ShouldShowProgressNotification() { return true; }
  45. virtual void OnPlayerStatsUpdate( int nUserSlot ) {}
  46. virtual bool ShouldSaveWithGame();
  47. bool ShouldSaveGlobal();
  48. virtual void PreRestoreSavedGame();
  49. virtual void PostRestoreSavedGame();
  50. void SetCount( int iCount ) { m_iCount = iCount; }
  51. int GetCount() { return m_iCount; }
  52. void SetProgressShown( int iProgressShown ) { m_iProgressShown = iProgressShown; }
  53. int GetProgressShown() { return m_iProgressShown; }
  54. virtual bool IsAchieved() { return m_bAchieved; }
  55. virtual bool IsActive();
  56. virtual bool IsAvailable(); // Is this achievement available? Might need DLC, etc
  57. virtual bool LocalPlayerCanEarn( void ) { return true; }
  58. void SetAchieved( bool bAchieved ) { m_bAchieved = bAchieved; }
  59. virtual void CheckAssetAwards( int nSlotId ) {}
  60. virtual bool IsMetaAchievement() { return false; }
  61. virtual void OnAchieved( void ) {}
  62. uint32 GetUnlockTime( void ) const { return m_uUnlockTime; }
  63. void SetUnlockTime( uint32 unlockTime ) { m_uUnlockTime = unlockTime; }
  64. uint64 GetComponentBits() { return m_iComponentBits; }
  65. virtual int GetNumComponents() { return m_iNumComponents; }
  66. virtual const char *GetComponentDisplayString( int iComponent );
  67. void SetComponentBits( uint64 iComponentBits );
  68. int GetNumComponentBitsSet( void );
  69. void OnComponentEvent( const char *pchComponentName );
  70. void EnsureComponentBitSetAndEvaluate( int iBitNumber );
  71. void EvaluateIsAlreadyAchieved();
  72. virtual void OnMapEvent( const char *pEventName );
  73. virtual void PrintAdditionalStatus() {} // for debugging, achievements may report additional status in achievement_status concmd
  74. virtual void OnSteamUserStatsStored() {}
  75. virtual void UpdateAchievement( int nData ) {}
  76. virtual bool ShouldShowOnHUD() { return m_bShowOnHUD; }
  77. virtual void SetShowOnHUD( bool bShow );
  78. virtual void SetUserSlot( int nUserSlot ) { m_nUserSlot = nUserSlot; }
  79. virtual void ClearAchievementData();
  80. virtual const char *GetIconPath() { return NULL; }
  81. void SetDisplayOrder( int iDisplayOrder ) { m_iDisplayOrder = iDisplayOrder; }
  82. int GetDisplayOrder( ) { return m_iDisplayOrder; }
  83. virtual void ReadProgress( IPlayerLocal *pPlayer ) {}
  84. virtual bool WriteProgress( IPlayerLocal *pPlayer ) { return false; }
  85. virtual void GetSettings( KeyValues* pNodeOut ); // serialize
  86. virtual void ApplySettings( /* const */ KeyValues* pNodeIn ); // unserialize
  87. virtual void Think( void ) { return; }
  88. // XBox Asset Awards
  89. void SetAssetAward( const char* assetAwardName );
  90. void SetAssetAwardID( int iAssetAwardID ) { m_iAssetAwardID = iAssetAwardID; }
  91. int GetAssetAwardID( void ) { return m_iAssetAwardID; }
  92. bool IsAssetAward( void ) { return m_iAssetAwardID > 0; }
  93. virtual bool CheckAchievementsEnabled( void );
  94. protected:
  95. virtual void FireGameEvent( IGameEvent *event );
  96. virtual void FireGameEvent_Internal( IGameEvent *event ) {};
  97. void SetVictimFilter( const char *pClassName );
  98. void SetAttackerFilter( const char *pClassName );
  99. void SetInflictorFilter( const char *pClassName );
  100. void SetInflictorEntityNameFilter( const char *pEntityName );
  101. void SetMapNameFilter( const char *pMapName );
  102. void SetComponentPrefix( const char *pPrefix );
  103. void IncrementCount( int iOptIncrement = 0 );
  104. void EvaluateNewAchievement();
  105. void AwardAchievement();
  106. void ShowProgressNotification();
  107. void HandleProgressUpdate();
  108. virtual void CalcProgressMsgIncrement();
  109. void SetNextThink( float flThinkTime );
  110. void ClearThink( void );
  111. const char *m_pszName; // name of this achievement
  112. int m_iAchievementID; // ID of this achievement
  113. int m_iFlags; // ACH_* flags for this achievement
  114. int m_iGoal; // goal # of steps to award this achievement
  115. int m_iProgressMsgIncrement; // after how many steps show we show a progress notification
  116. int m_iProgressMsgMinimum; // the minimum progress needed before showing progress notification
  117. int m_iPointValue; // # of points this achievement is worth (currently only used for XBox Live)
  118. bool m_bHideUntilAchieved; // should this achievement be hidden until achieved?
  119. bool m_bStoreProgressInSteam; // should incremental progress be stored in Steam. A counter with same name as achievement must be set up in Steam.
  120. const char *m_pInflictorClassNameFilter; // if non-NULL, inflictor class name to filter with
  121. const char *m_pInflictorEntityNameFilter; // if non-NULL, inflictor entity name to filter with
  122. const char *m_pVictimClassNameFilter; // if non-NULL, victim class name to filter with
  123. const char *m_pAttackerClassNameFilter; // if non-NULL, attacker class name to filter with
  124. const char *m_pMapNameFilter; // if non-NULL, map name to filter with
  125. const char *m_pGameDirFilter; // if non-NULL, game dir name to filter with
  126. const char **m_pszComponentNames;
  127. const char **m_pszComponentDisplayNames; // localizable strings for each component
  128. int m_iNumComponents;
  129. const char *m_pszComponentPrefix;
  130. int m_iComponentPrefixLen;
  131. bool m_bAchieved; // is this achievement achieved
  132. uint32 m_uUnlockTime; // time_t that this achievement was unlocked (0 if before Steamworks unlock time support)
  133. int m_iCount; // # of steps satisfied toward this achievement (only valid if not achieved)
  134. int m_iProgressShown; // # of progress msgs we've shown
  135. uint64 m_iComponentBits; // bitfield of components achieved
  136. CAchievementMgr *m_pAchievementMgr; // our achievement manager
  137. int m_nUserSlot;
  138. int m_iDisplayOrder; // Order in which the achievement is displayed in the UI
  139. bool m_bShowOnHUD; // if set, the player wants this achievement pinned to the HUD
  140. int m_iAssetAwardID; // ID of the avatar award asset associated with this achievement. Alliteration!
  141. friend class CAchievementMgr;
  142. public:
  143. DECLARE_DATADESC();
  144. };
  145. class CFailableAchievement : public CBaseAchievement
  146. {
  147. DECLARE_CLASS( CFailableAchievement, CBaseAchievement );
  148. public:
  149. CFailableAchievement();
  150. void SetFailed();
  151. virtual bool ShouldSaveWithGame();
  152. virtual void PreRestoreSavedGame();
  153. virtual void PostRestoreSavedGame();
  154. virtual bool IsAchieved() { return !m_bFailed && BaseClass::IsAchieved(); }
  155. virtual bool IsActive() { return m_bActivated && !m_bFailed && BaseClass::IsActive(); }
  156. bool IsFailed() { return m_bFailed; }
  157. virtual void OnMapEvent( const char *pEventName );
  158. virtual void OnActivationEvent() { Activate(); }
  159. virtual void OnEvaluationEvent();
  160. virtual const char *GetActivationEventName() =0;
  161. virtual const char *GetEvaluationEventName() =0;
  162. protected:
  163. void Activate();
  164. bool m_bActivated; // are we activated? (If there is a map event that turns us on, has that happened)
  165. bool m_bFailed; // has this achievement failed
  166. public:
  167. DECLARE_DATADESC();
  168. };
  169. class CMapAchievement : public CBaseAchievement
  170. {
  171. virtual void Init()
  172. {
  173. SetFlags( ACH_LISTEN_MAP_EVENTS | ACH_SAVE_GLOBAL );
  174. SetGoal( 1 );
  175. }
  176. };
  177. //----------------------------------------------------------------------------------------------------------------
  178. class CAchievement_AchievedCount : public CBaseAchievement
  179. {
  180. public:
  181. void Init();
  182. virtual void OnSteamUserStatsStored( void );
  183. virtual bool IsMetaAchievement() { return true; }
  184. int GetLowRange() { return m_iLowRange; }
  185. int GetHighRange() { return m_iHighRange; }
  186. int GetNumRequired() { return m_iNumRequired; }
  187. protected:
  188. void SetAchievementsRequired( int iNumRequired, int iLowRange, int iHighRange );
  189. private:
  190. int m_iNumRequired;
  191. int m_iLowRange;
  192. int m_iHighRange;
  193. };
  194. //
  195. // Helper class for achievement creation
  196. //
  197. typedef CBaseAchievement* (*achievementCreateFunc) (void);
  198. class CBaseAchievementHelper
  199. {
  200. public:
  201. CBaseAchievementHelper( achievementCreateFunc createFunc )
  202. {
  203. m_pfnCreate = createFunc;
  204. m_pNext = s_pFirst;
  205. s_pFirst = this;
  206. }
  207. achievementCreateFunc m_pfnCreate;
  208. CBaseAchievementHelper *m_pNext;
  209. static CBaseAchievementHelper *s_pFirst;
  210. };
  211. #define DECLARE_ACHIEVEMENT_( className, achievementID, achievementName, gameDirFilter, iPointValue, bHidden ) \
  212. static CBaseAchievement *Create_##className( void ) \
  213. { \
  214. CBaseAchievement *pAchievement = new className( ); \
  215. pAchievement->SetAchievementID( achievementID ); \
  216. pAchievement->SetName( achievementName ); \
  217. pAchievement->SetPointValue( iPointValue ); \
  218. pAchievement->SetHideUntilAchieved( bHidden ); \
  219. if ( gameDirFilter ) pAchievement->SetGameDirFilter( gameDirFilter ); \
  220. return pAchievement; \
  221. }; \
  222. static CBaseAchievementHelper g_##className##_Helper( Create_##className );
  223. #define DECLARE_ACHIEVEMENT( className, achievementID, achievementName, iPointValue ) \
  224. DECLARE_ACHIEVEMENT_( className, achievementID, achievementName, NULL, iPointValue, false )
  225. #define DECLARE_MAP_EVENT_ACHIEVEMENT_( achievementID, achievementName, gameDirFilter, iPointValue, bHidden ) \
  226. class CAchievement##achievementID : public CMapAchievement {}; \
  227. DECLARE_ACHIEVEMENT_( CAchievement##achievementID, achievementID, achievementName, gameDirFilter, iPointValue, bHidden ) \
  228. #define DECLARE_MAP_EVENT_ACHIEVEMENT( achievementID, achievementName, iPointValue ) \
  229. DECLARE_MAP_EVENT_ACHIEVEMENT_( achievementID, achievementName, NULL, iPointValue, false )
  230. #define DECLARE_MAP_EVENT_ACHIEVEMENT_HIDDEN( achievementID, achievementName, iPointValue ) \
  231. DECLARE_MAP_EVENT_ACHIEVEMENT_( achievementID, achievementName, NULL, iPointValue, true )
  232. #endif // BASEACHIEVEMENT_H