1
0
Fork 0

Codechange: Simplify AI/GameConfig::GetConfig. (#13900)

Reorganise these methods to reduce nesting and remove a raw pointer to a unique_ptr.
pull/13901/head
Peter Nelson 2025-03-26 21:53:05 +00:00 committed by GitHub
parent 325f7f9767
commit ff08a22aa4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 19 deletions

View File

@ -21,18 +21,17 @@
{ {
assert(company < MAX_COMPANIES); assert(company < MAX_COMPANIES);
std::unique_ptr<AIConfig> *config; if (_game_mode == GM_MENU) source = SSS_FORCE_NEWGAME;
if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
config = &_settings_newgame.script_config.ai[company]; if (source == SSS_DEFAULT) {
} else { Company *c = Company::GetIfValid(company);
if (source != SSS_FORCE_GAME) { if (c != nullptr && c->ai_config != nullptr) return c->ai_config.get();
Company *c = Company::GetIfValid(company);
if (c != nullptr && c->ai_config != nullptr) return c->ai_config.get();
}
config = &_settings_game.script_config.ai[company];
} }
if (*config == nullptr) *config = std::make_unique<AIConfig>();
return config->get(); auto &config = (source == SSS_FORCE_NEWGAME) ? _settings_newgame.script_config.ai[company] : _settings_game.script_config.ai[company];
if (config == nullptr) config = std::make_unique<AIConfig>();
return config.get();
} }
class AIInfo *AIConfig::GetInfo() const class AIInfo *AIConfig::GetInfo() const

View File

@ -17,14 +17,12 @@
/* static */ GameConfig *GameConfig::GetConfig(ScriptSettingSource source) /* static */ GameConfig *GameConfig::GetConfig(ScriptSettingSource source)
{ {
std::unique_ptr<GameConfig> *config; if (_game_mode == GM_MENU) source = SSS_FORCE_NEWGAME;
if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
config = &_settings_newgame.script_config.game; auto &config = (source == SSS_FORCE_NEWGAME) ? _settings_newgame.script_config.game : _settings_game.script_config.game;
} else { if (config == nullptr) config = std::make_unique<GameConfig>();
config = &_settings_game.script_config.game;
} return config.get();
if (*config == nullptr) *config = std::make_unique<GameConfig>();
return config->get();
} }
class GameInfo *GameConfig::GetInfo() const class GameInfo *GameConfig::GetInfo() const