A simple pathfinder (nothing fancy)

Monkey Forums/Monkey Code/A simple pathfinder (nothing fancy)

Supertino(Posted 2014) [#1]
I finally got around to creating an all in one Class for 2D pathfinding, it's nothing fancy and it's not a slick as A* or indeed anything more advanced. This will always find the shortest route but it wont always be the nicest.

I don't know what the technique is called but basically it spirals out for the Start location until it finds the Goal location which does result in a lot of squares being checked that don't need to be, it should not have much of an impact on performance unless you're going with large arrays with dimensions in the several hundreds.

You can play a test app here http://moarlasers.com/path/MonkeyGame.html [HTML5] and grab the source from here https://drive.google.com/folderview?id=0B1jBbicq8v_dbUtHWlNfZFh2Qm8&usp=drive_web



It's requires you use an Array in the array:Int[a][b] format.

The Methods
Prepare:Void(arr:Int[][], unpassable_vals:Int[], start_x:Int, start_y:Int, finish_x:Int, finish_y:Int) ' Call before you search
Search:Bool() ' Start the search, return True or False if path can be found
GetString:String() ' Get the Path as single string of U,D,L,R
Get:String[] () ' Get the Path as an Array containing U,D,L,R


The Prepare method needs a little explaining, lets assume this is being used for PacMan;
- The first argumant is requesting you feed in an array, this will typical be your level containing the walls, floors all represended with a value, walls are '1' floors as '2' for example, the Class will take a take a copy of this for it's needs and wont change the original.

- The second argument is where you can feed in values that you want the Class to treat as unpassable, such as walls

- The 3rd\4th is the array x&y of the where you want to start your search from

- The 5th and 6th us the array x&y where you want to end the search


The idea is you call Prepare then Search and get your Path info by calling Get or GetString



The Class



Snader(Posted 2014) [#2]
Great work!