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.

270 lines
11 KiB

  1. //========= Copyright 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 "hl2orange.spa.h"
  13. #include "iachievementmgr.h"
  14. class CAchievementMgr;
  15. //
  16. // Base class for achievements
  17. //
  18. class CBaseAchievement : public CGameEventListener, public IAchievement
  19. {
  20. DECLARE_CLASS_NOBASE( CBaseAchievement );
  21. public:
  22. CBaseAchievement();
  23. virtual ~CBaseAchievement();
  24. virtual void Init() {}
  25. virtual void ListenForEvents() {};
  26. virtual void Event_EntityKilled( CBaseEntity *pVictim, CBaseEntity *pAttacker, CBaseEntity *pInflictor, IGameEvent *event );
  27. int GetAchievementID() { return m_iAchievementID; }
  28. void SetAchievementID( int iAchievementID ) { m_iAchievementID = iAchievementID; }
  29. void SetName( const char *pszName ) { m_pszName = pszName; }
  30. const char *GetName() { return m_pszName; }
  31. const char *GetStat() { return m_pszStat?m_pszStat:GetName(); }
  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() {}
  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 LocalPlayerCanEarn( void ) { return true; }
  57. void SetAchieved( bool bAchieved ) { m_bAchieved = bAchieved; }
  58. virtual bool IsMetaAchievement() { return false; }
  59. virtual bool AlwaysListen() { return false; }
  60. virtual bool AlwaysEnabled() { return false; }
  61. //=============================================================================
  62. // HPE_BEGIN:
  63. // [pfreese] Notification method for derived classes
  64. //=============================================================================
  65. virtual void OnAchieved() {}
  66. uint32 GetUnlockTime() const { return m_uUnlockTime; }
  67. void SetUnlockTime( uint32 unlockTime ) { m_uUnlockTime = unlockTime; }
  68. //=============================================================================
  69. // HPE_END
  70. //=============================================================================
  71. uint64 GetComponentBits() { return m_iComponentBits; }
  72. void SetComponentBits( uint64 iComponentBits );
  73. void OnComponentEvent( const char *pchComponentName );
  74. void EnsureComponentBitSetAndEvaluate( int iBitNumber );
  75. void EvaluateIsAlreadyAchieved();
  76. virtual void OnMapEvent( const char *pEventName );
  77. virtual void PrintAdditionalStatus() {} // for debugging, achievements may report additional status in achievement_status concmd
  78. virtual void OnSteamUserStatsStored() {}
  79. virtual void UpdateAchievement( int nData ) {}
  80. virtual bool ShouldShowOnHUD() { return m_bShowOnHUD; }
  81. virtual void SetShowOnHUD( bool bShow );
  82. //=============================================================================
  83. // HPE_BEGIN:
  84. // [pfreese] Serialization methods
  85. //=============================================================================
  86. virtual void GetSettings( KeyValues* pNodeOut ); // serialize
  87. virtual void ApplySettings( /* const */ KeyValues* pNodeIn ); // unserialize
  88. //=============================================================================
  89. // HPE_END
  90. //=============================================================================
  91. virtual void Think( void ) { return; }
  92. const char *GetMapNameFilter( void ){ return m_pMapNameFilter; }
  93. CAchievementMgr *GetAchievementMgr( void ){ return m_pAchievementMgr; }
  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. void SetStat( const char* pStatName ) { m_pszStat = pStatName; }
  112. const char *m_pszName; // name of this achievement
  113. const char *m_pszStat; // stat this achievement uses
  114. int m_iAchievementID; // ID of this achievement
  115. int m_iFlags; // ACH_* flags for this achievement
  116. int m_iGoal; // goal # of steps to award this achievement
  117. int m_iProgressMsgIncrement; // after how many steps show we show a progress notification
  118. int m_iProgressMsgMinimum; // the minimum progress needed before showing progress notification
  119. int m_iPointValue; // # of points this achievement is worth (currently only used for XBox Live)
  120. bool m_bHideUntilAchieved; // should this achievement be hidden until achieved?
  121. bool m_bStoreProgressInSteam; // should incremental progress be stored in Steam. A counter with same name as achievement must be set up in Steam.
  122. const char *m_pInflictorClassNameFilter; // if non-NULL, inflictor class name to filter with
  123. const char *m_pInflictorEntityNameFilter; // if non-NULL, inflictor entity name to filter with
  124. const char *m_pVictimClassNameFilter; // if non-NULL, victim class name to filter with
  125. const char *m_pAttackerClassNameFilter; // if non-NULL, attacker class name to filter with
  126. const char *m_pMapNameFilter; // if non-NULL, map name to filter with
  127. const char *m_pGameDirFilter; // if non-NULL, game dir name to filter with
  128. const char **m_pszComponentNames;
  129. int m_iNumComponents;
  130. const char *m_pszComponentPrefix;
  131. int m_iComponentPrefixLen;
  132. bool m_bAchieved; // is this achievement achieved
  133. uint32 m_uUnlockTime; // time_t that this achievement was unlocked (0 if before Steamworks unlock time support)
  134. int m_iCount; // # of steps satisfied toward this achievement (only valid if not achieved)
  135. int m_iProgressShown; // # of progress msgs we've shown
  136. uint64 m_iComponentBits; // bitfield of components achieved
  137. CAchievementMgr *m_pAchievementMgr; // our achievement manager
  138. bool m_bShowOnHUD; // if set, the player wants this achievement pinned to the HUD
  139. friend class CAchievementMgr;
  140. public:
  141. DECLARE_DATADESC();
  142. };
  143. class CFailableAchievement : public CBaseAchievement
  144. {
  145. DECLARE_CLASS( CFailableAchievement, CBaseAchievement );
  146. public:
  147. CFailableAchievement();
  148. void SetFailed();
  149. virtual bool ShouldSaveWithGame();
  150. virtual void PreRestoreSavedGame();
  151. virtual void PostRestoreSavedGame();
  152. virtual bool IsAchieved() { return !m_bFailed && BaseClass::IsAchieved(); }
  153. virtual bool IsActive() { return m_bActivated && !m_bFailed && BaseClass::IsActive(); }
  154. bool IsFailed() { return m_bFailed; }
  155. virtual void OnMapEvent( const char *pEventName );
  156. virtual void OnActivationEvent() { Activate(); }
  157. virtual void OnEvaluationEvent();
  158. virtual const char *GetActivationEventName() =0;
  159. virtual const char *GetEvaluationEventName() =0;
  160. protected:
  161. void Activate();
  162. bool m_bActivated; // are we activated? (If there is a map event that turns us on, has that happened)
  163. bool m_bFailed; // has this achievement failed
  164. public:
  165. DECLARE_DATADESC();
  166. };
  167. class CMapAchievement : public CBaseAchievement
  168. {
  169. virtual void Init()
  170. {
  171. SetFlags( ACH_LISTEN_MAP_EVENTS | ACH_SAVE_GLOBAL );
  172. SetGoal( 1 );
  173. }
  174. };
  175. //----------------------------------------------------------------------------------------------------------------
  176. class CAchievement_AchievedCount : public CBaseAchievement
  177. {
  178. public:
  179. void Init();
  180. virtual void OnSteamUserStatsStored( void );
  181. virtual bool IsMetaAchievement() { return true; }
  182. int GetLowRange() { return m_iLowRange; }
  183. int GetHighRange() { return m_iHighRange; }
  184. int GetNumRequired() { return m_iNumRequired; }
  185. protected:
  186. void SetAchievementsRequired( int iNumRequired, int iLowRange, int iHighRange );
  187. private:
  188. int m_iNumRequired;
  189. int m_iLowRange;
  190. int m_iHighRange;
  191. };
  192. //
  193. // Helper class for achievement creation
  194. //
  195. typedef CBaseAchievement* (*achievementCreateFunc) (void);
  196. class CBaseAchievementHelper
  197. {
  198. public:
  199. CBaseAchievementHelper( achievementCreateFunc createFunc )
  200. {
  201. m_pfnCreate = createFunc;
  202. m_pNext = s_pFirst;
  203. s_pFirst = this;
  204. }
  205. achievementCreateFunc m_pfnCreate;
  206. CBaseAchievementHelper *m_pNext;
  207. static CBaseAchievementHelper *s_pFirst;
  208. };
  209. #define DECLARE_ACHIEVEMENT_( className, achievementID, achievementName, gameDirFilter, iPointValue, bHidden ) \
  210. static CBaseAchievement *Create_##className( void ) \
  211. { \
  212. CBaseAchievement *pAchievement = new className( ); \
  213. pAchievement->SetAchievementID( achievementID ); \
  214. pAchievement->SetName( achievementName ); \
  215. pAchievement->SetPointValue( iPointValue ); \
  216. pAchievement->SetHideUntilAchieved( bHidden ); \
  217. if ( gameDirFilter ) pAchievement->SetGameDirFilter( gameDirFilter ); \
  218. return pAchievement; \
  219. }; \
  220. static CBaseAchievementHelper g_##className##_Helper( Create_##className );
  221. #define DECLARE_ACHIEVEMENT( className, achievementID, achievementName, iPointValue ) \
  222. DECLARE_ACHIEVEMENT_( className, achievementID, achievementName, NULL, iPointValue, false )
  223. #define DECLARE_MAP_EVENT_ACHIEVEMENT_( achievementID, achievementName, gameDirFilter, iPointValue, bHidden ) \
  224. class CAchievement##achievementID : public CMapAchievement {}; \
  225. DECLARE_ACHIEVEMENT_( CAchievement##achievementID, achievementID, achievementName, gameDirFilter, iPointValue, bHidden ) \
  226. #define DECLARE_MAP_EVENT_ACHIEVEMENT( achievementID, achievementName, iPointValue ) \
  227. DECLARE_MAP_EVENT_ACHIEVEMENT_( achievementID, achievementName, NULL, iPointValue, false )
  228. #define DECLARE_MAP_EVENT_ACHIEVEMENT_HIDDEN( achievementID, achievementName, iPointValue ) \
  229. DECLARE_MAP_EVENT_ACHIEVEMENT_( achievementID, achievementName, NULL, iPointValue, true )
  230. #endif // BASEACHIEVEMENT_H