Open boneskull opened 11 years ago
It occurs to me the pathfinder doesn't make diagonal paths...heh
At any rate playing more with this it seems we'll need to transpose the map, or something, because pathfinding becomes confused about what tiles are where...
I think there may be an issue with the algorithm. I was able to use the algorithm from https://github.com/bgrins/javascript-astar to get my pathfinding working with proper condition evaluation.
This algorithm is synchronous of course and while it's fine for finding paths in say a 9x9 grid, I imagine there are performance issues when applied to larger maps.
Hello Chris, I thought about limiting the range of the path finding as well but wanted to add it it to the astar code. The submap creates a lot of new object instances for each search, you also need an astar instance for each search.
The easiest way to add a range limit in the current code is probably in the function: "this._valid = function (tileX, tileY)" which cuts off all tiles at a certain distance from the start position and limits the search...
I don't have a fully working example of this yet, but this is the idea. AStar takes a map and will find paths. However, we can significantly increase performance if we only want to find a path within a subset of that map.
Example:
The ground is made of impassable and passable tiles. I have a character on a tile. His range is 4; he can only move four tiles in any direction (up/down/left/right/diagonal). Some subset of these tiles may be impassable, and thus it may be impossible for the character to reach a tile within 4 spaces, because the path to get there may be longer than the range.
I need to compute these paths. But there's no need to search the whole map to get them, since the character only has a range*2+1 size square to possibly move within.
Solution is below, but it's not quite ideal and I'm unsure the best way to get around it. findPathInMap() returns an AStar instance, and you have to handle these manually (and call update() on them during ticks) in order for the paths to actually get found. Plus there's obviously some translation that needs to map the coordinates in the original map to the new, smaller map.
Another solution I was considering was another condition type where you could constrain the condition to different shapes. For example, you could say "this tile must be in group x AND the tile must be within rect y". How does that sound?
Usage is something like this:
Anyway, here's the current diff. A side-effect is that the fromJSON() method now allows you to take slices of it, which may or may not be useful elsewhere: