summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/script_commands.txt24
-rw-r--r--npc/dev/test.txt23
-rw-r--r--src/map/script.c64
3 files changed, 109 insertions, 2 deletions
diff --git a/doc/script_commands.txt b/doc/script_commands.txt
index e0e9646cd..d72d1d4b7 100644
--- a/doc/script_commands.txt
+++ b/doc/script_commands.txt
@@ -8337,6 +8337,30 @@ Example:
---------------------------------------
+*getdatatype(<argument>)
+
+This command returns the raw type of the given <argument>. 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("<string>", <position>)
This function will return true if the character number Position in the given
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,"");
diff --git a/src/map/script.c b/src/map/script.c
index 8184639e6..d85438903 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -16217,6 +16217,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 <int>
//-------------------------------------------------------
@@ -23661,6 +23715,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"),
@@ -24134,6 +24189,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);