Jump to content

  • Log In with Google      Sign In   
  • Create Account


Grid pathfinding with a lot of entities


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 SuperV1234   Members   -  Reputation: 375

Like
0Likes
Like

Posted 12 June 2012 - 06:08 AM

I'd like to explain this problem with a screenshot from a released game, DROD: Gunthro's Epic Blunder, by Caravel Games

Posted Image

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.


To be honest, I don't understand it that well.

Sponsor:

#2 Dawoodoz   Members   -  Reputation: 294

Like
1Likes
Like

Posted 12 June 2012 - 11:03 AM

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

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#3 SuperV1234   Members   -  Reputation: 375

Like
0Likes
Like

Posted 12 June 2012 - 01:09 PM

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?

#4 SuperV1234   Members   -  Reputation: 375

Like
0Likes
Like

Posted 12 June 2012 - 01:38 PM

Posted Image

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

#5 dmatter   Crossbones+   -  Reputation: 3030

Like
1Likes
Like

Posted 12 June 2012 - 01:46 PM

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.

Edited by dmatter, 12 June 2012 - 01:54 PM.


#6 SuperV1234   Members   -  Reputation: 375

Like
0Likes
Like

Posted 12 June 2012 - 01:55 PM


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.


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?

#7 dmatter   Crossbones+   -  Reputation: 3030

Like
2Likes
Like

Posted 12 June 2012 - 02:16 PM

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?

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?

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, 12 June 2012 - 03:47 PM.


#8 LorenzoGatti   Crossbones+   -  Reputation: 2662

Like
0Likes
Like

Posted 15 June 2012 - 05:59 AM

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.
Produci, consuma, crepa

#9 Tordin   Members   -  Reputation: 604

Like
0Likes
Like

Posted 19 June 2012 - 08:21 AM

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"

#10 leopardpm   Members   -  Reputation: 148

Like
1Likes
Like

Posted 19 June 2012 - 01:30 PM

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!

#11 leopardpm   Members   -  Reputation: 148

Like
1Likes
Like

Posted 19 June 2012 - 01:34 PM

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.

#12 DemonRad   Members   -  Reputation: 290

Like
0Likes
Like

Posted 30 June 2012 - 04:25 PM

that's very similiar the the windows' game "mine field".

Edited by DemonRad, 30 June 2012 - 04:28 PM.

Peace and love, now I understand really what it means! Guardian Angels exist! Thanks!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS