I am trying to achieve 2nd, preferably nth shortest path using the A* pathfinding algorithm. I have implemented the shortest path already:
while(open.length > 0) {
max = worldSize;
min = -1;
for(i in open) {
if(open[i].f < max) {
max = open[i].f;
min = i;
}
}
node = open.splice(min, 1)[0];
if(node.value === nodeEnd.value) {
path = closed[closed.push(node)-1];
do {
result.push({x: path.x, y:path.y});
} while(path = path.parent);
open = closed = astar = [];
result.reverse();
} else {
neighbors = findNeighbors(node.x, node.y);
for(i = 0; i < neighbors.length; ++i) {
path = newNode(node, neighbors[i]);
if(!astar[path.value]) {
path.g = node.g + manhattanDistance(neighbors[i], node);
path.f = path.g + manhattanDistance(neighbors[i], nodeEnd);
open.push(path);
astar[path.value] = true;
}
}
closed.push(node);
}
}
What can I do? I have zero experience in this and don't even understand the algorithm to its fullest (still researching at the moment). Thank you.