diff options
author | Yexo <yexo@openttd.org> | 2009-02-06 00:25:37 +0000 |
---|---|---|
committer | Yexo <yexo@openttd.org> | 2009-02-06 00:25:37 +0000 |
commit | cb3784d8b1a8aa20eab38cf28ee7a2649039ed4f (patch) | |
tree | bc4a66f71ecd553a2fcef1f5e1937be2943df355 /src | |
parent | 8f270af1242fc9e1746bfa3f4a7e8a70fdf6340f (diff) | |
download | openttd-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')
-rw-r--r-- | src/ai/ai_gui.cpp | 9 | ||||
-rw-r--r-- | src/ai/ai_info.cpp | 49 | ||||
-rw-r--r-- | src/ai/ai_info.hpp | 9 | ||||
-rw-r--r-- | src/ai/ai_scanner.cpp | 1 |
4 files changed, 65 insertions, 3 deletions
diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index a07ac4ede..62c2f37fb 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -261,9 +261,12 @@ struct AISettingsWindow : public Window { DrawFrameRect(4, y + 2, 23, y + 10, (current_value != 0) ? 6 : 4, (current_value != 0) ? FR_LOWERED : FR_NONE); } else { DrawArrowButtons(4, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + !!this->clicked_increase : 0, current_value > (*it).min_value, current_value < (*it).max_value); - static char buf[8]; - sprintf(buf, "%d", current_value); - x = DoDrawStringTruncated(buf, 28, y + 3, TC_ORANGE, this->width - 32); + if (it->labels != NULL && it->labels->Find(current_value) != it->labels->End()) { + x = DoDrawStringTruncated(it->labels->Find(current_value)->second, 28, y + 3, TC_ORANGE, this->width - 32); + } else { + SetDParam(0, current_value); + x = DrawStringTruncated(28, y + 3, STR_JUST_INT, TC_ORANGE, this->width - 32); + } } DoDrawStringTruncated((*it).description, max(x + 3, 54), y + 3, TC_LIGHT_BLUE, this->width - (4 + max(x + 3, 54))); 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; diff --git a/src/ai/ai_info.hpp b/src/ai/ai_info.hpp index fc04d6180..57317d3f0 100644 --- a/src/ai/ai_info.hpp +++ b/src/ai/ai_info.hpp @@ -6,6 +6,7 @@ #define AI_INFO #include <list> +#include "../core/smallmap_type.hpp" #include "api/ai_object.hpp" enum AIConfigFlags { @@ -14,6 +15,8 @@ enum AIConfigFlags { AICONFIG_BOOLEAN = 0x2, //!< This value is a boolean (either 0 (false) or 1 (true) ). }; +typedef SmallMap<int, char *> LabelMapping; + struct AIConfigItem { const char *name; //!< The name of the configuration setting. const char *description; //!< The description of the configuration setting. @@ -26,6 +29,7 @@ struct AIConfigItem { int random_deviation; //!< The maximum random deviation from the default value. int step_size; //!< The step size in the gui. AIConfigFlags flags; //!< Flags for the configuration setting. + LabelMapping *labels; //!< Text labels for the integer values. }; extern AIConfigItem _start_date_config; @@ -141,6 +145,11 @@ public: SQInteger AddSetting(HSQUIRRELVM vm); /** + * Add labels for a setting. + */ + SQInteger AddLabels(HSQUIRRELVM vm); + + /** * Get the default value for a setting. */ int GetSettingDefaultValue(const char *name); diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp index e9174504f..734058774 100644 --- a/src/ai/ai_scanner.cpp +++ b/src/ai/ai_scanner.cpp @@ -138,6 +138,7 @@ AIScanner::AIScanner() : SQAIInfo.PreRegister(engine); SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x"); SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddSetting, "AddSetting"); + SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddLabels, "AddLabels"); SQAIInfo.DefSQConst(engine, AICONFIG_RANDOM, "AICONFIG_RANDOM"); SQAIInfo.DefSQConst(engine, AICONFIG_BOOLEAN, "AICONFIG_BOOLEAN"); SQAIInfo.PostRegister(engine); |