2018-10-05 21:55:40 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Tube Library 2
|
|
|
|
==============
|
|
|
|
|
|
|
|
Copyright (C) 2018 Joachim Stolberg
|
|
|
|
|
|
|
|
LGPLv2.1+
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
internal.lua
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
-- for lazy programmers
|
2018-11-12 21:05:43 +01:00
|
|
|
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
2018-10-06 21:46:38 +02:00
|
|
|
local P = minetest.string_to_pos
|
2018-10-05 21:55:40 +02:00
|
|
|
local M = minetest.get_meta
|
|
|
|
|
2018-12-20 16:28:23 +01:00
|
|
|
-- Load support for intllib.
|
|
|
|
local MP = minetest.get_modpath("tubelib2")
|
|
|
|
local I,IS = dofile(MP.."/intllib.lua")
|
2018-10-05 21:55:40 +02:00
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
|
|
|
|
local Tube = {}
|
|
|
|
|
2018-10-14 21:28:31 +02:00
|
|
|
local Turn180Deg = {[0]=0,3,4,1,2,6,5}
|
2018-10-05 21:55:40 +02:00
|
|
|
|
|
|
|
-- To calculate param2 based on dir6d information
|
|
|
|
local DirToParam2 = {
|
|
|
|
-- dir1 / dir2 ==> param2 / type (Angled/Straight)
|
|
|
|
[12] = {11, "A"},
|
|
|
|
[13] = {12, "S"},
|
|
|
|
[14] = {14, "A"},
|
|
|
|
[15] = { 8, "A"},
|
|
|
|
[16] = {10, "A"},
|
|
|
|
[23] = { 7, "A"},
|
|
|
|
[24] = {21, "S"},
|
|
|
|
[25] = { 3, "A"},
|
|
|
|
[26] = {19, "A"},
|
|
|
|
[34] = { 5, "A"},
|
|
|
|
[35] = { 0, "A"},
|
|
|
|
[36] = {20, "A"},
|
|
|
|
[45] = {15, "A"},
|
|
|
|
[46] = {13, "A"},
|
|
|
|
[56] = { 4, "S"},
|
|
|
|
}
|
|
|
|
|
|
|
|
-- To retrieve dir6d values from the nodes param2
|
|
|
|
local Param2ToDir = {}
|
|
|
|
for k,item in pairs(DirToParam2) do
|
|
|
|
Param2ToDir[item[1]] = k
|
|
|
|
end
|
|
|
|
|
|
|
|
-- For neighbour position calculation
|
2018-10-14 21:28:31 +02:00
|
|
|
local Dir6dToVector = {[0] =
|
|
|
|
{x=0, y=0, z=0},
|
2018-10-05 21:55:40 +02:00
|
|
|
{x=0, y=0, z=1},
|
|
|
|
{x=1, y=0, z=0},
|
|
|
|
{x=0, y=0, z=-1},
|
|
|
|
{x=-1, y=0, z=0},
|
|
|
|
{x=0, y=-1, z=0},
|
|
|
|
{x=0, y=1, z=0},
|
|
|
|
}
|
|
|
|
|
|
|
|
local VectorToDir6d = {
|
|
|
|
[{x=0, y=0, z=1}] = 1,
|
|
|
|
[{x=1, y=0, z=0}] = 2,
|
|
|
|
[{x=0, y=0, z=-1}] = 3,
|
|
|
|
[{x=-1, y=0, z=0}] = 4,
|
|
|
|
[{x=0, y=-1, z=0}] = 5,
|
|
|
|
[{x=0, y=1, z=0}] = 6,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
tubelib2.Tube = Tube
|
|
|
|
tubelib2.Turn180Deg = Turn180Deg
|
|
|
|
tubelib2.DirToParam2 = DirToParam2
|
|
|
|
tubelib2.Param2ToDir = Param2ToDir
|
|
|
|
tubelib2.Dir6dToVector = Dir6dToVector
|
|
|
|
tubelib2.VectorToDir6d = VectorToDir6d
|
|
|
|
|
|
|
|
|
2018-10-05 21:55:40 +02:00
|
|
|
--
|
2019-02-09 11:04:42 +01:00
|
|
|
-- Tubelib2 Methods
|
2018-10-05 21:55:40 +02:00
|
|
|
--
|
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
function Tube:get_node_lvm(pos)
|
2018-10-05 21:55:40 +02:00
|
|
|
local node = minetest.get_node_or_nil(pos)
|
|
|
|
if node then
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
local vm = minetest.get_voxel_manip()
|
|
|
|
local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
|
|
|
|
local data = vm:get_data()
|
|
|
|
local param2_data = vm:get_param2_data()
|
|
|
|
local area = VoxelArea:new({MinEdge = MinEdge, MaxEdge = MaxEdge})
|
|
|
|
local idx = area:index(pos.x, pos.y, pos.z)
|
|
|
|
node = {
|
|
|
|
name = minetest.get_name_from_content_id(data[idx]),
|
|
|
|
param2 = param2_data[idx]
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
-- Read param2 from a primary node at the given position.
|
|
|
|
-- If dir == nil then node_pos = pos
|
|
|
|
-- Function returns param2, new_pos or nil
|
|
|
|
function Tube:get_primary_node_param2(pos, dir)
|
2018-12-16 18:53:19 +01:00
|
|
|
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
2019-02-09 11:04:42 +01:00
|
|
|
local node = self:get_node_lvm(npos)
|
2018-12-16 18:53:19 +01:00
|
|
|
if self.primary_node_names[node.name] then
|
2019-02-09 11:04:42 +01:00
|
|
|
return node.param2, npos
|
2018-11-09 23:37:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-16 18:53:19 +01:00
|
|
|
-- Check if node at given position is a tube node
|
|
|
|
-- If dir == nil then node_pos = pos
|
2019-02-09 11:04:42 +01:00
|
|
|
-- Function returns true/false
|
|
|
|
function Tube:is_primary_node(pos, dir)
|
|
|
|
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
|
|
|
local node = self:get_node_lvm(npos)
|
|
|
|
return self.primary_node_names[node.name]
|
2018-12-16 18:53:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if node at given position is a secondary node
|
|
|
|
-- If dir == nil then node_pos = pos
|
|
|
|
-- Function returns the new pos or nil
|
|
|
|
function Tube:secondary_node(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
|
|
|
local node = self:get_node_lvm(npos)
|
2018-12-16 18:53:19 +01:00
|
|
|
if self.secondary_node_names[node.name] then
|
|
|
|
return npos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-03 19:08:31 +01:00
|
|
|
-- Check if node has a connection on the given dir
|
|
|
|
function Tube:connected(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
return self:is_primary_node(pos, dir) or self:secondary_node(pos, dir)
|
2018-11-03 19:08:31 +01:00
|
|
|
end
|
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
function Tube:get_next_tube(pos, dir)
|
|
|
|
local param2, npos = self:get_primary_node_param2(pos, dir)
|
|
|
|
if param2 then
|
|
|
|
local val = Param2ToDir[param2 % 32]
|
|
|
|
local dir1, dir2 = math.floor(val / 10), val % 10
|
|
|
|
local num_conn = math.floor(param2 / 32)
|
|
|
|
if Turn180Deg[dir] == dir1 then
|
|
|
|
return npos, dir2, num_conn
|
|
|
|
else
|
|
|
|
return npos, dir1, num_conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return self:get_next_teleport_node(pos, dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return param2 and tube type ("A"/"S")
|
|
|
|
function Tube:encode_param2(dir1, dir2, num_conn)
|
|
|
|
if dir1 > dir2 then
|
|
|
|
dir1, dir2 = dir2, dir1
|
|
|
|
end
|
|
|
|
local param2, _type = unpack(DirToParam2[dir1 * 10 + dir2] or {0, "S"})
|
|
|
|
return (num_conn * 32) + param2, _type
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
-- Return dir1, dir2, num_conn
|
2018-11-03 19:08:31 +01:00
|
|
|
function Tube:decode_param2(pos, param2)
|
2018-10-05 21:55:40 +02:00
|
|
|
local val = Param2ToDir[param2 % 32]
|
|
|
|
if val then
|
|
|
|
local dir1, dir2 = math.floor(val / 10), val % 10
|
|
|
|
local num_conn = math.floor(param2 / 32)
|
|
|
|
return dir1, dir2, num_conn
|
|
|
|
end
|
2018-11-11 21:46:33 +01:00
|
|
|
end
|
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
function Tube:repair_tube(pos, dir)
|
2019-02-10 10:17:29 +01:00
|
|
|
local param2, npos = self:get_primary_node_param2(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
if param2 then
|
2019-02-10 10:17:29 +01:00
|
|
|
local dir1, dir2 = self:decode_param2(npos, param2)
|
2019-02-09 11:04:42 +01:00
|
|
|
local param2, tube_type = self:encode_param2(dir1, dir2, 2)
|
2019-02-10 10:17:29 +01:00
|
|
|
self.clbk_after_place_tube(npos, param2, tube_type, 2)
|
2019-02-09 11:04:42 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-14 21:28:31 +02:00
|
|
|
-- Return node next to pos in direction 'dir'
|
2018-11-09 23:37:23 +01:00
|
|
|
function Tube:get_node(pos, dir)
|
2018-10-14 21:28:31 +02:00
|
|
|
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
2019-02-09 11:04:42 +01:00
|
|
|
return npos, self:get_node_lvm(npos)
|
2018-10-14 21:28:31 +02:00
|
|
|
end
|
|
|
|
|
2019-02-09 11:04:42 +01:00
|
|
|
-- format and return given data as table
|
2019-03-02 19:27:51 +01:00
|
|
|
function Tube:get_tube_data(pos, dir1, dir2, num_tubes, state)
|
2019-02-09 11:04:42 +01:00
|
|
|
local param2, tube_type = self:encode_param2(dir1, dir2, num_tubes)
|
2019-03-02 19:27:51 +01:00
|
|
|
return pos, param2, tube_type, num_tubes, state
|
2019-02-09 11:04:42 +01:00
|
|
|
end
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
-- Return pos for a primary_node and true if num_conn < 2, else false
|
2018-10-05 21:55:40 +02:00
|
|
|
function Tube:friendly_primary_node(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
local param2, npos = self:get_primary_node_param2(pos, dir)
|
|
|
|
if param2 then
|
|
|
|
local _,_,num_conn = self:decode_param2(npos, param2)
|
2018-10-06 13:29:47 +02:00
|
|
|
-- tube node with max one connection?
|
2018-10-15 20:05:41 +02:00
|
|
|
return npos, (num_conn or 2) < 2
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 19:27:51 +01:00
|
|
|
function Tube:vector_to_dir(v)
|
|
|
|
if v.y > 0 then
|
|
|
|
return 6
|
|
|
|
elseif v.y < 0 then
|
|
|
|
return 5
|
|
|
|
else
|
|
|
|
return minetest.dir_to_facedir(v) + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-05 21:55:40 +02:00
|
|
|
-- 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 = {}
|
2018-11-11 18:47:06 +01:00
|
|
|
local allowed = table.copy(self.valid_dirs)
|
2018-10-05 21:55:40 +02:00
|
|
|
|
2019-01-09 20:11:40 +01:00
|
|
|
-- If the node at players "prefered position" is a tube,
|
|
|
|
-- then the other side of the new tube shall point to the player.
|
2019-02-14 21:54:58 +01:00
|
|
|
if preferred_pos then
|
|
|
|
local _, friendly = self:friendly_primary_node(preferred_pos)
|
|
|
|
if friendly then
|
|
|
|
local v = vector.direction(pos, preferred_pos)
|
2019-03-02 19:27:51 +01:00
|
|
|
local dir1 = self:vector_to_dir(v)
|
2019-02-14 21:54:58 +01:00
|
|
|
local dir2 = Turn180Deg[fdir]
|
|
|
|
return dir1, dir2, 1
|
2019-01-11 21:52:21 +01:00
|
|
|
end
|
2019-01-09 20:11:40 +01:00
|
|
|
end
|
2018-10-05 21:55:40 +02:00
|
|
|
-- Check for primary nodes (tubes)
|
|
|
|
for dir = 1,6 do
|
|
|
|
if allowed[dir] then
|
|
|
|
local npos, friendly = self:friendly_primary_node(pos, dir)
|
|
|
|
if npos then
|
|
|
|
if not friendly then
|
|
|
|
allowed[dir] = false
|
|
|
|
else
|
|
|
|
if preferred_pos and vector.equals(npos, preferred_pos) then
|
|
|
|
preferred_pos = nil
|
|
|
|
table.insert(tbl, 1, dir)
|
|
|
|
else
|
|
|
|
table.insert(tbl, dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If no tube around the pointed pos and player prefers a position,
|
|
|
|
-- then the new tube shall point to the player.
|
|
|
|
if #tbl == 0 and preferred_pos and fdir and allowed[Turn180Deg[fdir]] then
|
|
|
|
tbl[1] = Turn180Deg[fdir]
|
|
|
|
-- Already 2 dirs found?
|
|
|
|
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
|
|
|
|
local npos = self:secondary_node(pos, dir)
|
|
|
|
if npos then
|
|
|
|
if preferred_pos and vector.equals(npos, preferred_pos) then
|
|
|
|
preferred_pos = nil
|
|
|
|
table.insert(tbl, 2, dir)
|
|
|
|
else
|
|
|
|
table.insert(tbl, dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
tbl[1] = Turn180Deg[fdir]
|
|
|
|
end
|
|
|
|
if #tbl == 1 and allowed[Turn180Deg[tbl[1]]] then
|
|
|
|
tbl[2] = Turn180Deg[tbl[1]]
|
|
|
|
elseif #tbl == 1 and tbl[1] ~= Turn180Deg[fdir] and allowed[Turn180Deg[fdir]] then
|
|
|
|
tbl[2] = Turn180Deg[fdir]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #tbl >= 2 and tbl[1] ~= tbl[2] then
|
|
|
|
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
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
-- Determine a tube side without connection, increment the number of connections
|
|
|
|
-- and return the new data to be able to update the node:
|
|
|
|
-- new_pos, dir1, dir2, num_connections (1, 2)
|
2018-10-05 21:55:40 +02:00
|
|
|
function Tube:add_tube_dir(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
local param2, npos = self:get_primary_node_param2(pos, dir)
|
|
|
|
if param2 then
|
|
|
|
local d1, d2, num = self:decode_param2(npos, param2)
|
2018-12-16 21:31:49 +01:00
|
|
|
if not num then return end
|
2018-10-05 21:55:40 +02:00
|
|
|
-- not already connected to the new tube?
|
|
|
|
dir = Turn180Deg[dir]
|
|
|
|
if d1 ~= dir and dir ~= d2 then
|
|
|
|
if num == 0 then
|
|
|
|
d1 = dir
|
|
|
|
elseif num == 1 then
|
|
|
|
-- determine, which of d1, d2 has already a connection
|
|
|
|
if self:connected(npos, d1) then
|
|
|
|
d2 = dir
|
|
|
|
else
|
|
|
|
d1 = dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-03 20:12:38 +01:00
|
|
|
return npos, d1, d2, num
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
-- Decrement the number of tube connections
|
|
|
|
-- and return the new data to be able to update the node:
|
|
|
|
-- new_pos, dir1, dir2, num_connections (0, 1)
|
2018-10-05 21:55:40 +02:00
|
|
|
function Tube:del_tube_dir(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
local param2, npos = self:get_primary_node_param2(pos, dir)
|
|
|
|
if param2 then
|
|
|
|
local d1, d2, num = self:decode_param2(npos, param2)
|
2019-01-09 20:11:40 +01:00
|
|
|
-- check if node is connected to the given pos
|
|
|
|
dir = Turn180Deg[dir]
|
|
|
|
if d1 == dir or dir == d2 then
|
|
|
|
return npos, d1, d2, math.max((num or 1) - 1, 0)
|
|
|
|
end
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-06 21:46:38 +02:00
|
|
|
-- Pairing helper function
|
|
|
|
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)
|
|
|
|
meta:set_string("formspec", nil)
|
2018-12-20 16:28:23 +01:00
|
|
|
meta:set_string("infotext", I("Connected with ")..S(peer_pos))
|
2018-10-14 21:28:31 +02:00
|
|
|
return meta:get_int("tube_dir")
|
|
|
|
end
|
|
|
|
|
2018-11-09 23:37:23 +01:00
|
|
|
-- Jump over the teleport nodes to the next tube node
|
|
|
|
function Tube:get_next_teleport_node(pos, dir)
|
2019-02-09 11:04:42 +01:00
|
|
|
if pos then
|
|
|
|
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
|
|
|
local meta = M(npos)
|
|
|
|
local s = meta:get_string("tele_pos")
|
|
|
|
if s ~= "" then
|
|
|
|
local tele_pos = P(s)
|
|
|
|
local tube_dir = M(tele_pos):get_int("tube_dir")
|
|
|
|
if tube_dir ~= 0 then
|
|
|
|
return tele_pos, tube_dir
|
|
|
|
end
|
2018-11-09 23:37:23 +01:00
|
|
|
end
|
2018-10-20 19:19:44 +02:00
|
|
|
end
|
2018-10-06 21:46:38 +02:00
|
|
|
end
|
2018-10-14 21:28:31 +02:00
|
|
|
|
2018-12-16 18:53:19 +01:00
|
|
|
function Tube:dbg_out()
|
|
|
|
for pos1,item1 in pairs(self.connCache) do
|
|
|
|
for dir1,item2 in pairs(item1) do
|
|
|
|
print("pos1="..pos1..", dir1="..dir1..", pos2="..S(item2.pos2)..", dir2="..item2.dir2)
|
|
|
|
end
|
2018-10-14 21:28:31 +02:00
|
|
|
end
|
2018-11-09 23:37:23 +01:00
|
|
|
end
|
2018-12-16 18:53:19 +01:00
|
|
|
|
|
|
|
-- 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)
|
|
|
|
local cnt = 0
|
|
|
|
if dir then
|
|
|
|
while cnt <= self.max_tube_length do
|
2019-02-09 11:04:42 +01:00
|
|
|
local new_pos, new_dir, num = self:get_next_tube(pos, dir)
|
|
|
|
if not new_pos then break end
|
|
|
|
if cnt > 0 and num ~= 2 and self:is_primary_node(new_pos, new_dir) then
|
|
|
|
self:repair_tube(new_pos, new_dir)
|
2018-12-16 18:53:19 +01:00
|
|
|
end
|
|
|
|
pos, dir = new_pos, new_dir
|
|
|
|
cnt = cnt + 1
|
|
|
|
end
|
2018-12-20 16:28:23 +01:00
|
|
|
if cnt > self.max_tube_length then
|
|
|
|
return
|
|
|
|
end
|
2018-12-16 18:53:19 +01:00
|
|
|
if cnt > 0 then
|
|
|
|
return pos, dir, cnt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return table.copy(pos), dir, 0
|
|
|
|
end
|