techage/furnace/booster.lua

200 lines
5.3 KiB
Lua
Raw Permalink 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
2020-10-19 20:09:17 +03:00
AGPL v3
2019-06-10 22:31:58 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2019-06-10 22:31:58 +03:00
TA3 Booster
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
2019-06-10 22:31:58 +03:00
2019-06-16 22:06:16 +03:00
local PWR_NEEDED = 3
local CYCLE_TIME = 2
2019-06-10 22:31:58 +03:00
2020-01-31 21:55:10 +03:00
local Cable = techage.ElectricCable
2021-06-09 23:09:59 +03:00
local power = networks.power
2019-06-10 22:31:58 +03:00
local function infotext(pos, state)
M(pos):set_string("infotext", S("TA3 Booster")..": "..state)
2019-06-10 22:31:58 +03:00
end
local function swap_node(pos, name)
local node = techage.get_node_lvm(pos)
2019-06-10 22:31:58 +03:00
if node.name == name then
return
end
node.name = name
minetest.swap_node(pos, node)
end
2020-01-31 21:55:10 +03:00
local function play_sound(pos)
local mem = techage.get_mem(pos)
2020-02-02 00:00:58 +03:00
if not mem.handle or mem.handle == -1 then
mem.handle = minetest.sound_play("techage_booster", {
2022-01-03 23:40:31 +03:00
pos = pos,
2020-02-02 00:00:58 +03:00
gain = 1,
max_hear_distance = 7,
loop = true})
if mem.handle == -1 then
minetest.after(1, play_sound, pos)
end
end
end
2020-01-31 21:55:10 +03:00
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 after_place_node(pos)
local nvm = techage.get_nvm(pos)
Cable:after_place_node(pos)
local node = minetest.get_node(pos)
local indir = techage.side_to_indir("R", node.param2)
M(pos):set_int("indir", indir)
infotext(pos, "stopped")
end
local function after_dig_node(pos, oldnode)
Cable:after_dig_node(pos)
techage.del_mem(pos)
end
2019-06-10 22:31:58 +03:00
minetest.register_node("techage:ta3_booster", {
description = S("TA3 Booster"),
2019-06-10 22:31:58 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_appl_arrow.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
2019-06-17 20:57:08 +03:00
"techage_filling_ta3.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
2019-06-10 22:31:58 +03:00
"techage_filling_ta3.png^techage_appl_compressor.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_compressor.png^[transformFX^techage_frame_ta3.png",
},
2022-01-03 23:40:31 +03:00
2021-06-09 23:09:59 +03:00
on_timer = function(pos, elapsed)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
if consumed == PWR_NEEDED then
swap_node(pos, "techage:ta3_booster_on")
infotext(pos, "running")
play_sound(pos)
end
return true
end,
2020-01-31 21:55:10 +03:00
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2022-01-03 23:40:31 +03:00
2019-06-10 22:31:58 +03:00
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:ta3_booster_on", {
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_appl_arrow.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_frame_ta3.png",
"techage_filling_ta3.png^techage_appl_hole_pipe.png^techage_frame_ta3.png",
2019-06-17 20:57:08 +03:00
"techage_filling_ta3.png^techage_appl_hole_electric.png^techage_frame_ta3.png",
2019-06-10 22:31:58 +03:00
{
name = "techage_filling4_ta3.png^techage_appl_compressor4.png^techage_frame4_ta3.png",
2019-06-10 22:31:58 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.2,
},
},
{
name = "techage_filling4_ta3.png^techage_appl_compressor4.png^[transformFX]^techage_frame4_ta3.png",
2019-06-10 22:31:58 +03:00
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 0.2,
},
},
},
2022-01-03 23:40:31 +03:00
2021-06-09 23:09:59 +03:00
on_timer = function(pos, elapsed)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
if consumed < PWR_NEEDED then
swap_node(pos, "techage:ta3_booster")
infotext(pos, "no power")
stop_sound(pos)
end
return true
end,
2020-01-31 21:55:10 +03:00
after_place_node = after_place_node,
after_dig_node = after_dig_node,
2022-01-03 23:40:31 +03:00
2019-06-10 22:31:58 +03:00
paramtype2 = "facedir",
groups = {not_in_creative_inventory = 1},
diggable = false,
on_rotate = screwdriver.disallow,
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2021-06-09 23:09:59 +03:00
power.register_nodes({"techage:ta3_booster", "techage:ta3_booster_on"}, Cable, "con", {"B", "F", "L", "D", "U"})
2019-06-10 22:31:58 +03:00
-- for intra machine communication
techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, {
on_transfer = function(pos, in_dir, topic, payload)
if M(pos):get_int("indir") == in_dir then
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
2019-06-10 22:31:58 +03:00
if topic == "power" then
2020-02-02 00:00:58 +03:00
return techage.get_node_lvm(pos).name == "techage:ta3_booster_on" or
power.power_available(pos, Cable)
2020-07-16 19:34:19 +03:00
elseif topic == "running" then
return techage.get_node_lvm(pos).name == "techage:ta3_booster_on"
2020-01-31 21:55:10 +03:00
elseif topic == "start" and not nvm.running then
if power.power_available(pos, Cable) then
nvm.running = true
2019-06-17 20:57:08 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
2021-06-09 23:09:59 +03:00
swap_node(pos, "techage:ta3_booster_on")
infotext(pos, "running")
play_sound(pos)
2019-06-10 22:31:58 +03:00
else
infotext(pos, "no power")
end
elseif topic == "stop" then
2020-01-31 21:55:10 +03:00
nvm.running = false
2019-06-10 22:31:58 +03:00
swap_node(pos, "techage:ta3_booster")
2019-06-17 20:57:08 +03:00
minetest.get_node_timer(pos):stop()
2019-08-17 00:44:11 +03:00
infotext(pos, "stopped")
2020-01-31 21:55:10 +03:00
stop_sound(pos)
2019-06-10 22:31:58 +03:00
end
end
2020-02-02 00:00:58 +03:00
end,
on_node_load = function(pos, node)
if node.name == "techage:ta3_booster_on" then
play_sound(pos)
2022-01-03 23:40:31 +03:00
end
2020-02-02 00:00:58 +03:00
end,
2019-06-10 22:31:58 +03:00
})
minetest.register_craft({
output = "techage:ta3_booster",
recipe = {
{"basic_materials:steel_bar", "default:wood", "basic_materials:steel_bar"},
2019-06-15 17:14:46 +03:00
{"", "basic_materials:gear_steel", ""},
2019-06-10 22:31:58 +03:00
{"basic_materials:steel_bar", "default:wood", "basic_materials:steel_bar"},
},
})