From 08d8d99ab3a1ca5e8e134fac7e550ae8e1861ec6 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 03:49:16 +0200 Subject: Add crafting type: digging by chance --- api.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 872ce32..00c8f1b 100644 --- a/api.lua +++ b/api.lua @@ -55,6 +55,21 @@ minetest.after(0.01, function() }) end + elseif type(def.drop) == "table" then + for i=1,#def.drop.items do + local itit = def.drop.items[i] + for j=1,#itit.items do + local dstack = ItemStack(itit.items[j]) + if not dstack:is_empty() and dstack:get_name() ~= name then + unified_inventory.register_craft({ + type = "digging_chance", + items = {name}, + output = dstack:get_name(), + width = 0, + }) + end + end + end end end for _, recipes in pairs(unified_inventory.crafts_for.recipe) do @@ -203,6 +218,12 @@ unified_inventory.register_craft_type("digging", { height = 1, }) +unified_inventory.register_craft_type("digging_chance", { + description = "Digging (by chance)", + icon = "default_tool_steelpick.png^[transformFY.png", + width = 1, + height = 1, +}) function unified_inventory.register_page(name, def) unified_inventory.pages[name] = def -- cgit v1.2.3 From 09c731cee064e30b2e15b235a2eb4bb05683122d Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 04:21:02 +0200 Subject: Reduce redundancy in chance-based drops --- api.lua | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 00c8f1b..98884a3 100644 --- a/api.lua +++ b/api.lua @@ -44,6 +44,7 @@ minetest.after(0.01, function() print("Unified Inventory. inventory size: "..unified_inventory.items_list_size) for _, name in ipairs(unified_inventory.items_list) do local def = minetest.registered_items[name] + -- Simple drops if type(def.drop) == "string" then local dstack = ItemStack(def.drop) if not dstack:is_empty() and dstack:get_name() ~= name then @@ -55,21 +56,33 @@ minetest.after(0.01, function() }) end + -- Complex drops elseif type(def.drop) == "table" then + --[[ Extract single items from the table and save them into dedicated table + to register them later, in order to avoid duplicates ]] + local drop_memo = {} for i=1,#def.drop.items do local itit = def.drop.items[i] for j=1,#itit.items do local dstack = ItemStack(itit.items[j]) if not dstack:is_empty() and dstack:get_name() ~= name then - unified_inventory.register_craft({ - type = "digging_chance", - items = {name}, - output = dstack:get_name(), - width = 0, - }) + local dname = dstack:get_name() + if #itit.items == 1 and itit.rarity == 1 then + drop_memo[dname] = "digging" + elseif drop_memo[dname] ~= "digging" then + drop_memo[dname] = "digging_chance" + end end end end + for itemstring, crafttype in pairs(drop_memo) do + unified_inventory.register_craft({ + type = crafttype, + items = {name}, + output = itemstring, + width = 0, + }) + end end end for _, recipes in pairs(unified_inventory.crafts_for.recipe) do -- cgit v1.2.3 From 80f99cffe7e72f3fb9b62cd6731b63d74c52cf74 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 04:38:08 +0200 Subject: Take count into account for complex node drops --- api.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 98884a3..7ccf289 100644 --- a/api.lua +++ b/api.lua @@ -61,16 +61,27 @@ minetest.after(0.01, function() --[[ Extract single items from the table and save them into dedicated table to register them later, in order to avoid duplicates ]] local drop_memo = {} + --[[ Also take count into account ]] + local drop_count = {} for i=1,#def.drop.items do local itit = def.drop.items[i] for j=1,#itit.items do local dstack = ItemStack(itit.items[j]) if not dstack:is_empty() and dstack:get_name() ~= name then local dname = dstack:get_name() + local dcount = dstack:get_count() + if drop_count[dname] == nil then + drop_count[dname] = 0 + end if #itit.items == 1 and itit.rarity == 1 then + if drop_count[dname] == "digging_chance" then + drop_count[dname] = 0 + end drop_memo[dname] = "digging" + drop_count[dname] = drop_count[dname] + dcount elseif drop_memo[dname] ~= "digging" then drop_memo[dname] = "digging_chance" + drop_count[dname] = drop_count[dname] + dcount end end end @@ -79,7 +90,7 @@ minetest.after(0.01, function() unified_inventory.register_craft({ type = crafttype, items = {name}, - output = itemstring, + output = itemstring .. " " .. drop_count[itemstring], width = 0, }) end -- cgit v1.2.3 From 1c5fcd18cac5082a6255bdcc2f0ac91b6bd21fcc Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 04:47:15 +0200 Subject: Properly seperate chance digs from normal digs --- api.lua | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 7ccf289..02aca79 100644 --- a/api.lua +++ b/api.lua @@ -58,11 +58,10 @@ minetest.after(0.01, function() end -- Complex drops elseif type(def.drop) == "table" then - --[[ Extract single items from the table and save them into dedicated table + --[[ Extract single items from the table and save them into dedicated tables to register them later, in order to avoid duplicates ]] - local drop_memo = {} - --[[ Also take count into account ]] - local drop_count = {} + local drop_guaranteed = {} + local drop_maybe = {} for i=1,#def.drop.items do local itit = def.drop.items[i] for j=1,#itit.items do @@ -70,27 +69,33 @@ minetest.after(0.01, function() if not dstack:is_empty() and dstack:get_name() ~= name then local dname = dstack:get_name() local dcount = dstack:get_count() - if drop_count[dname] == nil then - drop_count[dname] = 0 - end if #itit.items == 1 and itit.rarity == 1 then - if drop_count[dname] == "digging_chance" then - drop_count[dname] = 0 + if drop_guaranteed[dname] == nil then + drop_guaranteed[dname] = 0 + end + drop_guaranteed[dname] = drop_guaranteed[dname] + dcount + else + if drop_maybe[dname] == nil then + drop_maybe[dname] = 0 end - drop_memo[dname] = "digging" - drop_count[dname] = drop_count[dname] + dcount - elseif drop_memo[dname] ~= "digging" then - drop_memo[dname] = "digging_chance" - drop_count[dname] = drop_count[dname] + dcount + drop_maybe[dname] = drop_maybe[dname] + dcount end end end end - for itemstring, crafttype in pairs(drop_memo) do + for itemstring, count in pairs(drop_guaranteed) do + unified_inventory.register_craft({ + type = "digging", + items = {name}, + output = itemstring .. " " .. count, + width = 0, + }) + end + for itemstring, count in pairs(drop_maybe) do unified_inventory.register_craft({ - type = crafttype, + type = "digging_chance", items = {name}, - output = itemstring .. " " .. drop_count[itemstring], + output = itemstring .. " " .. count, width = 0, }) end -- cgit v1.2.3 From af56ddf06bf0b3d9ebc02e571bf57aacf5cdb442 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 05:44:36 +0200 Subject: Consider max_items in complex drop analysis --- api.lua | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'api.lua') diff --git a/api.lua b/api.lua index 02aca79..a8d6369 100644 --- a/api.lua +++ b/api.lua @@ -56,25 +56,53 @@ minetest.after(0.01, function() }) end - -- Complex drops + -- Complex drops. Yes, it's really complex! elseif type(def.drop) == "table" then --[[ Extract single items from the table and save them into dedicated tables - to register them later, in order to avoid duplicates ]] + to register them later, in order to avoid duplicates. These tables counts + the total number of guaranteed drops and drops by chance (“maybes”) for each item. + For “maybes”, the final count is the theoretical maximum number of items, not + neccessarily the actual drop count. ]] local drop_guaranteed = {} local drop_maybe = {} + -- This is for catching an obscure corner case: If the top items table has + -- only items with rarity = 1, but max_items is set, then only the first + -- max_items will be part of the drop, any later entries are logically + -- impossible, so this variable is for keeping track of this + local max_items_left = def.drop.max_items + -- For checking whether we still encountered only guaranteed only so far; + -- for the first “maybe” item it will become false which will cause ALL + -- later items to be considered “maybes”. + -- A common idiom is: + -- { max_items 1, { items = { + -- { items={"example:1"}, rarity = 5 }, + -- { items={"example:2"}, rarity = 1 }, }}} + -- example:2 must be considered a “maybe” because max_items is set and it + -- appears after a “maybe” + local max_start = true + -- Let's iterate through the items madness! for i=1,#def.drop.items do + if max_items_left ~= nil and max_items_left <= 0 then break end local itit = def.drop.items[i] for j=1,#itit.items do local dstack = ItemStack(itit.items[j]) if not dstack:is_empty() and dstack:get_name() ~= name then local dname = dstack:get_name() local dcount = dstack:get_count() - if #itit.items == 1 and itit.rarity == 1 then + -- Guaranteed drops AND we are not yet in “maybe mode” + if #itit.items == 1 and itit.rarity == 1 and max_start then if drop_guaranteed[dname] == nil then drop_guaranteed[dname] = 0 end drop_guaranteed[dname] = drop_guaranteed[dname] + dcount + + if max_items_left ~= nil then + max_items_left = max_items_left - 1 + if max_items_left <= 0 then break end + end + -- Drop was a “maybe” else + if max_items_left ~= nil then max_start = false end if drop_maybe[dname] == nil then drop_maybe[dname] = 0 end -- cgit v1.2.3