Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / IanM's A* Pathfinding

Author
Message
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 7th Aug 2010 05:18
Hey, I seem to be making a lot of threads atm :/

Well this one is about pathfinding for an rts. When I move my units, obviously they have to make their way around buildings and the terrain itself.

I found this amazing collection of functions by IanM, http://forum.thegamecreators.com/?m=forum_view&t=11137&b=6, but I haven't a clue how to apply them in a real 3d situation.

Could someone try shed some light on that please?

Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 7th Aug 2010 05:38
I haven't heard of those but I wrote waypoint pathfinding using the A* algorithm. You just need to place the waypoints and connect them.
http://forum.thegamecreators.com/?m=forum_view&t=172370&b=1

My computer can render anything no matter the complexity of it in real time.
The memory is enormous. My computer is unique to everyone else’s. What computer do I have?

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 7th Aug 2010 14:30
@Mad Nightmare,
It's fairly simple.

You call CreateSearchMap to create and size your map, then call CreateSearchPathLists to create storage for a number of paths to be searched/remembered.

You block cells in the map using SetSearchMap, until you have your complete map, then finally, you call one of the search functions (SearchMapAStar4/SearchMapAStar8/SearchMapFlood4/SearchMapFlood8) to carry out the search and store the results in one of the path lists you created earlier.

I've uploaded a zip containing a simple framework I used for testing it.

Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 7th Aug 2010 18:22
Thanks for the link Madscientist, but i've kinda got my heart set on IanM's, they look quite complete and perfect for what I need.

IanM, thanks for the reply, obviously I was hoping that you would. The problem I face with my rts, is that the maps could be up to 1000x1000 units of space, which I imagine would increase the time it takes quite significantly.

(Just changed the map size to 1000x1000, and it takes 45ms for each search :/ )

So i'm unsure on how to go about this, would i have to make it so my units and building etc, snap to every 4th unit of space or something similar?

Thanks

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Aug 2010 13:17
I would class that as a big map for my code to handle, and I suspect the same for Madscientists code too.

As you suggest, you could set your search map resolution to a quarter of the size of your actual map - that's a valid solution to the problem if your map can handle it.

You could also put in fast line-of-sight checks so that you only carry out an A* search if you can't see the target point in the first place.

Another alternative is to make changes to the search code to allow your searches to share processing time with the rest of your game, by using the function pointers plug-in of mine with the co-routine commands. I may look at doing that with the code for you if I have time.

Mixing all of the above would give a reasonable solution to your problem.

I am also looking into other alternatives too, but I'm not sure what I can say about that yet as it's very early days and may be months until something comes from that.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 8th Aug 2010 14:17 Edited at: 8th Aug 2010 14:27
You could divide the map into chunks of 50x50 cells. Then you only look at cells which make up the border of a chunk when pathfinding.

Each chunk precalculates which border cells are accessible from other border cells. This could be done efficiently by giving each chunk a list of open areas. Each open area contains a list of the border cells which are in this open area. That way if you have a completely empty chunk it is only one list. The cost of a path from one border cell to another would be estimated when it is encountered.

Optimally, you would count the bottom and right border cells as being cells just outside of the chunk. That way the bottom border cells of one chunk are the top border cells of the one below.

When pathfinding you do one large pathfind using only edge cells of the chunks. Then once you have found which edge cells you will travel through, you can do per-chunk pathfinding to find the path across each chunk.

The good thing about this is that the per-chunk pathfinding only needs to be done when that chunk is reached, and yet you know that you need to do it at some point. This allows you to make a queue system to 'smooth' the number of pathfinds needed each frame.

When a large pathfind finishes, you add all the sub-pathfinds to a queue in decreasing priority, so the first chunk along the route would have the highest priority. You could give each frame an allotted time for pathfinding.
The program would then keep popping at item of the queue and running it until the allotted time had been used up, or there are no more items in the queue.

The only downside is that it may not always find the shortest path.

[b]
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 8th Aug 2010 15:51
Thanks for the replies. I hope to get to work on this tonight, I'll try and use the information you've given me, and if I get it working, I'll post some code showing how. xD

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Aug 2010 15:55
There's another problem with that too - my code doesn't support multiple maps and would require a full refresh of the map every time you switched from the larger scale map to the smaller scale.

Of course, there's nothing to stop some enterprising programmer taking my code and add that in...

One other thing that's just occurred to me is that my code supports a 2D grid-like system only, when what was requested was something in 3D. If you can map your 3D map to a 2D grid then my code is good enough (in general terms), but if you've got a true 3D environment (crossing but unconnected walkways etc), then a node-based system is the way to go, and Mad Nightmare can take a look at Madscientist's code instead.

The time-sharing technique I explained above (co-routines) will still work in either case.

Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 8th Aug 2010 16:01
My rts uses a 3d environment, but it's practically 3d, you can move the units around the x and z axis, and they just get positioned at the ground height

Don't worry, I don't plan on stealing your code and doing nothing with it.

Thanks for the help

Login to post a reply

Server time is: 2026-07-24 20:35:00
Your offset time is: 2026-07-24 20:35:00