lychees's picture
Upload 569 files
87b3b3a
/**
* @class Simplified Dijkstra's algorithm: all edges have a value of 1
* @augments ROT.Path
* @see ROT.Path
*/
ROT.Path.Dijkstra = function(toX, toY, passableCallback, options) {
ROT.Path.call(this, toX, toY, passableCallback, options);
this._computed = {};
this._todo = [];
this._add(toX, toY, null);
}
ROT.Path.Dijkstra.extend(ROT.Path);
/**
* Compute a path from a given point
* @see ROT.Path#compute
*/
ROT.Path.Dijkstra.prototype.compute = function(fromX, fromY, callback) {
var key = fromX+","+fromY;
if (!(key in this._computed)) { this._compute(fromX, fromY); }
if (!(key in this._computed)) { return; }
var item = this._computed[key];
while (item) {
callback(item.x, item.y);
item = item.prev;
}
}
/**
* Compute a non-cached value
*/
ROT.Path.Dijkstra.prototype._compute = function(fromX, fromY) {
while (this._todo.length) {
var item = this._todo.shift();
if (item.x == fromX && item.y == fromY) { return; }
var neighbors = this._getNeighbors(item.x, item.y);
for (var i=0;i<neighbors.length;i++) {
var neighbor = neighbors[i];
var x = neighbor[0];
var y = neighbor[1];
var id = x+","+y;
if (id in this._computed) { continue; } /* already done */
this._add(x, y, item);
}
}
}
ROT.Path.Dijkstra.prototype._add = function(x, y, prev) {
var obj = {
x: x,
y: y,
prev: prev
}
this._computed[x+","+y] = obj;
this._todo.push(obj);
}