Simplify craft guide mechanism

This commit is contained in:
jp 2015-12-01 13:09:40 +01:00
parent 9f46908cfc
commit 0189532095
2 changed files with 96 additions and 143 deletions

View File

@ -18,18 +18,14 @@ function enchanting.formspec(pos, tooltype)
"tooltip[speed;Your speed is increased]"
if tooltype == "sword" then
formspec = formspec..
"image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]"
formspec = formspec.."image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]"
elseif tooltype == "tool" then
formspec = formspec..
"image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]"..
formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;fast;Efficiency]"..
"image_button[3.9,1.77;4,1.12;bg_btn.png;durable;Durability]"
elseif tooltype == "armor" then
formspec = formspec..
"image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"
formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"
elseif tooltype == "boots" then
formspec = formspec..
"image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"..
formspec = formspec.."image_button[3.9,0.85;4,0.92;bg_btn.png;strong;Strength]"..
"image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed]"
end

View File

@ -40,33 +40,87 @@ function worktable.craft_output_recipe(pos, start_i, pagenum, stackname)
pagenum = math.floor(pagenum)
local inventory_size = meta:get_int("inventory_size")
local pagemax = math.floor((inventory_size-1) / (8*4) + 1) or 0
local craft, dye_color = {}, ""
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]"..
"list[context;item_craft_input;3,6.3;1,1;]"..
"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[5.5,0;0.7,1;prev;<]"..
"button[7.3,0;0.7,1;next;>]"..
"button[4,0.2;0.7,0.5;search;?]"..
"button[4.6,0.2;0.7,0.5;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]"..
"label[3,5.8;Input]"..
"field[1.8,0.32;2.6,1;filter;;]"
if stackname then
meta:set_string("item", stackname)
formspec = formspec.."label[0.15,7.5;"..meta:get_string("item"):sub(1,30).."]"
local stack_width = minetest.get_craft_recipe(stackname).width
local stack_items = minetest.get_craft_recipe(stackname).items
local stack_type = minetest.get_craft_recipe(stackname).type
local stack_output = minetest.get_craft_recipe(stackname).output
local stack_count = stack_output:match("%s(%d+)")
if stack_count and inv:is_empty("item_craft_input") then
inv:add_item("item_craft_input", stackname.." "..stack_count-1)
end
if stack_width == 0 or stack_width == 1 then
if #stack_items == 1 then
formspec = formspec.."list[context;craft_output_recipe;5,6.3;1,1;]"
else
formspec = formspec.."list[context;craft_output_recipe;5,5.3;1,"..#stack_items..";]"
end
inv:set_size("craft_output_recipe", 1 * #stack_items)
elseif stack_width == 2 then
formspec = formspec.."list[context;craft_output_recipe;5,5.3;2,3;]"
inv:set_size("craft_output_recipe", 2*3)
elseif stack_width == 3 then
if stack_type == "cooking" then
formspec = formspec.."list[context;craft_output_recipe;5,6.3;1,1;]"..
"image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png]"
inv:set_size("craft_output_recipe", 1)
else
formspec = formspec.."list[context;craft_output_recipe;5,5.3;3,3;]"
inv:set_size("craft_output_recipe", 3*3)
end
elseif stack_type == "cooking" and stack_width == 15 then
formspec = formspec.."list[context;craft_output_recipe;5,6.3;1,1;]"..
"image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png]"
inv:set_size("craft_output_recipe", 1)
end
for k, def in pairs(stack_items) do
craft[#craft+1] = def
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
dye_color = def:match("group:dye,%w+_([%w_]+)")
def = "dye:"..dye_color
elseif def:match("group:stone") or def:match("group:wood") or
def:match("group:leaves") or def:match("group:stick") or
def:match("group:sand") or def:match("group:tree") or
def:match("group:sapling") or def:match("group:book") then
def = "default:"..def:sub(7, string.len(def))
end
end
inv:set_stack("craft_output_recipe", k, def)
end
formspec = formspec.."image[4,6.3;1,1;gui_furnace_arrow_bg.png^[transformR90]"..
"button[1.2,6.35;1.5,1;trash;Clear]"..
"label["..(12/string.len(stackname))..",7.5;"..stackname: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
@ -77,10 +131,8 @@ function worktable.craftguide_update(pos, filter)
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 not (def.groups.not_in_creative_inventory == 1) and def.name ~= "unknown"
and def.description and def.description ~= "" then
if filter and def.name:find(filter) then
inv_items_list[#inv_items_list+1] = name
@ -168,7 +220,6 @@ end
function worktable.fields(pos, _, fields, sender)
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")
local inventory_size = meta:get_int("inventory_size")
start_i = tonumber(start_i) or 0
@ -183,34 +234,30 @@ function worktable.fields(pos, _, fields, sender)
worktable.crafting(pos)
elseif fields.craft_output_recipe then
worktable.craft_output_recipe(pos, 0, 1)
elseif fields.trash then
elseif fields.trash or fields.search or fields.clearfilter or
fields.prev or fields.next then
if fields.trash then
worktable.craft_output_recipe(pos, start_i, 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)
elseif fields.prev or fields.next then
if fields.prev or start_i >= inventory_size then
start_i = start_i - 8*4
elseif fields.next or start_i < 0 then
start_i = start_i + 8*4
end
if start_i < 0 or start_i >= inventory_size then
start_i = 0
end
worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, nil)
end
inv:set_list("item_craft_input", {})
inv:set_list("craft_output_recipe", {})
worktable.craft_output_recipe(pos, start_i, 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 >= inventory_size then
start_i = start_i - 8*4
elseif start_i < 0 or start_i >= inventory_size then
start_i = 0
end
worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, inputstack)
end
end
@ -272,103 +319,13 @@ function worktable.move(pos, from_list, from_index, to_list, to_index, count, _)
local stackname = inv:get_stack(from_list, from_index):get_name()
local meta = minetest.get_meta(pos)
local start_i = meta:get_int("start_i")
local craft = {}
local dye_color = ""
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+)")
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
--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
dye_color = craft[i]:match("group:dye,%w+_([%w_]+)")
craft[i] = "dye:"..dye_color
elseif craft[i]:match("group:stone") or craft[i]:match("group:wood") or
craft[i]:match("group:leaves") or craft[i]:match("group:stick") or
craft[i]:match("group:sand") or craft[i]:match("group:tree") or
craft[i]:match("group:sapling") then
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
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
dye_color = def:match("group:dye,%w+_([%w_]+)")
def = "dye:"..dye_color
elseif def:match("group:stone") or def:match("group:wood") or
def:match("group:leaves") or def:match("group:stick") or
def:match("group:sand") or def:match("group:tree") or
def:match("group:sapling") then
def = "default:"..def:sub(7, string.len(def))
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
elseif stack_type == "cooking" then
inv:set_stack("craft_output_recipe", 5, craft[1])
end
worktable.craft_output_recipe(pos, start_i, start_i / (8*4) + 1, stackname)
minetest.after(0, function()