v0.4 - on_update function for secondary nodes introduced

This commit is contained in:
Joachim Stolberg 2018-11-11 18:47:06 +01:00
parent 96d43cc69e
commit 8bc08daf83
5 changed files with 15 additions and 93 deletions

@ -1,79 +0,0 @@
--[[
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)
]]--
-- for lazy programmers
local M = minetest.get_meta
local Tube = tubelib2.Tube
function Tube:on_convert_tube(convert_tube_clbk)
self.convert_tube_clbk = convert_tube_clbk
end
-- Register legacy tube nodes.
function Tube:add_legacy_node_names(names)
for _,name in ipairs(names) do
self.legacy_node_names[name] = true
end
end
function Tube:convert_tube_line(pos, dir)
local convert_next_tube = function(self, pos, dir)
local npos, node = self:get_node(pos, dir)
if self.legacy_node_names[node.name] then
local dir1, dir2, num
if self.convert_tube_clbk then
dir1, dir2, num = self.convert_tube_clbk(npos, node.name, node.param2)
else
dir1, dir2, num = self:determine_dir1_dir2_and_num_conn(npos)
end
if dir1 then
self.clbk_after_place_tube(self:tube_data_to_table(npos, dir1,
dir2 or tubelib2.Turn180Deg[dir1], num))
if tubelib2.Turn180Deg[dir] == dir1 then
return npos, dir2
else
return npos, dir1
end
end
end
end
local cnt = 0
if not dir then return pos, cnt end
while cnt <= 100000 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
function Tube:set_pairing(pos, peer_pos)
M(pos):set_int("tube_dir", self:get_primary_dir(pos))
M(peer_pos):set_int("tube_dir", self:get_primary_dir(peer_pos))
local tube_dir1 = self:store_teleport_data(pos, peer_pos)
local tube_dir2 = self:store_teleport_data(peer_pos, pos)
self:delete_tube_meta_data(pos, tube_dir1)
self:delete_tube_meta_data(peer_pos, tube_dir2)
end

@ -3,6 +3,5 @@ tubelib2 = {}
dofile(minetest.get_modpath("tubelib2") .. "/tube_api.lua")
dofile(minetest.get_modpath("tubelib2") .. "/internal2.lua")
dofile(minetest.get_modpath("tubelib2") .. "/internal1.lua")
dofile(minetest.get_modpath("tubelib2") .. "/convert.lua")
-- Only for testing/demo purposes
dofile(minetest.get_modpath("tubelib2") .. "/tube_test.lua")

@ -22,6 +22,7 @@ local M = minetest.get_meta
local Tube = tubelib2.Tube
local Turn180Deg = tubelib2.Turn180Deg
local Dir6dToVector = tubelib2.Dir6dToVector
local tValidNum = {[0] = true, true, true} -- 0..2 are valid
--------------------------------------------------------------------------------------
-- node get/test functions
@ -182,7 +183,7 @@ function Tube:update_after_place_node(pos, dirs)
dirs = dirs or self.dirs_to_check
for _,dir in ipairs(dirs) do
local npos, d1, d2, num = self:add_tube_dir(pos, dir)
if npos then
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num]then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
lRes[#lRes+1] = dir
end
@ -196,7 +197,7 @@ function Tube:update_after_dig_node(pos, dirs)
dirs = dirs or self.dirs_to_check
for _,dir in ipairs(dirs) do
local npos, d1, d2, num = self:del_tube_dir(pos, dir)
if npos then
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num]then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
lRes[#lRes+1] = dir
end
@ -210,19 +211,20 @@ function Tube:update_after_place_tube(pos, placer, pointed_thing)
if dir1 == nil then
return false
end
self.clbk_after_place_tube(self:tube_data_to_table(pos, dir1, dir2, num_tubes))
if self.valid_dirs[dir1] and self.valid_dirs[dir2] and tValidNum[num_tubes]then
self.clbk_after_place_tube(self:tube_data_to_table(pos, dir1, dir2, num_tubes))
end
if num_tubes >= 1 then
local npos, d1, d2, num = self:add_tube_dir(pos, dir1)
if npos then
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num]then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
end
end
if num_tubes >= 2 then
local npos, d1, d2, num = self:add_tube_dir(pos, dir2)
if npos then
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num]then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
end
end
@ -234,13 +236,13 @@ function Tube:update_after_dig_tube(pos, param2)
local lRes = {}
local npos, d1, d2, num = self:del_tube_dir(pos, dir1)
if npos then
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num]then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
lRes[#lRes+1] = dir1
end
npos, d1, d2, num = self:del_tube_dir(pos, dir2)
if npos then
if npos and self.valid_dirs[d1] and self.valid_dirs[d2] and tValidNum[num]then
self.clbk_after_place_tube(self:tube_data_to_table(npos, d1, d2, num))
lRes[#lRes+1] = dir2
end

@ -116,11 +116,11 @@ local Tube = tubelib2.Tube
function Tube:fdir(player)
local pitch = player:get_look_pitch()
if pitch > 1.1 and self.allowed_6d_dirs[6] then -- up?
if pitch > 1.1 and self.valid_dirs[6] then -- up?
return 6
elseif pitch < -1.1 and self.allowed_6d_dirs[5] then -- down?
elseif pitch < -1.1 and self.valid_dirs[5] then -- down?
return 5
elseif not self.allowed_6d_dirs[1] then
elseif not self.valid_dirs[1] then
return 6
else
return minetest.dir_to_facedir(player:get_look_dir()) + 1
@ -203,7 +203,7 @@ end
-- 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.allowed_6d_dirs)
local allowed = table.copy(self.valid_dirs)
-- Check for primary nodes (tubes)
for dir = 1,6 do

@ -52,7 +52,7 @@ function Tube:new(attr)
clbk_after_place_tube = attr.after_place_tube,
pairingList = {}, -- teleporting nodes
}
o.allowed_6d_dirs = Tbl(o.dirs_to_check)
o.valid_dirs = Tbl(o.dirs_to_check)
setmetatable(o, self)
self.__index = self
return o