# 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
    00017 void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random)
    00018 {
    00019   free(this->name);
    00020   this->name = (name == NULL) ? NULL : strdup(name);
    00021   this->info = (name == NULL) ? NULL : this->FindInfo(this->name, version, force_exact_match);
    00022   this->version = (info == NULL) ? -1 : info->GetVersion();
    00023   this->is_random = is_random;
    00024   if (this->config_list != NULL) delete this->config_list;
    00025   this->config_list = (info == NULL) ? NULL : new ScriptConfigItemList();
    00026   if (this->config_list != NULL) this->PushExtraConfigList();
    00027
    00028   this->ClearConfigList();
    00029
    00030   if (_game_mode == GM_NORMAL && this->info != NULL) {
    00031     /* If we're in an existing game and the Script is changed, set all settings
    00032      *  for the Script that have the random flag to a random value. */
    00033     for (ScriptConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
    00034       if ((*it).flags & SCRIPTCONFIG_RANDOM) {
    00035         this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value - (*it).min_value) + (*it).min_value);
    00036       }
    00037     }
    00038     this->AddRandomDeviation();
    00039   }
    00040 }
    00041
    00042 ScriptConfig::ScriptConfig(const ScriptConfig *config)
    00043 {
    00044   this->name = (config->name == NULL) ? NULL : strdup(config->name);
    00045   this->info = config->info;
    00046   this->version = config->version;
    00047   this->config_list = NULL;
    00048   this->is_random = config->is_random;
    00049
    00050   for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
    00051     this->settings[strdup((*it).first)] = (*it).second;
    00052   }
    00053   this->AddRandomDeviation();
    00054 }
    00055
    00056 ScriptConfig::~ScriptConfig()
    00057 {
    00058   free(this->name);
    00059   this->ResetSettings();
    00060   if (this->config_list != NULL) delete this->config_list;
    00061 }
    00062
    00063 ScriptInfo *ScriptConfig::GetInfo() const
    00064 {
    00065   return this->info;
    00066 }
    00067
    00068 const ScriptConfigItemList *ScriptConfig::GetConfigList()
    00069 {
    00070   if (this->info != NULL) return this->info->GetConfigList();
    00071   if (this->config_list == NULL) {
    00072     this->config_list = new ScriptConfigItemList();
    00073     this->PushExtraConfigList();
    00074   }
    00075   return this->config_list;
    00076 }
    00077
    00078 void ScriptConfig::ClearConfigList()
    00079 {
    00080   for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00081     free((*it).first);
    00082   }
    00083   this->settings.clear();
    00084 }
    00085
    00086 int ScriptConfig::GetSetting(const char *name) const
    00087 {
    00088   /* Return default values if the difficulty is not set to Custom */
    00089   if (GetGameSettings().difficulty.diff_level != 3) {
    00090     return this->info->GetSettingDefaultValue(name);
    00091   }
    00092
    00093   SettingValueList::const_iterator it = this->settings.find(name);
    00094   if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
    00095   return (*it).second;
    00096 }
    00097
    00098 void ScriptConfig::SetSetting(const char *name, int value)
    00099 {
    00100   /* You can only set Script specific settings if an Script is selected. */
    00101   if (this->info == NULL) return;
    00102
    00103   const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
    00104   if (config_item == NULL) return;
    00105
    00106   value = Clamp(value, config_item->min_value, config_item->max_value);
    00107
    00108   SettingValueList::iterator it = this->settings.find(name);
    00109   if (it != this->settings.end()) {
    00110     (*it).second = value;
    00111   } else {
    00112     this->settings[strdup(name)] = value;
    00113   }
    00114 }
    00115
    00116 void ScriptConfig::ResetSettings()
    00117 {
    00118   for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00119     free((*it).first);
    00120   }
    00121   this->settings.clear();
    00122 }
    00123
    00124 void ScriptConfig::AddRandomDeviation()
    00125 {
    00126   for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
    00127     if ((*it).random_deviation != 0) {
    00128       this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
    00129     }
    00130   }
    00131 }
    00132
    00133 bool ScriptConfig::HasScript() const
    00134 {
    00135   return this->info != NULL;
    00136 }
    00137
    00138 bool ScriptConfig::IsRandom() const
    00139 {
    00140   return this->is_random;
    00141 }
    00142
    00143 const char *ScriptConfig::GetName() const
    00144 {
    00145   return this->name;
    00146 }
    00147
    00148 int ScriptConfig::GetVersion() const
    00149 {
    00150   return this->version;
    00151 }
    00152
    00153 void ScriptConfig::StringToSettings(const char *value)
    00154 {
    00155   char *value_copy = strdup(value);
    00156   char *s = value_copy;
    00157
    00158   while (s != NULL) {
    00159     /* Analyze the string ('name=value,name=value\0') */
    00160     char *item_name = s;
    00161     s = strchr(s, '=');
    00162     if (s == NULL) break;
    00163     if (*s == '\0') break;
    00164     *s = '\0';
    00165     s++;
    00166
    00167     char *item_value = s;
    00168     s = strchr(s, ',');
    00169     if (s != NULL) {
    00170       *s = '\0';
    00171       s++;
    00172     }
    00173
    00174     this->SetSetting(item_name, atoi(item_value));
    00175   }
    00176   free(value_copy);
    00177 }
    00178
    00179 void ScriptConfig::SettingsToString(char *string, size_t size) const
    00180 {
    00181   string[0] = '\0';
    00182   for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
    00183     char no[10];
    00184     snprintf(no, sizeof(no), "%d", (*it).second);
    00185
    00186     /* Check if the string would fit in the destination */
    00187     size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
    00188     /* If it doesn't fit, skip the next settings */
    00189     if (size <= needed_size) break;
    00190     size -= needed_size;
    00191
    00192     strcat(string, (*it).first);
    00193     strcat(string, "=");
    00194     strcat(string, no);
    00195     strcat(string, ",");
    00196   }
    00197   /* Remove the last ',', but only if at least one setting was saved. */
    00198   size_t len = strlen(string);
    00199   if (len > 0) string[len - 1] = '\0';
    00200 }