2016-12-11 19:24:52 +00:00
|
|
|
-- Various utilities for Jumper top-level modules
|
|
|
|
|
|
|
|
if (...) then
|
|
|
|
|
|
|
|
-- Dependencies
|
|
|
|
local _PATH = (...):gsub('%.utils$','')
|
|
|
|
local Path = require (_PATH .. '.path')
|
|
|
|
|
|
|
|
-- Local references
|
|
|
|
local pairs = pairs
|
|
|
|
local t_insert = table.insert
|
|
|
|
|
|
|
|
-- Raw array items count
|
|
|
|
local function arraySize(t)
|
|
|
|
local count = 0
|
2017-10-26 22:56:55 +00:00
|
|
|
for _ in pairs(t) do
|
2016-12-11 19:24:52 +00:00
|
|
|
count = count+1
|
|
|
|
end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Extract a path from a given start/end position
|
2018-01-24 22:39:38 +00:00
|
|
|
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
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
-- 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
|
2018-01-24 22:39:38 +00:00
|
|
|
local function outOfRange(i,low,up)
|
|
|
|
return (i< low or i > up)
|
|
|
|
end
|
2017-10-26 22:56:55 +00:00
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
return {
|
|
|
|
arraySize = arraySize,
|
|
|
|
indexOf = indexOf,
|
|
|
|
outOfRange = outOfRange,
|
|
|
|
traceBackPath = traceBackPath
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|