ai_config.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
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)
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);
00024 this->version = (info == NULL) ? -1 : info->GetVersion();
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
00030
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
00042
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
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
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()
00074 {
00075 return this->info;
00076 }
00077
00078 bool AIConfig::ResetInfo()
00079 {
00080 this->info = AI::FindInfo(this->name, -1);
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, bool forceNewgameSetting)
00095 {
00096 AIConfig **config;
00097 if (!forceNewgameSetting) {
00098 config = (_game_mode == GM_MENU) ? &_settings_newgame.ai_config[company] : &_settings_game.ai_config[company];
00099 } else {
00100 config = &_settings_newgame.ai_config[company];
00101 }
00102 if (*config == NULL) *config = new AIConfig();
00103 return *config;
00104 }
00105
00106 int AIConfig::GetSetting(const char *name)
00107 {
00108 SettingValueList::iterator it = this->settings.find(name);
00109
00110 if (it == this->settings.end() || ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) != 3) {
00111 if (this->info == NULL) {
00112 assert(strcmp("start_date", name) == 0);
00113 switch ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.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
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()
00166 {
00167 return this->info != NULL;
00168 }
00169
00170 const char *AIConfig::GetName()
00171 {
00172 return this->name;
00173 }
00174
00175 int AIConfig::GetVersion()
00176 {
00177 return this->version;
00178 }
00179
00180 void AIConfig::StringToSettings(const char *value)
00181 {
00182 char *value_copy = strdup(value);
00183 char *s = value_copy;
00184
00185 while (s != NULL) {
00186
00187 char *item_name = s;
00188 s = strchr(s, '=');
00189 if (s == NULL) break;
00190 if (*s == '\0') break;
00191 *s = '\0';
00192 s++;
00193
00194 char *item_value = s;
00195 s = strchr(s, ',');
00196 if (s != NULL) {
00197 *s = '\0';
00198 s++;
00199 }
00200
00201 this->SetSetting(item_name, atoi(item_value));
00202 }
00203 free(value_copy);
00204 }
00205
00206 void AIConfig::SettingsToString(char *string, size_t size)
00207 {
00208 string[0] = '\0';
00209 for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
00210 char no[10];
00211 snprintf(no, sizeof(no), "%d", (*it).second);
00212
00213
00214 size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
00215
00216 if (size <= needed_size) break;
00217 size -= needed_size;
00218
00219 strcat(string, (*it).first);
00220 strcat(string, "=");
00221 strcat(string, no);
00222 strcat(string, ",");
00223 }
00224
00225 size_t len = strlen(string);
00226 if (len > 0) string[len - 1] = '\0';
00227 }