add an output field, that is being filled with the crafting result and can be used for quickly emptying or filling the crafting grid with the last registered recipe

some of the recipes are still treated wrongly due to minetest/minetest#2222 but are failling gracefully
This commit is contained in:
Tim 2015-01-28 06:23:46 +01:00
parent 5fa3e1f626
commit 320a483f4f
1 changed files with 47 additions and 18 deletions

View File

@ -22,22 +22,12 @@ local function get_craft(pos, inventory, hash)
local hash = hash or minetest.hash_node_position(pos) local hash = hash or minetest.hash_node_position(pos)
local craft = autocrafterCache[hash] local craft = autocrafterCache[hash]
if not craft then if not craft then
if inventory:is_empty("recipe") then
set_infotext(pos, nil)
return
end
local recipe = inventory:get_list("recipe") local recipe = inventory:get_list("recipe")
local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe}) local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
craft = {recipe = recipe, consumption=count_index(recipe), output = output, decremented_input = decremented_input} craft = {recipe = recipe, consumption=count_index(recipe), output = output, decremented_input = decremented_input}
autocrafterCache[hash] = craft autocrafterCache[hash] = craft
set_infotext(pos, "Autocrafter: " .. output.item:get_name())
end
-- only return crafts that have an actual result
if not craft.output.item:is_empty() then
return craft
else
set_infotext(pos, "Autocrafter: unknown recipe")
end end
return craft
end end
local function start_crafter(pos) local function start_crafter(pos)
@ -78,6 +68,11 @@ local function after_recipe_change(pos, inventory)
end end
end end
craft = craft or get_craft(pos, inventory, hash)
local output_item = craft.output.item
set_infotext(pos, "Autocrafter: " .. output_item:get_name())
inventory:set_stack("output", 1, output_item)
start_crafter(pos) start_crafter(pos)
end end
@ -119,6 +114,12 @@ local function run_autocrafter(pos, elapsed)
local inventory = meta:get_inventory() local inventory = meta:get_inventory()
local craft = get_craft(pos, inventory) local craft = get_craft(pos, inventory)
-- only use crafts that have an actual result
if craft.output.item:is_empty() then
set_infotext(pos, "Autocrafter: unknown recipe")
return false
end
for step = 1, math.floor(elapsed/craft_time) do for step = 1, math.floor(elapsed/craft_time) do
local continue = autocraft(inventory, craft) local continue = autocraft(inventory, craft)
if not continue then return false end if not continue then return false end
@ -146,6 +147,8 @@ local function set_formspec(meta, enabled)
meta:set_string("formspec", meta:set_string("formspec",
"size[8,11]".. "size[8,11]"..
"list[context;recipe;0,0;3,3;]".. "list[context;recipe;0,0;3,3;]"..
"image[3,1;1,1;gui_hb_bg.png^[colorize:#141318:255]"..
"list[context;output;3,1;1,1;]"..
"image_button[3,2;1,1;pipeworks_button_" .. state .. ".png;" .. state .. ";;;false;pipeworks_button_interm.png]" .. "image_button[3,2;1,1;pipeworks_button_" .. state .. ".png;" .. state .. ";;;false;pipeworks_button_interm.png]" ..
"list[context;src;0,3.5;8,3;]".. "list[context;src;0,3.5;8,3;]"..
"list[context;dst;4,0;4,3;]".. "list[context;dst;4,0;4,3;]"..
@ -162,6 +165,19 @@ local function add_virtual_item(inv, listname, index, stack)
inv:set_stack(listname, index, stack_copy) inv:set_stack(listname, index, stack_copy)
end end
local function on_output_change(pos, inventory, stack)
if not stack then
inventory:set_list("output", {})
inventory:set_list("recipe", {})
else
local input = minetest.get_craft_recipe(stack:get_name())
if not input.items or input.type ~= "normal" then return end
inventory:set_list("recipe", input.items)
add_virtual_item(inventory, "output", 1, stack)
end
after_recipe_change(pos, inventory)
end
minetest.register_node("pipeworks:autocrafter", { minetest.register_node("pipeworks:autocrafter", {
description = "Autocrafter", description = "Autocrafter",
drawtype = "normal", drawtype = "normal",
@ -190,6 +206,7 @@ minetest.register_node("pipeworks:autocrafter", {
inv:set_size("src", 3*8) inv:set_size("src", 3*8)
inv:set_size("recipe", 3*3) inv:set_size("recipe", 3*3)
inv:set_size("dst", 4*3) inv:set_size("dst", 4*3)
inv:set_size("output", 1)
end, end,
on_receive_fields = function(pos, formname, fields, sender) on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
@ -227,10 +244,12 @@ minetest.register_node("pipeworks:autocrafter", {
add_virtual_item(inv, listname, index, stack) add_virtual_item(inv, listname, index, stack)
after_recipe_change(pos, inv) after_recipe_change(pos, inv)
return 0 return 0
else elseif listname == "output" then
after_inventory_change(pos, inv) on_output_change(pos, inv, stack)
return stack:get_count() return 0
end end
after_inventory_change(pos, inv)
return stack:get_count()
end, end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player) allow_metadata_inventory_take = function(pos, listname, index, stack, player)
update_autocrafter(pos) update_autocrafter(pos)
@ -239,23 +258,33 @@ minetest.register_node("pipeworks:autocrafter", {
inv:set_stack(listname, index, ItemStack("")) inv:set_stack(listname, index, ItemStack(""))
after_recipe_change(pos, inv) after_recipe_change(pos, inv)
return 0 return 0
else elseif listname == "output" then
after_inventory_change(pos, inv) on_output_change(pos, inv, nil)
return stack:get_count() return 0
end end
after_inventory_change(pos, inv)
return stack:get_count()
end, end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
update_autocrafter(pos) update_autocrafter(pos)
local inv = minetest.get_meta(pos):get_inventory() local inv = minetest.get_meta(pos):get_inventory()
local stack = inv:get_stack(from_list, from_index) local stack = inv:get_stack(from_list, from_index)
stack:set_count(count) stack:set_count(count)
if to_list == "output" then
on_output_change(pos, inv, stack)
return 0
elseif from_list == "output" then
on_output_change(pos, inv, nil)
return 0
end
if from_list == "recipe" then if from_list == "recipe" then
inv:set_stack(from_list, from_index, ItemStack("")) inv:set_stack(from_list, from_index, ItemStack(""))
end end
if to_list == "recipe" then if to_list == "recipe" then
add_virtual_item(inv, to_list, to_index, stack) add_virtual_item(inv, to_list, to_index, stack)
end end
if from_list == "recipe" or to_list == "recipe" then if from_list == "recipe" or to_list == "recipe" then
after_recipe_change(pos, inv) after_recipe_change(pos, inv)
return 0 return 0