Prepare for lib 'networks'

This commit is contained in:
Joachim Stolberg 2021-06-06 15:53:19 +02:00
parent 3dd6c80f18
commit bb155e464e
2 changed files with 31 additions and 37 deletions

View File

@ -29,40 +29,6 @@ local Param2ToDir = {
[5] = 3,
}
local function get_param2(meta)
if meta:get_int("tl2_param2") then
meta:set_int("netw_param2", meta:get_int("tl2_param2"))
meta:set_string("tl2_param2", "")
end
if meta:get_int("tl2_param2_copy") then
meta:set_int("netw_param2_copy", meta:get_int("tl2_param2_copy"))
meta:set_string("tl2_param2_copy", "")
end
return meta:get_int("netw_param2")
end
-- The switchbox can be open or hidden
local function legacy_switchbox(pos)
local meta = M(pos)
local name = networks.hidden_name(pos)
local node = techage.get_node_lvm(pos)
if name == "techage:powerswitch_box" then
local param2 = get_param2(meta)
if param2 == 0 then
meta:set_string("netw_name", "techage:powerswitch_box_off")
else
meta:set_string("netw_node", "techage:powerswitch_box_on")
end
elseif node.name == "techage:powerswitch_box" then
local param2 = get_param2(meta)
if param2 == 0 then
minetest.swap_node(pos, {name = "techage:powerswitch_box_off", param2 = node.param2})
else
minetest.swap_node(pos, {name = "techage:powerswitch_box_on", param2 = node.param2})
end
end
end
local function switch_on(pos, node, clicker, name)
if clicker and minetest.is_protected(pos, clicker:get_player_name()) then
return
@ -77,7 +43,7 @@ local function switch_on(pos, node, clicker, name)
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
legacy_switchbox(pos2)
techage.legacy_switches(pos2)
power.turn_switch_on(pos2, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on")
end
@ -96,7 +62,7 @@ local function switch_off(pos, node, clicker, name)
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
legacy_switchbox(pos2)
techage.legacy_switches(pos2)
power.turn_switch_off(pos2, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on")
end

View File

@ -15,7 +15,7 @@
local S2P = minetest.string_to_pos
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local M = minetest.get_meta
local N = function(pos) return minetest.get_node(pos).name end
local N = techage.get_node_lvm
local S = techage.S
local Cable = techage.ElectricCable
@ -28,6 +28,32 @@ local node_box = {
},
}
function techage.legacy_switches(pos)
local meta = M(pos)
local node = N(pos)
if node.name == "techage:powerswitch_box" then
if meta:get_int("netw_param2") == 0 then
node.name = "techage:powerswitch_box_off"
else
node.name = "techage:powerswitch_box_on"
end
minetest.swap_node(pos, node)
elseif meta:get_string("netw_name") == "techage:powerswitch_box" then
if meta:get_int("netw_param2") == 0 then
meta:set_string("netw_name", "techage:powerswitch_box_off")
else
meta:set_string("netw_name", "techage:powerswitch_box_on")
end
end
if meta:contains("tl2_param2_copy") then
meta:set_string("netw_param2_copy", meta:get_string("tl2_param2_copy"))
meta:set_string("tl2_param2_copy", "")
end
end
-- The on-switch is a "primary node" like cables
minetest.register_node("techage:powerswitch_box_on", {
description = S("TA Power Switch Box"),
@ -50,6 +76,7 @@ minetest.register_node("techage:powerswitch_box_on", {
return false
end,
on_rightclick = function(pos, node, clicker)
techage.legacy_switches(pos)
if power.turn_switch_off(pos, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on") then
minetest.sound_play("doors_glass_door_open", {
pos = pos,
@ -84,6 +111,7 @@ minetest.register_node("techage:powerswitch_box_off", {
"techage_electric_junction.png",
},
on_rightclick = function(pos, node, clicker)
techage.legacy_switches(pos)
if power.turn_switch_on(pos, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on") then
minetest.sound_play("doors_glass_door_open", {
pos = pos,