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

@ -289,13 +289,13 @@ end
-- To be called after successful node action to raise the timer
-- 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
self:start(pos, meta, true)
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
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)
if cnt > (self.aging_level1) and math.random(self.aging_level2) == 1 then
self:defect(pos, meta)

@ -122,7 +122,8 @@ local function autocraft(pos, meta, inventory, craft)
for i = 1, 9 do
inventory:add_item("dst", craft.decremented_input.items[i])
end
State:keep_running(pos, meta, COUNTDOWN_TICKS)
State:keep_running(pos, meta, COUNTDOWN_TICKS, output_item:get_count())
end
@ -286,7 +287,10 @@ end
minetest.register_node("tubelib_addons1:autocrafter", {
description = "Tubelib Autocrafter",
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)
local number = tubelib.add_node(pos, "tubelib_addons1:autocrafter")
@ -332,6 +336,7 @@ minetest.register_node("tubelib_addons1:autocrafter_active", {
description = "Tubelib Autocrafter",
drawtype = "normal",
tiles = {
'tubelib_front.png',
'tubelib_front.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", {
description = "Tubelib Autocrafter",
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)
local number = tubelib.add_node(pos, "tubelib_addons1:autocrafter")

@ -3,7 +3,7 @@
Tubelib Addons 1
================
Copyright (C) 2017 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
@ -15,14 +15,26 @@
]]--
local CYCLE_TIME = 4
local TICKS_TO_SLEEP = 5
local STOP_STATE = 0
local STANDBY_STATE = -1
local FAULT_STATE = -3
-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta
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]"..
default.gui_bg..
default.gui_bg_img..
@ -30,7 +42,7 @@ local function formspec(state)
"list[context;src;0,0;3,3;]"..
"item_image[0,0;1,1;default:leaves]"..
"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;]"..
"item_image[5,0;1,1;tubelib_addons1:biogas]"..
"list[current_player;main;0,4.3;8,4;]"..
@ -40,6 +52,8 @@ local function formspec(state)
"listring[current_player;main]"
end
State:register_formspec_func(formspec)
local function is_leaves(name)
return tubelib_addons1.FarmingNodes[name] ~= nil and
(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 inv = meta:get_inventory()
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()
elseif listname == "dst" then
return stack:get_count()
@ -87,118 +104,54 @@ local function place_top(pos, facedir, placer)
return true
end
local function convert_leaves_to_biogas(meta)
local function convert_leaves_to_biogas(pos, meta)
local inv = meta:get_inventory()
local biogas = ItemStack("tubelib_addons1:biogas")
if inv:room_for_item("dst", biogas) then -- enough output space?
-- Not enough output space?
if not inv:room_for_item("dst", biogas) then
State:blocked(pos, meta)
return
end
-- take 2 items
local items = {}
local fault = false
for i = 1, 2 do
items[i] = tubelib.get_num_items(meta, "src", 1)
if items[i] then -- input available?
if not is_leaves(items[i]:get_name()) then
fault = true
break
end
else
break
State:fault(pos, meta)
return
end
end
end
-- put result into dst inventory
if #items == 2 then
inv:add_item("dst", biogas)
return true
else
State:keep_running(pos, meta, COUNTDOWN_TICKS)
return
end
-- put leaves back to src inventory
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
local function start_the_machine(pos)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", TICKS_TO_SLEEP)
meta:set_string("infotext", "Tubelib Fermenter "..number..": running")
meta:set_string("formspec", formspec(tubelib.RUNNING))
minetest.get_node_timer(pos):start(CYCLE_TIME)
return false
end
local function stop_the_machine(pos)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", STOP_STATE)
minetest.get_node_timer(pos):stop()
meta:set_string("infotext", "Tubelib Fermenter "..number..": stopped")
meta:set_string("formspec", formspec(tubelib.STOPPED))
return false
end
local function goto_sleep(pos)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", STANDBY_STATE)
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
State:idle(pos, meta)
end
local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos)
local running = meta:get_int("running") - 1
local res = convert_leaves_to_biogas(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
convert_leaves_to_biogas(pos, meta)
return State:is_active(meta)
end
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") 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
State:state_button_event(pos, fields)
end
minetest.register_node("tubelib_addons1:fermenter", {
@ -233,11 +186,73 @@ minetest.register_node("tubelib_addons1:fermenter", {
return
end
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)
meta:set_string("number", number)
meta:set_int("running", STOP_STATE)
meta:set_string("infotext", "Tubelib Fermenter "..number..": stopped")
meta:set_string("formspec", formspec(tubelib.STOPPED))
local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_dig(pos, node, puncher, pointed_thing)
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,
on_dig = function(pos, node, puncher, pointed_thing)
@ -251,8 +266,6 @@ minetest.register_node("tubelib_addons1:fermenter", {
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,
@ -260,12 +273,11 @@ minetest.register_node("tubelib_addons1:fermenter", {
paramtype = "light",
sunlight_propagates = true,
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,
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("tubelib_addons1:fermenter_top", {
description = "Tubelib Fermenter Top",
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)
local meta = minetest.get_meta(pos)
return tubelib.get_item(meta, "dst")
return tubelib.get_item(M(pos), "dst")
end,
on_push_item = function(pos, side, item)
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "src", item)
return tubelib.put_item(M(pos), "src", item)
end,
on_unpull_item = function(pos, side, item)
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "dst", item)
return tubelib.put_item(M(pos), "dst", item)
end,
on_recv_message = function(pos, topic, payload)
if topic == "on" then
start_the_machine(pos)
elseif topic == "off" then
stop_the_machine(pos)
elseif topic == "state" then
local meta = minetest.get_meta(pos)
local running = meta:get_int("running")
return tubelib.statestring(running)
local resp = State:on_receive_message(pos, topic, payload)
if resp then
return resp
else
return "unsupported"
end
end,
on_node_load = function(pos)
local meta = minetest.get_meta(pos)
if meta:get_int("running") ~= STOP_STATE then
meta:set_int("running", STANDBY_STATE)
minetest.get_node_timer(pos):start(CYCLE_TIME * TICKS_TO_SLEEP)
end
State:on_node_load(pos)
end,
on_node_repair = function(pos)
return State:on_node_repair(pos)
end,
})

@ -237,6 +237,7 @@ minetest.register_node("tubelib_addons1:grinder_defect", {
end
end,
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,

@ -140,7 +140,7 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
end
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 stack = inv:get_stack(from_list, from_index)
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)
-- no filter configured?
if next(kvFilterItemNames) == nil then
return false
end
if next(kvFilterItemNames) == nil then return end
local busy = false
local moved_items_total = 0
local inv = meta:get_inventory()
local list = inv:get_list("src")
@ -224,7 +222,7 @@ local function distributing(pos, meta)
stack:set_count(0)
local color = Side2Color[side]
counter[color] = counter[color] + num
busy = true
moved_items_total = moved_items_total + num
else
second_try = true -- port blocked
end
@ -240,7 +238,7 @@ local function distributing(pos, meta)
stack:set_count(0)
local color = Side2Color[side]
counter[color] = counter[color] + num
busy = true
moved_items_total = moved_items_total + num
end
end
end
@ -248,17 +246,17 @@ local function distributing(pos, meta)
inv:set_list("src", list)
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
-- move items to the output slots
local function keep_running(pos, elapsed)
local meta = M(pos)
if distributing(pos, meta) then
State:keep_running(pos, meta, COUNTDOWN_TICKS)
else
State:idle(pos, meta)
end
distributing(pos, meta)
return State:is_active(meta)
end

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