This commit is contained in:
Joachim Stolberg 2018-12-10 20:11:46 +01:00
parent bbff8f8b53
commit 78ea6da8c0
3 changed files with 33 additions and 22 deletions

@ -173,17 +173,17 @@ function Tube:repair_tube_line(pos, dir)
end
-- fpos,fdir points to the secondary node to be updated.
-- npos,ndir are used to calculate the connection data to be written.
-- npos,ndir are used to calculate the connection data to be stored.
function Tube:update_secondary_node(fpos,fdir, npos,ndir)
-- [s]<-[n]----[f]->[s]
print("update_secondary_node", S(fpos), fdir)
local fpos2, node = self:get_node(fpos, fdir)
if self.secondary_node_names[node.name] and
minetest.registered_nodes[node.name].tubelib2_on_update then
if self.secondary_node_names[node.name] and self.clbk_update_secondary_node then
local npos2 = self:get_pos(npos, ndir)
if vector.equals(npos2, fpos2) then -- last tube removed?
npos2,ndir = nil,nil -- used to delete the data base
end
minetest.registered_nodes[node.name].tubelib2_on_update(fpos2, Turn180Deg[fdir], npos2, ndir)
self.clbk_update_secondary_node(node, fpos2, Turn180Deg[fdir], npos2, ndir)
end
end

@ -36,6 +36,8 @@ end
-- Tubelib2 Class
tubelib2.Tube = {}
local Tube = tubelib2.Tube
local Turn180Deg = tubelib2.Turn180Deg
--
-- API Functions
@ -64,6 +66,12 @@ function Tube:add_secondary_node_names(names)
end
end
-- Called for each connected node when the tube connection has been changed.
-- func(node, pos, out_dir, peer_pos, peer_in_dir)
function Tube:register_on_tube_update(update_secondary_node)
self.clbk_update_secondary_node = update_secondary_node
end
-- To be called after a secondary node is placed.
-- dirs is a list with valid dirs, like: {1,2,3,4}
function Tube:after_place_node(pos, dirs)
@ -106,6 +114,7 @@ function Tube:after_dig_node(pos, dirs)
end
end
-- To be called after a tube/primary node is removed.
function Tube:after_dig_tube(pos, oldnode, oldmetadata)
-- [s1][f1]----[n1] x [n2]-----[f2][s2]
-- s..secondary, f..far, n..near, x..node to be removed
@ -130,9 +139,9 @@ function Tube:get_connected_node_pos(pos, dir)
-- only one tube with wrong dir info?
if vector.equals(pos, npos) then
fpos,fdir = self:del_meta(pos, dir)
return self:get_pos(fpos,fdir)
npos,ndir = self:get_pos(fpos,fdir)
end
return npos, dir
return npos, fdir
end

@ -27,7 +27,7 @@ local Tube = tubelib2.Tube:new({
-- dirs_to_check = {5,6}, -- vertical only
dirs_to_check = {1,2,3,4,5,6},
max_tube_length = 1000,
show_infotext = false,
show_infotext = true,
primary_node_names = {"tubelib2:tubeS", "tubelib2:tubeA"},
secondary_node_names = {"default:chest", "default:chest_open",
"tubelib2:source", "tubelib2:junction", "tubelib2:teleporter"},
@ -36,6 +36,19 @@ local Tube = tubelib2.Tube:new({
end,
})
Tube:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir)
local sdir = tubelib2.dir_to_string(out_dir)
if not peer_pos then
print(S(pos).." to the "..sdir..": Not connected")
elseif Tube:secondary_node(peer_pos) then
local node = minetest.get_node(peer_pos)
print(S(pos).." to the "..sdir..": Connected with "..node.name)
else
print(S(pos).." to the "..sdir..": Connected with "..S(peer_pos))
end
end)
minetest.register_node("tubelib2:tubeS", {
description = "Tubelib2 Test tube",
tiles = { -- Top, base, right, left, front, back
@ -48,12 +61,12 @@ minetest.register_node("tubelib2:tubeS", {
},
after_place_node = function(pos, placer, itemstack, pointed_thing)
local t = minetest.get_us_time()
--local t = minetest.get_us_time()
if not Tube:after_place_tube(pos, placer, pointed_thing) then
minetest.remove_node(pos)
return true
end
print("place time", minetest.get_us_time() - t)
--print("place time", minetest.get_us_time() - t)
return false
end,
@ -185,18 +198,6 @@ minetest.register_node("tubelib2:junction", {
Tube:after_dig_node(pos)
end,
tubelib2_on_update = function(pos, out_dir, peer_pos, peer_in_dir)
local sdir = tubelib2.dir_to_string(out_dir)
if not peer_pos then
print(S(pos).." to the "..sdir..": Not connected")
elseif Tube:secondary_node(peer_pos) then
local node = minetest.get_node(peer_pos)
print(S(pos).." to the "..sdir..": Connected with "..node.name)
else
print(S(pos).." to the "..sdir..": Connected with "..S(peer_pos))
end
end,
paramtype2 = "facedir", -- important!
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
@ -294,8 +295,9 @@ local function remove_tube(itemstack, placer, pointed_thing)
Tube:tool_remove_tube(pos, "default_break_glass")
end
else
local dir = (minetest.dir_to_facedir(placer:get_look_dir()) % 4) + 1
minetest.chat_send_player(placer:get_player_name(),
"[Tool Help]\n"..
"[Tool Help] dir="..dir.."\n"..
" left: remove node\n"..
" right: repair tube line\n")
end