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 ai_config.cpp Implementation of AIConfig. */
10 #include "../stdafx.h"
11 #include "../settings_type.h"
12 #include "../string_func.h"
14 #include "ai_config.hpp"
15 #include "ai_info.hpp"
17 #include "../safeguards.h"
19 /** Configuration for AI start date, every AI has this setting. */
20 ScriptConfigItem _start_date_config
= {
22 "", // STR_AI_SETTINGS_START_DELAY
25 AI::START_NEXT_MEDIUM
,
27 AI::START_NEXT_MEDIUM
,
29 AI::START_NEXT_DEVIATION
,
36 AIConfig::AIConfig(const AIConfig
*config
) : ScriptConfig(config
)
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);
45 /* static */ AIConfig
*AIConfig::GetConfig(CompanyID company
, ScriptSettingSource source
)
48 if (source
== SSS_FORCE_NEWGAME
|| (source
== SSS_DEFAULT
&& _game_mode
== GM_MENU
)) {
49 config
= &_settings_newgame
.ai_config
[company
];
51 config
= &_settings_game
.ai_config
[company
];
53 if (*config
== nullptr) *config
= new AIConfig();
57 class AIInfo
*AIConfig::GetInfo() const
59 return static_cast<class AIInfo
*>(ScriptConfig::GetInfo());
62 ScriptInfo
*AIConfig::FindInfo(const char *name
, int version
, bool force_exact_match
)
64 return static_cast<ScriptInfo
*>(AI::FindInfo(name
, version
, force_exact_match
));
67 bool AIConfig::ResetInfo(bool force_exact_match
)
69 this->info
= (ScriptInfo
*)AI::FindInfo(this->name
, force_exact_match
? this->version
: -1, force_exact_match
);
70 return this->info
!= nullptr;
73 void AIConfig::PushExtraConfigList()
75 this->config_list
->push_back(_start_date_config
);
78 void AIConfig::ClearConfigList()
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");
84 ScriptConfig::ClearConfigList();
86 this->SetSetting("start_date", start_date
);
89 int AIConfig::GetSetting(const char *name
) const
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();
107 return ScriptConfig::GetSetting(name
);
110 void AIConfig::SetSetting(const char *name
, int value
)
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
);
116 SettingValueList::iterator it
= this->settings
.find(name
);
117 if (it
!= this->settings
.end()) {
118 (*it
).second
= value
;
120 this->settings
[stredup(name
)] = value
;
126 ScriptConfig::SetSetting(name
, value
);
129 void AIConfig::AddRandomDeviation()
131 int start_date
= this->GetSetting("start_date");
133 ScriptConfig::AddRandomDeviation();
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);