techage/furnace/firebox.lua

245 lines
6.5 KiB
Lua
Raw Normal View History

2019-06-10 22:31:58 +03:00
--[[
TechAge
=======
2020-01-31 21:55:10 +03:00
Copyright (C) 2019-2020 Joachim Stolberg
2019-06-10 22:31:58 +03:00
GPL v3
2019-06-10 22:31:58 +03:00
See LICENSE.txt for more information
TA3 Industrial Furnace Firebox
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
2019-06-10 22:31:58 +03:00
local firebox = techage.firebox
local fuel = techage.fuel
local Pipe = techage.LiquidPipe
local liquid = techage.liquid
2019-06-10 22:31:58 +03:00
local CYCLE_TIME = 2
local EFFICIENCY = 2 -- burn cycles
local CATEGORY = 3
2019-06-10 22:31:58 +03:00
2020-01-31 21:55:10 +03:00
local function has_fuel(pos, nvm)
print("has_fuel", nvm.burn_cycles, topic, payload)
return nvm.burn_cycles > 0 or (nvm.liquid and nvm.liquid.amount and nvm.liquid.amount > 0)
2019-06-10 22:31:58 +03:00
end
2020-01-31 21:55:10 +03:00
local function stop_firebox(pos, nvm)
nvm.running = false
2019-06-10 22:31:58 +03:00
firebox.swap_node(pos, "techage:furnace_firebox")
minetest.get_node_timer(pos):stop()
2020-01-31 21:55:10 +03:00
M(pos):set_string("formspec", fuel.formspec(nvm))
2019-06-10 22:31:58 +03:00
end
local function node_timer(pos, elapsed)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.liquid = nvm.liquid or {}
nvm.liquid.amount = nvm.liquid.amount or 0
if nvm.running then
fuel.formspec_update(pos, nvm)
nvm.burn_cycles = (nvm.burn_cycles or 0) - 1
if nvm.burn_cycles <= 0 then
if nvm.liquid.amount > 0 then
nvm.liquid.amount = nvm.liquid.amount - 1
nvm.burn_cycles = fuel.burntime(nvm.liquid.name) * EFFICIENCY / CYCLE_TIME
nvm.burn_cycles_total = nvm.burn_cycles
2019-06-10 22:31:58 +03:00
else
2020-01-31 21:55:10 +03:00
nvm.liquid.name = nil
stop_firebox(pos, nvm)
2019-06-10 22:31:58 +03:00
return false
end
end
return true
end
end
2020-01-31 21:55:10 +03:00
local function start_firebox(pos, nvm)
if not nvm.running then
nvm.running = true
2019-06-10 22:31:58 +03:00
node_timer(pos, 0)
firebox.swap_node(pos, "techage:furnace_firebox_on")
minetest.get_node_timer(pos):start(CYCLE_TIME)
2020-01-31 21:55:10 +03:00
M(pos):set_string("formspec", fuel.formspec(nvm))
2019-06-10 22:31:58 +03:00
end
end
local function booster_cmnd(pos, cmnd)
return techage.transfer(
pos,
"L", -- outdir
cmnd, -- topic
nil, -- payload
nil, -- network
{"techage:ta3_booster", "techage:ta3_booster_on"})
end
local _liquid = {
capa = fuel.CAPACITY,
fuel_cat = fuel.BT_OIL,
peek = liquid.srv_peek,
put = function(pos, indir, name, amount)
if fuel.valid_fuel(name, fuel.BT_OIL) then
return liquid.srv_put(pos, indir, name, amount)
end
return amount
end,
take = liquid.srv_take,
}
local _networks = {
2020-01-31 21:55:10 +03:00
pipe2 = {
sides = techage.networks.AllSides, -- Pipe connection sides
ntype = "tank",
},
}
2019-06-10 22:31:58 +03:00
minetest.register_node("techage:furnace_firebox", {
description = S("TA3 Furnace Oil Burner"),
2019-06-10 22:31:58 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_concrete.png^techage_appl_open.png^techage_frame_ta3.png",
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
2019-06-10 22:31:58 +03:00
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_appl_firehole.png^techage_frame_ta3.png",
},
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
groups = {cracky=2},
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
on_timer = node_timer,
can_dig = fuel.can_dig,
allow_metadata_inventory_take = fuel.allow_metadata_inventory_take,
allow_metadata_inventory_put = fuel.allow_metadata_inventory_put,
on_metadata_inventory_put = fuel.on_metadata_inventory_put,
on_receive_fields = fuel.on_receive_fields,
on_rightclick = fuel.on_rightclick,
liquid = _liquid,
networks = _networks,
2019-06-10 22:31:58 +03:00
on_construct = function(pos)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
techage.add_node(pos, "techage:furnace_firebox")
2020-01-31 21:55:10 +03:00
nvm.running = false
nvm.burn_cycles = 0
nvm.liquid = {}
nvm.liquid.amount = 0
2019-06-10 22:31:58 +03:00
local meta = M(pos)
2020-01-31 21:55:10 +03:00
meta:set_string("formspec", fuel.formspec(nvm))
2019-06-10 22:31:58 +03:00
local inv = meta:get_inventory()
inv:set_size('fuel', 1)
end,
})
minetest.register_node("techage:furnace_firebox_on", {
description = S("TA3 Furnace Oil Burner"),
2019-06-10 22:31:58 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_frame_ta3.png",
"techage_concrete.png^techage_frame_ta3.png",
{
image = "techage_concrete4.png^techage_appl_firehole4.png^techage_frame4_ta3.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.4,
},
},
},
paramtype2 = "facedir",
light_source = 8,
on_rotate = screwdriver.disallow,
diggable = false,
groups = {not_in_creative_inventory = 1},
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
drop = "techage:furnace_firebox",
on_timer = node_timer,
can_dig = fuel.can_dig,
allow_metadata_inventory_put = fuel.allow_metadata_inventory_put,
allow_metadata_inventory_take = fuel.allow_metadata_inventory_take,
on_receive_fields = fuel.on_receive_fields,
on_rightclick = fuel.on_rightclick,
liquid = _liquid,
networks = _networks,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.liquid = nvm.liquid or {}
nvm.liquid.amount = nvm.liquid.amount or 0
start_firebox(pos, nvm)
fuel.on_metadata_inventory_put(pos, listname, index, stack, player)
end,
2019-06-10 22:31:58 +03:00
})
minetest.register_craft({
output = "techage:furnace_firebox",
recipe = {
{'techage:basalt_stone', 'techage:basalt_stone', 'techage:basalt_stone'},
{'default:steel_ingot', '', 'default:steel_ingot'},
{'techage:basalt_stone', 'techage:basalt_stone', 'techage:basalt_stone'},
},
})
techage.register_node({"techage:furnace_firebox", "techage:furnace_firebox_on"}, {
on_recv_message = function(pos, src, topic, payload)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2019-06-10 22:31:58 +03:00
if topic == "state" then
2020-01-31 21:55:10 +03:00
if nvm.running then
return "running"
else
return "stopped"
end
elseif topic == "fuel" then
2020-01-31 21:55:10 +03:00
return nvm.liquid and nvm.liquid.amount and nvm.liquid.amount
2019-06-10 22:31:58 +03:00
else
return "unsupported"
end
end,
-- called from furnace_top
on_transfer = function(pos, in_dir, topic, payload)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2019-06-10 22:31:58 +03:00
if topic == "fuel" then
2020-01-31 21:55:10 +03:00
print(dump(nvm))
return has_fuel(pos, nvm) and booster_cmnd(pos, "power")
elseif topic == "running" then
2020-01-31 21:55:10 +03:00
return nvm.running and booster_cmnd(pos, "power")
2019-06-10 22:31:58 +03:00
elseif topic == "start" then
2020-01-31 21:55:10 +03:00
start_firebox(pos, nvm)
2019-06-10 22:31:58 +03:00
booster_cmnd(pos, "start")
elseif topic == "stop" then
2020-01-31 21:55:10 +03:00
stop_firebox(pos, nvm)
2019-06-10 22:31:58 +03:00
booster_cmnd(pos, "stop")
end
end
})
Pipe:add_secondary_node_names({"techage:furnace_firebox", "techage:furnace_firebox_on"})
2019-06-10 22:31:58 +03:00
minetest.register_lbm({
label = "[techage] Furnace firebox",
name = "techage:furnace",
nodenames = {"techage:furnace_firebox_on"},
run_at_every_load = true,
action = function(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
})
2019-06-11 21:31:53 +03:00