From 55bf75c43e4563091b40b1bf918bb7f002d61945 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sat, 1 Jun 2024 16:20:32 +0000 Subject: [PATCH] Remove no longer used random seeds from StartInfo --- lib/StartInfo.h | 19 ++++++++++-------- lib/gameState/CGameState.cpp | 37 ++---------------------------------- lib/gameState/CGameState.h | 1 - server/CGameHandler.cpp | 10 ++-------- server/CVCMIServer.cpp | 1 - test/game/CGameStateTest.cpp | 2 -- 6 files changed, 15 insertions(+), 55 deletions(-) diff --git a/lib/StartInfo.h b/lib/StartInfo.h index 0bcd64972..68ed79642 100644 --- a/lib/StartInfo.h +++ b/lib/StartInfo.h @@ -123,9 +123,6 @@ struct DLL_LINKAGE StartInfo : public Serializeable using TPlayerInfos = std::map; TPlayerInfos playerInfos; //color indexed - ui32 seedToBeUsed; //0 if not sure (client requests server to decide, will be send in reply pack) - ui32 seedPostInit; //so we know that game is correctly synced at the start; 0 if not known yet - ui32 mapfileChecksum; //0 if not relevant std::string startTimeIso8601; std::string fileURI; SimturnsInfo simturnsInfo; @@ -153,9 +150,13 @@ struct DLL_LINKAGE StartInfo : public Serializeable h & mode; h & difficulty; h & playerInfos; - h & seedToBeUsed; - h & seedPostInit; - h & mapfileChecksum; + if (h.version < Handler::Version::REMOVE_LIB_RNG) + { + uint32_t oldSeeds; + h & oldSeeds; + h & oldSeeds; + h & oldSeeds; + } h & startTimeIso8601; h & fileURI; h & simturnsInfo; @@ -169,8 +170,10 @@ struct DLL_LINKAGE StartInfo : public Serializeable h & campState; } - StartInfo() : mode(EStartMode::INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0), - mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))), fileURI("") + StartInfo() + : mode(EStartMode::INVALID) + , difficulty(1) + , startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))) { } diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index 1be6e9241..73d91053e 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -203,8 +203,6 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog } logGlobal->info("Map loaded!"); - checkMapChecksum(); - day = 0; logGlobal->debug("Initialization:"); @@ -236,18 +234,6 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog logGlobal->debug("\tChecking objectives"); map->checkForObjectives(); //needs to be run when all objects are properly placed - - auto seedAfterInit = getRandomGenerator().nextInt(); - logGlobal->info("Seed after init is %d (before was %d)", seedAfterInit, scenarioOps->seedToBeUsed); - if(scenarioOps->seedPostInit > 0) - { - //RNG must be in the same state on all machines when initialization is done (otherwise we have desync) - assert(scenarioOps->seedPostInit == seedAfterInit); - } - else - { - scenarioOps->seedPostInit = seedAfterInit; //store the post init "seed" - } } void CGameState::updateEntity(Metatype metatype, int32_t index, const JsonNode & data) @@ -307,7 +293,7 @@ void CGameState::initNewGame(const IMapService * mapService, bool allowSavingRan CStopWatch sw; // Gen map - CMapGenerator mapGenerator(*scenarioOps->mapGenOptions, callback, scenarioOps->seedToBeUsed); + CMapGenerator mapGenerator(*scenarioOps->mapGenOptions, callback, getRandomGenerator().nextInt()); progressTracking.include(mapGenerator); std::unique_ptr randomMap = mapGenerator.generate(); @@ -323,10 +309,9 @@ void CGameState::initNewGame(const IMapService * mapService, bool allowSavingRan std::shared_ptr options = scenarioOps->mapGenOptions; const std::string templateName = options->getMapTemplate()->getName(); - const ui32 seed = scenarioOps->seedToBeUsed; const std::string dt = vstd::getDateTimeISO8601Basic(std::time(nullptr)); - const std::string fileName = boost::str(boost::format("%s_%s_%d.vmap") % dt % templateName % seed ); + const std::string fileName = boost::str(boost::format("%s_%s_%d.vmap") % dt % templateName ); const auto fullPath = path / fileName; randomMap->name.appendRawString(boost::str(boost::format(" %s") % dt)); @@ -380,24 +365,6 @@ void CGameState::initCampaign() map = campaign->getCurrentMap().release(); } -void CGameState::checkMapChecksum() -{ - logGlobal->info("\tOur checksum for the map: %d", map->checksum); - if(scenarioOps->mapfileChecksum) - { - logGlobal->info("\tServer checksum for %s: %d", scenarioOps->mapname, scenarioOps->mapfileChecksum); - if(map->checksum != scenarioOps->mapfileChecksum) - { - logGlobal->error("Wrong map checksum!!!"); - throw std::runtime_error("Wrong checksum"); - } - } - else - { - scenarioOps->mapfileChecksum = map->checksum; - } -} - void CGameState::initGlobalBonuses() { const JsonNode & baseBonuses = VLC->settings()->getValue(EGameSettings::BONUSES_GLOBAL); diff --git a/lib/gameState/CGameState.h b/lib/gameState/CGameState.h index f5818d59b..efe61f927 100644 --- a/lib/gameState/CGameState.h +++ b/lib/gameState/CGameState.h @@ -201,7 +201,6 @@ public: private: // ----- initialization ----- void initNewGame(const IMapService * mapService, bool allowSavingRandomMap, Load::ProgressAccumulator & progressTracking); - void checkMapChecksum(); void initGlobalBonuses(); void initGrailPosition(); void initRandomFactionsForPlayers(); diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index a7f303981..b54ea81f7 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -548,11 +548,8 @@ void CGameHandler::reinitScripting() void CGameHandler::init(StartInfo *si, Load::ProgressAccumulator & progressTracking) { - if (si->seedToBeUsed == 0) - si->seedToBeUsed = CRandomGenerator::getDefault().nextInt(); - - logGlobal->info("Using random seed: %d", si->seedToBeUsed); - randomNumberGenerator = std::make_unique(si->seedToBeUsed); + randomNumberGenerator = std::make_unique(); + logGlobal->info("Using random seed: %d", randomNumberGenerator->nextInt()); CMapService mapService; gs = new CGameState(); @@ -561,9 +558,6 @@ void CGameHandler::init(StartInfo *si, Load::ProgressAccumulator & progressTrack gs->init(&mapService, si, progressTracking); logGlobal->info("Gamestate initialized!"); - // reset seed, so that clients can't predict any following random values - randomNumberGenerator = std::make_unique(); - for (auto & elem : gs->players) turnOrder->addPlayer(elem.first); diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index 9ce49099d..375568880 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -253,7 +253,6 @@ void CVCMIServer::prepareToRestart() } * si = * gh->gs->initialOpts; - si->seedToBeUsed = si->seedPostInit = 0; setState(EServerState::LOBBY); if (si->campState) { diff --git a/test/game/CGameStateTest.cpp b/test/game/CGameStateTest.cpp index 00da8456f..af42ad9c8 100644 --- a/test/game/CGameStateTest.cpp +++ b/test/game/CGameStateTest.cpp @@ -142,9 +142,7 @@ public: StartInfo si; si.mapname = "anything";//does not matter, map service mocked si.difficulty = 0; - si.mapfileChecksum = 0; si.mode = EStartMode::NEW_GAME; - si.seedToBeUsed = 42; std::unique_ptr header = mapService.loadMapHeader(ResourcePath(si.mapname));