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
|
|
|
|
2015-01-26 21:27:47 +01:00
|
|
|
local function count_index(invlist)
|
|
|
|
local index = {}
|
2013-10-30 08:33:09 +01:00
|
|
|
for _, stack in ipairs(invlist) do
|
2015-01-26 21:27:47 +01:00
|
|
|
local stack_name = stack:get_name()
|
|
|
|
index[stack_name] = (index[stack_name] or 0) + stack:get_count()
|
2013-10-30 08:33:09 +01:00
|
|
|
end
|
2015-01-26 21:27:47 +01:00
|
|
|
return index
|
2013-10-30 08:33:09 +01:00
|
|
|
end
|
|
|
|
|
2015-01-26 18:34:05 +01:00
|
|
|
local function get_cached_craft(pos)
|
|
|
|
local hash = minetest.hash_node_position(pos)
|
|
|
|
return hash, autocrafterCache[hash]
|
|
|
|
end
|
|
|
|
|
2015-01-26 21:27:47 +01:00
|
|
|
-- note, that this function assumes allready being updated to virtual items
|
|
|
|
-- and doesn't handle recipes with stacksizes > 1
|
|
|
|
local function on_recipe_change(pos, inventory)
|
2015-01-10 07:31:22 +01:00
|
|
|
if not inventory then return end
|
2013-10-04 17:03:27 +02:00
|
|
|
local recipe = inventory:get_list("recipe")
|
2015-01-10 07:31:22 +01:00
|
|
|
if not recipe then return end
|
2013-10-04 16:41:23 +02:00
|
|
|
|
2015-01-26 21:27:47 +01:00
|
|
|
local recipe_changed = false
|
2015-01-26 18:34:05 +01:00
|
|
|
local hash, craft = get_cached_craft(pos)
|
2015-01-26 21:27:47 +01:00
|
|
|
|
|
|
|
if not craft then
|
|
|
|
recipe_changed = true
|
2013-10-30 08:33:09 +01:00
|
|
|
else
|
2015-01-26 21:27:47 +01:00
|
|
|
-- check if it changed
|
|
|
|
local cached_recipe = craft.recipe
|
2013-10-30 08:33:09 +01:00
|
|
|
for i = 1, 9 do
|
2015-01-26 21:27:47 +01:00
|
|
|
if recipe[i]:get_name() ~= cached_recipe[i]:get_name() then
|
2015-01-26 18:34:05 +01:00
|
|
|
recipe_changed = true
|
2013-10-30 08:33:09 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-04 16:41:23 +02:00
|
|
|
|
2015-01-26 21:27:47 +01:00
|
|
|
if recipe_changed then
|
|
|
|
local output, decremented_input = minetest.get_craft_result({method = "normal", width = 3, items = recipe})
|
2015-01-26 21:34:32 +01:00
|
|
|
craft = {recipe = recipe, consumption=count_index(recipe), output = output, decremented_input = decremented_input}
|
2015-01-26 21:27:47 +01:00
|
|
|
autocrafterCache[hash] = craft
|
2013-01-20 07:57:58 +01:00
|
|
|
end
|
2015-01-26 21:27:47 +01:00
|
|
|
|
|
|
|
return craft
|
2013-01-19 15:37:27 +01:00
|
|
|
end
|
|
|
|
|
2014-11-02 09:35:43 +01:00
|
|
|
local function update_autocrafter(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if meta:get_string("virtual_items") == "" then
|
|
|
|
meta:set_string("virtual_items", "1")
|
|
|
|
local inv = meta:get_inventory()
|
2015-01-26 19:34:43 +01:00
|
|
|
for idx, stack in ipairs(inv:get_list("recipe")) do
|
2014-11-02 09:35:43 +01:00
|
|
|
minetest.item_drop(stack, "", pos)
|
2015-01-26 19:34:43 +01:00
|
|
|
stack:set_count(1)
|
|
|
|
stack:set_wear(0)
|
|
|
|
inv:set_stack("recipe", idx, stack)
|
2014-11-02 09:35:43 +01:00
|
|
|
end
|
2015-01-26 21:27:47 +01:00
|
|
|
on_recipe_change(pos, inv)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-26 22:04:56 +01:00
|
|
|
local function autocraft(inventory, craft)
|
2015-01-26 21:27:47 +01:00
|
|
|
local output_item = craft.output.item
|
2015-01-26 22:04:56 +01:00
|
|
|
-- check if we have enough room in dst
|
|
|
|
if not inventory:room_for_item("dst", output_item) then return false end
|
2015-01-26 21:27:47 +01:00
|
|
|
|
2015-01-26 21:34:32 +01:00
|
|
|
local consumption = craft.consumption
|
2015-01-26 21:27:47 +01:00
|
|
|
local inv_index = count_index(inventory:get_list("src"))
|
|
|
|
-- check if we have enough materials available
|
|
|
|
for itemname, number in pairs(consumption) do
|
2015-01-26 22:04:56 +01:00
|
|
|
if (not inv_index[itemname]) or inv_index[itemname] < number then return false end
|
2015-01-26 21:27:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- consume materials
|
|
|
|
for itemname, number in pairs(consumption) do
|
|
|
|
for i = 1, number do -- We have to do that since remove_item does not work if count > stack_max
|
|
|
|
inventory:remove_item("src", ItemStack(itemname))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- craft the result into the dst inventory and add any "replacements" as well
|
|
|
|
inventory:add_item("dst", output_item)
|
|
|
|
for i = 1, 9 do
|
|
|
|
inventory:add_item("dst", craft.decremented_input.items[i])
|
2014-11-02 09:35:43 +01:00
|
|
|
end
|
2015-01-26 22:04:56 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function run_autocrafter(inventory, pos)
|
|
|
|
if not inventory then return end
|
|
|
|
local recipe = inventory:get_list("recipe")
|
|
|
|
if not recipe then return end
|
|
|
|
|
|
|
|
local _, craft = get_cached_craft(pos)
|
|
|
|
if craft == nil then
|
|
|
|
update_autocrafter(pos) -- only does some unnecessary calls for "old" autocrafters
|
|
|
|
craft = on_recipe_change(pos, inventory)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- skip crafts that have no output
|
|
|
|
local output_item = craft.output.item
|
|
|
|
if output_item:is_empty() then return end
|
|
|
|
|
|
|
|
autocraft(inventory, craft)
|
2014-11-02 09:35:43 +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")
|
2014-11-02 09:35:43 +01:00
|
|
|
meta:set_string("virtual_items", "1")
|
2013-01-19 15:37:27 +01:00
|
|
|
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)
|
2014-11-02 09:35:43 +01:00
|
|
|
end,
|
|
|
|
on_punch = update_autocrafter,
|
2013-10-04 17:03:27 +02:00
|
|
|
can_dig = function(pos, player)
|
2014-11-02 09:35:43 +01:00
|
|
|
update_autocrafter(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2013-01-19 15:37:27 +01:00
|
|
|
local inv = meta:get_inventory()
|
2014-11-02 09:35:43 +01:00
|
|
|
return (inv:is_empty("src") and inv:is_empty("dst"))
|
2013-10-04 17:03:27 +02:00
|
|
|
end,
|
2013-10-04 20:36:17 +02:00
|
|
|
after_place_node = function(pos)
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.scan_for_tube_objects(pos)
|
2013-10-04 20:36:17 +02:00
|
|
|
end,
|
2013-10-04 17:56:03 +02:00
|
|
|
after_dig_node = function(pos)
|
2013-12-15 08:53:10 +01:00
|
|
|
pipeworks.scan_for_tube_objects(pos)
|
2013-10-04 20:36:17 +02:00
|
|
|
autocrafterCache[minetest.hash_node_position(pos)] = nil
|
2014-11-02 09:35:43 +01:00
|
|
|
end,
|
|
|
|
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
|
|
|
update_autocrafter(pos)
|
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
if listname == "recipe" then
|
|
|
|
local stack_copy = ItemStack(stack)
|
|
|
|
stack_copy:set_count(1)
|
|
|
|
inv:set_stack(listname, index, stack_copy)
|
2015-01-26 21:27:47 +01:00
|
|
|
on_recipe_change(pos, inv)
|
2014-11-02 09:35:43 +01:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
|
|
|
update_autocrafter(pos)
|
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
if listname == "recipe" then
|
|
|
|
inv:set_stack(listname, index, ItemStack(""))
|
2015-01-26 21:27:47 +01:00
|
|
|
on_recipe_change(pos, inv)
|
2014-11-02 09:35:43 +01:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
update_autocrafter(pos)
|
|
|
|
local inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
local stack = inv:get_stack(from_list, from_index)
|
|
|
|
stack:set_count(count)
|
|
|
|
if from_list == "recipe" then
|
|
|
|
inv:set_stack(from_list, from_index, ItemStack(""))
|
2015-01-26 21:27:47 +01:00
|
|
|
on_recipe_change(pos, inv)
|
2014-11-02 09:35:43 +01:00
|
|
|
return 0
|
|
|
|
elseif to_list == "recipe" then
|
|
|
|
local stack_copy = ItemStack(stack)
|
|
|
|
stack_copy:set_count(1)
|
|
|
|
inv:set_stack(to_list, to_index, stack_copy)
|
2015-01-26 21:27:47 +01:00
|
|
|
on_recipe_change(pos, inv)
|
2014-11-02 09:35:43 +01:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
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()
|
2015-01-26 22:04:56 +01:00
|
|
|
run_autocrafter(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
|
|
|
|
})
|