Skip to content
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
12 changes: 7 additions & 5 deletions src/path/astar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ ROT.Path.AStar.extend(ROT.Path);
/**
* Compute a path from a given point
* @see ROT.Path#compute
* @param {function} weightingCallback Will be called for every path item and used to add weight to decisions - returning 0 means no difficulty, higher numbers add negative votes to taking a path through that cell
*/
ROT.Path.AStar.prototype.compute = function(fromX, fromY, callback) {
ROT.Path.AStar.prototype.compute = function(fromX, fromY, callback, weightingCallback) {
this._todo = [];
this._done = {};
this._fromX = fromX;
this._fromY = fromY;
this._add(this._toX, this._toY, null);
weightingCallback = weightingCallback || function(){return 0};
this._add(this._toX, this._toY, null, weightingCallback);

while (this._todo.length) {
var item = this._todo.shift();
Expand All @@ -35,7 +37,7 @@ ROT.Path.AStar.prototype.compute = function(fromX, fromY, callback) {
var y = neighbor[1];
var id = x+","+y;
if (id in this._done) { continue; }
this._add(x, y, item);
this._add(x, y, item, weightingCallback);
}
}

Expand All @@ -48,13 +50,13 @@ ROT.Path.AStar.prototype.compute = function(fromX, fromY, callback) {
}
}

ROT.Path.AStar.prototype._add = function(x, y, prev) {
ROT.Path.AStar.prototype._add = function(x, y, prev, weightingCallback) {
var obj = {
x: x,
y: y,
prev: prev,
g: (prev ? prev.g+1 : 0),
h: this._distance(x, y)
h: this._distance(x, y) + weightingCallback(x, y)
}
this._done[x+","+y] = obj;

Expand Down