2017-02-26 05:47:16 +01:00
|
|
|
-- A simple special-purpose class, this is used for building up sets of three-dimensional points for fast reference
|
2016-12-31 07:38:18 +01:00
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
digtron.Pointset = {}
|
|
|
|
digtron.Pointset.__index = digtron.Pointset
|
2016-12-31 07:38:18 +01:00
|
|
|
|
2019-01-05 06:06:12 +01:00
|
|
|
-- from builtin\game\misc.lua, modified to take values directly to avoid creating an intermediate vector
|
|
|
|
local hash_node_position_values = function(x, y, z)
|
|
|
|
return (z + 32768) * 65536 * 65536
|
|
|
|
+ (y + 32768) * 65536
|
|
|
|
+ x + 32768
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset.create()
|
2016-12-31 07:38:18 +01:00
|
|
|
local set = {}
|
2023-06-19 07:44:40 +02:00
|
|
|
setmetatable(set,digtron.Pointset)
|
2016-12-31 07:38:18 +01:00
|
|
|
set.points = {}
|
|
|
|
return set
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:clear()
|
2019-01-09 08:20:02 +01:00
|
|
|
local points = self.points
|
2023-06-19 07:44:40 +02:00
|
|
|
for k in pairs(points) do
|
2019-01-09 08:20:02 +01:00
|
|
|
points[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:set(x, y, z, value)
|
2016-12-31 07:38:18 +01:00
|
|
|
-- sets a value in the 3D array "points".
|
2019-01-05 06:06:12 +01:00
|
|
|
self.points[hash_node_position_values(x,y,z)] = value
|
2016-12-31 07:38:18 +01:00
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:set_if_not_in(excluded, x, y, z, value)
|
2016-12-31 07:38:18 +01:00
|
|
|
-- If a value is not already set for this point in the 3D array "excluded", set it in "points"
|
|
|
|
if excluded:get(x, y, z) ~= nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self:set(x, y, z, value)
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:get(x, y, z)
|
2016-12-31 07:38:18 +01:00
|
|
|
-- return a value from the 3D array "points"
|
2019-01-05 06:06:12 +01:00
|
|
|
return self.points[hash_node_position_values(x,y,z)]
|
2016-12-31 07:38:18 +01:00
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:set_pos(pos, value)
|
2017-02-26 05:47:16 +01:00
|
|
|
self:set(pos.x, pos.y, pos.z, value)
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:set_pos_if_not_in(excluded, pos, value)
|
2017-02-26 05:47:16 +01:00
|
|
|
self:set_if_not_in(excluded, pos.x, pos.y, pos.z, value)
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:get_pos(pos)
|
2017-02-26 05:47:16 +01:00
|
|
|
return self:get(pos.x, pos.y, pos.z)
|
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:pop()
|
2016-12-31 07:38:18 +01:00
|
|
|
-- returns a point that's in the 3D array, and then removes it.
|
2019-01-05 06:06:12 +01:00
|
|
|
local hash, value = next(self.points)
|
|
|
|
if hash == nil then return nil end
|
|
|
|
local pos = minetest.get_position_from_hash(hash)
|
2023-06-19 07:44:40 +02:00
|
|
|
self.points[hash] = nil
|
2019-01-05 06:06:12 +01:00
|
|
|
return pos, value
|
2017-02-26 05:47:16 +01:00
|
|
|
end
|
|
|
|
|
2023-06-19 07:44:40 +02:00
|
|
|
function digtron.Pointset:get_pos_list(value)
|
|
|
|
-- Returns a list of all points with the given value in standard Minetest vector format.
|
|
|
|
-- If no value is provided, returns all points
|
2017-02-26 05:47:16 +01:00
|
|
|
local outlist = {}
|
2019-01-05 06:06:12 +01:00
|
|
|
for hash, pointsval in pairs(self.points) do
|
|
|
|
if value == nil or pointsval == value then
|
|
|
|
table.insert(outlist, minetest.get_position_from_hash(hash))
|
2017-02-26 05:47:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return outlist
|
|
|
|
end
|