Анатолий Гришаев
and 1 contributors

# NAME

AI::Pathfinding::AStar::Rectangle - AStar algorithm on rectangle map

# SYNOPSIS

``````  use AI::Pathfinding::AStar::Rectangle qw(create_map);

my \$map = create_map({height=>10, width=>10});

#
# -or-
#
# \$map = AI::Pathfinding::AStar::Rectangle->new({{height=>10, width=>10});

for my \$x (\$map->start_x..\$map->last_x){
for my \$y (\$map->start_y..\$map->last_y){
\$map->set_passability(\$x, \$y, \$A[\$x][\$y]) # 1 - Can pass througth , 0 - Can't pass
}
}

my \$path = \$map->astar( \$from_x, \$from_y, \$to_x, \$to_y);

print \$path, "\n"; # print path in presentation of "12346789" like keys at keyboard``````

# DESCRIPTION

AI::Pathfinding::AStar::Rectangle provide abstraction for Rectangle map with AStar algoritm

# OBJECT METHODS

new { "width" => map_width, "height" => map_heigth }

Create AI::Pathfinding::AStar::Rectangle object. Object represent map with given height and width.

set_passability x, y, value # value: 1 - can pass through point, 0 - can't

Set passability for point(x,y)

get_passability (x,y)

Get passability for point

astar(from_x, from_y, to_x, to_y)

Search path from one point to other

return path like "1234..9"

where 1 - mean go left-down 2 - down 3 - down-right ... 9 - right-up

dastar( from_x, from_y, to_x, to_y) Return diagonal path with AI =item width()

Get map width

height()

Get map height

start_x(), start_y()

Get/Set coords for leftbottom-most point

set_start_xy( self, x, y)

Set coordinates of left-bootom point

last_x(), last_y()

Get coords for right-upper point

foreach_xy( BLOCK )

Call BLOCK for every point on map.

\$map->foreach_xy( sub { \$A[\$a][\$b] = \$_ }) (\$a, \$b, \$_) (x, y, passability)

foreach_xy_set( sub { \$A[\$a][\$b] });
`````` set passability for every point at map.
BLOCK must return passability for point (\$a, \$b);
\$a and  \$b must be global var not declared as my, our, ``````
is_path_valid( start_x, start_y, path) Check if path is valid path, all points from ( start_x, start_y ) to path end is passable
``    In list context return ( end_x, end_y, weigth, true or false )``
path_goto( start_x, start_y, path)

In list context return ( end_x, end_y, weigth ) weight is sum of <diagonal (1379)> * 14 + <short path> * 10

draw_path( start_x, start_y, path) print path to STDOUT #!/usr/bin/perl # my \$m = AI::Pathfinding::AStar::Rectangle->new({ width => 16, height => 8 });
`````` \$m->foreach_xy_set( sub {  \$a < 12 && 1<\$b && \$b <9 } );
\$m->draw_path( 5, 5, '1666666888' );
``````

Result:

# Steps: 10 # 00010203040506070809101112131415 # |#|#|#|#|#|#|#|#|#|#|#|#|#|#|#0 # |#|#|#|#|#|#|#|#|#|#|#|#|#|#|#1 # |_|_|_|_|_|_|_|_|_|_|_|_|#|#|#2 # |_|_|_|_|_|_|_|_|_|_|_|_|#|#|#3 # |_|_|_|_|o|o|o|o|o|o|o|_|#|#|#4 # |_|_|_|_|_|o|_|_|_|_|o|_|#|#|#5 # |_|_|_|_|_|_|_|_|_|_|o|_|#|#|#6 # |_|_|_|_|_|_|_|_|_|_|o|_|#|#|#7 # |_|_|_|_|_|_|_|_|_|_|_|_|#|#|#8

See ./examples

None by default.

# AUTHOR

A.G. Grishaev, <gtoly@cpan.org<gt>