From 6b20c5b6988c889df35b890d93c338f8b87fa430 Mon Sep 17 00:00:00 2001 From: Haru Date: Sun, 2 Nov 2014 01:30:33 +0100 Subject: Added min() and max() script commands - Special thanks to Streusel, Xgear Signed-off-by: Haru --- doc/script_commands.txt | 12 ++++++++++++ npc/dev/test.txt | 4 ++++ src/map/script.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 9d2317942..4e28b5b71 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -7586,6 +7586,18 @@ Example: --------------------------------------- +*min({,...}) +*max({,...}) + +Returns the smallest (or biggest) from the set of given numbers. + +Example: + .@minimum = min(1, -6, -2, 8, 2); // .@minimum will be equal to -6 + .@maximum = max(0, 5, 10, 4); // .@maximum will be equal to 10 + .@level = min(BaseLevel, 70); // .@level will be the character's base level, capped to 70 + +--------------------------------------- + *md5("") Returns the md5 checksum of a number or string. diff --git a/npc/dev/test.txt b/npc/dev/test.txt index f9e4fc410..7b498e922 100644 --- a/npc/dev/test.txt +++ b/npc/dev/test.txt @@ -565,6 +565,10 @@ function script HerculesSelfTestHelper { callsub(OnCheck, "array shrink", .@x[1], 0); callsub(OnCheck, "array shrink and getarraysize", getarraysize(.@x), 0); + // min and max + callsub(OnCheck, "min()", min(5, -10, 8, 3, -2, 1000), -10); + callsub(OnCheck, "max()", max(5, -10, 8, 3, -2, 1000), 1000); + // Constants callsub(OnCheck, "'true' constant", true, 1); diff --git a/src/map/script.c b/src/map/script.c index 1516a0234..006f275eb 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -14835,6 +14835,36 @@ BUILDIN(distance) // <--- [zBuffer] List of mathematics commands +BUILDIN(min) +{ + int i, min; + + min = script_getnum(st, 2); + for (i = 3; script_hasdata(st, i); i++) { + int next = script_getnum(st, i); + if (next < min) + min = next; + } + script_pushint(st, min); + + return true; +} + +BUILDIN(max) +{ + int i, max; + + max = script_getnum(st, 2); + for (i = 3; script_hasdata(st, i); i++) { + int next = script_getnum(st, i); + if (next > max) + max = next; + } + script_pushint(st, max); + + return true; +} + BUILDIN(md5) { const char *tmpstr; @@ -19283,6 +19313,8 @@ void script_parse_builtin(void) { BUILDIN_DEF(pow,"ii"), BUILDIN_DEF(distance,"iiii"), // <--- [zBuffer] List of mathematics commands + BUILDIN_DEF(min, "i*"), + BUILDIN_DEF(max, "i*"), BUILDIN_DEF(md5,"s"), // [zBuffer] List of dynamic var commands ---> BUILDIN_DEF(getd,"s"), -- cgit v1.2.3-60-g2f50