Advertisement

Grid pathfinding with a lot of entities

Started by June 12, 2012 12:08 PM
10 comments, last by Dario Oliveri 12 years, 4 months ago
I'd like to explain this problem with a screenshot from a released game, DROD: Gunthro's Epic Blunder, by Caravel Games

uKepl.png

The game is turn-based and tile-based.

I'm trying to create something very similar (a clone of the game), and I've got most of the fundamentals done, but I'm having trouble implementing pathfinding.

Look at the screenshot. The guys in yellow are friendly, and want to kill the roaches.
Every turn, every guy in yellow pathfinds to the closest roach, and every roach pathfinds to the closest guy in yellow.

By closest I mean the target with the shortest path, not a simple distance calculation.

All of this without any kind of slowdown when loading the level or when passing turns.
And all of the entities change position every turn. Also (not shown in screenshot), there can be doors that open and close and change the level's layout.

Impressive.

---

I've tried implementing pathfinding in my clone.

First attempt was making every roach find a path to a yellow guy every turn, using a breadth-first search algorithm. Obviously incredibly slow with more than a single roach, and would get exponentially slower with more than a single yellow guy.

Second attempt was mas making every yellow guy generate a pathmap (still breadth-first search) every time he moved. Worked perfectly with multiple roaches and a single yellow guy, but adding more yellow guys made the game slow and unplayable.

Last attempt was implementing JPS (jump point search). Every entity would individually calculate a path to its target. Fast, but with a limited number of entities. Having less than half the entities in the screenshot would make the game slow. And also, I had to get the "closest" enemy by calculating distance, not shortest path.

---

I've asked on the DROD forums how they did it, and a user replied that it was breadth-first search. The game is open source, and I took a look at the [source code][3], but it's C++ (I'm using C#) and I found it confusing.

I don't know how to do it. Every approach I tried isn't good enough. And I believe that DROD generates global pathmaps, somehow, but I can't understand how every entity find the best individual path to other entities that move every turn.

What's the trick?

---

This is a reply I just got on the DROD forums:



> Without having looked at the code I'd wager it's two (or so) pathmaps
> for the whole room:
>
> One to the nearest enemy, and one to the nearest friendly for every
> tile.
>
> There's no need to make a separate pathmap for every entity when the
> overall goal is "move towards nearest enemy/friendly"... just mark
> every tile with the number of moves it takes to the nearest target and
> have the entity chose the move that takes it to the tile with the
> lowest number.[/quote]

To be honest, I don't understand it that well.
You reuse the first part of the algorithm that for each tile decide the distance to the closest target. Then you only need to let everyone walk to the neighbouring tile with the shortest remaining distance.

The X's are the targets and the numbers are distances.

2 1 X 1 1 2
3 2 1 1 X 1
4 3 2 2 1 2
5 4 3 3 2 3
Advertisement
So I should have a pathmap for friendly units and a pathmap for enemy units? And to check distances, I should run a breadth-first search from every tile, every turn? Wouldn't that be slow?
pathmaptest.png

This is a quick mockup I did.

The top-left pathmap is a breadth-first search for one of the enemies (in red).
The bottom-left one is a breadth-first search for the other enemy.
The pathmap on the top-right is the sum of the two searches.

Is this what you meant?

I see some problems with this, though... the paths created are not correct, there are many equal numbers and that could lead to strange behavior.
Also, considering I can have 200+ entities in a single room, this means I would have to run 200 breadth-first searches and sum all of them togheter.

Sounds slow..

The top-left pathmap is a breadth-first search for one of the enemies (in red).
The bottom-left one is a breadth-first search for the other enemy.
The pathmap on the top-right is the sum of the two searches.

Is this what you meant?

That's not right. It's possible to do with just a single search; you just have to seed the open-list with all the enemies, what you've done there is to do many searches with an open-list seeded with just 1 entity each time. It's a variant of breadth-first search called Dijkstra's Algorithm.

The free cells immediately surrounding any entity should have a distance score of 1. Take a closer look at Dawoodoz's example.

[quote name='SuperV1234' timestamp='1339529927' post='4948609']
The top-left pathmap is a breadth-first search for one of the enemies (in red).
The bottom-left one is a breadth-first search for the other enemy.
The pathmap on the top-right is the sum of the two searches.

Is this what you meant?

That's not right. It's possible to do with just a single search; you just have to seed the open-list with all the enemies, what you've done there is to do many searches with an open-list seeded with just 1 entity each time. It's a variant of breadth-first search called Dijkstra's Algorithm.
[/quote]

I'm very sorry, but I'm still not getting it :/

Let's say I have 100 enemies and 100 friendlies.
You're telling me that I can find the best possible path from every enemy to every friendly (and viceversa) with only two breadth-first searches?

Where do I have to start the searches at?
Advertisement

I'm very sorry, but I'm still not getting it :/

No worries, you'll kick yourself though because it's quite elegantly simple.

Let's say I have 100 enemies and 100 friendlies.
You're telling me that I can find the best possible path from every enemy to every friendly (and viceversa) with only two breadth-first searches?[/quote]
Not quite, but the best possible path from every enemy to every friendly isn't what you need. What you need is the best path for each enemy to its nearest friendly (and viceversa) and that can be done with just two search one for each type to generate your walkmaps.

Each cell in the walkmap represents the distance from that cell to an entity, so a unit sitting on that cell should examine the surrounding cells and pick one that is lower (and/or perhaps equal) to the value of the cell it is sat on.

Where do I have to start the searches at?[/quote]As you know a breadth-first search is based on a queue, you enqueue stuff to the back and dequeue stuff from the front.

To start the search you seed your queue with the cells of all entities (let's say all enemies) and mark them with a distance of 0.
Pop a cell off the front of the queue, add its surrounding neighbour cells to the back of the queue and mark each neighbour with a distance of 1+ whatever the central cell is marked as (e.g. 0+1 = 1), if you come across any neighbour cells that are already marked with a value then leave them alone (don't enqueue and don't change their value).
Loop until the queue is exhausted.
The cost of a distance transform decreases a little with more agents and targets: there are less empty walkable map cells to process and less propagation before convergence; if all cells are full, every unit is either gridlocked or adjacent to an enemy, with no propagation needed.
In fact, you might be able to prune useless propagation into empty areas by keeping a list of target cells (adjacent to non-gridlocked units, i.e. the potential "next steps" of some unit for which you need a valuation) and processing cells in order of increasing distance: if you would propagate a distance value that exceeds the current value for each target cell, you know it isn't going to contribute to pathfinding.

Omae Wa Mou Shindeiru

I´ve never done AI at this level befor, but how i would do it would be the folowing :

For each entity,
divide the map into blocks, lets say(32x32)
find the path in blocks, so it´s the same alogrithm, with less tiles.
when you find that path, send the blocks as idenpendet jobs to a multithread manager,
then you get the "local" path in each block. the output data is the blocks path.
on your main thread rebuild that path.

IF one enitity is the target of the entiys target, reuse that path.


i dont know if this make much sence in words ;P
"There will be major features. none to be thought of yet"
interesting problem!

I was thinking about this and how to fill in the 'walk map' and the algo I would try first is just loop thru the map, placing an incremented number next to any space which borders a non-zero space, loop until no more zero spaces

The entities which are surrounded by other entities (and thus can't move) are then not even checked, and if a space is reachable by two or more other spaces is only dealt with once! Seems like perhaps the speediest way to make the move maps. No need to use any pathfind algo at all!

This topic is closed to new replies.

Advertisement