OpenTTD Source  12.0-beta2
script_config.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../settings_type.h"
12 #include "../core/random_func.hpp"
13 #include "script_info.hpp"
14 #include "../textfile_gui.h"
15 #include "../string_func.h"
16 
17 #include "../safeguards.h"
18 
19 void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random)
20 {
21  free(this->name);
22  this->name = (name == nullptr) ? nullptr : stredup(name);
23  this->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match);
24  this->version = (info == nullptr) ? -1 : info->GetVersion();
25  this->is_random = is_random;
26  if (this->config_list != nullptr) delete this->config_list;
27  this->config_list = (info == nullptr) ? nullptr : new ScriptConfigItemList();
28  if (this->config_list != nullptr) this->PushExtraConfigList();
29 
30  this->ClearConfigList();
31 
32  if (_game_mode == GM_NORMAL && this->info != nullptr) {
33  /* If we're in an existing game and the Script is changed, set all settings
34  * for the Script that have the random flag to a random value. */
35  for (const auto &item : *this->info->GetConfigList()) {
36  if (item.flags & SCRIPTCONFIG_RANDOM) {
37  this->SetSetting(item.name, InteractiveRandomRange(item.max_value + 1 - item.min_value) + item.min_value);
38  }
39  }
40 
41  this->AddRandomDeviation();
42  }
43 }
44 
45 ScriptConfig::ScriptConfig(const ScriptConfig *config)
46 {
47  this->name = (config->name == nullptr) ? nullptr : stredup(config->name);
48  this->info = config->info;
49  this->version = config->version;
50  this->config_list = nullptr;
51  this->is_random = config->is_random;
52 
53  for (const auto &item : config->settings) {
54  this->settings[stredup(item.first)] = item.second;
55  }
56 
57  /* Virtual functions get called statically in constructors, so make it explicit to remove any confusion. */
59 }
60 
62 {
63  free(this->name);
64  this->ResetSettings();
65  if (this->config_list != nullptr) delete this->config_list;
66 }
67 
69 {
70  return this->info;
71 }
72 
74 {
75  if (this->info != nullptr) return this->info->GetConfigList();
76  if (this->config_list == nullptr) {
77  this->config_list = new ScriptConfigItemList();
78  this->PushExtraConfigList();
79  }
80  return this->config_list;
81 }
82 
84 {
85  for (const auto &item : this->settings) {
86  free(item.first);
87  }
88  this->settings.clear();
89 }
90 
92 {
93  for (const auto &item : *this->GetConfigList()) {
94  if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
95  this->SetSetting(item.name, this->GetSetting(item.name));
96  }
97  }
98 }
99 
100 int ScriptConfig::GetSetting(const char *name) const
101 {
102  const auto it = this->settings.find(name);
103  if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
104  return (*it).second;
105 }
106 
107 void ScriptConfig::SetSetting(const char *name, int value)
108 {
109  /* You can only set Script specific settings if an Script is selected. */
110  if (this->info == nullptr) return;
111 
112  const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
113  if (config_item == nullptr) return;
114 
115  value = Clamp(value, config_item->min_value, config_item->max_value);
116 
117  const auto it = this->settings.find(name);
118  if (it != this->settings.end()) {
119  (*it).second = value;
120  } else {
121  this->settings[stredup(name)] = value;
122  }
123 }
124 
126 {
127  for (const auto &item : this->settings) {
128  free(item.first);
129  }
130  this->settings.clear();
131 }
132 
133 void ScriptConfig::ResetEditableSettings(bool yet_to_start)
134 {
135  if (this->info == nullptr) return ResetSettings();
136 
137  for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
138  const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
139  assert(config_item != nullptr);
140 
141  bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
142  bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
143 
144  if (editable && visible) {
145  free(it->first);
146  it = this->settings.erase(it);
147  } else {
148  it++;
149  }
150  }
151 }
152 
154 {
155  for (const auto &item : *this->GetConfigList()) {
156  if (item.random_deviation != 0) {
157  this->SetSetting(item.name, InteractiveRandomRange(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name));
158  }
159  }
160 }
161 
163 {
164  return this->info != nullptr;
165 }
166 
168 {
169  return this->is_random;
170 }
171 
172 const char *ScriptConfig::GetName() const
173 {
174  return this->name;
175 }
176 
178 {
179  return this->version;
180 }
181 
182 void ScriptConfig::StringToSettings(const std::string &value)
183 {
184  char *value_copy = stredup(value.c_str());
185  char *s = value_copy;
186 
187  while (s != nullptr) {
188  /* Analyze the string ('name=value,name=value\0') */
189  char *item_name = s;
190  s = strchr(s, '=');
191  if (s == nullptr) break;
192  if (*s == '\0') break;
193  *s = '\0';
194  s++;
195 
196  char *item_value = s;
197  s = strchr(s, ',');
198  if (s != nullptr) {
199  *s = '\0';
200  s++;
201  }
202 
203  this->SetSetting(item_name, atoi(item_value));
204  }
205  free(value_copy);
206 }
207 
209 {
210  char string[1024];
211  char *last = lastof(string);
212  char *s = string;
213  *s = '\0';
214  for (const auto &item : this->settings) {
215  char no[10];
216  seprintf(no, lastof(no), "%d", item.second);
217 
218  /* Check if the string would fit in the destination */
219  size_t needed_size = strlen(item.first) + 1 + strlen(no);
220  /* If it doesn't fit, skip the next settings */
221  if (s + needed_size > last) break;
222 
223  s = strecat(s, item.first, last);
224  s = strecat(s, "=", last);
225  s = strecat(s, no, last);
226  s = strecat(s, ",", last);
227  }
228 
229  /* Remove the last ',', but only if at least one setting was saved. */
230  if (s != string) s[-1] = '\0';
231 
232  return string;
233 }
234 
235 const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
236 {
237  if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return nullptr;
238 
239  return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
240 }
ScriptConfig::FindInfo
virtual ScriptInfo * FindInfo(const char *name, int version, bool force_exact_match)=0
This function should call back to the Scanner in charge of this Config, to find the ScriptInfo belong...
ScriptConfig::~ScriptConfig
virtual ~ScriptConfig()
Delete an Script configuration.
Definition: script_config.cpp:61
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
ScriptConfig::ResetEditableSettings
void ResetEditableSettings(bool yet_to_start)
Reset only editable and visible settings to their default value.
Definition: script_config.cpp:133
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:35
GUISettings::ai_developer_tools
bool ai_developer_tools
activate AI developer tools
Definition: settings_type.h:187
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:36
ScriptConfigItemList
std::list< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition: script_config.hpp:48
ScriptInfo::GetConfigList
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
Definition: script_info.cpp:277
ScriptConfig::AddRandomDeviation
virtual void AddRandomDeviation()
Randomize all settings the Script requested to be randomized.
Definition: script_config.cpp:153
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition: fileio_type.h:119
ScriptConfig::GetInfo
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
Definition: script_config.cpp:68
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:172
ScriptConfig::SetSetting
virtual void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: script_config.cpp:107
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:167
SCRIPTCONFIG_INGAME
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
Definition: script_config.hpp:25
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:26
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition: fileio_type.h:121
ScriptConfig::version
int version
Version of the Script.
Definition: script_config.hpp:190
ScriptConfig::StringToSettings
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition: script_config.cpp:182
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:177
ScriptConfig::GetTextfile
const char * GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
Definition: script_config.cpp:235
ScriptConfig::config_list
ScriptConfigItemList * config_list
List with all settings defined by this Script.
Definition: script_config.hpp:193
ScriptConfig::ResetSettings
void ResetSettings()
Reset all settings to their default value.
Definition: script_config.cpp:125
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:191
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
ScriptConfig::AnchorUnchangeableSettings
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored.
Definition: script_config.cpp:91
GetTextfile
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:416
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:70
ScriptConfig::GetSetting
virtual int GetSetting(const char *name) const
Get the value of a setting for this config.
Definition: script_config.cpp:100
ScriptConfig::SettingsToString
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
Definition: script_config.cpp:208
script_info.hpp
ScriptConfig
Script settings.
Definition: script_config.hpp:55
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
SCRIPTCONFIG_RANDOM
@ SCRIPTCONFIG_RANDOM
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
Definition: script_config.hpp:23
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition: script_config.hpp:43
ScriptConfig::name
const char * name
Name of the Script.
Definition: script_config.hpp:189
ScriptConfig::ClearConfigList
virtual void ClearConfigList()
Routine that clears the config list.
Definition: script_config.cpp:83
ScriptInfo::GetConfigItem
const ScriptConfigItem * GetConfigItem(const char *name) const
Get the description of a certain Script config option.
Definition: script_info.cpp:282
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
ScriptConfig::Change
void Change(const char *name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
Definition: script_config.cpp:19
ScriptConfig::GetConfigList
const ScriptConfigItemList * GetConfigList()
Get the config list for this ScriptConfig.
Definition: script_config.cpp:73
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
ScriptInfo::GetSettingDefaultValue
int GetSettingDefaultValue(const char *name) const
Get the default value for a setting.
Definition: script_info.cpp:290
ScriptConfig::PushExtraConfigList
virtual void PushExtraConfigList()
In case you have mandatory non-Script-definable config entries in your list, add them to this functio...
Definition: script_config.hpp:200
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
ScriptConfigItem
Info about a single Script setting.
Definition: script_config.hpp:32
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
ScriptConfig::HasScript
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
Definition: script_config.cpp:162
ScriptConfig::is_random
bool is_random
True if the AI in this slot was randomly chosen.
Definition: script_config.hpp:194
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:593
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:192