techage/furnace/firebox.lua
2019-08-25 18:08:30 +02:00

229 lines
6.3 KiB
Lua

--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
TA3 Industrial Furnace Firebox
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local firebox = techage.firebox
local CYCLE_TIME = 2
local function has_fuel(pos, mem)
return mem.burn_cycles > 0 or firebox.has_fuel(pos)
end
local function stop_firebox(pos, mem)
mem.running = false
firebox.swap_node(pos, "techage:furnace_firebox")
minetest.get_node_timer(pos):stop()
M(pos):set_string("formspec", firebox.formspec(mem))
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
if mem.running then
mem.burn_cycles = (mem.burn_cycles or 0) - 1
if mem.burn_cycles <= 0 then
local taken = firebox.get_fuel(pos)
if taken then
mem.burn_cycles = (firebox.Burntime[taken:get_name()] or 1) / CYCLE_TIME
mem.burn_cycles_total = mem.burn_cycles
else
stop_firebox(pos, mem)
return false
end
end
return true
end
end
local function start_firebox(pos, mem)
if not mem.running then
mem.running = true
node_timer(pos, 0)
firebox.swap_node(pos, "techage:furnace_firebox_on")
minetest.get_node_timer(pos):start(CYCLE_TIME)
M(pos):set_string("formspec", firebox.formspec(mem))
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
minetest.register_node("techage:furnace_firebox", {
description = S("TA3 Furnace Firebox"),
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_biogas.png^techage_frame_ta3.png",
"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 = firebox.can_dig,
allow_metadata_inventory_put = firebox.allow_metadata_inventory,
allow_metadata_inventory_take = firebox.allow_metadata_inventory,
on_receive_fields = firebox.on_receive_fields,
on_rightclick = firebox.on_rightclick,
on_construct = function(pos)
local mem = tubelib2.init_mem(pos)
mem.running = false
mem.burn_cycles = 0
local meta = M(pos)
meta:set_string("formspec", firebox.formspec(mem))
local inv = meta:get_inventory()
inv:set_size('fuel', 1)
end,
})
minetest.register_node("techage:furnace_firebox_on", {
description = S("TA3 Furnace Firebox"),
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 = firebox.can_dig,
allow_metadata_inventory_put = firebox.allow_metadata_inventory,
allow_metadata_inventory_take = firebox.allow_metadata_inventory,
on_receive_fields = firebox.on_receive_fields,
on_rightclick = firebox.on_rightclick,
})
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_pull_item = function(pos, in_dir, num)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.get_items(inv, "fuel", num)
end,
on_push_item = function(pos, in_dir, stack)
if firebox.Burntime[stack:get_name()] then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local mem = tubelib2.get_mem(pos)
return techage.put_items(inv, "fuel", stack)
end
return false
end,
on_unpull_item = function(pos, in_dir, stack)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.put_items(inv, "fuel", stack)
end,
on_recv_message = function(pos, src, topic, payload)
local mem = tubelib2.get_mem(pos)
if topic == "state" then
if mem.running then
return "running"
else
return "stopped"
end
elseif topic == "fuel" then
local inv = M(pos):get_inventory()
local stack = inv:get_stack("fuel", 1)
return stack:get_count()
else
return "unsupported"
end
end,
-- called from furnace_top
on_transfer = function(pos, in_dir, topic, payload)
if topic == "fuel" then
local mem = tubelib2.get_mem(pos)
if booster_cmnd(pos, "power") then
-- fire keeps on even if the booster is off for some seconds
mem.booster_cnt = 3
end
mydbg("dbg2", "firebox fuel", mem.booster_cnt)
mem.booster_cnt = (mem.booster_cnt or 3) - 1
return has_fuel(pos, mem) and mem.booster_cnt > 0
elseif topic == "start" then
local mem = tubelib2.get_mem(pos)
start_firebox(pos, mem)
booster_cmnd(pos, "start")
mem.booster_cnt = 4
mydbg("dbg2", "firebox start", mem.booster_cnt)
elseif topic == "stop" then
local mem = tubelib2.get_mem(pos)
stop_firebox(pos, mem)
booster_cmnd(pos, "stop")
mydbg("dbg2", "firebox stop", mem.booster_cnt)
end
end
})
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
})
techage.register_entry_page("ta3f", "firebox",
S("TA3 Furnace Firebox"),
S("Part of the TA3 Industrial Furnace. Faster and more powerful than the standard furnace."),
"techage:furnace_firebox")