Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: AI/GS settings with the flag SCRIPTCONFIG_RANDOM could be altered after loading from a savegame. #7486

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/ai/ai_config.cpp
Expand Up @@ -35,11 +35,16 @@ ScriptConfigItem _start_date_config = {

AIConfig::AIConfig(const AIConfig *config) : ScriptConfig(config)
{
/* Override start_date as per AIConfig::AddRandomDeviation().
* This is necessary because the ScriptConfig constructor will instead call
* ScriptConfig::AddRandomDeviation(). */
int start_date = config->GetSetting("start_date");
this->SetSetting("start_date", start_date != 0 ? max(1, this->GetSetting("start_date")) : 0);
if (this->info == nullptr) {
this->PushExtraConfigList();
this->AddRandomDeviation();
} else {
/* Override start_date as per AIConfig::AddRandomDeviation().
* This is necessary because the ScriptConfig constructor will instead call
* ScriptConfig::AddRandomDeviation(). */
int start_date = config->GetSetting("start_date");
this->SetSetting("start_date", start_date != 0 ? max(1, this->GetSetting("start_date")) : 0);
}
}

/* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
Expand Down Expand Up @@ -126,13 +131,17 @@ void AIConfig::SetSetting(const char *name, int value)
ScriptConfig::SetSetting(name, value);
}

void AIConfig::AddRandomDeviation()
void AIConfig::AddRandomDeviation(bool all)
{
int start_date = this->GetSetting("start_date");

ScriptConfig::AddRandomDeviation();

/* start_date = 0 is a special case, where random deviation does not occur.
* If start_date was not already 0, then a minimum value of 1 must apply. */
this->SetSetting("start_date", start_date != 0 ? max(1, this->GetSetting("start_date")) : 0);
if (all && start_date != 0) {
start_date = max(1, this->GetSetting("start_date"));
}

this->SetSetting("start_date", start_date);
}
2 changes: 1 addition & 1 deletion src/ai/ai_config.hpp
Expand Up @@ -30,7 +30,7 @@ class AIConfig : public ScriptConfig {

int GetSetting(const char *name) const override;
void SetSetting(const char *name, int value) override;
void AddRandomDeviation() override;
void AddRandomDeviation(bool all = true) override;

/**
* When ever the AI Scanner is reloaded, all infos become invalid. This
Expand Down
6 changes: 3 additions & 3 deletions src/script/script_config.cpp
Expand Up @@ -29,15 +29,15 @@ void ScriptConfig::Change(const char *name, int version, bool force_exact_match,

this->ClearConfigList();

if (_game_mode == GM_NORMAL && this->info != nullptr) {
if (_game_mode == GM_NORMAL && _switch_mode != SM_LOAD_GAME && this->info != nullptr) {
/* If we're in an existing game and the Script is changed, set all settings
* for the Script that have the random flag to a random value. */
for (ScriptConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
if ((*it).flags & SCRIPTCONFIG_RANDOM) {
this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value + 1 - (*it).min_value) + (*it).min_value);
}
}
this->AddRandomDeviation();
this->AddRandomDeviation(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the entire changeset even this large? Wouldn't it make more sense to just not call AddRandomDeviation at all if the script that's being restarted after load has already been started? So scripts that were active when the save was made have their settings static (all of them), and scripts that were not started yet can get randomized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to answer this.

The scripts are not started at the time ScriptConfig::Change is called from ai_sl.cpp / game_sl.cpp.

Fix from commit 1 already prevents AddRandomDeviation from being called after load. So I'm confused as to what you're requesting here.

}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ void ScriptConfig::ResetSettings()
this->settings.clear();
}

void ScriptConfig::AddRandomDeviation()
void ScriptConfig::AddRandomDeviation(bool all)
{
for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
if ((*it).random_deviation != 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/script/script_config.hpp
Expand Up @@ -136,8 +136,9 @@ class ScriptConfig {

/**
* Randomize all settings the Script requested to be randomized.
* @param all if set to false, then all but "start_date" settings are randomized.
*/
virtual void AddRandomDeviation();
virtual void AddRandomDeviation(bool all = true);
LordAro marked this conversation as resolved.
Show resolved Hide resolved

/**
* Is this config attached to an Script? In other words, is there a Script
Expand Down