diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 675b967433e..ea7a8ebf061 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -5625,6 +5625,24 @@ Examples: --------------------------------------- +*equipidx() + +The equipidx function will attempt to equip the item at the given inventory +inventory index. This feature is only really useful when multiple instances +of an Item ID exist in the player's inventory. + +Returns 1 if the item was successfully equipped. +Returns 0 if the item failed to equip. + +See getinventorylist() for an explanation on retrieving an inventory index. + +Examples: + +//This will attempt to equip the item at inventory index 45. + equipidx(45); + +--------------------------------------- + *buyingstore() Invokes buying store preparation window like the skill 'Open Buying diff --git a/src/map/script.c b/src/map/script.c index f25e945fa7e..0ae0b5bdf9c 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -16522,6 +16522,48 @@ static BUILDIN(autoequip) return true; } +/** + * equipidx() + */ +static BUILDIN(equipidx) +{ + struct map_session_data *sd = script->rid2sd(st); + + if (sd == NULL) { + script_pushint(st, 0); + return true; + } + + int i = script_getnum(st, 2); + if (i < 0 || i >= sd->status.inventorySize) { + ShowError("buildin_equipidx: Index (%d) should be from 0-%d.\n", i, sd->status.inventorySize - 1); + script_pushint(st, 0); + return false; + } + + if (sd->status.inventory[i].equip != 0) { // item already equipped, run silently + script_pushint(st, 1); + return true; + } + + int nameid = sd->status.inventory[i].nameid; + struct item_data *item_data = itemdb->exists(nameid); + if (item_data == NULL) { + ShowError("buildin_equipidx: Invalid Item ID (%d).\n", nameid); + script_pushint(st, 0); + return false; + } + + if (pc->equipitem(sd, i, item_data->equip) == 0) { + ShowWarning("buildin_equipidx: Item ID (%d) at index (%d) cannot be equipped.\n", nameid, i); + script_pushint(st, 0); + return true; + } + + script_pushint(st, 1); + return true; +} + /*======================================================= * Equip2 * equip2 ,,,,,,; @@ -25498,6 +25540,7 @@ static void script_parse_builtin(void) BUILDIN_DEF(equip,"i"), BUILDIN_DEF(autoequip,"ii"), BUILDIN_DEF(equip2,"iiiiiii"), + BUILDIN_DEF(equipidx, "i?"), BUILDIN_DEF(setbattleflag,"si"), BUILDIN_DEF(getbattleflag,"s"), BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus