mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use std::unique_ptr for ai/game config.
parent
7f3820fa7e
commit
422ff9dbd8
|
@ -21,18 +21,18 @@
|
||||||
{
|
{
|
||||||
assert(company < MAX_COMPANIES);
|
assert(company < MAX_COMPANIES);
|
||||||
|
|
||||||
AIConfig **config;
|
std::unique_ptr<AIConfig> *config;
|
||||||
if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
|
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 {
|
} else {
|
||||||
if (source != SSS_FORCE_GAME) {
|
if (source != SSS_FORCE_GAME) {
|
||||||
Company *c = Company::GetIfValid(company);
|
Company *c = Company::GetIfValid(company);
|
||||||
if (c != nullptr && c->ai_config != nullptr) return c->ai_config.get();
|
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();
|
if (*config == nullptr) *config = std::make_unique<AIConfig>();
|
||||||
return *config;
|
return config->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
class AIInfo *AIConfig::GetInfo() const
|
class AIInfo *AIConfig::GetInfo() const
|
||||||
|
|
|
@ -24,7 +24,7 @@ public:
|
||||||
ScriptConfig()
|
ScriptConfig()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
AIConfig(const AIConfig *config) :
|
AIConfig(const AIConfig &config) :
|
||||||
ScriptConfig(config)
|
ScriptConfig(config)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@
|
||||||
|
|
||||||
AIConfig *config = c->ai_config.get();
|
AIConfig *config = c->ai_config.get();
|
||||||
if (config == nullptr) {
|
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();
|
config = c->ai_config.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,14 +189,8 @@
|
||||||
AI::scanner_library.reset();
|
AI::scanner_library.reset();
|
||||||
|
|
||||||
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
|
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
|
||||||
if (_settings_game.ai_config[c] != nullptr) {
|
_settings_game.script_config.ai[c].reset();
|
||||||
delete _settings_game.ai_config[c];
|
_settings_newgame.script_config.ai[c].reset();
|
||||||
_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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -207,17 +201,17 @@
|
||||||
* the AIConfig. If not, remove the AI from the list (which will assign
|
* the AIConfig. If not, remove the AI from the list (which will assign
|
||||||
* a random new AI on reload). */
|
* a random new AI on reload). */
|
||||||
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
|
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.script_config.ai[c] != nullptr && _settings_game.script_config.ai[c]->HasScript()) {
|
||||||
if (!_settings_game.ai_config[c]->ResetInfo(true)) {
|
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.ai_config[c]->GetName());
|
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.ai_config[c]->Change(std::nullopt);
|
_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.script_config.ai[c] != nullptr && _settings_newgame.script_config.ai[c]->HasScript()) {
|
||||||
if (!_settings_newgame.ai_config[c]->ResetInfo(false)) {
|
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.ai_config[c]->GetName());
|
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.ai_config[c]->Change(std::nullopt);
|
_settings_newgame.script_config.ai[c]->Change(std::nullopt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -258,7 +258,7 @@ struct AIConfigWindow : public Window {
|
||||||
|
|
||||||
case WID_AIC_MOVE_UP:
|
case WID_AIC_MOVE_UP:
|
||||||
if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) {
|
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->selected_slot = CompanyID(this->selected_slot - 1);
|
||||||
this->vscroll->ScrollTowards(this->selected_slot.base());
|
this->vscroll->ScrollTowards(this->selected_slot.base());
|
||||||
this->InvalidateData();
|
this->InvalidateData();
|
||||||
|
@ -267,7 +267,7 @@ struct AIConfigWindow : public Window {
|
||||||
|
|
||||||
case WID_AIC_MOVE_DOWN:
|
case WID_AIC_MOVE_DOWN:
|
||||||
if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) {
|
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->selected_slot;
|
||||||
this->vscroll->ScrollTowards(this->selected_slot.base());
|
this->vscroll->ScrollTowards(this->selected_slot.base());
|
||||||
this->InvalidateData();
|
this->InvalidateData();
|
||||||
|
|
|
@ -17,14 +17,14 @@
|
||||||
|
|
||||||
/* static */ GameConfig *GameConfig::GetConfig(ScriptSettingSource source)
|
/* 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)) {
|
if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
|
||||||
config = &_settings_newgame.game_config;
|
config = &_settings_newgame.script_config.game;
|
||||||
} else {
|
} else {
|
||||||
config = &_settings_game.game_config;
|
config = &_settings_game.script_config.game;
|
||||||
}
|
}
|
||||||
if (*config == nullptr) *config = new GameConfig();
|
if (*config == nullptr) *config = std::make_unique<GameConfig>();
|
||||||
return *config;
|
return config->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
class GameInfo *GameConfig::GetInfo() const
|
class GameInfo *GameConfig::GetInfo() const
|
||||||
|
|
|
@ -23,7 +23,7 @@ public:
|
||||||
ScriptConfig()
|
ScriptConfig()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
GameConfig(const GameConfig *config) :
|
GameConfig(const GameConfig &config) :
|
||||||
ScriptConfig(config)
|
ScriptConfig(config)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
@ -114,14 +114,8 @@
|
||||||
Game::scanner_info.reset();
|
Game::scanner_info.reset();
|
||||||
Game::scanner_library.reset();
|
Game::scanner_library.reset();
|
||||||
|
|
||||||
if (_settings_game.game_config != nullptr) {
|
_settings_game.script_config.game.reset();
|
||||||
delete _settings_game.game_config;
|
_settings_newgame.script_config.game.reset();
|
||||||
_settings_game.game_config = nullptr;
|
|
||||||
}
|
|
||||||
if (_settings_newgame.game_config != nullptr) {
|
|
||||||
delete _settings_newgame.game_config;
|
|
||||||
_settings_newgame.game_config = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,22 +158,22 @@
|
||||||
{
|
{
|
||||||
/* Check for both newgame as current game if we can reload the GameInfo inside
|
/* Check for both newgame as current game if we can reload the GameInfo inside
|
||||||
* the GameConfig. If not, remove the Game from the list. */
|
* the GameConfig. If not, remove the Game from the list. */
|
||||||
if (_settings_game.game_config != nullptr && _settings_game.game_config->HasScript()) {
|
if (_settings_game.script_config.game != nullptr && _settings_game.script_config.game->HasScript()) {
|
||||||
if (!_settings_game.game_config->ResetInfo(true)) {
|
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.game_config->GetName());
|
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.game_config->Change(std::nullopt);
|
_settings_game.script_config.game->Change(std::nullopt);
|
||||||
if (Game::instance != nullptr) {
|
if (Game::instance != nullptr) {
|
||||||
Game::instance.reset();
|
Game::instance.reset();
|
||||||
Game::info = nullptr;
|
Game::info = nullptr;
|
||||||
}
|
}
|
||||||
} else if (Game::instance != 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.script_config.game != nullptr && _settings_newgame.script_config.game->HasScript()) {
|
||||||
if (!_settings_newgame.game_config->ResetInfo(false)) {
|
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.game_config->GetName());
|
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.game_config->Change(std::nullopt);
|
_settings_newgame.script_config.game->Change(std::nullopt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -344,29 +344,14 @@ static void LoadIntroGame(bool load_newgrfs = true)
|
||||||
void MakeNewgameSettingsLive()
|
void MakeNewgameSettingsLive()
|
||||||
{
|
{
|
||||||
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
|
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
|
||||||
if (_settings_game.ai_config[c] != nullptr) {
|
_settings_game.script_config.ai[c].reset();
|
||||||
delete _settings_game.ai_config[c];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (_settings_game.game_config != nullptr) {
|
|
||||||
delete _settings_game.game_config;
|
|
||||||
}
|
}
|
||||||
|
_settings_game.script_config.game.reset();
|
||||||
|
|
||||||
/* Copy newgame settings to active settings.
|
/* Copy newgame settings to active settings.
|
||||||
* Also initialise old settings needed for savegame conversion. */
|
* Also initialise old settings needed for savegame conversion. */
|
||||||
_settings_game = _settings_newgame;
|
_settings_game = _settings_newgame;
|
||||||
_old_vds = _settings_client.company.vehicle;
|
_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)
|
void OpenBrowser(const std::string &url)
|
||||||
|
|
|
@ -35,14 +35,14 @@ void ScriptConfig::Change(std::optional<std::string> name, int version, bool for
|
||||||
this->ClearConfigList();
|
this->ClearConfigList();
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptConfig::ScriptConfig(const ScriptConfig *config)
|
ScriptConfig::ScriptConfig(const ScriptConfig &config)
|
||||||
{
|
{
|
||||||
this->name = config->name;
|
this->name = config.name;
|
||||||
this->info = config->info;
|
this->info = config.info;
|
||||||
this->version = config->version;
|
this->version = config.version;
|
||||||
this->to_load_data.reset();
|
this->to_load_data.reset();
|
||||||
|
|
||||||
for (const auto &item : config->settings) {
|
for (const auto &item : config.settings) {
|
||||||
this->settings[item.first] = item.second;
|
this->settings[item.first] = item.second;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ public:
|
||||||
* Create a new Script config that is a copy of an existing config.
|
* Create a new Script config that is a copy of an existing config.
|
||||||
* @param config The object to copy.
|
* @param config The object to copy.
|
||||||
*/
|
*/
|
||||||
ScriptConfig(const ScriptConfig *config);
|
ScriptConfig(const ScriptConfig &config);
|
||||||
|
|
||||||
/** Delete an Script configuration. */
|
/** Delete an Script configuration. */
|
||||||
virtual ~ScriptConfig();
|
virtual ~ScriptConfig();
|
||||||
|
|
|
@ -2009,3 +2009,31 @@ void IConsoleListSettings(const char *prefilter)
|
||||||
|
|
||||||
IConsolePrint(CC_HELP, "Use 'setting' command to change a value.");
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -595,6 +595,19 @@ struct CompanySettings {
|
||||||
VehicleDefaultSettings vehicle{}; ///< default settings for vehicles
|
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. */
|
/** All settings together for the game. */
|
||||||
struct GameSettings {
|
struct GameSettings {
|
||||||
DifficultySettings difficulty; ///< settings related to the difficulty
|
DifficultySettings difficulty; ///< settings related to the difficulty
|
||||||
|
@ -602,8 +615,7 @@ struct GameSettings {
|
||||||
ConstructionSettings construction; ///< construction of things in-game
|
ConstructionSettings construction; ///< construction of things in-game
|
||||||
AISettings ai; ///< what may the AI do?
|
AISettings ai; ///< what may the AI do?
|
||||||
ScriptSettings script; ///< settings for scripts
|
ScriptSettings script; ///< settings for scripts
|
||||||
ReferenceThroughBaseContainer<std::array<class AIConfig *, MAX_COMPANIES>> ai_config; ///< settings per company
|
ScriptConfigSettings script_config; ///< AI and Gamescript configuration.
|
||||||
class GameConfig *game_config; ///< settings for gamescript
|
|
||||||
PathfinderSettings pf; ///< settings for all pathfinders
|
PathfinderSettings pf; ///< settings for all pathfinders
|
||||||
OrderSettings order; ///< settings related to orders
|
OrderSettings order; ///< settings related to orders
|
||||||
VehicleSettings vehicle; ///< options for vehicles
|
VehicleSettings vehicle; ///< options for vehicles
|
||||||
|
|
Loading…
Reference in New Issue