Quarry, Harvester, Fermenter, Reformer, and Liquid Sampler converted to new states class and some code maintenance

This commit is contained in:
Joachim Stolberg 2018-12-27 22:37:13 +01:00
parent 6f124cfe54
commit 58718c2f0f
10 changed files with 605 additions and 605 deletions

@ -3,7 +3,7 @@
Tube Library Tube Library
============ ============
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

@ -5,6 +5,6 @@ tubelib_max_num_forceload_blocks (max number of Forceload Blocks) int 10
tubelib_basalt_stone_enabled (Basalt Stone enabled) bool true tubelib_basalt_stone_enabled (Basalt Stone enabled) bool true
# This aging value is used to calculate the lifetime of machines before # This aging value is used to calculate the lifetime of machines before
# they go defect . The value 100 (default) results in a lifetime # they go defect . The value 200 (default) results in a lifetime
# for standard machines of about 1000 - 4000 item processing cycles. # for standard machines of about 2000 - 8000 item processing cycles.
tubelib_machine_aging_value (machine aging value) int 100 tubelib_machine_aging_value (machine aging value) int 200

@ -64,11 +64,11 @@ local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
local meta = minetest.get_meta(pos) local meta = M(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 if State:get_state(meta) == tubelib.STANDBY then
State:start(pos, M(pos)) State:start(pos, meta)
end end
return stack:get_count() return stack:get_count()
elseif listname == "dst" then elseif listname == "dst" then
@ -78,7 +78,7 @@ local function allow_metadata_inventory_put(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)
@ -142,7 +142,7 @@ local function convert_leaves_to_biogas(pos, meta)
end end
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos) local meta = M(pos)
convert_leaves_to_biogas(pos, meta) convert_leaves_to_biogas(pos, meta)
return State:is_active(meta) return State:is_active(meta)
end end
@ -173,7 +173,7 @@ minetest.register_node("tubelib_addons1:fermenter", {
}, },
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size('src', 9) inv:set_size('src', 9)
inv:set_size('dst', 9) inv:set_size('dst', 9)
@ -190,7 +190,7 @@ minetest.register_node("tubelib_addons1:fermenter", {
end, end,
on_dig = function(pos, node, puncher, pointed_thing) on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_dig(pos, node, puncher, pointed_thing) minetest.node_dig(pos, node, puncher, pointed_thing)
@ -238,7 +238,7 @@ minetest.register_node("tubelib_addons1:fermenter_defect", {
}, },
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size('src', 9) inv:set_size('src', 9)
inv:set_size('dst', 9) inv:set_size('dst', 9)
@ -256,7 +256,7 @@ minetest.register_node("tubelib_addons1:fermenter_defect", {
end, end,
on_dig = function(pos, node, puncher, pointed_thing) on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_dig(pos, node, puncher, pointed_thing) minetest.node_dig(pos, node, puncher, pointed_thing)

@ -3,7 +3,7 @@
Tubelib Addons 1 Tubelib Addons 1
================ ================
Copyright (C) 2017,2018 Joachim Stolberg Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+ LGPLv2.1+
See LICENSE.txt for more information See LICENSE.txt for more information

@ -3,7 +3,7 @@
Tubelib Addons 1 Tubelib Addons 1
================ ================
Copyright (C) 2017,2018 Joachim Stolberg Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+ LGPLv2.1+
See LICENSE.txt for more information See LICENSE.txt for more information
@ -62,7 +62,6 @@ local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
local inv = M(pos):get_inventory()
if listname == "src" and State:get_state(M(pos)) == tubelib.STANDBY then if listname == "src" and State:get_state(M(pos)) == tubelib.STANDBY then
State:start(pos, M(pos)) State:start(pos, M(pos))
end end
@ -211,7 +210,7 @@ minetest.register_node("tubelib_addons1:grinder_defect", {
tiles = { tiles = {
-- up, down, right, left, back, front -- up, down, right, left, back, front
'tubelib_addons1_grinder.png', 'tubelib_addons1_grinder.png',
'tubelib_front.png^tubelib_defect.png', 'tubelib_front.png',
'tubelib_front.png^tubelib_defect.png', 'tubelib_front.png^tubelib_defect.png',
'tubelib_front.png^tubelib_defect.png', 'tubelib_front.png^tubelib_defect.png',
"tubelib_front.png^tubelib_defect.png", "tubelib_front.png^tubelib_defect.png",

@ -3,14 +3,14 @@
Tubelib Addons 1 Tubelib Addons 1
================ ================
Copyright (C) 2017,2018 Joachim Stolberg Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+ LGPLv2.1+
See LICENSE.txt for more information See LICENSE.txt for more information
harvester.lua harvester.lua
Harvester machine to chop wood, leaves and harvest farming crops. Harvester machine to chop wood, leaves and harvest farming crops and flowers.
The machine is able to harvest an square area of up to 33x33 blocks (radius = 16). The machine is able to harvest an square area of up to 33x33 blocks (radius = 16).
The base node has to be placed in the middle of the harvesting area. The base node has to be placed in the middle of the harvesting area.
@ -19,17 +19,28 @@
]]-- ]]--
-- 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 CYCLE_TIME = 4 local CYCLE_TIME = 4
local MAX_HEIGHT = 18 local MAX_HEIGHT = 18 -- harvesting altitude
local MAX_DIAMETER = 33 local MAX_DIAMETER = 33
local BURNING_TIME = 20 local BURNING_TIME = 20 -- fuel
local TICKS_TO_SLEEP = 10 local STANDBY_TICKS = 4 -- used for blocked state
local STOP_STATE = 0 local COUNTDOWN_TICKS = 2
local RUNNING = 1 local OFFSET = 5 -- for uneven terrains
local STANDBY_STATE = -1
local FAULT_STATE = -3 local State = tubelib.NodeStates:new({
local OFFSET = 5 node_name_passive = "tubelib_addons1:harvester_base",
node_name_defect = "tubelib_addons1:harvester_defect",
infotext_name = "Tubelib Fermenter",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
has_item_meter = true,
aging_factor = 15,
})
local Radius2Idx = {[4]=1 ,[6]=2, [8]=3, [10]=4, [12]=5, [14]=6, [16]=7} local Radius2Idx = {[4]=1 ,[6]=2, [8]=3, [10]=4, [12]=5, [14]=6, [16]=7}
@ -57,11 +68,12 @@ end
local WorkingSteps = gen_working_steps() local WorkingSteps = gen_working_steps()
local function formspec(this, state) local function formspec(pos, meta)
-- some recalculations -- some recalculations
local this = minetest.deserialize(meta:get_string("this"))
local endless = this.endless == 1 and "true" or "false" local endless = this.endless == 1 and "true" or "false"
local fuel = this.fuel * 100/BURNING_TIME local fuel = this.fuel * 100/BURNING_TIME
if state ~= tubelib.RUNNING then if State:get_state(meta) ~= tubelib.RUNNING then
fuel = 0 fuel = 0
end end
local radius = Radius2Idx[this.radius] or 2 local radius = Radius2Idx[this.radius] or 2
@ -78,18 +90,19 @@ local function formspec(this, state)
"item_image[1.5,3;1,1;tubelib_addons1:biofuel]".. "item_image[1.5,3;1,1;tubelib_addons1:biofuel]"..
"image[2.5,3;1,1;default_furnace_fire_bg.png^[lowpart:".. "image[2.5,3;1,1;default_furnace_fire_bg.png^[lowpart:"..
fuel..":default_furnace_fire_fg.png]".. fuel..":default_furnace_fire_fg.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[current_player;main;0.5,4.3;8,4;]".. "list[current_player;main;0.5,4.3;8,4;]"..
"listring[context;main]".. "listring[context;main]"..
"listring[current_player;main]" "listring[current_player;main]"
end end
State:register_formspec_func(formspec)
local function allow_metadata_inventory_put(pos, listname, index, stack, player) local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
local meta = minetest.get_meta(pos) local inv = M(pos):get_inventory()
local inv = meta:get_inventory()
if listname == "main" then if listname == "main" then
return stack:get_count() return stack:get_count()
elseif listname == "fuel" and stack:get_name() == "tubelib_addons1:biofuel" then elseif listname == "fuel" and stack:get_name() == "tubelib_addons1:biofuel" then
@ -117,43 +130,6 @@ local function get_next_pos(old_pos, idx)
return vector.add(old_pos, core.facedir_to_dir(facedir)) return vector.add(old_pos, core.facedir_to_dir(facedir))
end end
local function start_the_machine(pos, this, meta)
this.running = RUNNING
this.working_pos = working_start_pos(pos)
meta:set_string("infotext", "Tubelib Harvester "..this.number..": running")
meta:set_string("formspec", formspec(this, tubelib.RUNNING))
minetest.get_node_timer(pos):start(CYCLE_TIME)
meta:set_string("this", minetest.serialize(this))
return false
end
local function stop_the_machine(pos, this, meta)
this.running = STOP_STATE
this.idx = 0
meta:set_string("infotext", "Tubelib Harvester "..this.number..": stopped")
meta:set_string("formspec", formspec(this, tubelib.STOPPED))
minetest.get_node_timer(pos):stop()
meta:set_string("this", minetest.serialize(this))
return false
end
local function goto_standby(pos, this, meta)
this.running = STANDBY_STATE
meta:set_string("infotext", "Tubelib Harvester "..this.number..": standby")
meta:set_string("formspec", formspec(this, tubelib.STANDBY))
minetest.get_node_timer(pos):start(CYCLE_TIME * TICKS_TO_SLEEP)
meta:set_string("this", minetest.serialize(this))
return false
end
local function goto_fault(pos, this, meta)
this.running = FAULT_STATE
meta:set_string("infotext", "Tubelib Harvester "..this.number..": fault")
meta:set_string("formspec", formspec(this, tubelib.FAULT))
minetest.get_node_timer(pos):stop()
meta:set_string("this", minetest.serialize(this))
return false
end
-- Remove saplings lying arround -- Remove saplings lying arround
local function remove_all_sapling_items(pos) local function remove_all_sapling_items(pos)
@ -168,7 +144,7 @@ end
-- Remove wood/leave nodes and place sapling if necessary -- Remove wood/leave nodes and place sapling if necessary
-- Return false if inventory is full -- Return false if inventory is full
-- else return true -- else return true
local function remove_or_replace_node(pos, inv, node, order) local function remove_or_replace_node(this, pos, inv, node, order)
local next_pos = table.copy(pos) local next_pos = table.copy(pos)
next_pos.y = next_pos.y - 1 next_pos.y = next_pos.y - 1
@ -180,6 +156,7 @@ local function remove_or_replace_node(pos, inv, node, order)
if next_node then if next_node then
minetest.remove_node(pos) minetest.remove_node(pos)
inv:add_item("main", ItemStack(order.drop)) inv:add_item("main", ItemStack(order.drop))
this.num_items = this.num_items + 1
if tubelib_addons1.GroundNodes[next_node.name] ~= nil and order.plant then -- hit the ground? if tubelib_addons1.GroundNodes[next_node.name] ~= nil and order.plant then -- hit the ground?
minetest.set_node(pos, {name=order.plant, paramtype2 = "wallmounted", param2=1}) minetest.set_node(pos, {name=order.plant, paramtype2 = "wallmounted", param2=1})
if order.t1 ~= nil then if order.t1 ~= nil then
@ -237,9 +214,9 @@ local function harvest_field(this, meta)
pos.y = y_pos pos.y = y_pos
local node = minetest.get_node_or_nil(pos) local node = minetest.get_node_or_nil(pos)
if node and node.name ~= "air" then if node and node.name ~= "air" then
local order = tubelib_addons1.FarmingNodes[node.name] local order = tubelib_addons1.FarmingNodes[node.name] or tubelib_addons1.Flowers[node.name]
if order then if order then
if not remove_or_replace_node(pos, inv, node, order) then if not remove_or_replace_node(this, pos, inv, node, order) then
return false return false
end end
else else
@ -250,43 +227,42 @@ local function harvest_field(this, meta)
return true return true
end end
local function not_standby(pos, this, meta) local function not_blocked(pos, this, meta)
if this and this.running == STANDBY_STATE then if State:get_state(meta) == tubelib.BLOCKED then
if harvest_field(this, meta) then if harvest_field(this, meta) then
minetest.after(0, start_the_machine, pos, this, meta) minetest.after(0, State.start, State, pos, meta)
end end
return false return false
end end
return true return true
end end
-- move the copter to the next pos and harvest the field below -- move the "harvesting copter" to the next pos and harvest the field below
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos) local meta = M(pos)
local this = minetest.deserialize(meta:get_string("this")) local this = minetest.deserialize(meta:get_string("this"))
--print(this.working_pos.x, this.working_pos.z, this.running) if not_blocked(pos, this, meta) then
if not_standby(pos, this, meta) then
if check_fuel(pos, this, meta) then if check_fuel(pos, this, meta) then
if calc_new_pos(pos, this, meta) then if calc_new_pos(pos, this, meta) then
this.num_items = 0
if harvest_field(this, meta) then if harvest_field(this, meta) then
meta:set_string("this", minetest.serialize(this)) meta:set_string("this", minetest.serialize(this))
meta:set_string("infotext", meta:set_string("infotext",
"Tubelib Harvester "..this.number.. "Tubelib Harvester "..this.number..
": running ("..this.idx.."/"..this.max..")") ": running ("..this.idx.."/"..this.max..")")
return true State:keep_running(pos, meta, COUNTDOWN_TICKS, this.num_items)
else else
goto_standby(pos, this, meta) State:blocked(pos, meta)
end end
else else
stop_the_machine(pos, this, meta) State:stop(pos, meta)
end end
else else
goto_fault(pos, this, meta) State:fault(pos, meta)
end end
return false
end end
return true return State:is_active(meta)
end end
@ -294,7 +270,7 @@ 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) local meta = M(pos)
local this = minetest.deserialize(meta:get_string("this")) local this = minetest.deserialize(meta:get_string("this"))
local radius = this.radius local radius = this.radius
@ -302,7 +278,7 @@ local function on_receive_fields(pos, formname, fields, player)
radius = tonumber(fields.radius) radius = tonumber(fields.radius)
end end
if radius ~= this.radius then if radius ~= this.radius then
stop_the_machine(pos, this, meta) State:stop(pos, meta)
this.radius = radius this.radius = radius
this.max = (radius*2 + 1) * (radius*2 + 1) this.max = (radius*2 + 1) * (radius*2 + 1)
end end
@ -311,15 +287,17 @@ local function on_receive_fields(pos, formname, fields, player)
this.endless = fields.endless == "true" and 1 or 0 this.endless = fields.endless == "true" and 1 or 0
end end
if fields.button ~= nil then if fields.state_button ~= nil then
if this.running > STOP_STATE then local state = State:get_state(meta)
stop_the_machine(pos, this, meta) if state == tubelib.STOPPED or state == tubelib.STANDBY or state == tubelib.BLOCKED then
else this.idx = 0
start_the_machine(pos, this, meta) this.working_pos = working_start_pos(pos)
State:start(pos, meta)
elseif state == tubelib.RUNNING or state == tubelib.FAULT then
State:stop(pos, meta)
end end
else
meta:set_string("formspec", formspec(this, tubelib.state(this.running)))
end end
meta:set_string("this", minetest.serialize(this)) meta:set_string("this", minetest.serialize(this))
end end
@ -328,40 +306,99 @@ minetest.register_node("tubelib_addons1:harvester_base", {
tiles = { tiles = {
-- up, down, right, left, back, front -- up, down, right, left, back, front
'tubelib_front.png', 'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_harvester.png', 'tubelib_addons1_harvester.png',
}, },
on_construct = function(pos) after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size('main', 16) inv:set_size('main', 16)
inv:set_size('fuel', 1) inv:set_size('fuel', 1)
end,
after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:harvester_base") local number = tubelib.add_node(pos, "tubelib_addons1:harvester_base")
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Tubelib Harvester "..number..": stopped")
local this = { local this = {
number = number, number = number,
owner = placer:get_player_name(), owner = placer:get_player_name(),
working_pos = working_start_pos(pos), working_pos = working_start_pos(pos),
fuel = 0, fuel = 0,
running = STOP_STATE,
endless = 0, endless = 0,
radius = 6, radius = 6,
idx = 0, idx = 0,
max = (6+1+6) * (6+1+6) max = (6+1+6) * (6+1+6)
} }
meta:set_string("this", minetest.serialize(this)) meta:set_string("this", minetest.serialize(this))
meta:set_string("formspec", formspec(this, tubelib.STOPPED)) State:node_init(pos, number)
end, end,
on_dig = function(pos, node, puncher, pointed_thing) on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then if minetest.is_protected(pos, puncher:get_player_name()) then
return return
end end
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory()
if inv:is_empty("main") then
minetest.node_dig(pos, node, puncher, pointed_thing)
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_receive_fields = on_receive_fields,
on_timer = keep_running,
allow_metadata_inventory_put = allow_metadata_inventory_put,
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_wood_defaults(),
})
minetest.register_node("tubelib_addons1:harvester_defect", {
description = "Tubelib Harvester Base",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_harvester.png^tubelib_defect.png',
},
on_construct = function(pos)
local inv = M(pos):get_inventory()
inv:set_size('main', 16)
inv:set_size('fuel', 1)
end,
after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:harvester_base")
local this = {
number = number,
owner = placer:get_player_name(),
working_pos = working_start_pos(pos),
fuel = 0,
endless = 0,
radius = 6,
idx = 0,
max = (6+1+6) * (6+1+6)
}
local meta = M(pos)
meta:set_string("this", minetest.serialize(this))
State:node_init(pos, number)
State:defect(pos, meta)
end,
on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
end
local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("main") then if inv:is_empty("main") then
minetest.node_dig(pos, node, puncher, pointed_thing) minetest.node_dig(pos, node, puncher, pointed_thing)
@ -370,38 +407,17 @@ minetest.register_node("tubelib_addons1:harvester_base", {
end, end,
on_rotate = screwdriver.disallow, on_rotate = screwdriver.disallow,
on_receive_fields = on_receive_fields,
on_timer = keep_running,
allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take, allow_metadata_inventory_take = allow_metadata_inventory_take,
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
groups = {choppy=2, cracky=2, crumbly=2}, paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory=1},
is_ground_content = false, is_ground_content = false,
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
minetest.register_node("tubelib_addons1:harvester_base_active", {
description = "Tubelib Harvester Base",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_addons1_harvester.png',
},
on_rotate = screwdriver.disallow,
on_receive_fields = on_receive_fields,
on_timer = keep_running,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
paramtype = "light",
sunlight_propagates = true,
groups = {crumbly=0, not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_craft({ minetest.register_craft({
output = "tubelib_addons1:harvester_base", output = "tubelib_addons1:harvester_base",
@ -413,50 +429,33 @@ minetest.register_craft({
}) })
tubelib.register_node("tubelib_addons1:harvester_base", {}, { tubelib.register_node("tubelib_addons1:harvester_base", {"tubelib_addons1:harvester_defect"}, {
on_pull_stack = function(pos, side)
local meta = minetest.get_meta(pos)
return tubelib.get_stack(meta, "main")
end,
on_pull_item = function(pos, side) on_pull_item = function(pos, side)
local meta = minetest.get_meta(pos) return tubelib.get_item(M(pos), "main")
return tubelib.get_item(meta, "main")
end, end,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
if item:get_name() == "tubelib_addons1:biofuel" then return tubelib.put_item(M(pos), "fuel", item)
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "fuel", item)
end
return false
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), "main", item)
return tubelib.put_item(meta, "main", item)
end, end,
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)
local meta = minetest.get_meta(pos) if topic == "fuel" then
local this = minetest.deserialize(meta:get_string("this")) return tubelib.fuelstate(M(pos), "fuel")
if topic == "on" then end
start_the_machine(pos, this, meta)
elseif topic == "off" then local resp = State:on_receive_message(pos, topic, payload)
stop_the_machine(pos, this, meta) if resp then
elseif topic == "state" then return resp
return tubelib.statestring(this.running)
elseif topic == "fuel" then
local meta = minetest.get_meta(pos)
return tubelib.fuelstate(meta, "fuel")
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)
local this = minetest.deserialize(meta:get_string("this")) end,
if this and this.running ~= tubelib.STATE_STOPPED then on_node_repair = function(pos)
this.running = tubelib.STATE_STANDBY return State:on_node_repair(pos)
meta:set_string("this", minetest.serialize(this))
minetest.get_node_timer(pos):start(CYCLE_TIME * TICKS_TO_SLEEP)
end
end, end,
}) })
@ -468,7 +467,7 @@ minetest.register_lbm({
nodenames = {"tubelib_addons1:harvester_base", "tubelib:harvester_base_active"}, nodenames = {"tubelib_addons1:harvester_base", "tubelib:harvester_base_active"},
run_at_every_load = false, run_at_every_load = false,
action = function(pos, node) action = function(pos, node)
local meta = minetest.get_meta(pos) local meta = M(pos)
local this = minetest.deserialize(meta:get_string("this")) local this = minetest.deserialize(meta:get_string("this"))
if this then if this then
this.working_pos = this.copter_pos or working_start_pos(pos) this.working_pos = this.copter_pos or working_start_pos(pos)

@ -3,7 +3,7 @@
Tubelib Addons 1 Tubelib Addons 1
================ ================
Copyright (C) 2017,2018 Joachim Stolberg Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+ LGPLv2.1+
See LICENSE.txt for more information See LICENSE.txt for more information
@ -12,8 +12,46 @@
]]-- ]]--
-- 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 = 2
local CYCLE_TIME = 8 local CYCLE_TIME = 8
local State = tubelib.NodeStates:new({
node_name_passive = "tubelib_addons1:liquidsampler",
node_name_active = "tubelib_addons1:liquidsampler_active",
node_name_defect = "tubelib_addons1:liquidsampler_defect",
infotext_name = "Liquid Sampler",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
has_item_meter = true,
aging_factor = 8,
})
local function formspec(pos, meta)
return "size[9,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;src;0,0;1,4;]"..
"image[0,0;1,1;bucket.png]"..
"image[1,1;1,1;tubelib_gui_arrow.png]"..
"image_button[1,3;1,1;".. State:get_state_button_image(meta) ..";state_button;]"..
"list[context;dst;2,0;7,4;]"..
"list[current_player;main;0.5,4.5;8,4;]"..
"listring[current_player;main]"..
"listring[context;src]" ..
"listring[current_player;main]"..
"listring[context;dst]" ..
"listring[current_player;main]"
end
State:register_formspec_func(formspec)
local function get_pos(pos, facedir, side) local function get_pos(pos, facedir, side)
local offs = {F=0, R=1, B=2, L=3, D=4, U=5} local offs = {F=0, R=1, B=2, L=3, D=4, U=5}
local dst_pos = table.copy(pos) local dst_pos = table.copy(pos)
@ -32,96 +70,41 @@ local function test_liquid(node)
end end
local function sample_liquid(pos, meta) local function sample_liquid(pos, meta)
local giving_back = test_liquid(minetest.get_node(pos)) local water_pos = P(meta:get_string("water_pos"))
local giving_back = test_liquid(minetest.get_node(water_pos))
if giving_back then if giving_back then
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:room_for_item("dst", ItemStack(giving_back)) and if inv:room_for_item("dst", ItemStack(giving_back)) and
inv:contains_item("src", ItemStack("bucket:bucket_empty")) then inv:contains_item("src", ItemStack("bucket:bucket_empty")) then
minetest.remove_node(pos) minetest.remove_node(water_pos)
inv:remove_item("src", ItemStack("bucket:bucket_empty")) inv:remove_item("src", ItemStack("bucket:bucket_empty"))
inv:add_item("dst", ItemStack(giving_back)) inv:add_item("dst", ItemStack(giving_back))
return true -- success State:keep_running(pos, meta, COUNTDOWN_TICKS)
else else
return nil -- standby State:idle(pos, meta)
end end
else else
return false -- fault State:fault(pos, meta)
end end
end State:idle(pos, meta)
local function formspec(meta, state)
return "size[9,8.5]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[context;src;0,0;1,4;]"..
"image[0,0;1,1;bucket.png]"..
"image[1,1;1,1;tubelib_gui_arrow.png]"..
"image_button[1,3;1,1;".. tubelib.state_button(state) ..";button;]"..
"list[context;dst;2,0;7,4;]"..
"list[current_player;main;0.5,4.5;8,4;]"..
"listring[current_player;main]"..
"listring[context;src]" ..
"listring[current_player;main]"..
"listring[context;dst]" ..
"listring[current_player;main]"
end
local function switch_on(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_RUNNING)
meta:set_string("infotext", "Liquid Sampler "..number..": running")
meta:set_string("formspec", formspec(meta, tubelib.RUNNING))
node.name = "tubelib_addons1:liquidsampler_active"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
return false
end
local function switch_off(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_STOPPED)
meta:set_string("infotext", "Liquid Sampler "..number..": stopped")
meta:set_string("formspec", formspec(meta, tubelib.STOPPED))
node.name = "tubelib_addons1:liquidsampler"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
return false
end
local function goto_fault(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_FAULT)
meta:set_string("infotext", "Liquid Sampler "..number..": fault")
meta:set_string("formspec", formspec(meta, tubelib.FAULT))
node.name = "tubelib_addons1:liquidsampler"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(20)
return false
end
local function goto_standby(pos, node)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
meta:set_int("running", tubelib.STATE_STANDBY)
meta:set_string("infotext", "Liquid Sampler "..number..": standby")
meta:set_string("formspec", formspec(meta, tubelib.STANDBY))
node.name = "tubelib_addons1:liquidsampler"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(20)
return false
end end
local function allow_metadata_inventory_put(pos, listname, index, stack, player) local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
if listname == "src" and State:get_state(M(pos)) == tubelib.STANDBY then
State:start(pos, M(pos))
end
return stack:get_count() return stack:get_count()
end end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
local inv = M(pos):get_inventory()
local stack = inv:get_stack(from_list, from_index)
return allow_metadata_inventory_put(pos, to_list, to_index, stack, player)
end
local function allow_metadata_inventory_take(pos, listname, index, stack, player) local function allow_metadata_inventory_take(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
@ -129,53 +112,17 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return stack:get_count() return stack:get_count()
end end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player) local function keep_running(pos, elapsed)
if minetest.is_protected(pos, player:get_player_name()) then local meta = M(pos)
return 0 sample_liquid(pos, meta)
end return State:is_active(meta)
return count
end end
local function on_receive_fields(pos, formname, fields, sender) local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, sender: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 node = minetest.get_node(pos)
local running = meta:get_int("running")
if fields.button ~= nil then
if running == tubelib.STATE_RUNNING then
switch_off(pos, node)
meta:set_int("running", tubelib.STATE_STOPPED)
else
meta:set_int("running", tubelib.STATE_RUNNING)
switch_on(pos, node)
end
end
end
local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos)
local running = meta:get_int("running")
local water_pos = minetest.string_to_pos(meta:get_string("water_pos"))
local res = sample_liquid(water_pos, meta)
if res == nil then
local node = minetest.get_node(pos)
return goto_standby(pos, node)
elseif res == true then
if running <= 0 then
local node = minetest.get_node(pos)
return switch_on(pos, node)
end
elseif res == false then
if running > 0 then
local node = minetest.get_node(pos)
return goto_fault(pos, node)
end
end
meta:set_int("running", running)
return true
end end
minetest.register_node("tubelib_addons1:liquidsampler", { minetest.register_node("tubelib_addons1:liquidsampler", {
@ -190,33 +137,39 @@ minetest.register_node("tubelib_addons1:liquidsampler", {
'tubelib_addons1_liquidsampler.png', 'tubelib_addons1_liquidsampler.png',
}, },
on_construct = function(pos) after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos) local number = tubelib.add_node(pos, "tubelib_addons1:grinder")
State:node_init(pos, number)
local meta = M(pos)
local node = minetest.get_node(pos)
local water_pos = get_pos(pos, node.param2, "L")
meta:set_string("water_pos", minetest.pos_to_string(water_pos))
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size("src", 4) inv:set_size("src", 4)
inv:set_size("dst", 28) inv:set_size("dst", 28)
end, end,
after_place_node = function(pos, placer) on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos) local meta = M(pos)
meta:set_string("player_name", placer:get_player_name()) local inv = meta:get_inventory()
local number = tubelib.add_node(pos, "tubelib_addons1:liquidsampler") if inv:is_empty("dst") and inv:is_empty("src") then
meta:set_string("number", number) minetest.node_dig(pos, node, puncher, pointed_thing)
local node = minetest.get_node(pos)
local water_pos = get_pos(pos, node.param2, "L")
meta:set_string("water_pos", minetest.pos_to_string(water_pos))
switch_off(pos, node)
end,
on_receive_fields = on_receive_fields,
after_dig_node = function(pos)
tubelib.remove_node(pos) tubelib.remove_node(pos)
end
end, end,
on_timer = keep_running, after_dig_node = function(pos, oldnode, oldmetadata, digger)
on_rotate = screwdriver.disallow, 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", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
paramtype2 = "facedir", paramtype2 = "facedir",
@ -225,7 +178,6 @@ minetest.register_node("tubelib_addons1:liquidsampler", {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
minetest.register_node("tubelib_addons1:liquidsampler_active", { minetest.register_node("tubelib_addons1:liquidsampler_active", {
description = "Liquid Sampler", description = "Liquid Sampler",
tiles = { tiles = {
@ -247,21 +199,65 @@ minetest.register_node("tubelib_addons1:liquidsampler_active", {
'tubelib_addons1_liquidsampler.png', 'tubelib_addons1_liquidsampler.png',
}, },
on_receive_fields = on_receive_fields,
on_timer = keep_running,
on_rotate = screwdriver.disallow, on_rotate = screwdriver.disallow,
on_timer = keep_running,
after_dig_node = function(pos) on_receive_fields = on_receive_fields,
tubelib.remove_node(pos) allow_metadata_inventory_put = allow_metadata_inventory_put,
end, allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
paramtype2 = "facedir", paramtype2 = "facedir",
groups = {crumbly=0, not_in_creative_inventory=1}, groups = {crumbly=0, not_in_creative_inventory=1},
is_ground_content = false, is_ground_content = false,
drop = "tubelib_addons1:liquidsampler", sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("tubelib_addons1:liquidsampler_defect", {
description = "Liquid Sampler",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_liquidsampler.png^tubelib_defect.png',
'tubelib_addons1_liquidsampler_passive.png^tubelib_defect.png',
'tubelib_addons1_liquidsampler.png^tubelib_defect.png',
'tubelib_addons1_liquidsampler.png^tubelib_defect.png',
},
after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:liquidsampler")
State:node_init(pos, number)
local meta = M(pos)
local node = minetest.get_node(pos)
local water_pos = get_pos(pos, node.param2, "L")
meta:set_string("water_pos", minetest.pos_to_string(water_pos))
local inv = meta:get_inventory()
inv:set_size("src", 4)
inv:set_size("dst", 28)
State:defect(pos, meta)
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = M(pos)
local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_dig(pos, node, puncher, pointed_thing)
tubelib.remove_node(pos)
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,
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_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
@ -274,42 +270,29 @@ minetest.register_craft({
}, },
}) })
--------------------------------------------------------------- tubelib tubelib.register_node("tubelib_addons1:liquidsampler",
tubelib.register_node("tubelib_addons1:liquidsampler", {"tubelib_addons1:liquidsampler_active"}, { {"tubelib_addons1:liquidsampler_active", "tubelib_addons1:liquidsampler_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)
minetest.get_node_timer(pos):start(CYCLE_TIME)
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)
local node = minetest.get_node(pos) local resp = State:on_receive_message(pos, topic, payload)
if topic == "on" then if resp then
return switch_on(pos, node) return resp
elseif topic == "off" then
return switch_off(pos, node)
elseif topic == "state" then
local meta = minetest.get_meta(pos)
local running = meta:get_int("running") or tubelib.STATE_STOPPED
return tubelib.statestring(running)
else else
return "not supported" 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") ~= tubelib.STATE_STOPPED then end,
meta:set_int("running", tubelib.STATE_STANDBY) on_node_repair = function(pos)
minetest.get_node_timer(pos):start(20) return State:on_node_repair(pos)
end
end, end,
}) })
--------------------------------------------------------------- tubelib

@ -12,8 +12,9 @@
tubelib_addons1 = {} tubelib_addons1 = {}
-- table needed for Harvester -- tables needed for Harvester
tubelib_addons1.FarmingNodes = {} tubelib_addons1.FarmingNodes = {}
tubelib_addons1.Flowers = {}
-- table needed for Grinder -- table needed for Grinder
tubelib_addons1.GroundNodes = {} tubelib_addons1.GroundNodes = {}
@ -29,6 +30,10 @@ function tubelib_addons1.register_default_farming_node(name, drop, plant)
tubelib_addons1.FarmingNodes[name] = {drop = drop or name, plant = plant, t1 = 2400, t2 = 4800} tubelib_addons1.FarmingNodes[name] = {drop = drop or name, plant = plant, t1 = 2400, t2 = 4800}
end end
function tubelib_addons1.register_flower(name)
tubelib_addons1.FarmingNodes[name] = {drop = name}
end
-- farming nodes with ABM -- farming nodes with ABM
function tubelib_addons1.register_farming_node(name, drop, plant) function tubelib_addons1.register_farming_node(name, drop, plant)
tubelib_addons1.FarmingNodes[name] = {drop = drop or name, plant = plant} tubelib_addons1.FarmingNodes[name] = {drop = drop or name, plant = plant}
@ -221,3 +226,18 @@ gn("ethereal:quicksand2", "default:sand")
gn("ethereal:illumishroom") gn("ethereal:illumishroom")
gn("ethereal:illumishroom2") gn("ethereal:illumishroom2")
gn("ethereal:illumishroom3") gn("ethereal:illumishroom3")
-------------------------------------------------------------------------------
-- Registered flowers
-------------------------------------------------------------------------------
local function register_flowers()
for name,item in pairs(minetest.registered_items) do
if item.groups.flower == 1 then
tubelib_addons1.register_flower(name)
end
end
end
minetest.after(10, register_flowers)

@ -3,7 +3,7 @@
Tubelib Addons 1 Tubelib Addons 1
================ ================
Copyright (C) 2017,2018 Joachim Stolberg Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+ LGPLv2.1+
See LICENSE.txt for more information See LICENSE.txt for more information
@ -12,7 +12,7 @@
Quarry machine to dig stones and other ground blocks. Quarry machine to dig stones and other ground blocks.
The Quarry digs a hole 5x5 blocks large and up to 25 blocks deep. The Quarry digs a hole 5x5 blocks large and up to 100 blocks deep.
It starts at the given level (0 is same level as the quarry block, It starts at the given level (0 is same level as the quarry block,
1 is one level higher and so on)) and goes down to the given depth number. 1 is one level higher and so on)) and goes down to the given depth number.
It digs one block every 4 seconds. It digs one block every 4 seconds.
@ -20,24 +20,39 @@
]]-- ]]--
-- 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 CYCLE_TIME = 4 local CYCLE_TIME = 4
local BURNING_TIME = 16 local BURNING_TIME = 16
local TICKS_TO_SLEEP = 5 local STANDBY_TICKS = 4
local STOP_STATE = 0 local COUNTDOWN_TICKS = 5
local FAULT_STATE = -3
local Depth2Idx = {[1]=1 ,[2]=2, [3]=3, [5]=4, [10]=5, [15]=6, [20]=7, [25]=8, [50]=9, [100]=10} local Depth2Idx = {[1]=1 ,[2]=2, [3]=3, [5]=4, [10]=5, [15]=6, [20]=7, [25]=8, [50]=9, [100]=10}
local Level2Idx = {[2]=1, [1]=2, [0]=3, [-1]=4, [-2]=5, [-3]=6, local Level2Idx = {[2]=1, [1]=2, [0]=3, [-1]=4, [-2]=5, [-3]=6,
[-5]=7, [-10]=8, [-15]=9, [-20]=10} [-5]=7, [-10]=8, [-15]=9, [-20]=10}
local function quarry_formspec(meta, state) local State = tubelib.NodeStates:new({
node_name_passive = "tubelib_addons1:quarry",
node_name_active = "tubelib_addons1:quarry_active",
node_name_defect = "tubelib_addons1:quarry_defect",
infotext_name = "Tubelib Quarry",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
has_item_meter = true,
aging_factor = 12,
})
local function formspec(pos, meta)
local depth = meta:get_int("max_levels") or 1 local depth = meta:get_int("max_levels") or 1
local start_level = meta:get_int("start_level") or 1 local start_level = meta:get_int("start_level") or 1
local endless = meta:get_int("endless") or 0 local endless = meta:get_int("endless") or 0
local fuel = meta:get_int("fuel") or 0 local fuel = meta:get_int("fuel") or 0
-- some recalculations -- some recalculations
endless = endless == 1 and "true" or "false" endless = endless == 1 and "true" or "false"
if state == tubelib.RUNNING then if State:get_state(meta) ~= tubelib.RUNNING then
fuel = fuel * 100/BURNING_TIME fuel = fuel * 100/BURNING_TIME
else else
fuel = 0 fuel = 0
@ -57,12 +72,14 @@ local function quarry_formspec(meta, state)
"item_image[1.5,3;1,1;tubelib_addons1:biofuel]".. "item_image[1.5,3;1,1;tubelib_addons1:biofuel]"..
"image[2.5,3;1,1;default_furnace_fire_bg.png^[lowpart:".. "image[2.5,3;1,1;default_furnace_fire_bg.png^[lowpart:"..
fuel..":default_furnace_fire_fg.png]".. fuel..":default_furnace_fire_fg.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[current_player;main;0.5,4.3;8,4;]".. "list[current_player;main;0.5,4.3;8,4;]"..
"listring[context;main]".. "listring[context;main]"..
"listring[current_player;main]" "listring[current_player;main]"
end end
State:register_formspec_func(formspec)
local function get_pos(pos, facedir, side) local function get_pos(pos, facedir, side)
local offs = {F=0, R=1, B=2, L=3, D=4, U=5} local offs = {F=0, R=1, B=2, L=3, D=4, U=5}
local dst_pos = table.copy(pos) local dst_pos = table.copy(pos)
@ -110,46 +127,6 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return stack:get_count() return stack:get_count()
end end
local function start_the_machine(pos)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local number = meta:get_string("number")
meta:set_int("running", TICKS_TO_SLEEP)
meta:set_string("infotext", "Tubelib Quarry "..number..": running")
meta:set_string("formspec", quarry_formspec(meta, tubelib.RUNNING))
node.name = "tubelib_addons1:quarry_active"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
return false
end
local function stop_the_machine(pos)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local number = meta:get_string("number")
meta:set_int("running", STOP_STATE)
meta:set_int("idx", 1) -- restart from the beginning
meta:set_string("quarry_pos", nil)
meta:set_string("infotext", "Tubelib Quarry "..number..": stopped")
meta:set_string("formspec", quarry_formspec(meta, tubelib.STOPPED))
node.name = "tubelib_addons1:quarry"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
return false
end
local function goto_fault(pos)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
local number = meta:get_string("number")
meta:set_int("running", FAULT_STATE)
meta:set_string("infotext", "Tubelib Quarry "..number..": fault")
meta:set_string("formspec", quarry_formspec(meta, tubelib.FAULT))
node.name = "tubelib_addons1:quarry"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
return false
end
local QuarrySchedule = {0,0,3,3,3,3,2,2,2,2,1,1,1,1,0,3,0,0,3,3,2,2,1,0,0} local QuarrySchedule = {0,0,3,3,3,3,2,2,2,2,1,1,1,1,0,3,0,0,3,3,2,2,1,0,0}
@ -160,14 +137,26 @@ local function get_next_pos(pos, facedir, dir)
end end
local function quarry_next_node(pos, meta) local function quarry_next_node(pos, meta)
-- check fuel
local fuel = meta:get_int("fuel") or 0
if fuel <= 0 then
if tubelib.get_this_item(meta, "fuel", 1) == nil then
State:fault(pos, meta)
return
end
fuel = BURNING_TIME
else
fuel = fuel - 1
end
meta:set_int("fuel", fuel)
local idx = meta:get_int("idx") local idx = meta:get_int("idx")
local facedir = meta:get_int("facedir") local facedir = meta:get_int("facedir")
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
local endless = meta:get_int("endless") local endless = meta:get_int("endless")
local curr_level = meta:get_int("curr_level") local curr_level = meta:get_int("curr_level")
local stop_level = pos.y + meta:get_int("start_level") local stop_level = pos.y + meta:get_int("start_level")
- meta:get_int("max_levels") - meta:get_int("max_levels") + 1
+ 1
local quarry_pos = minetest.string_to_pos(meta:get_string("quarry_pos")) local quarry_pos = minetest.string_to_pos(meta:get_string("quarry_pos"))
if quarry_pos == nil then if quarry_pos == nil then
@ -191,7 +180,8 @@ local function quarry_next_node(pos, meta)
curr_level = pos.y + meta:get_int("start_level") -- start level curr_level = pos.y + meta:get_int("start_level") -- start level
meta:set_int("idx", 1) meta:set_int("idx", 1)
meta:set_string("quarry_pos", nil) meta:set_string("quarry_pos", nil)
return false -- stopped State:stop(pos, meta)
return
end end
meta:set_int("curr_level", curr_level) meta:set_int("curr_level", curr_level)
meta:set_int("idx", idx) meta:set_int("idx", idx)
@ -199,14 +189,12 @@ local function quarry_next_node(pos, meta)
if minetest.is_protected(quarry_pos, owner) then if minetest.is_protected(quarry_pos, owner) then
minetest.chat_send_player(owner, "[Tubelib Quarry] Area is protected!") minetest.chat_send_player(owner, "[Tubelib Quarry] Area is protected!")
return nil -- fault State:fault(pos, meta)
return
end end
local node = get_node_lvm(quarry_pos) local node = get_node_lvm(quarry_pos)
if node == nil then if node then
return true
end
local number = meta:get_string("number") local number = meta:get_string("number")
local order = tubelib_addons1.GroundNodes[node.name] local order = tubelib_addons1.GroundNodes[node.name]
if order ~= nil then if order ~= nil then
@ -216,54 +204,28 @@ local function quarry_next_node(pos, meta)
inv:add_item("main", ItemStack(order.drop)) inv:add_item("main", ItemStack(order.drop))
meta:set_string("infotext", "Tubelib Quarry "..number.. meta:set_string("infotext", "Tubelib Quarry "..number..
": running "..idx.."/"..(curr_level-pos.y)) ": running "..idx.."/"..(curr_level-pos.y))
return true State:keep_running(pos, meta, COUNTDOWN_TICKS, 1)
else else
return nil -- fault State:blocked(pos, meta)
end
end end
else
meta:set_string("infotext", "Tubelib Quarry "..number.. meta:set_string("infotext", "Tubelib Quarry "..number..
": running "..idx.."/"..(curr_level-pos.y)) ": running "..idx.."/"..(curr_level-pos.y))
return true end
end
end end
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos) local meta = M(pos)
local running = meta:get_int("running") - 1 quarry_next_node(pos, meta)
local fuel = meta:get_int("fuel") or 0 return State:is_active(meta)
local inv = meta:get_inventory()
-- check fuel
if fuel <= 0 then
if tubelib.get_this_item(meta, "fuel", 1) == nil then
return goto_fault(pos)
end
fuel = BURNING_TIME
else
fuel = fuel - 1
end
meta:set_int("fuel", fuel)
local busy = quarry_next_node(pos, meta)
if busy == true then
if running <= STOP_STATE then
return start_the_machine(pos)
else
running = TICKS_TO_SLEEP
end
elseif busy == nil then
return goto_fault(pos)
else
return stop_the_machine(pos)
end
meta:set_int("running", running)
meta:set_string("formspec", quarry_formspec(meta, 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) local meta = M(pos)
local max_levels = meta:get_int("max_levels") local max_levels = meta:get_int("max_levels")
if fields.depth then if fields.depth then
@ -271,7 +233,7 @@ local function on_receive_fields(pos, formname, fields, player)
end end
if max_levels ~= meta:get_int("max_levels") then if max_levels ~= meta:get_int("max_levels") then
meta:set_string("quarry_pos", nil) -- reset the quarry meta:set_string("quarry_pos", nil) -- reset the quarry
stop_the_machine(pos) State:stop(pos, meta)
meta:set_int("max_levels", max_levels) meta:set_int("max_levels", max_levels)
end end
@ -281,7 +243,7 @@ local function on_receive_fields(pos, formname, fields, player)
end end
if start_level ~= meta:get_int("start_level") then if start_level ~= meta:get_int("start_level") then
meta:set_string("quarry_pos", nil) -- reset the quarry meta:set_string("quarry_pos", nil) -- reset the quarry
stop_the_machine(pos) State:stop(pos, meta)
meta:set_int("start_level", start_level) meta:set_int("start_level", start_level)
end end
@ -291,15 +253,15 @@ local function on_receive_fields(pos, formname, fields, player)
end end
meta:set_int("endless", endless) meta:set_int("endless", endless)
local running = meta:get_int("running") or STOP_STATE if fields.state_button ~= nil then
if fields.button ~= nil then local state = State:get_state(meta)
if running > STOP_STATE then if state == tubelib.STOPPED or state == tubelib.STANDBY or state == tubelib.BLOCKED then
stop_the_machine(pos) meta:set_int("idx", 1) -- restart from the beginning
else meta:set_string("quarry_pos", nil)
start_the_machine(pos) State:start(pos, meta)
elseif state == tubelib.RUNNING or state == tubelib.FAULT then
State:stop(pos, meta)
end end
else
meta:set_string("formspec", quarry_formspec(meta, tubelib.state(running)))
end end
end end
@ -315,36 +277,27 @@ minetest.register_node("tubelib_addons1:quarry", {
'tubelib_addons1_quarry.png^[transformFX', 'tubelib_addons1_quarry.png^[transformFX',
}, },
on_construct = function(pos) after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size('main', 16) inv:set_size('main', 16)
inv:set_size('fuel', 1) inv:set_size('fuel', 1)
end,
after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:quarry") local number = tubelib.add_node(pos, "tubelib_addons1:quarry")
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Quarry "..number..": stopped")
local facedir = minetest.dir_to_facedir(placer:get_look_dir(), false) local facedir = minetest.dir_to_facedir(placer:get_look_dir(), false)
meta:set_int("facedir", facedir) meta:set_int("facedir", facedir)
meta:set_string("number", number) meta:set_string("number", number)
meta:set_string("owner", placer:get_player_name()) meta:set_string("owner", placer:get_player_name())
meta:set_int("running", STOP_STATE)
meta:set_int("endless", 0) meta:set_int("endless", 0)
meta:set_int("curr_level", -1) meta:set_int("curr_level", -1)
meta:set_int("max_levels", 1) meta:set_int("max_levels", 1)
meta:set_string("formspec", quarry_formspec(meta, tubelib.STOPPED)) State:node_init(pos, number)
end, end,
on_receive_fields = on_receive_fields,
on_rotate = screwdriver.disallow,
on_dig = function(pos, node, puncher, pointed_thing) on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then if minetest.is_protected(pos, puncher:get_player_name()) then
return return
end end
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("main") then if inv:is_empty("main") then
minetest.node_dig(pos, node, puncher, pointed_thing) minetest.node_dig(pos, node, puncher, pointed_thing)
@ -352,9 +305,17 @@ minetest.register_node("tubelib_addons1:quarry", {
end end
end, end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
end,
on_rotate = screwdriver.disallow,
on_receive_fields = on_receive_fields,
on_timer = keep_running,
allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take, allow_metadata_inventory_take = allow_metadata_inventory_take,
drop = "",
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
paramtype2 = "facedir", paramtype2 = "facedir",
@ -363,7 +324,6 @@ minetest.register_node("tubelib_addons1:quarry", {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
minetest.register_node("tubelib_addons1:quarry_active", { minetest.register_node("tubelib_addons1:quarry_active", {
description = "Tubelib Quarry", description = "Tubelib Quarry",
tiles = { tiles = {
@ -402,6 +362,48 @@ minetest.register_node("tubelib_addons1:quarry_active", {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
}) })
minetest.register_node("tubelib_addons1:quarry_defect", {
description = "Tubelib Quarry",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_quarry.png^tubelib_defect.png',
'tubelib_addons1_quarry_passive.png^tubelib_defect.png',
'tubelib_addons1_quarry.png^tubelib_defect.png',
'tubelib_addons1_quarry.png^[transformFX^tubelib_defect.png',
},
after_place_node = function(pos, placer)
local number = tubelib.add_node(pos, "tubelib_addons1:quarry")
State:node_init(pos, number)
State:defect(pos, M(pos))
end,
on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
end
local meta = M(pos)
local inv = meta:get_inventory()
if inv:is_empty("main") then
minetest.node_dig(pos, node, puncher, pointed_thing)
tubelib.remove_node(pos)
end
end,
on_rotate = screwdriver.disallow,
allow_metadata_inventory_put = allow_metadata_inventory_put,
allow_metadata_inventory_take = allow_metadata_inventory_take,
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_wood_defaults(),
})
minetest.register_craft({ minetest.register_craft({
output = "tubelib_addons1:quarry", output = "tubelib_addons1:quarry",
recipe = { recipe = {
@ -412,45 +414,34 @@ minetest.register_craft({
}) })
tubelib.register_node("tubelib_addons1:quarry", {"tubelib_addons1:quarry_active"}, { tubelib.register_node("tubelib_addons1:quarry",
{"tubelib_addons1:quarry_active", "tubelib_addons1:quarry_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), "main")
return tubelib.get_item(meta, "main")
end, end,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
if item:get_name() == "tubelib_addons1:biofuel" then return tubelib.put_item(M(pos), "fuel", item)
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "fuel", item)
end
return false
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), "main", item)
return tubelib.put_item(meta, "main", item)
end, end,
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)
if topic == "on" then if topic == "fuel" then
start_the_machine(pos) return tubelib.fuelstate(M(pos), "fuel")
elseif topic == "off" then end
stop_the_machine(pos)
elseif topic == "state" then local resp = State:on_receive_message(pos, topic, payload)
local meta = minetest.get_meta(pos) if resp then
local running = meta:get_int("running") return resp
return tubelib.statestring(running)
elseif topic == "fuel" then
local meta = minetest.get_meta(pos)
return tubelib.fuelstate(meta, "fuel")
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", TICKS_TO_SLEEP) on_node_repair = function(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME) return State:on_node_repair(pos)
end
end, end,
}) })

@ -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,13 +15,26 @@
]]-- ]]--
local CYCLE_TIME = 6 -- 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 function formspec(state) local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
local CYCLE_TIME = 2
local State = tubelib.NodeStates:new({
node_name_passive = "tubelib_addons1:reformer",
node_name_defect = "tubelib_addons1:reformer_defect",
infotext_name = "Tubelib Reformer",
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..
@ -29,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;tubelib_addons1:biogas]".. "item_image[0,0;1,1;tubelib_addons1:biogas]"..
"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:biofuel]".. "item_image[5,0;1,1;tubelib_addons1:biofuel]"..
"list[current_player;main;0,4.3;8,4;]".. "list[current_player;main;0,4.3;8,4;]"..
@ -39,13 +52,18 @@ local function formspec(state)
"listring[current_player;main]" "listring[current_player;main]"
end end
State:register_formspec_func(formspec)
local function allow_metadata_inventory_put(pos, listname, index, stack, player) local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return 0 return 0
end end
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if listname == "src" and stack:get_name() == "tubelib_addons1:biogas" then if listname == "src" and stack:get_name() == "tubelib_addons1:biogas" then
if State:get_state(meta) == tubelib.STANDBY then
State:start(pos, meta)
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()
@ -54,7 +72,7 @@ local function allow_metadata_inventory_put(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)
@ -80,7 +98,7 @@ local function place_top(pos, facedir, placer)
return true return true
end end
local function convert_biogas_to_biofuel(meta) local function convert_biogas_to_biofuel(pos, meta)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local biofuel = ItemStack("tubelib_addons1:biofuel") local biofuel = ItemStack("tubelib_addons1:biofuel")
if inv:room_for_item("dst", biofuel) then -- enough output space? if inv:room_for_item("dst", biofuel) then -- enough output space?
@ -88,96 +106,35 @@ local function convert_biogas_to_biofuel(meta)
if items then -- input available? if items then -- input available?
if items:get_name() == "tubelib_addons1:biogas" then -- valid input? if items:get_name() == "tubelib_addons1:biogas" then -- valid input?
inv:add_item("dst", biofuel) inv:add_item("dst", biofuel)
return true State:keep_running(pos, meta, COUNTDOWN_TICKS)
return
else else
inv:add_item("src", items) inv:add_item("src", items)
return nil -- error State:fault(pos, meta)
return
end end
else else
return false -- standby State:idle(pos, meta)
return
end end
else else
return false -- standby State:blocked(pos, meta)
return
end end
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 Reformer "..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 Reformer "..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 Reformer "..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 Reformer "..number..": fault")
meta:set_string("formspec", formspec(tubelib.FAULT))
return false
end
local function keep_running(pos, elapsed) local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos) local meta = M(pos)
local running = meta:get_int("running") - 1 convert_biogas_to_biofuel(pos, meta)
local res = convert_biogas_to_biofuel(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
@ -200,7 +157,7 @@ minetest.register_node("tubelib_addons1:reformer", {
}, },
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
inv:set_size('src', 9) inv:set_size('src', 9)
inv:set_size('dst', 9) inv:set_size('dst', 9)
@ -213,15 +170,11 @@ minetest.register_node("tubelib_addons1:reformer", {
return return
end end
local number = tubelib.add_node(pos, "tubelib_addons1:reformer") local number = tubelib.add_node(pos, "tubelib_addons1:reformer")
local meta = minetest.get_meta(pos) State:node_init(pos, number)
meta:set_string("number", number)
meta:set_int("running", STOP_STATE)
meta:set_string("infotext", "Tubelib Reformer "..number..": stopped")
meta:set_string("formspec", formspec(tubelib.STOPPED))
end, end,
on_dig = function(pos, node, puncher, pointed_thing) on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos) local meta = M(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("dst") and inv:is_empty("src") then if inv:is_empty("dst") and inv:is_empty("src") then
minetest.node_dig(pos, node, puncher, pointed_thing) minetest.node_dig(pos, node, puncher, pointed_thing)
@ -230,6 +183,10 @@ minetest.register_node("tubelib_addons1:reformer", {
end end
end, end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
end,
on_rotate = screwdriver.disallow, on_rotate = screwdriver.disallow,
on_timer = keep_running, on_timer = keep_running,
on_receive_fields = on_receive_fields, on_receive_fields = on_receive_fields,
@ -237,6 +194,7 @@ minetest.register_node("tubelib_addons1:reformer", {
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,
drop = "",
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
paramtype2 = "facedir", paramtype2 = "facedir",
@ -245,6 +203,65 @@ minetest.register_node("tubelib_addons1:reformer", {
sounds = default.node_sound_metal_defaults(), sounds = default.node_sound_metal_defaults(),
}) })
minetest.register_node("tubelib_addons1:reformer_defect", {
description = "Tubelib Reformer",
inventory_image = "tubelib_addons1_reformer_inventory.png",
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_reformer1_bottom.png^tubelib_defect.png',
'tubelib_addons1_reformer1_bottom.png^tubelib_defect.png',
'tubelib_addons1_reformer2_bottom.png^tubelib_defect.png',
'tubelib_addons1_reformer2_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 = M(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:reformer")
State:node_init(pos, number)
State:defect(pos, M(pos))
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = M(pos)
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,
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,
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(),
})
minetest.register_node("tubelib_addons1:reformer_top", { minetest.register_node("tubelib_addons1:reformer_top", {
description = "Tubelib Reformer Top", description = "Tubelib Reformer Top",
@ -288,37 +305,28 @@ minetest.register_craft({
}) })
tubelib.register_node("tubelib_addons1:reformer", {}, { tubelib.register_node("tubelib_addons1:reformer", {"tubelib_addons1:reformer_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,
}) })