From 1f9c83ef453741b7029b5706c7cd52edc7ce9039 Mon Sep 17 00:00:00 2001 From: Emistry Haoyan Date: Tue, 14 May 2019 23:44:20 +0800 Subject: Add *cap_value script command - add `cap_value(value, min, max)` script command. - avoid the ugly nested `min()` and `max()` combo if needed. --- doc/script_commands.txt | 12 ++++++++++++ src/map/script.c | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 516454365..2cbeff37b 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -8536,6 +8536,18 @@ Example: --------------------------------------- +*cap_value(, , ) + +Returns the number but capped between and . + +Example: + // capped between 0 ~ 100 + .@value = cap_value(10, 0, 100); // .@value will be equal to 10 + .@value = cap_value(1000, 0, 100); // .@value will be equal to 100 + .@value = cap_value(-10, 3, 100); // .@value will be equal to 3 + +--------------------------------------- + *md5("") Returns the md5 checksum of a number or string. diff --git a/src/map/script.c b/src/map/script.c index 5843ac292..8f56b8ee1 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -17778,6 +17778,17 @@ static BUILDIN(max) return true; } +static BUILDIN(cap_value) +{ + int value = script_getnum(st, 2); + int min = script_getnum(st, 3); + int max = script_getnum(st, 4); + + script_pushint(st, (int)cap_value(value, min, max)); + + return true; +} + static BUILDIN(md5) { const char *tmpstr; @@ -25797,6 +25808,7 @@ static void script_parse_builtin(void) // <--- List of mathematics commands BUILDIN_DEF(min, "i*"), BUILDIN_DEF(max, "i*"), + BUILDIN_DEF(cap_value, "iii"), BUILDIN_DEF(md5,"s"), BUILDIN_DEF(swap,"rr"), // [zBuffer] List of dynamic var commands ---> -- cgit v1.2.3-60-g2f50