summaryrefslogtreecommitdiff
path: root/src/ai/ai_config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ai/ai_config.cpp')
-rw-r--r--src/ai/ai_config.cpp55
1 files changed, 49 insertions, 6 deletions
diff --git a/src/ai/ai_config.cpp b/src/ai/ai_config.cpp
index cc309b62f..d09fc39c0 100644
--- a/src/ai/ai_config.cpp
+++ b/src/ai/ai_config.cpp
@@ -17,11 +17,17 @@ void AIConfig::ChangeAI(const char *name, int version)
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version);
this->version = (info == NULL) ? -1 : info->GetVersion();
+ /* The special casing for start_date is here to ensure that the
+ * start_date setting won't change even if you chose another AI. */
+ int start_date = this->GetSetting("start_date");
+
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
free((void*)(*it).first);
}
this->settings.clear();
+ this->SetSetting("start_date", start_date);
+
if (_game_mode == GM_NORMAL && this->info != NULL) {
/* If we're in an existing game and the AI is changed, set all settings
* for the AI that have the random flag to a random value. */
@@ -30,6 +36,7 @@ void AIConfig::ChangeAI(const char *name, int version)
this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value - (*it).min_value) + (*it).min_value);
}
}
+ this->AddRandomDeviation();
}
}
@@ -43,6 +50,7 @@ AIConfig::AIConfig(const AIConfig *config)
for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
this->settings[strdup((*it).first)] = (*it).second;
}
+ this->AddRandomDeviation();
}
AIConfig::~AIConfig()
@@ -75,11 +83,19 @@ AIConfig *AIConfig::GetConfig(CompanyID company, bool forceNewgameSetting)
int AIConfig::GetSetting(const char *name)
{
- assert(this->info != NULL);
-
SettingValueList::iterator it = this->settings.find(name);
/* Return the default value if the setting is not set, or if we are in a not-custom difficult level */
if (it == this->settings.end() || ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) != 3) {
+ if (this->info == NULL) {
+ assert(strcmp("start_date", name) == 0);
+ switch ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) {
+ case 0: return AI::START_NEXT_EASY;
+ case 1: return AI::START_NEXT_MEDIUM;
+ case 2: return AI::START_NEXT_HARD;
+ case 3: return AI::START_NEXT_MEDIUM;
+ default: NOT_REACHED();
+ }
+ }
return this->info->GetSettingDefaultValue(name);
}
return (*it).second;
@@ -88,12 +104,16 @@ int AIConfig::GetSetting(const char *name)
void AIConfig::SetSetting(const char *name, int value)
{
/* You can only set ai specific settings if an AI is selected. */
- assert(this->info != NULL);
+ if (this->info == NULL && strcmp("start_date", name) != 0) return;
- const AIConfigItem *config_item = this->info->GetConfigItem(name);
- if (config_item == NULL) return;
+ if (this->info == NULL && strcmp("start_date", name) == 0) {
+ value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
+ } else {
+ const AIConfigItem *config_item = this->info->GetConfigItem(name);
+ if (config_item == NULL) return;
- value = Clamp(value, config_item->min_value, config_item->max_value);
+ value = Clamp(value, config_item->min_value, config_item->max_value);
+ }
SettingValueList::iterator it = this->settings.find(name);
if (it != this->settings.end()) {
@@ -103,6 +123,29 @@ void AIConfig::SetSetting(const char *name, int value)
}
}
+void AIConfig::AddRandomDeviation()
+{
+ /* No AI configured, so fall back to some defaults */
+ if (this->info == NULL) {
+ int base_start_date;
+ switch (_settings_game.difficulty.diff_level) {
+ case 0: base_start_date = AI::START_NEXT_EASY; break;
+ case 1: base_start_date = AI::START_NEXT_MEDIUM; break;
+ case 2: base_start_date = AI::START_NEXT_HARD; break;
+ case 3: base_start_date = AI::START_NEXT_MEDIUM; break;
+ default: NOT_REACHED();
+ }
+ this->SetSetting("start_date", InteractiveRandomRange(AI::START_NEXT_DEVIATION * 2) - AI::START_NEXT_DEVIATION + base_start_date);
+ return;
+ }
+
+ for (AIConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
+ if ((*it).random_deviation != 0) {
+ this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
+ }
+ }
+}
+
bool AIConfig::HasAI()
{
return this->info != NULL;