瀏覽代碼

Pass whole gamestate over network

nordsoft 3 年之前
父節點
當前提交
7ee4fca120
共有 7 個文件被更改,包括 19 次插入10 次删除
  1. 3 3
      client/CServerHandler.cpp
  2. 2 1
      client/CServerHandler.h
  3. 4 3
      client/Client.cpp
  4. 1 1
      client/Client.h
  5. 1 1
      client/NetPacksLobbyClient.cpp
  6. 6 1
      lib/NetPacksLobby.h
  7. 2 0
      server/NetPacksLobbyServer.cpp

+ 3 - 3
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();

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

+ 4 - 3
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();

+ 1 - 1
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);

+ 1 - 1
client/NetPacksLobbyClient.cpp

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

+ 6 - 1
lib/NetPacksLobby.h

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

+ 2 - 0
server/NetPacksLobbyServer.cpp

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