From f8c0a1ce155a44636ce6602f59fd38a57ae3ade5 Mon Sep 17 00:00:00 2001 From: jp Date: Sat, 28 Nov 2015 16:22:41 +0100 Subject: [PATCH] Add Crafting Guide in the Work Table \o/ --- worktable.lua | 253 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 235 insertions(+), 18 deletions(-) diff --git a/worktable.lua b/worktable.lua index 4f743db..30d4264 100644 --- a/worktable.lua +++ b/worktable.lua @@ -1,6 +1,6 @@ local worktable = {} screwdriver = screwdriver or {} -local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots..default.get_hotbar_bg(0,3.25) +local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots local nodes = { -- Nodes allowed to be cut. Mod name = {node name}. ["default"] = {"wood", "junglewood", "pine_wood", "acacia_wood", @@ -34,31 +34,100 @@ local def = { -- Nodebox name, yield, definition. {"innerstair", 1, {{-.5,-.5,-.5,.5,0,.5},{-.5,0,0,.5,.5,.5},{-.5,0,-.5,0,.5,0}}} } -function worktable.crafting() - return "size[8,7;]"..xbg.. +function worktable.craft_output_recipe(pos, start_i, pagenum, stackname) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + pagenum = math.floor(pagenum) + local pagemax = math.floor((meta:get_int("inventory_size")-1) / (8*4) + 1) or 0 + + local formspec = "size[8,8;]"..xbg.. + "list[context;inv_items_list;0,1;8,4;"..tostring(start_i).."]".. + "list[context;item_craft_input;2.5,6.3;1,1;]".. + "list[context;craft_output_recipe;4.5,5.3;3,3;]".. + "image[3.5,6.3;1,1;gui_furnace_arrow_bg.png^[transformR90]".. + "tablecolumns[color;text;color;text]".. + "tableoptions[background=#00000000;highlight=#00000000;border=false]".. + "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(pagenum)..",#FFFFFF,/ "..tostring(pagemax).."]".. + "button[5.5,0;0.8,1;prev;<<]".. + "button[7.2,0;0.8,1;next;>>]".. + "button[4.35,0.18;0.6,0.6;search;?]".. + "button[4.8,0.18;0.6,0.6;clearfilter;X]".. + "button[0,0;1.5,1;backcraft;< Back]".. + "button[0.7,6.35;1.5,1;trash;Clear]".. + "tooltip[search;Search]".. + "tooltip[clearfilter;Reset]".. + "label[2.5,5.8;Input]" .. + "box[0.1,7.5;4,0.45;#555555]".. + "field[1.8,0.32;3,1;filter;;]" + + if stackname then + meta:set_string("item", stackname) + formspec = formspec.."label[0.15,7.5;"..meta:get_string("item"):sub(1,30).."]" + end + + inv:set_size("craft_output_recipe", 3*3) + meta:set_int("start_i", tostring(start_i)) + meta:set_string("formspec", formspec) +end + +function worktable.craftguide_update(pos, filter) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local inv_items_list = {} + + for name, def in pairs(minetest.registered_items) do + if (not def.groups.not_in_creative_inventory or + def.groups.not_in_creative_inventory == 0) and + def.description and def.description ~= "" and + def.name ~= "unknown" then + + if filter and def.name:find(filter) then + inv_items_list[#inv_items_list+1] = name + elseif filter == "all" then + inv_items_list[#inv_items_list+1] = name + end + end + end + table.sort(inv_items_list) + + inv:set_size("inv_items_list", #inv_items_list) + inv:set_list("inv_items_list", inv_items_list) + meta:set_int("inventory_size", #inv_items_list) +end + +function worktable.crafting(pos) + local meta = minetest.get_meta(pos) + local formspec = "size[8,7;]"..xbg.. + default.get_hotbar_bg(0,3.3).. "list[current_player;main;0,3.3;8,4;]".. "image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]".. + "button[0,0;1.5,1;back;< Back]".. + "button[0,1;1.5,1;craft_output_recipe;Guide]".. "list[current_player;craft;2,0;3,3;]".. "list[current_player;craftpreview;6,1;1,1;]".. "listring[current_player;main]".. "listring[current_player;craft]" + + meta:set_string("formspec", formspec) end function worktable.storage(pos) - local inv = minetest.get_meta(pos):get_inventory() - inv:set_size("storage", 8*2) - return "size[8,7]"..xbg.. - "list[context;storage;0,0;8,2;]".. + local meta = minetest.get_meta(pos) + local formspec = "size[8,7]"..xbg.. + default.get_hotbar_bg(0,3.25).. + "list[context;storage;0,1;8,2;]".. "list[current_player;main;0,3.25;8,4;]".. "listring[context;storage]".. - "listring[current_player;main]" + "listring[current_player;main]".. + "button[0,0;1.5,1;back;< Back]" + + meta:set_string("formspec", formspec) end -function worktable.construct(pos) +function worktable.main(pos) local meta = minetest.get_meta(pos) - local inv = meta:get_inventory() - local formspec = "size[8,7;]"..xbg.. + default.get_hotbar_bg(0,3.25).. "label[0.9,1.23;Cut]".. "label[0.9,2.23;Repair]".. "box[-0.05,1;2.05,0.9;#555555]".. @@ -74,24 +143,72 @@ function worktable.construct(pos) "list[current_player;main;0,3.25;8,4;]".. "button[0,0;2,1;craft;Crafting]".. "button[2,0;2,1;storage;Storage]" + meta:set_string("formspec", formspec) + return formspec +end + +function worktable.construct(pos) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() inv:set_size("forms", 4*3) inv:set_size("input", 1) inv:set_size("tool", 1) inv:set_size("hammer", 1) + inv:set_size("storage", 8*2) + inv:set_size("item_craft_input", 1) - meta:set_string("formspec", formspec) + meta:set_int("start_i", 0) meta:set_string("infotext", "Work Table") + worktable.main(pos) + worktable.craftguide_update(pos, "all") end function worktable.fields(pos, _, fields, sender) - local player = sender:get_player_name() - local inv = minetest.get_meta(pos):get_inventory() + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + local inputstack = inv:get_stack("item_craft_input", 1):get_name() + local start_i = meta:get_int("start_i") + start_i = tonumber(start_i) or 0 if fields.storage then - minetest.show_formspec(player, "", worktable.storage(pos)) + worktable.storage(pos) elseif fields.craft then - minetest.show_formspec(player, "", worktable.crafting(pos)) + worktable.crafting(pos) + elseif fields.back then + worktable.main(pos) + elseif fields.backcraft then + worktable.crafting(pos) + elseif fields.craft_output_recipe then + worktable.craft_output_recipe(pos, 0, 1) + elseif fields.trash then + inv:set_list("item_craft_input", {}) + inv:set_list("craft_output_recipe", {}) + worktable.craft_output_recipe(pos, meta:get_string("start_i"), meta:get_int("start_i") / (8*4) + 1, nil) + elseif fields.search then + worktable.craftguide_update(pos, fields.filter) + worktable.craft_output_recipe(pos, 0, 1, nil) + elseif fields.clearfilter then + worktable.craftguide_update(pos, "all") + worktable.craft_output_recipe(pos, 0, 1, nil) + end + + if fields.prev or fields.next then + if fields.prev then + start_i = start_i - 8*4 + elseif fields.next then + start_i = start_i + 8*4 + end + + if start_i < 0 then + start_i = start_i + 8*4 + elseif start_i >= meta:get_int("inventory_size") then + start_i = start_i - 8*4 + elseif start_i < 0 or start_i >= meta:get_int("inventory_size") then + start_i = 0 + end + + worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, inputstack) end end @@ -141,12 +258,112 @@ function worktable.take(pos, listname, _, stack, player) return -1 end return 0 + elseif listname == "inv_items_list" or listname == "item_craft_input" or + listname == "craft_output_recipe" then + return 0 end return stack:get_count() end -function worktable.move(_, from_list, _, to_list, _, count, _) - if from_list == "storage" and to_list == "storage" then return count end +function worktable.move(pos, from_list, from_index, to_list, to_index, count, _) + local inv = minetest.get_meta(pos):get_inventory() + local stackname = inv:get_stack(from_list, from_index):get_name() + local meta = minetest.get_meta(pos) + local craft = {} + + if from_list == "storage" and to_list == "storage" then + return count + end + if minetest.get_craft_recipe(stackname).items and + inv:is_empty("item_craft_input") and from_list == "inv_items_list" and + to_list == "item_craft_input" then + + local stack_items = minetest.get_craft_recipe(stackname).items + local stack_output = minetest.get_craft_recipe(stackname).output + local stack_width = minetest.get_craft_recipe(stackname).width + local stack_type = minetest.get_craft_recipe(stackname).type + local stack_count = stack_output:match("%s(%d+)") + --print(dump(minetest.get_craft_recipe(stackname))) + + for k, def in pairs(stack_items) do + craft[#craft+1] = def + end + + for i = 1, 9 do + if craft[i] and craft[i]:sub(1, 6) == "group:" then + if craft[i] == "group:liquid" then + craft[i] = "default:water_source" + elseif craft[i] == "group:vessel" then + craft[i] = "vessels:glass_bottle" + elseif craft[i] == "group:wool" then + craft[i] = "wool:white" + elseif craft[i]:find("group:dye") then + craft[i] = "dye:white" -- TODO: display respective dye color + else + craft[i] = "default:"..craft[i]:sub(7, string.len(craft[i])) + end + end + end + + if stack_width == 0 or stack_width == 1 then + if stack_count then + inv:add_item("item_craft_input", stackname.." "..stack_count-1) + end + if #stack_items == 1 then + inv:set_stack("craft_output_recipe", 5, craft[1]) + elseif #stack_items == 2 then + inv:set_stack("craft_output_recipe", 5, craft[1]) + inv:set_stack("craft_output_recipe", 8, craft[2]) + else + inv:set_stack("craft_output_recipe", 2, craft[1]) + inv:set_stack("craft_output_recipe", 5, craft[2]) + inv:set_stack("craft_output_recipe", 8, craft[3]) + end + elseif stack_width == 2 then + if stack_count then + inv:add_item("item_craft_input", stackname.." "..stack_count-1) + end + inv:set_stack("craft_output_recipe", 1, craft[1]) + inv:set_stack("craft_output_recipe", 2, craft[2]) + inv:set_stack("craft_output_recipe", 4, craft[3]) + inv:set_stack("craft_output_recipe", 5, craft[4]) + inv:set_stack("craft_output_recipe", 7, craft[5]) + inv:set_stack("craft_output_recipe", 8, craft[6]) + elseif stack_width == 3 then + if stack_count then + inv:add_item("item_craft_input", stackname.." "..stack_count-1) + end + for k, def in pairs(stack_items) do + for i = 1, 9 do + if def and def:sub(1, 6) == "group:" then + if def == "group:liquid" then + def = "default:water_source" + elseif def == "group:vessel" then + def = "vessels:glass_bottle" + elseif def == "group:wool" then + def = "wool:white" + elseif def:find("group:dye") then + def = "dye:white" -- TODO: display respective dye color + else + def = "default:"..def:sub(7, string.len(def)) + end + end + end + if stack_type == "cooking" then + inv:set_stack("craft_output_recipe", 5, def) + else + inv:set_stack("craft_output_recipe", k, def) + end + end + end + + minetest.after(0.1, function() + inv:set_stack("inv_items_list", from_index, stackname) + worktable.craft_output_recipe(pos, meta:get_int("start_i"), meta:get_int("start_i") / (8*4) + 1, stackname) + end) + + return 1 + end return 0 end