biogasmachines: compactor: Tubelib v2 adaptation, logic improvements.
Code updated to make machine fully compatible with Tubelib2 framework. Major changes: * supports Tubelib2 by using NodeStates class and new metadata * BLOCKED state introduced when no space left in output tray * set to FAULT when Biogas tank is empty * on_punch diagnostics removed * processing logic updated (fuel and ice checks reorganized, desired_state metadata introduced, improved state transition logic) Signed-off-by: Michal Cieslakiewicz <michal.cieslakiewicz@wp.pl>
This commit is contained in:
parent
9ea6941042
commit
ab24a65114
2
TODO
2
TODO
@ -1,5 +1,3 @@
|
||||
make remaining Biogas Machines compatible with Tubelib2:
|
||||
* compactor
|
||||
|
||||
change slprogtools:
|
||||
* read "tubelib_number" instead of "number" metadata
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
=======================================================================
|
||||
Tubelib Biogas Machines Mod
|
||||
by Micu (c) 2018
|
||||
by Micu (c) 2018, 2019
|
||||
|
||||
Copyright (C) 2018 Michal Cieslakiewicz
|
||||
Copyright (C) 2018, 2019 Michal Cieslakiewicz
|
||||
|
||||
Compactor is a heavy mechanical press with heating, compacting and
|
||||
cooling systems combined into one device. It compresses stone-like
|
||||
@ -16,10 +16,21 @@
|
||||
|
||||
Operational info:
|
||||
* machine requires Biogas as fuel (one unit lasts for 12 seconds) and
|
||||
Ice for cooling (one ice cube per source item)
|
||||
* if there is nothing to process or there is no ice, machine enters
|
||||
standby mode; it will automatically pick up work as soon as any valid
|
||||
item is loaded into input (source) tray and ice is available
|
||||
ice for cooling (one ice cube per compaction process)
|
||||
* if there is nothing to process but there is still Biogas in tank,
|
||||
machine enters standby mode; it will automatically pick up work as
|
||||
soon as any valid item is loaded into input (source) tray and ice
|
||||
is available
|
||||
* machine also enters standby mode if ice tray becomes empty;
|
||||
production resumes automatically as soon as ice tray is loaded again
|
||||
* if there is nothing to compact in source tray and Biogas tank is
|
||||
empty, machine switches off automatically
|
||||
* when fuel ends and there are still source items waiting in source
|
||||
tray, machine enters fault mode and has to be manually powered on
|
||||
again after refilling Biogas
|
||||
* if output tray is full and no new items can be put there, machine
|
||||
changes state to blocked (special standby mode); it will resume
|
||||
work as soon as there is space in output inventory
|
||||
* there is 1 tick gap between processing items to perform machinery
|
||||
cleaning and reload working trays; this is a design choice
|
||||
* working trays can only be emptied when machine is stopped; these trays
|
||||
@ -33,6 +44,21 @@
|
||||
* device always checks if expected products will fit output tray so
|
||||
some input items may be omitted temporarily
|
||||
|
||||
Tubelib v2 implementation info:
|
||||
* device updates itself every tick, so cycle_time must be set to 1
|
||||
even though production takes longer (start method sets timer to
|
||||
this value)
|
||||
* keep_running function is called every time item is produced
|
||||
(not every processing tick - function does not accept neither 0
|
||||
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
|
||||
* num_items in keep_running method is set to 1 (default value);
|
||||
machine aging is controlled by aging_factor solely; tubelib item
|
||||
counter is used to count production iterations not actual items
|
||||
|
||||
License: LGPLv2.1+
|
||||
=======================================================================
|
||||
|
||||
@ -45,10 +71,12 @@
|
||||
]]--
|
||||
|
||||
-- Biogas time in ticks
|
||||
local BIOGAS_WORK_TIME = 12
|
||||
local BIOGAS_TICKS = 12
|
||||
-- timing
|
||||
local TIMER_TICK_SEC = 1 -- Node timer tick
|
||||
local TICKS_TO_SLEEP = 5 -- Tubelib standby
|
||||
local TIMER_TICK_SEC = 1 -- Node timer tick
|
||||
local STANDBY_TICKS = 4 -- Standby mode timer frequency factor
|
||||
local COUNTDOWN_TICKS = 4 -- Ticks to standby
|
||||
|
||||
-- machine inventory
|
||||
local INV_H = 3 -- Inventory height (do not change)
|
||||
local INV_IN_W = 3 -- Input inventory width
|
||||
@ -119,11 +147,11 @@ local fmxy = {
|
||||
mid_x2 = tostring(INV_IN_W + 2), -- (3rd col of mid panel)
|
||||
inv_out_x = tostring(INV_IN_W + 3), -- (1st col of dst inv)
|
||||
inv_out_w = tostring(INV_OUT_W),
|
||||
biogas_time = tostring(BIOGAS_WORK_TIME * TIMER_TICK_SEC),
|
||||
biogas_time = tostring(BIOGAS_TICKS * TIMER_TICK_SEC),
|
||||
}
|
||||
|
||||
-- recipe hint
|
||||
local function formspec_recipe_hint_bar(recipe_idx, show_icons)
|
||||
-- recipe hint bar
|
||||
local function formspec_recipe_hint_bar(recipe_idx)
|
||||
if #hintbar_recipes == 0 or recipe_idx > #hintbar_recipes then
|
||||
return ""
|
||||
end
|
||||
@ -132,16 +160,12 @@ local function formspec_recipe_hint_bar(recipe_idx, show_icons)
|
||||
local output_name = recipe.output:get_name()
|
||||
local input_count = tostring(recipe.input:get_count())
|
||||
local output_count = tostring(recipe.output:get_count())
|
||||
--local input_desc = minetest.registered_nodes[input_name].description
|
||||
--local output_desc = minetest.registered_nodes[output_name].description
|
||||
return (show_icons and "item_image[0,0;1,1;" .. input_name .. "]" ..
|
||||
"item_image[" .. fmxy.inv_out_x .. ",0;1,1;" .. output_name ..
|
||||
"]" or "") ..
|
||||
return "item_image[0,0;1,1;" .. input_name .. "]" .. "item_image[" ..
|
||||
fmxy.inv_out_x .. ",0;1,1;" .. output_name .. "]" ..
|
||||
"label[0,3.25;Recipe]" ..
|
||||
"image_button[0.8,3.3;0.5,0.5;;left;<]" ..
|
||||
"label[1.2,3.25;" ..
|
||||
string.format("%2d / %2d", recipe_idx, #hintbar_recipes) ..
|
||||
"]" ..
|
||||
string.format("%2d / %2d", recipe_idx, #hintbar_recipes) .. "]" ..
|
||||
"image_button[1.9,3.3;0.5,0.5;;right;>]" ..
|
||||
"item_image[2.4,3.25;0.5,0.5;" .. input_name .. "]" ..
|
||||
"label[2.9,3.25;x " .. input_count .. "]" ..
|
||||
@ -156,14 +180,12 @@ local function formspec_recipe_hint_bar(recipe_idx, show_icons)
|
||||
"label[7,3.25;= " .. fmxy.biogas_time .. " sec]"
|
||||
end
|
||||
|
||||
-- Parameters:
|
||||
-- state - tubelib state
|
||||
-- fuel_percent - biogas used
|
||||
-- item_percent - item completion
|
||||
-- recipe_idx - index of recipe shown at hint bar
|
||||
-- show_icons - show image hints (bool)
|
||||
local function formspec(state, fuel_percent, item_percent,
|
||||
recipe_idx, show_icons)
|
||||
-- formspec
|
||||
local function formspec(self, pos, meta)
|
||||
local state = meta:get_int("tubelib_state")
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
local fuel_pct = tostring(100 * meta:get_int("fuel_ticks") / BIOGAS_TICKS)
|
||||
local item_pct = tostring(100 * (1 - meta:get_int("item_ticks") / meta:get_int("item_total")))
|
||||
return "size[8,8.25]" ..
|
||||
default.gui_bg ..
|
||||
default.gui_bg_img ..
|
||||
@ -173,22 +195,19 @@ local function formspec(state, fuel_percent, item_percent,
|
||||
"list[context;cic;" .. fmxy.mid_x15 .. ",0;1,1;]" ..
|
||||
"image[" .. fmxy.mid_x05 ..
|
||||
",1;1,1;biogasmachines_compactor_inv_bg.png^[lowpart:" ..
|
||||
tostring(fuel_percent) ..
|
||||
":biogasmachines_compactor_inv_fg.png]" ..
|
||||
fuel_pct .. ":biogasmachines_compactor_inv_fg.png]" ..
|
||||
"image[" .. fmxy.mid_x15 ..
|
||||
",1;1,1;gui_furnace_arrow_bg.png^[lowpart:" ..
|
||||
tostring(item_percent) ..
|
||||
":gui_furnace_arrow_fg.png^[transformR270]" ..
|
||||
item_pct .. ":gui_furnace_arrow_fg.png^[transformR270]" ..
|
||||
"list[context;fuel;" .. fmxy.inv_in_w .. ",2;1,1;]" ..
|
||||
"list[context;ice;" .. fmxy.mid_x2 .. ",2;1,1;]" ..
|
||||
(show_icons and "item_image[" .. fmxy.inv_in_w ..
|
||||
",2;1,1;tubelib_addons1:biogas]" ..
|
||||
"item_image[" .. fmxy.mid_x2 .. ",2;1,1;default:ice]" or "") ..
|
||||
"item_image[" .. fmxy.inv_in_w .. ",2;1,1;tubelib_addons1:biogas]" ..
|
||||
"item_image[" .. fmxy.mid_x2 .. ",2;1,1;default:ice]" ..
|
||||
"image_button[" .. fmxy.mid_x1 .. ",2;1,1;" ..
|
||||
tubelib.state_button(state) .. ";button;]" ..
|
||||
formspec_recipe_hint_bar(recipe_idx, show_icons) ..
|
||||
self:get_state_button_image(meta) .. ";state_button;]" ..
|
||||
formspec_recipe_hint_bar(recipe_idx) ..
|
||||
"list[context;dst;" .. fmxy.inv_out_x .. ",0;" .. fmxy.inv_out_w ..
|
||||
"," .. fmxy.inv_h .. ";]" ..
|
||||
"," .. fmxy.inv_h .. ";]" ..
|
||||
"list[current_player;main;0,4;8,1;]" ..
|
||||
"list[current_player;main;0,5.25;8,3;8]" ..
|
||||
"listring[context;dst]" ..
|
||||
@ -213,103 +232,105 @@ end
|
||||
-------
|
||||
]]--
|
||||
|
||||
local function compactor_start(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local number = meta:get_string("number")
|
||||
local fuel = meta:get_int("fuel_ticks")
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
local label = minetest.registered_nodes[node.name].description
|
||||
-- reset processing data
|
||||
local function state_meta_reset(pos, meta)
|
||||
meta:set_int("item_ticks", -1)
|
||||
meta:set_int("running", TICKS_TO_SLEEP)
|
||||
meta:set_string("infotext", label .. " " .. number .. ": running")
|
||||
meta:set_string("formspec", formspec(tubelib.RUNNING,
|
||||
100 * fuel / BIOGAS_WORK_TIME, 0, recipe_idx, true))
|
||||
node.name = "biogasmachines:compactor_active"
|
||||
minetest.swap_node(pos, node)
|
||||
minetest.get_node_timer(pos):start(TIMER_TICK_SEC)
|
||||
return false
|
||||
meta:set_int("item_total", -1)
|
||||
end
|
||||
|
||||
local function compactor_stop(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local number = meta:get_string("number")
|
||||
local fuel = meta:get_int("fuel_ticks")
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
local label = minetest.registered_nodes[node.name].description
|
||||
meta:set_int("item_ticks", -1)
|
||||
meta:set_int("running", tubelib.STATE_STOPPED)
|
||||
meta:set_string("infotext", label .. " " .. number .. ": stopped")
|
||||
meta:set_string("formspec", formspec(tubelib.STOPPED,
|
||||
100 * fuel / BIOGAS_WORK_TIME, 0, recipe_idx, true))
|
||||
node.name = "biogasmachines:compactor"
|
||||
minetest.swap_node(pos, node)
|
||||
minetest.get_node_timer(pos):stop()
|
||||
return false
|
||||
--[[
|
||||
-------------
|
||||
State machine
|
||||
-------------
|
||||
]]--
|
||||
|
||||
local machine = tubelib.NodeStates:new({
|
||||
node_name_passive = "biogasmachines:compactor",
|
||||
node_name_active = "biogasmachines:compactor_active",
|
||||
node_name_defect = "biogasmachines:compactor_defect",
|
||||
infotext_name = "Tubelib Compactor",
|
||||
cycle_time = TIMER_TICK_SEC,
|
||||
standby_ticks = STANDBY_TICKS,
|
||||
has_item_meter = true, -- used for production iterations actually
|
||||
aging_factor = 24,
|
||||
on_start = function(pos, meta, oldstate)
|
||||
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,
|
||||
})
|
||||
|
||||
-- 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
|
||||
|
||||
local function compactor_idle(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local number = meta:get_string("number")
|
||||
local fuel = meta:get_int("fuel_ticks")
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
local label = minetest.registered_nodes[node.name].description
|
||||
meta:set_int("item_ticks", -1)
|
||||
meta:set_int("running", tubelib.STATE_STANDBY)
|
||||
meta:set_string("infotext", label .. " " .. number .. ": standby")
|
||||
meta:set_string("formspec", formspec(tubelib.STANDBY,
|
||||
100 * fuel / BIOGAS_WORK_TIME, 0, recipe_idx, true))
|
||||
node.name = "biogasmachines:compactor"
|
||||
minetest.swap_node(pos, node)
|
||||
minetest.get_node_timer(pos):start(TIMER_TICK_SEC * TICKS_TO_SLEEP)
|
||||
return false
|
||||
end
|
||||
|
||||
local function compactor_fault(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local number = meta:get_string("number")
|
||||
local fuel = meta:get_int("fuel_ticks")
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
local label = minetest.registered_nodes[node.name].description
|
||||
meta:set_int("item_ticks", -1)
|
||||
meta:set_int("running", tubelib.STATE_FAULT)
|
||||
meta:set_string("infotext", label .. " " .. number .. ": fault")
|
||||
meta:set_string("formspec", formspec(tubelib.FAULT,
|
||||
100 * fuel / BIOGAS_WORK_TIME, 0, recipe_idx, true))
|
||||
node.name = "biogasmachines:compactor"
|
||||
minetest.swap_node(pos, node)
|
||||
minetest.get_node_timer(pos):stop()
|
||||
return false
|
||||
end
|
||||
|
||||
local function countdown_to_idle_or_stop(pos, stop)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local running = meta:get_int("running")
|
||||
if running > 0 then
|
||||
running = running - 1
|
||||
meta:set_int("running", running)
|
||||
if running == 0 then
|
||||
if stop then
|
||||
return compactor_stop(pos)
|
||||
else
|
||||
return compactor_idle(pos)
|
||||
end
|
||||
-- customized version of NodeStates:idle()
|
||||
local function countdown_to_halt(pos, meta, target_state)
|
||||
if target_state ~= tubelib.STANDBY and
|
||||
target_state ~= tubelib.BLOCKED and
|
||||
target_state ~= tubelib.STOPPED and
|
||||
target_state ~= tubelib.FAULT then
|
||||
return true
|
||||
end
|
||||
if machine:get_state(meta) == tubelib.RUNNING and
|
||||
meta:get_int("desired_state") ~= target_state then
|
||||
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)
|
||||
end
|
||||
if countdown < 0 then
|
||||
if machine:get_state(meta) == target_state then
|
||||
return true
|
||||
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
|
||||
return true
|
||||
end
|
||||
|
||||
-- countdown to one of two states depending on fuel availability
|
||||
local function fuel_countdown_to_halt(pos, meta, target_state_fuel, target_state_empty)
|
||||
local inv = meta:get_inventory()
|
||||
if meta:get_int("fuel_ticks") == 0 and inv:is_empty("fuel") then
|
||||
return countdown_to_halt(pos, meta, target_state_empty)
|
||||
else
|
||||
return countdown_to_halt(pos, meta, target_state_fuel)
|
||||
end
|
||||
end
|
||||
|
||||
--[[
|
||||
---------
|
||||
Callbacks
|
||||
---------
|
||||
]]--
|
||||
|
||||
-- do not allow to dig non-empty machine
|
||||
-- do not allow to dig protected or non-empty machine
|
||||
local function can_dig(pos, player)
|
||||
if minetest.is_protected(pos, player:get_player_name()) then
|
||||
return false
|
||||
end
|
||||
local meta = minetest.get_meta(pos);
|
||||
local inv = meta:get_inventory()
|
||||
return inv:is_empty("src") and inv:is_empty("dst")
|
||||
@ -321,6 +342,24 @@ local function after_dig_node(pos, oldnode, oldmetadata, digger)
|
||||
tubelib.remove_node(pos)
|
||||
end
|
||||
|
||||
-- init machine after placement
|
||||
local function after_place_node(pos, placer, itemstack, pointed_thing)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size('src', INV_H * INV_IN_W)
|
||||
inv:set_size('cur', 1) -- working tray (item)
|
||||
inv:set_size('cic', 1) -- working tray (ice)
|
||||
inv:set_size('fuel', 1)
|
||||
inv:set_size('ice', 1)
|
||||
inv:set_size('dst', INV_H * INV_OUT_W)
|
||||
meta:set_string("owner", placer:get_player_name())
|
||||
meta:set_int("fuel_ticks", 0)
|
||||
state_meta_reset(pos, meta)
|
||||
meta:set_int("recipe_idx", 1)
|
||||
local number = tubelib.add_node(pos, "biogasmachines:compactor")
|
||||
machine:node_init(pos, number)
|
||||
end
|
||||
|
||||
-- validate incoming items
|
||||
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
|
||||
if minetest.is_protected(pos, player:get_player_name()) then
|
||||
@ -353,11 +392,11 @@ end
|
||||
|
||||
-- validate items move
|
||||
local function allow_metadata_inventory_move(pos, from_list, from_index,
|
||||
to_list, to_index, count, player)
|
||||
to_list, to_index, count, player)
|
||||
local meta = minetest.get_meta(pos)
|
||||
if to_list == "cur" or to_list == "cic" or
|
||||
((from_list == "cur" or from_list == "cic") and
|
||||
meta:get_int("running") > 0) then
|
||||
machine:get_state(meta) == tubelib.RUNNING) then
|
||||
return 0
|
||||
end
|
||||
local inv = meta:get_inventory()
|
||||
@ -372,51 +411,23 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
|
||||
end
|
||||
if listname == "cur" or listname == "cic" then
|
||||
local meta = minetest.get_meta(pos)
|
||||
if meta:get_int("running") > 0 then
|
||||
if machine:get_state(meta) == tubelib.RUNNING then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
return stack:get_count()
|
||||
end
|
||||
|
||||
-- punch machine to see status info
|
||||
local function on_punch(pos, node, puncher, pointed_thing)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local player_name = puncher:get_player_name()
|
||||
if meta:get_string("owner") ~= player_name then
|
||||
return false
|
||||
end
|
||||
local msgclr = { ["fault"] = "#FFBFBF",
|
||||
["standby"] = "#BFFFFF",
|
||||
["stopped"] = "#BFBFFF",
|
||||
["running"] = "#BFFFBF"
|
||||
}
|
||||
local state = tubelib.statestring(meta:get_int("running"))
|
||||
minetest.chat_send_player(player_name,
|
||||
minetest.colorize("#FFFF00", "[Compactor:" ..
|
||||
meta:get_string("number") .. "]") .. " Status is " ..
|
||||
minetest.colorize(msgclr[state], "\"" .. state .. "\""))
|
||||
return true
|
||||
end
|
||||
|
||||
-- formspec button handler
|
||||
local function on_receive_fields(pos, formname, fields, player)
|
||||
if minetest.is_protected(pos, player:get_player_name()) then
|
||||
return
|
||||
end
|
||||
local meta = minetest.get_meta(pos)
|
||||
local running = meta:get_int("running")
|
||||
if fields and fields.button then
|
||||
if running > 0 or running == tubelib.STATE_FAULT then
|
||||
compactor_stop(pos)
|
||||
else
|
||||
compactor_start(pos)
|
||||
end
|
||||
if machine:state_button_event(pos, fields) then
|
||||
return
|
||||
end
|
||||
if fields and (fields.left or fields.right) then
|
||||
-- update hintbar
|
||||
local fuel = meta:get_int("fuel_ticks")
|
||||
local item_ticks = meta:get_int("item_ticks")
|
||||
local meta = minetest.get_meta(pos)
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
if fields.left then
|
||||
recipe_idx = math.max(recipe_idx - 1, 1)
|
||||
@ -424,15 +435,8 @@ local function on_receive_fields(pos, formname, fields, player)
|
||||
if fields.right then
|
||||
recipe_idx = math.min(recipe_idx + 1, #hintbar_recipes)
|
||||
end
|
||||
local item_pct = 0
|
||||
if item_ticks >= 0 then
|
||||
local item_time = meta:get_int("item_total")
|
||||
item_pct = 100 * (item_time - item_ticks) / item_time
|
||||
end
|
||||
meta:set_int("recipe_idx", recipe_idx)
|
||||
meta:set_string("formspec", formspec(tubelib.state(running),
|
||||
100 * fuel / BIOGAS_WORK_TIME, item_pct, recipe_idx,
|
||||
true))
|
||||
meta:set_string("formspec", formspec(machine, pos, meta))
|
||||
end
|
||||
end
|
||||
|
||||
@ -441,34 +445,23 @@ local function on_timer(pos, elapsed)
|
||||
local node = minetest.get_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inv = meta:get_inventory()
|
||||
local label = minetest.registered_nodes[node.name].description
|
||||
local number = meta:get_string("number")
|
||||
local running = meta:get_int("running")
|
||||
local itemcnt = meta:get_int("item_ticks")
|
||||
local fuel = meta:get_int("fuel_ticks")
|
||||
local recipe_idx = meta:get_int("recipe_idx")
|
||||
if fuel == 0 and inv:is_empty("fuel") then
|
||||
-- no fuel - no work (but wait a little)
|
||||
return countdown_to_idle_or_stop(pos, true)
|
||||
end
|
||||
if inv:is_empty("cic") and inv:is_empty("ice") then
|
||||
-- no ice - count towards standby
|
||||
return countdown_to_idle_or_stop(pos)
|
||||
end
|
||||
local recipe = {}
|
||||
local prodtime = -1
|
||||
local ice = ItemStack("default:ice 1")
|
||||
if inv:is_empty("cur") then
|
||||
local recipe = {}
|
||||
local inp
|
||||
if inv:is_empty("cur") then
|
||||
-- idle and ready, check for something to work with
|
||||
if inv:is_empty("src") then
|
||||
return countdown_to_idle_or_stop(pos)
|
||||
if inv:is_empty("src") or
|
||||
(inv:is_empty("cic") and inv:is_empty("ice")) then
|
||||
return fuel_countdown_to_halt(pos, meta,
|
||||
tubelib.STANDBY, tubelib.STOPPED)
|
||||
end
|
||||
-- find item to compact that fits output tray
|
||||
local is_src_ok = false
|
||||
local src_copy = inv:set_list("src_copy", inv:get_list("src"))
|
||||
for _, r in pairs(compactor_recipes) do
|
||||
local s = inv:remove_item("src_copy", r.input)
|
||||
if s:get_count() == r.input:get_count() then
|
||||
inp = inv:remove_item("src_copy", r.input)
|
||||
if inp:get_count() == r.input:get_count() then
|
||||
is_src_ok = true
|
||||
if inv:room_for_item("dst", r.output) then
|
||||
recipe = r
|
||||
@ -478,53 +471,82 @@ local function on_timer(pos, elapsed)
|
||||
end
|
||||
inv:set_size("src_copy", 0)
|
||||
if not recipe.time then
|
||||
-- no processing possible
|
||||
if is_src_ok then
|
||||
return true -- busy wait for space in dst
|
||||
-- source is ok but output tray is full
|
||||
if machine:get_state(meta) == tubelib.STANDBY then
|
||||
-- adapt behaviour to other biogas machines
|
||||
-- (standby->blocked should go through running)
|
||||
machine:start(pos, meta, true)
|
||||
return false
|
||||
else
|
||||
return fuel_countdown_to_halt(pos, meta,
|
||||
tubelib.BLOCKED, tubelib.FAULT)
|
||||
end
|
||||
else
|
||||
return countdown_to_idle_or_stop(pos)
|
||||
-- not enough items in source tray
|
||||
return fuel_countdown_to_halt(pos, meta,
|
||||
tubelib.STANDBY, tubelib.STOPPED)
|
||||
end
|
||||
elseif running == tubelib.STATE_STANDBY then
|
||||
elseif machine:get_state(meta) == tubelib.STANDBY or
|
||||
machine:get_state(meta) == tubelib.BLOCKED then
|
||||
-- something to do, wake up and re-entry
|
||||
return compactor_start(pos)
|
||||
machine:start(pos, meta, true)
|
||||
return false
|
||||
end
|
||||
local s = inv:remove_item("src", recipe.input)
|
||||
if s:is_empty() or s:get_count() < recipe.input:get_count() then
|
||||
return compactor_fault(pos) -- oops
|
||||
if fuel == 0 and inv:is_empty("fuel") then
|
||||
return countdown_to_halt(pos, meta, tubelib.FAULT)
|
||||
end
|
||||
inp = inv:remove_item("src", recipe.input)
|
||||
if inp:is_empty() or inp:get_count() < recipe.input:get_count() then
|
||||
machine_fault(pos, meta) -- oops
|
||||
return false
|
||||
end
|
||||
inv:set_stack("cur", 1, recipe.input)
|
||||
if inv:is_empty("cic") then
|
||||
s = inv:remove_item("ice", ice)
|
||||
if s:is_empty() then
|
||||
return compactor_fault(pos) -- oops
|
||||
inp = inv:remove_item("ice", ice)
|
||||
if inp:is_empty() then
|
||||
machine_fault(pos, meta) -- oops
|
||||
return false
|
||||
end
|
||||
inv:set_stack("cic", 1, s)
|
||||
inv:set_stack("cic", 1, inp)
|
||||
end
|
||||
meta:set_int("item_ticks", recipe.time)
|
||||
meta:set_int("item_total", recipe.time)
|
||||
itemcnt = recipe.time
|
||||
prodtime = recipe.time
|
||||
elseif inv:is_empty("cic") then
|
||||
-- ice removed manually while machine was off - reload
|
||||
if fuel == 0 and inv:is_empty("fuel") then
|
||||
return countdown_to_halt(pos, meta, tubelib.FAULT)
|
||||
end
|
||||
if inv:is_empty("ice") then
|
||||
return countdown_to_halt(pos, meta, tubelib.STANDBY)
|
||||
elseif machine:get_state(meta) == tubelib.STANDBY then
|
||||
machine:start(pos, meta, true)
|
||||
return false
|
||||
end
|
||||
inp = inv:remove_item("ice", ice)
|
||||
inv:set_stack("cic", 1, inp)
|
||||
else
|
||||
-- production tick
|
||||
if inv:is_empty("cic") then
|
||||
local s = inv:remove_item("ice", ice)
|
||||
if s:is_empty() then
|
||||
return compactor_fault(pos) -- oops
|
||||
end
|
||||
inv:set_stack("cic", 1, s)
|
||||
if running == tubelib.STATE_STANDBY then
|
||||
-- ice available again, wake up and re-entry
|
||||
return compactor_start(pos)
|
||||
end
|
||||
-- production
|
||||
if machine:get_state(meta) ~= tubelib.RUNNING or
|
||||
inv:is_empty("cur") or inv:is_empty("cic") then
|
||||
-- exception, should not happen - oops
|
||||
machine_fault(pos, meta)
|
||||
return false
|
||||
end
|
||||
local s = inv:get_stack("cur", 1)
|
||||
if s:is_empty() then
|
||||
return compactor_fault(pos) -- oops
|
||||
if fuel == 0 and inv:is_empty("fuel") then
|
||||
return countdown_to_halt(pos, meta, tubelib.FAULT)
|
||||
end
|
||||
recipe = compactor_recipes[s:get_name()] -- (reference!)
|
||||
if not recipe or not recipe.time then
|
||||
return compactor_fault(pos) -- oops
|
||||
inp = inv:get_stack("cur", 1)
|
||||
recipe = compactor_recipes[inp:get_name()] -- (reference!)
|
||||
if not recipe or not recipe.time or
|
||||
inp:get_count() ~= recipe.input:get_count() then
|
||||
machine_fault(pos, meta) -- oops
|
||||
return false
|
||||
end
|
||||
local itemcnt = meta:get_int("item_ticks")
|
||||
if itemcnt < 0 then
|
||||
meta:set_int("item_total", recipe.time)
|
||||
itemcnt = recipe.time -- compact again
|
||||
end
|
||||
itemcnt = itemcnt - 1
|
||||
@ -532,30 +554,28 @@ local function on_timer(pos, elapsed)
|
||||
inv:add_item("dst", recipe.output)
|
||||
inv:set_stack("cur", 1, ItemStack({}))
|
||||
inv:set_stack("cic", 1, ItemStack({}))
|
||||
itemcnt = -1
|
||||
state_meta_reset(pos, meta)
|
||||
-- item produced, increase aging
|
||||
machine:keep_running(pos, meta, COUNTDOWN_TICKS)
|
||||
else
|
||||
prodtime = recipe.time
|
||||
meta:set_int("item_ticks", itemcnt)
|
||||
end
|
||||
meta:set_int("item_ticks", itemcnt)
|
||||
-- consume fuel tick
|
||||
if fuel == 0 then
|
||||
if not inv:is_empty("fuel") then
|
||||
inv:remove_item("fuel",
|
||||
ItemStack("tubelib_addons1:biogas 1"))
|
||||
fuel = BIOGAS_WORK_TIME
|
||||
fuel = BIOGAS_TICKS
|
||||
else
|
||||
-- oops
|
||||
return compactor_fault(pos)
|
||||
machine_fault(pos, meta) -- oops
|
||||
return false
|
||||
end
|
||||
end
|
||||
fuel = fuel - 1
|
||||
meta:set_int("fuel_ticks", fuel)
|
||||
meta:set_int("fuel_ticks", fuel - 1)
|
||||
end
|
||||
meta:set_int("running", TICKS_TO_SLEEP)
|
||||
meta:set_string("infotext", label .. " " .. number .. ": running")
|
||||
meta:set_string("formspec", formspec(tubelib.RUNNING,
|
||||
100 * fuel / BIOGAS_WORK_TIME,
|
||||
100 * (recipe.time - itemcnt) / prodtime, recipe_idx, true))
|
||||
meta:set_int("tubelib_countdown", COUNTDOWN_TICKS)
|
||||
meta:set_int("desired_state", tubelib.RUNNING)
|
||||
meta:set_string("formspec", formspec(machine, pos, meta))
|
||||
return true
|
||||
end
|
||||
|
||||
@ -597,38 +617,21 @@ minetest.register_node("biogasmachines:compactor", {
|
||||
is_ground_content = false,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
|
||||
drop = "",
|
||||
can_dig = can_dig,
|
||||
after_dig_node = after_dig_node,
|
||||
on_punch = on_punch,
|
||||
|
||||
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
||||
machine:after_dig_node(pos, oldnode, oldmetadata, digger)
|
||||
after_dig_node(pos, oldnode, oldmetadata, digger)
|
||||
end,
|
||||
|
||||
on_rotate = screwdriver.disallow,
|
||||
on_timer = on_timer,
|
||||
on_receive_fields = on_receive_fields,
|
||||
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
||||
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
||||
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
||||
|
||||
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
||||
local node = minetest.get_node(pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local number = tubelib.add_node(pos, "biogasmachines:compactor")
|
||||
local inv = meta:get_inventory()
|
||||
inv:set_size('src', INV_H * INV_IN_W)
|
||||
inv:set_size('cur', 1) -- working tray (item)
|
||||
inv:set_size('cic', 1) -- working tray (ice)
|
||||
inv:set_size('fuel', 1)
|
||||
inv:set_size('ice', 1)
|
||||
inv:set_size('dst', INV_H * INV_OUT_W)
|
||||
local label = minetest.registered_nodes[node.name].description
|
||||
meta:set_string("number", number)
|
||||
meta:set_string("owner", placer:get_player_name())
|
||||
meta:set_int("running", tubelib.STATE_STOPPED)
|
||||
meta:set_int("fuel_ticks", 0)
|
||||
meta:set_int("item_ticks", -1)
|
||||
meta:set_int("item_total", 0)
|
||||
meta:set_int("recipe_idx", 1)
|
||||
meta:set_string("infotext", label .. " " .. number .. ": stopped")
|
||||
meta:set_string("formspec", formspec(tubelib.STOPPED, 0, 0, 1, true))
|
||||
end,
|
||||
after_place_node = after_place_node,
|
||||
})
|
||||
|
||||
minetest.register_node("biogasmachines:compactor_active", {
|
||||
@ -672,18 +675,69 @@ minetest.register_node("biogasmachines:compactor_active", {
|
||||
is_ground_content = false,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
|
||||
drop = "",
|
||||
can_dig = can_dig,
|
||||
after_dig_node = after_dig_node,
|
||||
on_punch = on_punch,
|
||||
|
||||
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
||||
machine:after_dig_node(pos, oldnode, oldmetadata, digger)
|
||||
after_dig_node(pos, oldnode, oldmetadata, digger)
|
||||
end,
|
||||
|
||||
on_rotate = screwdriver.disallow,
|
||||
on_timer = on_timer,
|
||||
on_receive_fields = on_receive_fields,
|
||||
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
||||
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
||||
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
||||
drop = "biogasmachines:compactor",
|
||||
})
|
||||
tubelib.register_node("biogasmachines:compactor", { "biogasmachines:compactor_active" }, {
|
||||
|
||||
minetest.register_node("biogasmachines:compactor_defect", {
|
||||
description = "Tubelib Compactor",
|
||||
tiles = {
|
||||
-- up, down, right, left, back, front
|
||||
"biogasmachines_compactor_top.png",
|
||||
"biogasmachines_bottom.png",
|
||||
"biogasmachines_compactor_side.png^tubelib_defect.png",
|
||||
"biogasmachines_compactor_side.png^tubelib_defect.png",
|
||||
"biogasmachines_compactor_side.png^tubelib_defect.png",
|
||||
"biogasmachines_compactor_side.png^tubelib_defect.png",
|
||||
},
|
||||
drawtype = "nodebox",
|
||||
node_box = {
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{ -0.5, -0.5, -0.5, 0.5, 0.375, 0.5 },
|
||||
{ -0.375, 0.4375, -0.375, 0.375, 0.5, 0.375 },
|
||||
{ -0.3125, 0.375, -0.3125, 0.3125, 0.4375, 0.3125 },
|
||||
}
|
||||
},
|
||||
selection_box = {
|
||||
type = "fixed",
|
||||
fixed = { -0.5, -0.5, -0.5, 0.5, 0.375, 0.5 },
|
||||
},
|
||||
|
||||
paramtype = "light",
|
||||
sunlight_propagates = true,
|
||||
paramtype2 = "facedir",
|
||||
groups = { choppy = 2, cracky = 2, crumbly = 2, not_in_creative_inventory = 1 },
|
||||
is_ground_content = false,
|
||||
sounds = default.node_sound_metal_defaults(),
|
||||
|
||||
can_dig = can_dig,
|
||||
after_dig_node = after_dig_node,
|
||||
on_rotate = screwdriver.disallow,
|
||||
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
||||
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
||||
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
||||
|
||||
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
||||
after_place_node(pos, placer, itemstack, pointed_thing)
|
||||
machine:defect(pos, minetest.get_meta(pos))
|
||||
end,
|
||||
})
|
||||
|
||||
tubelib.register_node("biogasmachines:compactor",
|
||||
{ "biogasmachines:compactor_active", "biogasmachines:compactor_defect" }, {
|
||||
|
||||
on_push_item = function(pos, side, item)
|
||||
local meta = minetest.get_meta(pos)
|
||||
@ -710,18 +764,24 @@ tubelib.register_node("biogasmachines:compactor", { "biogasmachines:compactor_ac
|
||||
|
||||
on_recv_message = function(pos, topic, payload)
|
||||
local meta = minetest.get_meta(pos)
|
||||
if topic == "on" then
|
||||
compactor_start(pos)
|
||||
elseif topic == "off" then
|
||||
compactor_stop(pos)
|
||||
elseif topic == "state" then
|
||||
return tubelib.statestring(meta:get_int("running"))
|
||||
elseif topic == "fuel" then
|
||||
if topic == "fuel" then
|
||||
return tubelib.fuelstate(meta, "fuel")
|
||||
end
|
||||
local resp = machine:on_receive_message(pos, topic, payload)
|
||||
if resp then
|
||||
return resp
|
||||
else
|
||||
return "unsupported"
|
||||
end
|
||||
end,
|
||||
|
||||
on_node_load = function(pos)
|
||||
machine:on_node_load(pos)
|
||||
end,
|
||||
|
||||
on_node_repair = function(pos)
|
||||
return machine:on_node_repair(pos)
|
||||
end,
|
||||
})
|
||||
|
||||
--[[
|
||||
|
Loading…
x
Reference in New Issue
Block a user