biogasmachines: freezer: processing logic improvements, bugfix.

Processing logic adapted from updated gasfurnace code (minus fuel).
Fixed checking for space in output tray for items with extra leftover
material (coal).

Signed-off-by: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
This commit is contained in:
Michal Cieslakiewicz 2019-01-29 22:41:30 +01:00
parent 922a3fd6c7
commit d3dfd92d46

View File

@ -37,6 +37,10 @@
* keep_running function is called every time item is produced * keep_running function is called every time item is produced
(not every processing tick - function does not accept neither 0 (not every processing tick - function does not accept neither 0
nor fractional values for num_items parameter) nor fractional values for num_items parameter)
* desired_state metadata allows to properly change non-running target
state during transition; when new state differs from old one, timer
is reset so it is guaranteed that each countdown starts from
COUNTDOWN_TICKS
License: LGPLv2.1+ License: LGPLv2.1+
======================================================================= =======================================================================
@ -224,7 +228,7 @@ local function get_input_item(inv, listname)
end end
-- reset processing data -- reset processing data
local function state_meta_reset(pos, meta, oldstate) local function state_meta_reset(pos, meta)
meta:set_int("item_ticks", -1) meta:set_int("item_ticks", -1)
meta:set_string("item_name", "") meta:set_string("item_name", "")
end end
@ -244,11 +248,24 @@ local machine = tubelib.NodeStates:new({
standby_ticks = STANDBY_TICKS, standby_ticks = STANDBY_TICKS,
has_item_meter = true, has_item_meter = true,
aging_factor = 8, aging_factor = 8,
on_start = state_meta_reset, on_start = function(pos, meta, oldstate)
on_stop = state_meta_reset, meta:set_int("desired_state", tubelib.RUNNING)
state_meta_reset(pos, meta)
end,
on_stop = function(pos, meta, oldstate)
meta:set_int("desired_state", tubelib.STOPPED)
state_meta_reset(pos, meta)
end,
formspec_func = formspec, formspec_func = formspec,
}) })
-- fault function for convenience as there is no on_fault method (yet)
local function machine_fault(pos, meta)
meta:set_int("desired_state", tubelib.FAULT)
state_meta_reset(pos, meta)
machine:fault(pos, meta)
end
-- customized version of NodeStates:idle() -- customized version of NodeStates:idle()
local function countdown_to_halt(pos, meta, target_state) local function countdown_to_halt(pos, meta, target_state)
if target_state ~= tubelib.STANDBY and if target_state ~= tubelib.STANDBY and
@ -257,23 +274,33 @@ local function countdown_to_halt(pos, meta, target_state)
target_state ~= tubelib.FAULT then target_state ~= tubelib.FAULT then
return true return true
end end
local countdown = meta:get_int("tubelib_countdown") if machine:get_state(meta) == tubelib.RUNNING and
if countdown > 0 then meta:get_int("desired_state") ~= target_state then
countdown = countdown - 1 meta:set_int("tubelib_countdown", COUNTDOWN_TICKS)
meta:set_int("desired_state", target_state)
end
local countdown = meta:get_int("tubelib_countdown") - 1
if countdown >= -1 then
-- we don't need anything less than -1
meta:set_int("tubelib_countdown", countdown) meta:set_int("tubelib_countdown", countdown)
if countdown == 0 then end
if target_state == tubelib.FAULT then if countdown < 0 then
state_meta_reset(pos, meta) if machine:get_state(meta) == target_state then
machine:fault(pos, meta) return true
elseif target_state == tubelib.STOPPED then
machine:stop(pos, meta)
elseif target_state == tubelib.BLOCKED then
machine:blocked(pos, meta)
else
machine:standby(pos, meta)
end
return false
end end
meta:set_int("desired_state", target_state)
-- workaround for switching between non-running states
meta:set_int("tubelib_state", tubelib.RUNNING)
if target_state == tubelib.FAULT then
machine_fault(pos, meta)
elseif target_state == tubelib.STOPPED then
machine:stop(pos, meta)
elseif target_state == tubelib.BLOCKED then
machine:blocked(pos, meta)
else
machine:standby(pos, meta)
end
return false
end end
return true return true
end end
@ -383,8 +410,6 @@ local function on_timer(pos, elapsed)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local label = minetest.registered_nodes[node.name].description
local number = meta:get_string("tubelib_number")
local itemcnt = meta:get_int("item_ticks") local itemcnt = meta:get_int("item_ticks")
local itemname = meta:get_string("item_name") local itemname = meta:get_string("item_name")
if itemcnt < 0 or itemname == "" then if itemcnt < 0 or itemname == "" then
@ -406,25 +431,27 @@ local function on_timer(pos, elapsed)
local inp = inv:get_stack("cur", 1) local inp = inv:get_stack("cur", 1)
inputname = inp:get_name() inputname = inp:get_name()
if not biogas_recipes[inputname] then if not biogas_recipes[inputname] then
state_meta_reset(pos, meta) machine_fault(pos, meta) -- oops
machine:fault(pos, meta) -- oops
return false return false
end end
prodtime = biogas_recipes[inputname].time prodtime = biogas_recipes[inputname].time
else else
-- prepare item, next tick will start processing -- prepare item, next tick will start processing
inv:set_list("dst_copy", inv:get_list("dst"))
for i, r in pairs(biogas_recipes) do for i, r in pairs(biogas_recipes) do
if inv:contains_item("src", ItemStack(i .. " 1")) and if inv:contains_item("src", ItemStack(i .. " 1")) then
inv:room_for_item("dst", local outp0 = inv:add_item("dst_copy",
ItemStack("tubelib_addons1:biogas " .. ItemStack("tubelib_addons1:biogas " .. tostring(r.count)))
tostring(r.count))) and local outp1 = r.extra and inv:add_item("dst_copy", r.extra) or
(not r.extra or inv:room_for_item("dst", r.extra)) ItemStack({})
then if outp0:is_empty() and outp1:is_empty() then
inputname = i inputname = i
prodtime = r.time prodtime = r.time
break break
end
end end
end end
inv:set_size("dst_copy", 0)
if not inputname then if not inputname then
-- no space in output -- no space in output
return countdown_to_halt(pos, meta, tubelib.BLOCKED) return countdown_to_halt(pos, meta, tubelib.BLOCKED)
@ -435,8 +462,7 @@ local function on_timer(pos, elapsed)
end end
local inp = inv:remove_item("src", ItemStack(inputname .. " 1")) local inp = inv:remove_item("src", ItemStack(inputname .. " 1"))
if inp:is_empty() then if inp:is_empty() then
state_meta_reset(pos, meta) machine_fault(pos, meta) -- oops
machine:fault(pos, meta) -- oops
return false return false
end end
inv:add_item("cur", inp) inv:add_item("cur", inp)
@ -447,8 +473,7 @@ local function on_timer(pos, elapsed)
-- production -- production
if machine:get_state(meta) ~= tubelib.RUNNING then if machine:get_state(meta) ~= tubelib.RUNNING then
-- exception, should not happen - oops -- exception, should not happen - oops
state_meta_reset(pos, meta) machine_fault(pos, meta)
machine:fault(pos, meta)
return false return false
end end
-- add item tick -- add item tick
@ -470,6 +495,7 @@ local function on_timer(pos, elapsed)
end end
end end
meta:set_int("tubelib_countdown", COUNTDOWN_TICKS) meta:set_int("tubelib_countdown", COUNTDOWN_TICKS)
meta:set_int("desired_state", tubelib.RUNNING)
meta:set_string("formspec", formspec(machine, pos, meta)) meta:set_string("formspec", formspec(machine, pos, meta))
return true return true
end end