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/>.
8 /** @file script_config.hpp ScriptConfig stores the configuration settings of every Script. */
10 #ifndef SCRIPT_CONFIG_HPP
11 #define SCRIPT_CONFIG_HPP
13 #include "../company_type.h"
14 #include "../textfile_gui.h"
15 #include "script_instance.hpp"
17 /** Maximum of 10 digits for MIN / MAX_INT32, 1 for the sign and 1 for '\0'. */
18 static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION
= 10 + 1 + 1;
20 /** Bitmask of flags for Script settings. */
21 enum ScriptConfigFlags
{
22 SCRIPTCONFIG_NONE
= 0x0, ///< No flags set.
24 SCRIPTCONFIG_BOOLEAN
= 0x2, ///< This value is a boolean (either 0 (false) or 1 (true) ).
25 SCRIPTCONFIG_INGAME
= 0x4, ///< This setting can be changed while the Script is running.
26 SCRIPTCONFIG_DEVELOPER
= 0x8, ///< This setting will only be visible when the Script development tools are active.
29 typedef std::map
<int, std::string
> LabelMapping
; ///< Map-type used to map the setting numbers to labels.
31 /** Info about a single Script setting. */
32 struct ScriptConfigItem
{
33 std::string name
; ///< The name of the configuration setting.
34 std::string description
; ///< The description of the configuration setting.
35 int min_value
= 0; ///< The minimal value this configuration setting can have.
36 int max_value
= 1; ///< The maximal value this configuration setting can have.
37 int default_value
= 0; ///< The default value of this configuration setting.
38 int step_size
= 1; ///< The step size in the gui.
39 ScriptConfigFlags flags
= SCRIPTCONFIG_NONE
; ///< Flags for the configuration setting.
40 LabelMapping labels
; ///< Text labels for the integer values.
41 bool complete_labels
= false; ///< True if all values have a label.
44 typedef std::vector
<ScriptConfigItem
> ScriptConfigItemList
; ///< List of ScriptConfig items.
51 /** List with name=>value pairs of all script-specific settings */
52 typedef std::map
<std::string
, int> SettingValueList
;
62 * Create a new Script config that is a copy of an existing config.
63 * @param config The object to copy.
65 ScriptConfig(const ScriptConfig
*config
);
67 /** Delete an Script configuration. */
68 virtual ~ScriptConfig();
71 * Set another Script to be loaded in this slot.
72 * @param name The name of the Script.
73 * @param version The version of the Script to load, or -1 of latest.
74 * @param force_exact_match If true try to find the exact same version
75 * as specified. If false any compatible version is ok.
77 void Change(std::optional
<const std::string
> name
, int version
= -1, bool force_exact_match
= false);
80 * Get the ScriptInfo linked to this ScriptConfig.
82 class ScriptInfo
*GetInfo() const;
85 * Get the config list for this ScriptConfig.
87 const ScriptConfigItemList
*GetConfigList();
90 * Where to get the config from, either default (depends on current game
91 * mode) or force either newgame or normal
93 enum ScriptSettingSource
{
94 SSS_DEFAULT
, ///< Get the Script config from the current game mode
95 SSS_FORCE_NEWGAME
, ///< Get the newgame Script config
96 SSS_FORCE_GAME
, ///< Get the Script config from the current game
100 * As long as the default of a setting has not been changed, the value of
101 * the setting is not stored. This to allow changing the difficulty setting
102 * without having to reset the script's config. However, when a setting may
103 * not be changed in game, we must "anchor" this value to what the setting
104 * would be at the time of starting. Otherwise changing the difficulty
105 * setting would change the setting's value (which isn't allowed).
107 void AnchorUnchangeableSettings();
110 * Get the value of a setting for this config. It might fallback to its
111 * 'info' to find the default value (if not set or if not-custom difficulty
113 * @return The (default) value of the setting, or -1 if the setting was not
116 int GetSetting(const std::string
&name
) const;
119 * Set the value of a setting for this config.
121 void SetSetting(const std::string_view name
, int value
);
124 * Reset all settings to their default value.
126 void ResetSettings();
129 * Reset only editable and visible settings to their default value.
131 void ResetEditableSettings(bool yet_to_start
);
134 * Is this config attached to an Script? In other words, is there a Script
135 * that is assigned to this slot.
137 bool HasScript() const;
140 * Get the name of the Script.
142 const std::string
&GetName() const;
145 * Get the version of the Script.
147 int GetVersion() const;
150 * Convert a string which is stored in the config file or savegames to
151 * custom settings of this Script.
153 void StringToSettings(const std::string
&value
);
156 * Convert the custom settings to a string that can be stored in the config
159 std::string
SettingsToString() const;
162 * Search a textfile file next to this script.
163 * @param type The type of the textfile to search for.
164 * @param slot #CompanyID to check status of.
165 * @return The filename for the textfile.
167 std::optional
<std::string
> GetTextfile(TextfileType type
, CompanyID slot
) const;
169 void SetToLoadData(ScriptInstance::ScriptData
*data
);
170 ScriptInstance::ScriptData
*GetToLoadData();
173 std::string name
; ///< Name of the Script
174 int version
; ///< Version of the Script
175 class ScriptInfo
*info
; ///< ScriptInfo object for related to this Script version
176 SettingValueList settings
; ///< List with all setting=>value pairs that are configure for this Script
177 std::unique_ptr
<ScriptConfigItemList
> config_list
; ///< List with all settings defined by this Script
178 std::unique_ptr
<ScriptInstance::ScriptData
> to_load_data
; ///< Data to load after the Script start.
181 * Routine that clears the config list.
183 void ClearConfigList();
186 * This function should call back to the Scanner in charge of this Config,
187 * to find the ScriptInfo belonging to a name+version.
189 virtual ScriptInfo
*FindInfo(const std::string
&name
, int version
, bool force_exact_match
) = 0;
192 #endif /* SCRIPT_CONFIG_HPP */