blib/lib/Game/DijkstraMap.pm

filter: Statementstmt 98.5% 335 / 340 Branchbran 82.9% 131 / 158 Conditioncond 67.2% 72 / 107 MC/DCmcdc 42.8% 27 / 63 Subroutinesub 100% 39 / 39 Podpod 95.6% 22 / 23 totaltotal 85.7% 626 / 730 SCAR 51.9
CC
149
Cov
89%
next
18.7
next_with
16.9
next_sq
15.6
CRAP 179.2
↑ Summary
1 # -*- Perl -*-
2 #
3 # Dijkstra Map path finding. run perldoc(1) on this file for additional
4 # documentation
5  
6 package Game::DijkstraMap;
7  
8 3 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::Util 1.26 qw(shuffle sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 our $VERSION = '1.04';
18  
19 3 ▶use constant SQRT2 => sqrt(2);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 with 'MooX::Rebuild';
22  
23 12 ▶has bad_cost => ( is => 'rw', default => sub { -2147483648 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
24 12 ▶has min_cost => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25 9 ▶has max_cost => ( is => 'rw', default => sub { 2147483647 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 has costfn => (
28 is => 'rw',
29 default => sub {
30 return sub {
31 81 ▶ my ( $self, $c ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
32 81 ▶ if ( $c eq '#' ) { return $self->bad_cost }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "#") 27 54 81
33 54 ▶ if ( $c eq 'x' ) { return $self->min_cost }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "x") 7 47 54
34 47 return $self->max_cost;
35 12 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
36 }
37 3 );
38 3 has dimap => ( is => 'rw' );
39 12 ▶has iters => ( is => 'rwp', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
40 12 ▶has next_m => ( is => 'rw', default => sub { 'next' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
41 9 ▶has normfn => ( is => 'rw', default => sub { \&norm_4way } );
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 sub BUILD {
44 12 ▶ my ( $self, $param ) = @_;
Subroutine BUILD: called CC=5 SCAR=16.3
Pod undocumented
45 croak "cannot have both map and str2map arguments"
46 12 ▶ if exists $param->{'map'} and exists $param->{'str2map'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"map"} and exists $param->{"str2map"} 1 11 12
Condition66%2/3
exists $param->{"map"} and exists $param->{"str2map"}
!ll&&!rl&&r
1101
MC/DC50%1/2
exists $param->{"map"} and exists $param->{"str2map"}
exists $param->{"map"} exists $param->{"str2map"}
Truth table66%2/3
A and B
A= exists $param->{"map"}B= exists $param->{"str2map"}
ABresult
0X0
100
111
47 $self->map( $param->{'map'} )
48 11 ▶ if exists $param->{'map'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"map"} 0 11 11
49 $self->map( $self->str2map( $param->{'str2map'} ) )
50 11 ▶ if exists $param->{'str2map'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"str2map"} 5 6 11
51 }
52  
53 sub adjacent_values {
54 5 ▶ my ( $dimap, $r, $c, $maxrow, $maxcol ) = @_;
Subroutine adjacent_values: called CC=9 SCAR=22.0
Pod documented
55 5 my @values;
56 5 for my $i ( -1, 1 ) {
57 10 my $x = $c + $i;
58 10 ▶ push @values, $dimap->[$r][$x] if $x >= 0 and $x <= $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 0 and $x <= $maxcol 6 4 10
Condition100%3/3
$x >= 0 and $x <= $maxcol
!ll&&!rl&&r
316
MC/DC100%2/2
$x >= 0 and $x <= $maxcol
$x >= 0 $x <= $maxcol
Truth table100%3/3
A and B
A= $x >= 0B= $x <= $maxcol
ABresult
0X0
100
111
59 10 for my $j ( -1 .. 1 ) {
60 30 $x = $r + $i;
61 30 my $y = $c + $j;
62 30 ▶ push @values, $dimap->[$x][$y]
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 0 and $x <= $maxrow and $y >= 0 and $y <= $maxcol 14 16 30
Condition100%9/9
$x >= 0 and $x <= $maxrow and $y >= 0 and $y <= $maxcol
!ll&&!rl&&r
15114
$x >= 0 and $x <= $maxrow and $y >= 0
!ll&&!rl&&r
12315
$x >= 0 and $x <= $maxrow
!ll&&!rl&&r
9318
MC/DC100%4/4
$x >= 0 and $x <= $maxrow and $y >= 0 and $y <= $maxcol
$x >= 0 $x <= $maxrow $y >= 0 $y <= $maxcol
Truth table100%5/5
A and B and C and D
A= $x >= 0B= $x <= $maxrowC= $y >= 0D= $y <= $maxcol
ABCDresult
0XXX0
10XX0
110X0
11100
11111
63 if $x >= 0
64 and $x <= $maxrow
65 and $y >= 0
66 and $y <= $maxcol;
67 }
68 }
69 5 return @values;
70 }
71  
72 sub adjacent_values_diag {
73 50 ▶ my ( $dimap, $r, $c, $maxrow, $maxcol ) = @_;
Subroutine adjacent_values_diag: called CC=9 SCAR=22.0
Pod documented
74 50 my @values;
75 50 ▶ push @values, $dimap->[ $r - 1 ][ $c - 1 ] if $r > 0 and $c > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > 0 and $c > 0 26 24 50
Condition100%3/3
$r > 0 and $c > 0
!ll&&!rl&&r
15926
MC/DC100%2/2
$r > 0 and $c > 0
$r > 0 $c > 0
Truth table100%3/3
A and B
A= $r > 0B= $c > 0
ABresult
0X0
100
111
76 50 ▶ push @values, $dimap->[ $r - 1 ][ $c + 1 ] if $r > 0 and $c < $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $r > 0 and $c < $maxcol 25 25 50
Condition100%3/3
$r > 0 and $c < $maxcol
!ll&&!rl&&r
151025
MC/DC100%2/2
$r > 0 and $c < $maxcol
$r > 0 $c < $maxcol
Truth table100%3/3
A and B
A= $r > 0B= $c < $maxcol
ABresult
0X0
100
111
77 50 ▶ push @values, $dimap->[ $r + 1 ][ $c - 1 ] if $r < $maxrow and $c > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r < $maxrow and $c > 0 25 25 50
Condition100%3/3
$r < $maxrow and $c > 0
!ll&&!rl&&r
131225
MC/DC100%2/2
$r < $maxrow and $c > 0
$r < $maxrow $c > 0
Truth table100%3/3
A and B
A= $r < $maxrowB= $c > 0
ABresult
0X0
100
111
78 50 ▶ push @values, $dimap->[ $r + 1 ][ $c + 1 ] if $r < $maxrow and $c < $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $r < $maxrow and $c < $maxcol 28 22 50
Condition100%3/3
$r < $maxrow and $c < $maxcol
!ll&&!rl&&r
13928
MC/DC100%2/2
$r < $maxrow and $c < $maxcol
$r < $maxrow $c < $maxcol
Truth table100%3/3
A and B
A= $r < $maxrowB= $c < $maxcol
ABresult
0X0
100
111
79 50 return @values;
80 }
81  
82 sub adjacent_values_sq {
83 270 ▶ my ( $dimap, $r, $c, $maxrow, $maxcol ) = @_;
Subroutine adjacent_values_sq: called CC=5 SCAR=16.1
Pod documented
84 270 my @values;
85 270 ▶ push @values, $dimap->[$r][ $c - 1 ] if $c > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > 0 196 74 270
86 270 ▶ push @values, $dimap->[$r][ $c + 1 ] if $c < $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $c < $maxcol 213 57 270
87 270 ▶ push @values, $dimap->[ $r - 1 ][$c] if $r > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > 0 199 71 270
88 270 ▶ push @values, $dimap->[ $r + 1 ][$c] if $r < $maxrow;
Branch: 1 branch
BranchTrueFalseTotal
if $r < $maxrow 210 60 270
89 270 return @values;
90 }
91  
92 sub clone {
93 1 ▶ my ($self) = @_;
Subroutine clone: called CC=4 SCAR=13.9
Pod documented
94 1 my $newdm = $self->rebuild;
95 1 my $dimap = $self->dimap;
96 1 ▶ if ( defined $dimap ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dimap) 1 0 1
97 1 my $map;
98 1 my $cols = $dimap->[0]->$#*;
99 1 for my $r ( 0 .. $dimap->$#* ) {
100 3 for my $c ( 0 .. $cols ) {
101 9 $map->[$r][$c] = $dimap->[$r][$c];
102 }
103 }
104 1 $newdm->dimap($map);
105 }
106 1 return $newdm;
107 }
108  
109 sub dimap_with {
110 2 ▶ my ( $self, $param ) = @_;
Subroutine dimap_with: called CC=9 SCAR=22.3
Pod documented
111 2 my $dimap = $self->dimap;
112 2 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 1 2
113 1 my $new_dimap;
114 1 my $badcost = $self->bad_cost;
115 1 my $cols = $dimap->[0]->$#*;
116 1 for my $r ( 0 .. $dimap->$#* ) {
117 3 COL: for my $c ( 0 .. $cols ) {
118 9 my $value = $dimap->[$r][$c];
119 9 ▶ if ( $value == $badcost ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == $badcost) 0 9 9
120 0 $new_dimap->[$r][$c] = $badcost;
121 0 next COL;
122 }
123 9 ▶ $value *= $param->{my_weight} // 1;
Condition50%1/2
$param->{"my_weight"} // 1
l!l
90
MC/DC0%0/1
$param->{"my_weight"} // 1
$param->{"my_weight"}
Truth table50%1/2
A
A= $param->{"my_weight"}
Aresult
11
00
124 9 my @here = map $_->values( [ $r, $c ] )->[0], $param->{objs}->@*;
125 9 for my $h ( 0 .. $#here ) {
126 17 ▶ if ( $here[$h] == $badcost ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($here[$h] == $badcost) 1 16 17
127 1 $new_dimap->[$r][$c] = $badcost;
128 1 next COL;
129 }
130 16 ▶ $value += $here[$h] * ( $param->{weights}->[$h] // 0 );
Condition50%1/2
$param->{"weights"}[$h] // 0
l!l
160
MC/DC0%0/1
$param->{"weights"}[$h] // 0
$param->{"weights"}[$h]
Truth table50%1/2
A
A= $param->{"weights"}[$h]
Aresult
11
00
131 }
132 8 $new_dimap->[$r][$c] = $value;
133 }
134 }
135 1 return $new_dimap;
136 }
137  
138 sub each_cell {
139 2 ▶ my ( $self, $fn ) = @_;
Subroutine each_cell: called CC=6 SCAR=18.0
Pod documented
140 2 ▶ croak "need a code ref" if !defined $fn or ref $fn ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fn or ref $fn ne "CODE" 1 1 2
Condition66%2/3
not defined $fn or ref $fn ne "CODE"
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $fn or ref $fn ne "CODE"
not defined $fn ref $fn ne "CODE"
Truth table66%2/3
not(A) or B
A= not defined $fnB= ref $fn ne "CODE"
ABresult
1X1
011
000
141 1 my $dimap = $self->dimap;
142 1 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 0 1 1
143 1 my $cols = $dimap->[0]->$#*;
144 1 for my $r ( 0 .. $dimap->$#* ) {
145 3 for my $c ( 0 .. $cols ) {
146 9 $fn->( $dimap, $r, $c, $self );
147 }
148 }
149 1 return $self;
150 }
151  
152 sub map {
153 8 ▶ my ( $self, $map ) = @_;
Subroutine map: called CC=8 SCAR=21.5
Pod documented
154 8 my $dimap = [];
155 8 ▶ croak "no valid map supplied"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $map or ref $map ne "ARRAY" or not defined $map->[0] or ref $map->[0] ne "ARRAY" 0 7 7
Condition66%6/9
not defined $map or ref $map ne "ARRAY" or not defined $map->[0] or ref $map->[0] ne "ARRAY"
l!l&&r!l&&!r
107
not defined $map or ref $map ne "ARRAY" or not defined $map->[0]
l!l&&r!l&&!r
107
not defined $map or ref $map ne "ARRAY"
l!l&&r!l&&!r
017
MC/DC25%1/4
not defined $map or ref $map ne "ARRAY" or not defined $map->[0] or ref $map->[0] ne "ARRAY"
not defined $map ref $map ne "ARRAY" not defined $map->[0] ref $map->[0] ne "ARRAY"
Truth table40%2/5
not(A) or B or not(C) or D
A= not defined $mapB= ref $map ne "ARRAY"C= not defined $map->[0]D= ref $map->[0] ne "ARRAY"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
156 if !defined $map
157 or ref $map ne 'ARRAY'
158 or !defined $map->[0]
159 or ref $map->[0] ne 'ARRAY';
160 7 my $cols = $map->[0]->@*;
161 7 for my $r ( 0 .. $map->$#* ) {
162 21 ▶ croak "unexpected column count at row $r" if $map->[$r]->@* != $cols;
Branch: 1 branch
BranchTrueFalseTotal
if @{$map->[$r]} != $cols 0 21 21
163 21 for my $c ( 0 .. $cols - 1 ) {
164 81 $dimap->[$r][$c] = $self->costfn->( $self, $map->[$r][$c] );
165 }
166 }
167 $self->_set_iters(
168 7 $self->normfn->( $dimap, $self->min_cost, $self->max_cost, $self->bad_cost ) );
169 7 $self->dimap($dimap);
170 7 return $self;
171 }
172  
173 sub next {
174 16 ▶ my ( $self, $r, $c, $value ) = @_;
Subroutine next: called CC=17 SCAR=29.3
Pod documented
175 16 my $dimap = $self->dimap;
176 16 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 3 13 16
177 13 my $maxrow = $dimap->$#*;
178 13 my $maxcol = $dimap->[0]->$#*;
179 13 ▶ croak "row $r out of bounds" if $r > $maxrow or $r < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $maxrow or $r < 0 0 13 13
Condition33%1/3
$r > $maxrow or $r < 0
l!l&&r!l&&!r
0013
MC/DC0%0/2
$r > $maxrow or $r < 0
$r > $maxrow $r < 0
Truth table33%1/3
A or B
A= $r > $maxrowB= $r < 0
ABresult
1X1
011
000
180 13 ▶ croak "col $c out of bounds" if $c > $maxcol or $c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > $maxcol or $c < 0 0 13 13
Condition33%1/3
$c > $maxcol or $c < 0
l!l&&r!l&&!r
0013
MC/DC0%0/2
$c > $maxcol or $c < 0
$c > $maxcol $c < 0
Truth table33%1/3
A or B
A= $c > $maxcolB= $c < 0
ABresult
1X1
011
000
181 13 my @adj;
182 13 ▶ $value //= $dimap->[$r][$c];
Condition66%2/3
$value //= $dimap->[$r][$c]
l!l&&r!l&&!r
1120
MC/DC0%0/2
$value //= $dimap->[$r][$c]
$value $dimap->[$r][$c]
Truth table66%2/3
A //= B
A= $valueB= $dimap->[$r][$c]
ABresult
1X1
011
000
183 13 ▶ return \@adj if $value <= $self->min_cost;
Branch: 1 branch
BranchTrueFalseTotal
if $value <= $self->min_cost 1 12 13
184  
185 12 for my $i ( -1, 1 ) {
186 24 my $x = $c + $i;
187 24 ▶ push @adj, [ [ $r, $x ], $dimap->[$r][$x] ] if $x >= 0 and $x <= $maxcol;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 0 and $x <= $maxcol 20 4 24
Condition66%2/3
$x >= 0 and $x <= $maxcol
!ll&&!rl&&r
4020
MC/DC50%1/2
$x >= 0 and $x <= $maxcol
$x >= 0 $x <= $maxcol
Truth table66%2/3
A and B
A= $x >= 0B= $x <= $maxcol
ABresult
0X0
100
111
188 24 for my $j ( -1 .. 1 ) {
189 72 $x = $r + $i;
190 72 my $y = $c + $j;
191 72 ▶ push @adj, [ [ $x, $y ], $dimap->[$x][$y] ]
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 0 and $x <= $maxrow and $y >= 0 and $y <= $maxcol 56 16 72
Condition77%7/9
$x >= 0 and $x <= $maxrow and $y >= 0 and $y <= $maxcol
!ll&&!rl&&r
16056
$x >= 0 and $x <= $maxrow and $y >= 0
!ll&&!rl&&r
12456
$x >= 0 and $x <= $maxrow
!ll&&!rl&&r
12060
MC/DC50%2/4
$x >= 0 and $x <= $maxrow and $y >= 0 and $y <= $maxcol
$x >= 0 $x <= $maxrow $y >= 0 $y <= $maxcol
Truth table60%3/5
A and B and C and D
A= $x >= 0B= $x <= $maxrowC= $y >= 0D= $y <= $maxcol
ABCDresult
0XXX0
10XX0
110X0
11100
11111
192 if $x >= 0
193 and $x <= $maxrow
194 and $y >= 0
195 and $y <= $maxcol;
196 }
197 }
198 12 my $badcost = $self->bad_cost;
199 12 ▶ return [ grep { $_->[1] < $value and $_->[1] != $badcost } @adj ];
Branch: 1 branch
BranchTrueFalseTotal
$_->[1] < $value and $_->[1] != $badcost 61 15 76
200 }
201  
202 sub next_best {
203 15 ▶ my ( $self, $r, $c ) = @_;
Subroutine next_best: called CC=1 SCAR=0.0
Pod documented
204 15 my $method = $self->next_m;
205 my @ret =
206 15 sort { $a->[1] <=> $b->[1] } shuffle $self->$method( $r, $c )->@*;
207 13 return $ret[0]->[0];
208 }
209  
210 # next() but only in square directions or "orthogonal" (but diagonals
211 # are orthogonal to one another) or in the "cardinal directions" (NSEW)
212 # but that term also seems unsatisfactory. "4-way" is also used for this
213 # with the assumption of cardinal directions
214 sub next_sq {
215 7 ▶ my ( $self, $r, $c, $value ) = @_;
Subroutine next_sq: called CC=13 SCAR=27.5
Pod documented
216 7 my $dimap = $self->dimap;
217 7 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 6 7
218 6 my $maxrow = $dimap->$#*;
219 6 my $maxcol = $dimap->[0]->$#*;
220 6 ▶ croak "row $r out of bounds" if $r > $maxrow or $r < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $maxrow or $r < 0 0 6 6
Condition33%1/3
$r > $maxrow or $r < 0
l!l&&r!l&&!r
006
MC/DC0%0/2
$r > $maxrow or $r < 0
$r > $maxrow $r < 0
Truth table33%1/3
A or B
A= $r > $maxrowB= $r < 0
ABresult
1X1
011
000
221 6 ▶ croak "col $c out of bounds" if $c > $maxcol or $c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > $maxcol or $c < 0 0 6 6
Condition33%1/3
$c > $maxcol or $c < 0
l!l&&r!l&&!r
006
MC/DC0%0/2
$c > $maxcol or $c < 0
$c > $maxcol $c < 0
Truth table33%1/3
A or B
A= $c > $maxcolB= $c < 0
ABresult
1X1
011
000
222 6 my @adj;
223 6 ▶ $value //= $dimap->[$r][$c];
Condition33%1/3
$value //= $dimap->[$r][$c]
l!l&&r!l&&!r
060
MC/DC0%0/2
$value //= $dimap->[$r][$c]
$value $dimap->[$r][$c]
Truth table33%1/3
A //= B
A= $valueB= $dimap->[$r][$c]
ABresult
1X1
011
000
224 6 ▶ return \@adj if $value <= $self->min_cost;
Branch: 1 branch
BranchTrueFalseTotal
if $value <= $self->min_cost 1 5 6
225  
226 5 ▶ if ( $c > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c > 0) 5 0 5
227 5 push @adj, [ [ $r, $c - 1 ], $dimap->[$r][ $c - 1 ] ];
228 }
229 5 ▶ if ( $c < $maxcol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $maxcol) 5 0 5
230 5 push @adj, [ [ $r, $c + 1 ], $dimap->[$r][ $c + 1 ] ];
231 }
232 5 ▶ if ( $r > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r > 0) 5 0 5
233 5 push @adj, [ [ $r - 1, $c ], $dimap->[ $r - 1 ][$c] ];
234 }
235 5 ▶ if ( $r < $maxrow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $maxrow) 5 0 5
236 5 push @adj, [ [ $r + 1, $c ], $dimap->[ $r + 1 ][$c] ];
237 }
238  
239 5 my $badcost = $self->bad_cost;
240 5 ▶ return [ grep { $_->[1] < $value and $_->[1] != $badcost } @adj ];
Branch: 1 branch
BranchTrueFalseTotal
$_->[1] < $value and $_->[1] != $badcost 14 6 20
241 }
242  
243 sub next_with {
244 1 ▶ my ( $self, $r, $c, $param ) = @_;
Subroutine next_with: called CC=15 SCAR=28.3
Pod documented
245 1 my $dimap = $self->dimap;
246 1 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 0 1 1
247  
248 1 my $badcost = $self->bad_cost;
249  
250 1 my $curcost = $dimap->[$r][$c];
251 1 ▶ return undef if $curcost <= $self->min_cost;
Branch: 1 branch
BranchTrueFalseTotal
if $curcost <= $self->min_cost 0 1 1
252 1 ▶ $curcost *= $param->{my_weight} // 1;
Condition50%1/2
$param->{"my_weight"} // 1
l!l
10
MC/DC0%0/1
$param->{"my_weight"} // 1
$param->{"my_weight"}
Truth table50%1/2
A
A= $param->{"my_weight"}
Aresult
11
00
253 1 my @here = map $_->values( [ $r, $c ] )->[0], $param->{objs}->@*;
254 1 for my $h ( 0 .. $#here ) {
255 # this may cause problems if something is standing on a cell
256 # they can no longer move into but where it is still legal for
257 # them to leave that cell
258 2 ▶ return undef if $here[$h] == $badcost;
Branch: 1 branch
BranchTrueFalseTotal
if $here[$h] == $badcost 0 2 2
259 2 ▶ $curcost += $here[$h] * ( $param->{weights}->[$h] // 0 );
Condition50%1/2
$param->{"weights"}[$h] // 0
l!l
20
MC/DC0%0/1
$param->{"weights"}[$h] // 0
$param->{"weights"}[$h]
Truth table50%1/2
A
A= $param->{"weights"}[$h]
Aresult
11
00
260 }
261  
262 1 my $method = $self->next_m;
263 1 my $coords = $self->$method( $r, $c, $self->max_cost );
264 1 ▶ return undef unless $coords->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @$coords 0 1 1
265 1 my @costs = map $_->values( map $_->[0], $coords->@* ), $param->{objs}->@*;
266 1 my @ret;
267 1 COORD: for my $p ( 0 .. $coords->$#* ) {
268 3 my @weights;
269 3 for my $k ( 0 .. $#costs ) {
270 5 ▶ next COORD if $costs[$k][$p] == $badcost;
Branch: 1 branch
BranchTrueFalseTotal
if $costs[$k][$p] == $badcost 1 4 5
271 4 ▶ push @weights, $costs[$k][$p] * ( $param->{weights}->[$k] // 0 );
Condition50%1/2
$param->{"weights"}[$k] // 0
l!l
40
MC/DC0%0/1
$param->{"weights"}[$k] // 0
$param->{"weights"}[$k]
Truth table50%1/2
A
A= $param->{"weights"}[$k]
Aresult
11
00
272 }
273 2 ▶ my $newcost = sum0 $coords->[$p][1] * ( $param->{my_weight} // 1 ), @weights;
Condition50%1/2
$param->{"my_weight"} // 1
l!l
20
MC/DC0%0/1
$param->{"my_weight"} // 1
$param->{"my_weight"}
Truth table50%1/2
A
A= $param->{"my_weight"}
Aresult
11
00
274 2 ▶ push @ret, [ $coords->[$p][0], $newcost ] if $newcost < $curcost;
Branch: 1 branch
BranchTrueFalseTotal
if $newcost < $curcost 1 1 2
275 }
276  
277 1 ▶ return undef unless @ret;
Branch: 1 branch
BranchTrueFalseTotal
unless @ret 0 1 1
278 1 @ret = sort { $a->[1] <=> $b->[1] } shuffle @ret;
279 1 return $ret[0]->[0];
280 }
281  
282 # 4-way "square" normalization as seen in the Brogue article (was called
283 # normalize_costs and used to be a method). one could possibly also
284 # normalize only in the diagonal directions...
285 sub norm_4way {
286 8 ▶ my ( $dimap, $mincost, $maxcost, $badcost, $adjvals ) = @_;
Subroutine norm_4way: called CC=14 SCAR=26.4
Pod documented
287 8 ▶ $adjvals //= \&adjacent_values_sq;
Condition100%2/2
$adjvals //= \&Game::DijkstraMap::adjacent_values_sq
l!l
17
MC/DC100%1/1
$adjvals //= \&Game::DijkstraMap::adjacent_values_sq
$adjvals
Truth table100%2/2
A
A= $adjvals
Aresult
11
00
288 8 my $iters = 0;
289 8 my $maxrow = $dimap->$#*;
290 8 my $maxcol = $dimap->[0]->$#*;
291 8 my $stable;
292 8 while (1) {
293 26 $stable = 1;
294 26 $iters++;
295 26 for my $r ( 0 .. $maxrow ) {
296 92 for my $c ( 0 .. $maxcol ) {
297 448 my $value = $dimap->[$r][$c];
298 448 ▶ next if $value == $badcost or $value == $mincost;
Branch: 1 branch
BranchTrueFalseTotal
if $value == $badcost or $value == $mincost 226 222 448
Condition100%3/3
$value == $badcost or $value == $mincost
l!l&&r!l&&!r
19729222
MC/DC100%2/2
$value == $badcost or $value == $mincost
$value == $badcost $value == $mincost
Truth table100%3/3
A or B
A= $value == $badcostB= $value == $mincost
ABresult
1X1
011
000
299 222 my $isneg;
300 222 ▶ if ( $value < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 40 182 222
301 40 $isneg = 1;
302 40 $value = abs $value;
303 }
304 222 my $best = $maxcost;
305 222 for my $nv ( $adjvals->( $dimap, $r, $c, $maxrow, $maxcol ) ) {
306 637 ▶ next if $nv == $badcost;
Branch: 1 branch
BranchTrueFalseTotal
if $nv == $badcost 221 416 637
307 416 $nv = abs $nv;
308 416 ▶ $best = $nv if $nv < $best;
Branch: 1 branch
BranchTrueFalseTotal
if $nv < $best 231 185 416
309 416 ▶ last if $best == $mincost;
Branch: 1 branch
BranchTrueFalseTotal
if $best == $mincost 40 376 416
310 }
311 222 ▶ if ( $value >= $best + 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= $best + 2) 52 170 222
312 52 my $newval = $best + 1;
313 52 ▶ $newval *= -1 if $isneg;
Branch: 1 branch
BranchTrueFalseTotal
if $isneg 18 34 52
314 52 $dimap->[$r][$c] = $newval;
315 52 $stable = 0;
316 }
317 }
318 }
319 26 ▶ last if $stable;
Branch: 1 branch
BranchTrueFalseTotal
if $stable 8 18 26
320 }
321 8 return $iters;
322 }
323  
324 # 8-way normalization could either be done with small integers where
325 # diagonals cost the same as square motion (this is non-Euclidean though
326 # traditional in roguelikes) ...
327 sub norm_8way {
328 1 ▶ push @_, \&adjacent_values;
Subroutine norm_8way: called CC=1 SCAR=0.0
Pod documented
329 1 &norm_4way; # perldoc perlsub explains this calling form
330 }
331  
332 # ... or one could instead use floating point values to better
333 # approximate diagonals costing sqrt(2) but this is more complicated,
334 # which is perhaps why many roguelikes use 4-way or non-Euclidean 8-way
335 sub norm_8way_euclid {
336 2 ▶ my ( $dimap, $mincost, $maxcost, $badcost ) = @_;
Subroutine norm_8way_euclid: called CC=13 SCAR=25.7
Pod documented
337 2 my $iters = 0;
338 2 my $maxrow = $dimap->$#*;
339 2 my $maxcol = $dimap->[0]->$#*;
340 2 my $stable;
341 2 while (1) {
342 5 $stable = 1;
343 5 $iters++;
344 5 for my $r ( 0 .. $maxrow ) {
345 16 for my $c ( 0 .. $maxcol ) {
346 56 my $value = $dimap->[$r][$c];
347 56 ▶ next if $value == $badcost or $value == $mincost;
Branch: 1 branch
BranchTrueFalseTotal
if $value == $badcost or $value == $mincost 9 47 56
Condition100%3/3
$value == $badcost or $value == $mincost
l!l&&r!l&&!r
4547
MC/DC100%2/2
$value == $badcost or $value == $mincost
$value == $badcost $value == $mincost
Truth table100%3/3
A or B
A= $value == $badcostB= $value == $mincost
ABresult
1X1
011
000
348 47 my $isneg;
349 47 ▶ if ( $value < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 0 47 47
350 0 $isneg = 1;
351 0 $value = abs $value;
352 }
353 47 my $best = [ $maxcost, 0 ];
354 47 for my $nr (
355 map( [ $_, 1 ], adjacent_values_sq( $dimap, $r, $c, $maxrow, $maxcol ) ),
356 map( [ $_, SQRT2 ], adjacent_values_diag( $dimap, $r, $c, $maxrow, $maxcol ) )
357 ) {
358 186 ▶ next if $nr->[0] == $badcost;
Branch: 1 branch
BranchTrueFalseTotal
if $nr->[0] == $badcost 4 182 186
359 182 $nr->[0] = abs $nr->[0];
360 182 ▶ $best = $nr if $nr->[0] < $best->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $nr->[0] < $best->[0] 84 98 182
361 182 ▶ last if $best->[0] == $mincost;
Branch: 1 branch
BranchTrueFalseTotal
if $best->[0] == $mincost 26 156 182
362 }
363 47 ▶ if ( $value > $best->[0] + SQRT2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $best->[0] + "1.4142135623731") 17 30 47
364 17 my $newval = $best->[0] + $best->[1];
365 17 ▶ $newval *= -1 if $isneg;
Branch: 1 branch
BranchTrueFalseTotal
if $isneg 0 17 17
366 17 $dimap->[$r][$c] = $newval;
367 17 $stable = 0;
368 }
369 }
370 }
371 5 ▶ last if $stable;
Branch: 1 branch
BranchTrueFalseTotal
if $stable 2 3 5
372 }
373 2 return $iters;
374 }
375  
376 sub normalize {
377 2 ▶ my ($self) = @_;
Subroutine normalize: called CC=2 SCAR=6.9
Pod documented
378 2 my $dimap = $self->dimap;
379 2 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 1 2
380 1 $self->_set_iters(
381 $self->normfn->( $dimap, $self->min_cost, $self->max_cost, $self->bad_cost ) );
382 1 return $self;
383 }
384  
385 sub path_best {
386 3 ▶ my ( $self, $r, $c, $method ) = @_;
Subroutine path_best: called CC=2 SCAR=6.9
Pod documented
387 3 my @path;
388 3 ▶ while ( my $next = $self->next_best( $r, $c, $method ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $self->next_best($r, $c, $method)) 7 2 9
389 7 push @path, $next;
390 7 ( $r, $c ) = @$next;
391 }
392 2 return \@path;
393 }
394  
395 sub recalc {
396 3 ▶ my ($self) = @_;
Subroutine recalc: called CC=6 SCAR=17.9
Pod documented
397 3 my $dimap = $self->dimap;
398 3 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 2 3
399 2 my $maxcost = $self->max_cost;
400 2 my $mincost = $self->min_cost;
401 2 my $badcost = $self->bad_cost;
402 2 my $maxcol = $dimap->[0]->$#*;
403 2 for my $r ( 0 .. $dimap->$#* ) {
404  
405 7 for my $c ( 0 .. $maxcol ) {
406 39 ▶ $dimap->[$r][$c] = $maxcost
Branch: 1 branch
BranchTrueFalseTotal
if $dimap->[$r][$c] != $mincost and $dimap->[$r][$c] != $badcost 13 26 39
Condition100%3/3
$dimap->[$r][$c] != $mincost and $dimap->[$r][$c] != $badcost
!ll&&!rl&&r
32313
MC/DC100%2/2
$dimap->[$r][$c] != $mincost and $dimap->[$r][$c] != $badcost
$dimap->[$r][$c] != $mincost $dimap->[$r][$c] != $badcost
Truth table100%3/3
A and B
A= $dimap->[$r][$c] != $mincostB= $dimap->[$r][$c] != $badcost
ABresult
0X0
100
111
407 if $dimap->[$r][$c] != $mincost and $dimap->[$r][$c] != $badcost;
408 }
409 }
410 $self->_set_iters(
411 2 $self->normfn->( $dimap, $mincost, $maxcost, $self->bad_cost ) );
412 2 $self->dimap($dimap);
413 2 return $self;
414 }
415  
416 sub str2map {
417 6 ▶ my ( $self_or_class, $str, $lf ) = @_;
Subroutine str2map: called CC=4 SCAR=14.5
Pod documented
418 6 ▶ croak "no string given" if !defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 6 6
419 6 ▶ $lf //= $/;
Condition33%1/3
$lf //= $/
l!l&&r!l&&!r
060
MC/DC0%0/2
$lf //= $/
$lf $/
Truth table33%1/3
A //= B
A= $lfB= $/
ABresult
1X1
011
000
420 6 my @map;
421 6 for my $line ( split $lf, $str ) {
422 18 push @map, [ split //, $line ];
423 }
424 6 return \@map;
425 }
426  
427 sub to_tsv {
428 3 ▶ my ( $self, $ref ) = @_;
Subroutine to_tsv: called CC=6 SCAR=17.9
Pod documented
429 3 ▶ if ( !defined $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 2 1 3
430 2 $ref = $self->dimap;
431 2 ▶ croak "cannot use an unset map" if !defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 1 1 2
432 }
433 2 my $s = '';
434 2 my $cols = $ref->[0]->$#*;
435 2 for my $r ( 0 .. $ref->$#* ) {
436 5 my $d = "\t";
437 5 for my $c ( 0 .. $cols ) {
438 13 $s .= $ref->[$r][$c] . $d;
439 13 ▶ $d = '' if $c == $cols - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $c == $cols - 1 5 8 13
440 }
441 5 $s .= $/;
442 }
443 2 return $s;
444 }
445  
446 sub unconnected {
447 3 ▶ my ($self) = @_;
Subroutine unconnected: called CC=5 SCAR=16.1
Pod documented
448 3 my $dimap = $self->dimap;
449 3 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 2 3
450 2 my @points;
451 2 my $maxcost = $self->max_cost;
452 2 my $maxcol = $dimap->[0]->$#*;
453 2 for my $r ( 0 .. $dimap->$#* ) {
454 7 for my $c ( 0 .. $maxcol ) {
455 39 ▶ push @points, [ $r, $c ] if $dimap->[$r][$c] == $maxcost;
Branch: 1 branch
BranchTrueFalseTotal
if $dimap->[$r][$c] == $maxcost 5 34 39
456 }
457 }
458 2 return \@points;
459 }
460  
461 sub update {
462 2 ▶ my $self = shift;
Subroutine update: called CC=8 SCAR=22.1
Pod documented
463 2 my $dimap = $self->dimap;
464 2 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 1 2
465 1 my $maxrow = $dimap->$#*;
466 1 my $maxcol = $dimap->[0]->$#*;
467 1 for my $ref (@_) {
468 1 my ( $r, $c ) = ( $ref->[0], $ref->[1] );
469 1 ▶ croak "row $r out of bounds" if $r > $maxrow or $r < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $maxrow or $r < 0 0 1 1
Condition33%1/3
$r > $maxrow or $r < 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$r > $maxrow or $r < 0
$r > $maxrow $r < 0
Truth table33%1/3
A or B
A= $r > $maxrowB= $r < 0
ABresult
1X1
011
000
470 1 ▶ croak "col $c out of bounds" if $c > $maxcol or $c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > $maxcol or $c < 0 0 1 1
Condition33%1/3
$c > $maxcol or $c < 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$c > $maxcol or $c < 0
$c > $maxcol $c < 0
Truth table33%1/3
A or B
A= $c > $maxcolB= $c < 0
ABresult
1X1
011
000
471 1 ▶ croak "value must be a number" unless looks_like_number $ref->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($ref->[2]) 0 1 1
472 1 $dimap->[$r][$c] = int $ref->[2];
473 }
474 1 $self->dimap($dimap);
475 1 return $self;
476 }
477  
478 sub values {
479 24 ▶ my $self = shift;
Subroutine values: called CC=7 SCAR=20.5
Pod documented
480 24 my $dimap = $self->dimap;
481 24 ▶ croak "dimap not set" if !defined $dimap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimap 1 23 24
482 23 my @values;
483 23 my $maxrow = $dimap->$#*;
484 23 my $maxcol = $dimap->[0]->$#*;
485 23 for my $point (@_) {
486 29 my ( $r, $c ) = ( $point->[0], $point->[1] );
487 29 ▶ croak "row $r out of bounds" if $r > $maxrow or $r < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > $maxrow or $r < 0 0 29 29
Condition33%1/3
$r > $maxrow or $r < 0
l!l&&r!l&&!r
0029
MC/DC0%0/2
$r > $maxrow or $r < 0
$r > $maxrow $r < 0
Truth table33%1/3
A or B
A= $r > $maxrowB= $r < 0
ABresult
1X1
011
000
488 29 ▶ croak "col $c out of bounds" if $c > $maxcol or $c < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c > $maxcol or $c < 0 0 29 29
Condition33%1/3
$c > $maxcol or $c < 0
l!l&&r!l&&!r
0029
MC/DC0%0/2
$c > $maxcol or $c < 0
$c > $maxcol $c < 0
Truth table33%1/3
A or B
A= $c > $maxcolB= $c < 0
ABresult
1X1
011
000
489 29 push @values, $dimap->[$r][$c];
490 }
491 23 return \@values;
492 }
493  
494 3 1;
495 __END__
↑ Summary