easystarjs icon indicating copy to clipboard operation
easystarjs copied to clipboard

modifying grid values between setGrid and and findPath/calculate causes wrong results

Open TeeTeeHaa opened this issue 4 years ago • 1 comments
trafficstars

If the values in the grid are modified between the function easystar.setGrid(grid); and the functions easystar.findPath(...); easystar.calculate(); a "valid but not shortest" path is found.

Code Example

Using node.js:

var easystarjs = require('easystarjs');
var easystar = new easystarjs.js();

var grid = [[1,1,1,1,1],
            [1,1,1,1,1],
            [1,1,1,1,1],
            [1,1,1,1,1],
            [1,1,1,1,1]];

console.log(JSON.stringify(grid));

easystar.setGrid(grid);

easystar.setAcceptableTiles(0);

for (let y = 0; y < grid.length; ++y) {
  for (let x = 0; x < grid[y].length; ++x) {
    grid[y][x] = 0;
  }
}
grid[2][2] = 1

console.log(JSON.stringify(grid));

// enabling the following line makes it work correctly: 
// easystar.setGrid(grid);

easystar.findPath(2, 3, 1, 2, function (path) {
  if (path === null) {
    console.log("Path was not found.");
  } else {
    console.log("Path of length %d was found.", path.length);
    console.log(JSON.stringify(path));
  }
});

easystar.calculate();

Expected Result

A path of length 3:

[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]
[[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0],[0,0,0,0,0]]
Path of length 3 was found.
[{"x":2,"y":3},{"x":1,"y":3},{"x":1,"y":2}]

Actual Result

A path of length 7 - which is valid but not the shortest path:

[[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,1,1,1,1]]
[[0,0,0,0,0],[0,0,0,0,0],[0,0,1,0,0],[0,0,0,0,0],[0,0,0,0,0]]
Path of length 7 was found.
[{"x":2,"y":3},{"x":2,"y":4},{"x":1,"y":4},{"x":0,"y":4},{"x":0,"y":3},{"x":0,"y":2},{"x":1,"y":2}]

Additional Information

easystar.js: 0.4.4 node.js: 12.19.0 os: Windows 7 64 Bit

P.S.

Nice library, very easy to use, few dependencies, perfect for my use case - except this little annoyance.

TeeTeeHaa avatar Aug 09 '21 12:08 TeeTeeHaa

is there an update in progress? or a workaround?

Matt-Maerz avatar Oct 02 '21 10:10 Matt-Maerz