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.

230 lines
11 KiB

  1. //====== Copyright � 1996-2005, 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 "matchmaking/imatchframework.h"
  12. #include "baseachievement.h"
  13. #include "GameEventListener.h"
  14. #include "iachievementmgr.h"
  15. #include "utlmap.h"
  16. #include "usermessages.h"
  17. #ifndef NO_STEAM
  18. #include "steam/steam_api.h"
  19. #endif
  20. #define THINK_CLEAR -1
  21. enum SyncAchievementValueDirection_t
  22. {
  23. ACHIEVEMENT_WRITE_ACHIEVEMENT,
  24. ACHIEVEMENT_READ_ACHIEVEMENT
  25. };
  26. typedef void* AsyncHandle_t;
  27. class CAchievementMgr : public CAutoGameSystemPerFrame, public CGameEventListener, public IMatchEventsSink
  28. {
  29. public:
  30. CAchievementMgr();
  31. static CAchievementMgr * GetInstance();
  32. static IAchievementMgr * GetInstanceInterface();
  33. virtual bool Init();
  34. virtual void PostInit();
  35. virtual void Shutdown();
  36. virtual void LevelInitPreEntity();
  37. virtual void LevelShutdownPreEntity();
  38. virtual void InitializeAchievements( );
  39. virtual void Update( float frametime );
  40. virtual bool IsPerFrame( void ) { return true; }
  41. void OnMapEvent( const char *pchEventName, int nUserSlot );
  42. // Interfaces exported to other dlls for achievement list queries
  43. IAchievement* GetAchievementByIndex( int index, int nUserSlot );
  44. IAchievement* GetAchievementByDisplayOrder( int orderIndex, int nUserSlot );
  45. IAchievement* GetAwardByDisplayOrder( int orderIndex, int nUserSlot );
  46. int GetAchievementCount( bool bAssets=false );
  47. CBaseAchievement *GetAchievementByID( int iAchievementID, int nUserSlot );
  48. CUtlMap<int, CBaseAchievement *> &GetAchievements( int nUserSlot ) { return m_mapAchievement[nUserSlot]; }
  49. CBaseAchievement *GetAchievementByName( const char *pchName, int nUserSlot );
  50. void UploadUserData( int nUserSlot );
  51. void UserConnected( int nUserSlot );
  52. void UserDisconnected( int nUserSlot );
  53. bool IsUserConnected( int nUserSlot ) { return m_bUserSlotActive[nUserSlot]; }
  54. void ReadAchievementsFromTitleData( int iController, int iSlot );
  55. void SaveGlobalState();
  56. void SaveGlobalStateIfDirty();
  57. bool IsAchievementAllowedInGame( int iAchievementID );
  58. bool HasAchieved( const char *pchName, int nUserSlot );
  59. void AwardAchievement( int iAchievementID, int nUserSlot );
  60. #if defined ( _X360 )
  61. void AwardXBoxAchievement( int iAchievementID, int iXBoxAchievementID, int nUserSlot );
  62. #endif
  63. void UpdateAchievement( int iAchievementID, int nData, int nUserSlot );
  64. #if defined( CLIENT_DLL ) && !defined( NO_STEAM )
  65. void UpdateStateFromSteam_Internal( int nUserSlot );
  66. #endif
  67. void PreRestoreSavedGame();
  68. void PostRestoreSavedGame();
  69. void ResetAchievements();
  70. void ClearAchievements( int nUserSlot );
  71. void ResetAchievement( int iAchievementID );
  72. void PrintAchievementStatus();
  73. float GetLastClassChangeTime( int nUserSlot ) { return m_flLastClassChangeTime[nUserSlot]; }
  74. float GetTeamplayStartTime( int nUserSlot ) { return m_flTeamplayStartTime[nUserSlot]; }
  75. int GetMiniroundsCompleted( int nUserSlot ) { return m_iMiniroundsCompleted[nUserSlot]; }
  76. const char *GetMapName() { return m_szMap; }
  77. bool IsCurrentMap( const char *pszMapName );
  78. void OnAchievementEvent( int iAchievementID, int nUserSlot );
  79. void SetDirty( bool bDirty, int nUserSlot ) { m_bDirty[nUserSlot] = bDirty; }
  80. bool CheckAchievementsEnabled();
  81. void CheckSignInState( bool bCheck ) { m_bCheckSigninState = bCheck; }
  82. // IMatchEventsSink
  83. virtual void OnEvent( KeyValues *pEvent );
  84. #if defined( CLIENT_DLL ) && !defined( NO_STEAM )
  85. bool LoggedIntoSteam() { return ( steamapicontext->SteamUser() && steamapicontext->SteamUserStats() && steamapicontext->SteamUser()->BLoggedOn() ); }
  86. #else
  87. bool LoggedIntoSteam() { return false; }
  88. #endif
  89. float GetTimeLastUpload() { return m_flTimeLastUpload; } // time we last uploaded to Steam
  90. bool WereCheatsEverOn( void ) { return m_bCheatsEverOn; }
  91. #if !defined(NO_STEAM)
  92. STEAM_CALLBACK( CAchievementMgr, Steam_OnUserStatsStored, UserStatsStored_t, m_CallbackUserStatsStored );
  93. #endif
  94. const CUtlVector<int>& GetAchievedDuringCurrentGame( int nPlayerSlot );
  95. void ResetAchievedDuringCurrentGame( int nPlayerSlot );
  96. // [sbodenbender] brought over from orange box
  97. void SetAchievementThink( CBaseAchievement *pAchievement, float flThinkTime );
  98. bool SyncAchievementsToTitleData( int iController, SyncAchievementValueDirection_t eOp );
  99. void SendWriteProfileEvent();
  100. void SendResetProfileEvent();
  101. UIProfileInfo* GetUIProfileInfo() { return &m_UIProfileInfo; };
  102. void ResetProfileInfo();
  103. bool GetWriteProfileResult() { return m_writeProfileResult; }
  104. void SetWriteProfileResult(bool result) { m_writeProfileResult = result; }
  105. CUserMessageBinder m_UMCMsgAchievementEvent;
  106. private:
  107. void FireGameEvent( IGameEvent *event );
  108. // [sbodenbender] gobetween for UI and profile data
  109. UIProfileInfo m_UIProfileInfo;
  110. bool m_writeProfileResult;
  111. void OnKillEvent( CBaseEntity *pVictim, CBaseEntity *pAttacker, CBaseEntity *pInflictor, IGameEvent *event );
  112. void ResetAchievement_Internal( CBaseAchievement *pAchievement );
  113. CUtlMap<int, CBaseAchievement *> m_mapAchievement[MAX_SPLITSCREEN_PLAYERS]; // map of all achievements
  114. CUtlVector<CBaseAchievement *> m_vecAchievement[MAX_SPLITSCREEN_PLAYERS]; // vector of all achievements for accessing by index
  115. CUtlVector<CBaseAchievement *> m_vecAward[MAX_SPLITSCREEN_PLAYERS]; // vector of all asset award achievements for accessing by index
  116. CUtlVector<CBaseAchievement *> m_vecKillEventListeners[MAX_SPLITSCREEN_PLAYERS]; // vector of achievements that are listening for kill events
  117. CUtlVector<CBaseAchievement *> m_vecMapEventListeners[MAX_SPLITSCREEN_PLAYERS]; // vector of achievements that are listening for map events
  118. CUtlVector<CBaseAchievement *> m_vecComponentListeners[MAX_SPLITSCREEN_PLAYERS]; // vector of achievements that are listening for components that make up an achievement
  119. CUtlVector<CBaseAchievement *> m_vecAchievementInOrder[MAX_SPLITSCREEN_PLAYERS]; // vector of all achievements for accessing by display order
  120. CUtlVector<CBaseAchievement *> m_vecAwardInOrder[MAX_SPLITSCREEN_PLAYERS]; // vector of all asset award achievements for accessing by display order
  121. // [sbodenbender] brought over from orange box
  122. struct achievementthink_t
  123. {
  124. float m_flThinkTime;
  125. CBaseAchievement *pAchievement;
  126. };
  127. CUtlVector<achievementthink_t> m_vecThinkListeners; // vector of achievements that are actively thinking
  128. float m_flLevelInitTime[MAX_SPLITSCREEN_PLAYERS];
  129. float m_flLastClassChangeTime[MAX_SPLITSCREEN_PLAYERS]; // Time when player last changed class
  130. float m_flTeamplayStartTime[MAX_SPLITSCREEN_PLAYERS]; // Time when player joined a non-spectating team. Not updated if she switches game teams; cleared if she joins spectator
  131. float m_iMiniroundsCompleted[MAX_SPLITSCREEN_PLAYERS]; // # of minirounds played since game start (for maps that have minirounds)
  132. char m_szMap[MAX_PATH]; // file base of map name, cached since we access it frequently in this form
  133. bool m_bDirty[MAX_SPLITSCREEN_PLAYERS]; // do we have interesting state that needs to be saved
  134. bool m_bUserSlotActive[MAX_SPLITSCREEN_PLAYERS];
  135. bool m_bCheatsEverOn; // have cheats ever been turned on in this level
  136. float m_flTimeLastUpload; // last time we uploaded to Steam
  137. bool m_bCheckSigninState;
  138. bool m_bReadingFromTitleData;
  139. #ifdef _X360
  140. struct PendingAchievementInfo_t {
  141. int nAchievementID; // Achievement we're waiting to check the status of
  142. int nUserSlot; // Which user is being awarded the achievement
  143. AsyncHandle_t pOverlappedResult; // Result we'll check again
  144. };
  145. CUtlVector<PendingAchievementInfo_t> m_pendingAchievementState;
  146. #endif
  147. CUtlVector<int> m_AchievementsAwarded[MAX_SPLITSCREEN_PLAYERS];
  148. CUtlVector<int> m_AchievementsAwardedDuringCurrentGame[MAX_SPLITSCREEN_PLAYERS];
  149. void ClearAchievementData( int nUserSlot );
  150. };
  151. class CAchievementMgrDelegateIAchievementMgr : public IAchievementMgr
  152. {
  153. public:
  154. virtual IAchievement* GetAchievementByIndex( int index, int nPlayerSlot ) { return m_pDelegate->CAchievementMgr::GetAchievementByIndex( index, nPlayerSlot ); }
  155. virtual IAchievement* GetAchievementByDisplayOrder( int orderIndex, int nPlayerSlot ) { return m_pDelegate->CAchievementMgr::GetAchievementByDisplayOrder( orderIndex, nPlayerSlot ); }
  156. virtual IAchievement* GetAwardByDisplayOrder( int orderIndex, int nPlayerSlot ) { return m_pDelegate->CAchievementMgr::GetAwardByDisplayOrder( orderIndex, nPlayerSlot ); }
  157. virtual CBaseAchievement* GetAchievementByID ( int id, int nPlayerSlot ) { return m_pDelegate->CAchievementMgr::GetAchievementByID( id, nPlayerSlot ); }
  158. virtual int GetAchievementCount( bool bAssets = false ) { return m_pDelegate->CAchievementMgr::GetAchievementCount( bAssets ); }
  159. virtual void InitializeAchievements( ) { m_pDelegate->CAchievementMgr::InitializeAchievements(); }
  160. virtual void AwardAchievement( int nAchievementID, int nPlayerSlot ) { m_pDelegate->CAchievementMgr::AwardAchievement( nAchievementID, nPlayerSlot ); }
  161. virtual void OnMapEvent( const char *pchEventName, int nPlayerSlot ) { m_pDelegate->CAchievementMgr::OnMapEvent( pchEventName, nPlayerSlot ); }
  162. virtual void SaveGlobalStateIfDirty( ) { m_pDelegate->CAchievementMgr::SaveGlobalState(); }
  163. virtual bool HasAchieved( const char *pchName, int nPlayerSlot ) { return m_pDelegate->CAchievementMgr::HasAchieved( pchName, nPlayerSlot ); }
  164. virtual const CUtlVector<int>& GetAchievedDuringCurrentGame( int nPlayerSlot ) { return m_pDelegate->CAchievementMgr::GetAchievedDuringCurrentGame( nPlayerSlot ); }
  165. virtual bool WereCheatsEverOn() { return m_pDelegate->CAchievementMgr::WereCheatsEverOn(); }
  166. virtual UIProfileInfo* GetUIProfileInfo() { return m_pDelegate->CAchievementMgr::GetUIProfileInfo(); }
  167. virtual void ResetProfileInfo() { m_pDelegate->CAchievementMgr::ResetProfileInfo(); }
  168. virtual void SendWriteProfileEvent() { m_pDelegate->CAchievementMgr::SendWriteProfileEvent(); }
  169. virtual void SendResetProfileEvent() { m_pDelegate->CAchievementMgr::SendResetProfileEvent(); }
  170. virtual bool GetWriteProfileResult() { return m_pDelegate->CAchievementMgr::GetWriteProfileResult(); }
  171. explicit CAchievementMgrDelegateIAchievementMgr( CAchievementMgr *pDelegate ) : m_pDelegate( pDelegate ) {}
  172. protected:
  173. CAchievementMgr *m_pDelegate;
  174. };
  175. // helper functions
  176. const char *GetModelName( CBaseEntity *pBaseEntity );
  177. #if defined ( _X360 )
  178. bool IsXboxFriends( int userID, int entityIndex );
  179. #endif
  180. #ifdef CLIENT_DLL
  181. bool CalcPlayersOnFriendsList( int iMinPlayers );
  182. bool CalcHasNumClanPlayers( int iClanTeammates );
  183. int CalcPlayerCount();
  184. int CalcTeammateCount();
  185. #endif // CLIENT
  186. extern ConVar cc_achievement_debug;
  187. #ifdef CLIENT_DLL
  188. bool MsgFunc_AchievementEvent( const CCSUsrMsg_AchievementEvent &msg );
  189. #endif // CLIENT_DLL
  190. #endif // ACHIEVEMENTMGR_H