I’ve implemented flood fill algorithm that takes array[15*15] with path and generates a queue of steps he took while filling the path. tl;dr it looks like this
std::queue <int> f_path;
void Enemy::find_path(int *map, int *grid, int node) {
if (grid[node] == 1) // colored-grid
return;
if (map[node] == 0) // grid with defined map (0 - no path, 1 path)
return;
f_path.push(node);
grid[node] = 1;
if ((node + 1) % 15 != 0) this->find_path(map, grid, node + 1); // go right
if (node % 15 != 0) this->find_path(map, grid, node - 1); // go left
if (node - 15 > 0) this->find_path(map, grid, node - 15); // go up
if (node + 15 < 15*15) this->find_path(map, grid, node + 15); // go down
}
But now I have a queue of steps it took to fill the grid, but I don’t know how to apply this information for my objects to follow and get from start to end. I mean, it’s simple with one path, but if it splits like this (9 is exit):
0 0 1 0 0
0 1 1 1 0
0 1 0 1 0
0 1 1 1 0
0 0 9 0 0
I will have both left and right path in queue so if I do a simple go(f_path.front()) it will do god’s know what. How do I filter it, so it only goes to exit and then stops? I can’t wrap my head arround it.
Deith, I will say you’re on the right track!
Right now your code will simply iterate through everything and get nowhere. You forgot a couple things.
First,
Enemy::find_pathhas to return a boolean: whether or not it reached the destination. So, at the top you haveI noticed the first one is to keep it from backtracking over itself.
The second one is pretty clear: It hits a wall. Therefore, have
return falseafter it to show it reached a dead-end. But you need a third so that, if it reaches its destination, it returns true.Then, when you call the four-way iterations, test if they return
true. If they do thenreturn trueagain since the goal was reached, thus searching, finding, and zipping back to the source!Do note that zipping back to the source part, because that’s the part you can use. Right now your queue will fill up with random junk going everywhere. It doesn’t empty after it goes the wrong way. However, that zip-back part is perfect – instead of a queue, use a stack, and once you reach the destination, push each node when zipping back onto the stack, thus providing a full path from beginning to end!
Hope I could help 😉
EDIT: Ok, so there is one more important thing I must mention: working, but inefficient paths.
Your algorithm will find a path, but not always the shortest path – in fact sometimes it’ll even find a really long path. Fortunately to fix this is somewhat simple. You need to have the coordinate of the destination, first. Then, at each iteration of the
find_pathfunction, reorder your direction iterators. First you choose right, then left, then up, then down. Instead, see which direction will head in the direction of the goal. Then check that direction. If it fails try the next closest. If that fails, then try the next closest, and the next closest (well, now the farthest).Yes, I know this will not be the shortest distance, since it generally tends toward wall-hugging, but it’s definitely better than going in completely random directions.