update craft guide

master
Brett O'Donnell 2012-11-09 20:16:50 +10:30
parent 989e16f793
commit b91f49afb9
2 changed files with 417 additions and 410 deletions

View File

@ -1,377 +1,390 @@
--[[ --[[
Craft Guide for Minetest Craft Guide for Minetest
Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com> Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
Source Code: https://github.com/cornernote/minetest-craft_guide Source Code: https://github.com/cornernote/minetest-craft_guide
License: GPLv3 License: GPLv3
CRAFT GUIDE API CRAFT GUIDE API
]]-- ]]--
-- expose object to other modules -- expose object to other modules
craft_guide = {} craft_guide = {}
-- define api variables -- define api variables
craft_guide.crafts = {} craft_guide.crafts = {}
craft_guide.craft_guide_size = 0
-- log
-- log craft_guide.log = function(message)
craft_guide.log = function(message) --if not craft_guide.DEBUG then return end
--if not craft_guide.DEBUG then return end minetest.log("action", "[CraftGuide] "..message)
minetest.log("action", "[CraftGuide] "..message) end
end
-- register_craft
-- register_craft craft_guide.register_craft = function(options)
craft_guide.register_craft = function(options) if options.output == nil then
if options.output == nil then return
return end
end local itemstack = ItemStack(options.output)
local itemstack = ItemStack(options.output) if itemstack:is_empty() then
if itemstack:is_empty() then return
return end
end --craft_guide.log("registered craft for - "..itemstack:get_name())
--craft_guide.log("registered craft for - "..itemstack:get_name()) if craft_guide.crafts[itemstack:get_name()]==nil then
if craft_guide.crafts[itemstack:get_name()]==nil then craft_guide.crafts[itemstack:get_name()] = {}
craft_guide.crafts[itemstack:get_name()] = {} end
end table.insert(craft_guide.crafts[itemstack:get_name()],options)
table.insert(craft_guide.crafts[itemstack:get_name()],options) end
end
-- get_craft_guide_formspec
-- get_craft_guide_formspec craft_guide.get_craft_guide_formspec = function(meta, search, page, alternate)
craft_guide.get_craft_guide_formspec = function(meta, page, alternate) if search == nil then
if page == nil then search = meta:get_string("search")
page = craft_guide.get_current_page(meta) end
end if page == nil then
if alternate == nil then page = craft_guide.get_current_page(meta)
alternate = craft_guide.get_current_alternate(meta) end
end if alternate == nil then
local start = (page-1) * (5*14) + 1 alternate = craft_guide.get_current_alternate(meta)
local pages = math.floor((craft_guide.craft_guide_size-1) / (5*14) + 1) end
local alternates = 0 local inv = meta:get_inventory()
local stack = meta:get_inventory():get_stack("output",1) local size = inv:get_size("main")
local crafts = craft_guide.crafts[stack:get_name()] local start = (page-1) * (5*14) + 1
if crafts ~= nil then local pages = math.floor((size-1) / (5*14) + 1)
alternates = #crafts local alternates = 0
end local stack = inv:get_stack("output",1)
local formspec = "size[14,10;]" local crafts = craft_guide.crafts[stack:get_name()]
.."label[0,5;--== Learn to Craft ==--]" if crafts ~= nil then
.."label[0,5.4;Drag any item to the Output box to see the]" alternates = #crafts
.."label[0,5.8;craft. Save your favorite items in Bookmarks.]" end
.."label[9,5.2;page "..tostring(page).." of "..tostring(pages).."]" local formspec = "size[14,10;]"
.."button[11,5;1.5,1;craft_guide_prev;<<]" .."list[current_name;main;0,0;14,5;"..tostring(start).."]"
.."button[12.5,5;1.5,1;craft_guide_next;>>]"
.."list[detached:craft_guide;main;0,0;14,5;"..tostring(start).."]" .."label[0,5;--== Learn to Craft ==--]"
.."label[0,6.5;Output]" .."label[0,5.4;Drag any item to the Output box to see the]"
.."list[current_name;output;0,7;1,1;]" .."label[0,5.8;craft. Save your favorite items in Bookmarks.]"
.."label[2,6.5;Inventory Craft]"
.."list[current_name;build;2,7;3,3;]" .."field[6,5.4;2,1;craft_guide_search_box;;"..tostring(search).."]"
.."label[6,6.5;Cook]" .."button[7.5,5.1;1.2,1;craft_guide_search_button;Search]"
.."list[current_name;cook;6,7;1,1;]"
.."label[6,8.5;Fuel]" .."label[9,5.2;page "..tostring(page).." of "..tostring(pages).."]"
.."list[current_name;fuel;6,9;1,1;]" .."button[11,5;1.5,1;craft_guide_prev;<<]"
.."label[8,6.5;Bookmarks]" .."button[12.5,5;1.5,1;craft_guide_next;>>]"
.."list[current_name;bookmark;8,7;6,3;]"
.."label[12,6.1;Bin ->]" .."label[0,6.5;Output]"
.."list[current_name;bin;13,6;1,1;]" .."list[current_name;output;0,7;1,1;]"
if alternates > 1 then
formspec = formspec .."label[2,6.5;Inventory Craft]"
.."label[0,8.6;recipe "..tostring(alternate).." of "..tostring(alternates).."]" .."list[current_name;build;2,7;3,3;]"
.."button[0,9;2,1;alternate;Alternate]"
end .."label[6,6.5;Cook]"
return formspec .."list[current_name;cook;6,7;1,1;]"
end .."label[6,8.5;Fuel]"
.."list[current_name;fuel;6,9;1,1;]"
-- on_construct .."label[8,6.5;Bookmarks]"
craft_guide.on_construct = function(pos) .."list[current_name;bookmark;8,7;6,3;]"
local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory() .."label[12,6.1;Bin ->]"
inv:set_size("output", 1) .."list[current_name;bin;13,6;1,1;]"
inv:set_size("build", 3*3) if alternates > 1 then
inv:set_size("cook", 1) formspec = formspec
inv:set_size("fuel", 1) .."label[0,8.6;recipe "..tostring(alternate).." of "..tostring(alternates).."]"
inv:set_size("bookmark", 6*3) .."button[0,9;2,1;alternate;Alternate]"
inv:set_size("bin", 1) end
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta)) return formspec
end end
-- on_receive_fields -- on_construct
craft_guide.on_receive_fields = function(pos, formname, fields, player) craft_guide.on_construct = function(pos)
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
local stack = meta:get_inventory():get_stack("output",1) inv:set_size("output", 1)
local crafts = craft_guide.crafts[stack:get_name()] inv:set_size("build", 3*3)
local alternate = craft_guide.get_current_alternate(meta) inv:set_size("cook", 1)
local alternates = 0 inv:set_size("fuel", 1)
if crafts ~= nil then inv:set_size("bookmark", 6*3)
alternates = #crafts inv:set_size("bin", 1)
end craft_guide.create_inventory(inv)
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
local page = craft_guide.get_current_page(meta) end
local pages = math.floor((craft_guide.craft_guide_size-1) / (5*14) + 1)
-- get an alternate recipe -- on_receive_fields
if fields.alternate then craft_guide.on_receive_fields = function(pos, formname, fields, player)
alternate = alternate+1 local meta = minetest.env:get_meta(pos);
craft_guide.update_recipe(meta, player, stack, alternate)
end local inv = meta:get_inventory()
if alternate > alternates then local size = inv:get_size("main",1)
alternate = 1 local stack = inv:get_stack("output",1)
end local crafts = craft_guide.crafts[stack:get_name()]
local alternate = craft_guide.get_current_alternate(meta)
-- change page local alternates = 0
if fields.craft_guide_prev then if crafts ~= nil then
page = page - 1 alternates = #crafts
end end
if fields.craft_guide_next then
page = page + 1 local page = craft_guide.get_current_page(meta)
end local pages = math.floor((size-1) / (5*14) + 1)
if page < 1 then
page = 1 local search
end
if page > pages then -- search
page = pages search = fields.craft_guide_search_box
end meta:set_string("search", search)
if fields.craft_guide_search_button then
-- update the formspec page = 1
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta, page, alternate)) end
end
-- change page
if fields.craft_guide_prev then
-- get_current_page page = page - 1
craft_guide.get_current_page = function(meta) end
local formspec = meta:get_string("formspec") if fields.craft_guide_next then
local page = string.match(formspec, "label%[[%d.]+,[%d.]+;page (%d+) of [%d.]+%]") page = page + 1
page = tonumber(page) or 1 end
return page if page < 1 then
end page = 1
end
if page > pages then
-- get_current_alternate page = pages
craft_guide.get_current_alternate = function(meta) end
local formspec = meta:get_string("formspec")
local alternate = string.match(formspec, "label%[[%d.]+,[%d.]+;recipe (%d+) of [%d.]+%]") -- get an alternate recipe
alternate = tonumber(alternate) or 1 if fields.alternate then
return alternate alternate = alternate+1
end craft_guide.update_recipe(meta, player, stack, alternate)
end
if alternate > alternates then
-- update_recipe alternate = 1
craft_guide.update_recipe = function(meta, player, stack, alternate) end
local inv = meta:get_inventory()
for i=0,inv:get_size("build"),1 do -- update the formspec
inv:set_stack("build", i, nil) craft_guide.create_inventory(inv, search)
end meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta, search, page, alternate))
inv:set_stack("cook", 1, nil) end
inv:set_stack("fuel", 1, nil)
if stack==nil then return end -- get_current_page
inv:set_stack("output", 1, stack:get_name()) craft_guide.get_current_page = function(meta)
local formspec = meta:get_string("formspec")
alternate = tonumber(alternate) or 1 local page = string.match(formspec, "label%[[%d.]+,[%d.]+;page (%d+) of [%d.]+%]")
craft_guide.log(player:get_player_name().." requests recipe "..alternate.." for "..stack:get_name()) page = tonumber(page) or 1
local crafts = craft_guide.crafts[stack:get_name()] return page
end
if crafts == nil then
minetest.chat_send_player(player:get_player_name(), "no recipe available for "..stack:get_name())
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta)) -- get_current_alternate
return craft_guide.get_current_alternate = function(meta)
end local formspec = meta:get_string("formspec")
if alternate < 1 or alternate > #crafts then local alternate = string.match(formspec, "label%[[%d.]+,[%d.]+;recipe (%d+) of [%d.]+%]")
alternate = 1 alternate = tonumber(alternate) or 1
end return alternate
local craft = crafts[alternate] end
-- show me the unknown items
craft_guide.log(dump(craft)) -- update_recipe
--minetest.chat_send_player(player:get_player_name(), "recipe for "..stack:get_name()..": "..dump(craft)) craft_guide.update_recipe = function(meta, player, stack, alternate)
local inv = meta:get_inventory()
local itemstack = ItemStack(craft.output) for i=0,inv:get_size("build"),1 do
inv:set_stack("output", 1, itemstack) inv:set_stack("build", i, nil)
end
-- cook inv:set_stack("cook", 1, nil)
if craft.type == "cooking" then inv:set_stack("fuel", 1, nil)
inv:set_stack("cook", 1, craft.recipe)
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta)) if stack==nil then return end
return inv:set_stack("output", 1, stack:get_name())
end
-- fuel alternate = tonumber(alternate) or 1
if craft.type == "fuel" then craft_guide.log(player:get_player_name().." requests recipe "..alternate.." for "..stack:get_name())
inv:set_stack("fuel", 1, craft.recipe) local crafts = craft_guide.crafts[stack:get_name()]
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
return if crafts == nil then
end minetest.chat_send_player(player:get_player_name(), "no recipe available for "..stack:get_name())
-- build (shaped or shapeless) meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
if craft.recipe[1] then return
if (type(craft.recipe[1]) == "string") then end
inv:set_stack("build", 1, craft.recipe[1]) if alternate < 1 or alternate > #crafts then
else alternate = 1
if craft.recipe[1][1] then end
inv:set_stack("build", 1, craft.recipe[1][1]) local craft = crafts[alternate]
end
if craft.recipe[1][2] then -- show me the unknown items
inv:set_stack("build", 2, craft.recipe[1][2]) craft_guide.log(dump(craft))
end --minetest.chat_send_player(player:get_player_name(), "recipe for "..stack:get_name()..": "..dump(craft))
if craft.recipe[1][3] then
inv:set_stack("build", 3, craft.recipe[1][3]) local itemstack = ItemStack(craft.output)
end inv:set_stack("output", 1, itemstack)
end
end -- cook
if craft.recipe[2] then if craft.type == "cooking" then
if (type(craft.recipe[2]) == "string") then inv:set_stack("cook", 1, craft.recipe)
inv:set_stack("build", 2, craft.recipe[2]) meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
else return
if craft.recipe[2][1] then end
inv:set_stack("build", 4, craft.recipe[2][1]) -- fuel
end if craft.type == "fuel" then
if craft.recipe[2][2] then inv:set_stack("fuel", 1, craft.recipe)
inv:set_stack("build", 5, craft.recipe[2][2]) meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
end return
if craft.recipe[2][3] then end
inv:set_stack("build", 6, craft.recipe[2][3]) -- build (shaped or shapeless)
end if craft.recipe[1] then
end if (type(craft.recipe[1]) == "string") then
end inv:set_stack("build", 1, craft.recipe[1])
if craft.recipe[3] then else
if (type(craft.recipe[3]) == "string") then if craft.recipe[1][1] then
inv:set_stack("build", 3, craft.recipe[3]) inv:set_stack("build", 1, craft.recipe[1][1])
else end
if craft.recipe[3][1] then if craft.recipe[1][2] then
inv:set_stack("build", 7, craft.recipe[3][1]) inv:set_stack("build", 2, craft.recipe[1][2])
end end
if craft.recipe[3][2] then if craft.recipe[1][3] then
inv:set_stack("build", 8, craft.recipe[3][2]) inv:set_stack("build", 3, craft.recipe[1][3])
end end
if craft.recipe[3][3] then end
inv:set_stack("build", 9, craft.recipe[3][3]) end
end if craft.recipe[2] then
end if (type(craft.recipe[2]) == "string") then
end inv:set_stack("build", 2, craft.recipe[2])
if craft.recipe[4] then else
if (type(craft.recipe[4]) == "string") then if craft.recipe[2][1] then
inv:set_stack("build", 4, craft.recipe[4]) inv:set_stack("build", 4, craft.recipe[2][1])
end end
end if craft.recipe[2][2] then
if craft.recipe[5] then inv:set_stack("build", 5, craft.recipe[2][2])
if (type(craft.recipe[5]) == "string") then end
inv:set_stack("build", 5, craft.recipe[5]) if craft.recipe[2][3] then
end inv:set_stack("build", 6, craft.recipe[2][3])
end end
if craft.recipe[6] then end
if (type(craft.recipe[6]) == "string") then end
inv:set_stack("build", 6, craft.recipe[6]) if craft.recipe[3] then
end if (type(craft.recipe[3]) == "string") then
end inv:set_stack("build", 3, craft.recipe[3])
if craft.recipe[7] then else
if (type(craft.recipe[7]) == "string") then if craft.recipe[3][1] then
inv:set_stack("build", 7, craft.recipe[7]) inv:set_stack("build", 7, craft.recipe[3][1])
end end
end if craft.recipe[3][2] then
if craft.recipe[8] then inv:set_stack("build", 8, craft.recipe[3][2])
if (type(craft.recipe[8]) == "string") then end
inv:set_stack("build", 8, craft.recipe[8]) if craft.recipe[3][3] then
end inv:set_stack("build", 9, craft.recipe[3][3])
end end
if craft.recipe[9] then end
if (type(craft.recipe[9]) == "string") then end
inv:set_stack("build", 9, craft.recipe[9]) if craft.recipe[4] then
end if (type(craft.recipe[4]) == "string") then
end inv:set_stack("build", 4, craft.recipe[4])
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta)) end
end end
if craft.recipe[5] then
if (type(craft.recipe[5]) == "string") then
-- create_detached_inventory inv:set_stack("build", 5, craft.recipe[5])
craft_guide.create_detached_inventory = function() end
local inv = minetest.create_detached_inventory("craft_guide", { end
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player) if craft.recipe[6] then
return 0 if (type(craft.recipe[6]) == "string") then
end, inv:set_stack("build", 6, craft.recipe[6])
allow_put = function(inv, listname, index, stack, player) end
return -1 end
end, if craft.recipe[7] then
allow_take = function(inv, listname, index, stack, player) if (type(craft.recipe[7]) == "string") then
return 0 inv:set_stack("build", 7, craft.recipe[7])
end, end
on_move = function(inv, from_list, from_index, to_list, to_index, count, player) end
end, if craft.recipe[8] then
on_put = function(inv, listname, index, stack, player) if (type(craft.recipe[8]) == "string") then
end, inv:set_stack("build", 8, craft.recipe[8])
on_take = function(inv, listname, index, stack, player) end
end, end
}) if craft.recipe[9] then
if (type(craft.recipe[9]) == "string") then
local craft_guide_list = {} inv:set_stack("build", 9, craft.recipe[9])
for name,def in pairs(minetest.registered_items) do end
-- local craft_recipe = minetest.get_craft_recipe(name); end
-- if craft_recipe.items ~= nil then meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
local craft = craft_guide.crafts[name]; end
if craft ~= nil then
if (not def.groups.not_in_craft_guide or def.groups.not_in_craft_guide == 0)
--and (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0) -- create_inventory
and def.description and def.description ~= "" then craft_guide.create_inventory = function(inv, search)
table.insert(craft_guide_list, name) local craft_guide_list = {}
end for name,def in pairs(minetest.registered_items) do
end -- local craft_recipe = minetest.get_craft_recipe(name);
end -- if craft_recipe.items ~= nil then
local craft = craft_guide.crafts[name];
table.sort(craft_guide_list) if craft ~= nil then
inv:set_size("main", #craft_guide_list) if (not def.groups.not_in_craft_guide or def.groups.not_in_craft_guide == 0)
for _,itemstring in ipairs(craft_guide_list) do --and (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0)
inv:add_item("main", ItemStack(itemstring)) and def.description and def.description ~= "" then
end if search then
craft_guide.craft_guide_size = #craft_guide_list if string.find(def.name, search) or string.find(def.description, search) then
craft_guide.log("craft_guide_size: "..dump(craft_guide.craft_guide_size)) table.insert(craft_guide_list, name)
end end
else
table.insert(craft_guide_list, name)
-- allow_metadata_inventory_move end
craft_guide.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) end
local meta = minetest.env:get_meta(pos) end
local inv = meta:get_inventory() end
if from_list == "bookmarks" and to_list == "bookmarks" then
return count table.sort(craft_guide_list)
end for i=0,inv:get_size("main"),1 do
if to_list == "bin" and from_list == "output" then inv:set_stack("main", i, nil)
inv:set_stack(from_list,from_index,nil) end
craft_guide.update_recipe(meta, player, inv:get_stack(from_list, from_index)) inv:set_size("main", #craft_guide_list)
end for _,itemstring in ipairs(craft_guide_list) do
if to_list == "bin" and from_list == "bookmark" then inv:add_item("main", ItemStack(itemstring))
inv:set_stack(from_list,from_index,nil) end
end end
if to_list == "bookmark" then
inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index):get_name())
end -- allow_metadata_inventory_move
if to_list == "output" then craft_guide.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
craft_guide.update_recipe(meta, player, inv:get_stack(from_list, from_index)) local meta = minetest.env:get_meta(pos)
end local inv = meta:get_inventory()
return 0 if to_list == "bin" and from_list == "output" then
end inv:set_stack(from_list,from_index,nil)
craft_guide.update_recipe(meta, player, inv:get_stack(from_list, from_index))
end
-- allow_metadata_inventory_put if to_list == "bin" and from_list == "bookmark" then
craft_guide.allow_metadata_inventory_put = function(pos, listname, index, stack, player) inv:set_stack(from_list,from_index,nil)
if listname == "bookmark" then end
minetest.env:get_meta(pos):get_inventory():set_stack(listname,index,stack) if to_list == "bookmark" then
end inv:set_stack(to_list, to_index, inv:get_stack(from_list, from_index):get_name())
if listname == "output" then if from_list == "output" then
local meta = minetest.env:get_meta(pos) inv:set_stack(from_list,from_index,nil)
craft_guide.update_recipe(meta, player, stack) end
end end
return 0 if to_list == "output" or from_list == "output" then
end craft_guide.update_recipe(meta, player, inv:get_stack(from_list, from_index))
end
if from_list == "bookmarks" and to_list == "bookmarks" then
-- allow_metadata_inventory_take return count
craft_guide.allow_metadata_inventory_take = function(pos, listname, index, stack, player) end
return 0 return 0
end end
-- allow_metadata_inventory_put
craft_guide.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
return 0
end
-- allow_metadata_inventory_take
craft_guide.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
return 0
end

View File

@ -1,34 +1,28 @@
--[[ --[[
Craft Guide for Minetest Craft Guide for Minetest
Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com> Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
Source Code: https://github.com/cornernote/minetest-craft_guide Source Code: https://github.com/cornernote/minetest-craft_guide
License: GPLv3 License: GPLv3
MAIN LOADER MAIN LOADER
]]-- ]]--
-- load api -- load api
dofile(minetest.get_modpath("craft_guide").."/api_craft_guide.lua") dofile(minetest.get_modpath("craft_guide").."/api_craft_guide.lua")
-- override minetest.register_craft -- override minetest.register_craft
local minetest_register_craft = minetest.register_craft local minetest_register_craft = minetest.register_craft
minetest.register_craft = function (options) minetest.register_craft = function (options)
minetest_register_craft(options) minetest_register_craft(options)
craft_guide.register_craft(options) craft_guide.register_craft(options)
end end
-- after the server starts -- register entities
minetest.after(0, function() dofile(minetest.get_modpath("craft_guide").."/register_node.lua")
-- create inventory dofile(minetest.get_modpath("craft_guide").."/register_craft.lua")
craft_guide.create_detached_inventory()
end) -- log that we started
-- register entities
dofile(minetest.get_modpath("craft_guide").."/register_node.lua")
dofile(minetest.get_modpath("craft_guide").."/register_craft.lua")
-- log that we started
minetest.log("action", "[MOD]"..minetest.get_current_modname().." -- loaded from "..minetest.get_modpath(minetest.get_current_modname())) minetest.log("action", "[MOD]"..minetest.get_current_modname().." -- loaded from "..minetest.get_modpath(minetest.get_current_modname()))