Created
June 14, 2018 11:09
-
-
Save gogoprog/c5815c0bbc5e12aa5c96cb7a51ef6ba7 to your computer and use it in GitHub Desktop.
Jumper sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Grid = require ("jumper.grid") | |
local Pathfinder = require ("jumper.pathfinder") | |
local map = { | |
{0,0,8,8,0,8,0,0}, | |
{0,0,9,9,8,0,0,0}, | |
{0,0,0,0,0,0,0,0}, | |
{0,0,0,0,0,0,0,0}, | |
{0,0,0,0,0,0,0,0}, | |
{0,0,0,0,0,0,0,0} | |
} | |
local walkable = function(v) return v ~= 9 end | |
local grid = Grid(map) | |
local myFinder = Pathfinder(grid, 'ASTAR', walkable) | |
function computeDistance( x1, y1, x2, y2 ) | |
return math.sqrt( (x2-x1)^2 + (y2-y1)^2 ) | |
end | |
local h = function(nodeA, nodeB) | |
local x1 = nodeA:getX() | |
local x2 = nodeB:getX() | |
local y1 = nodeA:getY() | |
local y2 = nodeB:getY() | |
local distance = computeDistance(x1,y1,x2,y2) | |
local factor = map[y1][x1] + map[y2][x2] | |
return distance + factor | |
end | |
myFinder:setHeuristic(h) | |
local p = myFinder:getPath(1,1, 8,1) | |
for node, count in p:nodes() do | |
print(('%d. Node(x: %d, y: %d)'):format(count, node:getX(), node:getY())) | |
end | |
print(('Path length: %.2f'):format(p:getLength())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment