From 580a832fdac823a4649f53edd0ba90fb6cd8bc5f Mon Sep 17 00:00:00 2001 From: Zefram Date: Wed, 30 Apr 2014 00:11:10 +0100 Subject: [PATCH] Use appropriate grid shape for each craft type New system of registration of craft types, recording for each a display description and the appropriate grid shape. Recipes of a registered type are shown in the correct grid. Recipes of unregistered craft types are still displayed as before, using the default 3x3 grid. --- api.lua | 43 ++++++++++++++++++++++++++++++++ init.lua | 1 + register.lua | 70 ++++++++++++++++++++++++---------------------------- 3 files changed, 76 insertions(+), 38 deletions(-) diff --git a/api.lua b/api.lua index 8c60fff..18bc875 100644 --- a/api.lua +++ b/api.lua @@ -98,12 +98,55 @@ function unified_inventory.register_craft(options) if itemstack:is_empty() then return end + if options.type == "normal" and options.width == 0 then + options = { type = "shapeless", items = options.items, output = options.output, width = 0 } + end if unified_inventory.crafts_table[itemstack:get_name()] == nil then unified_inventory.crafts_table[itemstack:get_name()] = {} end table.insert(unified_inventory.crafts_table[itemstack:get_name()],options) end +local craft_type_defaults = { + width = 3, + height = 3, + uses_crafting_grid = false, +} +function unified_inventory.canonicalise_craft_type(name, options) + if not options.description then options.description = name end + setmetatable(options, {__index = craft_type_defaults}) + return options +end +function unified_inventory.register_craft_type(name, options) + unified_inventory.registered_craft_types[name] = unified_inventory.canonicalise_craft_type(name, options) +end + +unified_inventory.register_craft_type("normal", { + description = "Crafting", + width = 3, + height = 3, + uses_crafting_grid = true, +}) + +unified_inventory.register_craft_type("shapeless", { + description = "Mixing", + width = 3, + height = 3, + uses_crafting_grid = true, +}) + +unified_inventory.register_craft_type("cooking", { + description = "Cooking", + width = 1, + height = 1, +}) + +unified_inventory.register_craft_type("digging", { + description = "Digging", + width = 1, + height = 1, +}) + function unified_inventory.register_page(name, def) unified_inventory.pages[name] = def end diff --git a/init.lua b/init.lua index dcd8ecf..ecb22da 100644 --- a/init.lua +++ b/init.lua @@ -11,6 +11,7 @@ unified_inventory.current_page = {} unified_inventory.current_searchbox = {} unified_inventory.current_index = {} unified_inventory.current_item = {} +unified_inventory.registered_craft_types = {} unified_inventory.crafts_table = {} unified_inventory.crafts_table_count = 0 unified_inventory.players = {} diff --git a/register.lua b/register.lua index f56ed97..0b2da2b 100644 --- a/register.lua +++ b/register.lua @@ -141,40 +141,14 @@ unified_inventory.register_page("craftguide", { formspec = formspec.."background[0,4.5;8,4;ui_main_inventory.png]" formspec = formspec.."label[0,0;Crafting Guide]" formspec = formspec.."listcolors[#00000000;#00000000]" - formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."craftrecipe;output;6,1;1,1;]" - formspec = formspec.."label[6,3.35;Method:]" local item_name = unified_inventory.current_item[player_name] - local craft = nil + local alternate, alternates, craft, craft_type if item_name then - formspec = formspec.."textarea[0.3,0.6;10,1;;Result: "..minetest.formspec_escape(item_name)..";]" - local alternates = 0 - local alternate = unified_inventory.alternate[player_name] + alternate = unified_inventory.alternate[player_name] local crafts = unified_inventory.crafts_table[item_name] if crafts ~= nil and #crafts > 0 then alternates = #crafts craft = crafts[alternate] - local method = craft.type - local allow_auto_craft = ((method == "normal") or (method == "shapeless")) - if craft.type == "normal" then - method = "crafting" - elseif craft.type == "shapeless" then - method = "shapeless crafting" - elseif craft.type == "alloy" then - method = "alloy cooking" - end - formspec = formspec.."label[6,3.75;"..method.."]" - if allow_auto_craft then - formspec = formspec.."label[6,1.95;Copy to craft grid:]" - .."button[6,2.5;0.6,0.5;craftguide_craft_1;1]" - .."button[6.6,2.5;0.6,0.5;craftguide_craft_10;10]" - .."button[7.2,2.5;0.6,0.5;craftguide_craft_max;All]" - end - end - if alternates > 1 then - formspec = formspec.."label[0,2.6;Recipe " - ..tostring(alternate).." of " - ..tostring(alternates).."]" - .."button[0,3.15;2,1;alternate;Alternate]" end end @@ -183,29 +157,35 @@ unified_inventory.register_page("craftguide", { name = player_name.."craftrecipe" }) - -- fake buttons just to make 3x3 grid - for y = 1, 3 do - for x = 1, 3 do - formspec = formspec.."image_button[" - ..(1.0 + x)..","..(0.0 + y)..";1.1,1.1;ui_blank_image.png;;]" - end - end - if not craft then craftinv:set_stack("output", 1, nil) return {formspec=formspec} end + craft_type = unified_inventory.registered_craft_types[craft.type] or unified_inventory.canonicalise_craft_type(craft.type, {}) + formspec = formspec.."label[6,3.35;Method:]" + formspec = formspec.."label[6,3.75;"..minetest.formspec_escape(craft_type.description).."]" + craftinv:set_stack("output", 1, craft.output) + formspec = formspec.."textarea[0.3,0.6;10,1;;Result: "..minetest.formspec_escape(item_name)..";]" + formspec = formspec.."list[detached:"..minetest.formspec_escape(player_name).."craftrecipe;output;6,1;1,1;]" + + -- fake buttons just to make grid + for y = 1, craft_type.height do + for x = 1, craft_type.width do + formspec = formspec.."image_button[" + ..(1.0 + x)..","..(0.0 + y)..";1.1,1.1;ui_blank_image.png;;]" + end + end local width = craft.width if width == 0 then -- Shapeless recipe - width = 3 + width = craft_type.width end local i = 1 - for y = 1, 3 do + for y = 1, craft_type.height do for x = 1, width do local item = craft.items[i] if item then @@ -227,6 +207,20 @@ unified_inventory.register_page("craftguide", { i = i + 1 end end + + if craft_type.uses_crafting_grid then + formspec = formspec.."label[6,1.95;Copy to craft grid:]" + .."button[6,2.5;0.6,0.5;craftguide_craft_1;1]" + .."button[6.6,2.5;0.6,0.5;craftguide_craft_10;10]" + .."button[7.2,2.5;0.6,0.5;craftguide_craft_max;All]" + end + + if alternates > 1 then + formspec = formspec.."label[0,2.6;Recipe " + ..tostring(alternate).." of " + ..tostring(alternates).."]" + .."button[0,3.15;2,1;alternate;Alternate]" + end return {formspec=formspec} end, })