techage/coal_power_station/turbine.lua

187 lines
4.8 KiB
Lua
Raw Normal View History

--[[
TechAge
=======
Copyright (C) 2019 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
2019-05-05 23:29:00 +03:00
TA3 Power Station Turbine
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
2019-06-08 23:57:01 +03:00
local Pipe = techage.SteamPipe
2019-06-16 22:06:16 +03:00
local function transfer_cooler(pos, topic, payload)
return techage.transfer(pos, 6, topic, payload, Pipe,
{"techage:cooler", "techage:cooler_on"})
end
local function transfer_generator(pos, topic, payload)
return techage.transfer(pos, "R", topic, payload, nil,
{"techage:generator", "techage:generator_on"})
end
local function swap_node(pos, name)
local node = techage.get_node_lvm(pos)
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
2019-11-11 23:25:20 +03:00
local function node_timer(pos)
local mem = tubelib2.get_mem(pos)
2019-05-07 20:55:07 +03:00
if mem.running then
mem.handle = minetest.sound_play("techage_turbine", {
pos = pos,
gain = 1,
max_hear_distance = 15})
2019-11-11 23:25:20 +03:00
end
return mem.running
end
local function play_sound(pos)
local mem = tubelib2.get_mem(pos)
if mem.running then
node_timer(pos)
minetest.get_node_timer(pos):start(2)
2019-05-07 20:55:07 +03:00
end
end
2019-05-07 20:55:07 +03:00
local function stop_sound(pos)
local mem = tubelib2.get_mem(pos)
2019-05-07 20:55:07 +03:00
if mem.running and mem.handle then
minetest.sound_stop(mem.handle)
mem.handle = nil
2019-11-11 23:25:20 +03:00
minetest.get_node_timer(pos):stop(2)
2019-05-07 20:55:07 +03:00
end
end
2019-05-05 23:29:00 +03:00
minetest.register_node("techage:turbine", {
description = S("TA3 Turbine"),
tiles = {
-- up, down, right, left, back, front
2019-05-05 23:29:00 +03:00
"techage_filling_ta3.png^techage_frame_ta3.png^techage_steam_hole.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_open.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_steam_hole.png",
"techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_turbine.png^techage_frame_ta3.png",
},
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2019-05-05 23:29:00 +03:00
minetest.register_node("techage:turbine_on", {
tiles = {
-- up, down, right, left, back, front
2019-05-05 23:29:00 +03:00
"techage_filling_ta3.png^techage_frame_ta3.png^techage_steam_hole.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_open.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_steam_hole.png",
{
2019-05-06 22:09:13 +03:00
image = "techage_filling4_ta3.png^techage_appl_turbine4.png^techage_frame4_ta3.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.4,
},
},
{
2019-05-06 22:09:13 +03:00
image = "techage_filling4_ta3.png^techage_appl_turbine4.png^techage_frame4_ta3.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.4,
},
},
},
2019-11-11 23:25:20 +03:00
on_timer = node_timer,
paramtype2 = "facedir",
groups = {not_in_creative_inventory=1},
diggable = false,
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2019-06-08 23:57:01 +03:00
-- for mechanical pipe connections
2019-05-21 17:37:05 +03:00
techage.power.register_node({"techage:turbine", "techage:turbine_on"}, {
conn_sides = {"L", "U"},
2019-06-08 23:57:01 +03:00
power_network = Pipe,
after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir)
local mem = tubelib2.get_mem(pos)
transfer_cooler(pos, "stop", nil)
swap_node(pos, "techage:turbine")
mem.running = false
stop_sound(pos)
end
2019-06-08 23:57:01 +03:00
})
-- for logical communication
techage.register_node({"techage:turbine", "techage:turbine_on"}, {
on_transfer = function(pos, in_dir, topic, payload)
local mem = tubelib2.get_mem(pos)
2019-06-16 22:06:16 +03:00
if topic == "trigger" then
return transfer_generator(pos, topic, payload)
2019-06-08 23:57:01 +03:00
elseif topic == "start" then
2019-06-16 22:06:16 +03:00
if transfer_cooler(pos, topic, payload) then
2019-06-08 23:57:01 +03:00
swap_node(pos, "techage:turbine_on")
mem.running = true
play_sound(pos)
2019-06-16 22:06:16 +03:00
return true
2019-06-08 23:57:01 +03:00
end
2019-06-16 22:06:16 +03:00
return false
elseif topic == "running" then
if not transfer_cooler(pos, topic, payload) then
swap_node(pos, "techage:turbine")
mem.running = false
stop_sound(pos)
return false
end
return true
2019-06-08 23:57:01 +03:00
elseif topic == "stop" then
2019-06-16 22:06:16 +03:00
transfer_cooler(pos, topic, payload)
2019-06-08 23:57:01 +03:00
swap_node(pos, "techage:turbine")
mem.running = false
stop_sound(pos)
2019-06-16 22:06:16 +03:00
return true
2019-06-08 23:57:01 +03:00
end
end
2019-05-21 17:37:05 +03:00
})
2019-05-05 23:29:00 +03:00
minetest.register_craft({
output = "techage:turbine",
recipe = {
2019-06-08 23:57:01 +03:00
{"basic_materials:steel_bar", "techage:steam_pipeS", "default:wood"},
2019-05-05 23:29:00 +03:00
{"techage:steam_pipeS", "basic_materials:gear_steel", ""},
{"default:wood", "techage:iron_ingot", "basic_materials:steel_bar"},
},
})
2019-05-07 20:55:07 +03:00
minetest.register_lbm({
label = "[techage] Turbine sound",
name = "techage:power_station",
nodenames = {"techage:turbine_on"},
run_at_every_load = true,
action = function(pos, node)
play_sound(pos)
end
})