From 2e4b63250d933dc87628e77e5daed7c5bb53df60 Mon Sep 17 00:00:00 2001 From: Jeremiah Reid Date: Thu, 31 Mar 2016 23:36:10 -0400 Subject: [PATCH] Adding tiebreaking to A* --- rot.js | 8 +++++--- src/path/astar.js | 6 ++++-- tests/spec/path.js | 10 ++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/rot.js b/rot.js index 72311f12..001c17ef 100644 --- a/rot.js +++ b/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 @@ -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; @@ -5290,7 +5291,8 @@ ROT.Path.AStar.prototype._add = function(x, y, prev) { var f = obj.g + obj.h; for (var i=0;i=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; }); @@ -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() {