1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apis/jumper/grid.lua

102 lines
2.9 KiB
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
--- The Grid class.
-- Implementation of the `grid` class.
-- The `grid` is a implicit graph which represents the 2D
-- world map layout on which the `pathfinder` object will run.
2017-10-26 22:56:55 +00:00
-- During a search, the `pathfinder` object needs to save some critical values.
-- These values are cached within each `node`
2016-12-11 19:24:52 +00:00
-- object, and the whole set of nodes are tight inside the `grid` object itself.
if (...) then
-- Dependencies
local _PATH = (...):gsub('%.grid$','')
-- Local references
local Utils = require (_PATH .. '.core.utils')
local Node = require (_PATH .. '.core.node')
-- Local references
local setmetatable = setmetatable
-- Offsets for straights moves
local straightOffsets = {
{x = 1, y = 0, z = 0} --[[W]], {x = -1, y = 0, z = 0}, --[[E]]
{x = 0, y = 1, z = 0} --[[S]], {x = 0, y = -1, z = 0}, --[[N]]
{x = 0, y = 0, z = 1} --[[U]], {x = 0, y = -0, z = -1}, --[[D]]
}
local Grid = {}
Grid.__index = Grid
2017-10-26 22:56:55 +00:00
function Grid:new(dim)
local newGrid = { }
newGrid._min_x, newGrid._max_x = dim.x, dim.ex
newGrid._min_y, newGrid._max_y = dim.y, dim.ey
newGrid._min_z, newGrid._max_z = dim.z, dim.ez
newGrid._nodes = { }
newGrid._width = (newGrid._max_x-newGrid._min_x)+1
newGrid._height = (newGrid._max_y-newGrid._min_y)+1
newGrid._length = (newGrid._max_z-newGrid._min_z)+1
return setmetatable(newGrid,Grid)
2016-12-11 19:24:52 +00:00
end
2017-10-26 22:56:55 +00:00
function Grid:isWalkableAt(x, y, z)
local node = self:getNodeAt(x,y,z)
return node and node.walkable ~= 1
2016-12-11 19:24:52 +00:00
end
function Grid:getWidth()
return self._width
end
function Grid:getHeight()
return self._height
end
function Grid:getNodes()
return self._nodes
end
function Grid:getBounds()
return self._min_x, self._min_y, self._min_z, self._max_x, self._max_y, self._max_z
end
--- Returns neighbours. The returned value is an array of __walkable__ nodes neighbouring a given `node`.
-- @treturn {node,...} an array of nodes neighbouring a given node
2017-10-26 22:56:55 +00:00
function Grid:getNeighbours(node)
2016-12-11 19:24:52 +00:00
local neighbours = {}
for i = 1,#straightOffsets do
local n = self:getNodeAt(
2017-10-28 00:24:48 +00:00
node.x + straightOffsets[i].x,
node.y + straightOffsets[i].y,
node.z + straightOffsets[i].z
2016-12-11 19:24:52 +00:00
)
2017-10-28 00:24:48 +00:00
if n and self:isWalkableAt(n.x, n.y, n.z) then
2016-12-11 19:24:52 +00:00
neighbours[#neighbours+1] = n
end
end
return neighbours
end
2017-10-28 00:24:48 +00:00
function Grid:getNodeAt(x,y,z)
2016-12-11 19:24:52 +00:00
if not x or not y or not z then return end
if Utils.outOfRange(x,self._min_x,self._max_x) then return end
if Utils.outOfRange(y,self._min_y,self._max_y) then return end
if Utils.outOfRange(z,self._min_z,self._max_z) then return end
2017-10-26 22:56:55 +00:00
-- inefficient
2016-12-11 19:24:52 +00:00
if not self._nodes[y] then self._nodes[y] = {} end
if not self._nodes[y][x] then self._nodes[y][x] = {} end
if not self._nodes[y][x][z] then self._nodes[y][x][z] = Node:new(x,y,z) end
return self._nodes[y][x][z]
end
return setmetatable(Grid,{
__call = function(self,...)
return self:new(...)
end
})
end