Location: PHPKode > scripts > Path finder > example.php
<?php

//Example of the map-array generating function
//This pathfinder example assumes that a square map is used. To use rectangular map you need to tweak the pathfinder a bit
//Here we assign one tile in four with a heavier weight and it is less favored for finding the path
//Map is an associative array which has a key of the tile's coordinates as the base. Only required array value is 'weight' but
//you can store additional information in the array for, say, public purposes, such as tile graphic data.
function getMap(){
	$map=array();
	for($x=1;$x<=20;$x++){
		for($y=1;$y<=20;$y++){
			$rand=rand(1,4);
			if($rand==1){
				$map[$x.'x'.$y]=array('weight'=>'3.0');
			} else {
				$map[$x.'x'.$y]=array('weight'=>'1.0');
			}
		}
	}
	return $map;
}

//Currently getMap is dynamic and changes with each try, but you can send a pre-generated maps here just as easily
//as long as the weight is set for each map tile element and the array key is in XxY format
$map=getMap();
require('class.pathfinder.php');
$path=new PathFinder(); //Create new pathfinder object
$path->setOrigin(1,1); //Set the start location
$path->setDestination(20,20); //Set the destination
$path->setMap($map); //Assign the map to use for calculations
$result=$path->returnPath(); //Returns an array of all the walked steps
echo '<pre>';
print_r($result);
echo '</pre>';

?>
Return current item: Path finder