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
============
Copyright (C) 2017 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
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
# This aging value is used to calculate the lifetime of machines before
# they go defect . The value 100 (default) results in a lifetime
# for standard machines of about 1000 - 4000 item processing cycles.
tubelib_machine_aging_value (machine aging value) int 100
# they go defect . The value 200 (default) results in a lifetime
# for standard machines of about 2000 - 8000 item processing cycles.
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
return 0
end
local meta = minetest.get_meta(pos)
local meta = M(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))
if State:get_state(meta) == tubelib.STANDBY then
State:start(pos, meta)
end
return stack:get_count()
elseif listname == "dst" then
@ -78,7 +78,7 @@ local function allow_metadata_inventory_put(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)
@ -142,7 +142,7 @@ local function convert_leaves_to_biogas(pos, meta)
end
local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos)
local meta = M(pos)
convert_leaves_to_biogas(pos, meta)
return State:is_active(meta)
end
@ -173,7 +173,7 @@ minetest.register_node("tubelib_addons1:fermenter", {
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local meta = M(pos)
local inv = meta:get_inventory()
inv:set_size('src', 9)
inv:set_size('dst', 9)
@ -190,7 +190,7 @@ minetest.register_node("tubelib_addons1:fermenter", {
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos)
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)
@ -238,7 +238,7 @@ minetest.register_node("tubelib_addons1:fermenter_defect", {
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local meta = M(pos)
local inv = meta:get_inventory()
inv:set_size('src', 9)
inv:set_size('dst', 9)
@ -256,7 +256,7 @@ minetest.register_node("tubelib_addons1:fermenter_defect", {
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos)
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)
@ -332,9 +332,9 @@ end
minetest.register_craft({
output = "tubelib_addons1:fermenter",
recipe = {
{"default:steel_ingot", "default:dirt", "default:steel_ingot"},
{"tubelib:tube1", "default:mese_crystal", "tubelib:tube1"},
{"default:steel_ingot", "group:wood", "default:steel_ingot"},
{"default:steel_ingot", "default:dirt", "default:steel_ingot"},
{"tubelib:tube1", "default:mese_crystal", "tubelib:tube1"},
{"default:steel_ingot", "group:wood", "default:steel_ingot"},
},
})

@ -3,7 +3,7 @@
Tubelib Addons 1
================
Copyright (C) 2017,2018 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
@ -129,9 +129,9 @@ minetest.register_node("tubelib_addons1:funnel", {
minetest.register_craft({
output = "tubelib_addons1:funnel 2",
recipe = {
{"group:wood", "", "group:wood"},
{"group:wood", "", "group:wood"},
{"default:steel_ingot", "default:mese_crystal", "tubelib:tube1"},
{"group:wood", "", "group:wood"},
{"group:wood", "", "group:wood"},
},
})

@ -3,7 +3,7 @@
Tubelib Addons 1
================
Copyright (C) 2017,2018 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
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
return 0
end
local inv = M(pos):get_inventory()
if listname == "src" and State:get_state(M(pos)) == tubelib.STANDBY then
State:start(pos, M(pos))
end
@ -211,7 +210,7 @@ minetest.register_node("tubelib_addons1:grinder_defect", {
tiles = {
-- up, down, right, left, back, front
'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",
@ -253,9 +252,9 @@ minetest.register_node("tubelib_addons1:grinder_defect", {
minetest.register_craft({
output = "tubelib_addons1:grinder",
recipe = {
{"group:wood", "default:tin_ingot", "group:wood"},
{"tubelib:tube1", "default:mese_crystal", "tubelib:tube1"},
{"group:wood", "default:tin_ingot", "group:wood"},
{"group:wood", "default:tin_ingot", "group:wood"},
{"tubelib:tube1", "default:mese_crystal", "tubelib:tube1"},
{"group:wood", "default:tin_ingot", "group:wood"},
},
})

@ -3,14 +3,14 @@
Tubelib Addons 1
================
Copyright (C) 2017,2018 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
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 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 MAX_HEIGHT = 18
local MAX_HEIGHT = 18 -- harvesting altitude
local MAX_DIAMETER = 33
local BURNING_TIME = 20
local TICKS_TO_SLEEP = 10
local STOP_STATE = 0
local RUNNING = 1
local STANDBY_STATE = -1
local FAULT_STATE = -3
local OFFSET = 5
local BURNING_TIME = 20 -- fuel
local STANDBY_TICKS = 4 -- used for blocked state
local COUNTDOWN_TICKS = 2
local OFFSET = 5 -- for uneven terrains
local State = tubelib.NodeStates:new({
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}
@ -57,11 +68,12 @@ end
local WorkingSteps = gen_working_steps()
local function formspec(this, state)
local function formspec(pos, meta)
-- some recalculations
local this = minetest.deserialize(meta:get_string("this"))
local endless = this.endless == 1 and "true" or "false"
local fuel = this.fuel * 100/BURNING_TIME
if state ~= tubelib.RUNNING then
if State:get_state(meta) ~= tubelib.RUNNING then
fuel = 0
end
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]"..
"image[2.5,3;1,1;default_furnace_fire_bg.png^[lowpart:"..
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;]"..
"listring[context;main]"..
"listring[current_player;main]"
end
State:register_formspec_func(formspec)
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local inv = M(pos):get_inventory()
if listname == "main" then
return stack:get_count()
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))
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
local function remove_all_sapling_items(pos)
@ -168,7 +144,7 @@ end
-- Remove wood/leave nodes and place sapling if necessary
-- Return false if inventory is full
-- 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)
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
minetest.remove_node(pos)
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?
minetest.set_node(pos, {name=order.plant, paramtype2 = "wallmounted", param2=1})
if order.t1 ~= nil then
@ -237,9 +214,9 @@ local function harvest_field(this, meta)
pos.y = y_pos
local node = minetest.get_node_or_nil(pos)
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 not remove_or_replace_node(pos, inv, node, order) then
if not remove_or_replace_node(this, pos, inv, node, order) then
return false
end
else
@ -250,43 +227,42 @@ local function harvest_field(this, meta)
return true
end
local function not_standby(pos, this, meta)
if this and this.running == STANDBY_STATE then
local function not_blocked(pos, this, meta)
if State:get_state(meta) == tubelib.BLOCKED 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
return false
end
return true
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 meta = minetest.get_meta(pos)
local meta = M(pos)
local this = minetest.deserialize(meta:get_string("this"))
--print(this.working_pos.x, this.working_pos.z, this.running)
if not_standby(pos, this, meta) then
if not_blocked(pos, this, meta) then
if check_fuel(pos, this, meta) then
if calc_new_pos(pos, this, meta) then
this.num_items = 0
if harvest_field(this, meta) then
meta:set_string("this", minetest.serialize(this))
meta:set_string("infotext",
"Tubelib Harvester "..this.number..
": running ("..this.idx.."/"..this.max..")")
return true
State:keep_running(pos, meta, COUNTDOWN_TICKS, this.num_items)
else
goto_standby(pos, this, meta)
State:blocked(pos, meta)
end
else
stop_the_machine(pos, this, meta)
State:stop(pos, meta)
end
else
goto_fault(pos, this, meta)
State:fault(pos, meta)
end
return false
end
return true
return State:is_active(meta)
end
@ -294,7 +270,7 @@ 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 meta = M(pos)
local this = minetest.deserialize(meta:get_string("this"))
local radius = this.radius
@ -302,7 +278,7 @@ local function on_receive_fields(pos, formname, fields, player)
radius = tonumber(fields.radius)
end
if radius ~= this.radius then
stop_the_machine(pos, this, meta)
State:stop(pos, meta)
this.radius = radius
this.max = (radius*2 + 1) * (radius*2 + 1)
end
@ -311,15 +287,17 @@ local function on_receive_fields(pos, formname, fields, player)
this.endless = fields.endless == "true" and 1 or 0
end
if fields.button ~= nil then
if this.running > STOP_STATE then
stop_the_machine(pos, this, meta)
else
start_the_machine(pos, this, meta)
if fields.state_button ~= nil then
local state = State:get_state(meta)
if state == tubelib.STOPPED or state == tubelib.STANDBY or state == tubelib.BLOCKED then
this.idx = 0
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
else
meta:set_string("formspec", formspec(this, tubelib.state(this.running)))
end
meta:set_string("this", minetest.serialize(this))
end
@ -328,40 +306,99 @@ minetest.register_node("tubelib_addons1:harvester_base", {
tiles = {
-- up, down, right, left, back, front
'tubelib_front.png',
'tubelib_front.png',
'tubelib_addons1_harvester.png',
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
after_place_node = function(pos, placer)
local meta = M(pos)
local inv = meta: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 meta = minetest.get_meta(pos)
meta:set_string("infotext", "Tubelib Harvester "..number..": stopped")
local this = {
number = number,
owner = placer:get_player_name(),
working_pos = working_start_pos(pos),
fuel = 0,
running = STOP_STATE,
endless = 0,
radius = 6,
idx = 0,
max = (6+1+6) * (6+1+6)
}
meta:set_string("this", minetest.serialize(this))
meta:set_string("formspec", formspec(this, tubelib.STOPPED))
State:node_init(pos, number)
end,
on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
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()
if inv:is_empty("main") then
minetest.node_dig(pos, node, puncher, pointed_thing)
@ -370,38 +407,17 @@ minetest.register_node("tubelib_addons1:harvester_base", {
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,
paramtype = "light",
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,
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({
output = "tubelib_addons1:harvester_base",
@ -413,50 +429,33 @@ minetest.register_craft({
})
tubelib.register_node("tubelib_addons1:harvester_base", {}, {
on_pull_stack = function(pos, side)
local meta = minetest.get_meta(pos)
return tubelib.get_stack(meta, "main")
end,
tubelib.register_node("tubelib_addons1:harvester_base", {"tubelib_addons1:harvester_defect"}, {
on_pull_item = function(pos, side)
local meta = minetest.get_meta(pos)
return tubelib.get_item(meta, "main")
return tubelib.get_item(M(pos), "main")
end,
on_push_item = function(pos, side, item)
if item:get_name() == "tubelib_addons1:biofuel" then
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "fuel", item)
end
return false
return tubelib.put_item(M(pos), "fuel", item)
end,
on_unpull_item = function(pos, side, item)
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "main", item)
return tubelib.put_item(M(pos), "main", item)
end,
on_recv_message = function(pos, topic, payload)
local meta = minetest.get_meta(pos)
local this = minetest.deserialize(meta:get_string("this"))
if topic == "on" then
start_the_machine(pos, this, meta)
elseif topic == "off" then
stop_the_machine(pos, this, meta)
elseif topic == "state" then
return tubelib.statestring(this.running)
elseif topic == "fuel" then
local meta = minetest.get_meta(pos)
return tubelib.fuelstate(meta, "fuel")
if topic == "fuel" then
return tubelib.fuelstate(M(pos), "fuel")
end
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)
local this = minetest.deserialize(meta:get_string("this"))
if this and this.running ~= tubelib.STATE_STOPPED then
this.running = tubelib.STATE_STANDBY
meta:set_string("this", minetest.serialize(this))
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,
})
@ -468,7 +467,7 @@ minetest.register_lbm({
nodenames = {"tubelib_addons1:harvester_base", "tubelib:harvester_base_active"},
run_at_every_load = false,
action = function(pos, node)
local meta = minetest.get_meta(pos)
local meta = M(pos)
local this = minetest.deserialize(meta:get_string("this"))
if this then
this.working_pos = this.copter_pos or working_start_pos(pos)

@ -3,7 +3,7 @@
Tubelib Addons 1
================
Copyright (C) 2017,2018 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
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 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 offs = {F=0, R=1, B=2, L=3, D=4, U=5}
local dst_pos = table.copy(pos)
@ -32,96 +70,41 @@ local function test_liquid(node)
end
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
local inv = meta:get_inventory()
if inv:room_for_item("dst", ItemStack(giving_back)) and
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:add_item("dst", ItemStack(giving_back))
return true -- success
State:keep_running(pos, meta, COUNTDOWN_TICKS)
else
return nil -- standby
State:idle(pos, meta)
end
else
return false -- fault
State:fault(pos, meta)
end
end
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
State:idle(pos, meta)
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "src" and State:get_state(M(pos)) == tubelib.STANDBY then
State:start(pos, M(pos))
end
return stack:get_count()
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)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
@ -129,53 +112,17 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
return stack:get_count()
end
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return count
local function keep_running(pos, elapsed)
local meta = M(pos)
sample_liquid(pos, meta)
return State:is_active(meta)
end
local function on_receive_fields(pos, formname, fields, sender)
if minetest.is_protected(pos, sender:get_player_name()) then
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 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
State:state_button_event(pos, fields)
end
minetest.register_node("tubelib_addons1:liquidsampler", {
@ -190,33 +137,39 @@ minetest.register_node("tubelib_addons1:liquidsampler", {
'tubelib_addons1_liquidsampler.png',
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
after_place_node = function(pos, placer)
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()
inv:set_size("src", 4)
inv:set_size("dst", 28)
end,
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("player_name", placer:get_player_name())
local number = tubelib.add_node(pos, "tubelib_addons1:liquidsampler")
meta:set_string("number", number)
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)
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_receive_fields = on_receive_fields,
after_dig_node = function(pos)
tubelib.remove_node(pos)
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
end,
on_timer = keep_running,
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",
@ -225,7 +178,6 @@ minetest.register_node("tubelib_addons1:liquidsampler", {
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("tubelib_addons1:liquidsampler_active", {
description = "Liquid Sampler",
tiles = {
@ -247,21 +199,65 @@ minetest.register_node("tubelib_addons1:liquidsampler_active", {
'tubelib_addons1_liquidsampler.png',
},
on_receive_fields = on_receive_fields,
on_timer = keep_running,
on_rotate = screwdriver.disallow,
after_dig_node = function(pos)
tubelib.remove_node(pos)
end,
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,
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {crumbly=0, not_in_creative_inventory=1},
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(),
})
@ -274,42 +270,29 @@ minetest.register_craft({
},
})
--------------------------------------------------------------- tubelib
tubelib.register_node("tubelib_addons1:liquidsampler", {"tubelib_addons1:liquidsampler_active"}, {
tubelib.register_node("tubelib_addons1:liquidsampler",
{"tubelib_addons1:liquidsampler_active", "tubelib_addons1:liquidsampler_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)
minetest.get_node_timer(pos):start(CYCLE_TIME)
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)
local node = minetest.get_node(pos)
if topic == "on" then
return switch_on(pos, node)
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)
local resp = State:on_receive_message(pos, topic, payload)
if resp then
return resp
else
return "not supported"
return "unsupported"
end
end,
on_node_load = function(pos)
local meta = minetest.get_meta(pos)
if meta:get_int("running") ~= tubelib.STATE_STOPPED then
meta:set_int("running", tubelib.STATE_STANDBY)
minetest.get_node_timer(pos):start(20)
end
State:on_node_load(pos)
end,
on_node_repair = function(pos)
return State:on_node_repair(pos)
end,
})
--------------------------------------------------------------- tubelib

@ -12,8 +12,9 @@
tubelib_addons1 = {}
-- table needed for Harvester
-- tables needed for Harvester
tubelib_addons1.FarmingNodes = {}
tubelib_addons1.Flowers = {}
-- table needed for Grinder
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}
end
function tubelib_addons1.register_flower(name)
tubelib_addons1.FarmingNodes[name] = {drop = name}
end
-- farming nodes with ABM
function tubelib_addons1.register_farming_node(name, drop, 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:illumishroom2")
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
================
Copyright (C) 2017,2018 Joachim Stolberg
Copyright (C) 2017-2019 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
@ -12,7 +12,7 @@
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,
1 is one level higher and so on)) and goes down to the given depth number.
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 BURNING_TIME = 16
local TICKS_TO_SLEEP = 5
local STOP_STATE = 0
local FAULT_STATE = -3
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 5
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,
[-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 start_level = meta:get_int("start_level") or 1
local endless = meta:get_int("endless") or 0
local fuel = meta:get_int("fuel") or 0
-- some recalculations
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
else
fuel = 0
@ -57,12 +72,14 @@ local function quarry_formspec(meta, state)
"item_image[1.5,3;1,1;tubelib_addons1:biofuel]"..
"image[2.5,3;1,1;default_furnace_fire_bg.png^[lowpart:"..
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;]"..
"listring[context;main]"..
"listring[current_player;main]"
end
State:register_formspec_func(formspec)
local function get_pos(pos, facedir, side)
local offs = {F=0, R=1, B=2, L=3, D=4, U=5}
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()
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}
@ -160,14 +137,26 @@ local function get_next_pos(pos, facedir, dir)
end
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 facedir = meta:get_int("facedir")
local owner = meta:get_string("owner")
local endless = meta:get_int("endless")
local curr_level = meta:get_int("curr_level")
local stop_level = pos.y + meta:get_int("start_level")
- meta:get_int("max_levels")
+ 1
- meta:get_int("max_levels") + 1
local quarry_pos = minetest.string_to_pos(meta:get_string("quarry_pos"))
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
meta:set_int("idx", 1)
meta:set_string("quarry_pos", nil)
return false -- stopped
State:stop(pos, meta)
return
end
meta:set_int("curr_level", curr_level)
meta:set_int("idx", idx)
@ -199,71 +189,43 @@ local function quarry_next_node(pos, meta)
if minetest.is_protected(quarry_pos, owner) then
minetest.chat_send_player(owner, "[Tubelib Quarry] Area is protected!")
return nil -- fault
State:fault(pos, meta)
return
end
local node = get_node_lvm(quarry_pos)
if node == nil then
return true
end
local number = meta:get_string("number")
local order = tubelib_addons1.GroundNodes[node.name]
if order ~= nil then
local inv = meta:get_inventory()
if inv:room_for_item("main", ItemStack(order.drop)) then
minetest.remove_node(quarry_pos)
inv:add_item("main", ItemStack(order.drop))
if node then
local number = meta:get_string("number")
local order = tubelib_addons1.GroundNodes[node.name]
if order ~= nil then
local inv = meta:get_inventory()
if inv:room_for_item("main", ItemStack(order.drop)) then
minetest.remove_node(quarry_pos)
inv:add_item("main", ItemStack(order.drop))
meta:set_string("infotext", "Tubelib Quarry "..number..
": running "..idx.."/"..(curr_level-pos.y))
State:keep_running(pos, meta, COUNTDOWN_TICKS, 1)
else
State:blocked(pos, meta)
end
else
meta:set_string("infotext", "Tubelib Quarry "..number..
": running "..idx.."/"..(curr_level-pos.y))
return true
else
return nil -- fault
end
end
meta:set_string("infotext", "Tubelib Quarry "..number..
": running "..idx.."/"..(curr_level-pos.y))
return true
end
local function keep_running(pos, elapsed)
local meta = minetest.get_meta(pos)
local running = meta:get_int("running") - 1
local fuel = meta:get_int("fuel") or 0
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
local meta = M(pos)
quarry_next_node(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 meta = M(pos)
local max_levels = meta:get_int("max_levels")
if fields.depth then
@ -271,7 +233,7 @@ local function on_receive_fields(pos, formname, fields, player)
end
if max_levels ~= meta:get_int("max_levels") then
meta:set_string("quarry_pos", nil) -- reset the quarry
stop_the_machine(pos)
State:stop(pos, meta)
meta:set_int("max_levels", max_levels)
end
@ -281,7 +243,7 @@ local function on_receive_fields(pos, formname, fields, player)
end
if start_level ~= meta:get_int("start_level") then
meta:set_string("quarry_pos", nil) -- reset the quarry
stop_the_machine(pos)
State:stop(pos, meta)
meta:set_int("start_level", start_level)
end
@ -291,15 +253,15 @@ local function on_receive_fields(pos, formname, fields, player)
end
meta:set_int("endless", endless)
local running = meta:get_int("running") or STOP_STATE
if fields.button ~= nil then
if running > STOP_STATE then
stop_the_machine(pos)
else
start_the_machine(pos)
if fields.state_button ~= nil then
local state = State:get_state(meta)
if state == tubelib.STOPPED or state == tubelib.STANDBY or state == tubelib.BLOCKED then
meta:set_int("idx", 1) -- restart from the beginning
meta:set_string("quarry_pos", nil)
State:start(pos, meta)
elseif state == tubelib.RUNNING or state == tubelib.FAULT then
State:stop(pos, meta)
end
else
meta:set_string("formspec", quarry_formspec(meta, tubelib.state(running)))
end
end
@ -315,36 +277,27 @@ minetest.register_node("tubelib_addons1:quarry", {
'tubelib_addons1_quarry.png^[transformFX',
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
after_place_node = function(pos, placer)
local meta = M(pos)
local inv = meta: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: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)
meta:set_int("facedir", facedir)
meta:set_string("number", number)
meta:set_string("owner", placer:get_player_name())
meta:set_int("running", STOP_STATE)
meta:set_int("endless", 0)
meta:set_int("curr_level", -1)
meta:set_int("max_levels", 1)
meta:set_string("formspec", quarry_formspec(meta, tubelib.STOPPED))
State:node_init(pos, number)
end,
on_receive_fields = on_receive_fields,
on_rotate = screwdriver.disallow,
on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
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)
@ -352,9 +305,17 @@ minetest.register_node("tubelib_addons1:quarry", {
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",
@ -363,7 +324,6 @@ minetest.register_node("tubelib_addons1:quarry", {
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("tubelib_addons1:quarry_active", {
description = "Tubelib Quarry",
tiles = {
@ -402,6 +362,48 @@ minetest.register_node("tubelib_addons1:quarry_active", {
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({
output = "tubelib_addons1:quarry",
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)
local meta = minetest.get_meta(pos)
return tubelib.get_item(meta, "main")
return tubelib.get_item(M(pos), "main")
end,
on_push_item = function(pos, side, item)
if item:get_name() == "tubelib_addons1:biofuel" then
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "fuel", item)
end
return false
return tubelib.put_item(M(pos), "fuel", item)
end,
on_unpull_item = function(pos, side, item)
local meta = minetest.get_meta(pos)
return tubelib.put_item(meta, "main", item)
return tubelib.put_item(M(pos), "main", 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)
elseif topic == "fuel" then
local meta = minetest.get_meta(pos)
return tubelib.fuelstate(meta, "fuel")
if topic == "fuel" then
return tubelib.fuelstate(M(pos), "fuel")
end
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", TICKS_TO_SLEEP)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
State:on_node_load(pos)
end,
on_node_repair = function(pos)
return State:on_node_repair(pos)
end,
})

@ -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,13 +15,26 @@
]]--
local CYCLE_TIME = 6
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 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]"..
default.gui_bg..
default.gui_bg_img..
@ -29,7 +42,7 @@ local function formspec(state)
"list[context;src;0,0;3,3;]"..
"item_image[0,0;1,1;tubelib_addons1:biogas]"..
"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:biofuel]"..
"list[current_player;main;0,4.3;8,4;]"..
@ -39,13 +52,18 @@ local function formspec(state)
"listring[current_player;main]"
end
State:register_formspec_func(formspec)
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
local meta = minetest.get_meta(pos)
local meta = M(pos)
local inv = meta:get_inventory()
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()
elseif listname == "dst" then
return stack:get_count()
@ -54,7 +72,7 @@ local function allow_metadata_inventory_put(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)
@ -80,104 +98,43 @@ local function place_top(pos, facedir, placer)
return true
end
local function convert_biogas_to_biofuel(meta)
local function convert_biogas_to_biofuel(pos, meta)
local inv = meta:get_inventory()
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?
local items = tubelib.get_num_items(meta, "src", 4)
if items then -- input available?
if items:get_name() == "tubelib_addons1:biogas" then -- valid input?
if items then -- input available?
if items:get_name() == "tubelib_addons1:biogas" then -- valid input?
inv:add_item("dst", biofuel)
return true
State:keep_running(pos, meta, COUNTDOWN_TICKS)
return
else
inv:add_item("src", items)
return nil -- error
State:fault(pos, meta)
return
end
else
return false -- standby
State:idle(pos, meta)
return
end
else
return false -- standby
State:blocked(pos, meta)
return
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 meta = minetest.get_meta(pos)
local running = meta:get_int("running") - 1
local res = convert_biogas_to_biofuel(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
local meta = M(pos)
convert_biogas_to_biofuel(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
@ -200,7 +157,7 @@ minetest.register_node("tubelib_addons1:reformer", {
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local meta = M(pos)
local inv = meta:get_inventory()
inv:set_size('src', 9)
inv:set_size('dst', 9)
@ -213,15 +170,11 @@ minetest.register_node("tubelib_addons1:reformer", {
return
end
local number = tubelib.add_node(pos, "tubelib_addons1:reformer")
local meta = minetest.get_meta(pos)
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))
State:node_init(pos, number)
end,
on_dig = function(pos, node, puncher, pointed_thing)
local meta = minetest.get_meta(pos)
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)
@ -230,6 +183,10 @@ minetest.register_node("tubelib_addons1:reformer", {
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,
@ -237,6 +194,7 @@ minetest.register_node("tubelib_addons1:reformer", {
allow_metadata_inventory_move = allow_metadata_inventory_move,
allow_metadata_inventory_take = allow_metadata_inventory_take,
drop = "",
paramtype = "light",
sunlight_propagates = true,
paramtype2 = "facedir",
@ -245,6 +203,65 @@ minetest.register_node("tubelib_addons1:reformer", {
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", {
description = "Tubelib Reformer Top",
@ -281,44 +298,35 @@ minetest.register_craft({
minetest.register_craft({
output = "tubelib_addons1:reformer",
recipe = {
{"default:steel_ingot", "default:clay", "default:steel_ingot"},
{"tubelib:tube1", "default:mese_crystal", "tubelib:tube1"},
{"default:steel_ingot", "group:wood", "default:steel_ingot"},
{"default:steel_ingot", "default:clay", "default:steel_ingot"},
{"tubelib:tube1", "default:mese_crystal", "tubelib:tube1"},
{"default:steel_ingot", "group:wood", "default:steel_ingot"},
},
})
tubelib.register_node("tubelib_addons1:reformer", {}, {
tubelib.register_node("tubelib_addons1:reformer", {"tubelib_addons1:reformer_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,
})