Skip to content

Commit

Permalink
Merge pull request #91 from nluqo/master
Browse files Browse the repository at this point in the history
Adding tiebreaking to A*
  • Loading branch information
ondras committed Apr 4, 2016
2 parents 3513ddb + 2e4b632 commit e58e99f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 5 additions & 3 deletions rot.js
@@ -1,6 +1,6 @@
/*
This is rot.js, the ROguelike Toolkit in JavaScript.
Version 0.6~dev, generated on Thu Mar 3 16:27:15 PST 2016.
Version 0.6~dev, generated on Thu Mar 31 23:30:04 EDT 2016.
*/
/**
* @namespace Top-level ROT namespace
Expand Down Expand Up @@ -5276,12 +5276,13 @@ ROT.Path.AStar.prototype.compute = function(fromX, fromY, callback) {
}

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

Expand All @@ -5290,7 +5291,8 @@ ROT.Path.AStar.prototype._add = function(x, y, prev) {
var f = obj.g + obj.h;
for (var i=0;i<this._todo.length;i++) {
var item = this._todo[i];
if (f < item.g + item.h) {
var itemF = item.g + item.h;
if (f < itemF || (f == itemF && h < item.h)) {
this._todo.splice(i, 0, obj);
return;
}
Expand Down
6 changes: 4 additions & 2 deletions src/path/astar.js
Expand Up @@ -49,12 +49,13 @@ ROT.Path.AStar.prototype.compute = function(fromX, fromY, callback) {
}

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

Expand All @@ -63,7 +64,8 @@ ROT.Path.AStar.prototype._add = function(x, y, prev) {
var f = obj.g + obj.h;
for (var i=0;i<this._todo.length;i++) {
var item = this._todo[i];
if (f < item.g + item.h) {
var itemF = item.g + item.h;
if (f < itemF || (f == itemF && h < item.h)) {
this._todo.splice(i, 0, obj);
return;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/spec/path.js
Expand Up @@ -59,9 +59,13 @@ describe("Path", function() {
if (x<0 || y<0 || x>=MAP6.length || y>=MAP6[0].length) { return false; }
return (MAP6[x][y] == 0);
}

var VISITS = 0;
var PASSABLE_CALLBACK_VISIT = function(x, y) { VISITS++; return true; }

beforeEach(function() {
PATH = [];
VISITS = 0;
});


Expand Down Expand Up @@ -151,6 +155,12 @@ describe("Path", function() {
astar.compute(X[0], X[1], PATH_CALLBACK);
expect(PATH.length).toEqual(0);
});

it("should efficiently compute path",function(){
var open_astar = new ROT.Path.AStar(0,0, PASSABLE_CALLBACK_VISIT);
open_astar.compute(50,0, PATH_CALLBACK);
expect(VISITS).toEqual(400);
});
}); /* 8-topology */

describe("4-topology", function() {
Expand Down

0 comments on commit e58e99f

Please sign in to comment.