OpenTTD Source 20250529-master-g10c159a79f
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 "table/strings.h"
20
21#include "../safeguards.h"
22
23void ScriptConfig::Change(std::optional<std::string_view> name, int version, bool force_exact_match)
24{
25 if (name.has_value()) {
26 this->name = name.value();
27 this->info = this->FindInfo(this->name, version, force_exact_match);
28 } else {
29 this->info = nullptr;
30 }
31 this->version = (info == nullptr) ? -1 : info->GetVersion();
32 this->config_list.reset();
33 this->to_load_data.reset();
34
35 this->ClearConfigList();
36}
37
38ScriptConfig::ScriptConfig(const ScriptConfig &config)
39{
40 this->name = config.name;
41 this->info = config.info;
42 this->version = config.version;
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.Test(ScriptConfigFlag::InGame)) {
79 this->SetSetting(item.name, this->GetSetting(item.name));
80 }
81 }
82}
83
84int 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
91void ScriptConfig::SetSetting(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
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.Test(ScriptConfigFlag::InGame);
119
120 if (editable && visible) {
121 it = this->settings.erase(it);
122 } else {
123 it++;
124 }
125 }
126}
127
129{
130 return this->info != nullptr;
131}
132
133const std::string &ScriptConfig::GetName() const
134{
135 return this->name;
136}
137
139{
140 return this->version;
141}
142
143void ScriptConfig::StringToSettings(std::string_view value)
144{
145 std::string_view to_process = value;
146 for (;;) {
147 /* Analyze the string ('name=value,name=value\0') */
148 size_t pos = to_process.find_first_of('=');
149 if (pos == std::string_view::npos) return;
150
151 std::string_view item_name = to_process.substr(0, pos);
152
153 to_process.remove_prefix(pos + 1);
154 pos = to_process.find_first_of(',');
155 int item_value = 0;
156 std::from_chars(to_process.data(), to_process.data() + std::min(pos, to_process.size()), item_value);
157
158 this->SetSetting(item_name, item_value);
159
160 if (pos == std::string_view::npos) return;
161 to_process.remove_prefix(pos + 1);
162 }
163}
164
166{
167 if (this->settings.empty()) return {};
168
169 std::string result;
170 for (const auto &item : this->settings) {
171 format_append(result, "{}={},", item.first, item.second);
172 }
173
174 /* Remove the last ','. */
175 result.resize(result.size() - 1);
176 return result;
177}
178
179std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
180{
181 if (slot == CompanyID::Invalid() || this->GetInfo() == nullptr) return std::nullopt;
182
183 return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
184}
185
186void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
187{
188 this->to_load_data.reset(data);
189}
190
191ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
192{
193 return this->to_load_data.get();
194}
195
196static std::pair<StringParameter, StringParameter> GetValueParams(const ScriptConfigItem &config_item, int value)
197{
198 if (config_item.flags.Test(ScriptConfigFlag::Boolean)) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}};
199
200 auto it = config_item.labels.find(value);
201 if (it != std::end(config_item.labels)) return {STR_JUST_RAW_STRING, it->second};
202
203 return {STR_JUST_INT, value};
204}
205
211std::string ScriptConfigItem::GetString(int value) const
212{
213 auto [param1, param2] = GetValueParams(*this, value);
214 return this->description.empty()
215 ? ::GetString(STR_JUST_STRING1, param1, param2)
216 : ::GetString(STR_AI_SETTINGS_SETTING, this->description, param1, param2);
217}
218
224{
225 return this->description.empty() ? TC_ORANGE : TC_LIGHT_BLUE;
226}
227
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Script settings.
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
std::optional< std::string > GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored.
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
void SetSetting(std::string_view name, int value)
Set the value of a setting for this config.
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.
void StringToSettings(std::string_view value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
virtual ~ScriptConfig()
Delete an Script configuration.
int version
Version of the Script.
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...
const std::string & GetName() const
Get the name of the Script.
class ScriptInfo * info
ScriptInfo object for related to this Script version.
std::unique_ptr< ScriptConfigItemList > config_list
List with all settings defined by this Script.
int GetVersion() const
Get the version of the Script.
void ClearConfigList()
Routine that clears the config list.
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
void ResetSettings()
Reset all settings to their default value.
std::string name
Name of the Script.
const ScriptConfigItemList * GetConfigList()
Get the config list for this ScriptConfig.
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
void ResetEditableSettings(bool yet_to_start)
Reset only editable and visible settings to their default value.
All static information from an Script like name, version, etc.
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
const std::string & GetMainScript() const
Get the filename of the main.nut script.
const ScriptConfigItem * GetConfigItem(std::string_view name) const
Get the description of a certain Script config option.
int GetVersion() const
Get the version of the script.
int GetSettingDefaultValue(const std::string &name) const
Get the default value for a setting.
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
@ AI_DIR
Subdirectory for all AI files.
Definition fileio_type.h:98
@ GAME_DIR
Subdirectory for all game scripts.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:302
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
@ Developer
This setting will only be visible when the Script development tools are active.
@ InGame
This setting can be changed while the Script is running.
ScriptInfo keeps track of all information of a script, like Author, Description, ....
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
GUISettings gui
settings related to the GUI
bool ai_developer_tools
activate AI/GS developer tools
Info about a single Script setting.
ScriptConfigFlags flags
Flags for the configuration setting.
std::string GetString(int value) const
Get string to display this setting in the configuration interface.
LabelMapping labels
Text labels for the integer values.
int min_value
The minimal value this configuration setting can have.
int max_value
The maximal value this configuration setting can have.
std::string description
The description of the configuration setting.
TextColour GetColour() const
Get text colour to display this setting in the configuration interface.
TextfileType
Additional text files accompanying Tar archives.