Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../settings_type.h"
00014 #include "../core/random_func.hpp"
00015 #include "script_info.hpp"
00016 #include "../textfile_gui.h"
00017
00018 void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random)
00019 {
00020 free(this->name);
00021 this->name = (name == NULL) ? NULL : strdup(name);
00022 this->info = (name == NULL) ? NULL : this->FindInfo(this->name, version, force_exact_match);
00023 this->version = (info == NULL) ? -1 : info->GetVersion();
00024 this->is_random = is_random;
00025 if (this->config_list != NULL) delete this->config_list;
00026 this->config_list = (info == NULL) ? NULL : new ScriptConfigItemList();
00027 if (this->config_list != NULL) this->PushExtraConfigList();
00028
00029 this->ClearConfigList();
00030
00031 if (_game_mode == GM_NORMAL && this->info != NULL) {
00032
00033
00034 for (ScriptConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
00035 if ((*it).flags & SCRIPTCONFIG_RANDOM) {
00036 this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value - (*it).min_value) + (*it).min_value);
00037 }
00038 }
00039 this->AddRandomDeviation();
00040 }
00041 }
00042
00043 ScriptConfig::ScriptConfig(const ScriptConfig *config)
00044 {
00045 this->name = (config->name == NULL) ? NULL : strdup(config->name);
00046 this->info = config->info;
00047 this->version = config->version;
00048 this->config_list = NULL;
00049 this->is_random = config->is_random;
00050
00051 for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
00052 this->settings[strdup((*it).first)] = (*it).second;
00053 }
00054 this->AddRandomDeviation();
00055 }
00056
00057 ScriptConfig::~ScriptConfig()
00058 {
00059 free(this->name);
00060 this->ResetSettings();
00061 if (this->config_list != NULL) delete this->config_list;
00062 }
00063
00064 ScriptInfo *ScriptConfig::GetInfo() const
00065 {
00066 return this->info;
00067 }
00068
00069 const ScriptConfigItemList *ScriptConfig::GetConfigList()
00070 {
00071 if (this->info != NULL) return this->info->GetConfigList();
00072 if (this->config_list == NULL) {
00073 this->config_list = new ScriptConfigItemList();
00074 this->PushExtraConfigList();
00075 }
00076 return this->config_list;
00077 }
00078
00079 void ScriptConfig::ClearConfigList()
00080 {
00081 for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
00082 free((*it).first);
00083 }
00084 this->settings.clear();
00085 }
00086
00087 void ScriptConfig::AnchorUnchangeableSettings()
00088 {
00089 for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
00090 if (((*it).flags & SCRIPTCONFIG_INGAME) == 0) {
00091 this->SetSetting((*it).name, this->GetSetting((*it).name));
00092 }
00093 }
00094 }
00095
00096 int ScriptConfig::GetSetting(const char *name) const
00097 {
00098 SettingValueList::const_iterator it = this->settings.find(name);
00099 if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
00100 return (*it).second;
00101 }
00102
00103 void ScriptConfig::SetSetting(const char *name, int value)
00104 {
00105
00106 if (this->info == NULL) return;
00107
00108 const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
00109 if (config_item == NULL) return;
00110
00111 value = Clamp(value, config_item->min_value, config_item->max_value);
00112
00113 SettingValueList::iterator it = this->settings.find(name);
00114 if (it != this->settings.end()) {
00115 (*it).second = value;
00116 } else {
00117 this->settings[strdup(name)] = value;
00118 }
00119 }
00120
00121 void ScriptConfig::ResetSettings()
00122 {
00123 for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
00124 free((*it).first);
00125 }
00126 this->settings.clear();
00127 }
00128
00129 void ScriptConfig::AddRandomDeviation()
00130 {
00131 for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
00132 if ((*it).random_deviation != 0) {
00133 this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
00134 }
00135 }
00136 }
00137
00138 bool ScriptConfig::HasScript() const
00139 {
00140 return this->info != NULL;
00141 }
00142
00143 bool ScriptConfig::IsRandom() const
00144 {
00145 return this->is_random;
00146 }
00147
00148 const char *ScriptConfig::GetName() const
00149 {
00150 return this->name;
00151 }
00152
00153 int ScriptConfig::GetVersion() const
00154 {
00155 return this->version;
00156 }
00157
00158 void ScriptConfig::StringToSettings(const char *value)
00159 {
00160 char *value_copy = strdup(value);
00161 char *s = value_copy;
00162
00163 while (s != NULL) {
00164
00165 char *item_name = s;
00166 s = strchr(s, '=');
00167 if (s == NULL) break;
00168 if (*s == '\0') break;
00169 *s = '\0';
00170 s++;
00171
00172 char *item_value = s;
00173 s = strchr(s, ',');
00174 if (s != NULL) {
00175 *s = '\0';
00176 s++;
00177 }
00178
00179 this->SetSetting(item_name, atoi(item_value));
00180 }
00181 free(value_copy);
00182 }
00183
00184 void ScriptConfig::SettingsToString(char *string, size_t size) const
00185 {
00186 string[0] = '\0';
00187 for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
00188 char no[10];
00189 snprintf(no, sizeof(no), "%d", (*it).second);
00190
00191
00192 size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
00193
00194 if (size <= needed_size) break;
00195 size -= needed_size;
00196
00197 strcat(string, (*it).first);
00198 strcat(string, "=");
00199 strcat(string, no);
00200 strcat(string, ",");
00201 }
00202
00203 size_t len = strlen(string);
00204 if (len > 0) string[len - 1] = '\0';
00205 }
00206
00207 const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
00208 {
00209 if (slot == INVALID_COMPANY || this->GetInfo() == NULL) return NULL;
00210
00211 return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
00212 }