Pathfinding algorithms
The ROT.Path
namespace provides utils for pathfinding: retrieving a set of coordinates represeting shortest route between two points. All pathfinding algorithms share the same usage paradigm:
- Create an input callback; function that returns passability info (true/false) for a given coordinates
- Instantialize the pathfinder; pass the values of
targetX
, targetY
and inputCallback
to the constructor
- Fourth constructor option is a configuration object; so far, only one key is supported:
topology
(values 4/6/8)
- Call the
compute
method with three arguments: sourceX
, sourceY
and outputCallback
The pathfinder will periodically call your outputCallback
; once for every cell in the path from [sourceX, sourceY]
to [targetX, targetY]
.
If no such path exists, outputCallback
will never be called.
NOTE: you can call the compute
method multiple times
(which is especially useful, performance-wise, when using ROT.Path.Dijkstra
),
but you must make sure that the passability information of the map never changes once
the pathfinder is created.
Dijkstra's algorithm
Simplified version of Dijkstra's algorithm: all edges have a length of 1.
var w = 150, h = 80;
ROT.RNG.setSeed(12345);
var display = new ROT.Display({width:w, height:h, fontSize:6});
SHOW(display.getContainer());
/* generate map and store its data */
var data = {};
var map = new ROT.Map.Uniform(w, h);
map.create(function(x, y, value) {
data[x+","+y] = value;
display.DEBUG(x, y, value);
});
/* input callback informs about map structure */
var passableCallback = function(x, y) {
return (data[x+","+y] === 0);
}
/* prepare path to given coords */
var dijkstra = new ROT.Path.Dijkstra(98, 38, passableCallback);
/* compute from given coords #1 */
dijkstra.compute(8, 45, function(x, y) {
display.draw(x, y, "", "", "#800");
});
/* compute from given coords #2 */
dijkstra.compute(130, 8, function(x, y) {
display.draw(x, y, "", "", "#800");
});
/* highlight */
display.draw(8, 45, "", "", "#3f3");
display.draw(130, 8, "", "", "#3f3");
display.draw(98, 38, "", "", "#f33");
A* algorithm
Simplified version of A* algorithm: all edges have a length of 1.
var w = 150, h = 80;
ROT.RNG.setSeed(12345);
var display = new ROT.Display({width:w, height:h, fontSize:6});
SHOW(display.getContainer());
/* generate map and store its data */
var data = {};
var map = new ROT.Map.Uniform(w, h);
map.create(function(x, y, value) {
data[x+","+y] = value;
display.DEBUG(x, y, value);
});
/* input callback informs about map structure */
var passableCallback = function(x, y) {
return (data[x+","+y] === 0);
}
/* prepare path to given coords */
var astar = new ROT.Path.AStar(98, 38, passableCallback);
/* compute from given coords #1 */
astar.compute(8, 45, function(x, y) {
display.draw(x, y, "", "", "#800");
});
/* compute from given coords #2 */
astar.compute(130, 8, function(x, y) {
display.draw(x, y, "", "", "#800");
});
/* highlight */
display.draw(8, 45, "", "", "#3f3");
display.draw(130, 8, "", "", "#3f3");
display.draw(98, 38, "", "", "#f33");