Initial commit
This commit is contained in:
105
sys/apis/jumper/core/assert.lua
Normal file
105
sys/apis/jumper/core/assert.lua
Normal file
@@ -0,0 +1,105 @@
|
||||
-- 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
|
||||
175
sys/apis/jumper/core/bheap.lua
Normal file
175
sys/apis/jumper/core/bheap.lua
Normal file
@@ -0,0 +1,175 @@
|
||||
--- A light implementation of Binary heaps data structure.
|
||||
-- While running a search, some search algorithms (Astar, Dijkstra, Jump Point Search) have to maintains
|
||||
-- a list of nodes called __open list__. Retrieve from this list the lowest cost node can be quite slow,
|
||||
-- as it normally requires to skim through the full set of nodes stored in this list. This becomes a real
|
||||
-- problem especially when dozens of nodes are being processed (on large maps).
|
||||
--
|
||||
-- The current module implements a <a href="http://www.policyalmanac.org/games/binaryHeaps.htm">binary heap</a>
|
||||
-- data structure, from which the search algorithm will instantiate an open list, and cache the nodes being
|
||||
-- examined during a search. As such, retrieving the lower-cost node is faster and globally makes the search end
|
||||
-- up quickly.
|
||||
--
|
||||
-- This module is internally used by the library on purpose.
|
||||
-- It should normally not be used explicitely, yet it remains fully accessible.
|
||||
--
|
||||
|
||||
--[[
|
||||
Notes:
|
||||
This lighter implementation of binary heaps, based on :
|
||||
https://github.com/Yonaba/Binary-Heaps
|
||||
--]]
|
||||
|
||||
if (...) then
|
||||
|
||||
-- Dependency
|
||||
local Utils = require((...):gsub('%.bheap$','.utils'))
|
||||
|
||||
-- Local reference
|
||||
local floor = math.floor
|
||||
|
||||
-- Default comparison function
|
||||
local function f_min(a,b) return a < b end
|
||||
|
||||
-- Percolates up
|
||||
local function percolate_up(heap, index)
|
||||
if index == 1 then return end
|
||||
local pIndex
|
||||
if index <= 1 then return end
|
||||
if index%2 == 0 then
|
||||
pIndex = index/2
|
||||
else pIndex = (index-1)/2
|
||||
end
|
||||
if not heap._sort(heap._heap[pIndex], heap._heap[index]) then
|
||||
heap._heap[pIndex], heap._heap[index] =
|
||||
heap._heap[index], heap._heap[pIndex]
|
||||
percolate_up(heap, pIndex)
|
||||
end
|
||||
end
|
||||
|
||||
-- Percolates down
|
||||
local function percolate_down(heap,index)
|
||||
local lfIndex,rtIndex,minIndex
|
||||
lfIndex = 2*index
|
||||
rtIndex = lfIndex + 1
|
||||
if rtIndex > heap._size then
|
||||
if lfIndex > heap._size then return
|
||||
else minIndex = lfIndex end
|
||||
else
|
||||
if heap._sort(heap._heap[lfIndex],heap._heap[rtIndex]) then
|
||||
minIndex = lfIndex
|
||||
else
|
||||
minIndex = rtIndex
|
||||
end
|
||||
end
|
||||
if not heap._sort(heap._heap[index],heap._heap[minIndex]) then
|
||||
heap._heap[index],heap._heap[minIndex] = heap._heap[minIndex],heap._heap[index]
|
||||
percolate_down(heap,minIndex)
|
||||
end
|
||||
end
|
||||
|
||||
-- Produces a new heap
|
||||
local function newHeap(template,comp)
|
||||
return setmetatable({_heap = {},
|
||||
_sort = comp or f_min, _size = 0},
|
||||
template)
|
||||
end
|
||||
|
||||
|
||||
--- The `heap` class.<br/>
|
||||
-- This class is callable.
|
||||
-- _Therefore,_ <code>heap(...)</code> _is used to instantiate new heaps_.
|
||||
-- @type heap
|
||||
local heap = setmetatable({},
|
||||
{__call = function(self,...)
|
||||
return newHeap(self,...)
|
||||
end})
|
||||
heap.__index = heap
|
||||
|
||||
--- Checks if a `heap` is empty
|
||||
-- @class function
|
||||
-- @treturn bool __true__ of no item is queued in the heap, __false__ otherwise
|
||||
-- @usage
|
||||
-- if myHeap:empty() then
|
||||
-- print('Heap is empty!')
|
||||
-- end
|
||||
function heap:empty()
|
||||
return (self._size==0)
|
||||
end
|
||||
|
||||
--- Clears the `heap` (removes all items queued in the heap)
|
||||
-- @class function
|
||||
-- @treturn heap self (the calling `heap` itself, can be chained)
|
||||
-- @usage myHeap:clear()
|
||||
function heap:clear()
|
||||
self._heap = {}
|
||||
self._size = 0
|
||||
self._sort = self._sort or f_min
|
||||
return self
|
||||
end
|
||||
|
||||
--- Adds a new item in the `heap`
|
||||
-- @class function
|
||||
-- @tparam value item a new value to be queued in the heap
|
||||
-- @treturn heap self (the calling `heap` itself, can be chained)
|
||||
-- @usage
|
||||
-- myHeap:push(1)
|
||||
-- -- or, with chaining
|
||||
-- myHeap:push(1):push(2):push(4)
|
||||
function heap:push(item)
|
||||
if item then
|
||||
self._size = self._size + 1
|
||||
self._heap[self._size] = item
|
||||
percolate_up(self, self._size)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
--- Pops from the `heap`.
|
||||
-- Removes and returns the lowest cost item (with respect to the comparison function being used) from the `heap`.
|
||||
-- @class function
|
||||
-- @treturn value a value previously pushed into the heap
|
||||
-- @usage
|
||||
-- while not myHeap:empty() do
|
||||
-- local lowestValue = myHeap:pop()
|
||||
-- ...
|
||||
-- end
|
||||
function heap:pop()
|
||||
local root
|
||||
if self._size > 0 then
|
||||
root = self._heap[1]
|
||||
self._heap[1] = self._heap[self._size]
|
||||
self._heap[self._size] = nil
|
||||
self._size = self._size-1
|
||||
if self._size>1 then
|
||||
percolate_down(self, 1)
|
||||
end
|
||||
end
|
||||
return root
|
||||
end
|
||||
|
||||
--- Restores the `heap` property.
|
||||
-- Reorders the `heap` with respect to the comparison function being used.
|
||||
-- When given argument __item__ (a value existing in the `heap`), will sort from that very item in the `heap`.
|
||||
-- Otherwise, the whole `heap` will be cheacked.
|
||||
-- @class function
|
||||
-- @tparam[opt] value item the modified value
|
||||
-- @treturn heap self (the calling `heap` itself, can be chained)
|
||||
-- @usage myHeap:heapify()
|
||||
function heap:heapify(item)
|
||||
if self._size == 0 then return end
|
||||
if item then
|
||||
local i = Utils.indexOf(self._heap,item)
|
||||
if i then
|
||||
percolate_down(self, i)
|
||||
percolate_up(self, i)
|
||||
end
|
||||
return
|
||||
end
|
||||
for i = floor(self._size/2),1,-1 do
|
||||
percolate_down(self,i)
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
return heap
|
||||
end
|
||||
98
sys/apis/jumper/core/heuristics.lua
Normal file
98
sys/apis/jumper/core/heuristics.lua
Normal file
@@ -0,0 +1,98 @@
|
||||
--- 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
|
||||
32
sys/apis/jumper/core/lookuptable.lua
Normal file
32
sys/apis/jumper/core/lookuptable.lua
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
100
sys/apis/jumper/core/node.lua
Normal file
100
sys/apis/jumper/core/node.lua
Normal file
@@ -0,0 +1,100 @@
|
||||
--- The Node class.
|
||||
-- The `node` represents a cell (or a tile) on a collision map. Basically, for each single cell (tile)
|
||||
-- in the collision map passed-in upon initialization, a `node` object will be generated
|
||||
-- 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
|
||||
-- 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>.
|
||||
-- @type Node
|
||||
local Node = {}
|
||||
Node.__index = Node
|
||||
|
||||
--- Inits a new `node`
|
||||
-- @class function
|
||||
-- @tparam int x the x-coordinate of the node on the collision map
|
||||
-- @tparam int y the y-coordinate of the node on the collision map
|
||||
-- @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)
|
||||
end
|
||||
|
||||
-- Enables the use of operator '<' to compare nodes.
|
||||
-- Will be used to sort a collection of nodes in a binary heap on the basis of their F-cost
|
||||
function Node.__lt(A,B) return (A._f < B._f) end
|
||||
|
||||
--- Returns x-coordinate of a `node`
|
||||
-- @class function
|
||||
-- @treturn number the x-coordinate of the `node`
|
||||
-- @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()
|
||||
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()
|
||||
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.
|
||||
-- @class function
|
||||
-- @treturn node self (the calling `node` itself, can be chained)
|
||||
-- @usage
|
||||
-- local thisNode = Node(1,2)
|
||||
-- thisNode:reset()
|
||||
function Node:reset()
|
||||
self._g, self._h, self._f = nil, nil, nil
|
||||
self._opened, self._closed, self._parent = nil, nil, nil
|
||||
return self
|
||||
end
|
||||
|
||||
return setmetatable(Node,
|
||||
{__call = function(self,...)
|
||||
return Node:new(...)
|
||||
end}
|
||||
)
|
||||
end
|
||||
201
sys/apis/jumper/core/path.lua
Normal file
201
sys/apis/jumper/core/path.lua
Normal file
@@ -0,0 +1,201 @@
|
||||
--- The Path class.
|
||||
-- The `path` class is a structure which represents a path (ordered set of nodes) from a start location to a goal.
|
||||
-- An instance from this class would be a result of a request addressed to `Pathfinder:getPath`.
|
||||
--
|
||||
-- This module is internally used by the library on purpose.
|
||||
-- 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>.
|
||||
-- @type Path
|
||||
local Path = {}
|
||||
Path.__index = Path
|
||||
|
||||
--- Inits a new `path`.
|
||||
-- @class function
|
||||
-- @treturn path a `path`
|
||||
-- @usage local p = Path()
|
||||
function Path:new()
|
||||
return setmetatable({_nodes = {}}, Path)
|
||||
end
|
||||
|
||||
--- Iterates on each single `node` along a `path`. At each step of iteration,
|
||||
-- returns the `node` plus a count value. Aliased as @{Path:nodes}
|
||||
-- @class function
|
||||
-- @treturn node a `node`
|
||||
-- @treturn int the count for the number of nodes
|
||||
-- @see Path:nodes
|
||||
-- @usage
|
||||
-- for node, count in p:iter() do
|
||||
-- ...
|
||||
-- end
|
||||
function Path:iter()
|
||||
local i,pathLen = 1,#self._nodes
|
||||
return function()
|
||||
if self._nodes[i] then
|
||||
i = i+1
|
||||
return self._nodes[i-1],i-1
|
||||
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,...)
|
||||
return Path:new(...)
|
||||
end
|
||||
})
|
||||
end
|
||||
168
sys/apis/jumper/core/utils.lua
Normal file
168
sys/apis/jumper/core/utils.lua
Normal file
@@ -0,0 +1,168 @@
|
||||
-- Various utilities for Jumper top-level modules
|
||||
|
||||
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
|
||||
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()
|
||||
path._grid = finder._grid
|
||||
while true do
|
||||
if node._parent then
|
||||
t_insert(path._nodes,1,node)
|
||||
node = node._parent
|
||||
else
|
||||
t_insert(path._nodes,1,startNode)
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Lookup for value in a table
|
||||
local indexOf = function(t,v)
|
||||
for i = 1,#t do
|
||||
if t[i] == v then return i end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Is i out of range
|
||||
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
|
||||
}
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user