digtron/class_pointset.lua
Buckaroo Banzai 3e1c1ca208
add luacheck and ci workflow (#88)
* add luacheck and ci workflow

* fix another bunch

* more fixes

* more fixes

* regex magic

* add deps

* doc.lua special rules

* more regex magic

* final fixes

---------

Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
2023-06-19 07:44:40 +02:00

77 lines
2.1 KiB
Lua

-- A simple special-purpose class, this is used for building up sets of three-dimensional points for fast reference
digtron.Pointset = {}
digtron.Pointset.__index = digtron.Pointset
-- 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
function digtron.Pointset.create()
local set = {}
setmetatable(set,digtron.Pointset)
set.points = {}
return set
end
function digtron.Pointset:clear()
local points = self.points
for k in pairs(points) do
points[k] = nil
end
end
function digtron.Pointset:set(x, y, z, value)
-- sets a value in the 3D array "points".
self.points[hash_node_position_values(x,y,z)] = value
end
function digtron.Pointset:set_if_not_in(excluded, x, y, z, value)
-- 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
function digtron.Pointset:get(x, y, z)
-- return a value from the 3D array "points"
return self.points[hash_node_position_values(x,y,z)]
end
function digtron.Pointset:set_pos(pos, value)
self:set(pos.x, pos.y, pos.z, value)
end
function digtron.Pointset:set_pos_if_not_in(excluded, pos, value)
self:set_if_not_in(excluded, pos.x, pos.y, pos.z, value)
end
function digtron.Pointset:get_pos(pos)
return self:get(pos.x, pos.y, pos.z)
end
function digtron.Pointset:pop()
-- returns a point that's in the 3D array, and then removes it.
local hash, value = next(self.points)
if hash == nil then return nil end
local pos = minetest.get_position_from_hash(hash)
self.points[hash] = nil
return pos, value
end
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
local outlist = {}
for hash, pointsval in pairs(self.points) do
if value == nil or pointsval == value then
table.insert(outlist, minetest.get_position_from_hash(hash))
end
end
return outlist
end