OpenTTD Source  14.0-beta1
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 "api/script_object.hpp"
15 #include "../textfile_gui.h"
16 #include "../string_func.h"
17 #include <charconv>
18 
19 #include "../safeguards.h"
20 
21 void ScriptConfig::Change(std::optional<const std::string> name, int version, bool force_exact_match, bool is_random)
22 {
23  if (name.has_value()) {
24  this->name = std::move(name.value());
25  this->info = this->FindInfo(this->name, version, force_exact_match);
26  } else {
27  this->info = nullptr;
28  }
29  this->version = (info == nullptr) ? -1 : info->GetVersion();
30  this->is_random = is_random;
31  this->config_list.reset();
32  this->to_load_data.reset();
33 
34  this->ClearConfigList();
35 }
36 
37 ScriptConfig::ScriptConfig(const ScriptConfig *config)
38 {
39  this->name = config->name;
40  this->info = config->info;
41  this->version = config->version;
42  this->is_random = config->is_random;
43  this->to_load_data.reset();
44 
45  for (const auto &item : config->settings) {
46  this->settings[item.first] = item.second;
47  }
48 }
49 
51 {
52  this->ResetSettings();
53  this->to_load_data.reset();
54 }
55 
57 {
58  return this->info;
59 }
60 
62 {
63  if (this->info != nullptr) return this->info->GetConfigList();
64  if (this->config_list == nullptr) {
65  this->config_list = std::make_unique<ScriptConfigItemList>();
66  }
67  return this->config_list.get();
68 }
69 
71 {
72  this->settings.clear();
73 }
74 
76 {
77  for (const auto &item : *this->GetConfigList()) {
78  if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
79  this->SetSetting(item.name, this->GetSetting(item.name));
80  }
81  }
82 }
83 
84 int ScriptConfig::GetSetting(const std::string &name) const
85 {
86  const auto it = this->settings.find(name);
87  if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
88  return (*it).second;
89 }
90 
91 void ScriptConfig::SetSetting(const std::string_view name, int value)
92 {
93  /* You can only set Script specific settings if an Script is selected. */
94  if (this->info == nullptr) return;
95 
96  const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
97  if (config_item == nullptr) return;
98 
99  value = Clamp(value, config_item->min_value, config_item->max_value);
100 
101  this->settings[std::string{name}] = value;
102 }
103 
105 {
106  this->settings.clear();
107 }
108 
109 void ScriptConfig::ResetEditableSettings(bool yet_to_start)
110 {
111  if (this->info == nullptr) return ResetSettings();
112 
113  for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
114  const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
115  assert(config_item != nullptr);
116 
117  bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
118  bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
119 
120  if (editable && visible) {
121  it = this->settings.erase(it);
122  } else {
123  it++;
124  }
125  }
126 }
127 
129 {
130  for (const auto &item : *this->GetConfigList()) {
131  if (item.random_deviation != 0) {
132  this->SetSetting(item.name, ScriptObject::GetRandomizer(OWNER_NONE).Next(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name));
133  }
134  }
135 }
136 
138 {
139  return this->info != nullptr;
140 }
141 
143 {
144  return this->is_random;
145 }
146 
147 const std::string &ScriptConfig::GetName() const
148 {
149  return this->name;
150 }
151 
153 {
154  return this->version;
155 }
156 
157 void ScriptConfig::StringToSettings(const std::string &value)
158 {
159  std::string_view to_process = value;
160  for (;;) {
161  /* Analyze the string ('name=value,name=value\0') */
162  size_t pos = to_process.find_first_of('=');
163  if (pos == std::string_view::npos) return;
164 
165  std::string_view item_name = to_process.substr(0, pos);
166 
167  to_process.remove_prefix(pos + 1);
168  pos = to_process.find_first_of(',');
169  int item_value = 0;
170  std::from_chars(to_process.data(), to_process.data() + std::min(pos, to_process.size()), item_value);
171 
172  this->SetSetting(item_name, item_value);
173 
174  if (pos == std::string_view::npos) return;
175  to_process.remove_prefix(pos + 1);
176  }
177 }
178 
180 {
181  if (this->settings.empty()) return {};
182 
183  std::string result;
184  for (const auto &item : this->settings) {
185  fmt::format_to(std::back_inserter(result), "{}={},", item.first, item.second);
186  }
187 
188  /* Remove the last ','. */
189  result.resize(result.size() - 1);
190  return result;
191 }
192 
193 std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
194 {
195  if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return std::nullopt;
196 
197  return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
198 }
199 
200 void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
201 {
202  this->to_load_data.reset(data);
203 }
204 
205 ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
206 {
207  return this->to_load_data.get();
208 }
209 
ScriptConfig::config_list
std::unique_ptr< ScriptConfigItemList > config_list
List with all settings defined by this Script.
Definition: script_config.hpp:190
ScriptConfigItemList
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition: script_config.hpp:45
ScriptConfig::~ScriptConfig
virtual ~ScriptConfig()
Delete an Script configuration.
Definition: script_config.cpp:50
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:54
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:109
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:35
ScriptInfo::GetSettingDefaultValue
int GetSettingDefaultValue(const std::string &name) const
Get the default value for a setting.
Definition: script_info.cpp:279
GUISettings::ai_developer_tools
bool ai_developer_tools
activate AI/GS developer tools
Definition: settings_type.h:217
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:36
ScriptInfo::GetConfigList
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
Definition: script_info.cpp:266
ScriptConfig::AddRandomDeviation
void AddRandomDeviation()
Randomize all settings the Script requested to be randomized.
Definition: script_config.cpp:128
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:56
GetTextfile
std::optional< std::string > GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename)
Search a textfile file next to the given content.
Definition: textfile_gui.cpp:862
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:142
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:187
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:157
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:152
ScriptConfig::Change
void Change(std::optional< const std::string > 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:21
ScriptConfig::ResetSettings
void ResetSettings()
Reset all settings to their default value.
Definition: script_config.cpp:104
ScriptConfig::to_load_data
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
Definition: script_config.hpp:192
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:188
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:75
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:61
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:179
script_info.hpp
ScriptConfig
Script settings.
Definition: script_config.hpp:50
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition: script_config.cpp:84
ScriptConfig::name
std::string name
Name of the Script.
Definition: script_config.hpp:186
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
ScriptConfig::GetTextfile
std::optional< std::string > GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
Definition: script_config.cpp:193
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition: script_config.hpp:40
ScriptConfig::ClearConfigList
void ClearConfigList()
Routine that clears the config list.
Definition: script_config.cpp:70
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
ScriptConfig::GetConfigList
const ScriptConfigItemList * GetConfigList()
Get the config list for this ScriptConfig.
Definition: script_config.cpp:61
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
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
ScriptConfig::SetSetting
void SetSetting(const std::string_view name, int value)
Set the value of a setting for this config.
Definition: script_config.cpp:91
ScriptConfig::FindInfo
virtual ScriptInfo * FindInfo(const std::string &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::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:137
ScriptConfig::is_random
bool is_random
True if the AI in this slot was randomly chosen.
Definition: script_config.hpp:191
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:636
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:189
ScriptInfo::GetConfigItem
const ScriptConfigItem * GetConfigItem(const std::string_view name) const
Get the description of a certain Script config option.
Definition: script_info.cpp:271
ScriptConfig::GetName
const std::string & GetName() const
Get the name of the Script.
Definition: script_config.cpp:147