2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2009-01-13 12:52:09 +00:00
|
|
|
#include "../core/random_func.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai.hpp"
|
|
|
|
#include "ai_config.hpp"
|
|
|
|
|
2010-01-29 00:03:31 +00:00
|
|
|
void AIConfig::ChangeAI(const char *name, int version, bool force_exact_match, bool is_random_ai)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
free((void *)this->name);
|
|
|
|
this->name = (name == NULL) ? NULL : strdup(name);
|
2010-01-29 00:03:31 +00:00
|
|
|
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version, force_exact_match);
|
2009-01-13 00:10:58 +00:00
|
|
|
this->version = (info == NULL) ? -1 : info->GetVersion();
|
2010-01-09 14:41:22 +00:00
|
|
|
this->is_random_ai = is_random_ai;
|
2009-01-20 16:49:10 +00:00
|
|
|
if (this->config_list != NULL) delete this->config_list;
|
|
|
|
this->config_list = (info == NULL) ? NULL : new AIConfigItemList();
|
|
|
|
if (this->config_list != NULL) this->config_list->push_back(_start_date_config);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-01-13 16:53:03 +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 AI. */
|
|
|
|
int start_date = this->GetSetting("start_date");
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
|
|
|
free((void*)(*it).first);
|
|
|
|
}
|
|
|
|
this->settings.clear();
|
2009-01-13 12:52:09 +00:00
|
|
|
|
2009-01-13 16:53:03 +00:00
|
|
|
this->SetSetting("start_date", start_date);
|
|
|
|
|
2009-01-13 12:52:09 +00:00
|
|
|
if (_game_mode == GM_NORMAL && this->info != NULL) {
|
|
|
|
/* If we're in an existing game and the AI is changed, set all settings
|
|
|
|
* for the AI that have the random flag to a random value. */
|
|
|
|
for (AIConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
|
|
|
|
if ((*it).flags & AICONFIG_RANDOM) {
|
|
|
|
this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value - (*it).min_value) + (*it).min_value);
|
|
|
|
}
|
|
|
|
}
|
2009-01-13 16:53:03 +00:00
|
|
|
this->AddRandomDeviation();
|
2009-01-13 12:52:09 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2009-01-13 00:10:58 +00:00
|
|
|
AIConfig::AIConfig(const AIConfig *config)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
this->name = (config->name == NULL) ? NULL : strdup(config->name);
|
|
|
|
this->info = config->info;
|
2009-01-13 00:10:58 +00:00
|
|
|
this->version = config->version;
|
2009-01-20 16:49:10 +00:00
|
|
|
this->config_list = NULL;
|
2010-01-09 14:41:22 +00:00
|
|
|
this->is_random_ai = config->is_random_ai;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
|
|
|
|
this->settings[strdup((*it).first)] = (*it).second;
|
|
|
|
}
|
2009-01-13 16:53:03 +00:00
|
|
|
this->AddRandomDeviation();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AIConfig::~AIConfig()
|
|
|
|
{
|
2009-01-17 15:07:35 +00:00
|
|
|
free((void *)this->name);
|
2009-01-20 16:49:10 +00:00
|
|
|
this->ResetSettings();
|
|
|
|
if (this->config_list != NULL) delete this->config_list;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
AIInfo *AIConfig::GetInfo() const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return this->info;
|
|
|
|
}
|
|
|
|
|
2011-06-03 19:18:39 +00:00
|
|
|
bool AIConfig::ResetInfo(bool force_exact_match)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-06-03 19:18:39 +00:00
|
|
|
this->info = AI::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
|
2009-01-21 00:18:30 +00:00
|
|
|
return this->info != NULL;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2009-01-20 16:49:10 +00:00
|
|
|
const AIConfigItemList *AIConfig::GetConfigList()
|
|
|
|
{
|
|
|
|
if (this->info != NULL) return this->info->GetConfigList();
|
|
|
|
if (this->config_list == NULL) {
|
|
|
|
this->config_list = new AIConfigItemList();
|
|
|
|
this->config_list->push_back(_start_date_config);
|
|
|
|
}
|
|
|
|
return this->config_list;
|
|
|
|
}
|
|
|
|
|
2010-03-15 22:42:43 +00:00
|
|
|
AIConfig *AIConfig::GetConfig(CompanyID company, AISettingSource source)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
AIConfig **config;
|
2010-03-15 22:42:43 +00:00
|
|
|
if (source == AISS_FORCE_NEWGAME || (source == AISS_DEFAULT && _game_mode == GM_MENU)) {
|
2009-01-12 17:11:45 +00:00
|
|
|
config = &_settings_newgame.ai_config[company];
|
2010-03-15 22:42:43 +00:00
|
|
|
} else {
|
|
|
|
config = &_settings_game.ai_config[company];
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
if (*config == NULL) *config = new AIConfig();
|
|
|
|
return *config;
|
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
int AIConfig::GetSetting(const char *name) const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2010-01-07 00:09:27 +00:00
|
|
|
SettingValueList::const_iterator it = this->settings.find(name);
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Return the default value if the setting is not set, or if we are in a not-custom difficult level */
|
2011-01-02 12:52:37 +00:00
|
|
|
if (it == this->settings.end() || GetGameSettings().difficulty.diff_level != 3) {
|
2009-01-13 16:53:03 +00:00
|
|
|
if (this->info == NULL) {
|
|
|
|
assert(strcmp("start_date", name) == 0);
|
2011-01-02 12:52:37 +00:00
|
|
|
switch (GetGameSettings().difficulty.diff_level) {
|
2009-01-13 16:53:03 +00:00
|
|
|
case 0: return AI::START_NEXT_EASY;
|
|
|
|
case 1: return AI::START_NEXT_MEDIUM;
|
|
|
|
case 2: return AI::START_NEXT_HARD;
|
|
|
|
case 3: return AI::START_NEXT_MEDIUM;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
return this->info->GetSettingDefaultValue(name);
|
|
|
|
}
|
|
|
|
return (*it).second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AIConfig::SetSetting(const char *name, int value)
|
|
|
|
{
|
|
|
|
/* You can only set ai specific settings if an AI is selected. */
|
2009-01-13 16:53:03 +00:00
|
|
|
if (this->info == NULL && strcmp("start_date", name) != 0) return;
|
2009-01-13 13:09:49 +00:00
|
|
|
|
2009-01-13 16:53:03 +00:00
|
|
|
if (this->info == NULL && strcmp("start_date", name) == 0) {
|
|
|
|
value = Clamp(value, AI::START_NEXT_MIN, AI::START_NEXT_MAX);
|
|
|
|
} else {
|
|
|
|
const AIConfigItem *config_item = this->info->GetConfigItem(name);
|
|
|
|
if (config_item == NULL) return;
|
2009-01-13 13:09:49 +00:00
|
|
|
|
2009-01-13 16:53:03 +00:00
|
|
|
value = Clamp(value, config_item->min_value, config_item->max_value);
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
SettingValueList::iterator it = this->settings.find(name);
|
|
|
|
if (it != this->settings.end()) {
|
|
|
|
(*it).second = value;
|
|
|
|
} else {
|
|
|
|
this->settings[strdup(name)] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:49:10 +00:00
|
|
|
void AIConfig::ResetSettings()
|
2009-01-13 16:53:03 +00:00
|
|
|
{
|
2009-01-20 16:49:10 +00:00
|
|
|
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
|
|
|
free((void*)(*it).first);
|
2009-01-13 16:53:03 +00:00
|
|
|
}
|
2009-01-20 16:49:10 +00:00
|
|
|
this->settings.clear();
|
|
|
|
}
|
2009-01-13 16:53:03 +00:00
|
|
|
|
2009-01-20 16:49:10 +00:00
|
|
|
void AIConfig::AddRandomDeviation()
|
|
|
|
{
|
|
|
|
for (AIConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
|
2009-01-13 16:53:03 +00:00
|
|
|
if ((*it).random_deviation != 0) {
|
|
|
|
this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
bool AIConfig::HasAI() const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return this->info != NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-09 14:41:22 +00:00
|
|
|
bool AIConfig::IsRandomAI() const
|
|
|
|
{
|
|
|
|
return this->is_random_ai;
|
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
const char *AIConfig::GetName() const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
return this->name;
|
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
int AIConfig::GetVersion() const
|
2009-01-13 00:10:58 +00:00
|
|
|
{
|
|
|
|
return this->version;
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
void AIConfig::StringToSettings(const char *value)
|
|
|
|
{
|
|
|
|
char *value_copy = strdup(value);
|
|
|
|
char *s = value_copy;
|
|
|
|
|
|
|
|
while (s != NULL) {
|
|
|
|
/* Analyze the string ('name=value,name=value\0') */
|
|
|
|
char *item_name = s;
|
|
|
|
s = strchr(s, '=');
|
|
|
|
if (s == NULL) break;
|
|
|
|
if (*s == '\0') break;
|
|
|
|
*s = '\0';
|
|
|
|
s++;
|
|
|
|
|
|
|
|
char *item_value = s;
|
|
|
|
s = strchr(s, ',');
|
|
|
|
if (s != NULL) {
|
|
|
|
*s = '\0';
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->SetSetting(item_name, atoi(item_value));
|
|
|
|
}
|
|
|
|
free(value_copy);
|
|
|
|
}
|
|
|
|
|
2010-01-07 00:09:27 +00:00
|
|
|
void AIConfig::SettingsToString(char *string, size_t size) const
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
string[0] = '\0';
|
2010-01-07 00:09:27 +00:00
|
|
|
for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
2009-01-12 17:11:45 +00:00
|
|
|
char no[10];
|
|
|
|
snprintf(no, sizeof(no), "%d", (*it).second);
|
|
|
|
|
|
|
|
/* Check if the string would fit in the destination */
|
2009-03-07 20:55:24 +00:00
|
|
|
size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
|
2009-01-12 17:11:45 +00:00
|
|
|
/* If it doesn't fit, skip the next settings */
|
2009-03-07 20:55:24 +00:00
|
|
|
if (size <= needed_size) break;
|
|
|
|
size -= needed_size;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
strcat(string, (*it).first);
|
|
|
|
strcat(string, "=");
|
|
|
|
strcat(string, no);
|
|
|
|
strcat(string, ",");
|
|
|
|
}
|
2009-01-20 15:58:40 +00:00
|
|
|
/* Remove the last ',', but only if at least one setting was saved. */
|
|
|
|
size_t len = strlen(string);
|
|
|
|
if (len > 0) string[len - 1] = '\0';
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|