summaryrefslogtreecommitdiff
path: root/src/map
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2018-11-13 15:42:20 +0100
committerGitHub <noreply@github.com>2018-11-13 15:42:20 +0100
commite606053634bd3dcfad567ed0065c2d3062c4222b (patch)
tree963f32f530b1c6b1701702b69143cecafd684ec6 /src/map
parentbeacddf2e4b9393f85d92508f4639a4118350ac9 (diff)
parentb0a321c309521a6b0bc35c8f9c58ce57f3f38cbd (diff)
downloadhercules-e606053634bd3dcfad567ed0065c2d3062c4222b.tar.gz
hercules-e606053634bd3dcfad567ed0065c2d3062c4222b.tar.bz2
hercules-e606053634bd3dcfad567ed0065c2d3062c4222b.tar.xz
hercules-e606053634bd3dcfad567ed0065c2d3062c4222b.zip
Merge pull request #2304 from Helianthella/data-to-string
add data_to_string()
Diffstat (limited to 'src/map')
-rw-r--r--src/map/script.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/map/script.c b/src/map/script.c
index 8aceb0532..fc1ece663 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -16717,6 +16717,63 @@ static BUILDIN(getdatatype)
return true;
}
+static BUILDIN(data_to_string)
+{
+ if (script_hasdata(st, 2)) {
+ struct script_data *data = script_getdata(st, 2);
+
+ if (data_isstring(data)) {
+ script_pushcopy(st, 2);
+ } else if (data_isint(data)) {
+ char *str = NULL;
+
+ CREATE(str, char, 20);
+ safesnprintf(str, 20, "%"PRId64"", data->u.num);
+ script_pushstr(st, str);
+ } else if (data_islabel(data)) {
+ const char *str = "";
+
+ // XXX: because all we have is the label pos we can't be sure which
+ // one is the correct label if more than one has the same pos.
+ // We might want to store both the pos and str_data index in
+ // data->u.num, similar to how C_NAME stores both the array
+ // index and str_data index in u.num with bitmasking. This
+ // would also avoid the awkward for() loops as we could
+ // directly access the string with script->get_str().
+
+ if (st->oid) {
+ struct npc_data *nd = map->id2nd(st->oid);
+
+ for (int i = 0; i < nd->u.scr.label_list_num; ++i) {
+ if (nd->u.scr.label_list[i].pos == data->u.num) {
+ str = nd->u.scr.label_list[i].name;
+ break;
+ }
+ }
+ } else {
+ for (int i = LABEL_START; script->str_data[i].next != 0; i = script->str_data[i].next) {
+ if (script->str_data[i].label == data->u.num) {
+ str = script->get_str(i);
+ break;
+ }
+ }
+ }
+
+ script_pushconststr(st, str);
+ } else if (data_isreference(data)) {
+ script_pushstrcopy(st, reference_getname(data));
+ } else {
+ ShowWarning("script:data_to_string: unknown data type!\n");
+ script->reportdata(data);
+ script_pushconststr(st, "");
+ }
+ } else {
+ script_pushconststr(st, ""); // NIL
+ }
+
+ return true;
+}
+
//=======================================================
// chr <int>
//-------------------------------------------------------
@@ -25191,6 +25248,8 @@ static void script_parse_builtin(void)
BUILDIN_DEF(charat,"si"),
BUILDIN_DEF(isstr,"v"),
BUILDIN_DEF(getdatatype, "?"),
+ BUILDIN_DEF(data_to_string, "?"),
+ BUILDIN_DEF2(getd, "string_to_data", "?"),
BUILDIN_DEF(chr,"i"),
BUILDIN_DEF(ord,"s"),
BUILDIN_DEF(setchar,"ssi"),