1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Pass whole gamestate over network

This commit is contained in:
nordsoft 2022-09-28 23:15:05 +04:00
parent 4a7d290112
commit 7ee4fca120
7 changed files with 19 additions and 10 deletions

View File

@ -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();

View File

@ -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<CCampaignState> cs = {});
void showServerError(std::string txt);

View File

@ -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();

View File

@ -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);

View File

@ -111,7 +111,7 @@ bool LobbyStartGame::applyOnLobbyHandler(CServerHandler * handler)
void LobbyStartGame::applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler)
{
GH.pushIntT<CLoadingScreen>(std::bind(&CServerHandler::startGameplay, handler));
GH.pushIntT<CLoadingScreen>(std::bind(&CServerHandler::startGameplay, handler, nullptr));
}
bool LobbyUpdateState::applyOnLobbyHandler(CServerHandler * handler)

View File

@ -142,8 +142,9 @@ struct LobbyStartGame : public CLobbyPackToPropagate
{
// Set by server
std::shared_ptr<StartInfo> 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 <typename Handler> void serialize(Handler &h, const int version)
{
h & initializedStartInfo;
bool sps = h.smartPointerSerialization;
h.smartPointerSerialization = true;
h & initializedGameState;
h.smartPointerSerialization = sps;
}
};

View File

@ -181,6 +181,8 @@ bool LobbyStartGame::applyOnServer(CVCMIServer * srv)
return false;
initializedStartInfo = std::make_shared<StartInfo>(*srv->gh->getStartInfo(true));
initializedGameState = srv->gh->gameState();
return true;
}