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

OpenTTD Source 14.0-beta1

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 <http://www.gnu.org/licenses/>.
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 ScriptConfigFlags {
22 SCRIPTCONFIG_NONE = 0x0,
23 // Unused flag 0x1.
24 SCRIPTCONFIG_BOOLEAN = 0x2,
25 SCRIPTCONFIG_INGAME = 0x4,
26 SCRIPTCONFIG_DEVELOPER = 0x8,
27 };
28
29 typedef std::map<int, std::string> LabelMapping;
30
32 struct ScriptConfigItem {
33 std::string name;
34 std::string description;
35 int min_value = 0;
36 int max_value = 1;
37 int default_value = 0;
38 int random_deviation = 0;
39 int step_size = 1;
40 ScriptConfigFlags flags = SCRIPTCONFIG_NONE;
41 LabelMapping labels;
42 bool complete_labels = false;
43 };
44
45 typedef std::vector<ScriptConfigItem> ScriptConfigItemList;
46
50 class ScriptConfig {
51 protected:
53 typedef std::map<std::string, int> SettingValueList;
54
55 public:
56 ScriptConfig() :
57 version(-1),
58 info(nullptr),
59 is_random(false),
60 to_load_data(nullptr)
61 {}
62
67 ScriptConfig(const ScriptConfig *config);
68
70 virtual ~ScriptConfig();
71
80 void Change(std::optional<const std::string> name, int version = -1, bool force_exact_match = false, bool is_random = false);
81
85 class ScriptInfo *GetInfo() const;
86
90 const ScriptConfigItemList *GetConfigList();
91
96 enum ScriptSettingSource {
97 SSS_DEFAULT,
98 SSS_FORCE_NEWGAME,
99 SSS_FORCE_GAME,
100 };
101
110 void AnchorUnchangeableSettings();
111
119 int GetSetting(const std::string &name) const;
120
124 void SetSetting(const std::string_view name, int value);
125
129 void ResetSettings();
130
134 void ResetEditableSettings(bool yet_to_start);
135
139 void AddRandomDeviation();
140
145 bool HasScript() const;
146
150 bool IsRandom() const;
151
155 const std::string &GetName() const;
156
160 int GetVersion() const;
161
166 void StringToSettings(const std::string &value);
167
172 std::string SettingsToString() const;
173
180 std::optional<std::string> GetTextfile(TextfileType type, CompanyID slot) const;
181
182 void SetToLoadData(ScriptInstance::ScriptData *data);
183 ScriptInstance::ScriptData *GetToLoadData();
184
185 protected:
186 std::string name;
187 int version;
188 class ScriptInfo *info;
189 SettingValueList settings;
190 std::unique_ptr<ScriptConfigItemList> config_list;
191 bool is_random;
192 std::unique_ptr<ScriptInstance::ScriptData> to_load_data;
193
197 void ClearConfigList();
198
203 virtual ScriptInfo *FindInfo(const std::string &name, int version, bool force_exact_match) = 0;
204 };
205
206 #endif /* SCRIPT_CONFIG_HPP */
ScriptConfig::config_list
std::unique_ptr< ScriptConfigItemList > config_list
List with all settings defined by this Script.
Definition: script_config.hpp:190
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
ScriptConfigItem::step_size
int step_size
The step size in the gui.
Definition: script_config.hpp:39
ScriptConfigFlags
ScriptConfigFlags
Bitmask of flags for Script settings.
Definition: script_config.hpp:21
ScriptConfigItem::name
std::string name
The name of the configuration setting.
Definition: script_config.hpp:33
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
ScriptConfig::SettingValueList
std::map< std::string, int > SettingValueList
List with name=>value pairs of all script-specific settings.
Definition: script_config.hpp:53
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
SCRIPTCONFIG_BOOLEAN
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
Definition: script_config.hpp:24
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:35
ScriptConfigItem::complete_labels
bool complete_labels
True if all values have a label.
Definition: script_config.hpp:42
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:36
ScriptConfig::AddRandomDeviation
void AddRandomDeviation()
Randomize all settings the Script requested to be randomized.
Definition: script_config.cpp:128
ScriptConfig::SSS_DEFAULT
@ SSS_DEFAULT
Get the Script config from the current game mode.
Definition: script_config.hpp:97
ScriptConfig::GetInfo
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
Definition: script_config.cpp:56
SCRIPTCONFIG_NONE
@ SCRIPTCONFIG_NONE
No flags set.
Definition: script_config.hpp:22
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
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
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
ScriptConfig
Script settings.
Definition: script_config.hpp:50
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition: script_config.hpp:98
ScriptConfig::GetSetting
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
Definition: script_config.cpp:84
script_instance.hpp
ScriptConfig::name
std::string name
Name of the Script.
Definition: script_config.hpp:186
ScriptConfigItem::default_value
int default_value
The default value of this configuration setting.
Definition: script_config.hpp:37
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::description
std::string description
The description of the configuration setting.
Definition: script_config.hpp:34
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
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
ScriptConfigItem::labels
LabelMapping labels
Text labels for the integer values.
Definition: script_config.hpp:41
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
LabelMapping
std::map< int, std::string > LabelMapping
Map-type used to map the setting numbers to labels.
Definition: script_config.hpp:29
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:99
ScriptConfigItem::random_deviation
int random_deviation
The maximum random deviation from the default value.
Definition: script_config.hpp:38
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
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:189
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:96
ScriptConfig::GetName
const std::string & GetName() const
Get the name of the Script.
Definition: script_config.cpp:147