diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index eb45cd1e56..0660dfe0f5 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -208,7 +208,7 @@ if (_settings_game.ai_config[c] != nullptr && _settings_game.ai_config[c]->HasScript()) { if (!_settings_game.ai_config[c]->ResetInfo(true)) { Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_game.ai_config[c]->GetName()); - _settings_game.ai_config[c]->Change(nullptr); + _settings_game.ai_config[c]->Change(std::nullopt); if (Company::IsValidAiID(c)) { /* The code belonging to an already running AI was deleted. We can only do * one thing here to keep everything sane and that is kill the AI. After @@ -225,7 +225,7 @@ if (_settings_newgame.ai_config[c] != nullptr && _settings_newgame.ai_config[c]->HasScript()) { if (!_settings_newgame.ai_config[c]->ResetInfo(false)) { Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_newgame.ai_config[c]->GetName()); - _settings_newgame.ai_config[c]->Change(nullptr); + _settings_newgame.ai_config[c]->Change(std::nullopt); } } } diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index eff3055f96..c67446f7cc 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -175,7 +175,7 @@ if (_settings_game.game_config != nullptr && _settings_game.game_config->HasScript()) { if (!_settings_game.game_config->ResetInfo(true)) { Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_game.game_config->GetName()); - _settings_game.game_config->Change(nullptr); + _settings_game.game_config->Change(std::nullopt); if (Game::instance != nullptr) { delete Game::instance; Game::instance = nullptr; @@ -188,7 +188,7 @@ if (_settings_newgame.game_config != nullptr && _settings_newgame.game_config->HasScript()) { if (!_settings_newgame.game_config->ResetInfo(false)) { Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_newgame.game_config->GetName()); - _settings_newgame.game_config->Change(nullptr); + _settings_newgame.game_config->Change(std::nullopt); } } } diff --git a/src/openttd.cpp b/src/openttd.cpp index 97face4e6c..b048f1c6c9 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -373,7 +373,7 @@ void MakeNewgameSettingsLive() if (_settings_newgame.ai_config[c] != nullptr) { _settings_game.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]); if (!AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->HasScript()) { - AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr); + AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(std::nullopt); } } } diff --git a/src/saveload/ai_sl.cpp b/src/saveload/ai_sl.cpp index 90e96ba9a2..927c130470 100644 --- a/src/saveload/ai_sl.cpp +++ b/src/saveload/ai_sl.cpp @@ -66,7 +66,7 @@ struct AIPLChunkHandler : ChunkHandler { /* Free all current data */ for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { - AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr); + AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(std::nullopt); } CompanyID index; @@ -85,13 +85,13 @@ struct AIPLChunkHandler : ChunkHandler { AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME); if (_ai_saveload_name.empty()) { /* A random AI. */ - config->Change(nullptr, -1, false, true); + config->Change(std::nullopt, -1, false, true); } else { - config->Change(_ai_saveload_name.c_str(), _ai_saveload_version, false, _ai_saveload_is_random); + config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random); if (!config->HasScript()) { /* No version of the AI available that can load the data. Try to load the * latest version of the AI instead. */ - config->Change(_ai_saveload_name.c_str(), -1, false, _ai_saveload_is_random); + config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random); if (!config->HasScript()) { if (_ai_saveload_name.compare("%_dummy") != 0) { Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index 9e938bafb3..1eb6539710 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -62,7 +62,7 @@ struct GSDTChunkHandler : ChunkHandler { const std::vector slt = SlCompatTableHeader(_game_script_desc, _game_script_sl_compat); /* Free all current data */ - GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(nullptr); + GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(std::nullopt); if (SlIterateArray() == -1) return; @@ -77,11 +77,11 @@ struct GSDTChunkHandler : ChunkHandler { GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME); if (!_game_saveload_name.empty()) { - config->Change(_game_saveload_name.c_str(), _game_saveload_version, false, _game_saveload_is_random); + config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random); if (!config->HasScript()) { /* No version of the GameScript available that can load the data. Try to load the * latest version of the GameScript instead. */ - config->Change(_game_saveload_name.c_str(), -1, false, _game_saveload_is_random); + config->Change(_game_saveload_name, -1, false, _game_saveload_is_random); if (!config->HasScript()) { if (_game_saveload_name.compare("%_dummy") != 0) { Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version); diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 6e9514e140..148279c902 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -17,10 +17,14 @@ #include "../safeguards.h" -void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random) +void ScriptConfig::Change(std::optional name, int version, bool force_exact_match, bool is_random) { - this->name = (name == nullptr) ? "" : name; - this->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match); + if (name.has_value()) { + this->name = std::move(name.value()); + this->info = this->FindInfo(this->name, version, force_exact_match); + } else { + this->info = nullptr; + } this->version = (info == nullptr) ? -1 : info->GetVersion(); this->is_random = is_random; this->config_list.reset(); diff --git a/src/script/script_config.hpp b/src/script/script_config.hpp index 22d5832fd7..cc9e62f702 100644 --- a/src/script/script_config.hpp +++ b/src/script/script_config.hpp @@ -83,7 +83,7 @@ public: * as specified. If false any compatible version is ok. * @param is_random Is the Script chosen randomly? */ - void Change(const char *name, int version = -1, bool force_exact_match = false, bool is_random = false); + void Change(std::optional name, int version = -1, bool force_exact_match = false, bool is_random = false); /** * Get the ScriptInfo linked to this ScriptConfig. diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 2437d22742..c47bbadee0 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -172,7 +172,7 @@ struct ScriptListWindow : public Window { void ChangeScript() { if (this->selected == -1) { - GetConfig(slot)->Change(nullptr); + GetConfig(slot)->Change(std::nullopt); } else { ScriptInfoList::const_iterator it = this->info_list->cbegin(); std::advance(it, this->selected); diff --git a/src/settings.cpp b/src/settings.cpp index 098df6761a..158945a79a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -869,7 +869,7 @@ static void AILoadConfig(IniFile &ini, const char *grpname) /* Clean any configured AI */ for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { - AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(nullptr); + AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(std::nullopt); } /* If no group exists, return */ @@ -879,7 +879,7 @@ static void AILoadConfig(IniFile &ini, const char *grpname) for (item = group->item; c < MAX_COMPANIES && item != nullptr; c++, item = item->next) { AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME); - config->Change(item->name.c_str()); + config->Change(item->name); if (!config->HasScript()) { if (item->name != "none") { Debug(script, 0, "The AI by the name '{}' was no longer found, and removed from the list.", item->name); @@ -896,7 +896,7 @@ static void GameLoadConfig(IniFile &ini, const char *grpname) IniItem *item; /* Clean any configured GameScript */ - GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(nullptr); + GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(std::nullopt); /* If no group exists, return */ if (group == nullptr) return; @@ -906,7 +906,7 @@ static void GameLoadConfig(IniFile &ini, const char *grpname) GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME); - config->Change(item->name.c_str()); + config->Change(item->name); if (!config->HasScript()) { if (item->name != "none") { Debug(script, 0, "The GameScript by the name '{}' was no longer found, and removed from the list.", item->name);