fermenter converted to new states class and some code maintenance

This commit is contained in:
Joachim Stolberg 2018-12-25 22:06:05 +01:00
parent 8d152ce19c
commit 6f124cfe54
7 changed files with 176 additions and 168 deletions

@ -217,12 +217,9 @@ local function distributing(pos, meta)
-- rejected items from other filter slots -- rejected items from other filter slots
local rejected = FilterCache[hash].kvRejectedItemNames local rejected = FilterCache[hash].kvRejectedItemNames
if items == nil then if items == nil then return end
-- this slot is empty
return false
end
local busy = false local moved_items_total = 0
if next(items) then if next(items) then
for _,item in ipairs(items) do for _,item in ipairs(items) do
local name, num = item[1], item[2] local name, num = item[1], item[2]
@ -234,7 +231,7 @@ local function distributing(pos, meta)
rejected[name] = num rejected[name] = num
else else
counter[listname] = counter[listname] + num counter[listname] = counter[listname] + num
busy = true moved_items_total = moved_items_total + num
end end
end end
end end
@ -254,7 +251,7 @@ local function distributing(pos, meta)
else else
counter[listname] = counter[listname] + num counter[listname] = counter[listname] + num
moved_items = moved_items + num moved_items = moved_items + num
busy = true moved_items_total = moved_items_total + num
end end
end end
end end
@ -265,17 +262,17 @@ local function distributing(pos, meta)
end end
end end
meta:set_string("item_counter", minetest.serialize(counter)) meta:set_string("item_counter", minetest.serialize(counter))
return busy if moved_items_total > 0 then
State:keep_running(pos, meta, COUNTDOWN_TICKS, moved_items_total)
else
State:idle(pos, meta)
end
end end
-- move items to the output slots -- move items to the output slots
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = M(pos) local meta = M(pos)
if distributing(pos, meta) then distributing(pos, meta)
State:keep_running(pos, meta, COUNTDOWN_TICKS)
else
State:idle(pos, meta)
end
return State:is_active(meta) return State:is_active(meta)
end end

@ -289,13 +289,13 @@ end
-- To be called after successful node action to raise the timer -- To be called after successful node action to raise the timer
-- and keep the node in state RUNNING -- and keep the node in state RUNNING
function NodeStates:keep_running(pos, meta, val) function NodeStates:keep_running(pos, meta, val, num_items)
-- set to RUNNING if not already done -- set to RUNNING if not already done
self:start(pos, meta, true) self:start(pos, meta, true)
meta:set_int("tubelib_countdown", val) meta:set_int("tubelib_countdown", val)
meta:set_int("tubelib_item_meter", meta:get_int("tubelib_item_meter") + 1) meta:set_int("tubelib_item_meter", meta:get_int("tubelib_item_meter") + (num_items or 1))
if self.aging_level1 then if self.aging_level1 then
local cnt = meta:get_int("tubelib_aging") + 1 local cnt = meta:get_int("tubelib_aging") + (num_items or 1)
meta:set_int("tubelib_aging", cnt) meta:set_int("tubelib_aging", cnt)
if cnt > (self.aging_level1) and math.random(self.aging_level2) == 1 then if cnt > (self.aging_level1) and math.random(self.aging_level2) == 1 then
self:defect(pos, meta) self:defect(pos, meta)

@ -122,7 +122,8 @@ local function autocraft(pos, meta, inventory, craft)
for i = 1, 9 do for i = 1, 9 do
inventory:add_item("dst", craft.decremented_input.items[i]) inventory:add_item("dst", craft.decremented_input.items[i])
end end
State:keep_running(pos, meta, COUNTDOWN_TICKS)
State:keep_running(pos, meta, COUNTDOWN_TICKS, output_item:get_count())
end end
@ -286,7 +287,10 @@ end
minetest.register_node("tubelib_addons1:autocrafter", { minetest.register_node("tubelib_addons1:autocrafter", {
description = "Tubelib Autocrafter", description = "Tubelib Autocrafter",
drawtype = "normal", drawtype = "normal",
tiles = {'tubelib_front.png', 'tubelib_addons1_autocrafter.png'}, tiles = {
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_autocrafter.png'},
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:autocrafter") local number = tubelib.add_node(pos, "tubelib_addons1:autocrafter")
@ -332,6 +336,7 @@ minetest.register_node("tubelib_addons1:autocrafter_active", {
description = "Tubelib Autocrafter", description = "Tubelib Autocrafter",
drawtype = "normal", drawtype = "normal",
tiles = { tiles = {
'tubelib_front.png',
'tubelib_front.png', 'tubelib_front.png',
{ {
image = 'tubelib_addons1_autocrafter_active.png', image = 'tubelib_addons1_autocrafter_active.png',
@ -363,7 +368,11 @@ minetest.register_node("tubelib_addons1:autocrafter_active", {
minetest.register_node("tubelib_addons1:autocrafter_defect", { minetest.register_node("tubelib_addons1:autocrafter_defect", {
description = "Tubelib Autocrafter", description = "Tubelib Autocrafter",
drawtype = "normal", drawtype = "normal",
tiles = {'tubelib_front.png', 'tubelib_addons1_autocrafter.png'}, tiles = {
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_autocrafter.png^tubelib_defect.png'
},
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:autocrafter") local number = tubelib.add_node(pos, "tubelib_addons1:autocrafter")

@ -3,7 +3,7 @@
Tubelib Addons 1 Tubelib Addons 1
================ ================
Copyright (C) 2017 Joachim Stolberg Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+ LGPLv2.1+
See LICENSE.txt for more information See LICENSE.txt for more information
@ -15,14 +15,26 @@
]]-- ]]--
local CYCLE_TIME = 4 -- for lazy programmers
local TICKS_TO_SLEEP = 5 local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local STOP_STATE = 0 local P = minetest.string_to_pos
local STANDBY_STATE = -1 local M = minetest.get_meta
local FAULT_STATE = -3
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 2
local function formspec(state) local State = tubelib.NodeStates:new({
node_name_passive = "tubelib_addons1:fermenter",
node_name_defect = "tubelib_addons1:fermenter_defect",
infotext_name = "Tubelib Fermenter",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
has_item_meter = true,
aging_factor = 10,
})
local function formspec(pos, meta)
return "size[8,8]".. return "size[8,8]"..
default.gui_bg.. default.gui_bg..
default.gui_bg_img.. default.gui_bg_img..
@ -30,7 +42,7 @@ local function formspec(state)
"list[context;src;0,0;3,3;]".. "list[context;src;0,0;3,3;]"..
"item_image[0,0;1,1;default:leaves]".. "item_image[0,0;1,1;default:leaves]"..
"image[3.5,1;1,1;tubelib_gui_arrow.png]".. "image[3.5,1;1,1;tubelib_gui_arrow.png]"..
"image_button[3.5,3;1,1;".. tubelib.state_button(state) ..";button;]".. "image_button[3.5,3;1,1;".. State:get_state_button_image(meta) ..";state_button;]"..
"list[context;dst;5,0;3,3;]".. "list[context;dst;5,0;3,3;]"..
"item_image[5,0;1,1;tubelib_addons1:biogas]".. "item_image[5,0;1,1;tubelib_addons1:biogas]"..
"list[current_player;main;0,4.3;8,4;]".. "list[current_player;main;0,4.3;8,4;]"..
@ -40,6 +52,8 @@ local function formspec(state)
"listring[current_player;main]" "listring[current_player;main]"
end end
State:register_formspec_func(formspec)
local function is_leaves(name) local function is_leaves(name)
return tubelib_addons1.FarmingNodes[name] ~= nil and return tubelib_addons1.FarmingNodes[name] ~= nil and
(tubelib_addons1.FarmingNodes[name].leaves == true or (tubelib_addons1.FarmingNodes[name].leaves == true or
@ -53,6 +67,9 @@ local function allow_metadata_inventory_put(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if listname == "src" and is_leaves(stack:get_name()) then if listname == "src" and is_leaves(stack:get_name()) then
if State:get_state(M(pos)) == tubelib.STANDBY then
State:start(pos, M(pos))
end
return stack:get_count() return stack:get_count()
elseif listname == "dst" then elseif listname == "dst" then
return stack:get_count() return stack:get_count()
@ -87,118 +104,54 @@ local function place_top(pos, facedir, placer)
return true return true
end end
local function convert_leaves_to_biogas(meta) local function convert_leaves_to_biogas(pos, meta)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local biogas = ItemStack("tubelib_addons1:biogas") local biogas = ItemStack("tubelib_addons1:biogas")
if inv:room_for_item("dst", biogas) then -- enough output space?
local items = {} -- Not enough output space?
local fault = false if not inv:room_for_item("dst", biogas) then
for i = 1, 2 do State:blocked(pos, meta)
items[i] = tubelib.get_num_items(meta, "src", 1) return
if items[i] then -- input available?
if not is_leaves(items[i]:get_name()) then
fault = true
break
end
else
break
end
end
if #items == 2 then
inv:add_item("dst", biogas)
return true
else
for i = 1, #items do
inv:add_item("src", items[i])
end
if fault then
return nil -- error
else
return false -- standby
end
end
else
return false -- standby
end end
end
-- take 2 items
local function start_the_machine(pos) local items = {}
local meta = minetest.get_meta(pos) local fault = false
local number = meta:get_string("number") for i = 1, 2 do
meta:set_int("running", TICKS_TO_SLEEP) items[i] = tubelib.get_num_items(meta, "src", 1)
meta:set_string("infotext", "Tubelib Fermenter "..number..": running") if items[i] then -- input available?
meta:set_string("formspec", formspec(tubelib.RUNNING)) if not is_leaves(items[i]:get_name()) then
minetest.get_node_timer(pos):start(CYCLE_TIME) State:fault(pos, meta)
return false return
end end
end
local function stop_the_machine(pos) end
local meta = minetest.get_meta(pos)
local number = meta:get_string("number") -- put result into dst inventory
meta:set_int("running", STOP_STATE) if #items == 2 then
minetest.get_node_timer(pos):stop() inv:add_item("dst", biogas)
meta:set_string("infotext", "Tubelib Fermenter "..number..": stopped") State:keep_running(pos, meta, COUNTDOWN_TICKS)
meta:set_string("formspec", formspec(tubelib.STOPPED)) return
return false end
end
-- put leaves back to src inventory
local function goto_sleep(pos) for i = 1, #items do
local meta = minetest.get_meta(pos) inv:add_item("src", items[i])
local number = meta:get_string("number") end
meta:set_int("running", STANDBY_STATE) State:idle(pos, meta)
minetest.get_node_timer(pos):start(CYCLE_TIME * TICKS_TO_SLEEP)
meta:set_string("infotext", "Tubelib Fermenter "..number..": standby")
meta:set_string("formspec", formspec(tubelib.STANDBY))
return false
end
local function goto_fault(pos)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", FAULT_STATE)
minetest.get_node_timer(pos):stop()
meta:set_string("infotext", "Tubelib Fermenter "..number..": fault")
meta:set_string("formspec", formspec(tubelib.FAULT))
return false
end end
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local running = meta:get_int("running") - 1 convert_leaves_to_biogas(pos, meta)
local res = convert_leaves_to_biogas(meta) return State:is_active(meta)
if res == true then
if running <= STOP_STATE then
return start_the_machine(pos)
else
running = TICKS_TO_SLEEP
end
elseif res == false then
if running <= STOP_STATE then
local node = minetest.get_node(pos)
return goto_sleep(pos, node)
end
else
return goto_fault(pos)
end
meta:set_int("running", running)
meta:set_string("formspec", formspec(tubelib.RUNNING))
return true
end end
local function on_receive_fields(pos, formname, fields, player) local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return return
end end
local meta = minetest.get_meta(pos) State:state_button_event(pos, fields)
local running = meta:get_int("running") or STOP_STATE
if fields.button ~= nil then
if running > STOP_STATE or running == FAULT_STATE then
stop_the_machine(pos)
else
start_the_machine(pos)
end
end
end end
minetest.register_node("tubelib_addons1:fermenter", { minetest.register_node("tubelib_addons1:fermenter", {
@ -233,11 +186,73 @@ minetest.register_node("tubelib_addons1:fermenter", {
return return
end end
local number = tubelib.add_node(pos, "tubelib_addons1:fermenter") local number = tubelib.add_node(pos, "tubelib_addons1:fermenter")
State:node_init(pos, number)
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("number", number) local inv = meta:get_inventory()
meta:set_int("running", STOP_STATE) if inv:is_empty("dst") and inv:is_empty("src") then
meta:set_string("infotext", "Tubelib Fermenter "..number..": stopped") minetest.node_dig(pos, node, puncher, pointed_thing)
meta:set_string("formspec", formspec(tubelib.STOPPED)) minetest.remove_node({x=pos.x, y=pos.y+1, z=pos.z})
tubelib.remove_node(pos)
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
end,
on_rotate = screwdriver.disallow,
on_timer = keep_running,
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 = "",
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("tubelib_addons1:fermenter_defect", {
description = "Tubelib Fermenter",
inventory_image = "tubelib_addons1_fermenter_inventory.png",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_fermenter_bottom.png^tubelib_defect.png',
'tubelib_addons1_fermenter_bottom.png^tubelib_defect.png',
'tubelib_addons1_fermenter_bottom.png^tubelib_defect.png',
'tubelib_addons1_fermenter_bottom.png^tubelib_defect.png',
},
selection_box = {
type = "fixed",
fixed = { -8/16, -8/16, -8/16, 8/16, 24/16, 8/16 },
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size('src', 9)
inv:set_size('dst', 9)
end,
after_place_node = function(pos, placer)
local facedir = minetest.dir_to_facedir(placer:get_look_dir(), false)
if place_top({x=pos.x, y=pos.y+1, z=pos.z}, facedir, placer) == false then
minetest.remove_node(pos)
return
end
local number = tubelib.add_node(pos, "tubelib_addons1:fermenter")
State:node_init(pos, number)
State:defect(pos, M(pos))
end, end,
on_dig = function(pos, node, puncher, pointed_thing) on_dig = function(pos, node, puncher, pointed_thing)
@ -251,8 +266,6 @@ minetest.register_node("tubelib_addons1:fermenter", {
end, end,
on_rotate = screwdriver.disallow, on_rotate = screwdriver.disallow,
on_timer = keep_running,
on_receive_fields = on_receive_fields,
allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take, allow_metadata_inventory_take = allow_metadata_inventory_take,
@ -260,12 +273,11 @@ minetest.register_node("tubelib_addons1:fermenter", {
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2}, groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
is_ground_content = false, is_ground_content = false,
sounds = default.node_sound_metal_defaults(), sounds = default.node_sound_metal_defaults(),
}) })
minetest.register_node("tubelib_addons1:fermenter_top", { minetest.register_node("tubelib_addons1:fermenter_top", {
description = "Tubelib Fermenter Top", description = "Tubelib Fermenter Top",
tiles = { tiles = {
@ -327,37 +339,28 @@ minetest.register_craft({
}) })
tubelib.register_node("tubelib_addons1:fermenter", {}, { tubelib.register_node("tubelib_addons1:fermenter", {"tubelib_addons1:fermenter_defect"}, {
on_pull_item = function(pos, side) on_pull_item = function(pos, side)
local meta = minetest.get_meta(pos) return tubelib.get_item(M(pos), "dst")
return tubelib.get_item(meta, "dst")
end, end,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = minetest.get_meta(pos) return tubelib.put_item(M(pos), "src", item)
return tubelib.put_item(meta, "src", item)
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
local meta = minetest.get_meta(pos) return tubelib.put_item(M(pos), "dst", item)
return tubelib.put_item(meta, "dst", item)
end, end,
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)
if topic == "on" then local resp = State:on_receive_message(pos, topic, payload)
start_the_machine(pos) if resp then
elseif topic == "off" then return resp
stop_the_machine(pos)
elseif topic == "state" then
local meta = minetest.get_meta(pos)
local running = meta:get_int("running")
return tubelib.statestring(running)
else else
return "unsupported" return "unsupported"
end end
end, end,
on_node_load = function(pos) on_node_load = function(pos)
local meta = minetest.get_meta(pos) State:on_node_load(pos)
if meta:get_int("running") ~= STOP_STATE then end,
meta:set_int("running", STANDBY_STATE) on_node_repair = function(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME * TICKS_TO_SLEEP) return State:on_node_repair(pos)
end
end, end,
}) })

@ -237,6 +237,7 @@ minetest.register_node("tubelib_addons1:grinder_defect", {
end end
end, end,
on_rotate = screwdriver.disallow,
allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_move = allow_metadata_inventory_move, allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take, allow_metadata_inventory_take = allow_metadata_inventory_take,

@ -140,7 +140,7 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
end end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index) local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player) return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
@ -195,11 +195,9 @@ local function distributing(pos, meta)
local open_ports = table.copy(FilterCache[hash].OpenPorts) local open_ports = table.copy(FilterCache[hash].OpenPorts)
-- no filter configured? -- no filter configured?
if next(kvFilterItemNames) == nil then if next(kvFilterItemNames) == nil then return end
return false
end
local busy = false local moved_items_total = 0
local inv = meta:get_inventory() local inv = meta:get_inventory()
local list = inv:get_list("src") local list = inv:get_list("src")
@ -224,7 +222,7 @@ local function distributing(pos, meta)
stack:set_count(0) stack:set_count(0)
local color = Side2Color[side] local color = Side2Color[side]
counter[color] = counter[color] + num counter[color] = counter[color] + num
busy = true moved_items_total = moved_items_total + num
else else
second_try = true -- port blocked second_try = true -- port blocked
end end
@ -240,7 +238,7 @@ local function distributing(pos, meta)
stack:set_count(0) stack:set_count(0)
local color = Side2Color[side] local color = Side2Color[side]
counter[color] = counter[color] + num counter[color] = counter[color] + num
busy = true moved_items_total = moved_items_total + num
end end
end end
end end
@ -248,17 +246,17 @@ local function distributing(pos, meta)
inv:set_list("src", list) inv:set_list("src", list)
meta:set_string("item_counter", minetest.serialize(counter)) meta:set_string("item_counter", minetest.serialize(counter))
return busy if moved_items_total > 0 then
State:keep_running(pos, meta, COUNTDOWN_TICKS, moved_items_total)
else
State:idle(pos, meta)
end
end end
-- move items to the output slots -- move items to the output slots
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = M(pos) local meta = M(pos)
if distributing(pos, meta) then distributing(pos, meta)
State:keep_running(pos, meta, COUNTDOWN_TICKS)
else
State:idle(pos, meta)
end
return State:is_active(meta) return State:is_active(meta)
end end

@ -44,7 +44,7 @@ local function pushing(pos, meta)
State:blocked(pos, meta) State:blocked(pos, meta)
return return
end end
State:keep_running(pos, meta, COUNTDOWN_TICKS) State:keep_running(pos, meta, COUNTDOWN_TICKS, items:get_count())
return return
end end
State:idle(pos, meta) State:idle(pos, meta)