From 8351b97f5293caa4b12840e2eec51f74da202409 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Sat, 4 Feb 2023 14:24:10 +0000 Subject: [PATCH] Add: [Script] Labels for negative values of a setting --- src/script/api/script_info_docs.hpp | 5 ++++- src/script/script_info.cpp | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/script/api/script_info_docs.hpp b/src/script/api/script_info_docs.hpp index fad4b152df..cae5852180 100644 --- a/src/script/api/script_info_docs.hpp +++ b/src/script/api/script_info_docs.hpp @@ -254,13 +254,16 @@ public: * user will see the corresponding name. * @param setting_name The name of the setting. * @param value_names A table that maps values to names. The first - * character of every identifier is ignored and the rest should + * character of every identifier is ignored, the second character + * could be '_' to indicate the value is negative, and the rest should * be an integer of the value you define a name for. The value * is a short description of that value. * To define labels for a setting named "competition_level" you could * for example call it like this: * AddLabels("competition_level", {_0 = "no competition", _1 = "some competition", * _2 = "a lot of competition"}); + * Another example, for a setting with a negative value: + * AddLabels("amount", {__1 = "less than one", _0 = "none", _1 = "more than one"}); * * @note This is a function provided by OpenTTD, you don't have to * include it in your Script but should just call it from GetSettings. diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index dc4e07dee2..79f4982fb6 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -252,7 +252,14 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm) if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR; /* Because squirrel doesn't support identifiers starting with a digit, * we skip the first character. */ - int key = atoi(key_string + 1); + key_string++; + int sign = 1; + if (*key_string == '_') { + /* When the second character is '_', it indicates the value is negative. */ + sign = -1; + key_string++; + } + int key = atoi(key_string) * sign; StrMakeValidInPlace(const_cast(label)); /* !Contains() prevents stredup from leaking. */