Sign in to follow this  
VittorioRomeo

Grid pathfinding with a lot of entities

Recommended Posts

VittorioRomeo    405
I'd like to explain this problem with a screenshot from a released game, [url="http://caravelgames.com/Articles/Games_2/GatEB.html"]DROD: Gunthro's Epic Blunder, by Caravel Games[/url]

[img]http://i.stack.imgur.com/uKepl.png[/img]

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:


[quote]
> 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.

Share this post


Link to post
Share on other sites
Dawoodoz    461
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.
[code]
2 1 X 1 1 2
3 2 1 1 X 1
4 3 2 2 1 2
5 4 3 3 2 3
[/code]

Share this post


Link to post
Share on other sites
VittorioRomeo    405
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?

Share this post


Link to post
Share on other sites
VittorioRomeo    405
[img]https://dl.dropbox.com/u/3724424/pathmaptest.png[/img]

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..

Share this post


Link to post
Share on other sites
dmatter    4844
[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?[/quote]
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. Edited by dmatter

Share this post


Link to post
Share on other sites
VittorioRomeo    405
[quote name='dmatter' timestamp='1339530388' post='4948613']
[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?[/quote]
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?

Share this post


Link to post
Share on other sites
dmatter    4844
[quote name='SuperV1234' timestamp='1339530939' post='4948615']
I'm very sorry, but I'm still not getting it :/[/quote]
No worries, you'll kick yourself though because it's quite elegantly simple.

[quote]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.

[quote]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. Edited by dmatter

Share this post


Link to post
Share on other sites
LorenzoGatti    4450
The cost of a distance transform [i]decreases[/i] 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.

Share this post


Link to post
Share on other sites
Tordin    625
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

Share this post


Link to post
Share on other sites
leopardpm    162
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!

Share this post


Link to post
Share on other sites
leopardpm    162
actually, don't loop thru the 'map' - but rather generate an array of all unobstructed map spaces, looping thru that and updating it and the map with each iteration.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this