Prepare for lib 'networks'

This commit is contained in:
Joachim Stolberg 2021-06-05 20:18:04 +02:00
parent 282f52d77e
commit 6699fea14c
7 changed files with 49 additions and 126 deletions

View File

@ -61,7 +61,7 @@ end
minetest.register_node("techage:sink", {
description = "Sink",
tiles = {'techage_electric_button.png'},
tiles = {'techage_electric_button.png^[colorize:#000000:50'},
on_timer = function(pos, elapsed)
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)

View File

@ -169,6 +169,14 @@ minetest.register_globalstep(function(dtime)
techage.SystemTime = techage.SystemTime + dtime
end)
-- used by TA1 hammer: dug_node[player_name] = pos
techage.dug_node = {}
minetest.register_on_dignode(function(pos, oldnode, digger)
if not digger then return end
-- store pos for tools without own 'register_on_dignode'
techage.dug_node[digger:get_player_name()] = pos
end)
-------------------------------------------------------------------
-- API helper functions
-------------------------------------------------------------------

View File

@ -193,7 +193,7 @@ dofile(MP.."/basic_machines/chest.lua")
--dofile(MP.."/furnace/recipes.lua")
-- Tools
--dofile(MP.."/tools/trowel.lua")
dofile(MP.."/tools/trowel.lua")
--dofile(MP.."/tools/repairkit.lua")
--dofile(MP.."/tools/pipe_wrench.lua")
--dofile(MP.."/basic_machines/blackhole.lua")

View File

@ -43,8 +43,10 @@ local names = networks.register_junction("techage:electric_junction", 2/8, Boxes
Cable:after_place_node(pos)
end,
tubelib2_on_update2 = function(pos, dir1, tlib2, node)
local name = "techage:electric_junction" .. networks.junction_type(pos, Cable)
minetest.swap_node(pos, {name = name, param2 = 0})
if not networks.hidden_name(pos) then
local name = "techage:electric_junction" .. networks.junction_type(pos, Cable)
minetest.swap_node(pos, {name = name, param2 = 0})
end
power.update_network(pos, 0, tlib2, node)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)

View File

@ -18,6 +18,7 @@ local M = minetest.get_meta
local S = techage.S
local Cable = techage.ElectricCable
local power = networks.power
local Param2ToDir = {
[0] = 6,
@ -28,9 +29,17 @@ local Param2ToDir = {
[5] = 3,
}
local function is_switchbox(pos)
return techage.get_node_lvm(pos).name == "techage:powerswitch_box" or
M(pos):get_string("techage_hidden_nodename") == "techage:powerswitch_box"
local function legacy_switchbox(pos)
local name = networks.hidden_name(pos)
if name == "techage:powerswitch_box" then
local meta = M(pos)
meta:set_int("networks_param2_copy", meta:get_int("tl2_param2_copy"))
if meta:get_int("networks_param2_copy") == 0 then
meta:set_string("networks_nodename", "techage:powerswitch_box_off")
else
meta:set_string("networks_nodename", "techage:powerswitch_box_on")
end
end
end
local function switch_on(pos, node, clicker, name)
@ -47,14 +56,8 @@ local function switch_on(pos, node, clicker, name)
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
if is_switchbox(pos2) then
if M(pos2):get_int("tl2_param2_copy") == 0 then
M(pos2):set_int("tl2_param2", techage.get_node_lvm(pos2).param2)
else
M(pos2):set_int("tl2_param2", M(pos2):get_int("tl2_param2_copy"))
end
Cable:after_place_tube(pos2, clicker)
end
legacy_switchbox(pos2)
power.turn_switch_on(pos2, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on")
end
local function switch_off(pos, node, clicker, name)
@ -72,13 +75,9 @@ local function switch_off(pos, node, clicker, name)
local dir = Param2ToDir[node.param2]
local pos2 = tubelib2.get_pos(pos, dir)
if is_switchbox(pos2) then
local node2 = techage.get_node_lvm(pos2)
node2.param2 = M(pos2):get_int("tl2_param2")
M(pos2):set_int("tl2_param2_copy", M(pos2):get_int("tl2_param2"))
M(pos2):set_int("tl2_param2", 0)
Cable:after_dig_tube(pos2, node2)
end
legacy_switchbox(pos2)
power.turn_switch_off(pos2, Cable, "techage:powerswitch_box_off", "techage:powerswitch_box_on")
end

View File

@ -73,9 +73,9 @@ minetest.register_lbm({
label = "[techage] legacy Power Switch Box",
name = "techage:powerswitch_box",
nodenames = {"techage:powerswitch_box"},
run_at_every_load = false,
run_at_every_load = true,
action = function(pos, node)
if M(pos):get_int("tl2_param2" == 0 then
if M(pos):get_int("tl2_param2") == 0 then
minetest.swap_node(pos, {name = "techage:powerswitch_box_off", param2 = node.param2})
M(pos):set_int("networks_param2", 0)
else

View File

@ -17,53 +17,6 @@ local P = minetest.string_to_pos
local M = minetest.get_meta
local S = techage.S
-- used by other tools: dug_node[player_name] = pos
techage.dug_node = {}
-- Determine if one node in the surrounding is a hidden tube/cable/pipe
local function other_hidden_nodes(pos, node_name)
return M({x=pos.x+1, y=pos.y, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x-1, y=pos.y, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y+1, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y-1, z=pos.z}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y, z=pos.z+1}):get_string(node_name) ~= "" or
M({x=pos.x, y=pos.y, z=pos.z-1}):get_string(node_name) ~= ""
end
local function hide_node(pos, node, meta, placer)
local inv = placer:get_inventory()
local stack = inv:get_stack("main", 1)
local taken = stack:take_item(1)
local ndef = minetest.registered_nodes[taken:get_name()]
-- test if it is a simple node without logic
if taken:get_count() == 1
and ndef
and not ndef.groups.soil
and not ndef.after_place_node
and not ndef.on_construct then
meta:set_string("techage_hidden_nodename", node.name)
meta:set_string("techage_hidden_param2", node.param2)
local param2 = 0
if ndef.paramtype2 and ndef.paramtype2 == "facedir" then
param2 = minetest.dir_to_facedir(placer:get_look_dir(), true)
end
minetest.swap_node(pos, {name = taken:get_name(), param2 = param2})
inv:set_stack("main", 1, stack)
end
end
local function open_node(pos, node, meta, placer)
local name = meta:get_string("techage_hidden_nodename")
local param2 = meta:get_string("techage_hidden_param2")
minetest.swap_node(pos, {name = name, param2 = param2})
meta:set_string("techage_hidden_nodename", "")
meta:set_string("techage_hidden_param2", "")
local inv = placer:get_inventory()
inv:add_item("main", ItemStack(node.name))
end
-- Hide or open a node
local function replace_node(itemstack, placer, pointed_thing)
if pointed_thing.type == "node" then
local pos = pointed_thing.under
@ -71,17 +24,24 @@ local function replace_node(itemstack, placer, pointed_thing)
if minetest.is_protected(pos, name) then
return
end
local meta = M(pos)
local node = minetest.get_node(pos)
local node = minetest.get_node(pos)
local res = false
if minetest.get_item_group(node.name, "techage_trowel") == 1 then
hide_node(pos, node, meta, placer)
elseif meta:get_string("techage_hidden_nodename") ~= "" then
open_node(pos, node, meta, placer)
res = networks.hide_node(pos, node, placer)
elseif networks.hidden_name(pos) then
res = networks.open_node(pos, node, placer)
else
minetest.chat_send_player(placer:get_player_name(), "Invalid/unsuported block!")
return
end
if res then
minetest.sound_play("default_dig_snappy", {
pos = pos,
gain = 1,
max_hear_distance = 5})
elseif placer and placer.get_player_name then
minetest.chat_send_player(placer:get_player_name(), "Invalid fill material in inventory slot 1!")
end
minetest.sound_play("default_dig_snappy", {
pos = pos,
gain = 1,
max_hear_distance = 5})
end
end
@ -97,20 +57,6 @@ minetest.register_tool("techage:trowel", {
stack_max = 1,
})
minetest.register_on_dignode(function(pos, oldnode, digger)
if not digger then return end
-- If hidden nodes are arround, the removed one was probably
-- a hidden node, too.
if other_hidden_nodes(pos, "techage_hidden_nodename") then
-- test both hidden networks
techage.ElectricCable:after_dig_node(pos, oldnode, digger)
-- probably a hidden node with mem data
techage.del_mem(pos)
else
-- store pos for other tools without own 'register_on_dignode'
techage.dug_node[digger:get_player_name()] = pos
end
end)
minetest.register_craft({
output = "techage:trowel",
@ -120,35 +66,3 @@ minetest.register_craft({
{"", "", "default:stick"},
},
})
local function get_new_can_dig(old_can_dig)
return function(pos, player, ...)
if M(pos):get_string("techage_hidden_nodename") ~= "" then
if player and player.get_player_name then
minetest.chat_send_player(player:get_player_name(), S("Use a trowel to remove the node."))
end
return false
end
if old_can_dig then
return old_can_dig(pos, player, ...)
else
return true
end
end
end
-- Change can_dig for already registered nodes.
for _, ndef in pairs(minetest.registered_nodes) do
local old_can_dig = ndef.can_dig
minetest.override_item(ndef.name, {
can_dig = get_new_can_dig(old_can_dig)
})
end
-- Change can_dig for all nodes that are going to be registered in the future.
local old_register_node = minetest.register_node
minetest.register_node = function(name, def)
local old_can_dig = def.can_dig
def.can_dig = get_new_can_dig(old_can_dig)
return old_register_node(name, def)
end