From 456e9e5318223ef96ca92a3979158f6c202e630c Mon Sep 17 00:00:00 2001 From: Emistry Haoyan Date: Tue, 9 Apr 2019 23:23:33 +0800 Subject: [PATCH] Add *setfavoriteitem & *autofavoriteitem script command. - set an item as favorite item or not based on itemID or inventory index. - if an item is set to favorite item, it will be moved into favorite tab, else move out from favorite tab. - only non-equipped item can adjust the favorite item state. --- doc/script_commands.txt | 25 ++++++++++++++++++ src/map/itemdb.h | 3 ++- src/map/pc.c | 7 +++++ src/map/script.c | 57 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/doc/script_commands.txt b/doc/script_commands.txt index b53dda2f62b..4e71c7e3e66 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -3183,6 +3183,31 @@ runs of getcartinventorylist(). --------------------------------------- +*setfavoriteitem(, ) + +This function will set an item in inventory as favorite or not. +If its favorite item, it will be moved to favorite tab, else move out from favorite tab. +Preferable to use inventory index (idx) since it provide better accuracy to determine which item +in case player has multiple items with same item_id. + +Valid Parameters: + - item_id or inventory index, refer *getinventorylist() + - true/false (true = favorite item, false = otherwise) + +--------------------------------------- + +*autofavoriteitem(, ) + +This function will auto set an item as favorite when is obtained. +If its favorite item, it will be auto moved to favorite tab, else move out from favorite tab. +This setting affect not only attached player, but also everyone player globally. + +Valid Parameters: + - item ID + - true/false (true = favorite item, false = otherwise) + +--------------------------------------- + *cardscnt() This function will return the number of cards inserted into the weapon diff --git a/src/map/itemdb.h b/src/map/itemdb.h index 31578799383..ba6ae6f9122 100644 --- a/src/map/itemdb.h +++ b/src/map/itemdb.h @@ -520,7 +520,8 @@ struct item_data { unsigned no_refine : 1; // [celest] unsigned delay_consume : 1; ///< Signifies items that are not consumed immediately upon double-click [Skotlex] unsigned trade_restriction : 9; ///< Item trade restrictions mask (@see enum ItemTradeRestrictions) - unsigned autoequip: 1; + unsigned autoequip : 1; + unsigned auto_favorite : 1; unsigned buyingstore : 1; unsigned bindonequip : 1; unsigned keepafteruse : 1; diff --git a/src/map/pc.c b/src/map/pc.c index 140cf7ac145..79ffe225da0 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -4778,6 +4778,13 @@ static int pc_additem(struct map_session_data *sd, const struct item *item_data, sd->weight += w; clif->updatestatus(sd,SP_WEIGHT); + + // auto-favorite + if (data->flag.auto_favorite > 0) { + sd->status.inventory[i].favorite = 1; + clif->favorite_item(sd, i); + } + //Auto-equip if(data->flag.autoequip) pc->equipitem(sd, i, data->equip); diff --git a/src/map/script.c b/src/map/script.c index fe8638ac32b..ddbce9e67fc 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -24559,6 +24559,61 @@ static BUILDIN(getcalendartime) return true; } +static BUILDIN(setfavoriteitem) +{ + struct map_session_data *sd = script_rid2sd(st); + int idx = script_getnum(st, 2); + int value = script_getnum(st, 3); + int nameid = script_getnum(st, 2); + struct item_data *id = itemdb->exists(nameid); + + if (sd == NULL) { + ShowError("buildin_setfavoriteitem: No player attached.\n"); + return false; + } + + // search for idx if item id given + if (nameid >= ITEMID_RED_POTION || nameid >= sd->status.inventorySize) { + if (id == NULL) { + ShowError("buildin_setfavoriteitem: Invalid item %d.\n", nameid); + return false; + } + idx = pc->search_inventory(sd, nameid); + if (idx == INDEX_NOT_FOUND) { + ShowError("buildin_setfavoriteitem: Item %d not found.\n", nameid); + return false; + } + } + + if (idx < 0 || idx >= sd->status.inventorySize) { + ShowError("buildin_setfavouriteitem: Invalid inventory index %d (min: %d, max: %d).\n", idx, 0, (sd->status.inventorySize - 1)); + return false; + } else if (sd->inventory_data[idx] == NULL || sd->inventory_data[idx]->nameid <= 0) { + ShowWarning("buildin_setfavouriteitem: Current inventory index %d has no data.\n", idx); + return false; + } else if (sd->status.inventory[idx].equip == 0) { + sd->status.inventory[idx].favorite = cap_value(value, 0, 1); + clif->favorite_item(sd, idx); + } + + return true; +} + +static BUILDIN(autofavoriteitem) +{ + int nameid = script_getnum(st, 2); + int flag = script_getnum(st, 3); + struct item_data *item_data; + + if ((item_data = itemdb->exists(nameid)) == NULL) { + ShowError("buildin_autofavoriteitem: Invalid item '%d'.\n", nameid); + return false; + } + + item_data->flag.auto_favorite = cap_value(flag, 0, 1); + return true; +} + /** place holder for the translation macro **/ static BUILDIN(_) { @@ -25902,6 +25957,8 @@ static void script_parse_builtin(void) BUILDIN_DEF(expandInventoryResult, "i"), BUILDIN_DEF(expandInventory, "i"), BUILDIN_DEF(getInventorySize, ""), + BUILDIN_DEF(setfavoriteitem, "ii"), + BUILDIN_DEF(autofavoriteitem, "ii"), }; int i, len = ARRAYLENGTH(BUILDIN); RECREATE(script->buildin, char *, script->buildin_count + len); // Pre-alloc to speed up