# script_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 "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     /* If we're in an existing game and the Script is changed, set all settings
    00033      *  for the Script that have the random flag to a random value. */
    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 int ScriptConfig::GetSetting(const char *name) const
    00088 {
    00089   /* Return default values if the difficulty is not set to Custom */
    00090   if (GetGameSettings().difficulty.diff_level != 3) {
    00091     return this->info->GetSettingDefaultValue(name);
    00092   }
    00093
    00094   SettingValueList::const_iterator it = this->settings.find(name);
    00095   if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
    00096   return (*it).second;
    00097 }
    00098
    00099 void ScriptConfig::SetSetting(const char *name, int value)
    00100 {
    00101   /* You can only set Script specific settings if an Script is selected. */
    00102   if (this->info == NULL) return;
    00103
    00104   const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
    00105   if (config_item == NULL) return;
    00106
    00107   value = Clamp(value, config_item->min_value, config_item->max_value);
    00108
    00109   SettingValueList::iterator it = this->settings.find(name);
    00110   if (it != this->settings.end()) {
    00111     (*it).second = value;
    00112   } else {
    00113     this->settings[strdup(name)] = value;
    00114   }
    00115 }
    00116
    00117 void ScriptConfig::ResetSettings()
    00118 {
    00119   for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00120     free((*it).first);
    00121   }
    00122   this->settings.clear();
    00123 }
    00124
    00125 void ScriptConfig::AddRandomDeviation()
    00126 {
    00127   for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
    00128     if ((*it).random_deviation != 0) {
    00129       this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
    00130     }
    00131   }
    00132 }
    00133
    00134 bool ScriptConfig::HasScript() const
    00135 {
    00136   return this->info != NULL;
    00137 }
    00138
    00139 bool ScriptConfig::IsRandom() const
    00140 {
    00141   return this->is_random;
    00142 }
    00143
    00144 const char *ScriptConfig::GetName() const
    00145 {
    00146   return this->name;
    00147 }
    00148
    00149 int ScriptConfig::GetVersion() const
    00150 {
    00151   return this->version;
    00152 }
    00153
    00154 void ScriptConfig::StringToSettings(const char *value)
    00155 {
    00156   char *value_copy = strdup(value);
    00157   char *s = value_copy;
    00158
    00159   while (s != NULL) {
    00160     /* Analyze the string ('name=value,name=value\0') */
    00161     char *item_name = s;
    00162     s = strchr(s, '=');
    00163     if (s == NULL) break;
    00164     if (*s == '\0') break;
    00165     *s = '\0';
    00166     s++;
    00167
    00168     char *item_value = s;
    00169     s = strchr(s, ',');
    00170     if (s != NULL) {
    00171       *s = '\0';
    00172       s++;
    00173     }
    00174
    00175     this->SetSetting(item_name, atoi(item_value));
    00176   }
    00177   free(value_copy);
    00178 }
    00179
    00180 void ScriptConfig::SettingsToString(char *string, size_t size) const
    00181 {
    00182   string[0] = '\0';
    00183   for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00184     char no[10];
    00185     snprintf(no, sizeof(no), "%d", (*it).second);
    00186
    00187     /* Check if the string would fit in the destination */
    00188     size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
    00189     /* If it doesn't fit, skip the next settings */
    00190     if (size <= needed_size) break;
    00191     size -= needed_size;
    00192
    00193     strcat(string, (*it).first);
    00194     strcat(string, "=");
    00195     strcat(string, no);
    00196     strcat(string, ",");
    00197   }
    00198   /* Remove the last ',', but only if at least one setting was saved. */
    00199   size_t len = strlen(string);
    00200   if (len > 0) string[len - 1] = '\0';
    00201 }
    00202
    00203 const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
    00204 {
    00205   if (slot == INVALID_COMPANY || this->GetInfo() == NULL) return NULL;
    00206
    00207   return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
    00208 }