From 6f744aef8805d2a2184de1bf1cf5bb1fba4819a3 Mon Sep 17 00:00:00 2001 From: elexis Date: Thu, 19 May 2016 19:42:07 +0000 Subject: [PATCH] Simulation context cleanup, refs #3991, #3168. Save the viewed player in the CGame class. Add the const keyword back to the SimContext to help find mistakes at compiletime. This was SVN commit r18201. --- source/gui/scripting/ScriptFunctions.cpp | 6 ++---- source/ps/Game.cpp | 12 +++++++++++- source/ps/Game.h | 17 +++++++++++++++++ source/simulation2/Simulation2.cpp | 7 +------ source/simulation2/Simulation2.h | 4 +--- source/simulation2/system/SimContext.cpp | 9 ++------- source/simulation2/system/SimContext.h | 3 --- 7 files changed, 34 insertions(+), 24 deletions(-) diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 52204dbacc..925bc230b1 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -237,10 +237,8 @@ void SetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) void SetViewedPlayer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) { - if (!g_Game) - return; - - g_Game->GetSimulation2()->GetSimContext().SetCurrentDisplayedPlayer(id); + if (g_Game) + g_Game->SetViewedPlayerID(id); } JS::Value GetEngineInfo(ScriptInterface::CxPrivate* pCxPrivate) diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp index d42f95e0f4..72f9951299 100644 --- a/source/ps/Game.cpp +++ b/source/ps/Game.cpp @@ -345,10 +345,20 @@ int CGame::GetPlayerID() void CGame::SetPlayerID(player_id_t playerID) { m_PlayerID = playerID; + m_ViewedPlayerID = playerID; + if (m_TurnManager) m_TurnManager->SetPlayerID(m_PlayerID); +} - m_Simulation2->SetCurrentDisplayedPlayer(g_Game->GetPlayerID()); +int CGame::GetViewedPlayerID() +{ + return m_ViewedPlayerID; +} + +void CGame::SetViewedPlayerID(player_id_t playerID) +{ + m_ViewedPlayerID = playerID; } void CGame::StartGame(JS::MutableHandleValue attribs, const std::string& savedState) diff --git a/source/ps/Game.h b/source/ps/Game.h index 33e1c15199..862ae4deb4 100644 --- a/source/ps/Game.h +++ b/source/ps/Game.h @@ -40,29 +40,43 @@ struct CColor; class CGame { NONCOPYABLE(CGame); + /** * pointer to the CWorld object representing the game world. **/ CWorld *m_World; + /** * pointer to the CSimulation2 object operating on the game world. **/ CSimulation2 *m_Simulation2; + /** * pointer to the CGameView object representing the view into the game world. **/ CGameView *m_GameView; + /** * the game has been initialized and ready for use if true. **/ bool m_GameStarted; + /** * Timescale multiplier for simulation rate. **/ float m_SimRate; + /** + * Index assigned to the current player. + * 1-8 to control players, 0 for gaia, -1 for observer. + */ player_id_t m_PlayerID; + /** + * Differs from m_PlayerID if a defeated player or observer views another player. + */ + player_id_t m_ViewedPlayerID; + CNetTurnManager* m_TurnManager; public: @@ -95,6 +109,9 @@ public: int GetPlayerID(); void SetPlayerID(player_id_t playerID); + int GetViewedPlayerID(); + void SetViewedPlayerID(player_id_t playerID); + /** * Retrieving player colors from scripts is slow, so this updates an * internal cache of all players' colors. diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 7b978e1deb..160c73b4b3 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -607,11 +607,6 @@ void CSimulation2::EnableSerializationTest() m->m_EnableSerializationTest = true; } -void CSimulation2::SetCurrentDisplayedPlayer(int playerID) -{ - m->m_SimContext.SetCurrentDisplayedPlayer(playerID); -} - entity_id_t CSimulation2::AddEntity(const std::wstring& templateName) { return m->m_ComponentManager.AddEntity(templateName, m->m_ComponentManager.AllocateNewEntity()); @@ -662,7 +657,7 @@ const CSimulation2::InterfaceListUnordered& CSimulation2::GetEntitiesWithInterfa return m->m_ComponentManager.GetEntitiesWithInterfaceUnordered(iid); } -CSimContext& CSimulation2::GetSimContext() const +const CSimContext& CSimulation2::GetSimContext() const { return m->m_SimContext; } diff --git a/source/simulation2/Simulation2.h b/source/simulation2/Simulation2.h index 1bf4e812d7..5a0a240c8e 100644 --- a/source/simulation2/Simulation2.h +++ b/source/simulation2/Simulation2.h @@ -57,8 +57,6 @@ public: void EnableOOSLog(); void EnableSerializationTest(); - void SetCurrentDisplayedPlayer(int playerID); - /** * Load all scripts in the specified directory (non-recursively), * so they can register new component types and functions. This @@ -219,7 +217,7 @@ public: */ const InterfaceListUnordered& GetEntitiesWithInterfaceUnordered(int iid); - CSimContext& GetSimContext() const; + const CSimContext& GetSimContext() const; ScriptInterface& GetScriptInterface() const; bool ComputeStateHash(std::string& outHash, bool quick); diff --git a/source/simulation2/system/SimContext.cpp b/source/simulation2/system/SimContext.cpp index 28fe544336..8836b73ecb 100644 --- a/source/simulation2/system/SimContext.cpp +++ b/source/simulation2/system/SimContext.cpp @@ -24,7 +24,7 @@ #include "ps/Game.h" CSimContext::CSimContext() : - m_ComponentManager(NULL), m_UnitManager(NULL), m_Terrain(NULL), m_CurrentDisplayedPlayer(0) + m_ComponentManager(NULL), m_UnitManager(NULL), m_Terrain(NULL) { } @@ -67,10 +67,5 @@ ScriptInterface& CSimContext::GetScriptInterface() const int CSimContext::GetCurrentDisplayedPlayer() const { - return g_Game ? m_CurrentDisplayedPlayer : -1; -} - -void CSimContext::SetCurrentDisplayedPlayer(int player) -{ - m_CurrentDisplayedPlayer = player; + return g_Game ? g_Game->GetViewedPlayerID() : -1; } diff --git a/source/simulation2/system/SimContext.h b/source/simulation2/system/SimContext.h index 0a8c7f5949..b50d0f71da 100644 --- a/source/simulation2/system/SimContext.h +++ b/source/simulation2/system/SimContext.h @@ -53,7 +53,6 @@ public: * Currently relies on g_Game being initialised (evil globals...) */ int GetCurrentDisplayedPlayer() const; - void SetCurrentDisplayedPlayer(int player); private: CComponentManager* m_ComponentManager; @@ -62,8 +61,6 @@ private: CEntityHandle m_SystemEntity; - int m_CurrentDisplayedPlayer; - friend class CSimulation2Impl; };