OpenTTD Source  1.11.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  this->AddRandomDeviation();
41  }
42 }
43 
44 ScriptConfig::ScriptConfig(const ScriptConfig *config)
45 {
46  this->name = (config->name == nullptr) ? nullptr : stredup(config->name);
47  this->info = config->info;
48  this->version = config->version;
49  this->config_list = nullptr;
50  this->is_random = config->is_random;
51 
52  for (const auto &item : config->settings) {
53  this->settings[stredup(item.first)] = item.second;
54  }
55  this->AddRandomDeviation();
56 }
57 
59 {
60  free(this->name);
61  this->ResetSettings();
62  if (this->config_list != nullptr) delete this->config_list;
63 }
64 
66 {
67  return this->info;
68 }
69 
71 {
72  if (this->info != nullptr) return this->info->GetConfigList();
73  if (this->config_list == nullptr) {
74  this->config_list = new ScriptConfigItemList();
75  this->PushExtraConfigList();
76  }
77  return this->config_list;
78 }
79 
81 {
82  for (const auto &item : this->settings) {
83  free(item.first);
84  }
85  this->settings.clear();
86 }
87 
89 {
90  for (const auto &item : *this->GetConfigList()) {
91  if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
92  this->SetSetting(item.name, this->GetSetting(item.name));
93  }
94  }
95 }
96 
97 int ScriptConfig::GetSetting(const char *name) const
98 {
99  const auto it = this->settings.find(name);
100  if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
101  return (*it).second;
102 }
103 
104 void ScriptConfig::SetSetting(const char *name, int value)
105 {
106  /* You can only set Script specific settings if an Script is selected. */
107  if (this->info == nullptr) return;
108 
109  const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
110  if (config_item == nullptr) return;
111 
112  value = Clamp(value, config_item->min_value, config_item->max_value);
113 
114  const auto it = this->settings.find(name);
115  if (it != this->settings.end()) {
116  (*it).second = value;
117  } else {
118  this->settings[stredup(name)] = value;
119  }
120 }
121 
123 {
124  for (const auto &item : this->settings) {
125  free(item.first);
126  }
127  this->settings.clear();
128 }
129 
130 void ScriptConfig::ResetEditableSettings(bool yet_to_start)
131 {
132  if (this->info == nullptr) return ResetSettings();
133 
134  for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
135  const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
136  assert(config_item != nullptr);
137 
138  bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
139  bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
140 
141  if (editable && visible) {
142  free(it->first);
143  it = this->settings.erase(it);
144  } else {
145  it++;
146  }
147  }
148 }
149 
151 {
152  for (const auto &item : *this->GetConfigList()) {
153  if (item.random_deviation != 0) {
154  this->SetSetting(item.name, InteractiveRandomRange(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name));
155  }
156  }
157 }
158 
160 {
161  return this->info != nullptr;
162 }
163 
165 {
166  return this->is_random;
167 }
168 
169 const char *ScriptConfig::GetName() const
170 {
171  return this->name;
172 }
173 
175 {
176  return this->version;
177 }
178 
179 void ScriptConfig::StringToSettings(const char *value)
180 {
181  char *value_copy = stredup(value);
182  char *s = value_copy;
183 
184  while (s != nullptr) {
185  /* Analyze the string ('name=value,name=value\0') */
186  char *item_name = s;
187  s = strchr(s, '=');
188  if (s == nullptr) break;
189  if (*s == '\0') break;
190  *s = '\0';
191  s++;
192 
193  char *item_value = s;
194  s = strchr(s, ',');
195  if (s != nullptr) {
196  *s = '\0';
197  s++;
198  }
199 
200  this->SetSetting(item_name, atoi(item_value));
201  }
202  free(value_copy);
203 }
204 
205 void ScriptConfig::SettingsToString(char *string, const char *last) const
206 {
207  char *s = string;
208  *s = '\0';
209  for (const auto &item : this->settings) {
210  char no[10];
211  seprintf(no, lastof(no), "%d", item.second);
212 
213  /* Check if the string would fit in the destination */
214  size_t needed_size = strlen(item.first) + 1 + strlen(no);
215  /* If it doesn't fit, skip the next settings */
216  if (string + needed_size > last) break;
217 
218  s = strecat(s, item.first, last);
219  s = strecat(s, "=", last);
220  s = strecat(s, no, last);
221  s = strecat(s, ",", last);
222  }
223 
224  /* Remove the last ',', but only if at least one setting was saved. */
225  if (s != string) s[-1] = '\0';
226 }
227 
228 const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
229 {
230  if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return nullptr;
231 
232  return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
233 }
ScriptConfig::StringToSettings
void StringToSettings(const char *value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition: script_config.cpp:179
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::SettingsToString
void SettingsToString(char *string, const char *last) const
Convert the custom settings to a string that can be stored in the config file or savegames.
Definition: script_config.cpp:205
ScriptConfig::~ScriptConfig
virtual ~ScriptConfig()
Delete an Script configuration.
Definition: script_config.cpp:58
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
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:130
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:166
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:150
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:65
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:169
ScriptConfig::SetSetting
virtual void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: script_config.cpp:104
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:164
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::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:174
ScriptConfig::GetTextfile
const char * GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
Definition: script_config.cpp:228
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:122
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:88
GetTextfile
const char * GetTextfile(TextfileType type, Subdirectory dir, const char *filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:385
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:97
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:442
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:80
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:70
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:454
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:383
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:159
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:567
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:192