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