UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: Alex777 on February 21, 2006, 06:32:19 PM

Title: A* pathfinding demo
Post by: Alex777 on February 21, 2006, 06:32:19 PM
  A* pathfinding



I thought some folks on here might be interested in this routine.  It demonstrates the A* pathfinding algorithm (with several different heuristics), Djikstra's algorithm for a breadth-first search, and a best-first search.  Compiles in PlayBASIC 1.089c ->PlayBASIC V1.64N.  User Instructions are in the comment block at the start.  Comments?

[pbcode]
; PROJECT : AStarDemo
; AUTHOR  :  Alex Henderson
; CREATED : 2/19/2006
; EDITED  : 2/21/2006
; ---------------------------------------------------------------------

RemStart
USER INSTRUCTIONS:
All hexes (nodes) are initialized to a movement cost = 1.  
The hex coordinates of the hex under the cursor are shown in the upper L.
Left-click to increase the movement cost, to a maximum = 9.  
Right-click to decrease the movement cost, to a minimum = 1.  
"X" key = make the hex into blocking terrain.
"S" key = select a start hex for the path.
"D" key = select a destination.
"P" key = calculate & show the path.
"F" key = save terrain map as "TerrainMap.txt".
"L" key = load "TerrainMap.txt".
The path length in hexes & the total movement cost is displayed.
You can change the start or destination & press "P" again.
copyright: public domain
RemEnd

[/pbcode]
Title: A* pathfinding demo
Post by: kevin on February 21, 2006, 06:57:07 PM
Now that's seriously impressive.  Not just the path finding code, but your usage of the PB's built in helper functions is superb !.  

Excellent Demo !



Related Links:

--- Flood Fill Path finding - Demo - Source Code / Tutorial (https://www.underwaredesign.com/forums/index.php?topic=4307.0)
Title: A* pathfinding demo
Post by: Alex777 on February 21, 2006, 09:25:56 PM
<< Now that's seriously impressive. >>

Thanks, Kevin.  Actually, what's seriously impressive are PB's fast and flexible array functions, which made implementing A* a pleasure.

Calypson, I'd be interested in your heuristic(s).  Wanna share?
Title: A* pathfinding demo
Post by: kevin on February 21, 2006, 09:36:08 PM
I'm just glad some bodies using that stuff.. There somewhat abstract.. oh well  :)
Title: A* pathfinding demo
Post by: Alex777 on February 21, 2006, 09:55:50 PM
i think the key, for me anyway, is sticking to 1D arrays as much as possible - stacks, queues, and lists.  PB has great tools for manipulating these, although some things are not documented anywhere.

One thing we could still use is the ability to rearrange a 1D array according to the sorted order of another 1D array.  For example:

Sort2Arrays Array1(), StartElement, EndElement, Array2(), StartElement,    EndElement

where Array1() is sorted and the elements of Array2() are rearranged so that its elements remain in the same position relative to the corresponding elements in Array1().  If element 3 moves to element 7 in Array1() after sorting, then element 3 moves to element 7 in Array2() also.
Title: A* pathfinding demo
Post by: kevin on February 21, 2006, 10:00:09 PM
I assume you want to store references in the one array and the sort key in the other ?
Title: A* pathfinding demo
Post by: Alex777 on February 21, 2006, 10:43:14 PM
Yah - I guess that's a simpler way to say it.    :rolleyes:
Title: A* pathfinding demo
Post by: Digital Awakening on February 21, 2006, 10:58:19 PM
It would be nice to be able to sort a typed 1D array after a specific type. Say I have an array with units and I want to sort them after how much power they have I could sort units() after units().power.

I could probably learn a lot from that code though. Thanks Alex :)
Title: A* pathfinding demo
Post by: Bustaballs on February 22, 2006, 02:20:06 AM
I tried to run it but got a compile error
Title: A* pathfinding demo
Post by: Alex777 on February 22, 2006, 06:09:59 AM
QuoteI tried to run it but got a compile error
[snapback]9027[/snapback]

Which one?
Title: A* pathfinding demo
Post by: BlinkOk on February 22, 2006, 07:00:25 AM
i got a syntax error for "flushkeys" and i just deleted the line and it worked ok. i got a real old version though
Title: A* pathfinding demo
Post by: Bustaballs on February 22, 2006, 08:39:29 AM
QuoteWhich one?
[snapback]9029[/snapback]

the first post