OpenTTD Source: src/script/script_config.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../settings_type.h"
12#include "script_info.hpp"
13#include "api/script_object.hpp"
14#include "../textfile_gui.h"
15#include "../string_func.h"
16#include <charconv>
17
18#include "table/strings.h"
19
20#include "../safeguards.h"
21
22 void ScriptConfig::Change(std::optional<std::string_view> name, int version, bool force_exact_match)
23{
24 if (name.has_value()) {
25 this->name = name.value();
26 this->info = this->FindInfo(this->name, version, force_exact_match);
27 } else {
28 this->info = nullptr;
29 }
30 this->version = (info == nullptr) ? -1 : info->GetVersion();
31 this->config_list.reset();
32 this->to_load_data.reset();
33
34 this->ClearConfigList();
35}
36
37ScriptConfig::ScriptConfig(const ScriptConfig &config)
38{
39 this->name = config.name;
40 this->info = config.info;
41 this->version = config.version;
42 this->to_load_data.reset();
43
44 for (const auto &item : config.settings) {
45 this->settings[item.first] = item.second;
46 }
47}
48
49ScriptConfig::~ScriptConfig()
50{
51 this->ResetSettings();
52 this->to_load_data.reset();
53}
54
55 ScriptInfo *ScriptConfig::GetInfo() const
56{
57 return this->info;
58}
59
60 const ScriptConfigItemList *ScriptConfig::GetConfigList()
61{
62 if (this->info != nullptr) return this->info->GetConfigList();
63 if (this->config_list == nullptr) {
64 this->config_list = std::make_unique<ScriptConfigItemList>();
65 }
66 return this->config_list.get();
67}
68
69 void ScriptConfig::ClearConfigList()
70{
71 this->settings.clear();
72}
73
74 void ScriptConfig::AnchorUnchangeableSettings()
75{
76 for (const auto &item : *this->GetConfigList()) {
77 if (!item.flags.Test(ScriptConfigFlag::InGame)) {
78 this->SetSetting(item.name, this->GetSetting(item.name));
79 }
80 }
81}
82
83 int ScriptConfig::GetSetting(const std::string &name) const
84{
85 const auto it = this->settings.find(name);
86 if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
87 return it->second;
88}
89
90 void ScriptConfig::SetSetting(std::string_view name, int value)
91{
92 /* You can only set Script specific settings if an Script is selected. */
93 if (this->info == nullptr) return;
94
95 const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
96 if (config_item == nullptr) return;
97
98 value = Clamp(value, config_item->min_value, config_item->max_value);
99
100 this->settings[std::string{name}] = value;
101}
102
103 void ScriptConfig::ResetSettings()
104{
105 this->settings.clear();
106}
107
108 void ScriptConfig::ResetEditableSettings(bool yet_to_start)
109{
110 if (this->info == nullptr) return ResetSettings();
111
112 for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
113 const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
114 assert(config_item != nullptr);
115
116 bool editable = yet_to_start || config_item->flags.Test(ScriptConfigFlag::InGame);
117 bool visible = _settings_client.gui.ai_developer_tools || !config_item->flags.Test(ScriptConfigFlag::Developer);
118
119 if (editable && visible) {
120 it = this->settings.erase(it);
121 } else {
122 it++;
123 }
124 }
125}
126
127 bool ScriptConfig::HasScript() const
128{
129 return this->info != nullptr;
130}
131
132 const std::string &ScriptConfig::GetName() const
133{
134 return this->name;
135}
136
137 int ScriptConfig::GetVersion() const
138{
139 return this->version;
140}
141
142 void ScriptConfig::StringToSettings(std::string_view value)
143{
144 std::string_view to_process = value;
145 for (;;) {
146 /* Analyze the string ('name=value,name=value\0') */
147 size_t pos = to_process.find_first_of('=');
148 if (pos == std::string_view::npos) return;
149
150 std::string_view item_name = to_process.substr(0, pos);
151
152 to_process.remove_prefix(pos + 1);
153 pos = to_process.find_first_of(',');
154 int item_value = 0;
155 std::from_chars(to_process.data(), to_process.data() + std::min(pos, to_process.size()), item_value);
156
157 this->SetSetting(item_name, item_value);
158
159 if (pos == std::string_view::npos) return;
160 to_process.remove_prefix(pos + 1);
161 }
162}
163
164std::string ScriptConfig::SettingsToString() const
165{
166 if (this->settings.empty()) return {};
167
168 std::string result;
169 for (const auto &item : this->settings) {
170 format_append(result, "{}={},", item.first, item.second);
171 }
172
173 /* Remove the last ','. */
174 result.resize(result.size() - 1);
175 return result;
176}
177
178std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
179{
180 if (slot == CompanyID::Invalid() || this->GetInfo() == nullptr) return std::nullopt;
181
182 return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
183}
184
185void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
186{
187 this->to_load_data.reset(data);
188}
189
190ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
191{
192 return this->to_load_data.get();
193}
194
195static std::pair<StringParameter, StringParameter> GetValueParams(const ScriptConfigItem &config_item, int value)
196{
197 if (config_item.flags.Test(ScriptConfigFlag::Boolean)) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}};
198
199 auto it = config_item.labels.find(value);
200 if (it != std::end(config_item.labels)) return {STR_JUST_RAW_STRING, it->second};
201
202 return {STR_JUST_INT, value};
203}
204
210std::string ScriptConfigItem::GetString(int value) const
211{
212 auto [param1, param2] = GetValueParams(*this, value);
213 return this->description.empty()
214 ? ::GetString(STR_JUST_STRING1, param1, param2)
215 : ::GetString(STR_AI_SETTINGS_SETTING, this->description, param1, param2);
216}
217
222 TextColour ScriptConfigItem::GetColour() const
223{
224 return this->description.empty() ? TC_ORANGE : TC_LIGHT_BLUE;
225}
226
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
ScriptConfig
Script settings.
Definition script_config.hpp:53
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition script_config.hpp:180
ScriptConfig::GetTextfile
std::optional< std::string > GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
Definition script_config.cpp:178
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:74
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:127
ScriptConfig::SetSetting
void SetSetting(std::string_view name, int value)
Set the value of a setting for this config.
Definition script_config.cpp:90
ScriptConfig::Change
void Change(std::optional< std::string_view > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
Definition script_config.cpp:22
ScriptConfig::StringToSettings
void StringToSettings(std::string_view value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition script_config.cpp:142
ScriptConfig::~ScriptConfig
virtual ~ScriptConfig()
Delete an Script configuration.
Definition script_config.cpp:49
ScriptConfig::version
int version
Version of the Script.
Definition script_config.hpp:178
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::GetName
const std::string & GetName() const
Get the name of the Script.
Definition script_config.cpp:132
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition script_config.hpp:179
ScriptConfig::config_list
std::unique_ptr< ScriptConfigItemList > config_list
List with all settings defined by this Script.
Definition script_config.hpp:181
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition script_config.cpp:137
ScriptConfig::ClearConfigList
void ClearConfigList()
Routine that clears the config list.
Definition script_config.cpp:69
ScriptConfig::to_load_data
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
Definition script_config.hpp:182
ScriptConfig::GetInfo
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
Definition script_config.cpp:55
ScriptConfig::ResetSettings
void ResetSettings()
Reset all settings to their default value.
Definition script_config.cpp:103
ScriptConfig::name
std::string name
Name of the Script.
Definition script_config.hpp:177
ScriptConfig::GetConfigList
const ScriptConfigItemList * GetConfigList()
Get the config list for this ScriptConfig.
Definition script_config.cpp:60
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition script_config.cpp:83
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:164
ScriptConfig::ResetEditableSettings
void ResetEditableSettings(bool yet_to_start)
Reset only editable and visible settings to their default value.
Definition script_config.cpp:108
ScriptInfo
All static information from an Script like name, version, etc.
Definition script_info.hpp:30
ScriptInfo::GetConfigList
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
Definition script_info.cpp:253
ScriptInfo::GetMainScript
const std::string & GetMainScript() const
Get the filename of the main.nut script.
Definition script_info.hpp:75
ScriptInfo::GetConfigItem
const ScriptConfigItem * GetConfigItem(std::string_view name) const
Get the description of a certain Script config option.
Definition script_info.cpp:258
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition script_info.hpp:55
ScriptInfo::GetSettingDefaultValue
int GetSettingDefaultValue(const std::string &name) const
Get the default value for a setting.
Definition script_info.cpp:266
OWNER_DEITY
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
Definition company_type.h:28
AI_DIR
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:99
GAME_DIR
@ GAME_DIR
Subdirectory for all game scripts.
Definition fileio_type.h:101
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
ScriptConfigItemList
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition script_config.hpp:48
ScriptConfigFlag::Boolean
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
ScriptConfigFlag::Developer
@ Developer
This setting will only be visible when the Script development tools are active.
ScriptConfigFlag::InGame
@ InGame
This setting can be changed while the Script is running.
script_info.hpp
ScriptInfo keeps track of all information of a script, like Author, Description, ....
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition settings_type.h:650
GUISettings::ai_developer_tools
bool ai_developer_tools
activate AI/GS developer tools
Definition settings_type.h:252
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >
ScriptConfigItem
Info about a single Script setting.
Definition script_config.hpp:33
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition script_config.hpp:40
ScriptConfigItem::GetString
std::string GetString(int value) const
Get string to display this setting in the configuration interface.
Definition script_config.cpp:210
ScriptConfigItem::labels
LabelMapping labels
Text labels for the integer values.
Definition script_config.hpp:41
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition script_config.hpp:36
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition script_config.hpp:37
ScriptConfigItem::description
std::string description
The description of the configuration setting.
Definition script_config.hpp:35
ScriptConfigItem::GetColour
TextColour GetColour() const
Get text colour to display this setting in the configuration interface.
Definition script_config.cpp:222
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition textfile_type.h:14