Skip to content

Commit

Permalink
Add technic.process_recipe() function (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
OgelGames authored Feb 6, 2024
1 parent ebe381c commit 04b0663
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 35 deletions.
13 changes: 13 additions & 0 deletions technic/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,16 @@ function technic.can_insert_unique_stack(pos, node, incoming_stack, direction)
end
return technic.default_can_insert(pos, node, incoming_stack, direction)
end

function technic.process_recipe(recipe, inv)
local dst_copy = inv:get_list("dst")
for _,item in ipairs(recipe.output) do
if not inv:room_for_item("dst", ItemStack(item)) then
inv:set_list("dst", dst_copy)
return false
end
inv:add_item("dst", item)
end
inv:set_list("src", recipe.new_input)
return true
end
16 changes: 5 additions & 11 deletions technic/machines/other/coal_alloy_furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,18 @@ minetest.register_abm({
end

-- Get what to cook if anything
local result = technic.get_recipe("alloy", inv:get_list("src"))
local recipe = technic.get_recipe("alloy", inv:get_list("src"))

local was_active = false

if meta:get_float("fuel_time") < meta:get_float("fuel_totaltime") then
was_active = true
meta:set_int("fuel_time", meta:get_int("fuel_time") + 1)
if result then
if recipe then
meta:set_int("src_time", meta:get_int("src_time") + 1)
if meta:get_int("src_time") >= result.time then
if meta:get_int("src_time") >= recipe.time then
meta:set_int("src_time", 0)
local result_stack = ItemStack(result.output)
if inv:room_for_item("dst", result_stack) then
inv:set_list("src", result.new_input)
inv:add_item("dst", result_stack)
end
technic.process_recipe(recipe, inv)
end
else
meta:set_int("src_time", 0)
Expand Down Expand Up @@ -175,9 +171,7 @@ minetest.register_abm({
return
end

local recipe = technic.get_recipe("alloy", inv:get_list("src"))

if not recipe then
if not technic.get_recipe("alloy", inv:get_list("src")) then
if was_active then
meta:set_string("infotext", S("@1 is empty", machine_name))
technic.swap_node(pos, "technic:coal_alloy_furnace")
Expand Down
30 changes: 6 additions & 24 deletions technic/machines/register/machine_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ function technic.register_base_machine(nodename, data)
meta:set_int("src_time", meta:get_int("src_time") + round(def.speed*10))
end
while true do
local result = inv:get_list("src") and technic.get_recipe(typename, inv:get_list("src"))
if not result then
local recipe = inv:get_list("src") and technic.get_recipe(typename, inv:get_list("src"))
if not recipe then
technic.swap_node(pos, nodename)
meta:set_string("infotext", infotext_idle)
meta:set_int(tier.."_EU_demand", 0)
Expand All @@ -144,39 +144,21 @@ function technic.register_base_machine(nodename, data)
technic.swap_node(pos, nodename.."_active")
meta:set_string("infotext", infotext_active .. "\n" ..
S("Demand: @1", technic.EU_string(machine_demand[EU_upgrade+1])))
if meta:get_int("src_time") < round(result.time*10) then
if meta:get_int("src_time") < round(recipe.time*10) then
if not powered then
technic.swap_node(pos, nodename)
meta:set_string("infotext", infotext_unpowered)
end
return
end
local output = result.output
if type(output) ~= "table" then output = { output } end
local output_stacks = {}
for _, o in ipairs(output) do
table.insert(output_stacks, ItemStack(o))
end
local room_for_output = true
inv:set_size("dst_tmp", inv:get_size("dst"))
inv:set_list("dst_tmp", inv:get_list("dst"))
for _, o in ipairs(output_stacks) do
if not inv:room_for_item("dst_tmp", o) then
room_for_output = false
break
end
inv:add_item("dst_tmp", o)
end
if not room_for_output then
if not technic.process_recipe(recipe, inv) then
technic.swap_node(pos, nodename)
meta:set_string("infotext", infotext_idle)
meta:set_int(tier.."_EU_demand", 0)
meta:set_int("src_time", round(result.time*10))
meta:set_int("src_time", round(recipe.time*10))
return
end
meta:set_int("src_time", meta:get_int("src_time") - round(result.time*10))
inv:set_list("src", result.new_input)
inv:set_list("dst", inv:get_list("dst_tmp"))
meta:set_int("src_time", meta:get_int("src_time") - round(recipe.time*10))
end
end

Expand Down

0 comments on commit 04b0663

Please sign in to comment.