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
| End Pos: $this->dx, $this->dy
"; 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
"; } public function update( ) { $i = 0; while( $this->ax != $this->dx || $this->ay != $this->dy ) { $this->calculateCost( ); $i++; } echo "Path solved in $i steps!
"; } } ?>