From a5d2bdfdf8dab45a70bb7adebb17f66b8959f49e Mon Sep 17 00:00:00 2001 From: gumi Date: Sat, 12 Sep 2020 13:55:37 -0300 Subject: add a script command to get craft recipe ingredients fixup for getcraftrecipe --- src/emap/init.c | 1 + src/emap/script_buildins.c | 89 +++++++++++++++++++++++++++++++++++++++------- src/emap/script_buildins.h | 1 + 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/emap/init.c b/src/emap/init.c index c321823..40e7c47 100644 --- a/src/emap/init.c +++ b/src/emap/init.c @@ -147,6 +147,7 @@ HPExport void plugin_init (void) addScriptCommand("getcraftslotid", "ii", getCraftSlotId); addScriptCommand("getcraftslotamount", "ii", getCraftSlotAmount); addScriptCommand("validatecraft", "i", validateCraft); + addScriptCommand("getcraftrecipe", "iirr", getCraftRecipe); addScriptCommand("getq", "i", getq); addScriptCommand("getq1", "i", getq); addScriptCommand("getq2", "i", getq2); diff --git a/src/emap/script_buildins.c b/src/emap/script_buildins.c index 1ada441..b189f0a 100644 --- a/src/emap/script_buildins.c +++ b/src/emap/script_buildins.c @@ -34,6 +34,7 @@ #include "emap/clif.h" #include "emap/craft.h" +#include "emap/craftconf.h" #include "emap/lang.h" #include "emap/map.h" #include "emap/hashtable.h" @@ -1801,6 +1802,70 @@ BUILDIN(findCraftEntry) return true; } +BUILDIN(getCraftRecipe) +{ + // arg 0: recipe id + // arg 1: inventory id + // arg 2: qty array + // arg 3: item array + // returns size + + struct script_data *data4 = script_getdata(st, 4); + struct script_data *data5 = script_getdata(st, 5); + + if (!data_isreference(data4) || reference_toconstant(data4) + || !data_isreference(data5) || reference_toconstant(data5)) { + ShowError("script:getcraftrecipe: 3rd and 4th arguments must be variables\n"); + st->state = END; + return false; + } + + int recipe_id = script_getnum(st, 2); + struct craft_db_entry *entry = idb_get(craftconf_db, recipe_id); + + if (!entry) { + // recipe does not exist + script_pushint(st, -1); + return true; + } + + int inventory_id = script_getnum(st, 3); + int inv_count = VECTOR_LENGTH(entry->inventories); + + if (inventory_id >= inv_count) { + // inventory does not exist + script_pushint(st, -2); + return true; + } + + struct craft_db_inventory *entry_inventory = &VECTOR_INDEX(entry->inventories, inventory_id); + + int32 id4 = reference_getid(data4); + uint32 start4 = reference_getindex(data4); + const char *name4 = reference_getname(data4); + struct reg_db *ref4 = reference_getref(data4); + int32 id5 = reference_getid(data5); + uint32 start5 = reference_getindex(data5); + const char *name5 = reference_getname(data5); + struct reg_db *ref5 = reference_getref(data5); + + size_t size = 0; + + for (int i = 0; i < craft_inventory_size; i++) { + struct item_pair *entryItem = &entry_inventory->items[i]; + + uint32 index4 = start4 + size; + uint32 index5 = start5 + size; + + script->set_reg(st, NULL, reference_uid(id4, index4), name4, (const void *)h64BPTRSIZE(entryItem->amount), ref4); + script->set_reg(st, NULL, reference_uid(id5, index5), name5, (const void *)h64BPTRSIZE(entryItem->index), ref5); + size++; + } + + script_pushint(st, size); + return true; +} + BUILDIN(useCraft) { getSD() @@ -1879,9 +1944,9 @@ BUILDIN(getInvIndexLink) } /*========================================== - * Shows an emoticon on top of the player/npc - * emotion emotion#, , - *------------------------------------------*/ +* Shows an emoticon on top of the player/npc +* emotion emotion#, , +*------------------------------------------*/ //Optional second parameter added by [Skotlex] BUILDIN(emotion) { @@ -2450,15 +2515,15 @@ BUILDIN(getNpcSubtype) } /*========================================== - * return the battle stats of a structure - * Supported values are most of UDT_* ones - * Not all values are supported; Only those without - * another assessor are (eg. MaxHP vs UDT_MAXHP) - * - * From 1 onwards: Str, Agi, Vit, Int, Dex, Luck - * From 7 onwards: AtkMin, AtkMax, Def, MDef, Hit, Flee - * From 13 onwards: MAtkMin, MatkMax - *------------------------------------------*/ +* return the battle stats of a structure +* Supported values are most of UDT_* ones +* Not all values are supported; Only those without +* another assessor are (eg. MaxHP vs UDT_MAXHP) +* +* From 1 onwards: Str, Agi, Vit, Int, Dex, Luck +* From 7 onwards: AtkMin, AtkMax, Def, MDef, Hit, Flee +* From 13 onwards: MAtkMin, MatkMax +*------------------------------------------*/ BUILDIN(readBattleParam) { struct map_session_data *sd; diff --git a/src/emap/script_buildins.h b/src/emap/script_buildins.h index 8426f2a..d17c1f6 100644 --- a/src/emap/script_buildins.h +++ b/src/emap/script_buildins.h @@ -103,6 +103,7 @@ BUILDIN(setItemOptionByIndex); BUILDIN(isInstance); BUILDIN(readBattleParam); BUILDIN(getNpcSubtype); +BUILDIN(getCraftRecipe); // TMW2 Build Ins BUILDIN(getguildinfo); -- cgit v1.2.3-60-g2f50