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.

183 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef ACHIEVEMENTMGR_H
  7. #define ACHIEVEMENTMGR_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "baseachievement.h"
  12. #include "GameEventListener.h"
  13. #include "hl2orange.spa.h"
  14. #include "iachievementmgr.h"
  15. #include "utlmap.h"
  16. #ifndef NO_STEAM
  17. #include "steam/steam_api.h"
  18. #endif
  19. #define THINK_CLEAR -1
  20. class CAchievementMgr : public CAutoGameSystemPerFrame, public CGameEventListener, public IAchievementMgr
  21. {
  22. public:
  23. //=============================================================================
  24. // HPE_BEGIN
  25. // [dwenger] Steam Cloud Support
  26. //=============================================================================
  27. enum SteamCloudPersisting
  28. {
  29. SteamCloudPersist_Off = 0,
  30. SteamCloudPersist_On,
  31. };
  32. CAchievementMgr( SteamCloudPersisting ePersistToSteamCloud = SteamCloudPersist_Off );
  33. //=============================================================================
  34. // HPE_END
  35. //=============================================================================
  36. virtual bool Init();
  37. virtual void PostInit();
  38. virtual void Shutdown();
  39. virtual void LevelInitPreEntity();
  40. virtual void LevelShutdownPreEntity();
  41. virtual void InitializeAchievements();
  42. virtual void Update( float frametime );
  43. #ifdef GAME_DLL
  44. virtual void FrameUpdatePostEntityThink();
  45. #endif
  46. void OnMapEvent( const char *pchEventName );
  47. // Interfaces exported to other dlls for achievement list queries
  48. IAchievement* GetAchievementByIndex( int index );
  49. int GetAchievementCount();
  50. CBaseAchievement *GetAchievementByID( int iAchievementID );
  51. CUtlMap<int, CBaseAchievement *> &GetAchievements() { return m_mapAchievement; }
  52. CBaseAchievement *GetAchievementByName( const char *pchName );
  53. bool HasAchieved( const char *pchName );
  54. void UploadUserData();
  55. void DownloadUserData();
  56. void SaveGlobalState( bool bAsync = false );
  57. void LoadGlobalState();
  58. void SaveGlobalStateIfDirty( bool bAsync = false );
  59. void EnsureGlobalStateLoaded();
  60. void AwardAchievement( int iAchievementID );
  61. void UpdateAchievement( int iAchievementID, int nData );
  62. void PreRestoreSavedGame();
  63. void PostRestoreSavedGame();
  64. void ResetAchievements();
  65. void ResetAchievement( int iAchievementID );
  66. void PrintAchievementStatus();
  67. float GetLastClassChangeTime() { return m_flLastClassChangeTime; }
  68. float GetTeamplayStartTime() { return m_flTeamplayStartTime; }
  69. int GetMiniroundsCompleted() { return m_iMiniroundsCompleted; }
  70. const char *GetMapName() { return m_szMap; }
  71. void OnAchievementEvent( int iAchievementID, int iCount = 1 );
  72. void CheckMetaAchievements( void );
  73. void SetDirty( bool bDirty )
  74. {
  75. if (bDirty)
  76. {
  77. m_bGlobalStateDirty = true;
  78. m_bSteamDataDirty = true;
  79. }
  80. }
  81. //=============================================================================
  82. // HPE_END
  83. //=============================================================================
  84. bool CheckAchievementsEnabled();
  85. bool LoggedIntoSteam()
  86. {
  87. #if !defined(NO_STEAM)
  88. return ( steamapicontext->SteamUser() && steamapicontext->SteamUserStats() && steamapicontext->SteamUser()->BLoggedOn() );
  89. #else
  90. return false;
  91. #endif
  92. }
  93. float GetTimeLastUpload() { return m_flTimeLastSaved; } // time we last uploaded to Steam
  94. bool WereCheatsEverOn( void ) { return m_bCheatsEverOn; }
  95. #if !defined(NO_STEAM)
  96. STEAM_CALLBACK( CAchievementMgr, Steam_OnUserStatsReceived, UserStatsReceived_t, m_CallbackUserStatsReceived );
  97. STEAM_CALLBACK( CAchievementMgr, Steam_OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored );
  98. #endif
  99. void SetAchievementThink( CBaseAchievement *pAchievement, float flThinkTime );
  100. private:
  101. void FireGameEvent( IGameEvent *event );
  102. void OnKillEvent( CBaseEntity *pVictim, CBaseEntity *pAttacker, CBaseEntity *pInflictor, IGameEvent *event );
  103. void ResetAchievement_Internal( CBaseAchievement *pAchievement );
  104. void UpdateStateFromSteam_Internal();
  105. CUtlMap<int, CBaseAchievement *> m_mapAchievement; // map of all achievements
  106. CUtlVector<CBaseAchievement *> m_vecAchievement; // vector of all achievements for accessing by index
  107. CUtlVector<CBaseAchievement *> m_vecKillEventListeners; // vector of achievements that are listening for kill events
  108. CUtlVector<CBaseAchievement *> m_vecMapEventListeners; // vector of achievements that are listening for map events
  109. CUtlVector<CBaseAchievement *> m_vecComponentListeners; // vector of achievements that are listening for components that make up an achievement
  110. CUtlMap<int, CAchievement_AchievedCount *> m_mapMetaAchievement; // map of CAchievement_AchievedCount
  111. struct achievementthink_t
  112. {
  113. float m_flThinkTime;
  114. CBaseAchievement *pAchievement;
  115. };
  116. CUtlVector<achievementthink_t> m_vecThinkListeners; // vector of achievements that are actively thinking
  117. float m_flLevelInitTime;
  118. float m_flLastClassChangeTime; // Time when player last changed class
  119. float m_flTeamplayStartTime; // Time when player joined a non-spectating team. Not updated if she switches game teams; cleared if she joins spectator
  120. float m_iMiniroundsCompleted; // # of minirounds played since game start (for maps that have minirounds)
  121. char m_szMap[MAX_PATH]; // file base of map name, cached since we access it frequently in this form
  122. bool m_bGlobalStateDirty; // do we have interesting state changes that needs to be saved?
  123. bool m_bSteamDataDirty; // do we have changes to upload to Steamworks?
  124. bool m_bGlobalStateLoaded; // have we loaded global state
  125. bool m_bCheatsEverOn; // have cheats ever been turned on in this level
  126. float m_flTimeLastSaved; // last time we uploaded to Steam
  127. //=============================================================================
  128. // HPE_BEGIN
  129. // [dwenger] Steam Cloud Support
  130. //=============================================================================
  131. bool m_bPersistToSteamCloud; // true = persist data to steam cloud
  132. //=============================================================================
  133. // HPE_END
  134. //=============================================================================
  135. CUtlVector<int> m_AchievementsAwarded;
  136. };
  137. // helper functions
  138. const char *GetModelName( CBaseEntity *pBaseEntity );
  139. #ifdef CLIENT_DLL
  140. bool CalcPlayersOnFriendsList( int iMinPlayers );
  141. bool CalcHasNumClanPlayers( int iClanTeammates );
  142. int CalcPlayerCount();
  143. int CalcTeammateCount();
  144. #endif // CLIENT
  145. class IMatchmaking;
  146. extern ConVar cc_achievement_debug;
  147. extern IMatchmaking *matchmaking;
  148. #ifdef CLIENT_DLL
  149. void MsgFunc_AchievementEvent( bf_read &msg );
  150. #endif // CLIENT_DLL
  151. #endif // ACHIEVEMENTMGR_H