(svn r23364) -Codechange: refactor AIConfig, moving it mostly to Scriptconfig
This commit is contained in:
@@ -23,22 +23,6 @@
|
||||
/** Maximum number of operations allowed for getting a particular setting. */
|
||||
static const int MAX_GET_SETTING_OPS = 100000;
|
||||
|
||||
/** Configuration for AI start date, every AI has this setting. */
|
||||
AIConfigItem _start_date_config = {
|
||||
"start_date",
|
||||
"Number of days to start this AI after the previous one (give or take)",
|
||||
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,
|
||||
AICONFIG_NONE,
|
||||
NULL
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the API version provided by the AI is supported.
|
||||
* @param api_version The API version as provided by the AI.
|
||||
@@ -61,11 +45,18 @@ template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
|
||||
SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
|
||||
SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddSetting, "AddSetting");
|
||||
SQAIInfo.DefSQAdvancedMethod(engine, &AIInfo::AddLabels, "AddLabels");
|
||||
SQAIInfo.DefSQConst(engine, AICONFIG_NONE, "AICONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, AICONFIG_RANDOM, "AICONFIG_RANDOM");
|
||||
SQAIInfo.DefSQConst(engine, AICONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, AICONFIG_INGAME, "AICONFIG_INGAME");
|
||||
SQAIInfo.DefSQConst(engine, AICONFIG_AI_DEVELOPER, "AICONFIG_AI_DEVELOPER");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
|
||||
|
||||
/* Pre 1.2 had an AI prefix */
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "AICONFIG_NONE");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "AICONFIG_RANDOM");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
|
||||
SQAIInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "AICONFIG_INGAME");
|
||||
|
||||
SQAIInfo.PostRegister(engine);
|
||||
engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
|
||||
engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
|
||||
@@ -81,15 +72,11 @@ template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
|
||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
||||
if (res != 0) return res;
|
||||
|
||||
AIConfigItem config = _start_date_config;
|
||||
ScriptConfigItem config = _start_date_config;
|
||||
config.name = strdup(config.name);
|
||||
config.description = strdup(config.description);
|
||||
info->config_list.push_back(config);
|
||||
info->config_list.push_front(config);
|
||||
|
||||
/* Check if we have settings */
|
||||
if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) {
|
||||
if (!info->GetSettings()) return SQ_ERROR;
|
||||
}
|
||||
if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
|
||||
if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_SETTING_OPS)) return SQ_ERROR;
|
||||
} else {
|
||||
@@ -141,11 +128,6 @@ template <> const char *GetClassName<AIInfo, ST_AI>() { return "AIInfo"; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool AIInfo::GetSettings()
|
||||
{
|
||||
return this->engine->CallMethod(*this->SQ_instance, "GetSettings", NULL, MAX_GET_SETTING_OPS);
|
||||
}
|
||||
|
||||
AIInfo::AIInfo() :
|
||||
min_loadable_version(0),
|
||||
use_as_random(false),
|
||||
@@ -155,18 +137,6 @@ AIInfo::AIInfo() :
|
||||
|
||||
AIInfo::~AIInfo()
|
||||
{
|
||||
/* Free all allocated strings */
|
||||
for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
|
||||
free((*it).name);
|
||||
free((*it).description);
|
||||
if (it->labels != NULL) {
|
||||
for (LabelMapping::iterator it2 = (*it).labels->Begin(); it2 != (*it).labels->End(); it2++) {
|
||||
free(it2->second);
|
||||
}
|
||||
delete it->labels;
|
||||
}
|
||||
}
|
||||
this->config_list.clear();
|
||||
free(this->api_version);
|
||||
}
|
||||
|
||||
@@ -176,191 +146,6 @@ bool AIInfo::CanLoadFromVersion(int version) const
|
||||
return version >= this->min_loadable_version && version <= this->GetVersion();
|
||||
}
|
||||
|
||||
SQInteger AIInfo::AddSetting(HSQUIRRELVM vm)
|
||||
{
|
||||
AIConfigItem config;
|
||||
memset(&config, 0, sizeof(config));
|
||||
config.max_value = 1;
|
||||
config.step_size = 1;
|
||||
uint items = 0;
|
||||
|
||||
/* Read the table, and find all properties we care about */
|
||||
sq_pushnull(vm);
|
||||
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
||||
const SQChar *sqkey;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &sqkey))) return SQ_ERROR;
|
||||
const char *key = SQ2OTTD(sqkey);
|
||||
|
||||
if (strcmp(key, "name") == 0) {
|
||||
const SQChar *sqvalue;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
|
||||
char *name = strdup(SQ2OTTD(sqvalue));
|
||||
char *s;
|
||||
/* Don't allow '=' and ',' in configure setting names, as we need those
|
||||
* 2 chars to nicely store the settings as a string. */
|
||||
while ((s = strchr(name, '=')) != NULL) *s = '_';
|
||||
while ((s = strchr(name, ',')) != NULL) *s = '_';
|
||||
config.name = name;
|
||||
items |= 0x001;
|
||||
} else if (strcmp(key, "description") == 0) {
|
||||
const SQChar *sqdescription;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
|
||||
config.description = strdup(SQ2OTTD(sqdescription));
|
||||
items |= 0x002;
|
||||
} else if (strcmp(key, "min_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.min_value = res;
|
||||
items |= 0x004;
|
||||
} else if (strcmp(key, "max_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.max_value = res;
|
||||
items |= 0x008;
|
||||
} else if (strcmp(key, "easy_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.easy_value = res;
|
||||
items |= 0x010;
|
||||
} else if (strcmp(key, "medium_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.medium_value = res;
|
||||
items |= 0x020;
|
||||
} else if (strcmp(key, "hard_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.hard_value = res;
|
||||
items |= 0x040;
|
||||
} else if (strcmp(key, "random_deviation") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.random_deviation = res;
|
||||
items |= 0x200;
|
||||
} else if (strcmp(key, "custom_value") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.custom_value = res;
|
||||
items |= 0x080;
|
||||
} else if (strcmp(key, "step_size") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.step_size = res;
|
||||
} else if (strcmp(key, "flags") == 0) {
|
||||
SQInteger res;
|
||||
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
|
||||
config.flags = (AIConfigFlags)res;
|
||||
items |= 0x100;
|
||||
} else {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "unknown setting property '%s'", key);
|
||||
this->engine->ThrowError(error);
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
sq_pop(vm, 2);
|
||||
}
|
||||
sq_pop(vm, 1);
|
||||
|
||||
/* Don't allow both random_deviation and AICONFIG_RANDOM to
|
||||
* be set for the same config item. */
|
||||
if ((items & 0x200) != 0 && (config.flags & AICONFIG_RANDOM) != 0) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "Setting both random_deviation and AICONFIG_RANDOM is not allowed");
|
||||
this->engine->ThrowError(error);
|
||||
return SQ_ERROR;
|
||||
}
|
||||
/* Reset the bit for random_deviation as it's optional. */
|
||||
items &= ~0x200;
|
||||
|
||||
/* Make sure all properties are defined */
|
||||
uint mask = (config.flags & AICONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
|
||||
if (items != mask) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "please define all properties of a setting (min/max not allowed for booleans)");
|
||||
this->engine->ThrowError(error);
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
this->config_list.push_back(config);
|
||||
return 0;
|
||||
}
|
||||
|
||||
SQInteger AIInfo::AddLabels(HSQUIRRELVM vm)
|
||||
{
|
||||
const SQChar *sq_setting_name;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &sq_setting_name))) return SQ_ERROR;
|
||||
const char *setting_name = SQ2OTTD(sq_setting_name);
|
||||
|
||||
AIConfigItem *config = NULL;
|
||||
for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
|
||||
if (strcmp((*it).name, setting_name) == 0) config = &(*it);
|
||||
}
|
||||
|
||||
if (config == NULL) {
|
||||
char error[1024];
|
||||
snprintf(error, sizeof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
|
||||
this->engine->ThrowError(error);
|
||||
return SQ_ERROR;
|
||||
}
|
||||
if (config->labels != NULL) return SQ_ERROR;
|
||||
|
||||
config->labels = new LabelMapping;
|
||||
|
||||
/* Read the table and find all labels */
|
||||
sq_pushnull(vm);
|
||||
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
||||
const SQChar *sq_key;
|
||||
const SQChar *sq_label;
|
||||
if (SQ_FAILED(sq_getstring(vm, -2, &sq_key))) return SQ_ERROR;
|
||||
if (SQ_FAILED(sq_getstring(vm, -1, &sq_label))) return SQ_ERROR;
|
||||
/* Because squirrel doesn't support identifiers starting with a digit,
|
||||
* we skip the first character. */
|
||||
const char *key_string = SQ2OTTD(sq_key);
|
||||
int key = atoi(key_string + 1);
|
||||
const char *label = SQ2OTTD(sq_label);
|
||||
|
||||
/* !Contains() prevents strdup from leaking. */
|
||||
if (!config->labels->Contains(key)) config->labels->Insert(key, strdup(label));
|
||||
|
||||
sq_pop(vm, 2);
|
||||
}
|
||||
sq_pop(vm, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const AIConfigItemList *AIInfo::GetConfigList() const
|
||||
{
|
||||
return &this->config_list;
|
||||
}
|
||||
|
||||
const AIConfigItem *AIInfo::GetConfigItem(const char *name) const
|
||||
{
|
||||
for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
|
||||
if (strcmp((*it).name, name) == 0) return &(*it);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int AIInfo::GetSettingDefaultValue(const char *name) const
|
||||
{
|
||||
for (AIConfigItemList::const_iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
|
||||
if (strcmp((*it).name, name) != 0) continue;
|
||||
/* The default value depends on the difficulty level */
|
||||
switch (GetGameSettings().difficulty.diff_level) {
|
||||
case 0: return (*it).easy_value;
|
||||
case 1: return (*it).medium_value;
|
||||
case 2: return (*it).hard_value;
|
||||
case 3: return (*it).custom_value;
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
/* There is no such setting */
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
AILibrary::~AILibrary()
|
||||
{
|
||||
|
Reference in New Issue
Block a user