1
0
Fork 0

Codechange: Use std::unique_ptr for ai/game config.

pull/13896/head
Peter Nelson 2025-03-23 23:29:33 +00:00 committed by Peter Nelson
parent 7f3820fa7e
commit 422ff9dbd8
12 changed files with 86 additions and 73 deletions

View File

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

View File

@ -24,7 +24,7 @@ public:
ScriptConfig()
{}
AIConfig(const AIConfig *config) :
AIConfig(const AIConfig &config) :
ScriptConfig(config)
{}

View File

@ -45,7 +45,7 @@
AIConfig *config = c->ai_config.get();
if (config == nullptr) {
c->ai_config = std::make_unique<AIConfig>(AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME));
c->ai_config = std::make_unique<AIConfig>(*AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME));
config = c->ai_config.get();
}
@ -189,14 +189,8 @@
AI::scanner_library.reset();
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
if (_settings_game.ai_config[c] != nullptr) {
delete _settings_game.ai_config[c];
_settings_game.ai_config[c] = nullptr;
}
if (_settings_newgame.ai_config[c] != nullptr) {
delete _settings_newgame.ai_config[c];
_settings_newgame.ai_config[c] = nullptr;
}
_settings_game.script_config.ai[c].reset();
_settings_newgame.script_config.ai[c].reset();
}
}
}
@ -207,17 +201,17 @@
* the AIConfig. If not, remove the AI from the list (which will assign
* a random new AI on reload). */
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
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(std::nullopt);
if (_settings_game.script_config.ai[c] != nullptr && _settings_game.script_config.ai[c]->HasScript()) {
if (!_settings_game.script_config.ai[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.script_config.ai[c]->GetName());
_settings_game.script_config.ai[c]->Change(std::nullopt);
}
}
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(std::nullopt);
if (_settings_newgame.script_config.ai[c] != nullptr && _settings_newgame.script_config.ai[c]->HasScript()) {
if (!_settings_newgame.script_config.ai[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.script_config.ai[c]->GetName());
_settings_newgame.script_config.ai[c]->Change(std::nullopt);
}
}

View File

@ -258,7 +258,7 @@ struct AIConfigWindow : public Window {
case WID_AIC_MOVE_UP:
if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
std::swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]);
std::swap(GetGameSettings().script_config.ai[this->selected_slot], GetGameSettings().script_config.ai[this->selected_slot - 1]);
this->selected_slot = CompanyID(this->selected_slot - 1);
this->vscroll->ScrollTowards(this->selected_slot.base());
this->InvalidateData();
@ -267,7 +267,7 @@ struct AIConfigWindow : public Window {
case WID_AIC_MOVE_DOWN:
if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
std::swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]);
std::swap(GetGameSettings().script_config.ai[this->selected_slot], GetGameSettings().script_config.ai[this->selected_slot + 1]);
++this->selected_slot;
this->vscroll->ScrollTowards(this->selected_slot.base());
this->InvalidateData();

View File

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

View File

@ -23,7 +23,7 @@ public:
ScriptConfig()
{}
GameConfig(const GameConfig *config) :
GameConfig(const GameConfig &config) :
ScriptConfig(config)
{}

View File

@ -114,14 +114,8 @@
Game::scanner_info.reset();
Game::scanner_library.reset();
if (_settings_game.game_config != nullptr) {
delete _settings_game.game_config;
_settings_game.game_config = nullptr;
}
if (_settings_newgame.game_config != nullptr) {
delete _settings_newgame.game_config;
_settings_newgame.game_config = nullptr;
}
_settings_game.script_config.game.reset();
_settings_newgame.script_config.game.reset();
}
}
@ -164,22 +158,22 @@
{
/* Check for both newgame as current game if we can reload the GameInfo inside
* the GameConfig. If not, remove the Game from the list. */
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(std::nullopt);
if (_settings_game.script_config.game != nullptr && _settings_game.script_config.game->HasScript()) {
if (!_settings_game.script_config.game->ResetInfo(true)) {
Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_game.script_config.game->GetName());
_settings_game.script_config.game->Change(std::nullopt);
if (Game::instance != nullptr) {
Game::instance.reset();
Game::info = nullptr;
}
} else if (Game::instance != nullptr) {
Game::info = _settings_game.game_config->GetInfo();
Game::info = _settings_game.script_config.game->GetInfo();
}
}
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(std::nullopt);
if (_settings_newgame.script_config.game != nullptr && _settings_newgame.script_config.game->HasScript()) {
if (!_settings_newgame.script_config.game->ResetInfo(false)) {
Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_newgame.script_config.game->GetName());
_settings_newgame.script_config.game->Change(std::nullopt);
}
}
}

View File

@ -344,29 +344,14 @@ static void LoadIntroGame(bool load_newgrfs = true)
void MakeNewgameSettingsLive()
{
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
if (_settings_game.ai_config[c] != nullptr) {
delete _settings_game.ai_config[c];
}
}
if (_settings_game.game_config != nullptr) {
delete _settings_game.game_config;
_settings_game.script_config.ai[c].reset();
}
_settings_game.script_config.game.reset();
/* Copy newgame settings to active settings.
* Also initialise old settings needed for savegame conversion. */
_settings_game = _settings_newgame;
_old_vds = _settings_client.company.vehicle;
for (CompanyID c = CompanyID::Begin(); 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);
}
}
void OpenBrowser(const std::string &url)

View File

@ -35,14 +35,14 @@ void ScriptConfig::Change(std::optional<std::string> name, int version, bool for
this->ClearConfigList();
}
ScriptConfig::ScriptConfig(const ScriptConfig *config)
ScriptConfig::ScriptConfig(const ScriptConfig &config)
{
this->name = config->name;
this->info = config->info;
this->version = config->version;
this->name = config.name;
this->info = config.info;
this->version = config.version;
this->to_load_data.reset();
for (const auto &item : config->settings) {
for (const auto &item : config.settings) {
this->settings[item.first] = item.second;
}
}

View File

@ -66,7 +66,7 @@ public:
* Create a new Script config that is a copy of an existing config.
* @param config The object to copy.
*/
ScriptConfig(const ScriptConfig *config);
ScriptConfig(const ScriptConfig &config);
/** Delete an Script configuration. */
virtual ~ScriptConfig();

View File

@ -2009,3 +2009,31 @@ void IConsoleListSettings(const char *prefilter)
IConsolePrint(CC_HELP, "Use 'setting' command to change a value.");
}
ScriptConfigSettings::ScriptConfigSettings()
{
/* Instantiate here, because unique_ptr needs a complete type. */
}
ScriptConfigSettings::~ScriptConfigSettings()
{
/* Instantiate here, because unique_ptr needs a complete type. */
}
ScriptConfigSettings::ScriptConfigSettings(const ScriptConfigSettings &other)
{
*this = other;
}
ScriptConfigSettings &ScriptConfigSettings::operator=(const ScriptConfigSettings &other)
{
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
if (other.ai[c] != nullptr) {
this->ai[c] = std::make_unique<AIConfig>(*other.ai[c]);
}
}
if (other.game != nullptr) {
this->game = std::make_unique<GameConfig>(*other.game);
}
return *this;
}

View File

@ -595,6 +595,19 @@ struct CompanySettings {
VehicleDefaultSettings vehicle{}; ///< default settings for vehicles
};
/** Container for AI and Game script configuration. */
struct ScriptConfigSettings
{
ReferenceThroughBaseContainer<std::array<std::unique_ptr<class AIConfig>, MAX_COMPANIES>> ai; ///< settings per company
std::unique_ptr<class GameConfig> game; ///< settings for gamescript
ScriptConfigSettings();
~ScriptConfigSettings();
ScriptConfigSettings(const ScriptConfigSettings &other);
ScriptConfigSettings &operator=(const ScriptConfigSettings &other);
};
/** All settings together for the game. */
struct GameSettings {
DifficultySettings difficulty; ///< settings related to the difficulty
@ -602,8 +615,7 @@ struct GameSettings {
ConstructionSettings construction; ///< construction of things in-game
AISettings ai; ///< what may the AI do?
ScriptSettings script; ///< settings for scripts
ReferenceThroughBaseContainer<std::array<class AIConfig *, MAX_COMPANIES>> ai_config; ///< settings per company
class GameConfig *game_config; ///< settings for gamescript
ScriptConfigSettings script_config; ///< AI and Gamescript configuration.
PathfinderSettings pf; ///< settings for all pathfinders
OrderSettings order; ///< settings related to orders
VehicleSettings vehicle; ///< options for vehicles