Skip to content

Commit

Permalink
Extend the 'node.param2' support for all 24 possible values
Browse files Browse the repository at this point in the history
  • Loading branch information
joe7575 committed Jan 7, 2022
1 parent 4651811 commit fbe7d2f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 94 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ optional: intllib


## License
Copyright (C) 2017-2021 Joachim Stolberg
Copyright (C) 2017-2022 Joachim Stolberg
Code: Licensed under the GNU LGPL version 2.1 or later.
See LICENSE.txt and http://www.gnu.org/licenses/lgpl-2.1.txt
Textures: CC0
Expand Down Expand Up @@ -89,5 +89,6 @@ Textures: CC0
- 2020-05-31 v1.9 * Generator function 'get_tube_line' added, storage improvements
- 2021-01-23 v2.0 * Add functions for easy & fast 'valid side' checking (PR #8)
- 2021-05-24 v2.1 * Add API functions 'register_on_tube_update2'
- 2022-01-05 v2.2 * Extend the 'node.param2' support for all 24 possible values


20 changes: 10 additions & 10 deletions internal1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
See LICENSE.txt for more information
internal1.lua
First level functions behind the API
]]--
Expand Down Expand Up @@ -133,7 +133,7 @@ end
--------------------------------------------------------------------------------------

-- Pairing helper function. NOT USED (see internal2.lua)!!!
function Tube:store_teleport_data(pos, peer_pos)
function Tube:store_teleport_data(pos, peer_pos)
local meta = M(pos)
meta:set_string("tele_pos", S(peer_pos))
meta:set_string("channel", nil)
Expand Down Expand Up @@ -183,40 +183,40 @@ function Tube:update_after_place_tube(pos, placer, pointed_thing)
if self.valid_dirs[dir1] and self.valid_dirs[dir2] and tValidNum[num_tubes] then
self.clbk_after_place_tube(self:get_tube_data(pos, dir1, dir2, num_tubes))
end

if num_tubes >= 1 then
local npos, d1, d2, num = self:add_tube_dir(pos, dir1)
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and num < 2 then
self.clbk_after_place_tube(self:get_tube_data(npos, d1, d2, num+1))
end
end

if num_tubes >= 2 then
local npos, d1, d2, num = self:add_tube_dir(pos, dir2)
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and num < 2 then
self.clbk_after_place_tube(self:get_tube_data(npos, d1, d2, num+1))
end
end
return true, dir1, dir2, num_tubes
end
end

function Tube:update_after_dig_tube(pos, param2)
local dir1, dir2 = self:decode_param2(pos, param2)

local npos, d1, d2, num = self:del_tube_dir(pos, dir1)
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num] then
self.clbk_after_place_tube(self:get_tube_data(npos, d1, d2, num))
else
dir1 = nil
end

npos, d1, d2, num = self:del_tube_dir(pos, dir2)
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num] then
self.clbk_after_place_tube(self:get_tube_data(npos, d1, d2, num))
else
dir2 = nil
end

return dir1, dir2
end

Expand All @@ -229,7 +229,7 @@ function Tube:replace_nodes(pos1, pos2, dir1, dir2)
pos = get_pos(pos, dir1)
end
self.clbk_after_place_tube(self:get_tube_data(pos2, dir1, dir2, 1))
end
end

function Tube:switch_nodes(pos, dir, state)
pos = get_pos(pos, dir)
Expand Down
40 changes: 20 additions & 20 deletions internal2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Tube:get_node_lvm(pos)
end

-- Read param2 from a primary node at the given position.
-- If dir == nil then node_pos = pos
-- If dir == nil then node_pos = pos
-- Function returns param2, new_pos or nil
function Tube:get_primary_node_param2(pos, dir)
local npos = vector.add(pos, Dir6dToVector[dir or 0])
Expand All @@ -104,7 +104,7 @@ function Tube:get_primary_node_param2(pos, dir)
end

-- Check if node at given position is a tube node
-- If dir == nil then node_pos = pos
-- If dir == nil then node_pos = pos
-- Function returns true/false
function Tube:is_primary_node(pos, dir)
local npos = vector.add(pos, Dir6dToVector[dir or 0])
Expand All @@ -113,7 +113,7 @@ function Tube:is_primary_node(pos, dir)
end

-- Get secondary node at given position
-- If dir == nil then node_pos = pos
-- If dir == nil then node_pos = pos
-- Function returns node and new_pos or nil
function Tube:get_secondary_node(pos, dir)
local npos = vector.add(pos, Dir6dToVector[dir or 0])
Expand All @@ -124,7 +124,7 @@ function Tube:get_secondary_node(pos, dir)
end

-- Get special registered nodes at given position
-- If dir == nil then node_pos = pos
-- 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])
Expand All @@ -135,7 +135,7 @@ function Tube:get_special_node(pos, dir)
end

-- Check if node at given position is a secondary node
-- If dir == nil then node_pos = pos
-- If dir == nil then node_pos = pos
-- Function returns true/false
function Tube:is_secondary_node(pos, dir)
local npos = vector.add(pos, Dir6dToVector[dir or 0])
Expand All @@ -144,7 +144,7 @@ function Tube:is_secondary_node(pos, dir)
end

-- Check if node at given position is a special node
-- If dir == nil then node_pos = pos
-- 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])
Expand Down Expand Up @@ -216,7 +216,7 @@ end
function Tube:get_tube_data(pos, dir1, dir2, num_tubes, state)
local param2, tube_type = self:encode_param2(dir1, dir2, num_tubes)
return pos, param2, tube_type, num_tubes, state
end
end

-- Return pos for a primary_node and true if num_conn < 2, else false
function Tube:friendly_primary_node(pos, dir)
Expand All @@ -238,12 +238,12 @@ function Tube:vector_to_dir(v)
end
end

-- Check all 6 possible positions for known nodes considering preferred_pos
-- Check all 6 possible positions for known nodes considering preferred_pos
-- and the players fdir and return dir1, dir2 and the number of tubes to connect to (0..2).
function Tube:determine_tube_dirs(pos, preferred_pos, fdir)
local tbl = {}
local allowed = table.copy(self.valid_dirs)

-- If the node at players "prefered position" is a tube,
-- then the other side of the new tube shall point to the player.
if preferred_pos then
Expand Down Expand Up @@ -282,7 +282,7 @@ function Tube:determine_tube_dirs(pos, preferred_pos, fdir)
elseif #tbl >= 2 then
return tbl[1], tbl[2], 2
end

-- Check for secondary nodes (chests and so on)
for dir = 1,6 do
if allowed[dir] then
Expand All @@ -301,16 +301,16 @@ function Tube:determine_tube_dirs(pos, preferred_pos, fdir)
end
end
end
-- player pointed to an unknown node to force the tube orientation?

-- player pointed to an unknown node to force the tube orientation?
if preferred_pos and fdir then
if tbl[1] == Turn180Deg[fdir] and allowed[fdir] then
tbl[2] = fdir
elseif allowed[Turn180Deg[fdir]] then
tbl[2] = Turn180Deg[fdir]
end
end

-- dir1, dir2 still unknown?
if fdir then
if #tbl == 0 and allowed[Turn180Deg[fdir]] then
Expand All @@ -324,14 +324,14 @@ function Tube:determine_tube_dirs(pos, preferred_pos, fdir)
end

if #tbl >= 2 and tbl[1] ~= tbl[2] then
local num_tubes = (self:connected(pos, tbl[1]) and 1 or 0) +
local num_tubes = (self:connected(pos, tbl[1]) and 1 or 0) +
(self:connected(pos, tbl[2]) and 1 or 0)
return tbl[1], tbl[2], math.min(2, num_tubes)
end
end

-- Determine a tube side without connection, increment the number of connections
-- and return the new data to be able to update the node:
-- and return the new data to be able to update the node:
-- new_pos, dir1, dir2, num_connections (1, 2)
function Tube:add_tube_dir(pos, dir)
local param2, npos = self:get_primary_node_param2(pos, dir)
Expand Down Expand Up @@ -359,7 +359,7 @@ function Tube:add_tube_dir(pos, dir)
end

-- Decrement the number of tube connections
-- and return the new data to be able to update the node:
-- and return the new data to be able to update the node:
-- new_pos, dir1, dir2, num_connections (0, 1)
function Tube:del_tube_dir(pos, dir)
local param2, npos = self:get_primary_node_param2(pos, dir)
Expand All @@ -372,9 +372,9 @@ function Tube:del_tube_dir(pos, dir)
end
end
end

-- Pairing helper function
function Tube:store_teleport_data(pos, peer_pos)
function Tube:store_teleport_data(pos, peer_pos)
local meta = M(pos)
meta:set_string("tele_pos", S(peer_pos))
meta:set_string("channel", nil)
Expand Down Expand Up @@ -409,7 +409,7 @@ function Tube:dbg_out()
end
end
end

-- Walk to the end of the tube line and return pos and outdir of both head tube nodes.
-- If no tube is available, return nil
function Tube:walk_tube_line(pos, dir)
Expand All @@ -429,4 +429,4 @@ function Tube:walk_tube_line(pos, dir)
end
end
return table.copy(pos), dir, 0
end
end
10 changes: 5 additions & 5 deletions storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ local function update_mod_storage()
storage:set_string(k, minetest.serialize(v))
MemStore[k] = nil -- remove from memory
end
end
end
-- run every 10 minutes
minetest.after(600, update_mod_storage)
end

minetest.register_on_shutdown(function()
for k,v in pairs(MemStore) do
storage:set_string(k, minetest.serialize(v))
end
end
end)

minetest.after(600, update_mod_storage)
Expand All @@ -52,14 +52,14 @@ local function empty_block(block)
end
return empty
end

minetest.after(1, function()
local tbl = storage:to_table()
for k,v in pairs(tbl.fields) do
if empty_block(v) then
storage:set_string(k, "")
end
end
end
end)

--
Expand All @@ -81,7 +81,7 @@ local function new_node(block, node_key)
end

local function unlock(pos)
local block_key = math.floor((pos.z+32768)/16)*4096*4096 +
local block_key = math.floor((pos.z+32768)/16)*4096*4096 +
math.floor((pos.y+32768)/16)*4096 + math.floor((pos.x+32768)/16)
local node_key = (pos.z%16)*16*16 + (pos.y%16)*16 + (pos.x%16)
local block = MemStore[block_key] or new_block(block_key)
Expand Down
Loading

0 comments on commit fbe7d2f

Please sign in to comment.