OpenTTD Source: src/ai/ai_config.cpp Source File

OpenTTD Source 1.11.0-RC1

ai_config.cpp
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 #include "../stdafx.h"
11 #include "../settings_type.h"
12 #include "../string_func.h"
13 #include "ai.hpp"
14 #include "ai_config.hpp"
15 #include "ai_info.hpp"
16
17 #include "../safeguards.h"
18
20 ScriptConfigItem _start_date_config = {
21 "start_date",
22 "", // STR_AI_SETTINGS_START_DELAY
23 AI::START_NEXT_MIN,
24 AI::START_NEXT_MAX,
25 AI::START_NEXT_MEDIUM,
26 AI::START_NEXT_EASY,
27 AI::START_NEXT_MEDIUM,
28 AI::START_NEXT_HARD,
29 AI::START_NEXT_DEVIATION,
30 30,
31 SCRIPTCONFIG_NONE,
32 nullptr,
33 false
34 };
35
36 AIConfig::AIConfig(const AIConfig *config) : ScriptConfig(config)
37 {
38 /* Override start_date as per AIConfig::AddRandomDeviation().
39 * This is necessary because the ScriptConfig constructor will instead call
40 * ScriptConfig::AddRandomDeviation(). */
41 int start_date = config->GetSetting("start_date");
42 this->SetSetting("start_date", start_date != 0 ? std::max(1, this->GetSetting("start_date")) : 0);
43 }
44
45 /* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
46 {
47 AIConfig **config;
48 if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
49 config = &_settings_newgame.ai_config[company];
50 } else {
51 config = &_settings_game.ai_config[company];
52 }
53 if (*config == nullptr) *config = new AIConfig();
54 return *config;
55 }
56
57 class AIInfo *AIConfig::GetInfo() const
58 {
59 return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
60 }
61
62 ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
63 {
64 return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
65 }
66
67 bool AIConfig::ResetInfo(bool force_exact_match)
68 {
69 this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
70 return this->info != nullptr;
71 }
72
73 void AIConfig::PushExtraConfigList()
74 {
75 this->config_list->push_back(_start_date_config);
76 }
77
78 void AIConfig::ClearConfigList()
79 {
80 /* The special casing for start_date is here to ensure that the
81 * start_date setting won't change even if you chose another Script. */
82 int start_date = this->GetSetting("start_date");
83
84 ScriptConfig::ClearConfigList();
85
86 this->SetSetting("start_date", start_date);
87 }
88
89 int AIConfig::GetSetting(const char *name) const
90 {
91 if (this->info == nullptr) {
92 SettingValueList::const_iterator it = this->settings.find(name);
93 if (it == this->settings.end()) {
94 assert(strcmp("start_date", name) == 0);
95 switch (GetGameSettings().script.settings_profile) {
96 case SP_EASY: return AI::START_NEXT_EASY;
97 case SP_MEDIUM: return AI::START_NEXT_MEDIUM;
98 case SP_HARD: return AI::START_NEXT_HARD;
99 case SP_CUSTOM: return AI::START_NEXT_MEDIUM;
100 default: NOT_REACHED();
101 }
102 }
103
104 return (*it).second;
105 }
106
107 return ScriptConfig::GetSetting(name);
108 }
109
110 void AIConfig::SetSetting(const char *name, int value)
111 {
112 if (this->info == nullptr) {
113 if (strcmp("start_date", name) != 0) return;
114 value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
115
116 SettingValueList::iterator it = this->settings.find(name);
117 if (it != this->settings.end()) {
118 (*it).second = value;
119 } else {
120 this->settings[stredup(name)] = value;
121 }
122
123 return;
124 }
125
126 ScriptConfig::SetSetting(name, value);
127 }
128
129 void AIConfig::AddRandomDeviation()
130 {
131 int start_date = this->GetSetting("start_date");
132
133 ScriptConfig::AddRandomDeviation();
134
135 /* start_date = 0 is a special case, where random deviation does not occur.
136 * If start_date was not already 0, then a minimum value of 1 must apply. */
137 this->SetSetting("start_date", start_date != 0 ? std::max(1, this->GetSetting("start_date")) : 0);
138 }
AIConfig
Definition: ai_config.hpp:16
GameSettings::ai_config
class AIConfig * ai_config[MAX_COMPANIES]
settings per company
Definition: settings_type.h:564
_start_date_config
ScriptConfigItem _start_date_config
Configuration for AI start date, every AI has this setting.
Definition: ai_config.cpp:20
SP_HARD
@ SP_HARD
Hard difficulty.
Definition: settings_type.h:39
AI::FindInfo
static class AIInfo * FindInfo(const char *name, int version, bool force_exact_match)
Wrapper function for AIScanner::FindInfo.
Definition: ai_core.cpp:338
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
AIConfig::AddRandomDeviation
void AddRandomDeviation() override
Randomize all settings the Script requested to be randomized.
Definition: ai_config.cpp:129
AIConfig::ClearConfigList
void ClearConfigList() override
Routine that clears the config list.
Definition: ai_config.cpp:78
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:161
ai.hpp
ai_info.hpp
SP_MEDIUM
@ SP_MEDIUM
Medium difficulty.
Definition: settings_type.h:38
GetGameSettings
static GameSettings & GetGameSettings()
Get the settings-object applicable for the current situation: the newgame settings when we're in the ...
Definition: settings_type.h:601
AIConfig::PushExtraConfigList
void PushExtraConfigList() override
In case you have mandatory non-Script-definable config entries in your list, add them to this functio...
Definition: ai_config.cpp:73
ScriptConfig::AddRandomDeviation
virtual void AddRandomDeviation()
Randomize all settings the Script requested to be randomized.
Definition: script_config.cpp:150
ScriptConfig::SSS_DEFAULT
@ SSS_DEFAULT
Get the Script config from the current game mode.
Definition: script_config.hpp:103
ScriptConfig::GetInfo
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
Definition: script_config.cpp:65
SCRIPTCONFIG_NONE
@ SCRIPTCONFIG_NONE
No flags set.
Definition: script_config.hpp:22
AIConfig::GetConfig
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:45
SP_CUSTOM
@ SP_CUSTOM
No profile, special "custom" highscore.
Definition: settings_type.h:43
ScriptConfig::SetSetting
virtual void SetSetting(const char *name, int value)
Set the value of a setting for this config.
Definition: script_config.cpp:104
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
ScriptConfig::version
int version
Version of the Script.
Definition: script_config.hpp:190
AIConfig::FindInfo
ScriptInfo * FindInfo(const char *name, int version, bool force_exact_match) override
This function should call back to the Scanner in charge of this Config, to find the ScriptInfo belong...
Definition: ai_config.cpp:62
ScriptConfig::config_list
ScriptConfigItemList * config_list
List with all settings defined by this Script.
Definition: script_config.hpp:193
SP_EASY
@ SP_EASY
Easy difficulty.
Definition: settings_type.h:37
ScriptConfig::info
class ScriptInfo * info
ScriptInfo object for related to this Script version.
Definition: script_config.hpp:191
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:77
ScriptConfig::GetSetting
virtual int GetSetting(const char *name) const
Get the value of a setting for this config.
Definition: script_config.cpp:97
AIConfig::SetSetting
void SetSetting(const char *name, int value) override
Set the value of a setting for this config.
Definition: ai_config.cpp:110
ScriptConfig
Script settings.
Definition: script_config.hpp:55
ScriptConfig::SSS_FORCE_NEWGAME
@ SSS_FORCE_NEWGAME
Get the newgame Script config.
Definition: script_config.hpp:104
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
AIInfo
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
ScriptConfig::name
const char * name
Name of the Script.
Definition: script_config.hpp:189
ScriptConfig::ClearConfigList
virtual void ClearConfigList()
Routine that clears the config list.
Definition: script_config.cpp:80
ScriptInfo::name
const char * name
Full name of the script.
Definition: script_info.hpp:156
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
AIConfig::GetSetting
int GetSetting(const char *name) const override
Get the value of a setting for this config.
Definition: ai_config.cpp:89
_settings_newgame
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:81
AIConfig::ResetInfo
bool ResetInfo(bool force_exact_match)
When ever the AI Scanner is reloaded, all infos become invalid.
Definition: ai_config.cpp:67
ai_config.hpp
ScriptConfig::settings
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
Definition: script_config.hpp:192
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:102