summaryrefslogtreecommitdiff
path: root/src/ai/ai_info.cpp
diff options
context:
space:
mode:
authorYexo <yexo@openttd.org>2009-02-06 00:25:37 +0000
committerYexo <yexo@openttd.org>2009-02-06 00:25:37 +0000
commitcb3784d8b1a8aa20eab38cf28ee7a2649039ed4f (patch)
treebc4a66f71ecd553a2fcef1f5e1937be2943df355 /src/ai/ai_info.cpp
parent8f270af1242fc9e1746bfa3f4a7e8a70fdf6340f (diff)
downloadopenttd-cb3784d8b1a8aa20eab38cf28ee7a2649039ed4f.tar.xz
(svn r15366) -Add [NoAI]: Add AddLabels() where you can define labels for the values of the settings in info.nut
Diffstat (limited to 'src/ai/ai_info.cpp')
-rw-r--r--src/ai/ai_info.cpp49
1 files changed, 49 insertions, 0 deletions
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;