# ai_config.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../settings_type.h"
    00014 #include "../core/random_func.hpp"
    00015 #include "ai.hpp"
    00016 #include "ai_config.hpp"
    00017
    00018 void AIConfig::ChangeAI(const char *name, int version, bool force_exact_match, bool is_random_ai)
    00019 {
    00020   free((void *)this->name);
    00021   this->name = (name == NULL) ? NULL : strdup(name);
    00022   this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version, force_exact_match);
    00023   this->version = (info == NULL) ? -1 : info->GetVersion();
    00024   this->is_random_ai = is_random_ai;
    00025   if (this->config_list != NULL) delete this->config_list;
    00026   this->config_list = (info == NULL) ? NULL : new AIConfigItemList();
    00027   if (this->config_list != NULL) this->config_list->push_back(_start_date_config);
    00028
    00029   /* The special casing for start_date is here to ensure that the
    00030    *  start_date setting won't change even if you chose another AI. */
    00031   int start_date = this->GetSetting("start_date");
    00032
    00033   for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00034     free((void*)(*it).first);
    00035   }
    00036   this->settings.clear();
    00037
    00038   this->SetSetting("start_date", start_date);
    00039
    00040   if (_game_mode == GM_NORMAL && this->info != NULL) {
    00041     /* If we're in an existing game and the AI is changed, set all settings
    00042      *  for the AI that have the random flag to a random value. */
    00043     for (AIConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
    00044       if ((*it).flags & AICONFIG_RANDOM) {
    00045         this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value - (*it).min_value) + (*it).min_value);
    00046       }
    00047     }
    00048     this->AddRandomDeviation();
    00049   }
    00050 }
    00051
    00052 AIConfig::AIConfig(const AIConfig *config)
    00053 {
    00054   this->name = (config->name == NULL) ? NULL : strdup(config->name);
    00055   this->info = config->info;
    00056   this->version = config->version;
    00057   this->config_list = NULL;
    00058   this->is_random_ai = config->is_random_ai;
    00059
    00060   for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
    00061     this->settings[strdup((*it).first)] = (*it).second;
    00062   }
    00063   this->AddRandomDeviation();
    00064 }
    00065
    00066 AIConfig::~AIConfig()
    00067 {
    00068   free((void *)this->name);
    00069   this->ResetSettings();
    00070   if (this->config_list != NULL) delete this->config_list;
    00071 }
    00072
    00073 AIInfo *AIConfig::GetInfo() const
    00074 {
    00075   return this->info;
    00076 }
    00077
    00078 bool AIConfig::ResetInfo()
    00079 {
    00080   this->info = AI::FindInfo(this->name, -1, false);
    00081   return this->info != NULL;
    00082 }
    00083
    00084 const AIConfigItemList *AIConfig::GetConfigList()
    00085 {
    00086   if (this->info != NULL) return this->info->GetConfigList();
    00087   if (this->config_list == NULL) {
    00088     this->config_list = new AIConfigItemList();
    00089     this->config_list->push_back(_start_date_config);
    00090   }
    00091   return this->config_list;
    00092 }
    00093
    00094 AIConfig *AIConfig::GetConfig(CompanyID company, AISettingSource source)
    00095 {
    00096   AIConfig **config;
    00097   if (source == AISS_FORCE_NEWGAME || (source == AISS_DEFAULT && _game_mode == GM_MENU)) {
    00098     config = &_settings_newgame.ai_config[company];
    00099   } else {
    00100     config = &_settings_game.ai_config[company];
    00101   }
    00102   if (*config == NULL) *config = new AIConfig();
    00103   return *config;
    00104 }
    00105
    00106 int AIConfig::GetSetting(const char *name) const
    00107 {
    00108   SettingValueList::const_iterator it = this->settings.find(name);
    00109   /* Return the default value if the setting is not set, or if we are in a not-custom difficult level */
    00110   if (it == this->settings.end() || GetGameSettings().difficulty.diff_level != 3) {
    00111     if (this->info == NULL) {
    00112       assert(strcmp("start_date", name) == 0);
    00113       switch (GetGameSettings().difficulty.diff_level) {
    00114         case 0: return AI::START_NEXT_EASY;
    00115         case 1: return AI::START_NEXT_MEDIUM;
    00116         case 2: return AI::START_NEXT_HARD;
    00117         case 3: return AI::START_NEXT_MEDIUM;
    00118         default: NOT_REACHED();
    00119       }
    00120     }
    00121     return this->info->GetSettingDefaultValue(name);
    00122   }
    00123   return (*it).second;
    00124 }
    00125
    00126 void AIConfig::SetSetting(const char *name, int value)
    00127 {
    00128   /* You can only set ai specific settings if an AI is selected. */
    00129   if (this->info == NULL && strcmp("start_date", name) != 0) return;
    00130
    00131   if (this->info == NULL && strcmp("start_date", name) == 0) {
    00132     value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
    00133   } else {
    00134     const AIConfigItem *config_item = this->info->GetConfigItem(name);
    00135     if (config_item == NULL) return;
    00136
    00137     value = Clamp(value, config_item->min_value, config_item->max_value);
    00138   }
    00139
    00140   SettingValueList::iterator it = this->settings.find(name);
    00141   if (it != this->settings.end()) {
    00142     (*it).second = value;
    00143   } else {
    00144     this->settings[strdup(name)] = value;
    00145   }
    00146 }
    00147
    00148 void AIConfig::ResetSettings()
    00149 {
    00150   for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00151     free((void*)(*it).first);
    00152   }
    00153   this->settings.clear();
    00154 }
    00155
    00156 void AIConfig::AddRandomDeviation()
    00157 {
    00158   for (AIConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
    00159     if ((*it).random_deviation != 0) {
    00160       this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
    00161     }
    00162   }
    00163 }
    00164
    00165 bool AIConfig::HasAI() const
    00166 {
    00167   return this->info != NULL;
    00168 }
    00169
    00170 bool AIConfig::IsRandomAI() const
    00171 {
    00172   return this->is_random_ai;
    00173 }
    00174
    00175 const char *AIConfig::GetName() const
    00176 {
    00177   return this->name;
    00178 }
    00179
    00180 int AIConfig::GetVersion() const
    00181 {
    00182   return this->version;
    00183 }
    00184
    00185 void AIConfig::StringToSettings(const char *value)
    00186 {
    00187   char *value_copy = strdup(value);
    00188   char *s = value_copy;
    00189
    00190   while (s != NULL) {
    00191     /* Analyze the string ('name=value,name=value\0') */
    00192     char *item_name = s;
    00193     s = strchr(s, '=');
    00194     if (s == NULL) break;
    00195     if (*s == '\0') break;
    00196     *s = '\0';
    00197     s++;
    00198
    00199     char *item_value = s;
    00200     s = strchr(s, ',');
    00201     if (s != NULL) {
    00202       *s = '\0';
    00203       s++;
    00204     }
    00205
    00206     this->SetSetting(item_name, atoi(item_value));
    00207   }
    00208   free(value_copy);
    00209 }
    00210
    00211 void AIConfig::SettingsToString(char *string, size_t size) const
    00212 {
    00213   string[0] = '\0';
    00214   for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00215     char no[10];
    00216     snprintf(no, sizeof(no), "%d", (*it).second);
    00217
    00218     /* Check if the string would fit in the destination */
    00219     size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
    00220     /* If it doesn't fit, skip the next settings */
    00221     if (size <= needed_size) break;
    00222     size -= needed_size;
    00223
    00224     strcat(string, (*it).first);
    00225     strcat(string, "=");
    00226     strcat(string, no);
    00227     strcat(string, ",");
    00228   }
    00229   /* Remove the last ',', but only if at least one setting was saved. */
    00230   size_t len = strlen(string);
    00231   if (len > 0) string[len - 1] = '\0';
    00232 }