OpenTTD Source  13.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 <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 
28 };
29 
31 
34  const char *name;
35  const char *description;
36  int min_value;
37  int max_value;
39  int easy_value;
41  int hard_value;
43  int step_size;
47 };
48 
49 typedef std::list<ScriptConfigItem> ScriptConfigItemList;
50 
52 
56 class ScriptConfig {
57 protected:
59  typedef std::map<const char *, int, StringCompare> SettingValueList;
60 
61 public:
62  ScriptConfig() :
63  name(nullptr),
64  version(-1),
65  info(nullptr),
66  config_list(nullptr),
67  is_random(false),
68  to_load_data(nullptr)
69  {}
70 
75  ScriptConfig(const ScriptConfig *config);
76 
78  virtual ~ScriptConfig();
79 
88  void Change(const char *name, int version = -1, bool force_exact_match = false, bool is_random = false);
89 
93  class ScriptInfo *GetInfo() const;
94 
99 
108  };
109 
119 
127  virtual int GetSetting(const char *name) const;
128 
132  virtual void SetSetting(const char *name, int value);
133 
137  void ResetSettings();
138 
142  void ResetEditableSettings(bool yet_to_start);
143 
147  virtual void AddRandomDeviation();
148 
153  bool HasScript() const;
154 
158  bool IsRandom() const;
159 
163  const char *GetName() const;
164 
168  int GetVersion() const;
169 
174  void StringToSettings(const std::string &value);
175 
180  std::string SettingsToString() const;
181 
188  const char *GetTextfile(TextfileType type, CompanyID slot) const;
189 
190  void SetToLoadData(ScriptInstance::ScriptData *data);
191  ScriptInstance::ScriptData *GetToLoadData();
192 
193 protected:
194  const char *name;
195  int version;
196  class ScriptInfo *info;
199  bool is_random;
200  std::unique_ptr<ScriptInstance::ScriptData> to_load_data;
201 
206  virtual void PushExtraConfigList() {};
207 
211  virtual void ClearConfigList();
212 
217  virtual ScriptInfo *FindInfo(const char *name, int version, bool force_exact_match) = 0;
218 };
219 
220 #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...
ScriptConfigItem::step_size
int step_size
The step size in the gui.
Definition: script_config.hpp:43
ScriptConfigFlags
ScriptConfigFlags
Bitmask of flags for Script settings.
Definition: script_config.hpp:22
ScriptConfigItem::labels
LabelMapping * labels
Text labels for the integer values.
Definition: script_config.hpp:45
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:40
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:25
ScriptConfigItem::min_value
int min_value
The minimal value this configuration setting can have.
Definition: script_config.hpp:36
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:46
ScriptConfigItem::max_value
int max_value
The maximal value this configuration setting can have.
Definition: script_config.hpp:37
ScriptConfigItemList
std::list< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
Definition: script_config.hpp:49
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:105
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:23
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:26
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:27
ScriptConfig::version
int version
Version of the Script.
Definition: script_config.hpp:195
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:30
ScriptConfigItem::easy_value
int easy_value
The default value on easy difficulty setting.
Definition: script_config.hpp:39
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:198
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:38
ScriptConfig::to_load_data
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
Definition: script_config.hpp:200
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:196
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:35
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:56
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition: script_config.hpp:106
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:24
ScriptConfig::name
const char * name
Name of the Script.
Definition: script_config.hpp:194
ScriptConfigItem::flags
ScriptConfigFlags flags
Flags for the configuration setting.
Definition: script_config.hpp:44
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:59
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:206
ScriptConfigItem::hard_value
int hard_value
The default value on hard difficulty setting.
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:33
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:107
ScriptConfigItem::random_deviation
int random_deviation
The maximum random deviation from the default value.
Definition: script_config.hpp:42
ScriptConfigItem::name
const char * name
The name of the configuration setting.
Definition: script_config.hpp:34
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:199
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:197
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:104