2013-10-04 17:56:03 +02:00
|
|
|
local autocrafterCache = {} -- caches some recipe data to avoid to call the slow function minetest.get_craft_result() every second
|
2013-01-19 15:37:27 +01:00
|
|
|
|
2013-10-04 17:03:27 +02:00
|
|
|
function autocraft(inventory, pos)
|
|
|
|
local recipe = inventory:get_list("recipe")
|
|
|
|
local recipe_last
|
|
|
|
local result
|
|
|
|
local new
|
2013-10-04 16:41:23 +02:00
|
|
|
|
2013-10-04 17:56:03 +02:00
|
|
|
if autocrafterCache[minetest.hash_node_position(pos)] == nil then
|
|
|
|
--print("first call for this autocrafter at pos("..pos.x..","..pos.y..","..pos.z..")")
|
|
|
|
recipe_last = {}
|
2013-10-04 17:03:27 +02:00
|
|
|
for i = 1, 9 do
|
2013-10-04 17:56:03 +02:00
|
|
|
recipe_last[i] = recipe[i]
|
2013-10-04 17:03:27 +02:00
|
|
|
--print (recipe_last[i]:get_name())
|
2013-10-04 17:56:03 +02:00
|
|
|
recipe[i] = ItemStack({name = recipe[i]:get_name(), count = 1})
|
|
|
|
end
|
|
|
|
result, new = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
|
|
|
autocrafterCache[minetest.hash_node_position(pos)] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
|
|
|
|
else
|
|
|
|
local autocrafterCacheEntry = autocrafterCache[minetest.hash_node_position(pos)]
|
|
|
|
recipe_last = autocrafterCacheEntry["recipe"]
|
|
|
|
result = autocrafterCacheEntry["result"]
|
|
|
|
new = autocrafterCacheEntry["new"]
|
|
|
|
local recipeUnchanged = true
|
|
|
|
for i = 1, 9 do
|
2013-10-04 17:03:27 +02:00
|
|
|
if recipe[i]:get_name() ~= recipe_last[i]:get_name() then
|
|
|
|
recipeUnchanged = False
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if recipe[i]:get_count() ~= recipe_last[i]:get_count() then
|
|
|
|
recipeUnchanged = False
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if recipeUnchanged then
|
2013-10-04 17:56:03 +02:00
|
|
|
--print("autocrafter recipe unchanged")
|
2013-10-04 17:03:27 +02:00
|
|
|
else
|
2013-10-04 17:56:03 +02:00
|
|
|
--print("autocrafter recipe changed at pos("..pos.x..","..pos.y..","..pos.z..")")
|
2013-10-04 17:03:27 +02:00
|
|
|
for i = 1, 9 do
|
|
|
|
recipe_last[i] = recipe[i]
|
|
|
|
recipe[i] = ItemStack({name = recipe[i]:get_name(), count = 1})
|
|
|
|
end
|
|
|
|
result, new = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
2013-10-04 17:56:03 +02:00
|
|
|
autocrafterCache[minetest.hash_node_position(pos)] = {["recipe"] = recipe, ["result"] = result, ["new"] = new}
|
2013-10-04 17:03:27 +02:00
|
|
|
end
|
|
|
|
end
|
2013-10-04 16:41:23 +02:00
|
|
|
|
2013-10-04 17:03:27 +02:00
|
|
|
local input = inventory:get_list("input")
|
2013-01-19 15:37:27 +01:00
|
|
|
if result.item:is_empty() then return end
|
2013-10-04 17:03:27 +02:00
|
|
|
result = result.item
|
2013-04-07 11:30:11 +02:00
|
|
|
if not inventory:room_for_item("dst", result) then return end
|
2013-10-04 17:03:27 +02:00
|
|
|
local to_use = {}
|
|
|
|
for _, item in ipairs(recipe) do
|
|
|
|
if item~= nil and not item:is_empty() then
|
|
|
|
if to_use[item:get_name()] == nil then
|
|
|
|
to_use[item:get_name()] = 1
|
2013-01-19 15:37:27 +01:00
|
|
|
else
|
2013-10-04 17:03:27 +02:00
|
|
|
to_use[item:get_name()] = to_use[item:get_name()]+1
|
2013-01-19 15:37:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local stack
|
2013-10-04 17:03:27 +02:00
|
|
|
for itemname, number in pairs(to_use) do
|
|
|
|
stack = ItemStack({name = itemname, count = number})
|
|
|
|
if not inventory:contains_item("src", stack) then return end
|
2013-01-19 15:37:27 +01:00
|
|
|
end
|
2013-10-04 17:03:27 +02:00
|
|
|
for itemname, number in pairs(to_use) do
|
|
|
|
stack = ItemStack({name = itemname, count = number})
|
|
|
|
inventory:remove_item("src", stack)
|
2013-01-19 15:37:27 +01:00
|
|
|
end
|
2013-10-04 17:03:27 +02:00
|
|
|
inventory:add_item("dst", result)
|
|
|
|
for i = 1, 9 do
|
|
|
|
inventory:add_item("dst", new.items[i])
|
2013-01-20 07:57:58 +01:00
|
|
|
end
|
2013-01-19 15:37:27 +01:00
|
|
|
end
|
|
|
|
|
2013-10-04 17:03:27 +02:00
|
|
|
minetest.register_node("pipeworks:autocrafter", {
|
|
|
|
description = "Autocrafter",
|
|
|
|
drawtype = "normal",
|
|
|
|
tiles = {"pipeworks_autocrafter.png"},
|
|
|
|
groups = {snappy = 3, tubedevice = 1, tubedevice_receiver = 1},
|
|
|
|
tube = {insert_object = function(pos, node, stack, direction)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return inv:add_item("src", stack)
|
|
|
|
end,
|
|
|
|
can_insert = function(pos, node, stack, direction)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return inv:room_for_item("src", stack)
|
|
|
|
end,
|
|
|
|
input_inventory = "dst",
|
|
|
|
connect_sides = {left = 1, right = 1, front = 1, back = 1, top = 1, bottom = 1}},
|
2013-01-19 15:37:27 +01:00
|
|
|
on_construct = function(pos)
|
2013-07-01 05:55:07 +02:00
|
|
|
local meta = minetest.get_meta(pos)
|
2013-01-19 15:37:27 +01:00
|
|
|
meta:set_string("formspec",
|
|
|
|
"size[8,11]"..
|
|
|
|
"list[current_name;recipe;0,0;3,3;]"..
|
2013-01-20 09:20:26 +01:00
|
|
|
"list[current_name;src;0,3.5;8,3;]"..
|
2013-01-19 15:37:27 +01:00
|
|
|
"list[current_name;dst;4,0;4,3;]"..
|
|
|
|
"list[current_player;main;0,7;8,4;]")
|
|
|
|
meta:set_string("infotext", "Autocrafter")
|
|
|
|
local inv = meta:get_inventory()
|
2013-10-04 17:03:27 +02:00
|
|
|
inv:set_size("src", 3*8)
|
|
|
|
inv:set_size("recipe", 3*3)
|
|
|
|
inv:set_size("dst", 4*3)
|
|
|
|
end,
|
|
|
|
can_dig = function(pos, player)
|
|
|
|
local meta = minetest.get_meta(pos);
|
2013-01-19 15:37:27 +01:00
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return (inv:is_empty("src") and inv:is_empty("recipe") and inv:is_empty("dst"))
|
2013-10-04 17:03:27 +02:00
|
|
|
end,
|
2013-10-04 17:56:03 +02:00
|
|
|
after_place_node = tube_scanforobjects(pos),
|
|
|
|
after_dig_node = function(pos)
|
|
|
|
tube_scanforobjects(pos)
|
|
|
|
autocrafterCache[minetest.hash_node_position(pos)] = nil
|
|
|
|
end
|
rewrote autoplacement code to make it more aware of filters,
autocrafter, nodebreaker, deployer, and made sure each item will only
initiate a connection to those sides which can accept such. Fixed
various autorouting bugs as I ran across them. Autorouting for various
devices is now:
filters: left and right sides only
nodebreaker, deployer: back only
autocrafter: all six sides
chests: top, bottom, left, right, back (not front)
furnace: bottom, left, right, back (not the top or front)
2013-06-24 03:36:13 +02:00
|
|
|
})
|
2013-01-19 15:37:27 +01:00
|
|
|
|
2013-10-04 17:03:27 +02:00
|
|
|
minetest.register_abm({nodenames = {"pipeworks:autocrafter"}, interval = 1, chance = 1,
|
|
|
|
action = function(pos, node)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
autocraft(inv, pos)
|
rewrote autoplacement code to make it more aware of filters,
autocrafter, nodebreaker, deployer, and made sure each item will only
initiate a connection to those sides which can accept such. Fixed
various autorouting bugs as I ran across them. Autorouting for various
devices is now:
filters: left and right sides only
nodebreaker, deployer: back only
autocrafter: all six sides
chests: top, bottom, left, right, back (not front)
furnace: bottom, left, right, back (not the top or front)
2013-06-24 03:36:13 +02:00
|
|
|
end
|
|
|
|
})
|