2018-11-06 20:45:02 +01:00
|
|
|
--[[
|
|
|
|
|
|
|
|
=======================================================================
|
|
|
|
Tubelib Biogas Machines Mod
|
2019-01-26 15:54:41 +01:00
|
|
|
by Micu (c) 2018, 2019
|
2018-11-06 20:45:02 +01:00
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
Copyright (C) 2018, 2019 Michal Cieslakiewicz
|
2018-11-06 20:45:02 +01:00
|
|
|
|
|
|
|
Freezer - Biogas-powered machine that converts water to ice
|
|
|
|
(technically Biogas is a coolant here, not fuel, however it
|
|
|
|
is 'used' in the same way). Device changes water in bucket
|
|
|
|
to ice and empty bucket. If pipeworks mod is installed and pipe
|
|
|
|
with water is connected to device, in absence of water buckets
|
2018-11-10 15:03:43 +01:00
|
|
|
it produces ice from water supplied via pipes at the same rate.
|
2018-11-06 20:45:02 +01:00
|
|
|
Device automatically shuts off when there is nothing to freeze,
|
2018-11-10 15:03:43 +01:00
|
|
|
so Biogas is not wasted. However when machine is powered off via
|
|
|
|
button while item is being frozen, Biogas used to partially cool
|
|
|
|
down water is lost.
|
|
|
|
Internal water valve works only when device runs, so in off state
|
|
|
|
water pipe detector is also off. This is a design feature to save
|
|
|
|
CPU resources for inactive machine (timer is stopped).
|
2018-11-06 20:45:02 +01:00
|
|
|
|
2018-11-11 11:24:07 +01:00
|
|
|
Operational info:
|
2018-11-15 17:20:31 +01:00
|
|
|
* machine checks for buckets with water first and if there are none
|
|
|
|
tries to take water from pipeline
|
2019-01-29 18:04:30 +01:00
|
|
|
* if there is nothing to freeze and Biogas tank is empty, machine
|
|
|
|
switches off automatically
|
|
|
|
* when coolant ends, machine enters fault mode and has to be
|
|
|
|
manually powered on again after refilling Biogas
|
2019-01-26 15:54:41 +01:00
|
|
|
* if there is nothing to freeze but there is still Biogas in tank,
|
|
|
|
machine enters standby mode; it will automatically pick up work
|
2019-01-29 18:04:30 +01:00
|
|
|
as soon as any water source becomes available again
|
|
|
|
* if output tray is full and no new items can be put there, machine
|
|
|
|
changes state to blocked (special standby mode); it will resume
|
|
|
|
work as soon as there is space in output inventory
|
2018-11-11 11:24:07 +01:00
|
|
|
* there is 1 tick gap between items to unload ice and load internal
|
2018-11-15 17:20:31 +01:00
|
|
|
water tank or freezing tray; this a design choice to make timer
|
|
|
|
callback run faster
|
2018-11-11 11:24:07 +01:00
|
|
|
* Biogas is used only when device actually freezes
|
|
|
|
* partially frozen items (due to work being interrupted by on/off
|
|
|
|
switch) have to be frozen again from beginning, Biogas used for
|
|
|
|
such partial freeze is not recoverable
|
2018-11-19 14:05:20 +01:00
|
|
|
* bucket freezing tray cannot be emptied manually when machine is
|
|
|
|
running, stop the device to take water bucket; please note that
|
|
|
|
tray cannot be loaded manually, please use input inventory
|
2018-11-11 11:24:07 +01:00
|
|
|
* when using pipes, internal water tank is filled completely before
|
|
|
|
process starts; shutting down water source during freezing does
|
2018-11-13 17:28:24 +01:00
|
|
|
not stop it
|
2018-11-11 11:24:07 +01:00
|
|
|
* machine cannot be recovered unless input, output and fuel trays
|
|
|
|
are all empty
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
Tubelib v2 implementation info:
|
|
|
|
* device updates itself every tick, so cycle_time must be set to 1
|
|
|
|
even though production takes longer (start method sets timer to
|
|
|
|
this value)
|
|
|
|
* keep_running function is called every time item is produced
|
|
|
|
(not every processing tick - function does not accept neither 0
|
|
|
|
nor fractional values for num_items parameter)
|
2019-01-29 18:04:30 +01:00
|
|
|
* desired_state metadata allows to properly change non-running target
|
|
|
|
state during transition; when new state differs from old one, timer
|
|
|
|
is reset so it is guaranteed that each countdown starts from
|
|
|
|
COUNTDOWN_TICKS
|
2019-02-01 17:27:01 +01:00
|
|
|
* num_items in keep_running method is set to 1 (default value);
|
|
|
|
machine aging is controlled by aging_factor solely; tubelib item
|
|
|
|
counter is used to count production iterations not actual items
|
2019-01-26 15:54:41 +01:00
|
|
|
|
2018-11-06 20:45:02 +01:00
|
|
|
License: LGPLv2.1+
|
|
|
|
=======================================================================
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2018-11-10 15:03:43 +01:00
|
|
|
--[[
|
|
|
|
---------
|
|
|
|
Variables
|
|
|
|
---------
|
|
|
|
]]--
|
|
|
|
|
2018-11-11 11:24:07 +01:00
|
|
|
-- timing
|
2019-01-26 15:54:41 +01:00
|
|
|
local BIOGAS_TICKS = 24 -- Biogas work time
|
|
|
|
local ICE_TICKS = 4 -- Ice creation time
|
2018-11-11 11:24:07 +01:00
|
|
|
local TIMER_TICK_SEC = 1 -- Node timer tick
|
2019-01-26 15:54:41 +01:00
|
|
|
local STANDBY_TICKS = 4 -- Standby mode timer frequency factor
|
|
|
|
local COUNTDOWN_TICKS = 4 -- Ticks to standby
|
2018-11-11 11:24:07 +01:00
|
|
|
-- machine inventory
|
|
|
|
local INV_H = 3 -- Inventory height
|
|
|
|
local INV_IN_W = 4 -- Input inventory width
|
|
|
|
local INV_OUT_W = (6 - INV_IN_W) -- Output inventory width
|
2018-11-10 15:03:43 +01:00
|
|
|
-- item processed
|
|
|
|
local SOURCE_EMPTY = 0
|
|
|
|
local SOURCE_BUCKET = 1
|
|
|
|
local SOURCE_PIPE = 2
|
|
|
|
-- water sources
|
2019-01-15 19:24:09 +01:00
|
|
|
local water_bucket = { ["bucket:bucket_water"] = true,
|
|
|
|
["bucket:bucket_river_water"] = true }
|
2018-11-10 15:03:43 +01:00
|
|
|
|
|
|
|
--[[
|
|
|
|
--------
|
|
|
|
Formspec
|
|
|
|
--------
|
|
|
|
]]--
|
2019-01-26 15:54:41 +01:00
|
|
|
|
2018-11-19 12:07:07 +01:00
|
|
|
-- static data for formspec
|
|
|
|
local fmxy = {
|
|
|
|
inv_h = tostring(INV_H),
|
|
|
|
inv_in_w = tostring(INV_IN_W),
|
|
|
|
mid_x = tostring(INV_IN_W + 1),
|
|
|
|
inv_out_w = tostring(INV_OUT_W),
|
|
|
|
inv_out_x = tostring(INV_IN_W + 2),
|
2019-01-26 15:54:41 +01:00
|
|
|
biogas_time = tostring(BIOGAS_TICKS * TIMER_TICK_SEC),
|
|
|
|
ice_time = tostring(ICE_TICKS * TIMER_TICK_SEC),
|
|
|
|
ice_qty = tostring(BIOGAS_TICKS / ICE_TICKS)
|
2018-11-19 12:07:07 +01:00
|
|
|
}
|
2018-11-10 15:03:43 +01:00
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
-- formspec
|
|
|
|
local function formspec(self, pos, meta)
|
|
|
|
local state = meta:get_int("tubelib_state")
|
|
|
|
local source = meta:get_int("source")
|
|
|
|
local fuel_pct = tostring(100 * meta:get_int("fuel_ticks") / BIOGAS_TICKS)
|
|
|
|
local item_pct = tostring(100 * (ICE_TICKS - meta:get_int("item_ticks")) / ICE_TICKS)
|
2018-11-10 15:03:43 +01:00
|
|
|
return "size[8,8.25]" ..
|
|
|
|
default.gui_bg ..
|
|
|
|
default.gui_bg_img ..
|
|
|
|
default.gui_slots ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"list[context;src;0,0;" .. fmxy.inv_in_w .. "," .. fmxy.inv_h .. ";]" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
"item_image[0,0;1,1;bucket:bucket_water]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"list[context;cur;" .. fmxy.inv_in_w .. ",0;1,1;]" ..
|
|
|
|
"image[" .. fmxy.mid_x .. ",0;1,1;biogasmachines_freezer_pipe_inv_" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
(source == SOURCE_PIPE and "fg" or "bg") .. ".png]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"image[" .. fmxy.inv_in_w ..
|
2018-11-11 11:24:07 +01:00
|
|
|
",1;1,1;biogasmachines_freezer_inv_bg.png^[lowpart:" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
fuel_pct .. ":biogasmachines_freezer_inv_fg.png]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"image[" .. fmxy.mid_x .. ",1;1,1;gui_furnace_arrow_bg.png^[lowpart:" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
item_pct .. ":gui_furnace_arrow_fg.png^[transformR270]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"list[context;fuel;" .. fmxy.inv_in_w .. ",2;1,1;]" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
"item_image[" .. fmxy.inv_in_w .. ",2;1,1;tubelib_addons1:biogas]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"image_button[" .. fmxy.mid_x .. ",2;1,1;" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
self:get_state_button_image(meta) .. ";state_button;]" ..
|
2018-11-11 11:24:07 +01:00
|
|
|
"item_image[1,3.25;0.5,0.5;tubelib_addons1:biogas]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"label[1.5,3.25;= " .. fmxy.biogas_time .. " sec]" ..
|
2018-11-11 11:24:07 +01:00
|
|
|
"item_image[3,3.25;0.5,0.5;default:ice]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"label[3.5,3.25;= " .. fmxy.ice_time .. " sec]" ..
|
2018-11-11 11:24:07 +01:00
|
|
|
"item_image[5.25,3.25;0.5,0.5;tubelib_addons1:biogas]" ..
|
|
|
|
"image[5.75,3.25;0.5,0.5;tubelib_gui_arrow.png^[resize:16x16]" ..
|
|
|
|
"item_image[6.25,3.25;0.5,0.5;default:ice]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"label[6.75,3.25;x " .. fmxy.ice_qty .. "]" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
"item_image[" .. fmxy.inv_out_x .. ",0;1,1;default:ice]" ..
|
2018-11-19 12:07:07 +01:00
|
|
|
"list[context;dst;" .. fmxy.inv_out_x .. ",0;" .. fmxy.inv_out_w ..
|
|
|
|
"," .. fmxy.inv_h .. ";]" ..
|
2018-11-10 15:03:43 +01:00
|
|
|
"list[current_player;main;0,4;8,1;]" ..
|
|
|
|
"list[current_player;main;0,5.25;8,3;8]" ..
|
|
|
|
"listring[context;dst]" ..
|
|
|
|
"listring[current_player;main]" ..
|
|
|
|
"listring[context;src]" ..
|
|
|
|
"listring[current_player;main]" ..
|
|
|
|
"listring[context;fuel]" ..
|
|
|
|
"listring[current_player;main]" ..
|
2019-01-26 15:54:41 +01:00
|
|
|
(state == tubelib.RUNNING and source ~= SOURCE_PIPE and
|
2018-11-19 14:05:20 +01:00
|
|
|
"box[" .. fmxy.inv_in_w .. ",0;0.82,0.9;#2F4FBF]" or
|
|
|
|
"listring[context;cur]listring[current_player;main]") ..
|
2018-11-10 15:03:43 +01:00
|
|
|
default.get_hotbar_bg(0, 4)
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
-------
|
|
|
|
Helpers
|
|
|
|
-------
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- get bucket with water (itemstack)
|
2018-11-11 11:24:07 +01:00
|
|
|
local function get_water_bucket(inv, listname)
|
2018-11-10 15:03:43 +01:00
|
|
|
local stack = ItemStack({})
|
2019-01-15 19:24:09 +01:00
|
|
|
for i, _ in pairs(water_bucket) do
|
2018-11-10 15:03:43 +01:00
|
|
|
stack = inv:remove_item(listname, ItemStack(i .. " 1"))
|
|
|
|
if not stack:is_empty() then break end
|
|
|
|
end
|
|
|
|
return stack
|
|
|
|
end
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
-- reset processing data
|
2019-01-29 18:04:30 +01:00
|
|
|
local function state_meta_reset(pos, meta)
|
2018-11-19 14:05:20 +01:00
|
|
|
meta:set_int("source", SOURCE_EMPTY)
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_int("item_ticks", ICE_TICKS)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
--[[
|
|
|
|
-------------
|
|
|
|
State machine
|
|
|
|
-------------
|
|
|
|
]]--
|
2018-11-10 15:03:43 +01:00
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
local machine = tubelib.NodeStates:new({
|
|
|
|
node_name_passive = "biogasmachines:freezer",
|
|
|
|
node_name_active = "biogasmachines:freezer_active",
|
|
|
|
node_name_defect = "biogasmachines:freezer_defect",
|
|
|
|
infotext_name = "Tubelib Water Freezer",
|
|
|
|
cycle_time = TIMER_TICK_SEC,
|
|
|
|
standby_ticks = STANDBY_TICKS,
|
|
|
|
has_item_meter = true,
|
|
|
|
aging_factor = 8,
|
2019-01-29 18:04:30 +01:00
|
|
|
on_start = function(pos, meta, oldstate)
|
|
|
|
meta:set_int("desired_state", tubelib.RUNNING)
|
|
|
|
state_meta_reset(pos, meta)
|
|
|
|
end,
|
|
|
|
on_stop = function(pos, meta, oldstate)
|
|
|
|
meta:set_int("desired_state", tubelib.STOPPED)
|
|
|
|
state_meta_reset(pos, meta)
|
|
|
|
end,
|
2019-01-26 15:54:41 +01:00
|
|
|
formspec_func = formspec,
|
|
|
|
})
|
2018-11-10 15:03:43 +01:00
|
|
|
|
2019-01-29 18:04:30 +01:00
|
|
|
-- fault function for convenience as there is no on_fault method (yet)
|
|
|
|
local function machine_fault(pos, meta)
|
|
|
|
meta:set_int("desired_state", tubelib.FAULT)
|
|
|
|
state_meta_reset(pos, meta)
|
|
|
|
machine:fault(pos, meta)
|
|
|
|
end
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
-- customized version of NodeStates:idle()
|
|
|
|
local function countdown_to_halt(pos, meta, target_state)
|
|
|
|
if target_state ~= tubelib.STANDBY and
|
|
|
|
target_state ~= tubelib.BLOCKED and
|
|
|
|
target_state ~= tubelib.STOPPED and
|
|
|
|
target_state ~= tubelib.FAULT then
|
|
|
|
return true
|
|
|
|
end
|
2019-01-29 18:04:30 +01:00
|
|
|
if machine:get_state(meta) == tubelib.RUNNING and
|
|
|
|
meta:get_int("desired_state") ~= target_state then
|
|
|
|
meta:set_int("tubelib_countdown", COUNTDOWN_TICKS)
|
|
|
|
meta:set_int("desired_state", target_state)
|
|
|
|
end
|
|
|
|
local countdown = meta:get_int("tubelib_countdown") - 1
|
|
|
|
if countdown >= -1 then
|
|
|
|
-- we don't need anything less than -1
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_int("tubelib_countdown", countdown)
|
2019-01-29 18:04:30 +01:00
|
|
|
end
|
|
|
|
if countdown < 0 then
|
|
|
|
if machine:get_state(meta) == target_state then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
meta:set_int("desired_state", target_state)
|
|
|
|
-- workaround for switching between non-running states
|
|
|
|
meta:set_int("tubelib_state", tubelib.RUNNING)
|
|
|
|
if target_state == tubelib.FAULT then
|
|
|
|
machine_fault(pos, meta)
|
|
|
|
elseif target_state == tubelib.STOPPED then
|
|
|
|
machine:stop(pos, meta)
|
|
|
|
elseif target_state == tubelib.BLOCKED then
|
|
|
|
machine:blocked(pos, meta)
|
|
|
|
else
|
|
|
|
machine:standby(pos, meta)
|
2019-01-26 15:54:41 +01:00
|
|
|
end
|
2019-01-29 18:04:30 +01:00
|
|
|
return false
|
2019-01-26 15:54:41 +01:00
|
|
|
end
|
|
|
|
return true
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
|
2019-01-29 18:04:30 +01:00
|
|
|
-- countdown to one of two states depending on fuel availability
|
|
|
|
local function fuel_countdown_to_halt(pos, meta, target_state_fuel, target_state_empty)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local fuel = meta:get_int("fuel_ticks")
|
|
|
|
if fuel == 0 and inv:is_empty("fuel") then
|
|
|
|
return countdown_to_halt(pos, meta, target_state_empty)
|
|
|
|
else
|
|
|
|
return countdown_to_halt(pos, meta, target_state_fuel)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-10 15:03:43 +01:00
|
|
|
--[[
|
|
|
|
---------
|
|
|
|
Callbacks
|
|
|
|
---------
|
|
|
|
]]--
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
-- do not allow to dig protected or non-empty machine
|
2018-11-10 15:03:43 +01:00
|
|
|
local function can_dig(pos, player)
|
2019-01-26 15:54:41 +01:00
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return false
|
|
|
|
end
|
2018-11-10 15:03:43 +01:00
|
|
|
local meta = minetest.get_meta(pos);
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return inv:is_empty("src") and inv:is_empty("dst")
|
|
|
|
and inv:is_empty("fuel")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- cleanup after digging
|
|
|
|
local function after_dig_node(pos, oldnode, oldmetadata, digger)
|
|
|
|
tubelib.remove_node(pos)
|
|
|
|
if minetest.get_modpath("pipeworks") then
|
|
|
|
pipeworks.scan_for_pipe_objects(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
-- init machine after placement
|
|
|
|
local function after_place_node(pos, placer, itemstack, pointed_thing)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
inv:set_size('src', INV_H * INV_IN_W)
|
|
|
|
inv:set_size('cur', 1)
|
|
|
|
inv:set_size('fuel', 1)
|
|
|
|
inv:set_size('dst', INV_H * INV_OUT_W)
|
|
|
|
meta:set_string("owner", placer:get_player_name())
|
|
|
|
meta:set_int("fuel_ticks", 0)
|
|
|
|
state_meta_reset(pos, meta)
|
|
|
|
local number = tubelib.add_node(pos, "biogasmachines:freezer")
|
|
|
|
machine:node_init(pos, number)
|
|
|
|
if minetest.get_modpath("pipeworks") then
|
|
|
|
pipeworks.scan_for_pipe_objects(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-10 15:03:43 +01:00
|
|
|
-- validate incoming items
|
|
|
|
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" then
|
2019-01-15 19:24:09 +01:00
|
|
|
if water_bucket[stack:get_name()] then
|
2018-11-10 15:03:43 +01:00
|
|
|
return stack:get_count()
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
elseif listname == "cur" or listname == "dst" then
|
|
|
|
return 0
|
|
|
|
elseif listname == "fuel" then
|
|
|
|
if stack:get_name() == "tubelib_addons1:biogas" then
|
|
|
|
return stack:get_count()
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- validate items move
|
|
|
|
local function allow_metadata_inventory_move(pos, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2018-11-24 13:10:24 +01:00
|
|
|
if to_list == "cur" or
|
2019-01-26 15:54:41 +01:00
|
|
|
(from_list == "cur" and machine:get_state(meta) == tubelib.RUNNING) then
|
2018-11-24 13:10:24 +01:00
|
|
|
return 0
|
|
|
|
end
|
2018-11-10 15:03:43 +01:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- validate items retrieval
|
|
|
|
local function allow_metadata_inventory_take(pos, listname, index, stack, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
if listname == "cur" then
|
2018-11-19 14:05:20 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2019-01-26 15:54:41 +01:00
|
|
|
if machine:get_state(meta) == tubelib.RUNNING then
|
2018-11-19 14:05:20 +01:00
|
|
|
return 0
|
|
|
|
end
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
return stack:get_count()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- formspec callback
|
|
|
|
local function on_receive_fields(pos, formname, fields, player)
|
|
|
|
if minetest.is_protected(pos, player:get_player_name()) then
|
|
|
|
return
|
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
machine:state_button_event(pos, fields)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
-- tick-based item production
|
2018-11-10 15:03:43 +01:00
|
|
|
local function on_timer(pos, elapsed)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
local label = minetest.registered_nodes[node.name].description
|
2019-01-26 15:54:41 +01:00
|
|
|
local number = meta:get_string("tubelib_number")
|
2018-11-10 15:03:43 +01:00
|
|
|
local source = meta:get_int("source")
|
2019-01-26 15:54:41 +01:00
|
|
|
local fuel = meta:get_int("fuel_ticks")
|
2018-11-10 15:03:43 +01:00
|
|
|
local pipe = source == SOURCE_PIPE
|
|
|
|
if source == SOURCE_EMPTY then
|
|
|
|
-- try to start freezing bucket or water from pipe
|
|
|
|
pipe = biogasmachines.is_pipe_with_water(pos, node)
|
|
|
|
local output = { ItemStack("default:ice 1") }
|
2018-11-19 14:05:20 +01:00
|
|
|
if not inv:is_empty("cur") or not inv:is_empty("src") then
|
2018-11-10 15:03:43 +01:00
|
|
|
-- source: water bucket
|
|
|
|
source = SOURCE_BUCKET
|
|
|
|
pipe = false
|
|
|
|
output[#output + 1] = ItemStack("bucket:bucket_empty 1")
|
|
|
|
elseif pipe then
|
|
|
|
-- source: water pipe
|
|
|
|
source = SOURCE_PIPE
|
|
|
|
else
|
2019-01-29 18:04:30 +01:00
|
|
|
-- no source, count towards standby/stop
|
|
|
|
return fuel_countdown_to_halt(pos, meta,
|
|
|
|
tubelib.STANDBY, tubelib.STOPPED)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
if machine:get_state(meta) == tubelib.STANDBY then
|
2018-11-10 15:03:43 +01:00
|
|
|
-- something to do, wake up and re-entry
|
2019-01-26 15:54:41 +01:00
|
|
|
machine:start(pos, meta, true)
|
|
|
|
return false
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
2018-11-19 14:05:20 +01:00
|
|
|
if inv:is_empty("cur") then
|
2019-01-26 15:54:41 +01:00
|
|
|
-- check if there is space in output
|
2019-01-29 18:04:30 +01:00
|
|
|
inv:set_list("dst_copy", inv:get_list("dst"))
|
|
|
|
local is_dst_ok = true
|
2018-11-19 14:05:20 +01:00
|
|
|
for _, stack in ipairs(output) do
|
2019-01-29 18:04:30 +01:00
|
|
|
local outp = inv:add_item("dst_copy", stack)
|
|
|
|
if not outp:is_empty() then
|
|
|
|
is_dst_ok = false
|
|
|
|
break
|
2018-11-19 14:05:20 +01:00
|
|
|
end
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
2019-01-29 18:04:30 +01:00
|
|
|
inv:set_size("dst_copy", 0)
|
|
|
|
if not is_dst_ok then
|
|
|
|
return fuel_countdown_to_halt(pos, meta,
|
|
|
|
tubelib.BLOCKED, tubelib.FAULT)
|
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
if machine:get_state(meta) == tubelib.BLOCKED then
|
|
|
|
-- new free output slots, wake up and re-entry
|
|
|
|
machine:start(pos, meta, true)
|
|
|
|
return false
|
|
|
|
end
|
2018-11-19 14:05:20 +01:00
|
|
|
-- process another water unit
|
2019-01-29 18:04:30 +01:00
|
|
|
if fuel == 0 and inv:is_empty("fuel") then
|
|
|
|
return countdown_to_halt(pos, meta, tubelib.FAULT)
|
|
|
|
end
|
2018-11-19 14:05:20 +01:00
|
|
|
if source == SOURCE_BUCKET then
|
|
|
|
local inp = get_water_bucket(inv, "src")
|
|
|
|
if inp:is_empty() then
|
2019-01-29 18:04:30 +01:00
|
|
|
machine_fault(pos, meta) -- oops
|
2019-01-26 15:54:41 +01:00
|
|
|
return false
|
2018-11-19 14:05:20 +01:00
|
|
|
end
|
|
|
|
inv:add_item("cur", inp)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
2019-01-29 18:04:30 +01:00
|
|
|
elseif fuel == 0 and inv:is_empty("fuel") then
|
|
|
|
return countdown_to_halt(pos, meta, tubelib.FAULT)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
meta:set_int("source", source)
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_int("item_ticks", ICE_TICKS)
|
2018-11-10 15:03:43 +01:00
|
|
|
else
|
2019-01-26 15:54:41 +01:00
|
|
|
-- continue freezing process
|
|
|
|
if machine:get_state(meta) ~= tubelib.RUNNING then
|
|
|
|
-- exception, should not happen - oops
|
2019-01-29 18:04:30 +01:00
|
|
|
machine_fault(pos, meta)
|
2019-01-26 15:54:41 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- add item tick
|
2019-01-29 18:04:30 +01:00
|
|
|
if fuel == 0 and inv:is_empty("fuel") then
|
|
|
|
return countdown_to_halt(pos, meta, tubelib.FAULT)
|
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
local itemcnt = meta:get_int("item_ticks") - 1
|
2018-11-10 15:03:43 +01:00
|
|
|
if itemcnt == 0 then
|
|
|
|
inv:add_item("dst", ItemStack("default:ice 1"))
|
|
|
|
if source == SOURCE_BUCKET then
|
|
|
|
inv:set_stack("cur", 1, ItemStack({}))
|
|
|
|
inv:add_item("dst", ItemStack("bucket:bucket_empty 1"))
|
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
state_meta_reset(pos, meta)
|
|
|
|
-- item produced, increase aging
|
2019-02-01 17:27:01 +01:00
|
|
|
machine:keep_running(pos, meta, COUNTDOWN_TICKS)
|
2019-01-26 15:54:41 +01:00
|
|
|
else
|
|
|
|
meta:set_int("item_ticks", itemcnt)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
-- consume fuel tick
|
2019-01-26 15:54:41 +01:00
|
|
|
if fuel == 0 then
|
2018-11-10 15:03:43 +01:00
|
|
|
if not inv:is_empty("fuel") then
|
2019-01-26 15:54:41 +01:00
|
|
|
inv:remove_item("fuel",
|
2018-11-10 15:03:43 +01:00
|
|
|
ItemStack("tubelib_addons1:biogas 1"))
|
2019-01-26 15:54:41 +01:00
|
|
|
fuel = BIOGAS_TICKS
|
2018-11-10 15:03:43 +01:00
|
|
|
else
|
2019-01-29 18:04:30 +01:00
|
|
|
machine_fault(pos, meta) -- oops
|
2019-01-26 15:54:41 +01:00
|
|
|
return false
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_int("fuel_ticks", fuel - 1)
|
2018-11-10 15:03:43 +01:00
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_int("tubelib_countdown", COUNTDOWN_TICKS)
|
2019-01-29 18:04:30 +01:00
|
|
|
meta:set_int("desired_state", tubelib.RUNNING)
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_string("infotext", label .. " " .. number .. ": running (water " ..
|
2018-11-10 15:03:43 +01:00
|
|
|
(pipe and "from pipe" or "in buckets") .. ")")
|
2019-01-26 15:54:41 +01:00
|
|
|
meta:set_string("formspec", formspec(machine, pos, meta))
|
2018-11-10 15:03:43 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
-----------------
|
|
|
|
Node registration
|
|
|
|
-----------------
|
|
|
|
]]--
|
|
|
|
|
2018-11-06 20:45:02 +01:00
|
|
|
minetest.register_node("biogasmachines:freezer", {
|
2018-11-07 15:46:21 +01:00
|
|
|
description = "Tubelib Water Freezer",
|
2018-11-06 20:45:02 +01:00
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2018-11-13 23:38:39 +01:00
|
|
|
"biogasmachines_freezer_top.png",
|
|
|
|
"biogasmachines_freezer_bottom.png",
|
|
|
|
"biogasmachines_freezer_side.png",
|
|
|
|
"biogasmachines_freezer_side.png",
|
|
|
|
"biogasmachines_freezer_side.png",
|
|
|
|
"biogasmachines_freezer_side.png"
|
|
|
|
},
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{ -0.5, -0.375, -0.5, 0.5, 0.5, 0.5 },
|
|
|
|
{ -0.5, -0.5, -0.5, -0.375, -0.375, -0.375 },
|
|
|
|
{ 0.375, -0.5, -0.5, 0.5, -0.375, -0.375 },
|
|
|
|
{ 0.375, -0.5, 0.375, 0.5, -0.375, 0.5 },
|
|
|
|
{ -0.5, -0.5, 0.375, -0.375, -0.375, 0.5 },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
selection_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
|
2018-11-06 20:45:02 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
2018-11-10 15:03:43 +01:00
|
|
|
groups = { choppy = 2, cracky = 2, crumbly = 2 },
|
2018-11-06 20:45:02 +01:00
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
|
|
|
|
2018-11-13 15:16:18 +01:00
|
|
|
pipe_connections = { top = 1,
|
|
|
|
bottom = 1,
|
|
|
|
front = 1,
|
|
|
|
back = 1,
|
|
|
|
left = 1,
|
|
|
|
right = 1 },
|
2019-01-26 15:54:41 +01:00
|
|
|
|
|
|
|
drop = "",
|
2018-11-10 15:03:43 +01:00
|
|
|
can_dig = can_dig,
|
2019-01-26 15:54:41 +01:00
|
|
|
|
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
|
|
|
machine:after_dig_node(pos, oldnode, oldmetadata, digger)
|
|
|
|
after_dig_node(pos, oldnode, oldmetadata, digger)
|
|
|
|
end,
|
|
|
|
|
2018-11-10 15:03:43 +01:00
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
on_timer = on_timer,
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
2019-01-26 15:54:41 +01:00
|
|
|
after_place_node = after_place_node,
|
2018-11-10 15:03:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_node("biogasmachines:freezer_active", {
|
|
|
|
description = "Tubelib Water Freezer",
|
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
2018-11-13 23:38:39 +01:00
|
|
|
{
|
|
|
|
image = "biogasmachines_freezer_active_top.png",
|
|
|
|
backface_culling = false,
|
|
|
|
animation = {
|
|
|
|
type = "vertical_frames",
|
|
|
|
aspect_w = 32,
|
|
|
|
aspect_h = 32,
|
|
|
|
length = 4.0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"biogasmachines_freezer_bottom.png",
|
|
|
|
"biogasmachines_freezer_side.png",
|
|
|
|
"biogasmachines_freezer_side.png",
|
|
|
|
"biogasmachines_freezer_side.png",
|
|
|
|
"biogasmachines_freezer_side.png"
|
|
|
|
},
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{ -0.5, -0.375, -0.5, 0.5, 0.5, 0.5 },
|
|
|
|
{ -0.5, -0.5, -0.5, -0.375, -0.375, -0.375 },
|
|
|
|
{ 0.375, -0.5, -0.5, 0.5, -0.375, -0.375 },
|
|
|
|
{ 0.375, -0.5, 0.375, 0.5, -0.375, 0.5 },
|
|
|
|
{ -0.5, -0.5, 0.375, -0.375, -0.375, 0.5 },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
selection_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
|
2018-11-10 15:03:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = { crumbly = 0, not_in_creative_inventory = 1 },
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
2018-11-06 20:45:02 +01:00
|
|
|
|
2018-11-13 17:28:24 +01:00
|
|
|
pipe_connections = { top = 1,
|
|
|
|
bottom = 1,
|
|
|
|
front = 1,
|
|
|
|
back = 1,
|
|
|
|
left = 1,
|
|
|
|
right = 1 },
|
2018-11-10 15:03:43 +01:00
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
drop = "",
|
2018-11-10 15:03:43 +01:00
|
|
|
can_dig = can_dig,
|
2019-01-26 15:54:41 +01:00
|
|
|
|
|
|
|
after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
|
|
|
machine:after_dig_node(pos, oldnode, oldmetadata, digger)
|
|
|
|
after_dig_node(pos, oldnode, oldmetadata, digger)
|
|
|
|
end,
|
|
|
|
|
2018-11-10 15:03:43 +01:00
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
on_timer = on_timer,
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
2018-11-06 20:45:02 +01:00
|
|
|
})
|
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
minetest.register_node("biogasmachines:freezer_defect", {
|
|
|
|
description = "Tubelib Water Freezer",
|
|
|
|
tiles = {
|
|
|
|
-- up, down, right, left, back, front
|
|
|
|
"biogasmachines_freezer_top.png",
|
|
|
|
"biogasmachines_freezer_bottom.png",
|
|
|
|
"biogasmachines_freezer_side.png^tubelib_defect.png",
|
|
|
|
"biogasmachines_freezer_side.png^tubelib_defect.png",
|
|
|
|
"biogasmachines_freezer_side.png^tubelib_defect.png",
|
|
|
|
"biogasmachines_freezer_side.png^tubelib_defect.png"
|
|
|
|
},
|
|
|
|
drawtype = "nodebox",
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{ -0.5, -0.375, -0.5, 0.5, 0.5, 0.5 },
|
|
|
|
{ -0.5, -0.5, -0.5, -0.375, -0.375, -0.375 },
|
|
|
|
{ 0.375, -0.5, -0.5, 0.5, -0.375, -0.375 },
|
|
|
|
{ 0.375, -0.5, 0.375, 0.5, -0.375, 0.5 },
|
|
|
|
{ -0.5, -0.5, 0.375, -0.375, -0.375, 0.5 },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
selection_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
|
|
|
|
},
|
|
|
|
|
|
|
|
paramtype = "light",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype2 = "facedir",
|
|
|
|
groups = { choppy = 2, cracky = 2, crumbly = 2, not_in_creative_inventory = 1 },
|
|
|
|
is_ground_content = false,
|
|
|
|
sounds = default.node_sound_metal_defaults(),
|
|
|
|
|
|
|
|
pipe_connections = { top = 1,
|
|
|
|
bottom = 1,
|
|
|
|
front = 1,
|
|
|
|
back = 1,
|
|
|
|
left = 1,
|
|
|
|
right = 1 },
|
|
|
|
|
|
|
|
can_dig = can_dig,
|
|
|
|
after_dig_node = after_dig_node,
|
|
|
|
on_rotate = screwdriver.disallow,
|
|
|
|
allow_metadata_inventory_put = allow_metadata_inventory_put,
|
|
|
|
allow_metadata_inventory_move = allow_metadata_inventory_move,
|
|
|
|
allow_metadata_inventory_take = allow_metadata_inventory_take,
|
|
|
|
|
|
|
|
after_place_node = function(pos, placer, itemstack, pointed_thing)
|
|
|
|
after_place_node(pos, placer, itemstack, pointed_thing)
|
|
|
|
machine:defect(pos, minetest.get_meta(pos))
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
tubelib.register_node("biogasmachines:freezer",
|
|
|
|
{ "biogasmachines:freezer_active", "biogasmachines:freezer_defect" }, {
|
2018-11-11 11:24:07 +01:00
|
|
|
|
|
|
|
on_push_item = function(pos, side, item)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2019-01-15 19:24:09 +01:00
|
|
|
if water_bucket[item:get_name()] then
|
2018-11-11 11:24:07 +01:00
|
|
|
return tubelib.put_item(meta, "src", item)
|
|
|
|
elseif item:get_name() == "tubelib_addons1:biogas" then
|
|
|
|
return tubelib.put_item(meta, "fuel", item)
|
|
|
|
end
|
2019-01-26 15:54:41 +01:00
|
|
|
return false
|
2018-11-11 11:24:07 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_pull_item = function(pos, side)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return tubelib.get_item(meta, "dst")
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_unpull_item = function(pos, side, item)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return tubelib.put_item(meta, "dst", item)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_recv_message = function(pos, topic, payload)
|
|
|
|
local meta = minetest.get_meta(pos)
|
2019-01-26 15:54:41 +01:00
|
|
|
if topic == "fuel" then
|
2018-11-11 11:24:07 +01:00
|
|
|
return tubelib.fuelstate(meta, "fuel")
|
2019-01-26 15:54:41 +01:00
|
|
|
end
|
|
|
|
local resp = machine:on_receive_message(pos, topic, payload)
|
|
|
|
if resp then
|
|
|
|
return resp
|
2018-11-11 11:24:07 +01:00
|
|
|
else
|
|
|
|
return "unsupported"
|
|
|
|
end
|
|
|
|
end,
|
2019-01-26 15:54:41 +01:00
|
|
|
|
|
|
|
on_node_load = function(pos)
|
|
|
|
machine:on_node_load(pos)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_node_repair = function(pos)
|
|
|
|
return machine:on_node_repair(pos)
|
|
|
|
end,
|
2018-11-11 11:24:07 +01:00
|
|
|
})
|
2018-11-10 15:03:43 +01:00
|
|
|
|
|
|
|
--[[
|
|
|
|
--------
|
|
|
|
Crafting
|
|
|
|
--------
|
|
|
|
]]--
|
|
|
|
|
2018-11-13 17:28:24 +01:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "biogasmachines:freezer",
|
|
|
|
recipe = {
|
|
|
|
{ "default:steelblock", "default:glass", "default:steelblock" },
|
|
|
|
{ "default:mese_crystal", "bucket:bucket_empty", "tubelib:tube1" },
|
|
|
|
{ "group:wood", "default:copper_ingot", "group:wood" },
|
|
|
|
},
|
|
|
|
})
|
2018-11-10 15:03:43 +01:00
|
|
|
|
2019-01-26 15:54:41 +01:00
|
|
|
if minetest.get_modpath("unified_inventory") and unified_inventory then
|
2018-11-13 17:28:24 +01:00
|
|
|
unified_inventory.register_craft_type("freezing", {
|
|
|
|
description = "Freezing",
|
|
|
|
icon = 'biogasmachines_freezer_inv_fg.png',
|
|
|
|
width = 1,
|
|
|
|
height = 1,
|
|
|
|
})
|
2019-01-15 19:24:09 +01:00
|
|
|
for i, _ in pairs(water_bucket) do
|
2018-11-13 17:28:24 +01:00
|
|
|
unified_inventory.register_craft({
|
|
|
|
type = "freezing",
|
|
|
|
items = { i },
|
|
|
|
output = "default:ice",
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|