I’ve been working on updating my A* Graph Search Algorithm (A* on Github) and toying with the idea of adding variable costs to the grid, also known as weighted paths.
I opened an issue to add weighting to graph nodes a while ago, but am a little stuck on the implementation details. My initial thought was to change the semantics of the graph. Right now, here is a sample graph:
var GraphNodeType = {
OPEN: 0,
WALL: 1
};
// 0 represents an open node, 1 represents a wall
var graph = new Graph([
[0,0,0,0],
[1,0,0,1],
[1,1,0,0]
]);
My initial plan for adding weighting was something like this:
var GraphNodeType = {
WALL: 0,
};
// Anything > 0 represents the cost to travel to that node, 0 represents a wall
var graph = new Graph([
[2,1,3,4],
[0,1,1,0],
[0,0,3,10]
]);
One major issue with that is that it breaks backwards compatibility with the plugin. A user, spellfork, on the Github issue came up with a neat solution, basically passing in two separate arrays to the graph function, where one represents the “obstacle map” (walls), and the second represents the “terrain map” (costs).
// First array: 0 represents an open node, 1 represents a wall
// Second array: movement costs are represented by numeric value
var graph = new Graph([
[0,0,0,0],
[1,0,0,1],
[1,1,0,0]
],[
[2,1,3,4],
[0,1,1,0],
[0,0,3,10]
]);
This is nice because the terrain map is optional, but not as nice if you need to do a lot of work to generate the two separate maps.
Maybe there is a better solution that I am not thinking of? Any ideas about the best way to implement this? Drop a comment here or over in the Github issue to help get this implemented!