techage/power/powerswitch.lua

304 lines
8.1 KiB
Lua
Raw Normal View History

2020-01-31 21:55:10 +03:00
--[[
TechAge
=======
2022-06-06 21:39:55 +03:00
Copyright (C) 2019-2022 Joachim Stolberg
2020-01-31 21:55:10 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2020-01-31 21:55:10 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-01-31 21:55:10 +03:00
TA3 Power Switch (large and small)
]]--
-- for lazy programmers
local P = minetest.string_to_pos
local M = minetest.get_meta
local S = techage.S
local Cable = techage.ElectricCable
2021-06-05 21:18:04 +03:00
local power = networks.power
2020-01-31 21:55:10 +03:00
local Param2ToDir = {
[0] = 6,
[1] = 5,
[2] = 2,
[3] = 4,
[4] = 1,
[5] = 3,
}
2021-07-19 21:37:31 +03:00
local function sign_in(pos, node)
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
M(pos2):set_int("switch_sign_in", 1)
end
local function sign_off(pos, node)
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
M(pos2):set_int("switch_sign_in", 0)
end
2020-01-31 21:55:10 +03:00
local function switch_on(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
end
node.name = name
minetest.swap_node(pos, node)
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
2022-01-03 23:40:31 +03:00
2021-06-06 16:53:19 +03:00
techage.legacy_switches(pos2)
2021-06-05 21:18:04 +03:00
power.turn_switch_on(pos2, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on")
2020-01-31 21:55:10 +03:00
end
local function switch_off(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
end
node.name = name
minetest.swap_node(pos, node)
minetest.get_node_timer(pos):stop()
minetest.sound_play("techage_button", {
pos = pos,
gain = 0.5,
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
2022-01-03 23:40:31 +03:00
2021-06-06 16:53:19 +03:00
techage.legacy_switches(pos2)
2021-06-05 21:18:04 +03:00
power.turn_switch_off(pos2, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on")
2020-01-31 21:55:10 +03:00
end
minetest.register_node("techage:powerswitch", {
description = S("TA Power Switch"),
inventory_image = "techage_appl_switch_inv.png",
tiles = {
'techage_appl_switch_off.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -1/4, -8/16, -1/4, 1/4, -7/16, 1/4},
{ -1/6, -12/16, -1/6, 1/6, -8/16, 1/6},
},
},
2022-01-03 23:40:31 +03:00
2020-01-31 21:55:10 +03:00
after_place_node = function(pos, placer)
local meta = M(pos)
local number = techage.add_node(pos, "techage:powerswitch")
meta:set_string("node_number", number)
meta:set_string("owner", placer:get_player_name())
meta:set_string("infotext", S("TA Power Switch").." "..number)
2020-02-25 21:43:27 +03:00
local node = minetest.get_node(pos)
switch_on(pos, node, placer, "techage:powerswitch_on")
2021-07-19 21:37:31 +03:00
sign_in(pos, node)
2020-01-31 21:55:10 +03:00
end,
on_rightclick = function(pos, node, clicker)
switch_on(pos, node, clicker, "techage:powerswitch_on")
end,
2021-07-19 21:37:31 +03:00
after_dig_node = sign_off,
2020-01-31 21:55:10 +03:00
on_rotate = screwdriver.disallow,
paramtype = "light",
use_texture_alpha = techage.CLIP,
2020-01-31 21:55:10 +03:00
sunlight_propagates = true,
paramtype2 = "wallmounted",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:powerswitch_on", {
description = S("TA Power Switch"),
inventory_image = "techage_appl_switch_inv.png",
tiles = {
'techage_appl_switch_on.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -1/4, -8/16, -1/4, 1/4, -7/16, 1/4},
{ -1/6, -12/16, -1/6, 1/6, -8/16, 1/6},
},
},
2022-01-03 23:40:31 +03:00
2020-01-31 21:55:10 +03:00
on_rightclick = function(pos, node, clicker)
switch_off(pos, node, clicker, "techage:powerswitch")
end,
2021-07-19 21:37:31 +03:00
after_dig_node = sign_off,
2020-01-31 21:55:10 +03:00
drop = "techage:powerswitch",
on_rotate = screwdriver.disallow,
paramtype = "light",
use_texture_alpha = techage.CLIP,
2020-01-31 21:55:10 +03:00
sunlight_propagates = true,
paramtype2 = "wallmounted",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory = 1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:powerswitchsmall", {
description = S("TA Power Switch Small"),
inventory_image = "techage_smart_button_inventory.png",
tiles = {
'techage_smart_button_off.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -5/32, -16/32, -5/32, 5/32, -15/32, 5/32},
{ -2/16, -12/16, -2/16, 2/16, -8/16, 2/16},
},
},
2022-01-03 23:40:31 +03:00
2020-01-31 21:55:10 +03:00
after_place_node = function(pos, placer)
local meta = M(pos)
local number = techage.add_node(pos, "techage:powerswitchsmall")
meta:set_string("node_number", number)
meta:set_string("owner", placer:get_player_name())
meta:set_string("infotext", S("TA Power Switch Small").." "..number)
2020-02-25 21:43:27 +03:00
local node = minetest.get_node(pos)
switch_on(pos, node, placer, "techage:powerswitchsmall_on")
2021-07-19 21:37:31 +03:00
sign_in(pos, node)
2020-01-31 21:55:10 +03:00
end,
on_rightclick = function(pos, node, clicker)
switch_on(pos, node, clicker, "techage:powerswitchsmall_on")
end,
2021-07-19 21:37:31 +03:00
after_dig_node = sign_off,
2020-01-31 21:55:10 +03:00
on_rotate = screwdriver.disallow,
paramtype = "light",
use_texture_alpha = techage.CLIP,
2020-01-31 21:55:10 +03:00
sunlight_propagates = true,
paramtype2 = "wallmounted",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:powerswitchsmall_on", {
description = S("TA Power Switch Small"),
inventory_image = "techage_appl_switch_inv.png",
tiles = {
'techage_smart_button_on.png',
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -5/32, -16/32, -5/32, 5/32, -15/32, 5/32},
{ -2/16, -12/16, -2/16, 2/16, -8/16, 2/16},
},
},
2022-01-03 23:40:31 +03:00
2020-01-31 21:55:10 +03:00
on_rightclick = function(pos, node, clicker)
switch_off(pos, node, clicker, "techage:powerswitchsmall")
end,
2021-07-19 21:37:31 +03:00
after_dig_node = sign_off,
2020-01-31 21:55:10 +03:00
drop = "techage:powerswitchsmall",
on_rotate = screwdriver.disallow,
paramtype = "light",
use_texture_alpha = techage.CLIP,
2020-01-31 21:55:10 +03:00
sunlight_propagates = true,
paramtype2 = "wallmounted",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory = 1},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
2020-04-11 11:14:52 +03:00
techage.register_node({"techage:powerswitch", "techage:powerswitch_on",
"techage:powerswitchsmall", "techage:powerswitchsmall_on"}, {
2020-01-31 21:55:10 +03:00
on_recv_message = function(pos, src, topic, payload)
local node = techage.get_node_lvm(pos)
if topic == "on" and node.name == "techage:powerswitch" then
switch_on(pos, node, nil, "techage:powerswitch_on")
return true
elseif topic == "on" and node.name == "techage:powerswitchsmall" then
switch_on(pos, node, nil, "techage:powerswitchsmall_on")
return true
elseif topic == "off" and node.name == "techage:powerswitch_on" then
switch_off(pos, node, nil, "techage:powerswitch")
return true
elseif topic == "off" and node.name == "techage:powerswitchsmall_on" then
switch_off(pos, node, nil, "techage:powerswitchsmall")
return true
elseif topic == "state" then
2022-01-03 23:40:31 +03:00
if node.name == "techage:powerswitch_on" or
2020-01-31 21:55:10 +03:00
node.name == "techage:powerswitchsmall_on" then
return "on"
end
return "off"
else
return "unsupported"
end
end,
2022-06-06 21:39:55 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
local node = techage.get_node_lvm(pos)
if topic == 1 and payload[1] == 1 and node.name == "techage:powerswitch" then
switch_on(pos, node, nil, "techage:powerswitch_on")
return 0
elseif topic == 1 and payload[1] == 1 and node.name == "techage:powerswitchsmall" then
switch_on(pos, node, nil, "techage:powerswitchsmall_on")
return 0
elseif topic == 1 and payload[1] == 0 and node.name == "techage:powerswitch_on" then
switch_off(pos, node, nil, "techage:powerswitch")
return 0
elseif topic == 1 and payload[1] == 0 and node.name == "techage:powerswitchsmall_on" then
switch_off(pos, node, nil, "techage:powerswitchsmall")
return 0
else
return 2
end
end,
on_beduino_request_data = function(pos, src, topic, payload)
local node = techage.get_node_lvm(pos)
2023-09-16 22:03:46 +03:00
if topic == 131 or topic == 142 then
2022-06-06 21:39:55 +03:00
if node.name == "techage:powerswitch_on" or
node.name == "techage:powerswitchsmall_on" then
return 0, {1}
end
return 0, {0}
else
return 2, ""
end
end,
2022-01-03 23:40:31 +03:00
})
2020-01-31 21:55:10 +03:00
minetest.register_craft({
output = "techage:powerswitch 2",
recipe = {
{"", "", ""},
{"dye:yellow", "dye:red", "dye:yellow"},
{"basic_materials:plastic_sheet", "basic_materials:copper_wire", "basic_materials:plastic_sheet"},
},
})
minetest.register_craft({
type = "shapeless",
output = "techage:powerswitchsmall",
recipe = {"techage:powerswitch"},
})