mirror of
https://github.com/joe7575/tubelib2.git
synced 2024-08-19 04:54:11 +02:00
Generator function 'get_tube_line' added, storage improvements
This commit is contained in:
parent
4fccd2e17d
commit
e4130fff1e
@ -80,5 +80,6 @@ Textures: CC0
|
|||||||
- 2019-10-25 v1.6 * callback 'tubelib2_on_update2' added
|
- 2019-10-25 v1.6 * callback 'tubelib2_on_update2' added
|
||||||
- 2020-01-03 v1.7 * max_tube_length bugfix
|
- 2020-01-03 v1.7 * max_tube_length bugfix
|
||||||
- 2020-02-02 v1.8 * 'special nodes' as alternative to 'secondary nodes' introduced
|
- 2020-02-02 v1.8 * 'special nodes' as alternative to 'secondary nodes' introduced
|
||||||
|
- 2020-05-31 v1.9 * Generator function 'get_tube_line' added, storage improvements
|
||||||
|
|
||||||
|
|
||||||
|
2
init.lua
2
init.lua
@ -9,4 +9,4 @@ dofile(MP .. "/internal1.lua")
|
|||||||
dofile(MP .. "/tube_api.lua")
|
dofile(MP .. "/tube_api.lua")
|
||||||
dofile(MP .. "/storage.lua")
|
dofile(MP .. "/storage.lua")
|
||||||
-- Only for testing/demo purposes
|
-- Only for testing/demo purposes
|
||||||
--dofile(MP .. "/tube_test.lua")
|
dofile(MP .. "/tube_test.lua")
|
||||||
|
@ -97,9 +97,9 @@ end
|
|||||||
-- Function returns param2, new_pos or nil
|
-- Function returns param2, new_pos or nil
|
||||||
function Tube:get_primary_node_param2(pos, dir)
|
function Tube:get_primary_node_param2(pos, dir)
|
||||||
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
local npos = vector.add(pos, Dir6dToVector[dir or 0])
|
||||||
local node = self:get_node_lvm(npos)
|
self.node = self:get_node_lvm(npos)
|
||||||
if self.primary_node_names[node.name] then
|
if self.primary_node_names[self.node.name] then
|
||||||
return node.param2, npos
|
return self.node.param2, npos
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
21
storage.lua
21
storage.lua
@ -42,6 +42,25 @@ end)
|
|||||||
|
|
||||||
minetest.after(600, update_mod_storage)
|
minetest.after(600, update_mod_storage)
|
||||||
|
|
||||||
|
local function empty_block(block)
|
||||||
|
local empty = true
|
||||||
|
local tbl = minetest.deserialize(block)
|
||||||
|
for k,v in pairs(tbl) do
|
||||||
|
if k ~= "used" and k ~= "best_before" then
|
||||||
|
empty = false
|
||||||
|
end
|
||||||
|
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)
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Local helper functions
|
-- Local helper functions
|
||||||
@ -118,7 +137,7 @@ function tubelib2.walk_over_all(clbk)
|
|||||||
if mem then
|
if mem then
|
||||||
if node_key ~= "used" and node_key ~= "best_before" then
|
if node_key ~= "used" and node_key ~= "best_before" then
|
||||||
local pos = keys_to_pos(block_key, node_key)
|
local pos = keys_to_pos(block_key, node_key)
|
||||||
local node = techage.get_node_lvm(pos)
|
local node = tubelib2.get_node_lvm(pos)
|
||||||
clbk(pos, node, mem)
|
clbk(pos, node, mem)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
38
tube_api.lua
38
tube_api.lua
@ -13,7 +13,7 @@
|
|||||||
]]--
|
]]--
|
||||||
|
|
||||||
-- Version for compatibility checks, see readme.md/history
|
-- Version for compatibility checks, see readme.md/history
|
||||||
tubelib2.version = 1.8
|
tubelib2.version = 1.9
|
||||||
|
|
||||||
-- for lazy programmers
|
-- for lazy programmers
|
||||||
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
|
||||||
@ -48,6 +48,24 @@ local function get_pos(pos, dir)
|
|||||||
end
|
end
|
||||||
tubelib2.get_pos = get_pos
|
tubelib2.get_pos = get_pos
|
||||||
|
|
||||||
|
function tubelib2.get_node_lvm(pos)
|
||||||
|
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
|
||||||
|
|
||||||
local function update1(self, pos, dir)
|
local function update1(self, pos, dir)
|
||||||
local fpos,fdir = self:walk_tube_line(pos, dir)
|
local fpos,fdir = self:walk_tube_line(pos, dir)
|
||||||
self:infotext(get_pos(pos, dir), fpos)
|
self:infotext(get_pos(pos, dir), fpos)
|
||||||
@ -381,3 +399,21 @@ end
|
|||||||
function Tube:switch_tube_line(pos, dir, state)
|
function Tube:switch_tube_line(pos, dir, state)
|
||||||
self:switch_nodes(pos, dir, state)
|
self:switch_nodes(pos, dir, state)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Generator function to iterate over a tube line
|
||||||
|
-- Returns for each tube: i , pos, node
|
||||||
|
function Tube:get_tube_line(pos, dir)
|
||||||
|
if pos and dir then
|
||||||
|
self.ref = {pos = pos, dir = dir}
|
||||||
|
return function(self, i)
|
||||||
|
if i < self.max_tube_length then
|
||||||
|
local new_pos, new_dir, num = self:get_next_tube(self.ref.pos, self.ref.dir)
|
||||||
|
if new_pos then
|
||||||
|
self.ref.pos, self.ref.dir = new_pos, new_dir
|
||||||
|
i = i + 1
|
||||||
|
return i, self.ref.pos, self.node
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end, self, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@ -45,6 +45,9 @@ Tube:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir)
|
|||||||
print(S(pos).." to the "..sdir..": Connected with "..node.name.." at "..S(peer_pos).."/"..peer_in_dir)
|
print(S(pos).." to the "..sdir..": Connected with "..node.name.." at "..S(peer_pos).."/"..peer_in_dir)
|
||||||
else
|
else
|
||||||
print(S(pos).." to the "..sdir..": Connected with "..S(peer_pos).."/"..peer_in_dir)
|
print(S(pos).." to the "..sdir..": Connected with "..S(peer_pos).."/"..peer_in_dir)
|
||||||
|
for i, pos, node in Tube:get_tube_line(pos, out_dir) do
|
||||||
|
print("walk", S(pos), node.name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user