2018-10-05 21:55:40 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Tube Library 2
|
|
|
|
==============
|
|
|
|
|
|
|
|
Copyright (C) 2017-2018 Joachim Stolberg
|
|
|
|
|
|
|
|
LGPLv2.1+
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
tube_api.lua
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2018-10-27 17:53:07 +02:00
|
|
|
-- Version for compatibility checks, see readme.md/history
|
2018-11-09 23:37:23 +01:00
|
|
|
tubelib2.version = 0.4
|
2018-10-27 17:53:07 +02:00
|
|
|
|
2018-10-05 21:55:40 +02:00
|
|
|
-- 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-05 21:55:40 +02:00
|
|
|
local M = minetest.get_meta
|
|
|
|
|
2018-10-27 17:53:07 +02:00
|
|
|
local Dir2Str = {"north", "east", "south", "west", "down", "up"}
|
|
|
|
|
|
|
|
function tubelib2.dir_to_string(dir)
|
|
|
|
return Dir2Str[dir]
|
|
|
|
end
|
2018-10-05 21:55:40 +02:00
|
|
|
|
|
|
|
local function Tbl(list)
|
|
|
|
local tbl = {}
|
|
|
|
for _,item in ipairs(list) do
|
|
|
|
tbl[item] = true
|
|
|
|
end
|
|
|
|
return tbl
|
|
|
|
end
|
|
|
|
|
2018-11-09 23:37:23 +01:00
|
|
|
-- Tubelib2 Class
|
|
|
|
tubelib2.Tube = {}
|
|
|
|
local Tube = tubelib2.Tube
|
2018-10-28 18:45:07 +01:00
|
|
|
|
2018-10-05 21:55:40 +02:00
|
|
|
--
|
|
|
|
-- API Functions
|
|
|
|
--
|
|
|
|
|
|
|
|
function Tube:new(attr)
|
|
|
|
local o = {
|
2018-11-09 23:37:23 +01:00
|
|
|
dirs_to_check = attr.dirs_to_check or {1,2,3,4,5,6},
|
2018-10-05 21:55:40 +02:00
|
|
|
max_tube_length = attr.max_tube_length or 1000,
|
|
|
|
primary_node_names = Tbl(attr.primary_node_names or {}),
|
|
|
|
secondary_node_names = Tbl(attr.secondary_node_names or {}),
|
|
|
|
show_infotext = attr.show_infotext or false,
|
2018-10-15 20:05:41 +02:00
|
|
|
clbk_after_place_tube = attr.after_place_tube,
|
2018-10-06 21:46:38 +02:00
|
|
|
pairingList = {}, -- teleporting nodes
|
2018-10-05 21:55:40 +02:00
|
|
|
}
|
2018-11-11 18:47:06 +01:00
|
|
|
o.valid_dirs = Tbl(o.dirs_to_check)
|
2018-10-05 21:55:40 +02:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
-- Register (foreign) tubelib compatible nodes.
|
2018-10-05 21:55:40 +02:00
|
|
|
function Tube:add_secondary_node_names(names)
|
|
|
|
for _,name in ipairs(names) do
|
|
|
|
self.secondary_node_names[name] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-27 17:53:07 +02:00
|
|
|
-- To be called after a secondary node is placed.
|
2018-11-09 23:37:23 +01:00
|
|
|
-- dirs is a list with valid dirs, like: {1,2,3,4}
|
|
|
|
function Tube:after_place_node(pos, dirs)
|
|
|
|
-- [s][f]----[n] x
|
|
|
|
-- s..secondary, f..far, n..near, x..node to be placed
|
|
|
|
for _,dir in ipairs(self:update_after_place_node(pos, dirs)) do
|
2018-11-13 21:09:05 +01:00
|
|
|
local fpos,fdir = self:repair_meta(pos, dir)
|
2018-11-09 23:37:23 +01:00
|
|
|
local npos, ndir = self:get_pos(pos, dir)
|
|
|
|
self:update_secondary_node(fpos,fdir, npos,ndir)
|
|
|
|
self:update_secondary_node(npos,ndir, fpos,fdir)
|
2018-10-14 21:28:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-27 17:53:07 +02:00
|
|
|
-- To be called after a tube/primary node is placed.
|
2018-11-11 21:46:33 +01:00
|
|
|
-- Returns false, if placing is not allowed
|
2018-10-15 20:05:41 +02:00
|
|
|
function Tube:after_place_tube(pos, placer, pointed_thing)
|
2018-11-09 23:37:23 +01:00
|
|
|
-- [s1][f1]----[n1] x [n2]-----[f2][s2]
|
|
|
|
-- s..secondary, f..far, n..near, x..node to be placed
|
2018-11-13 21:09:05 +01:00
|
|
|
local res,dir1,dir2,cnt = self:update_after_place_tube(pos, placer, pointed_thing)
|
|
|
|
if res and cnt > 0 then -- other nodes around?
|
2018-11-09 23:37:23 +01:00
|
|
|
local fpos1,fdir1 = self:del_meta(pos, dir1)
|
|
|
|
local fpos2,fdir2 = self:del_meta(pos, dir2)
|
|
|
|
self:add_meta(fpos1, fpos2,fdir2)
|
|
|
|
self:add_meta(fpos2, fpos1,fdir1)
|
|
|
|
self:update_secondary_node(fpos1,fdir1, fpos2,fdir2)
|
|
|
|
self:update_secondary_node(fpos2,fdir2, fpos1,fdir1)
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
2018-11-09 23:37:23 +01:00
|
|
|
return res
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
|
2018-11-09 23:37:23 +01:00
|
|
|
function Tube:after_dig_node(pos, dirs)
|
|
|
|
-- [s][f]----[n] x
|
|
|
|
-- s..secondary, f..far, n..near, x..node to be removed
|
|
|
|
for _,dir in ipairs(self:update_after_dig_node(pos, dirs)) do
|
|
|
|
local fpos,fdir = self:get_meta(pos, dir)
|
|
|
|
local npos,ndir = self:get_pos(pos, dir)
|
|
|
|
self:add_meta(npos, fpos,fdir)
|
|
|
|
self:add_meta(fpos, npos,ndir)
|
|
|
|
self:update_secondary_node(fpos,fdir, npos,ndir)
|
2018-10-14 21:28:31 +02:00
|
|
|
end
|
|
|
|
end
|
2018-10-05 21:55:40 +02:00
|
|
|
|
2018-10-15 20:05:41 +02:00
|
|
|
function Tube:after_dig_tube(pos, oldnode, oldmetadata)
|
2018-11-09 23:37:23 +01:00
|
|
|
-- [s1][f1]----[n1] x [n2]-----[f2][s2]
|
|
|
|
-- s..secondary, f..far, n..near, x..node to be removed
|
|
|
|
for _,dir in ipairs(self:update_after_dig_tube(pos, oldnode.param2)) do
|
|
|
|
local fpos,fdir = self:get_oldmeta(pos, dir, oldmetadata)
|
|
|
|
local npos,ndir = self:get_pos(pos, dir)
|
|
|
|
self:add_meta(npos, fpos,fdir)
|
|
|
|
self:add_meta(fpos, npos,ndir)
|
|
|
|
self:update_secondary_node(fpos,fdir, npos,ndir)
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
2018-11-12 21:05:43 +01:00
|
|
|
self:update_secondary_nodes_after_dig_tube(pos)
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
|
2018-11-09 23:37:23 +01:00
|
|
|
-- From source node to destination node via tubes.
|
|
|
|
-- pos is the source node position, dir the output dir
|
|
|
|
-- The returned pos is the destination position, dir
|
|
|
|
-- is the direction into the destination node.
|
|
|
|
function Tube:get_connected_node_pos(pos, dir)
|
2018-11-13 21:09:05 +01:00
|
|
|
local fpos,fdir = self:repair_meta(pos, dir)
|
2018-11-09 23:37:23 +01:00
|
|
|
local npos,ndir = self:get_pos(fpos,fdir)
|
2018-11-13 21:09:05 +01:00
|
|
|
-- 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)
|
|
|
|
end
|
2018-11-09 23:37:23 +01:00
|
|
|
return npos, dir
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-11-03 19:08:31 +01:00
|
|
|
-- To be called from a repair tool in the case of a "WorldEdit" or with
|
|
|
|
-- legacy nodes corrupted tube line.
|
2018-11-09 23:37:23 +01:00
|
|
|
function Tube:tool_repair_tube(pos)
|
|
|
|
local res,dir1,dir2 = self:determine_next_node(pos)
|
|
|
|
if res then
|
|
|
|
local fpos1,fdir1,cnt1 = self:repair_tube_line(pos, dir1)
|
|
|
|
local fpos2,fdir2,cnt2 = self:repair_tube_line(pos, dir2)
|
|
|
|
self:add_meta(fpos1, fpos2,fdir2)
|
|
|
|
self:add_meta(fpos2, fpos1,fdir1)
|
|
|
|
self:update_secondary_node(fpos1,fdir1, fpos2,fdir2)
|
|
|
|
self:update_secondary_node(fpos2,fdir2, fpos1,fdir1)
|
|
|
|
return dir1, dir2, fpos1, fpos2, fdir1, fdir2, cnt1 or 0, cnt2 or 0
|
|
|
|
end
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
|
2018-10-06 13:29:47 +02:00
|
|
|
|
|
|
|
-- To be called from a repair tool in the case, tube nodes are "unbreakable".
|
2018-10-14 21:28:31 +02:00
|
|
|
function Tube:tool_remove_tube(pos, sound)
|
2018-11-09 23:37:23 +01:00
|
|
|
local oldnode, oldmeta = self:remove_tube(pos, sound)
|
|
|
|
if oldnode then
|
|
|
|
self:after_dig_tube(pos, oldnode, oldmeta)
|
2018-10-05 21:55:40 +02:00
|
|
|
end
|
|
|
|
end
|
2018-10-06 21:46:38 +02:00
|
|
|
|
2018-11-09 23:37:23 +01:00
|
|
|
|
2018-10-06 21:46:38 +02:00
|
|
|
function Tube:prepare_pairing(pos, tube_dir, sFormspec)
|
|
|
|
local meta = M(pos)
|
2018-11-13 21:37:09 +01:00
|
|
|
if meta:get_int("tube_dir") ~= 0 then -- already prepared?
|
|
|
|
-- update tube_dir only
|
|
|
|
meta:set_int("tube_dir", tube_dir)
|
|
|
|
else
|
|
|
|
meta:set_int("tube_dir", tube_dir)
|
|
|
|
meta:set_string("channel", nil)
|
|
|
|
meta:set_string("infotext", "Unconnected")
|
|
|
|
meta:set_string("formspec", sFormspec)
|
|
|
|
end
|
2018-10-06 21:46:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Tube:pairing(pos, channel)
|
|
|
|
if self.pairingList[channel] and pos ~= self.pairingList[channel] then
|
|
|
|
-- store peer position on both nodes
|
|
|
|
local peer_pos = self.pairingList[channel]
|
2018-11-09 23:37:23 +01:00
|
|
|
|
2018-10-14 21:28:31 +02:00
|
|
|
local tube_dir1 = self:store_teleport_data(pos, peer_pos)
|
|
|
|
local tube_dir2 = self:store_teleport_data(peer_pos, pos)
|
2018-11-09 23:37:23 +01:00
|
|
|
local fpos1,fdir1 = self:repair_tube_line(pos, tube_dir1)
|
|
|
|
local fpos2,fdir2 = self:repair_tube_line(peer_pos, tube_dir2)
|
|
|
|
self:add_meta(fpos1, fpos2,fdir2)
|
|
|
|
self:add_meta(fpos2, fpos1,fdir1)
|
|
|
|
self:update_secondary_node(fpos1,fdir1, fpos2,fdir2)
|
|
|
|
self:update_secondary_node(fpos2,fdir2, fpos1,fdir1)
|
|
|
|
|
2018-10-06 21:46:38 +02:00
|
|
|
self.pairingList[channel] = nil
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
self.pairingList[channel] = pos
|
|
|
|
local meta = M(pos)
|
|
|
|
meta:set_string("channel", channel)
|
|
|
|
meta:set_string("infotext", "Unconnected ("..channel..")")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-14 21:28:31 +02:00
|
|
|
function Tube:stop_pairing(pos, oldmetadata, sFormspec)
|
2018-10-06 21:46:38 +02:00
|
|
|
-- unpair peer node
|
2018-10-14 21:28:31 +02:00
|
|
|
if oldmetadata and oldmetadata.fields and oldmetadata.fields.tele_pos then
|
2018-10-27 17:53:07 +02:00
|
|
|
local tele_pos = minetest.string_to_pos(oldmetadata.fields.tele_pos)
|
2018-10-14 21:28:31 +02:00
|
|
|
local peer_meta = M(tele_pos)
|
2018-10-06 21:46:38 +02:00
|
|
|
if peer_meta then
|
2018-11-09 23:37:23 +01:00
|
|
|
self:after_place_node(tele_pos, {peer_meta:get_int("tube_dir")})
|
|
|
|
|
2018-10-06 21:46:38 +02:00
|
|
|
peer_meta:set_string("channel", nil)
|
|
|
|
peer_meta:set_string("tele_pos", nil)
|
|
|
|
peer_meta:set_string("formspec", sFormspec)
|
|
|
|
peer_meta:set_string("infotext", "Unconnected")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|