All pastes #592411 Raw Edit

Something

public text v1 · immutable
#592411 ·published 2007-06-27 19:13 UTC
rendered paste body
<?php

///// 					DONT EDIT THIS FILE PLEASE					/////
/////						THIS CLASS ALMOST WORKS :)				/////
/////						  TODO: Refactor						/////
/////		TODO: Use an array to get avoid accidental trace back	/////

//	In costAround [$i][0] is the current map position area.
//	In costAround [$i][1] is the initial move cost.
//	In costAround [$i][2] is the distance move cost.
//	In costAround [$i][3] is the total cost.

class AStar
{
	private $area;
	private $destPos;
	
	private $costAround;
	
	private $ax;
	private $ay;
	
	private $dx;
	private $dy;
	
	public function AStar( $x1, $y1, $x2, $y2 )
	{
		//	Get the 4 area's around the starting location.
		$this->area = new Areamap
							 ( 
							 "x = $x1 - 1 AND y = $y1 OR
							  x = $x1 + 1 AND y = $y1 OR
							  x = $x1 AND y = $y1 - 1 OR
							  x = $x1 AND y = $y1 + 1
							  ORDER BY y, x" 
							 );
							  
		$this->costAround = array( );
		
		$this->ax = $x1;
		$this->ay = $y1;
		
		$this->dx = $x2;
		$this->dy = $y2;
		
		echo "Start Pos: $this->ax, $this->ay </br> | End Pos: $this->dx, $this->dy<br>";
		
		for ( $i = 0; $i <= $this->area->count; $i++ )
		{
			$this->costAround[ $i ][ 0 ] = $this->area->items[ $i ];	
			$this->costAround[ $i ][ 1 ] = 1;
			$this->costAround[ $i ][ 2 ] = 2;
			$this->costAround[ $i ][ 3 ] = 3;
		}
		
		$this->update( );
	}
	
	public function calculateCost( )
	{	
		$this->area = new Areamap( "x = $this->ax - 1 AND y = $this->ay OR
							  x = $this->ax + 1 AND y = $this->ay OR
							  x = $this->ax AND y = $this->ay - 1 OR
							  x = $this->ax AND y = $this->ay + 1
							  ORDER BY y, x" );
							  
		for ( $i = 0; $i <= $this->area->count; $i++ )
		{
			$this->costAround[ $i ][ 0 ] = $this->area->items[ $i ];
			$this->costAround[ $i ][ 1 ] = 1;
			$this->costAround[ $i ][ 2 ] = 2;
			$this->costAround[ $i ][ 3 ] = 3;
		}
		
		for( $i = 0; $i <= 3; $i++ )
		{
			$x2 = ( $this->dx - $this->costAround[ $i ][ 0 ]->x ) * ( $this->dx - $this->costAround[ $i ][ 0 ]->x );
			$y2 = ( $this->dy - $this->costAround[ $i ][ 0 ]->y ) * ( $this->dy - $this->costAround[ $i ][ 0 ]->y ) ;
						
			$this->costAround[ $i ][ 1 ] = 10;
			$this->costAround[ $i ][ 2 ] = sqrt( $x2 + $y2 );
			if( $this->costAround[ $i ][ 0 ]->terrain != 2 )
				$this->costAround[ $i ][ 3 ] = $this->costAround[ $i ][ 1 ] + $this->costAround[ $i ][ 2 ];
			else
				$this->costAround[ $i ][ 3 ] = 99999999;
		}
		
		$smallestCost = 9999999;
		$smallestIndex = -1;
		
		for( $i = 0; $i <= 3; $i++ )
		{
			if( $this->costAround[ $i ][ 3 ] <= $smallestCost )
			{
				$smallestCost 	= $this->costAround[ $i ][ 3 ];
				$smallestIndex	= $i;
			}
		}
		
		$this->area = $this->costAround[ $smallestIndex ][ 0 ];
		$this->ax = $this->area->x;
		$this->ay = $this->area->y;
		echo "Next Move: $this->ax, ";
		echo "$this->ay<br>";
	}
	
	public function update( )
	{			
		$i = 0;
		
		while( $this->ax != $this->dx || $this->ay != $this->dy )
		{
			$this->calculateCost( );
			$i++;
		}
		
		echo "Path solved in $i steps!<br>";
	}
}
?>