From 67b0fec6ed4d2ac92c7f7e6507c68461ee7e2917 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Thu, 1 Feb 2024 15:42:55 +0000 Subject: [PATCH] Fix: "restart current" reproduces how the script config of the current game was when it started Restore Random AI slot upon stopping a randomly started AI. On "restart current" console command, use the seed of the current game to set the seed of scripts. Apply random deviation to non-random script settings upon starting a new game, except when using "restart current" via console, where the randomization counter is simulated but the values remain not deviated. Show random deviation range for scripts only outside normal game mode. --- src/ai/ai_core.cpp | 5 ++++- src/game/game.hpp | 3 +-- src/game/game_core.cpp | 3 +-- src/genworld.cpp | 5 ----- src/misc.cpp | 28 ++++++++++++++++++++++++++++ src/saveload/afterload.cpp | 2 +- src/script/script_config.cpp | 7 ++++++- src/script/script_gui.cpp | 6 +++--- 8 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index ac60b78ad9..06ade9d34f 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -48,7 +48,6 @@ /* Load default data and store the name in the settings */ config->Change(info->GetName(), -1, false, true); } - if (rerandomise_ai) config->AddRandomDeviation(); config->AnchorUnchangeableSettings(); Backup cur_company(_current_company, company, FILE_LINE); @@ -115,6 +114,10 @@ cur_company.Restore(); InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); + + if (AIConfig::GetConfig(company)->IsRandom()) { + AIConfig::GetConfig(company)->Change(std::nullopt); + } CloseWindowById(WC_SCRIPT_SETTINGS, company); } diff --git a/src/game/game.hpp b/src/game/game.hpp index 9d83920024..c3664cd696 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -31,9 +31,8 @@ public: /** * Start up a new GameScript. - * @param randomise Whether to randomise the configured GameScript. */ - static void StartNew(bool randomise = true); + static void StartNew(); /** * Uninitialize the Game system. diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index 51279a742a..c7763429ff 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -69,7 +69,7 @@ } } -/* static */ void Game::StartNew(bool randomise) +/* static */ void Game::StartNew() { if (Game::instance != nullptr) return; @@ -83,7 +83,6 @@ GameInfo *info = config->GetInfo(); if (info == nullptr) return; - if (randomise) config->AddRandomDeviation(); config->AnchorUnchangeableSettings(); Backup cur_company(_current_company, FILE_LINE); diff --git a/src/genworld.cpp b/src/genworld.cpp index b657273f95..bb3bc93956 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -91,11 +91,8 @@ static void _GenerateWorld() try { _generating_world = true; if (_network_dedicated) Debug(net, 3, "Generating map, please wait..."); - /* Set the Random() seed to generation_seed so we produce the same map with the same seed */ - _random.SetSeed(_settings_game.game_creation.generation_seed); SetGeneratingWorldProgress(GWP_MAP_INIT, 2); SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0); - ScriptObject::InitializeRandomizers(); BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP); @@ -307,8 +304,6 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti _settings_game.construction.map_height_limit = std::max(MAP_HEIGHT_LIMIT_AUTO_MINIMUM, std::min(MAX_MAP_HEIGHT_LIMIT, estimated_height + MAP_HEIGHT_LIMIT_AUTO_CEILING_ROOM)); } - if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = InteractiveRandom(); - /* Load the right landscape stuff, and the NewGRFs! */ GfxLoadSprites(); LoadStringWidthTable(); diff --git a/src/misc.cpp b/src/misc.cpp index dd95950dda..0e25391bce 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -8,6 +8,9 @@ /** @file misc.cpp Misc functions that shouldn't be here. */ #include "stdafx.h" +#include "ai/ai_config.hpp" +#include "game/game_config.hpp" +#include "genworld.h" #include "landscape.h" #include "news_func.h" #include "ai/ai.hpp" @@ -173,4 +176,29 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin _gamelog.Mode(); _gamelog.GRFAddList(_grfconfig); _gamelog.StopAction(); + + /* Set the Random() seed to generation_seed so we produce the same map with the same seed. */ + if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = InteractiveRandom(); + + _random.SetSeed(_settings_game.game_creation.generation_seed); + ScriptObject::InitializeRandomizers(); + + if (reset_settings) { + for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { + _settings_game.ai_config[c] = nullptr; + if (_settings_newgame.ai_config[c] != nullptr) { + _settings_game.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]); + } + } + _settings_game.game_config = nullptr; + if (_settings_newgame.game_config != nullptr) { + _settings_game.game_config = new GameConfig(_settings_newgame.game_config); + } + } + + /* Set random deviation for scripts. */ + for (auto &ai_config : _settings_game.ai_config) { + if (ai_config != nullptr) ai_config->AddRandomDeviation(); + } + if (_settings_game.game_config != nullptr) _settings_game.game_config->AddRandomDeviation(); } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 30542dd8e0..a9cf5d5049 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -553,7 +553,7 @@ static void StartScripts() } /* Start the GameScript. */ - Game::StartNew(false); + Game::StartNew(); ShowScriptDebugWindowIfScriptError(); } diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 852ffa1f43..ee350001b5 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -32,6 +32,10 @@ void ScriptConfig::Change(std::optional name, int version, bo this->to_load_data.reset(); this->ClearConfigList(); + + if (_game_mode == GM_NORMAL && _switch_mode != SM_LOAD_GAME && this->info != nullptr) { + this->AddRandomDeviation(); + } } ScriptConfig::ScriptConfig(const ScriptConfig *config) @@ -129,7 +133,8 @@ void ScriptConfig::AddRandomDeviation() { for (const auto &item : *this->GetConfigList()) { if (item.random_deviation != 0) { - this->SetSetting(item.name, ScriptObject::GetRandomizer(OWNER_NONE).Next(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name)); + uint32_t randomize = ScriptObject::GetRandomizer(OWNER_NONE).Next(item.random_deviation * 2 + 1); + if (_switch_mode != SM_RESTARTGAME) this->SetSetting(item.name, randomize - item.random_deviation + this->GetSetting(item.name)); } } } diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 7712bad358..9b077e028e 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -379,14 +379,14 @@ struct ScriptSettingsWindow : public Window { TextColour colour; uint idx = 0; if (config_item.description.empty()) { - if (this->slot != OWNER_DEITY && !Company::IsValidID(this->slot) && config_item.random_deviation != 0) { + if (this->slot != OWNER_DEITY && _game_mode != GM_NORMAL && config_item.random_deviation != 0) { str = STR_AI_SETTINGS_JUST_DEVIATION; } else { str = STR_JUST_STRING1; } colour = TC_ORANGE; } else { - if (this->slot != OWNER_DEITY && !Company::IsValidID(this->slot) && config_item.random_deviation != 0) { + if (this->slot != OWNER_DEITY && _game_mode != GM_NORMAL && config_item.random_deviation != 0) { str = STR_AI_SETTINGS_SETTING_DEVIATION; } else { str = STR_AI_SETTINGS_SETTING; @@ -405,7 +405,7 @@ struct ScriptSettingsWindow : public Window { DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value); } - if (this->slot == OWNER_DEITY || Company::IsValidID(this->slot) || config_item.random_deviation == 0) { + if (this->slot == OWNER_DEITY || _game_mode == GM_NORMAL || config_item.random_deviation == 0) { auto config_iterator = config_item.labels.find(current_value); if (config_iterator != config_item.labels.end()) { SetDParam(idx++, STR_JUST_RAW_STRING);