ai_config.cpp

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

Generated on Sun Jan 9 16:01:50 2011 for OpenTTD by  doxygen 1.6.1