summaryrefslogtreecommitdiff
path: root/autocrafter.lua
diff options
context:
space:
mode:
authorTim <t4im@users.noreply.github.com>2015-01-26 22:04:56 +0100
committerTim <t4im@users.noreply.github.com>2015-01-27 23:41:46 +0100
commit2ccce52976a9c11313486ef3602c002b757f0dbe (patch)
treeba118daf448bd4edd3af88a40e7fb371816a8dd7 /autocrafter.lua
parentf427bae557f8376e51d296f871f33d56dc541e12 (diff)
downloadpipeworks-2ccce52976a9c11313486ef3602c002b757f0dbe.tar
pipeworks-2ccce52976a9c11313486ef3602c002b757f0dbe.tar.gz
pipeworks-2ccce52976a9c11313486ef3602c002b757f0dbe.tar.bz2
pipeworks-2ccce52976a9c11313486ef3602c002b757f0dbe.tar.xz
pipeworks-2ccce52976a9c11313486ef3602c002b757f0dbe.zip
split autocraft into a run function and a function autocrafting a singe step
this gives us flexibility for future changes
Diffstat (limited to 'autocrafter.lua')
-rw-r--r--autocrafter.lua38
1 files changed, 24 insertions, 14 deletions
diff --git a/autocrafter.lua b/autocrafter.lua
index 0fc5c78..3579e8b 100644
--- a/autocrafter.lua
+++ b/autocrafter.lua
@@ -61,25 +61,16 @@ local function update_autocrafter(pos)
end
end
-local function autocraft(inventory, pos)
- if not inventory then return end
- local recipe = inventory:get_list("recipe")
- if not recipe then return end
-
- local hash, 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
-
+local function autocraft(inventory, craft)
local output_item = craft.output.item
- if output_item:is_empty() or not inventory:room_for_item("dst", output_item) then return end
+ -- check if we have enough room in dst
+ if not inventory:room_for_item("dst", output_item) then return false end
local consumption = craft.consumption
local inv_index = count_index(inventory:get_list("src"))
-- check if we have enough materials available
for itemname, number in pairs(consumption) do
- if (not inv_index[itemname]) or inv_index[itemname] < number then return end
+ if (not inv_index[itemname]) or inv_index[itemname] < number then return false end
end
-- consume materials
@@ -94,6 +85,25 @@ local function autocraft(inventory, pos)
for i = 1, 9 do
inventory:add_item("dst", craft.decremented_input.items[i])
end
+ 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)
end
minetest.register_node("pipeworks:autocrafter", {
@@ -191,6 +201,6 @@ minetest.register_abm({nodenames = {"pipeworks:autocrafter"}, interval = 1, chan
action = function(pos, node)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
- autocraft(inv, pos)
+ run_autocrafter(inv, pos)
end
})