Created
March 13, 2023 19:23
-
-
Save nkh/5c61232f0b648709a71b66070b1838d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package AI::Pathfinding::AStar::Test; | |
use base AI::Pathfinding::AStar; | |
use strict ; | |
use warnings ; | |
use feature qw/say/ ; | |
sub new | |
{ | |
my $invocant = shift; | |
my $class = ref($invocant) || $invocant; | |
my $self = bless {}, $class; | |
my $map = $self->{map} = {}; | |
my $y = 1; | |
print "\e[2J\e[H" ; | |
say "\e[2;30;31m 123456789012345678901234567890123456789012345678901234567890\e[m" ; | |
while (<DATA>) | |
{ | |
chomp; | |
my @cols = split //; | |
my $map_k ='' ; | |
for my $x ( 0 .. $#cols ) | |
{ | |
my $cell = $cols[$x]; | |
my $x_1 = $x + 1; | |
$map_k .= $map->{$x_1.'.'.$y} = $cell ne '|' ? 1 : 0 ; | |
} | |
printf "\e[2;30;31m%-2d\e[m$_\n", $y ; | |
$y++; | |
} | |
return $self; | |
} | |
sub getSurrounding | |
{ | |
my ($self, $source, $target) = @_; | |
my ($x, $y) = split(/\./, $source); | |
my $map = $self->{map}; | |
my $surrounding = []; | |
my ($orthogonal_cost, $diagonal_cost) = (10, 140) ; | |
for my $node (($x+1).'.'.$y, $x.'.'.($y+1), ($x-1).'.'.$y, $x.'.'.($y-1)) | |
{ | |
push @$surrounding, [$node, $orthogonal_cost, calc_heuristic($node, $target)] | |
if exists $map->{$node} && $map->{$node} ; | |
} | |
# diagonal | |
# for my $node ( ($x+1).'.'.($y+1), ($x+1).'.'.($y-1), ($x-1).'.'.($y+1), ($x-1).'.'.($y-1)) | |
# { | |
# push @$surrounding, [$node, $diagonal_cost, calc_heuristic($node, $target)] | |
# if exists $map->{$node} && $map->{$node} ; | |
# } | |
return $surrounding; | |
} | |
my ($XHK, $YHK) = (1, 1); | |
sub calc_heuristic | |
{ | |
my ($source, $target) = @_; | |
my ($x1, $y1) = split(/\./, $source); | |
my ($x2, $y2) = split(/\./, $target); | |
return (abs($x1-$x2) * $XHK) + (abs($y1-$y2) * $YHK); | |
} | |
my $g = AI::Pathfinding::AStar::Test->new() ; | |
my @colors = (31 .. 36) x 5 ; | |
while (@ARGV > 1) | |
{ | |
my ($start, $end) = ($ARGV[0], $ARGV[1]) ; | |
shift @ARGV ; | |
my ($sx, $sy) = split(/\./, $start); | |
my ($ex, $ey) = split(/\./, $end); | |
($XHK, $YHK) = (1, 1); | |
if($sx < $ex) { $XHK = 2 } elsif($sy > $ey) { $YHK = 2 } | |
my $color = shift @colors ; | |
for my $position (@{$g->findPath($start, $end)}) | |
{ | |
my ($x, $y) = split(/\./, $position); | |
$y += 1 ; # account for horizontal ruler | |
$x += 2 ; # account for vertical ruler | |
print "\e[$y;${x}H\e[${color}mx\e[m" ; | |
} | |
} | |
print "\e[30;0H\e[m" ; | |
# print "$start -> $end: @$path1\n" ; | |
__DATA__ | |
............................................................ | |
...|..........|..............|..........|................|.. | |
...|....|.....|..............|....|.....|................|.. | |
...|..|....||||||||..........|..|....||||||||.......|||||||| | |
......|....|.....|.||||.........|....|.....|...|.||||....|.. | |
......|....|.........|..........|....|.............|........ | |
......|....|....................|....|...................... | |
......|....|||||||||...|........|....|||||||||||||.....||||. | |
...............|.......|.................|...|.........|.... | |
........|..............|..........|......................... | |
.......................|.................................... | |
...|..........|........|.....|..........|................... | |
...|....|.....|..............|....|.....|................... | |
...|..|....||||||||..........|..|....||||||||||||......||||. | |
......|....|.....|.||||.........|....|.....|...|.||||....|.. | |
......|....|.........|..........|....|.............|........ | |
......|....|....................|....|...................... | |
.......................|.................................... | |
...|..........|........|.....|..........|................... | |
......|....|||||||||............|....|||||||||||||.....||||. | |
...............|.........................|...|.........|.... | |
........|...............|.........|......................... | |
......|....|....................|....|...................... | |
......|....|||||||||............|....|||||||||||||.....||||. | |
...............|.........................|...|.........|.... | |
........|...............|.........|......................... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment