Files
@ r10724:cec22216aab8
Branch filter:
Location: cpp/openttd-patchpack/source/src/ai/ai_config.cpp
r10724:cec22216aab8
4.2 KiB
text/x-c
(svn r15057) -Fix [NoAI]: clamp the values of a setting between the ones allowed by info.nut
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | /* $Id$ */
/** @file ai_config.cpp Implementation of AIConfig. */
#include "../stdafx.h"
#include "../openttd.h"
#include "../settings_type.h"
#include "../core/random_func.hpp"
#include "ai.hpp"
#include "ai_config.hpp"
#include "ai_info.hpp"
void AIConfig::ChangeAI(const char *name, int version)
{
free((void *)this->name);
this->name = (name == NULL) ? NULL : strdup(name);
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version);
this->version = (info == NULL) ? -1 : info->GetVersion();
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
free((void*)(*it).first);
}
this->settings.clear();
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);
}
}
}
}
AIConfig::AIConfig(const AIConfig *config)
{
this->name = (config->name == NULL) ? NULL : strdup(config->name);
this->info = config->info;
this->version = config->version;
for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
this->settings[strdup((*it).first)] = (*it).second;
}
}
AIConfig::~AIConfig()
{
this->ChangeAI(NULL);
}
AIInfo *AIConfig::GetInfo()
{
return this->info;
}
bool AIConfig::ResetInfo()
{
this->info = AI::FindInfo(this->name, this->version);
return this->info != NULL;
}
AIConfig *AIConfig::GetConfig(CompanyID company, bool forceNewgameSetting)
{
AIConfig **config;
if (!forceNewgameSetting) {
config = (_game_mode == GM_MENU) ? &_settings_newgame.ai_config[company] : &_settings_game.ai_config[company];
} else {
config = &_settings_newgame.ai_config[company];
}
if (*config == NULL) *config = new AIConfig();
return *config;
}
int AIConfig::GetSetting(const char *name)
{
assert(this->info != NULL);
SettingValueList::iterator it = this->settings.find(name);
/* Return the default value if the setting is not set, or if we are in a not-custom difficult level */
if (it == this->settings.end() || ((_game_mode == GM_MENU) ? _settings_newgame.difficulty.diff_level : _settings_game.difficulty.diff_level) != 3) {
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. */
assert(this->info != NULL);
const AIConfigItem *config_item = this->info->GetConfigItem(name);
if (config_item == NULL) return;
value = Clamp(value, config_item->min_value, config_item->max_value);
SettingValueList::iterator it = this->settings.find(name);
if (it != this->settings.end()) {
(*it).second = value;
} else {
this->settings[strdup(name)] = value;
}
}
bool AIConfig::HasAI()
{
return this->info != NULL;
}
const char *AIConfig::GetName()
{
return this->name;
}
int AIConfig::GetVersion()
{
return this->version;
}
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);
}
void AIConfig::SettingsToString(char *string, int size)
{
string[0] = '\0';
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
char no[10];
snprintf(no, sizeof(no), "%d", (*it).second);
/* Check if the string would fit in the destination */
size -= strlen((*it).first) - 1 - strlen(no) - 1;
/* If it doesn't fit, skip the next settings */
if (size <= 0) return;
strcat(string, (*it).first);
strcat(string, "=");
strcat(string, no);
strcat(string, ",");
}
string[strlen(string) - 1] = '\0';
}
|