Skip to content

Commit 2028fcc

Browse files
author
Daniel
committed
If diagonal is disabled and a tile has no non-diagonal neighbours, force allow diagonal as a fall-back
1 parent e93574b commit 2028fcc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

astar.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ var astar = {
9494
}
9595
var neighbors = astar.neighbors(grid, currentNode, diagonalThisNode);
9696

97+
var validNeighborCount = 0;
98+
for(var i in neighbors) {
99+
var neighbor = neighbors[i];
100+
if (!neighbor.closed && !neighbor.isWall() && !beenVisited) {
101+
validNeighborCount++;
102+
}
103+
}
104+
105+
//This will help us find paths that are mostly not diagonal, but may have node that diagonal is the only option
106+
if ((validNeighborCount == 0) && (diagonal == false)) {
107+
diagonalThisNode = (currentNode.options.diagonal != null) ? currentNode.options.diagonal : true;
108+
if (diagonalThisNode) {
109+
neighbors = astar.neighbors(grid, currentNode, true);
110+
}
111+
}
112+
97113
for(var i=0, il = neighbors.length; i < il; i++) {
98114
var neighbor = neighbors[i];
99115

0 commit comments

Comments
 (0)