Skip to content

Add option to specify wall value #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions astar.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ var astar = {
path to the closest node if the target is unreachable.
* @param {Function} [options.heuristic] Heuristic function (see
* astar.heuristics).
* @param {Number} [options.isWall] Specifies a wall value with
* an initial / undefined value of 0.
*/
search: function(graph, start, end, options) {
graph.cleanDirty();
options = options || {};
var heuristic = options.heuristic || astar.heuristics.manhattan;
var closest = options.closest || false;
var isWall = options.isWall || 0;

var openHeap = getHeap();
var closestNode = start; // set the start node to be the closest if required
Expand Down Expand Up @@ -78,7 +81,7 @@ var astar = {
for (var i = 0, il = neighbors.length; i < il; ++i) {
var neighbor = neighbors[i];

if (neighbor.closed || neighbor.isWall()) {
if (neighbor.closed || neighbor.isWall(isWall)) {
// Not a valid node to process, skip to next neighbor.
continue;
}
Expand Down Expand Up @@ -272,8 +275,8 @@ GridNode.prototype.getCost = function(fromNeighbor) {
return this.weight;
};

GridNode.prototype.isWall = function() {
return this.weight === 0;
GridNode.prototype.isWall = function(isWall) {
return this.weight === isWall;
};

function BinaryHeap(scoreFunction) {
Expand Down Expand Up @@ -401,4 +404,4 @@ return {
Graph: Graph
};

});
});