2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file ai_config.cpp Implementation of AIConfig. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../settings_type.h"
|
2014-04-25 15:40:32 +00:00
|
|
|
#include "../string_func.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai.hpp"
|
|
|
|
#include "ai_config.hpp"
|
2011-11-29 23:26:35 +00:00
|
|
|
#include "ai_info.hpp"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
/** Configuration for AI start date, every AI has this setting. */
|
|
|
|
ScriptConfigItem _start_date_config = {
|
|
|
|
"start_date",
|
2011-12-18 18:21:55 +00:00
|
|
|
"", // STR_AI_SETTINGS_START_DELAY
|
2011-11-29 23:26:35 +00:00
|
|
|
AI::START_NEXT_MIN,
|
|
|
|
AI::START_NEXT_MAX,
|
|
|
|
AI::START_NEXT_MEDIUM,
|
|
|
|
AI::START_NEXT_EASY,
|
|
|
|
AI::START_NEXT_MEDIUM,
|
|
|
|
AI::START_NEXT_HARD,
|
|
|
|
AI::START_NEXT_DEVIATION,
|
|
|
|
30,
|
|
|
|
SCRIPTCONFIG_NONE,
|
2019-04-10 21:07:06 +00:00
|
|
|
nullptr,
|
2012-06-01 15:19:59 +00:00
|
|
|
false
|
2011-11-29 23:26:35 +00:00
|
|
|
};
|
|
|
|
|
2019-02-11 18:59:55 +00:00
|
|
|
AIConfig::AIConfig(const AIConfig *config) : ScriptConfig(config)
|
|
|
|
{
|
|
|
|
/* Override start_date as per AIConfig::AddRandomDeviation().
|
|
|
|
* This is necessary because the ScriptConfig constructor will instead call
|
|
|
|
* ScriptConfig::AddRandomDeviation(). */
|
|
|
|
int start_date = config->GetSetting("start_date");
|
2021-01-08 10:16:18 +00:00
|
|
|
this->SetSetting("start_date", start_date != 0 ? std::max(1, this->GetSetting("start_date")) : 0);
|
2019-02-11 18:59:55 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
/* static */ AIConfig *AIConfig::GetConfig(CompanyID company, ScriptSettingSource source)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:26:35 +00:00
|
|
|
AIConfig **config;
|
|
|
|
if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
|
|
|
|
config = &_settings_newgame.ai_config[company];
|
|
|
|
} else {
|
|
|
|
config = &_settings_game.ai_config[company];
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
if (*config == nullptr) *config = new AIConfig();
|
2011-11-29 23:26:35 +00:00
|
|
|
return *config;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
class AIInfo *AIConfig::GetInfo() const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:26:35 +00:00
|
|
|
return static_cast<class AIInfo *>(ScriptConfig::GetInfo());
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
ScriptInfo *AIConfig::FindInfo(const char *name, int version, bool force_exact_match)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:26:35 +00:00
|
|
|
return static_cast<ScriptInfo *>(AI::FindInfo(name, version, force_exact_match));
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-06-03 19:18:39 +00:00
|
|
|
bool AIConfig::ResetInfo(bool force_exact_match)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:26:35 +00:00
|
|
|
this->info = (ScriptInfo *)AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
|
2019-04-10 21:07:06 +00:00
|
|
|
return this->info != nullptr;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
void AIConfig::PushExtraConfigList()
|
2009-01-20 16:49:10 +00:00
|
|
|
{
|
2011-11-29 23:26:35 +00:00
|
|
|
this->config_list->push_back(_start_date_config);
|
2009-01-20 16:49:10 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
void AIConfig::ClearConfigList()
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:26:35 +00:00
|
|
|
/* The special casing for start_date is here to ensure that the
|
|
|
|
* start_date setting won't change even if you chose another Script. */
|
|
|
|
int start_date = this->GetSetting("start_date");
|
|
|
|
|
|
|
|
ScriptConfig::ClearConfigList();
|
|
|
|
|
|
|
|
this->SetSetting("start_date", start_date);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
int AIConfig::GetSetting(const char *name) const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->info == nullptr) {
|
2011-11-29 23:26:35 +00:00
|
|
|
SettingValueList::const_iterator it = this->settings.find(name);
|
2012-12-05 19:35:38 +00:00
|
|
|
if (it == this->settings.end()) {
|
2009-01-13 16:53:03 +00:00
|
|
|
assert(strcmp("start_date", name) == 0);
|
2012-12-05 19:36:04 +00:00
|
|
|
switch (GetGameSettings().script.settings_profile) {
|
2012-12-05 19:34:45 +00:00
|
|
|
case SP_EASY: return AI::START_NEXT_EASY;
|
|
|
|
case SP_MEDIUM: return AI::START_NEXT_MEDIUM;
|
|
|
|
case SP_HARD: return AI::START_NEXT_HARD;
|
|
|
|
case SP_CUSTOM: return AI::START_NEXT_MEDIUM;
|
2009-01-13 16:53:03 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
return (*it).second;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
return ScriptConfig::GetSetting(name);
|
2009-01-13 16:53:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
void AIConfig::SetSetting(const char *name, int value)
|
2009-01-13 00:10:58 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->info == nullptr) {
|
2011-11-29 23:26:35 +00:00
|
|
|
if (strcmp("start_date", name) != 0) return;
|
|
|
|
value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
|
2009-01-13 00:10:58 +00:00
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
SettingValueList::iterator it = this->settings.find(name);
|
|
|
|
if (it != this->settings.end()) {
|
|
|
|
(*it).second = value;
|
|
|
|
} else {
|
2014-04-25 15:40:32 +00:00
|
|
|
this->settings[stredup(name)] = value;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
return;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:26:35 +00:00
|
|
|
ScriptConfig::SetSetting(name, value);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
2019-01-31 14:35:13 +00:00
|
|
|
|
|
|
|
void AIConfig::AddRandomDeviation()
|
|
|
|
{
|
|
|
|
int start_date = this->GetSetting("start_date");
|
|
|
|
|
|
|
|
ScriptConfig::AddRandomDeviation();
|
|
|
|
|
|
|
|
/* start_date = 0 is a special case, where random deviation does not occur.
|
|
|
|
* If start_date was not already 0, then a minimum value of 1 must apply. */
|
2021-01-08 10:16:18 +00:00
|
|
|
this->SetSetting("start_date", start_date != 0 ? std::max(1, this->GetSetting("start_date")) : 0);
|
2019-01-31 14:35:13 +00:00
|
|
|
}
|