OpenTTD Source  13.2.1
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  this->to_load_data.reset();
30 
31  this->ClearConfigList();
32 
33  if (_game_mode == GM_NORMAL && this->info != nullptr) {
34  /* If we're in an existing game and the Script is changed, set all settings
35  * for the Script that have the random flag to a random value. */
36  for (const auto &item : *this->info->GetConfigList()) {
37  if (item.flags & SCRIPTCONFIG_RANDOM) {
38  this->SetSetting(item.name, InteractiveRandomRange(item.max_value + 1 - item.min_value) + item.min_value);
39  }
40  }
41 
42  this->AddRandomDeviation();
43  }
44 }
45 
46 ScriptConfig::ScriptConfig(const ScriptConfig *config)
47 {
48  this->name = (config->name == nullptr) ? nullptr : stredup(config->name);
49  this->info = config->info;
50  this->version = config->version;
51  this->config_list = nullptr;
52  this->is_random = config->is_random;
53  this->to_load_data.reset();
54 
55  for (const auto &item : config->settings) {
56  this->settings[stredup(item.first)] = item.second;
57  }
58 
59  /* Virtual functions get called statically in constructors, so make it explicit to remove any confusion. */
61 }
62 
64 {
65  free(this->name);
66  this->ResetSettings();
67  if (this->config_list != nullptr) delete this->config_list;
68  this->to_load_data.reset();
69 }
70 
72 {
73  return this->info;
74 }
75 
77 {
78  if (this->info != nullptr) return this->info->GetConfigList();
79  if (this->config_list == nullptr) {
80  this->config_list = new ScriptConfigItemList();
81  this->PushExtraConfigList();
82  }
83  return this->config_list;
84 }
85 
87 {
88  for (const auto &item : this->settings) {
89  free(item.first);
90  }
91  this->settings.clear();
92 }
93 
95 {
96  for (const auto &item : *this->GetConfigList()) {
97  if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
98  this->SetSetting(item.name, this->GetSetting(item.name));
99  }
100  }
101 }
102 
103 int ScriptConfig::GetSetting(const char *name) const
104 {
105  const auto it = this->settings.find(name);
106  if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
107  return (*it).second;
108 }
109 
110 void ScriptConfig::SetSetting(const char *name, int value)
111 {
112  /* You can only set Script specific settings if an Script is selected. */
113  if (this->info == nullptr) return;
114 
115  const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
116  if (config_item == nullptr) return;
117 
118  value = Clamp(value, config_item->min_value, config_item->max_value);
119 
120  const auto it = this->settings.find(name);
121  if (it != this->settings.end()) {
122  (*it).second = value;
123  } else {
124  this->settings[stredup(name)] = value;
125  }
126 }
127 
129 {
130  for (const auto &item : this->settings) {
131  free(item.first);
132  }
133  this->settings.clear();
134 }
135 
136 void ScriptConfig::ResetEditableSettings(bool yet_to_start)
137 {
138  if (this->info == nullptr) return ResetSettings();
139 
140  for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
141  const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
142  assert(config_item != nullptr);
143 
144  bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
145  bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
146 
147  if (editable && visible) {
148  free(it->first);
149  it = this->settings.erase(it);
150  } else {
151  it++;
152  }
153  }
154 }
155 
157 {
158  for (const auto &item : *this->GetConfigList()) {
159  if (item.random_deviation != 0) {
160  this->SetSetting(item.name, InteractiveRandomRange(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name));
161  }
162  }
163 }
164 
166 {
167  return this->info != nullptr;
168 }
169 
171 {
172  return this->is_random;
173 }
174 
175 const char *ScriptConfig::GetName() const
176 {
177  return this->name;
178 }
179 
181 {
182  return this->version;
183 }
184 
185 void ScriptConfig::StringToSettings(const std::string &value)
186 {
187  char *value_copy = stredup(value.c_str());
188  char *s = value_copy;
189 
190  while (s != nullptr) {
191  /* Analyze the string ('name=value,name=value\0') */
192  char *item_name = s;
193  s = strchr(s, '=');
194  if (s == nullptr) break;
195  if (*s == '\0') break;
196  *s = '\0';
197  s++;
198 
199  char *item_value = s;
200  s = strchr(s, ',');
201  if (s != nullptr) {
202  *s = '\0';
203  s++;
204  }
205 
206  this->SetSetting(item_name, atoi(item_value));
207  }
208  free(value_copy);
209 }
210 
212 {
213  char string[1024];
214  char *last = lastof(string);
215  char *s = string;
216  *s = '\0';
217  for (const auto &item : this->settings) {
219  seprintf(no, lastof(no), "%d", item.second);
220 
221  /* Check if the string would fit in the destination */
222  size_t needed_size = strlen(item.first) + 1 + strlen(no);
223  /* If it doesn't fit, skip the next settings */
224  if (s + needed_size > last) break;
225 
226  s = strecat(s, item.first, last);
227  s = strecat(s, "=", last);
228  s = strecat(s, no, last);
229  s = strecat(s, ",", last);
230  }
231 
232  /* Remove the last ',', but only if at least one setting was saved. */
233  if (s != string) s[-1] = '\0';
234 
235  return string;
236 }
237 
238 const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
239 {
240  if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return nullptr;
241 
242  return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
243 }
244 
245 void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
246 {
247  this->to_load_data.reset(data);
248 }
249 
250 ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
251 {
252  return this->to_load_data.get();
253 }
254 
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...
INT32_DIGITS_WITH_SIGN_AND_TERMINATION
static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION
Maximum of 10 digits for MIN / MAX_INT32, 1 for the sign and 1 for '\0'.
Definition: script_config.hpp:22
ScriptConfig::~ScriptConfig
virtual ~ScriptConfig()
Delete an Script configuration.
Definition: script_config.cpp:63
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:53
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:136
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:39
GUISettings::ai_developer_tools
bool ai_developer_tools
activate AI/GS developer tools
Definition: settings_type.h:193
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:40
ScriptConfigItemList
std::list< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition: script_config.hpp:52
ScriptInfo::GetConfigList
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
Definition: script_info.cpp:284
ScriptConfig::AddRandomDeviation
virtual void AddRandomDeviation()
Randomize all settings the Script requested to be randomized.
Definition: script_config.cpp:156
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:71
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:175
ScriptConfig::SetSetting
virtual void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: script_config.cpp:110
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:170
SCRIPTCONFIG_INGAME
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
Definition: script_config.hpp:29
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:30
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:198
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:185
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:180
ScriptConfig::GetTextfile
const char * GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
Definition: script_config.cpp:238
ScriptConfig::config_list
ScriptConfigItemList * config_list
List with all settings defined by this Script.
Definition: script_config.hpp:201
ScriptConfig::ResetSettings
void ResetSettings()
Reset all settings to their default value.
Definition: script_config.cpp:128
ScriptConfig::to_load_data
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
Definition: script_config.hpp:203
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:199
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:94
GetTextfile
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:414
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:103
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:211
script_info.hpp
ScriptConfig
Script settings.
Definition: script_config.hpp:59
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
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:138
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:27
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition: script_config.hpp:47
ScriptConfig::name
const char * name
Name of the Script.
Definition: script_config.hpp:197
ScriptConfig::ClearConfigList
virtual void ClearConfigList()
Routine that clears the config list.
Definition: script_config.cpp:86
ScriptInfo::GetConfigItem
const ScriptConfigItem * GetConfigItem(const char *name) const
Get the description of a certain Script config option.
Definition: script_info.cpp:289
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:76
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:297
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:209
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:85
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:36
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:402
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:165
ScriptConfig::is_random
bool is_random
True if the AI in this slot was randomly chosen.
Definition: script_config.hpp:202
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:604
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:200