4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file script_config.hpp ScriptConfig stores the configuration settings of every Script. */
12 #ifndef SCRIPT_CONFIG_HPP
13 #define SCRIPT_CONFIG_HPP
17 #include "../core/smallmap_type.hpp"
18 #include "../core/string_compare_type.hpp"
19 #include "../company_type.h"
20 #include "../textfile_gui.h"
22 /** Bitmask of flags for Script settings. */
23 enum ScriptConfigFlags
{
24 SCRIPTCONFIG_NONE
= 0x0, ///< No flags set.
25 SCRIPTCONFIG_RANDOM
= 0x1, ///< When randomizing the Script, pick any value between min_value and max_value when on custom difficulty setting.
26 SCRIPTCONFIG_BOOLEAN
= 0x2, ///< This value is a boolean (either 0 (false) or 1 (true) ).
27 SCRIPTCONFIG_INGAME
= 0x4, ///< This setting can be changed while the Script is running.
28 SCRIPTCONFIG_DEVELOPER
= 0x8, ///< This setting will only be visible when the Script development tools are active.
31 typedef SmallMap
<int, char *> LabelMapping
; ///< Map-type used to map the setting numbers to labels.
33 /** Info about a single Script setting. */
34 struct ScriptConfigItem
{
35 const char *name
; ///< The name of the configuration setting.
36 const char *description
; ///< The description of the configuration setting.
37 int min_value
; ///< The minimal value this configuration setting can have.
38 int max_value
; ///< The maximal value this configuration setting can have.
39 int custom_value
; ///< The default value on custom difficulty setting.
40 int easy_value
; ///< The default value on easy difficulty setting.
41 int medium_value
; ///< The default value on medium difficulty setting.
42 int hard_value
; ///< The default value on hard difficulty setting.
43 int random_deviation
; ///< The maximum random deviation from the default value.
44 int step_size
; ///< The step size in the gui.
45 ScriptConfigFlags flags
; ///< Flags for the configuration setting.
46 LabelMapping
*labels
; ///< Text labels for the integer values.
47 bool complete_labels
; ///< True if all values have a label.
50 typedef std::list
<ScriptConfigItem
> ScriptConfigItemList
; ///< List of ScriptConfig items.
52 extern ScriptConfigItem _start_date_config
;
59 /** List with name=>value pairs of all script-specific settings */
60 typedef std::map
<const char *, int, StringCompare
> SettingValueList
;
72 * Create a new Script config that is a copy of an existing config.
73 * @param config The object to copy.
75 ScriptConfig(const ScriptConfig
*config
);
77 /** Delete an Script configuration. */
78 virtual ~ScriptConfig();
81 * Set another Script to be loaded in this slot.
82 * @param name The name of the Script.
83 * @param version The version of the Script to load, or -1 of latest.
84 * @param force_exact_match If true try to find the exact same version
85 * as specified. If false any compatible version is ok.
86 * @param is_random Is the Script chosen randomly?
88 void Change(const char *name
, int version
= -1, bool force_exact_match
= false, bool is_random
= false);
91 * Get the ScriptInfo linked to this ScriptConfig.
93 class ScriptInfo
*GetInfo() const;
96 * Get the config list for this ScriptConfig.
98 const ScriptConfigItemList
*GetConfigList();
101 * Where to get the config from, either default (depends on current game
102 * mode) or force either newgame or normal
104 enum ScriptSettingSource
{
105 SSS_DEFAULT
, ///< Get the Script config from the current game mode
106 SSS_FORCE_NEWGAME
, ///< Get the newgame Script config
107 SSS_FORCE_GAME
, ///< Get the Script config from the current game
111 * As long as the default of a setting has not been changed, the value of
112 * the setting is not stored. This to allow changing the difficulty setting
113 * without having to reset the script's config. However, when a setting may
114 * not be changed in game, we must "anchor" this value to what the setting
115 * would be at the time of starting. Otherwise changing the difficulty
116 * setting would change the setting's value (which isn't allowed).
118 void AnchorUnchangeableSettings();
121 * Get the value of a setting for this config. It might fallback to his
122 * 'info' to find the default value (if not set or if not-custom difficulty
124 * @return The (default) value of the setting, or -1 if the setting was not
127 virtual int GetSetting(const char *name
) const;
130 * Set the value of a setting for this config.
132 virtual void SetSetting(const char *name
, int value
);
135 * Reset all settings to their default value.
137 void ResetSettings();
140 * Randomize all settings the Script requested to be randomized.
142 void AddRandomDeviation();
145 * Is this config attached to an Script? In other words, is there a Script
146 * that is assigned to this slot.
148 bool HasScript() const;
151 * Is the current Script a randomly chosen Script?
153 bool IsRandom() const;
156 * Get the name of the Script.
158 const char *GetName() const;
161 * Get the version of the Script.
163 int GetVersion() const;
166 * Convert a string which is stored in the config file or savegames to
167 * custom settings of this Script.
169 void StringToSettings(const char *value
);
172 * Convert the custom settings to a string that can be stored in the config
175 void SettingsToString(char *string
, const char *last
) const;
178 * Search a textfile file next to this script.
179 * @param type The type of the textfile to search for.
180 * @param slot #CompanyID to check status of.
181 * @return The filename for the textfile, \c NULL otherwise.
183 const char *GetTextfile(TextfileType type
, CompanyID slot
) const;
186 const char *name
; ///< Name of the Script
187 int version
; ///< Version of the Script
188 class ScriptInfo
*info
; ///< ScriptInfo object for related to this Script version
189 SettingValueList settings
; ///< List with all setting=>value pairs that are configure for this Script
190 ScriptConfigItemList
*config_list
; ///< List with all settings defined by this Script
191 bool is_random
; ///< True if the AI in this slot was randomly chosen.
194 * In case you have mandatory non-Script-definable config entries in your
195 * list, add them to this function.
197 virtual void PushExtraConfigList() {};
200 * Routine that clears the config list.
202 virtual void ClearConfigList();
205 * This function should call back to the Scanner in charge of this Config,
206 * to find the ScriptInfo belonging to a name+version.
208 virtual ScriptInfo
*FindInfo(const char *name
, int version
, bool force_exact_match
) = 0;
211 #endif /* SCRIPT_CONFIG_HPP */