summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnnieRuru <jeankof@ymail.com>2015-12-23 18:05:43 +0800
committerHaru <haru@dotalux.com>2016-01-05 20:33:56 +0100
commita70f77c15946b419af2f33c9d6986fc351e54f5e (patch)
treec599f1d0410ba1edc7baf497fdd31fab0c642b26
parentabb6bb0c917031ad3c215b40a19bbf31f7bcd720 (diff)
downloadhercules-a70f77c15946b419af2f33c9d6986fc351e54f5e.tar.gz
hercules-a70f77c15946b419af2f33c9d6986fc351e54f5e.tar.bz2
hercules-a70f77c15946b419af2f33c9d6986fc351e54f5e.tar.xz
hercules-a70f77c15946b419af2f33c9d6986fc351e54f5e.zip
Add *swap script command
- extremely useful when writing algorithm functions Closes #1012 as merged Signed-off-by: Haru <haru@dotalux.com>
-rw-r--r--doc/script_commands.txt13
-rw-r--r--src/map/script.c71
2 files changed, 84 insertions, 0 deletions
diff --git a/doc/script_commands.txt b/doc/script_commands.txt
index e5663f7bf..cb7ded9df 100644
--- a/doc/script_commands.txt
+++ b/doc/script_commands.txt
@@ -7779,6 +7779,19 @@ Example:
---------------------------------------
+*swap <variable>,<variable>;
+
+Swap the value of 2 variables. Both sides must be same integer or string type.
+
+Example:
+ .@var1 = 111;
+ .@var2 = 222;
+ swap .@var1, .@var2;
+ mes "var1 = "+ .@var1; // return 222
+ mes "var2 = "+ .@var2; // return 111
+
+---------------------------------------
+
*query_sql("your MySQL query"{, <array variable>{, <array variable>{, ...}}});
*query_logsql("your MySQL query"{, <array variable>{, <array variable>{, ...}}});
diff --git a/src/map/script.c b/src/map/script.c
index 03d1a4ed2..1e19cb85b 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -15736,6 +15736,76 @@ BUILDIN(md5)
return true;
}
+BUILDIN(swap)
+{
+ struct map_session_data *sd = NULL;
+ struct script_data *data1, *data2;
+ const char *varname1, *varname2;
+ int64 uid1, uid2;
+
+ data1 = script_getdata(st,2);
+ data2 = script_getdata(st,3);
+
+ if (!data_isreference(data1) || !data_isreference(data2) || reference_toconstant(data1) || reference_toconstant(data2)) {
+ st->state = END;
+ return true; // avoid print error message twice
+ }
+
+ if (reference_toparam(data1) || reference_toparam(data2)) {
+ ShowError("script:swap: detected parameter type constant.\n");
+ if (reference_toparam(data1))
+ script->reportdata(data1);
+ if (reference_toparam(data2))
+ script->reportdata(data2);
+ st->state = END;
+ return false;
+ }
+
+ varname1 = reference_getname(data1);
+ varname2 = reference_getname(data2);
+
+ if ((is_string_variable(varname1) && !is_string_variable(varname2)) || (!is_string_variable(varname1) && is_string_variable(varname2))) {
+ ShowError("script:swap: both sides must be same integer or string type.\n");
+ script->reportdata(data1);
+ script->reportdata(data2);
+ st->state = END;
+ return false;
+ }
+
+ if (not_server_variable(*varname1) || not_server_variable(*varname2)) {
+ sd = script->rid2sd(st);
+ if (sd == NULL)
+ return true; // avoid print error message twice
+ }
+
+ uid1 = reference_getuid(data1);
+ uid2 = reference_getuid(data2);
+
+ if (is_string_variable(varname1)) {
+ const char *value1, *value2;
+
+ value1 = script_getstr(st,2);
+ value2 = script_getstr(st,3);
+
+ if (strcmpi(value1, value2)) {
+ script->set_reg(st, sd, uid1, varname1, (void*)(value2), script_getref(st,3));
+ script->set_reg(st, sd, uid2, varname2, (void*)(value1), script_getref(st,2));
+ }
+ }
+ else {
+ int value1, value2;
+
+ value1 = script_getnum(st,2);
+ value2 = script_getnum(st,3);
+
+ if (value1 != value2) {
+ script->set_reg(st, sd, uid1, varname1, (void*)h64BPTRSIZE(value2), script_getref(st,3));
+ script->set_reg(st, sd, uid2, varname2, (void*)h64BPTRSIZE(value1), script_getref(st,2));
+ }
+ }
+ return true;
+}
+
// [zBuffer] List of dynamic var commands --->
BUILDIN(setd)
@@ -20489,6 +20559,7 @@ void script_parse_builtin(void) {
BUILDIN_DEF(min, "i*"),
BUILDIN_DEF(max, "i*"),
BUILDIN_DEF(md5,"s"),
+ BUILDIN_DEF(swap,"rr"),
// [zBuffer] List of dynamic var commands --->
BUILDIN_DEF(getd,"s"),
BUILDIN_DEF(setd,"sv"),