mirror of
https://github.com/kepler155c/opus
synced 2025-10-25 04:37:40 +00:00
proxy + pathfinding optimization
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
-- Various assertion function for API methods argument-checking
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependancies
|
||||
local _PATH = (...):gsub('%.core.assert$','')
|
||||
local Utils = require (_PATH .. '.core.utils')
|
||||
|
||||
-- Local references
|
||||
local lua_type = type
|
||||
local floor = math.floor
|
||||
local concat = table.concat
|
||||
local next = next
|
||||
local pairs = pairs
|
||||
local getmetatable = getmetatable
|
||||
|
||||
-- Is I an integer ?
|
||||
local function isInteger(i)
|
||||
return lua_type(i) ==('number') and (floor(i)==i)
|
||||
end
|
||||
|
||||
-- Override lua_type to return integers
|
||||
local function type(v)
|
||||
return isInteger(v) and 'int' or lua_type(v)
|
||||
end
|
||||
|
||||
-- Does the given array contents match a predicate type ?
|
||||
local function arrayContentsMatch(t,...)
|
||||
local n_count = Utils.arraySize(t)
|
||||
if n_count < 1 then return false end
|
||||
local init_count = t[0] and 0 or 1
|
||||
local n_count = (t[0] and n_count-1 or n_count)
|
||||
local types = {...}
|
||||
if types then types = concat(types) end
|
||||
for i=init_count,n_count,1 do
|
||||
if not t[i] then return false end
|
||||
if types then
|
||||
if not types:match(type(t[i])) then return false end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Checks if arg is a valid array map
|
||||
local function isMap(m)
|
||||
if not arrayContentsMatch(m, 'table') then return false end
|
||||
local lsize = Utils.arraySize(m[next(m)])
|
||||
for k,v in pairs(m) do
|
||||
if not arrayContentsMatch(m[k], 'string', 'int') then return false end
|
||||
if Utils.arraySize(v)~=lsize then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Checks if s is a valid string map
|
||||
local function isStringMap(s)
|
||||
if lua_type(s) ~= 'string' then return false end
|
||||
local w
|
||||
for row in s:gmatch('[^\n\r]+') do
|
||||
if not row then return false end
|
||||
w = w or #row
|
||||
if w ~= #row then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- Does instance derive straight from class
|
||||
local function derives(instance, class)
|
||||
return getmetatable(instance) == class
|
||||
end
|
||||
|
||||
-- Does instance inherits from class
|
||||
local function inherits(instance, class)
|
||||
return (getmetatable(getmetatable(instance)) == class)
|
||||
end
|
||||
|
||||
-- Is arg a boolean
|
||||
local function isBoolean(b)
|
||||
return (b==true or b==false)
|
||||
end
|
||||
|
||||
-- Is arg nil ?
|
||||
local function isNil(n)
|
||||
return (n==nil)
|
||||
end
|
||||
|
||||
local function matchType(value, types)
|
||||
return types:match(type(value))
|
||||
end
|
||||
|
||||
return {
|
||||
arrayContentsMatch = arrayContentsMatch,
|
||||
derives = derives,
|
||||
inherits = inherits,
|
||||
isInteger = isInteger,
|
||||
isBool = isBoolean,
|
||||
isMap = isMap,
|
||||
isStrMap = isStringMap,
|
||||
isOutOfRange = isOutOfRange,
|
||||
isNil = isNil,
|
||||
type = type,
|
||||
matchType = matchType
|
||||
}
|
||||
|
||||
end
|
||||
@@ -1,98 +0,0 @@
|
||||
--- Heuristic functions for search algorithms.
|
||||
-- A <a href="http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html">distance heuristic</a>
|
||||
-- provides an *estimate of the optimal distance cost* from a given location to a target.
|
||||
-- As such, it guides the pathfinder to the goal, helping it to decide which route is the best.
|
||||
--
|
||||
-- This script holds the definition of some built-in heuristics available through jumper.
|
||||
--
|
||||
-- Distance functions are internally used by the `pathfinder` to evaluate the optimal path
|
||||
-- from the start location to the goal. These functions share the same prototype:
|
||||
-- local function myHeuristic(nodeA, nodeB)
|
||||
-- -- function body
|
||||
-- end
|
||||
-- Jumper features some built-in distance heuristics, namely `MANHATTAN`, `EUCLIDIAN`, `DIAGONAL`, `CARDINTCARD`.
|
||||
-- You can also supply your own heuristic function, following the same template as above.
|
||||
|
||||
|
||||
local abs = math.abs
|
||||
local sqrt = math.sqrt
|
||||
local sqrt2 = sqrt(2)
|
||||
local max, min = math.max, math.min
|
||||
|
||||
local Heuristics = {}
|
||||
--- Manhattan distance.
|
||||
-- <br/>This heuristic is the default one being used by the `pathfinder` object.
|
||||
-- <br/>Evaluates as <code>distance = |dx|+|dy|</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('MANHATTAN')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.MANHATTAN)
|
||||
function Heuristics.MANHATTAN(nodeA, nodeB)
|
||||
local dx = abs(nodeA._x - nodeB._x)
|
||||
local dy = abs(nodeA._y - nodeB._y)
|
||||
local dz = abs(nodeA._z - nodeB._z)
|
||||
return (dx + dy + dz)
|
||||
end
|
||||
|
||||
--- Euclidian distance.
|
||||
-- <br/>Evaluates as <code>distance = squareRoot(dx*dx+dy*dy)</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('EUCLIDIAN')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.EUCLIDIAN)
|
||||
function Heuristics.EUCLIDIAN(nodeA, nodeB)
|
||||
local dx = nodeA._x - nodeB._x
|
||||
local dy = nodeA._y - nodeB._y
|
||||
local dz = nodeA._z - nodeB._z
|
||||
return sqrt(dx*dx+dy*dy+dz*dz)
|
||||
end
|
||||
|
||||
--- Diagonal distance.
|
||||
-- <br/>Evaluates as <code>distance = max(|dx|, abs|dy|)</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('DIAGONAL')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.DIAGONAL)
|
||||
function Heuristics.DIAGONAL(nodeA, nodeB)
|
||||
local dx = abs(nodeA._x - nodeB._x)
|
||||
local dy = abs(nodeA._y - nodeB._y)
|
||||
return max(dx,dy)
|
||||
end
|
||||
|
||||
--- Cardinal/Intercardinal distance.
|
||||
-- <br/>Evaluates as <code>distance = min(dx, dy)*squareRoot(2) + max(dx, dy) - min(dx, dy)</code>
|
||||
-- @class function
|
||||
-- @tparam node nodeA a node
|
||||
-- @tparam node nodeB another node
|
||||
-- @treturn number the distance from __nodeA__ to __nodeB__
|
||||
-- @usage
|
||||
-- -- First method
|
||||
-- pathfinder:setHeuristic('CARDINTCARD')
|
||||
-- -- Second method
|
||||
-- local Distance = require ('jumper.core.heuristics')
|
||||
-- pathfinder:setHeuristic(Distance.CARDINTCARD)
|
||||
function Heuristics.CARDINTCARD(nodeA, nodeB)
|
||||
local dx = abs(nodeA._x - nodeB._x)
|
||||
local dy = abs(nodeA._y - nodeB._y)
|
||||
return min(dx,dy) * sqrt2 + max(dx,dy) - min(dx,dy)
|
||||
end
|
||||
|
||||
return Heuristics
|
||||
@@ -1,32 +0,0 @@
|
||||
local addNode(self, node, nextNode, ed)
|
||||
if not self._pathDB[node] then self._pathDB[node] = {} end
|
||||
self._pathDB[node][ed] = (nextNode == ed and node or nextNode)
|
||||
end
|
||||
|
||||
-- Path lookupTable
|
||||
local lookupTable = {}
|
||||
lookupTable.__index = lookupTable
|
||||
|
||||
function lookupTable:new()
|
||||
local lut = {_pathDB = {}}
|
||||
return setmetatable(lut, lookupTable)
|
||||
end
|
||||
|
||||
function lookupTable:addPath(path)
|
||||
local st, ed = path._nodes[1], path._nodes[#path._nodes]
|
||||
for node, count in path:nodes() do
|
||||
local nextNode = path._nodes[count+1]
|
||||
if nextNode then addNode(self, node, nextNode, ed) end
|
||||
end
|
||||
end
|
||||
|
||||
function lookupTable:hasPath(nodeA, nodeB)
|
||||
local found
|
||||
found = self._pathDB[nodeA] and self._path[nodeA][nodeB]
|
||||
if found then return true, true end
|
||||
found = self._pathDB[nodeB] and self._path[nodeB][nodeA]
|
||||
if found then return true, false end
|
||||
return false
|
||||
end
|
||||
|
||||
return lookupTable
|
||||
@@ -4,14 +4,12 @@
|
||||
-- and then cached within the `grid`.
|
||||
--
|
||||
-- In the following implementation, nodes can be compared using the `<` operator. The comparison is
|
||||
-- made with regards of their `f` cost. From a given node being examined, the `pathfinder` will expand the search
|
||||
-- made with regards of their `f` cost. From a given node being examined, the `pathfinder` will expand the search
|
||||
-- to the next neighbouring node having the lowest `f` cost. See `core.bheap` for more details.
|
||||
--
|
||||
--
|
||||
|
||||
if (...) then
|
||||
|
||||
local assert = assert
|
||||
|
||||
--- The `Node` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore,_ <code>Node(...)</code> _acts as a shortcut to_ <code>Node:new(...)</code>.
|
||||
@@ -26,7 +24,7 @@ if (...) then
|
||||
-- @treturn node a new `node`
|
||||
-- @usage local node = Node(3,4)
|
||||
function Node:new(x,y,z)
|
||||
return setmetatable({_x = x, _y = y, _z = z, _clearance = {}}, Node)
|
||||
return setmetatable({_x = x, _y = y, _z = z }, Node)
|
||||
end
|
||||
|
||||
-- Enables the use of operator '<' to compare nodes.
|
||||
@@ -36,48 +34,24 @@ if (...) then
|
||||
--- Returns x-coordinate of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the x-coordinate of the `node`
|
||||
-- @usage local x = node:getX()
|
||||
-- @usage local x = node:getX()
|
||||
function Node:getX() return self._x end
|
||||
|
||||
|
||||
--- Returns y-coordinate of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the y-coordinate of the `node`
|
||||
-- @usage local y = node:getY()
|
||||
-- @treturn number the y-coordinate of the `node`
|
||||
-- @usage local y = node:getY()
|
||||
function Node:getY() return self._y end
|
||||
|
||||
function Node:getZ() return self._z end
|
||||
|
||||
|
||||
--- Returns x and y coordinates of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the x-coordinate of the `node`
|
||||
-- @treturn number the y-coordinate of the `node`
|
||||
-- @usage local x, y = node:getPos()
|
||||
-- @usage local x, y = node:getPos()
|
||||
function Node:getPos() return self._x, self._y, self._z end
|
||||
|
||||
--- Returns the amount of true [clearance](http://aigamedev.com/open/tutorial/clearance-based-pathfinding/#TheTrueClearanceMetric)
|
||||
-- for a given `node`
|
||||
-- @class function
|
||||
-- @tparam string|int|func walkable the value for walkable locations in the collision map array.
|
||||
-- @treturn int the clearance of the `node`
|
||||
-- @usage
|
||||
-- -- Assuming walkable was 0
|
||||
-- local clearance = node:getClearance(0)
|
||||
function Node:getClearance(walkable)
|
||||
return self._clearance[walkable]
|
||||
end
|
||||
|
||||
--- Removes the clearance value for a given walkable.
|
||||
-- @class function
|
||||
-- @tparam string|int|func walkable the value for walkable locations in the collision map array.
|
||||
-- @treturn node self (the calling `node` itself, can be chained)
|
||||
-- @usage
|
||||
-- -- Assuming walkable is defined
|
||||
-- node:removeClearance(walkable)
|
||||
function Node:removeClearance(walkable)
|
||||
self._clearance[walkable] = nil
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Clears temporary cached attributes of a `node`.
|
||||
-- Deletes the attributes cached within a given node after a pathfinding call.
|
||||
-- This function is internally used by the search algorithms, so you should not use it explicitely.
|
||||
@@ -91,10 +65,10 @@ if (...) then
|
||||
self._opened, self._closed, self._parent = nil, nil, nil
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
return setmetatable(Node,
|
||||
{__call = function(self,...)
|
||||
return Node:new(...)
|
||||
{__call = function(_,...)
|
||||
return Node:new(...)
|
||||
end}
|
||||
)
|
||||
end
|
||||
@@ -6,17 +6,7 @@
|
||||
-- It should normally not be used explicitely, yet it remains fully accessible.
|
||||
--
|
||||
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependencies
|
||||
local _PATH = (...):match('(.+)%.path$')
|
||||
local Heuristic = require (_PATH .. '.heuristics')
|
||||
|
||||
-- Local references
|
||||
local abs, max = math.abs, math.max
|
||||
local t_insert, t_remove = table.insert, table.remove
|
||||
|
||||
--- The `Path` class.<br/>
|
||||
-- This class is callable.
|
||||
-- Therefore, <em><code>Path(...)</code></em> acts as a shortcut to <em><code>Path:new(...)</code></em>.
|
||||
@@ -42,8 +32,8 @@ if (...) then
|
||||
-- for node, count in p:iter() do
|
||||
-- ...
|
||||
-- end
|
||||
function Path:iter()
|
||||
local i,pathLen = 1,#self._nodes
|
||||
function Path:nodes()
|
||||
local i = 1
|
||||
return function()
|
||||
if self._nodes[i] then
|
||||
i = i+1
|
||||
@@ -51,150 +41,9 @@ if (...) then
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Iterates on each single `node` along a `path`. At each step of iteration,
|
||||
-- returns a `node` plus a count value. Alias for @{Path:iter}
|
||||
-- @class function
|
||||
-- @name Path:nodes
|
||||
-- @treturn node a `node`
|
||||
-- @treturn int the count for the number of nodes
|
||||
-- @see Path:iter
|
||||
-- @usage
|
||||
-- for node, count in p:nodes() do
|
||||
-- ...
|
||||
-- end
|
||||
Path.nodes = Path.iter
|
||||
|
||||
--- Evaluates the `path` length
|
||||
-- @class function
|
||||
-- @treturn number the `path` length
|
||||
-- @usage local len = p:getLength()
|
||||
function Path:getLength()
|
||||
local len = 0
|
||||
for i = 2,#self._nodes do
|
||||
len = len + Heuristic.EUCLIDIAN(self._nodes[i], self._nodes[i-1])
|
||||
end
|
||||
return len
|
||||
end
|
||||
|
||||
--- Counts the number of steps.
|
||||
-- Returns the number of waypoints (nodes) in the current path.
|
||||
-- @class function
|
||||
-- @tparam node node a node to be added to the path
|
||||
-- @tparam[opt] int index the index at which the node will be inserted. If omitted, the node will be appended after the last node in the path.
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @usage local nSteps = p:countSteps()
|
||||
function Path:addNode(node, index)
|
||||
index = index or #self._nodes+1
|
||||
t_insert(self._nodes, index, node)
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- `Path` filling modifier. Interpolates between non contiguous nodes along a `path`
|
||||
-- to build a fully continuous `path`. This maybe useful when using search algorithms such as Jump Point Search.
|
||||
-- Does the opposite of @{Path:filter}
|
||||
-- @class function
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @see Path:filter
|
||||
-- @usage p:fill()
|
||||
function Path:fill()
|
||||
local i = 2
|
||||
local xi,yi,dx,dy
|
||||
local N = #self._nodes
|
||||
local incrX, incrY
|
||||
while true do
|
||||
xi,yi = self._nodes[i]._x,self._nodes[i]._y
|
||||
dx,dy = xi-self._nodes[i-1]._x,yi-self._nodes[i-1]._y
|
||||
if (abs(dx) > 1 or abs(dy) > 1) then
|
||||
incrX = dx/max(abs(dx),1)
|
||||
incrY = dy/max(abs(dy),1)
|
||||
t_insert(self._nodes, i, self._grid:getNodeAt(self._nodes[i-1]._x + incrX, self._nodes[i-1]._y +incrY))
|
||||
N = N+1
|
||||
else i=i+1
|
||||
end
|
||||
if i>N then break end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- `Path` compression modifier. Given a `path`, eliminates useless nodes to return a lighter `path`
|
||||
-- consisting of straight moves. Does the opposite of @{Path:fill}
|
||||
-- @class function
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @see Path:fill
|
||||
-- @usage p:filter()
|
||||
function Path:filter()
|
||||
local i = 2
|
||||
local xi,yi,dx,dy, olddx, olddy
|
||||
xi,yi = self._nodes[i]._x, self._nodes[i]._y
|
||||
dx, dy = xi - self._nodes[i-1]._x, yi-self._nodes[i-1]._y
|
||||
while true do
|
||||
olddx, olddy = dx, dy
|
||||
if self._nodes[i+1] then
|
||||
i = i+1
|
||||
xi, yi = self._nodes[i]._x, self._nodes[i]._y
|
||||
dx, dy = xi - self._nodes[i-1]._x, yi - self._nodes[i-1]._y
|
||||
if olddx == dx and olddy == dy then
|
||||
t_remove(self._nodes, i-1)
|
||||
i = i - 1
|
||||
end
|
||||
else break end
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Clones a `path`.
|
||||
-- @class function
|
||||
-- @treturn path a `path`
|
||||
-- @usage local p = path:clone()
|
||||
function Path:clone()
|
||||
local p = Path:new()
|
||||
for node in self:nodes() do p:addNode(node) end
|
||||
return p
|
||||
end
|
||||
|
||||
--- Checks if a `path` is equal to another. It also supports *filtered paths* (see @{Path:filter}).
|
||||
-- @class function
|
||||
-- @tparam path p2 a path
|
||||
-- @treturn boolean a boolean
|
||||
-- @usage print(myPath:isEqualTo(anotherPath))
|
||||
function Path:isEqualTo(p2)
|
||||
local p1 = self:clone():filter()
|
||||
local p2 = p2:clone():filter()
|
||||
for node, count in p1:nodes() do
|
||||
if not p2._nodes[count] then return false end
|
||||
local n = p2._nodes[count]
|
||||
if n._x~=node._x or n._y~=node._y then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--- Reverses a `path`.
|
||||
-- @class function
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @usage myPath:reverse()
|
||||
function Path:reverse()
|
||||
local _nodes = {}
|
||||
for i = #self._nodes,1,-1 do
|
||||
_nodes[#_nodes+1] = self._nodes[i]
|
||||
end
|
||||
self._nodes = _nodes
|
||||
return self
|
||||
end
|
||||
|
||||
--- Appends a given `path` to self.
|
||||
-- @class function
|
||||
-- @tparam path p a path
|
||||
-- @treturn path self (the calling `path` itself, can be chained)
|
||||
-- @usage myPath:append(anotherPath)
|
||||
function Path:append(p)
|
||||
for node in p:nodes() do self:addNode(node) end
|
||||
return self
|
||||
end
|
||||
|
||||
return setmetatable(Path,
|
||||
{__call = function(self,...)
|
||||
{__call = function(_,...)
|
||||
return Path:new(...)
|
||||
end
|
||||
})
|
||||
|
||||
@@ -5,125 +5,20 @@ if (...) then
|
||||
-- Dependencies
|
||||
local _PATH = (...):gsub('%.utils$','')
|
||||
local Path = require (_PATH .. '.path')
|
||||
local Node = require (_PATH .. '.node')
|
||||
|
||||
-- Local references
|
||||
local pairs = pairs
|
||||
local type = type
|
||||
local t_insert = table.insert
|
||||
local assert = assert
|
||||
local coroutine = coroutine
|
||||
|
||||
-- Raw array items count
|
||||
local function arraySize(t)
|
||||
local count = 0
|
||||
for k,v in pairs(t) do
|
||||
for _ in pairs(t) do
|
||||
count = count+1
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
-- Parses a string map and builds an array map
|
||||
local function stringMapToArray(str)
|
||||
local map = {}
|
||||
local w, h
|
||||
for line in str:gmatch('[^\n\r]+') do
|
||||
if line then
|
||||
w = not w and #line or w
|
||||
assert(#line == w, 'Error parsing map, rows must have the same size!')
|
||||
h = (h or 0) + 1
|
||||
map[h] = {}
|
||||
for char in line:gmatch('.') do
|
||||
map[h][#map[h]+1] = char
|
||||
end
|
||||
end
|
||||
end
|
||||
return map
|
||||
end
|
||||
|
||||
-- Collects and returns the keys of a given array
|
||||
local function getKeys(t)
|
||||
local keys = {}
|
||||
for k,v in pairs(t) do keys[#keys+1] = k end
|
||||
return keys
|
||||
end
|
||||
|
||||
-- Calculates the bounds of a 2d array
|
||||
local function getArrayBounds(map)
|
||||
local min_x, max_x
|
||||
local min_y, max_y
|
||||
for y in pairs(map) do
|
||||
min_y = not min_y and y or (y<min_y and y or min_y)
|
||||
max_y = not max_y and y or (y>max_y and y or max_y)
|
||||
for x in pairs(map[y]) do
|
||||
min_x = not min_x and x or (x<min_x and x or min_x)
|
||||
max_x = not max_x and x or (x>max_x and x or max_x)
|
||||
end
|
||||
end
|
||||
return min_x,max_x,min_y,max_y
|
||||
end
|
||||
|
||||
-- Converts an array to a set of nodes
|
||||
local function arrayToNodes(map)
|
||||
local min_x, max_x
|
||||
local min_y, max_y
|
||||
local min_z, max_z
|
||||
local nodes = {}
|
||||
for y in pairs(map) do
|
||||
min_y = not min_y and y or (y<min_y and y or min_y)
|
||||
max_y = not max_y and y or (y>max_y and y or max_y)
|
||||
nodes[y] = {}
|
||||
for x in pairs(map[y]) do
|
||||
min_x = not min_x and x or (x<min_x and x or min_x)
|
||||
max_x = not max_x and x or (x>max_x and x or max_x)
|
||||
nodes[y][x] = {}
|
||||
for z in pairs(map[y][x]) do
|
||||
min_z = not min_z and z or (z<min_z and z or min_z)
|
||||
max_z = not max_z and z or (z>max_z and z or max_z)
|
||||
nodes[y][x][z] = Node:new(x,y,z)
|
||||
end
|
||||
end
|
||||
end
|
||||
return nodes,
|
||||
(min_x or 0), (max_x or 0),
|
||||
(min_y or 0), (max_y or 0),
|
||||
(min_z or 0), (max_z or 0)
|
||||
end
|
||||
|
||||
-- Iterator, wrapped within a coroutine
|
||||
-- Iterates around a given position following the outline of a square
|
||||
local function around()
|
||||
local iterf = function(x0, y0, z0, s)
|
||||
local x, y, z = x0-s, y0-s, z0-s
|
||||
coroutine.yield(x, y, z)
|
||||
repeat
|
||||
x = x + 1
|
||||
coroutine.yield(x,y,z)
|
||||
until x == x0+s
|
||||
repeat
|
||||
y = y + 1
|
||||
coroutine.yield(x,y,z)
|
||||
until y == y0 + s
|
||||
repeat
|
||||
z = z + 1
|
||||
coroutine.yield(x,y,z)
|
||||
until z == z0 + s
|
||||
repeat
|
||||
x = x - 1
|
||||
coroutine.yield(x, y,z)
|
||||
until x == x0-s
|
||||
repeat
|
||||
y = y - 1
|
||||
coroutine.yield(x,y,z)
|
||||
until y == y0-s+1
|
||||
repeat
|
||||
z = z - 1
|
||||
coroutine.yield(x,y,z)
|
||||
until z == z0-s+1
|
||||
end
|
||||
return coroutine.create(iterf)
|
||||
end
|
||||
|
||||
-- Extract a path from a given start/end position
|
||||
local function traceBackPath(finder, node, startNode)
|
||||
local path = Path:new()
|
||||
@@ -151,17 +46,11 @@ if (...) then
|
||||
local function outOfRange(i,low,up)
|
||||
return (i< low or i > up)
|
||||
end
|
||||
|
||||
|
||||
return {
|
||||
arraySize = arraySize,
|
||||
getKeys = getKeys,
|
||||
indexOf = indexOf,
|
||||
outOfRange = outOfRange,
|
||||
getArrayBounds = getArrayBounds,
|
||||
arrayToNodes = arrayToNodes,
|
||||
strToMap = stringMapToArray,
|
||||
around = around,
|
||||
drAround = drAround,
|
||||
traceBackPath = traceBackPath
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user