diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index e8605fb39..9e31e2148 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -516,7 +516,7 @@ void CServerHandler::sendStartGame(bool allowOnlyAI) const sendLobbyPack(lsg); } -void CServerHandler::startGameplay() +void CServerHandler::startGameplay(CGameState * gameState) { if(CMM) CMM->disable(); @@ -525,10 +525,10 @@ void CServerHandler::startGameplay() switch(si->mode) { case StartInfo::NEW_GAME: - client->newGame(); + client->newGame(gameState); break; case StartInfo::CAMPAIGN: - client->newGame(); + client->newGame(gameState); break; case StartInfo::LOAD_GAME: client->loadGame(); diff --git a/client/CServerHandler.h b/client/CServerHandler.h index 4650bf4d9..4306161f7 100644 --- a/client/CServerHandler.h +++ b/client/CServerHandler.h @@ -21,6 +21,7 @@ class PlayerColor; struct StartInfo; class CMapInfo; +class CGameState; struct ClientPlayer; struct CPack; struct CPackForLobby; @@ -142,7 +143,7 @@ public: void sendGuiAction(ui8 action) const override; void sendStartGame(bool allowOnlyAI = false) const override; - void startGameplay(); + void startGameplay(CGameState * gameState = nullptr); void endGameplay(bool closeConnection = true, bool restart = false); void startCampaignScenario(std::shared_ptr cs = {}); void showServerError(std::string txt); diff --git a/client/Client.cpp b/client/Client.cpp index e145c1b12..8e4a82848 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -180,14 +180,15 @@ events::EventBus * CClient::eventBus() const return clientEventBus.get(); } -void CClient::newGame() +void CClient::newGame(CGameState * gameState) { CSH->th->update(); CMapService mapService; - gs = new CGameState(); + gs = gameState ? gameState : new CGameState(); gs->preInit(VLC); logNetwork->trace("\tCreating gamestate: %i", CSH->th->getDiff()); - gs->init(&mapService, CSH->si.get(), settings["general"]["saveRandomMaps"].Bool()); + if(!gameState) + gs->init(&mapService, CSH->si.get(), settings["general"]["saveRandomMaps"].Bool()); logNetwork->trace("Initializing GameState (together): %d ms", CSH->th->getDiff()); initMapHandler(); diff --git a/client/Client.h b/client/Client.h index 569277e93..be7e87091 100644 --- a/client/Client.h +++ b/client/Client.h @@ -150,7 +150,7 @@ public: vstd::CLoggerBase * logger() const override; events::EventBus * eventBus() const override; - void newGame(); + void newGame(CGameState * gameState); void loadGame(); void serialize(BinarySerializer & h, const int version); void serialize(BinaryDeserializer & h, const int version); diff --git a/client/NetPacksLobbyClient.cpp b/client/NetPacksLobbyClient.cpp index 88df3c774..66b89981b 100644 --- a/client/NetPacksLobbyClient.cpp +++ b/client/NetPacksLobbyClient.cpp @@ -111,7 +111,7 @@ bool LobbyStartGame::applyOnLobbyHandler(CServerHandler * handler) void LobbyStartGame::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler) { - GH.pushIntT(std::bind(&CServerHandler::startGameplay, handler)); + GH.pushIntT(std::bind(&CServerHandler::startGameplay, handler, nullptr)); } bool LobbyUpdateState::applyOnLobbyHandler(CServerHandler * handler) diff --git a/lib/NetPacksLobby.h b/lib/NetPacksLobby.h index 0f70fb2e8..d03c473c6 100644 --- a/lib/NetPacksLobby.h +++ b/lib/NetPacksLobby.h @@ -142,8 +142,9 @@ struct LobbyStartGame : public CLobbyPackToPropagate { // Set by server std::shared_ptr initializedStartInfo; + CGameState * initializedGameState; - LobbyStartGame() : initializedStartInfo(nullptr) {} + LobbyStartGame() : initializedStartInfo(nullptr), initializedGameState(nullptr) {} bool checkClientPermissions(CVCMIServer * srv) const; bool applyOnServer(CVCMIServer * srv); void applyOnServerAfterAnnounce(CVCMIServer * srv); @@ -153,6 +154,10 @@ struct LobbyStartGame : public CLobbyPackToPropagate template void serialize(Handler &h, const int version) { h & initializedStartInfo; + bool sps = h.smartPointerSerialization; + h.smartPointerSerialization = true; + h & initializedGameState; + h.smartPointerSerialization = sps; } }; diff --git a/server/NetPacksLobbyServer.cpp b/server/NetPacksLobbyServer.cpp index 5885630fd..0e2ab7c92 100644 --- a/server/NetPacksLobbyServer.cpp +++ b/server/NetPacksLobbyServer.cpp @@ -181,6 +181,8 @@ bool LobbyStartGame::applyOnServer(CVCMIServer * srv) return false; initializedStartInfo = std::make_shared(*srv->gh->getStartInfo(true)); + initializedGameState = srv->gh->gameState(); + return true; }