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.

AppGameKit Classic Chat / [SOLVED] @Phaelax: pathfinding

Author
Message
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 14th Jun 2018 20:19 Edited at: 14th Jun 2018 20:30
Hi,
Recently I downloaded the A* pathfinding library from here
but when the "Restrict diagonals" is on then the A*8 and Flood8 methods fail giving an "Array index out of bounds..." message

The author of this post has marked a post as an answer.

Go to answer

PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 14th Jun 2018 21:45 Edited at: 14th Jun 2018 21:47
This post has been marked by the post author as the answer.
Here, I fixed up Phaelax's A* a while ago but never got round to posting it, I fixed a few bugs and made it more AGK2 compatible, I also added a few functions to help with debugging.

Quote: "/* *************************************************
* Original Author: IanM (DBP)
* Ported to AGK: Phaelax
* Date: June 22, 2013
* *************************************************
* COMMANDS
* ----------------------------------------
* CreateSearchMap(mapwidth, mapheight, cellwidth, cellheight)
* CreateSearchPathList(NumberOfPaths)
* SetSearchMap(x, y, value) (values greater than 0 are not walkable)
* GetSearchMap(x, y)
* GetSearchPathX(path, move)
* GetSearchPathY(path, move)
* GetSearchPathSize(path)
* SetSearchRestrictDiagonals(mode) (1 will avoid diagonals between blocked cells)
* SetMaximumCost(cost)
*
* SearchMapAStar4(path, startX, startY, finishX, finishY)
* SearchMapAStar8(path, startX, startY, finishX, finishY)
* SearchMapFlood4(path, startX, startY, finishX, finishY)
* SearchMapFlood8(path, startX, startY, finishX, finishY)
*
* GetFloodCost(finishX, finishY) (Uses previous Flood4 search to identify cost of specified target)

*************************************************
* Ported to AGK2: PartTimeCoder
* Date: July 29, 2017
* ----------------------------------------
* Removed: GoSub and Init function requirments
* Fixed: now works with #Option_Explicit
* Added: DrawSearchPath(path, col)
* Added: DrawSearchMap(col)
* Added: DrawSearchGrid(col)
* Added: WorldToGridX(world_x)
* Added: WorldToGridY(world_y)
* Added: GridToWorldX(grid_x)
* Added: GridToWorldY(grid_y)
* Changed: CreateSearchMap() added cell dimensions for draw functions
* Fixed: SearchMapFlood8() Throwing out of bounds error. (Y=-1)
* Fixed: SearchMapAStar8() Throwing out of bounds error when no path found. (Y=-1)
* *************************************************

*/"


AStar.agc


Example
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 15th Jun 2018 08:07
Thank you very much.
I'll check the modified code asap
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Jun 2018 16:24
This is the first I've heard of any bugs in the ported code. I must investigate!
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 15th Jun 2018 16:45 Edited at: 15th Jun 2018 21:35
Hi Phaelax, you did a great job and I will use your library in my projects.
I confirm the modification of PartTimeCoder solves the issue I mentioned in my first post.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 15th Jun 2018 17:06
@Phaelax

From your original post

Quote: "For the most part, everything works flawlessly, except that I get an out of bounds error when restricting diagonals with the Flood8 call. I'll try to track it down, but in the meantime the other 3 methods work just fine. "


You did an excellent job porting the code and it works great, the only real bug was not checking a 'Y' variable for a out of bounds, in some cases it was being set to -1, all I did was change (if Y < SEARCH_MapHeight) to (if Y >= 0 and Y < SEARCH_MapHeight) in 2 places in the code, plus as mentioned above removed the init and gosub as they are not needed, the lib is self-initializing and as I always use #Option_Explicit I added variable declarations to all the functions
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 16th Jun 2018 08:32
I am wondering if I can use more than one path in the astar implementation
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 16th Jun 2018 09:31
Yes you can, you set the number of paths you want to use with 'CreateSearchPathList(NumberOfPaths)' and use the path index in each of the function calls (SearchMap* and GetSearchPath*, the example uses a single path but this does work as I remember testing it.
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 16th Jun 2018 14:52
What's the use in using two or more paths? In which case could it be useful?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 16th Jun 2018 16:21 Edited at: 16th Jun 2018 16:23
if your game is like a turn-based game where 1 entity moves at a time then a single path would suffice and you just reuse it for each entity generating a new path each time, but if its like a RTS where each entity would need its own path and several entities move simultaneously then you would use multiple paths or a path for each entity, once a path is generated it remains in memory at the index you used in SearchMap* functions so each unit in your game will be completely independent.

I hope Phaelax does not mind me editing and reposting his lib but I spent some time this afternoon making the whole thing more multi-path friendly after reading your post, I added a few functions to set/get the start and target marks for each path, the original example used the same global start+target I have made it so each path index can have its own set of markers by adding a type array and some functions into the lib

in this example use F1(path 0) to F4(path 3) to set the active path, set start and target markers and generate a path as usual, you will see when switching between paths (F*) that each path is now independent

Modified AStar.agc


New Example


EDIT:

Calling CreateSearchPathList() will reset markers for all paths so you will want to think ahead when it comes to the amount of paths required, maybe add more than you will need or set it to thwe maximum number of units in your game.
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 16th Jun 2018 17:50
Your explenation is very useful. Thank you very much.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Jun 2018 11:06
No problem at all, never hurts to update every few years or so!

I used multiple paths when I was making an RTS, multiple units traveling to different spots at the same time.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 25th Jun 2018 20:07
I took a look at the


I noticed that the Cost variable is always zero. Maybe the following code

should be
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Jun 2018 21:01
In regards to what?

I believe the cost variable is only used if different terrain types are implemented. Like you'd move slower over mud and faster over grass. So like it might be a shorter path over the swamp, but walking around it is technically faster.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
george++
AGK Tool Maker
16
Years of Service
User Offline
Joined: 13th May 2007
Location: Thessaloniki, Hellas
Posted: 27th Jun 2018 11:59
The 'cost' variable is declared locally. It is always zero since it is used only in the 'if' statement

Login to post a reply

Server time is: 2024-04-19 13:18:26
Your offset time is: 2024-04-19 13:18:26