From 08d8d99ab3a1ca5e8e134fac7e550ae8e1861ec6 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 03:49:16 +0200 Subject: [PATCH 1/5] Add crafting type: digging by chance --- api.lua | 21 +++++++++++++++++++++ locale/de.txt | 3 +++ locale/template.txt | 3 +++ textures/ui_craftgrid_icon.png | Bin 488 -> 896 bytes 4 files changed, 27 insertions(+) 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 diff --git a/locale/de.txt b/locale/de.txt index d678b68..5b28630 100644 --- a/locale/de.txt +++ b/locale/de.txt @@ -1,5 +1,8 @@ # Translation mostly by Xanthin +### api.lua ### +Digging (by chance) = Graben (durch Zufall) + ### bags.lua ### Bags = Rucksaecke Bag 1 = Rucksack 1 diff --git a/locale/template.txt b/locale/template.txt index ae382e3..4268257 100644 --- a/locale/template.txt +++ b/locale/template.txt @@ -1,5 +1,8 @@ # Translation by +### api.lua ### +Digging (by chance) = + # Template ### bags.lua ### Bags = diff --git a/textures/ui_craftgrid_icon.png b/textures/ui_craftgrid_icon.png index b6831a9c1d1a8e8cb292f4566badf19db2fc80ee..67313279d302cf78dbfb9d050b95a8d4fd9b89ab 100644 GIT binary patch literal 896 zcmeAS@N?(olHy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}oo>D9yob@h zG#5YL(|gEx@1sRrwUuolb5!=(I4k-U#m{rRb4AXEi9?}*0Yo&Ia|4N;7LmU{@oc=j zKMklVpsV@W>FL*nLjUePd%k(Q?6qvp$$PdHov_;b@cyZHv1(HM3=SXvFH3s Date: Sun, 7 Aug 2016 04:21:02 +0200 Subject: [PATCH 2/5] Reduce redundancy in chance-based drops --- api.lua | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) 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 From 80f99cffe7e72f3fb9b62cd6731b63d74c52cf74 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 04:38:08 +0200 Subject: [PATCH 3/5] Take count into account for complex node drops --- api.lua | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 From 1c5fcd18cac5082a6255bdcc2f0ac91b6bd21fcc Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 04:47:15 +0200 Subject: [PATCH 4/5] Properly seperate chance digs from normal digs --- api.lua | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) 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_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_guaranteed[dname] = drop_guaranteed[dname] + dcount + else + if drop_maybe[dname] == nil then + drop_maybe[dname] = 0 + end + 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 = crafttype, + type = "digging", items = {name}, - output = itemstring .. " " .. drop_count[itemstring], + output = itemstring .. " " .. count, + width = 0, + }) + end + for itemstring, count in pairs(drop_maybe) do + unified_inventory.register_craft({ + type = "digging_chance", + items = {name}, + output = itemstring .. " " .. count, width = 0, }) end From af56ddf06bf0b3d9ebc02e571bf57aacf5cdb442 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sun, 7 Aug 2016 05:44:36 +0200 Subject: [PATCH 5/5] Consider max_items in complex drop analysis --- api.lua | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) 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