From cb3784d8b1a8aa20eab38cf28ee7a2649039ed4f Mon Sep 17 00:00:00 2001 From: Yexo Date: Fri, 6 Feb 2009 00:25:37 +0000 Subject: (svn r15366) -Add [NoAI]: Add AddLabels() where you can define labels for the values of the settings in info.nut --- src/ai/ai_info.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/ai/ai_info.cpp') diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp index 8dae0511e..4c217680c 100644 --- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -210,6 +210,12 @@ AIInfo::~AIInfo() for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) { free((char *)(*it).name); free((char *)(*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(); } @@ -323,6 +329,49 @@ SQInteger AIInfo::AddSetting(HSQUIRRELVM vm) return 0; } +SQInteger AIInfo::AddLabels(HSQUIRRELVM vm) +{ + const SQChar *sq_setting_name; + sq_getstring(vm, -2, &sq_setting_name); + const char *setting_name = FS2OTTD(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; + sq_getstring(vm, -2, &sq_key); + sq_getstring(vm, -1, &sq_label); + /* Because squirrel doesn't support identifiers starting with a digit, + * we skip the first character. */ + const char *key_string = FS2OTTD(sq_key); + int key = atoi(key_string + 1); + const char *label = FS2OTTD(sq_label); + + if (config->labels->Find(key) == config->labels->End()) config->labels->Insert(key, strdup(label)); + + sq_pop(vm, 2); + } + sq_pop(vm, 1); + + return 0; +} + const AIConfigItemList *AIInfo::GetConfigList() { return &this->config_list; -- cgit v1.2.3-54-g00ecf