From e3070452686f3247b3a7402ca8c7d62695915c31 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Sun, 9 Feb 2020 10:55:39 +0100 Subject: [PATCH] v1.8 * 'special nodes' as alternative to 'secondary nodes' introduced --- README.md | 1 + internal1.lua | 3 +-- internal2.lua | 20 ++++++++++++++++++++ tube_api.lua | 40 +++++++++++++++++++++++++++++----------- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index bf819ed..378abd7 100644 --- a/README.md +++ b/README.md @@ -79,5 +79,6 @@ Textures: CC0 - 2019-07-12 v1.5 * internal handling of secondary nodes changed - 2019-10-25 v1.6 * callback 'tubelib2_on_update2' added - 2020-01-03 v1.7 * max_tube_length bugfix +- 2020-02-02 v1.8 * 'special nodes' as alternative to 'secondary nodes' introduced diff --git a/internal1.lua b/internal1.lua index 4abc557..43e5e78 100644 --- a/internal1.lua +++ b/internal1.lua @@ -101,8 +101,7 @@ function Tube:update_secondary_node(pos1, dir1, pos2, dir2) ndef.tubelib2_on_update2(pos1, dir1, self, node) elseif ndef.tubelib2_on_update then ndef.tubelib2_on_update(node, pos1, dir1, pos2, Turn180Deg[dir2]) - end - if self.clbk_update_secondary_node then + elseif self.clbk_update_secondary_node then self.clbk_update_secondary_node(node, pos1, dir1, pos2, Turn180Deg[dir2]) end end diff --git a/internal2.lua b/internal2.lua index ebb735d..2a02031 100644 --- a/internal2.lua +++ b/internal2.lua @@ -123,6 +123,17 @@ function Tube:get_secondary_node(pos, dir) end end +-- Get special registered nodes at given position +-- If dir == nil then node_pos = pos +-- Function returns node and new_pos or nil +function Tube:get_special_node(pos, dir) + local npos = vector.add(pos, Dir6dToVector[dir or 0]) + local node = self:get_node_lvm(npos) + if self.special_node_names[node.name] then + return node, npos + end +end + -- Check if node at given position is a secondary node -- If dir == nil then node_pos = pos -- Function returns true/false @@ -132,6 +143,15 @@ function Tube:is_secondary_node(pos, dir) return self.secondary_node_names[node.name] end +-- Check if node at given position is a special node +-- If dir == nil then node_pos = pos +-- Function returns true/false +function Tube:is_special_node(pos, dir) + local npos = vector.add(pos, Dir6dToVector[dir or 0]) + local node = self:get_node_lvm(npos) + return self.special_node_names[node.name] +end + -- Check if node has a connection on the given dir function Tube:connected(pos, dir) return self:is_primary_node(pos, dir) or self:is_secondary_node(pos, dir) diff --git a/tube_api.lua b/tube_api.lua index 7e4483c..59c0c09 100644 --- a/tube_api.lua +++ b/tube_api.lua @@ -13,7 +13,7 @@ ]]-- -- Version for compatibility checks, see readme.md/history -tubelib2.version = 1.7 +tubelib2.version = 1.8 -- for lazy programmers local S = function(pos) if pos then return minetest.pos_to_string(pos) end end @@ -113,10 +113,17 @@ local function update3(self, pos, dir1, dir2) end end -local function update4(self, pos, dirs) +local function update_secondary_nodes_after_node_placed(self, pos, dirs) dirs = dirs or self.dirs_to_check + -- check surrounding for secondary nodes for _,dir in ipairs(dirs) do - local _,npos = self:get_secondary_node(pos, dir) + local tmp, npos + if self.force_to_use_tubes then + tmp, npos = self:get_special_node(pos, dir) + print("tmp, npos", tmp, S(npos)) + else + tmp, npos = self:get_secondary_node(pos, dir) + end if npos then self:update_secondary_node(npos, Turn180Deg[dir], pos, dir) self:update_secondary_node(pos, dir, npos, Turn180Deg[dir]) @@ -124,10 +131,16 @@ local function update4(self, pos, dirs) end end -local function update5(self, pos, dirs) +local function update_secondary_nodes_after_node_dug(self, pos, dirs) dirs = dirs or self.dirs_to_check + -- check surrounding for secondary nodes for _,dir in ipairs(dirs) do - local _,npos = self:get_secondary_node(pos, dir) + local tmp, npos + if self.force_to_use_tubes then + tmp, npos = self:get_special_node(pos, dir) + else + tmp, npos = self:get_secondary_node(pos, dir) + end if npos then self:update_secondary_node(npos, Turn180Deg[dir]) self:update_secondary_node(pos, dir) @@ -151,6 +164,7 @@ function Tube:new(attr) tube_type = attr.tube_type or "unknown", pairingList = {}, -- teleporting nodes connCache = {}, -- connection cache {pos1 = {dir1 = {pos2 = pos2, dir2 = dir2},...} + special_node_names = {}, -- use add_special_node_names() to register nodes } o.valid_dirs = Tbl(o.dirs_to_check) setmetatable(o, self) @@ -165,6 +179,14 @@ function Tube:add_secondary_node_names(names) end end +-- Register further nodes, which should be updated after +-- a node/tube is placed/dug +function Tube:add_special_node_names(names) + for _,name in ipairs(names) do + self.special_node_names[name] = true + 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) @@ -185,9 +207,7 @@ function Tube:after_place_node(pos, dirs) update1(self, pos, dir) end end - if not self.force_to_use_tubes then - update4(self, pos, dirs) - end + update_secondary_nodes_after_node_placed(self, pos, dirs) end -- To be called after a tube/primary node is placed. @@ -208,9 +228,7 @@ function Tube:after_dig_node(pos, dirs) for _,dir in ipairs(self:update_after_dig_node(pos, dirs)) do update1(self, pos, dir) end - if not self.force_to_use_tubes then - update5(self, pos, dirs) - end + update_secondary_nodes_after_node_dug(self, pos, dirs) end -- To be called after a tube/primary node is removed.