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

OpenTTD Source 15.0-RC2

script_config.hpp
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#ifndef SCRIPT_CONFIG_HPP
11#define SCRIPT_CONFIG_HPP
12
13#include "../company_type.h"
14#include "../textfile_gui.h"
15#include "script_instance.hpp"
16
18 static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION = 10 + 1 + 1;
19
21 enum class ScriptConfigFlag : uint8_t {
22 /* Unused flag 0x1. */
23 Boolean = 1,
24 InGame = 2,
25 Developer = 3,
26};
27
28using ScriptConfigFlags = EnumBitSet<ScriptConfigFlag, uint8_t>;
29
30 typedef std::map<int, std::string> LabelMapping;
31
33 struct ScriptConfigItem {
34 std::string name;
35 std::string description;
36 int min_value = 0;
37 int max_value = 1;
38 int default_value = 0;
39 int step_size = 1;
40 ScriptConfigFlags flags{};
41 LabelMapping labels;
42 bool complete_labels = false;
43
44 std::string GetString(int value) const;
45 TextColour GetColour() const;
46};
47
48 typedef std::vector<ScriptConfigItem> ScriptConfigItemList;
49
53 class ScriptConfig {
54protected:
56 typedef std::map<std::string, int> SettingValueList;
57
58public:
59 ScriptConfig() :
60 version(-1),
61 info(nullptr),
62 to_load_data(nullptr)
63 {}
64
69 ScriptConfig(const ScriptConfig &config);
70
72 virtual ~ScriptConfig();
73
81 void Change(std::optional<std::string_view> name, int version = -1, bool force_exact_match = false);
82
86 class ScriptInfo *GetInfo() const;
87
91 const ScriptConfigItemList *GetConfigList();
92
97 enum ScriptSettingSource : uint8_t {
98 SSS_DEFAULT,
99 SSS_FORCE_NEWGAME,
100 SSS_FORCE_GAME,
101 };
102
111 void AnchorUnchangeableSettings();
112
120 int GetSetting(const std::string &name) const;
121
125 void SetSetting(std::string_view name, int value);
126
130 void ResetSettings();
131
135 void ResetEditableSettings(bool yet_to_start);
136
141 bool HasScript() const;
142
146 const std::string &GetName() const;
147
151 int GetVersion() const;
152
157 void StringToSettings(std::string_view value);
158
163 std::string SettingsToString() const;
164
171 std::optional<std::string> GetTextfile(TextfileType type, CompanyID slot) const;
172
173 void SetToLoadData(ScriptInstance::ScriptData *data);
174 ScriptInstance::ScriptData *GetToLoadData();
175
176protected:
177 std::string name;
178 int version;
179 class ScriptInfo *info;
180 SettingValueList settings;
181 std::unique_ptr<ScriptConfigItemList> config_list;
182 std::unique_ptr<ScriptInstance::ScriptData> to_load_data;
183
187 void ClearConfigList();
188
193 virtual ScriptInfo *FindInfo(const std::string &name, int version, bool force_exact_match) = 0;
194};
195
196#endif /* SCRIPT_CONFIG_HPP */
EnumBitSet< ScriptConfigFlag, uint8_t >
ScriptConfig
Script settings.
Definition script_config.hpp:53
ScriptConfig::ScriptSettingSource
ScriptSettingSource
Where to get the config from, either default (depends on current game mode) or force either newgame o...
Definition script_config.hpp:97
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition script_config.hpp:100
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition script_config.hpp:99
ScriptConfig::SSS_DEFAULT
@ SSS_DEFAULT
Get the Script config from the current game mode.
Definition script_config.hpp:98
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::SettingValueList
std::map< std::string, int > SettingValueList
List with name=>value pairs of all script-specific settings.
Definition script_config.hpp:56
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
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
ScriptConfigItemList
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition script_config.hpp:48
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:18
ScriptConfigFlag
ScriptConfigFlag
Flags for Script settings.
Definition script_config.hpp:21
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.
LabelMapping
std::map< int, std::string > LabelMapping
Map-type used to map the setting numbers to labels.
Definition script_config.hpp:30
script_instance.hpp
The ScriptInstance tracks a script.
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::name
std::string name
The name of the configuration setting.
Definition script_config.hpp:34
ScriptConfigItem::default_value
int default_value
The default value of this configuration setting.
Definition script_config.hpp:38
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::step_size
int step_size
The step size in the gui.
Definition script_config.hpp:39
ScriptConfigItem::description
std::string description
The description of the configuration setting.
Definition script_config.hpp:35
ScriptConfigItem::complete_labels
bool complete_labels
True if all values have a label.
Definition script_config.hpp:42
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