techage/icta_controller/signaltower.lua

159 lines
3.9 KiB
Lua
Raw Permalink Normal View History

2020-02-29 21:20:54 +03:00
--[[
TechAge
=======
2022-06-05 22:20:43 +03:00
Copyright (C) 2019-2022 Joachim Stolberg
2020-02-29 21:20:54 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2020-02-29 21:20:54 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-02-29 21:20:54 +03:00
ICTA Controller - Signal Tower
]]--
local function switch_on(pos, node, color)
local meta = minetest.get_meta(pos)
meta:set_string("state", color)
2020-03-01 14:32:11 +03:00
node.name = "techage:ta4_signaltower_"..color
2020-02-29 21:20:54 +03:00
minetest.swap_node(pos, node)
2022-01-03 23:40:31 +03:00
end
2020-02-29 21:20:54 +03:00
local function switch_off(pos, node)
local meta = minetest.get_meta(pos)
meta:set_string("state", "off")
2020-03-01 14:32:11 +03:00
node.name = "techage:ta4_signaltower"
2020-02-29 21:20:54 +03:00
minetest.swap_node(pos, node)
2022-01-03 23:40:31 +03:00
end
2020-02-29 21:20:54 +03:00
2020-03-01 14:32:11 +03:00
minetest.register_node("techage:ta4_signaltower", {
2020-02-29 21:20:54 +03:00
description = "TA4 Signal Tower",
tiles = {
'techage_signaltower_top.png',
'techage_signaltower_top.png',
'techage_signaltower.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -5/32, -16/32, -5/32, 5/32, 16/32, 5/32},
},
},
2022-01-03 23:40:31 +03:00
2020-02-29 21:20:54 +03:00
after_place_node = function(pos, placer)
2020-03-01 14:32:11 +03:00
local number = techage.add_node(pos, "techage:ta4_signaltower")
2020-02-29 21:20:54 +03:00
local meta = minetest.get_meta(pos)
meta:set_string("state", "off")
meta:set_string("infotext", "TA4 Signal Tower "..number)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
switch_on(pos, node, "green")
end
end,
2020-06-14 00:32:00 +03:00
after_dig_node = function(pos, oldnode, oldmetadata)
techage.remove_node(pos, oldnode, oldmetadata)
2020-02-29 21:20:54 +03:00
end,
paramtype = "light",
use_texture_alpha = techage.CLIP,
2022-01-03 23:40:31 +03:00
light_source = 0,
2020-02-29 21:20:54 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_glass_defaults(),
})
for _,color in ipairs({"green", "amber", "red"}) do
2020-03-01 14:32:11 +03:00
minetest.register_node("techage:ta4_signaltower_"..color, {
2020-02-29 21:20:54 +03:00
description = "TA4 Signal Tower",
tiles = {
'techage_signaltower_top.png',
'techage_signaltower_top.png',
'techage_signaltower_'..color..'.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -5/32, -16/32, -5/32, 5/32, 16/32, 5/32},
},
},
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
switch_off(pos, node)
end
end,
paramtype = "light",
use_texture_alpha = techage.CLIP,
2022-01-03 23:40:31 +03:00
light_source = 10,
2020-02-29 21:20:54 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
groups = {crumbly=0, not_in_creative_inventory=1},
is_ground_content = false,
sounds = default.node_sound_glass_defaults(),
2020-03-01 14:32:11 +03:00
drop = "techage:ta4_signaltower",
2020-02-29 21:20:54 +03:00
})
end
minetest.register_craft({
2020-03-01 14:32:11 +03:00
output = "techage:ta4_signaltower",
2020-02-29 21:20:54 +03:00
recipe = {
{"dye:red", "default:copper_ingot", ""},
{"dye:orange", "default:glass", ""},
{"dye:green", "techage:ta4_wlanchip", ""},
},
})
2022-01-03 23:40:31 +03:00
techage.register_node({"techage:ta4_signaltower",
"techage:ta4_signaltower_green",
"techage:ta4_signaltower_amber",
2020-03-01 14:32:11 +03:00
"techage:ta4_signaltower_red"}, {
2020-02-29 21:20:54 +03:00
on_recv_message = function(pos, src, topic, payload)
local node = minetest.get_node(pos)
if topic == "green" then
switch_on(pos, node, "green")
elseif topic == "amber" then
switch_on(pos, node, "amber")
elseif topic == "red" then
switch_on(pos, node, "red")
elseif topic == "off" then
switch_off(pos, node)
elseif topic == "state" then
local meta = minetest.get_meta(pos)
return meta:get_string("state")
end
end,
2022-06-05 22:20:43 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
if topic == 2 then
local color = ({"green", "amber", "red"})[payload[1]]
local node = minetest.get_node(pos)
if color then
switch_on(pos, node, color)
else
switch_off(pos, node)
end
return 0
else
return 2 -- unknown or invalid topic
end
end,
on_beduino_request_data = function(pos, src, topic, payload)
if topic == 130 then
local meta = minetest.get_meta(pos)
local color = ({off = 0, green = 1, amber = 2, red = 3})[meta:get_string("state")] or 1
return 0, {color}
else
return 2, "" -- unknown or invalid topic
end
end,
})