From 45da6d5c2be92b78b559ef8ab03cde0df98d8a69 Mon Sep 17 00:00:00 2001 From: atemo Date: Mon, 16 Nov 2020 18:35:06 +0100 Subject: [PATCH] Simple support of negative index in array -1 : getarraysize() - 1 -2 : getarraysize() - 2 ... Downside: no more warning when the index is negative (which was often used to check) --- doc/script_commands.txt | 4 ++++ src/map/script.cpp | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 280abee5adc..28755102aed 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -644,6 +644,10 @@ value from another array) to get at an array value: This will make .@arrayofnumbers[100] equal to 10. + .@arrayofnumbers[-1] = 20; + +This will make .@arrayofnumbers[100] (the last index) equal to 20. + Index numbering always starts with 0 and arrays can hold over 2 billion variables. As such, the (guaranteed) allowed values for indices are in the range 0 ~ 2147483647. diff --git a/src/map/script.cpp b/src/map/script.cpp index 1e9638c6768..4c648380b31 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -6522,13 +6522,17 @@ BUILDIN_FUNC(getelementofarray) id = reference_getid(data); i = script_getnum(st, 3); - if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) { + if (std::abs(i) >= SCRIPT_MAX_ARRAYSIZE) { ShowWarning("script:getelementofarray: index out of range (%" PRId64 ")\n", i); script_reportdata(data); script_pushnil(st); st->state = END; return SCRIPT_CMD_FAILURE;// out of range } + if (i < 0) { + struct map_session_data* sd = NULL; + i = cap_value( (script_array_highest_key(st, sd, reference_getname(data), reference_getref(data)) + i), 0, SCRIPT_MAX_ARRAYSIZE ); + } push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data)); return SCRIPT_CMD_SUCCESS;