From 9d582035fc69ffd706077add90e9276550b49565 Mon Sep 17 00:00:00 2001 From: kisuka Date: Tue, 23 Oct 2018 16:52:46 -0700 Subject: Added countnameditem script command. Signed-off-by: Haru --- src/map/script.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'src') diff --git a/src/map/script.c b/src/map/script.c index e7a2fdb14..e104cd891 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -7713,6 +7713,65 @@ static BUILDIN(countitem2) return true; } +/*========================================== + * countnameditem(item ID, { }) + * returns number of named items. + *------------------------------------------*/ +static BUILDIN(countnameditem) +{ + int count = 0; + struct item_data* id = NULL; + struct map_session_data *sd; + + if (script_hasdata(st, 3)) { + if (script_isstringtype(st, 3)) { + // Character name was given + sd = script->nick2sd(st, script_getstr(st, 3)); + } else { + // Character ID was given + sd = script->charid2sd(st, script_getnum(st, 3)); + } + } else { + // Use RID by default if no name was provided + sd = script->rid2sd(st); + } + + // Player not attached + if (sd == NULL) { + return true; + } + + if (script_isstringtype(st, 2)) { + // Get item from DB via item name + id = itemdb->search_name(script_getstr(st, 2)); + } else { + // Get item from DB via item ID + id = itemdb->exists(script_getnum(st, 2)); + } + + if (id == NULL) { + ShowError("buildin_countnameditem: Invalid item '%s'.\n", script_getstr(st, 2)); // returns string, regardless of what it was + script_pushint(st, 0); + return false; + } + + for (int i = 0; i < MAX_INVENTORY; i++) { + if (sd->status.inventory[i].nameid > 0 && + sd->inventory_data[i] != NULL && + sd->status.inventory[i].amount > 0 && + sd->status.inventory[i].nameid == id->nameid && + sd->status.inventory[i].card[0] == CARD0_CREATE && + sd->status.inventory[i].card[2] == sd->status.char_id && + sd->status.inventory[i].card[3] == sd->status.char_id >> 16) + { + count += sd->status.inventory[i].amount; + } + } + + script_pushint(st, count); + return true; +} + /*========================================== * Check if item with this amount can fit in inventory * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY) @@ -25038,6 +25097,7 @@ static void script_parse_builtin(void) BUILDIN_DEF(rand,"i?"), BUILDIN_DEF(countitem,"v"), BUILDIN_DEF(countitem2,"viiiiiii"), + BUILDIN_DEF(countnameditem,"v?"), BUILDIN_DEF(checkweight,"vi*"), BUILDIN_DEF(checkweight2,"rr"), BUILDIN_DEF(readparam,"i?"), -- cgit v1.2.3-60-g2f50