techage/oil/reboiler.lua

273 lines
6.8 KiB
Lua
Raw Normal View History

--[[
TechAge
=======
2020-02-02 00:00:58 +03:00
Copyright (C) 2019-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
TA3 Oil Reboiler
]]--
local S2P = minetest.string_to_pos
local P2S = minetest.pos_to_string
local M = minetest.get_meta
local S = techage.S
local Pipe = techage.LiquidPipe
local networks = techage.networks
local liquid = techage.liquid
local Flip = techage.networks.Flip
local Cable = techage.ElectricCable
local power = techage.power
local CYCLE_TIME = 6
local CAPA = 12
local PWR_NEEDED = 14
2020-02-02 00:00:58 +03:00
local function play_sound(pos)
local mem = techage.get_mem(pos)
if not mem.handle or mem.handle == -1 then
mem.handle = minetest.sound_play("techage_reboiler", {
pos = pos,
gain = 1,
max_hear_distance = 15,
loop = true})
if mem.handle == -1 then
minetest.after(1, play_sound, pos)
end
end
end
local function stop_sound(pos)
local mem = techage.get_mem(pos)
if mem.handle then
minetest.sound_stop(mem.handle)
mem.handle = nil
end
end
local function swap_node(pos, on)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
if on then
local node = techage.get_node_lvm(pos)
node.name = "techage:ta3_reboiler_on"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
2020-02-02 00:00:58 +03:00
play_sound(pos)
2020-01-31 21:55:10 +03:00
elseif not on and nvm.running then
local node = techage.get_node_lvm(pos)
node.name = "techage:ta3_reboiler"
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
2020-01-31 21:55:10 +03:00
nvm.running = false
power.consumer_stop(pos, Cable)
2020-02-02 00:00:58 +03:00
stop_sound(pos)
end
end
2020-02-02 00:00:58 +03:00
local function on_power(pos)
swap_node(pos, true)
end
2020-02-02 00:00:58 +03:00
local function on_nopower(pos)
swap_node(pos, false)
end
local function pump_cmnd(pos, cmnd, payload)
return techage.transfer(
pos,
"R", -- outdir
cmnd, -- topic
payload, -- payload
Pipe, -- Pipe
{"techage:ta3_distiller1"})
end
local function start_node(pos)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
if nvm.running then return end
2020-01-31 21:55:10 +03:00
nvm.liquid = nvm.liquid or {}
nvm.liquid.amount = nvm.liquid.amount or 0
if nvm.liquid.amount >= 5 and nvm.liquid.name == "techage:oil_source" then
if power.power_available(pos, Cable) then
nvm.running = true
power.consumer_start(pos, Cable, CYCLE_TIME)
end
end
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
2020-01-31 21:55:10 +03:00
power.consumer_alive(pos, nvm)
2020-01-31 21:55:10 +03:00
if nvm.liquid.amount >= 5 and nvm.liquid.name == "techage:oil_source" then
nvm.liquid.amount = nvm.liquid.amount - 5
local leftover = pump_cmnd(pos, "put")
if (tonumber(leftover) or 1) > 0 then
2020-01-31 21:55:10 +03:00
nvm.liquid.amount = nvm.liquid.amount + 5
nvm.error = 25 -- = 5 pump cycles
M(pos):set_string("infotext", S("TA3 Oil Reboiler: blocked"))
swap_node(pos, false)
return false
end
return true
end
swap_node(pos, false)
return false
end
2020-02-02 00:00:58 +03:00
local function after_place_node(pos)
Pipe:after_place_node(pos)
Cable:after_place_node(pos)
end
2020-02-02 00:00:58 +03:00
local function after_dig_node(pos, oldnode)
Pipe:after_dig_node(pos)
2020-02-02 00:00:58 +03:00
Cable:after_dig_node(pos)
2020-01-31 21:55:10 +03:00
techage.del_mem(pos)
end
2020-02-02 00:00:58 +03:00
local function tubelib2_on_update2(pos, outdir, tlib2, node)
if tlib2 == Pipe then
liquid.update_network(pos, outdir)
else
power.update_network(pos, outdir, tlib2)
end
end
local liquid_def = {
capa = CAPA,
peek = liquid.srv_peek,
put = function(pos, indir, name, amount)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
if nvm.error and nvm.error > 0 then
nvm.error = nvm.error - 1
if nvm.error <= 0 then
M(pos):set_string("infotext", S("TA3 Oil Reboiler"))
start_node(pos)
return liquid.srv_put(pos, indir, name, amount)
else
return amount
end
else
start_node(pos)
return liquid.srv_put(pos, indir, name, amount)
end
end,
take = liquid.srv_take,
}
2020-02-02 00:00:58 +03:00
local net_def = {
2020-01-31 21:55:10 +03:00
pipe2 = {
sides = {L = true, R = true}, -- Pipe connection sides
ntype = "tank",
},
2020-02-02 00:00:58 +03:00
ele1 = {
sides = techage.networks.AllSides, -- Cable connection sides
ntype = "con1",
on_power = on_power,
on_nopower = on_nopower,
nominal = PWR_NEEDED,
},
}
minetest.register_node("techage:ta3_reboiler", {
description = S("TA3 Oil Reboiler"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png^techage_appl_arrow.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_reboiler.png^techage_frame_ta3.png^[transformFX",
"techage_filling_ta3.png^techage_appl_reboiler.png^techage_frame_ta3.png",
},
after_place_node = function(pos, placer)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
nvm.liquid = {}
local meta = M(pos)
meta:set_string("infotext", S("TA3 Oil Reboiler"))
meta:set_int("outdir", networks.side_to_outdir(pos, "R"))
Pipe:after_place_node(pos)
power.after_place_node(pos)
end,
2020-02-02 00:00:58 +03:00
tubelib2_on_update2 = tubelib2_on_update2,
on_timer = node_timer,
2020-02-02 00:00:58 +03:00
after_place_node = after_place_node,
after_dig_node = after_dig_node,
after_dig_node = after_dig_node,
2020-02-02 00:00:58 +03:00
liquid = liquid_def,
networks = net_def,
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
groups = {cracky=2},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("techage:ta3_reboiler_on", {
description = S("TA3 Oil Reboiler"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png^techage_appl_arrow.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
2020-02-02 00:00:58 +03:00
{
image = "techage_filling4_ta3.png^techage_appl_reboiler4.png^techage_frame4_ta3.png^[transformFX",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
2020-02-02 00:00:58 +03:00
{
image = "techage_filling4_ta3.png^techage_appl_reboiler4.png^techage_frame4_ta3.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 2.0,
},
},
},
2020-02-02 00:00:58 +03:00
tubelib2_on_update2 = tubelib2_on_update2,
on_timer = node_timer,
2020-02-02 00:00:58 +03:00
liquid = liquid_def,
networks = net_def,
paramtype2 = "facedir",
on_rotate = screwdriver.disallow,
diggable = false,
groups = {not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_metal_defaults(),
})
Pipe:add_secondary_node_names({"techage:ta3_reboiler", "techage:ta3_reboiler_on"})
2020-02-02 00:00:58 +03:00
Cable:add_secondary_node_names({"techage:ta3_reboiler", "techage:ta3_reboiler_on"})
minetest.register_craft({
output = "techage:ta3_reboiler",
recipe = {
{"", "basic_materials:heating_element", ""},
{"default:mese_crystal_fragment", "techage:t3_pump", "default:mese_crystal_fragment"},
{"", "basic_materials:heating_element", ""},
},
})