From 8d3220163ef4bbdf9a94728f35971bccf6508439 Mon Sep 17 00:00:00 2001 From: gumi Date: Wed, 21 Jun 2017 14:12:48 -0400 Subject: add getdatatype() buildin --- src/map/script.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/map/script.c b/src/map/script.c index 5bdda8512..2f411a1a4 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -16211,6 +16211,60 @@ BUILDIN(isstr) return true; } +enum datatype { + DATATYPE_NIL = 1 << 7, // we don't start at 1, to leave room for primitives + DATATYPE_STR = 1 << 8, + DATATYPE_INT = 1 << 9, + DATATYPE_CONST = 1 << 10, + DATATYPE_PARAM = 1 << 11, + DATATYPE_VAR = 1 << 12, + DATATYPE_LABEL = 1 << 13, +}; + +BUILDIN(getdatatype) { + int type; + + if (script_hasdata(st, 2)) { + struct script_data *data = script_getdata(st, 2); + + if (data_isstring(data)) { + type = DATATYPE_STR; + if (data->type == C_CONSTSTR) { + type |= DATATYPE_CONST; + } + } else if (data_isint(data)) { + type = DATATYPE_INT; + } else if (data_islabel(data)) { + type = DATATYPE_LABEL; + } else if (data_isreference(data)) { + if (reference_toconstant(data)) { + type = DATATYPE_CONST | DATATYPE_INT; + } else if (reference_toparam(data)) { + type = DATATYPE_PARAM | DATATYPE_INT; + } else if (reference_tovariable(data)) { + type = DATATYPE_VAR; + if (is_string_variable(reference_getname(data))) { + type |= DATATYPE_STR; + } else { + type |= DATATYPE_INT; + } + } else { + ShowError("script:getdatatype: Unknown reference type!\n"); + script->reportdata(data); + st->state = END; + return false; + } + } else { + type = data->type; // fallback to primitive type if unknown + } + } else { + type = DATATYPE_NIL; // nothing was passed + } + + script_pushint(st, type); + return true; +} + //======================================================= // chr //------------------------------------------------------- @@ -23652,6 +23706,7 @@ void script_parse_builtin(void) { BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris] BUILDIN_DEF(charat,"si"), BUILDIN_DEF(isstr,"v"), + BUILDIN_DEF(getdatatype, "?"), BUILDIN_DEF(chr,"i"), BUILDIN_DEF(ord,"s"), BUILDIN_DEF(setchar,"ssi"), @@ -24125,6 +24180,15 @@ void script_hardcoded_constants(void) script->set_constant("PERM_DISABLE_EXP", PC_PERM_DISABLE_EXP, false, false); script->set_constant("PERM_DISABLE_SKILL_USAGE", PC_PERM_DISABLE_SKILL_USAGE, false, false); + script->constdb_comment("Data types"); + script->set_constant("DATATYPE_NIL", DATATYPE_NIL, false, false); + script->set_constant("DATATYPE_STR", DATATYPE_STR, false, false); + script->set_constant("DATATYPE_INT", DATATYPE_INT, false, false); + script->set_constant("DATATYPE_CONST", DATATYPE_CONST, false, false); + script->set_constant("DATATYPE_PARAM", DATATYPE_PARAM, false, false); + script->set_constant("DATATYPE_VAR", DATATYPE_VAR, false, false); + script->set_constant("DATATYPE_LABEL", DATATYPE_LABEL, false, false); + script->constdb_comment("Renewal"); #ifdef RENEWAL script->set_constant("RENEWAL", 1, false, false); -- cgit v1.2.3-60-g2f50 From f9ddfcc6e373ab8245ae6fb70737a284c55b3203 Mon Sep 17 00:00:00 2001 From: gumi Date: Wed, 21 Jun 2017 14:21:07 -0400 Subject: add documentation for getdatatype() --- doc/script_commands.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 34089f767..8b3cebbc2 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -8334,6 +8334,30 @@ Example: --------------------------------------- +*getdatatype() + +This command returns the raw type of the given . Unlike +isstr, this command does not evaluate the argument. The returned type +is bitmasked. + +types include: + + DATATYPE_NIL + DATATYPE_STR + DATATYPE_INT + DATATYPE_CONST + DATATYPE_PARAM + DATATYPE_VAR + DATATYPE_LABEL + +Example: + + getdatatype() // DATATYPE_NIL + getdatatype("foo") // DATATYPE_STR + getdatatype(@foo$) // (DATATYPE_VAR | DATATYPE_STR) + +--------------------------------------- + *charisalpha("", ) This function will return true if the character number Position in the given -- cgit v1.2.3-60-g2f50 From 2623a35e451a463f4be14638b37bc294d08094a1 Mon Sep 17 00:00:00 2001 From: Haru Date: Tue, 11 Jul 2017 11:23:15 +0200 Subject: Add tests for getdatatype Signed-off-by: Haru --- npc/dev/test.txt | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/npc/dev/test.txt b/npc/dev/test.txt index b35beb8ed..a6f89f857 100644 --- a/npc/dev/test.txt +++ b/npc/dev/test.txt @@ -9,8 +9,8 @@ //= This file is part of Hercules. //= http://herc.ws - http://github.com/HerculesWS/Hercules //= -//= Copyright (C) 2013-2015 Hercules Dev Team -//= Copyright (C) 2013-2015 Haru +//= Copyright (C) 2013-2017 Hercules Dev Team +//= Copyright (C) 2013-2017 Haru //= //= Hercules is free software: you can redistribute it and/or modify //= it under the terms of the GNU General Public License as published by @@ -742,6 +742,19 @@ function script HerculesSelfTestHelper { callsub(OnCheckStr, "sprintf (positional)", sprintf("'%2$+05d'", 5, 6), "'+0006'"); callsub(OnCheckStr, "sprintf (positional)", sprintf("'%2$s' '%1$c'", "First", "Second"), "'Second' 'F'"); + callsub(OnCheck, "Getdatatype (integer)", getdatatype(5), DATATYPE_INT); + callsub(OnCheck, "Getdatatype (constant string)", getdatatype("foo"), DATATYPE_STR | DATATYPE_CONST); + callsub(OnCheck, "Getdatatype (parameter)", getdatatype(Hp), DATATYPE_INT | DATATYPE_PARAM); + callsub(OnCheck, "Getdatatype (numeric variable)", getdatatype(.@x), DATATYPE_INT | DATATYPE_VAR); + callsub(OnCheck, "Getdatatype (string variable)", getdatatype(.@x$), DATATYPE_STR | DATATYPE_VAR); + callsub(OnCheck, "Getdatatype (label)", getdatatype(OnTestGetdatatype), DATATYPE_LABEL); + //callsub(OnCheck, "Getdatatype (constant)", getdatatype(DATATYPE_CONST), DATATYPE_CONST); // FIXME + callsub(OnCheck, "Getdatatype (returned integer)", getdatatype(callsub(OnTestReturnValue, 5)), DATATYPE_INT); + callsub(OnCheck, "Getdatatype (returned string)", getdatatype(callsub(OnTestReturnValue, "foo")), DATATYPE_STR | DATATYPE_CONST); + callsub(OnCheck, "Getdatatype (getarg default value)", callsub(OnTestGetdatatypeDefault), DATATYPE_INT); + callsub(OnCheck, "Getdatatype (getarg integer value)", callsub(OnTestGetdatatype, 5), DATATYPE_INT); + callsub(OnCheck, "Getdatatype (getarg string)", callsub(OnTestGetdatatype, "foo"), DATATYPE_STR | DATATYPE_CONST); + if (.errors) { debugmes "Script engine self-test [ \033[0;31mFAILED\033[0m ]"; debugmes "**** The test was completed with " + .errors + " errors. ****"; @@ -786,6 +799,12 @@ OnTestScopeArrays: OnTestVarOfAnotherNPC: return getvariableofnpc(.x, getarg(0)); +OnTestGetdatatypeDefault: + return getdatatype(getarg(0, 0)); + +OnTestGetdatatype: + return getdatatype(getarg(0)); + OnReportError: .@msg$ = getarg(0,"Unknown Error"); .@val$ = getarg(1,""); -- cgit v1.2.3-60-g2f50