• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
VittorioRomeo

Grid pathfinding with a lot of entities

11 posts in this topic

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

Share this post


Link to post
Share on other sites
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]
1

Share this post


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

Share this post


Link to post
Share on other sites
[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..
0

Share this post


Link to post
Share on other sites
[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
1

Share this post


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

Share this post


Link to post
Share on other sites
[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
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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.
1

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  
Followers 0