Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TeeTeeHaa opened this issue Aug 9, 2021 · 1 comment

Comments

@TeeTeeHaa
Copy link

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.

@Matt-Maerz
Copy link

Matt-Maerz commented Oct 2, 2021

is there an update in progress?
or a workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants