OpenTTD Source  13.2.1
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 <map>
14 #include <list>
15 #include "../core/smallmap_type.hpp"
16 #include "../core/string_compare_type.hpp"
17 #include "../company_type.h"
18 #include "../textfile_gui.h"
19 #include "script_instance.hpp"
20 
22 static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION = 10 + 1 + 1;
23 
31 };
32 
34 
37  const char *name;
38  const char *description;
39  int min_value;
40  int max_value;
42  int easy_value;
44  int hard_value;
46  int step_size;
50 };
51 
52 typedef std::list<ScriptConfigItem> ScriptConfigItemList;
53 
55 
59 class ScriptConfig {
60 protected:
62  typedef std::map<const char *, int, StringCompare> SettingValueList;
63 
64 public:
65  ScriptConfig() :
66  name(nullptr),
67  version(-1),
68  info(nullptr),
69  config_list(nullptr),
70  is_random(false),
71  to_load_data(nullptr)
72  {}
73 
78  ScriptConfig(const ScriptConfig *config);
79 
81  virtual ~ScriptConfig();
82 
91  void Change(const char *name, int version = -1, bool force_exact_match = false, bool is_random = false);
92 
96  class ScriptInfo *GetInfo() const;
97 
102 
111  };
112 
122 
130  virtual int GetSetting(const char *name) const;
131 
135  virtual void SetSetting(const char *name, int value);
136 
140  void ResetSettings();
141 
145  void ResetEditableSettings(bool yet_to_start);
146 
150  virtual void AddRandomDeviation();
151 
156  bool HasScript() const;
157 
161  bool IsRandom() const;
162 
166  const char *GetName() const;
167 
171  int GetVersion() const;
172 
177  void StringToSettings(const std::string &value);
178 
183  std::string SettingsToString() const;
184 
191  const char *GetTextfile(TextfileType type, CompanyID slot) const;
192 
193  void SetToLoadData(ScriptInstance::ScriptData *data);
194  ScriptInstance::ScriptData *GetToLoadData();
195 
196 protected:
197  const char *name;
198  int version;
199  class ScriptInfo *info;
202  bool is_random;
203  std::unique_ptr<ScriptInstance::ScriptData> to_load_data;
204 
209  virtual void PushExtraConfigList() {};
210 
214  virtual void ClearConfigList();
215 
220  virtual ScriptInfo *FindInfo(const char *name, int version, bool force_exact_match) = 0;
221 };
222 
223 #endif /* SCRIPT_CONFIG_HPP */
ScriptConfig::FindInfo
virtual ScriptInfo * FindInfo(const char *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...
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:22
ScriptConfigItem::step_size
int step_size
The step size in the gui.
Definition: script_config.hpp:46
ScriptConfigFlags
ScriptConfigFlags
Bitmask of flags for Script settings.
Definition: script_config.hpp:25
ScriptConfigItem::labels
LabelMapping * labels
Text labels for the integer values.
Definition: script_config.hpp:48
ScriptConfig::~ScriptConfig
virtual ~ScriptConfig()
Delete an Script configuration.
Definition: script_config.cpp:63
ScriptConfigItem::medium_value
int medium_value
The default value on medium difficulty setting.
Definition: script_config.hpp:43
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:136
SCRIPTCONFIG_BOOLEAN
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
Definition: script_config.hpp:28
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:39
SmallMap
Implementation of simple mapping class.
Definition: smallmap_type.hpp:26
ScriptConfigItem::complete_labels
bool complete_labels
True if all values have a label.
Definition: script_config.hpp:49
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:40
ScriptConfigItemList
std::list< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition: script_config.hpp:52
ScriptConfig::AddRandomDeviation
virtual void AddRandomDeviation()
Randomize all settings the Script requested to be randomized.
Definition: script_config.cpp:156
ScriptConfig::SSS_DEFAULT
@ SSS_DEFAULT
Get the Script config from the current game mode.
Definition: script_config.hpp:108
ScriptConfig::GetInfo
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
Definition: script_config.cpp:71
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:175
SCRIPTCONFIG_NONE
@ SCRIPTCONFIG_NONE
No flags set.
Definition: script_config.hpp:26
ScriptConfig::SetSetting
virtual void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: script_config.cpp:110
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:170
SCRIPTCONFIG_INGAME
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
Definition: script_config.hpp:29
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:30
ScriptConfig::version
int version
Version of the Script.
Definition: script_config.hpp:198
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:185
LabelMapping
SmallMap< int, char * > LabelMapping
Map-type used to map the setting numbers to labels.
Definition: script_config.hpp:33
ScriptConfigItem::easy_value
int easy_value
The default value on easy difficulty setting.
Definition: script_config.hpp:42
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:180
ScriptConfig::GetTextfile
const char * GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
Definition: script_config.cpp:238
ScriptConfig::config_list
ScriptConfigItemList * config_list
List with all settings defined by this Script.
Definition: script_config.hpp:201
ScriptConfig::ResetSettings
void ResetSettings()
Reset all settings to their default value.
Definition: script_config.cpp:128
ScriptConfigItem::custom_value
int custom_value
The default value on custom difficulty setting.
Definition: script_config.hpp:41
ScriptConfig::to_load_data
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
Definition: script_config.hpp:203
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:199
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:94
ScriptConfigItem::description
const char * description
The description of the configuration setting.
Definition: script_config.hpp:38
ScriptConfig::GetSetting
virtual int GetSetting(const char *name) const
Get the value of a setting for this config.
Definition: script_config.cpp:103
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:211
ScriptConfig
Script settings.
Definition: script_config.hpp:59
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition: script_config.hpp:109
script_instance.hpp
SCRIPTCONFIG_RANDOM
@ SCRIPTCONFIG_RANDOM
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
Definition: script_config.hpp:27
ScriptConfig::name
const char * name
Name of the Script.
Definition: script_config.hpp:197
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition: script_config.hpp:47
ScriptConfig::ClearConfigList
virtual void ClearConfigList()
Routine that clears the config list.
Definition: script_config.cpp:86
ScriptConfig::Change
void Change(const char *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:19
ScriptConfig::SettingValueList
std::map< const char *, int, StringCompare > SettingValueList
List with name=>value pairs of all script-specific settings.
Definition: script_config.hpp:62
ScriptConfig::GetConfigList
const ScriptConfigItemList * GetConfigList()
Get the config list for this ScriptConfig.
Definition: script_config.cpp:76
TextfileType
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14
ScriptConfig::PushExtraConfigList
virtual void PushExtraConfigList()
In case you have mandatory non-Script-definable config entries in your list, add them to this functio...
Definition: script_config.hpp:209
ScriptConfigItem::hard_value
int hard_value
The default value on hard difficulty setting.
Definition: script_config.hpp:44
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:36
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:110
ScriptConfigItem::random_deviation
int random_deviation
The maximum random deviation from the default value.
Definition: script_config.hpp:45
ScriptConfigItem::name
const char * name
The name of the configuration setting.
Definition: script_config.hpp:37
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:165
_start_date_config
ScriptConfigItem _start_date_config
Configuration for AI start date, every AI has this setting.
Definition: ai_config.cpp:20
ScriptConfig::is_random
bool is_random
True if the AI in this slot was randomly chosen.
Definition: script_config.hpp:202
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:200
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:107