tested with Hyperloop Elevator

This commit is contained in:
Joachim Stolberg 2018-10-20 19:19:44 +02:00
parent fe0b251078
commit 6bb5e76d0d
5 changed files with 112 additions and 22 deletions

55
convert.lua Normal file

@ -0,0 +1,55 @@
--[[
Tube Library 2
==============
Copyright (C) 2017-2018 Joachim Stolberg
LGPLv2.1+
See LICENSE.txt for more information
convert.lua
Optional module, only needed to convert legacy tubes into tubelib2 tubes.
This is done by means of a callback function:
dir1, dir2, num = func(pos, name, param2)
]]--
local Tube = tubelib2.Tube
function Tube:on_convert_tube(convert_tube_clbk)
self.convert_tube_clbk = convert_tube_clbk
end
function Tube:convert_to_tubelib2(pos1, dir1)
local pos2, dir2, cnt = self:convert_tube_line(pos1, dir1)
self:add_meta_data(pos1, pos2, dir1, dir2, cnt)
end
function Tube:convert_tube_line(pos, dir)
local convert_next_tube = function(self, pos, dir)
local npos, node = self:get_next_node(pos, dir)
local dir1, dir2, num = self.convert_tube_clbk(npos, node.name, node.param2)
if dir1 then
self.clbk_after_place_tube(self:tube_data_to_table(npos, dir1, dir2, num))
if tubelib2.Turn180Deg[dir] == dir1 then
return npos, dir2
else
return npos, dir1
end
end
end
local cnt = 0
if not dir then return pos, cnt end
while cnt <= self.max_tube_length do
local new_pos, new_dir = convert_next_tube(self, pos, dir)
if not new_dir then break end
pos, dir = new_pos, new_dir
cnt = cnt + 1
end
return pos, dir, cnt
end

@ -3,3 +3,4 @@ tubelib2 = {}
dofile(minetest.get_modpath("tubelib2") .. "/tube_api.lua")
dofile(minetest.get_modpath("tubelib2") .. "/internal.lua")
dofile(minetest.get_modpath("tubelib2") .. "/tube_test.lua")
dofile(minetest.get_modpath("tubelib2") .. "/convert.lua")

@ -44,6 +44,7 @@ local M = minetest.get_meta
local Turn180Deg = {[0]=0,3,4,1,2,6,5}
tubelib2.Turn180Deg = Turn180Deg
-- To calculate param2 based on dir6d information
local DirToParam2 = {
@ -189,13 +190,6 @@ function Tube:friendly_primary_node(pos, dir)
end
end
function Tube:secondary_node(pos, dir)
local npos, node = self:get_next_node(pos, dir)
if self.secondary_node_names[node.name] then
return npos
end
end
-- Jump over the teleport nodes to the next tube node
function Tube:get_next_teleport_node(pos, dir)
local npos = vector.add(pos, Dir6dToVector[dir or 0])
@ -216,14 +210,16 @@ function Tube:update_head_tube(pos1, pos2, dir2, num_tubes)
local node = get_node_lvm(pos1)
if self.primary_node_names[node.name] then
local d1, d2, num = self:decode_param2(node.param2)
num = (self:connected(pos1, d1) and 1 or 0) + (self:connected(pos1, d2) and 1 or 0)
node.param2 = self:encode_param2(d1, d2, num)
minetest.set_node(pos1, node)
if self.show_infotext then
M(pos1):set_string("infotext", S(pos2).." / "..num_tubes.." tubes")
if d1 and d2 then
num = (self:connected(pos1, d1) and 1 or 0) + (self:connected(pos1, d2) and 1 or 0)
node.param2 = self:encode_param2(d1, d2, num)
minetest.set_node(pos1, node)
if self.show_infotext then
M(pos1):set_string("infotext", S(pos2).." / "..num_tubes.." tubes")
end
M(pos1):set_string("peer_pos", S(pos2))
M(pos1):set_int("peer_dir", dir2)
end
M(pos1):set_string("peer_pos", S(pos2))
M(pos1):set_int("peer_dir", dir2)
end
end
@ -231,7 +227,9 @@ end
-- dir1/dir2 are the tube output directions (inventory nodes)
function Tube:add_meta_data(pos1, pos2, dir1, dir2, num_tubes)
self:update_head_tube(pos1, pos2, dir2, num_tubes)
self:update_head_tube(pos2, pos1, dir1, num_tubes)
if not vector.equals(pos1, pos2) then
self:update_head_tube(pos2, pos1, dir1, num_tubes)
end
end
-- Delete meta data on both tube sides.
@ -507,10 +505,16 @@ function Tube:get_peer_tube_head(node_tbl)
end
-- repair tube line
local pos2, dir2, cnt = self:find_peer_tube_head(node_tbl)
self:add_meta_data(node_tbl.pos, pos2, node_tbl.dir, dir2, cnt)
return pos2, dir2
if pos2 then
self:add_meta_data(node_tbl.pos, pos2, Turn180Deg[node_tbl.dir], dir2, cnt)
return pos2, dir2
end
end
-- pos is the position of the removed node
-- dir1, dir2 are the neighbor sides to be checked for meta data
-- oldmetadata is also used to check for meta data
-- If meta data is found (peer_pos), it is used to determine the tube head.
function Tube:delete_tube_meta_data(pos, dir1, dir2, oldmetadata)
-- tube with two connections?
if dir2 then

@ -56,6 +56,27 @@ function Tube:add_secondary_node_names(names)
end
end
-- Check if node at given position is a tube node
-- If dir == nil then node_pos = pos
-- Function returns the new pos or nil
function Tube:primary_node(pos, dir)
local npos, node = self:get_next_node(pos, dir)
local _,_,num_conn = self:decode_param2(node.param2)
if self.primary_node_names[node.name] then
return npos
end
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)
local npos, node = self:get_next_node(pos, dir)
if self.secondary_node_names[node.name] then
return npos
end
end
-- 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
@ -185,11 +206,19 @@ function Tube:tool_remove_tube(pos, sound)
gain=1,
max_hear_distance=5,
loop=false})
local npos1 = self:friendly_primary_node(pos, dir1)
local npos2 = self:friendly_primary_node(pos, dir2)
minetest.remove_node(pos)
if npos1 then self:tool_repair_tubes(npos1) end
if npos2 then self:tool_repair_tubes(npos2) end
self:delete_tube_meta_data(pos, dir1, dir2)
local npos, d1, d2, num = self:del_tube_dir(pos, dir1)
if npos then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
end
npos, d1, d2, num = self:del_tube_dir(pos, dir2)
if npos then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
end
end
end

@ -139,7 +139,8 @@ minetest.register_node("tubelib2:source", {
on_timer = function(pos, elapsed)
local tube_dir = M(pos):get_int("tube_dir")
local dest_pos = Tube:get_connected_node_pos(pos, tube_dir)
local dest_pos, dest_dir = Tube:get_connected_node_pos(pos, tube_dir)
print("on_timer: dest_pos="..S(dest_pos).." dest_dir="..dest_dir)
local inv = minetest.get_inventory({type="node", pos=dest_pos})
local stack = ItemStack("default:dirt")
if inv then