blib/lib/NetHack/FOV.pm

filter: Statementstmt 94.8% 92 / 97 Branchbran 87.1% 54 / 62 Conditioncond 76.1% 16 / 21 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 88.6% 179 / 202 CC 40 SCAR 29.0
CC
40
Cov
90%
_quadrant
29.0
_Q_path
6.0
_trace
5.0
CRAP 18.2
↑ Summary
1 #!/usr/bin/env perl
2 package NetHack::FOV;
3  
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 0.01;
10 1 our @EXPORT_OK = qw(calculate_fov);
11 1 our @ISA = qw(Exporter);
12  
13 74376 ▶sub _clear {
Subroutine _clear: called CC=1 SCAR=0.0
14 74376 my ($self, $x, $y) = @_;
15  
16 74376 return $self->{cbi}->($x + $self->{x}, $y + $self->{y});
17 }
18  
19 26899 ▶sub _see {
Subroutine _see: called CC=1 SCAR=0.0
20 26899 my ($self, $x, $y) = @_;
21  
22 26899 return $self->{cbo}->($x + $self->{x}, $y + $self->{y});
23 }
24  
25 6868 ▶sub _Q_path {
Subroutine _Q_path: called CC=6 SCAR=17.9
26 6868 my ($self, $x, $y) = @_;
27  
28 6868 my ($px, $py) = (0,0);
29  
30 6868 my $flip = abs($x) > abs($y);
31  
32 6868 ▶ my ($rmaj, $rmin) = $flip ? (\$px,\$py) : (\$py,\$px);
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 4105 2763 6868
33 6868 ▶ my ($dmaj, $dmin) = $flip ? ( $x , $y ) : ( $y , $x );
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 4105 2763 6868
34  
35 6868 my $fmin = -abs($dmaj);
36  
37 6868 for (2 .. abs($dmaj)) {
38 27625 $fmin += 2*abs($dmin);
39 27625 ▶ if ($fmin >= 0) { $fmin -= 2*abs($dmaj); $$rmin += ($dmin <=> 0); }
Branch: 1 branch
BranchTrueFalseTotal
if ($fmin >= 0) 8443 19182 27625
40 27625 $$rmaj += ($dmaj <=> 0);
41 27625 ▶ if (!$self->_clear($px, $py)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_clear($px, $py)) 4095 23530 27625
42 4095 return 0;
43 }
44 }
45  
46 2773 return 1;
47 }
48  
49 4377 ▶sub _quadrant {
Subroutine _quadrant: called CC=28 SCAR=33.7
50 4377 my ($self, $hs, $row, $left, $right_mark) = @_;
51  
52 4377 my ($right, $right_edge);
53  
54 4377 ▶ my $rail = ($hs == 1) ? 79 - $self->{x} : $self->{x};
Branch: 1 branch
BranchTrueFalseTotal
$hs == 1 ? : 2165 2212 4377
55 # Why does this have to be irregular
56  
57 4377 ▶ while ($left <= $right_mark) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left <= $right_mark) 6462 4377 10839
58 #print "in quadrant, $rail $hs $row $left $right_mark\n";
59 6462 $right_edge = $left;
60 6462 my $left_clear = $self->_clear($hs*$left, $row);
61 6462 ▶ while ($self->_clear($hs*$right_edge, $row) == $left_clear &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_clear($hs * $right_edge, $row) == $left_clear and $left_clear || $right_edge <= $right_mark + 1) 27968 6462 34430
Condition100%6/6
$self->_clear($hs * $right_edge, $row) == $left_clear and $left_clear || $right_edge <= $right_mark + 1
!ll&&!rl&&r
5018144427968
$left_clear or $right_edge <= $right_mark + 1
l!l&&r!l&&!r
17287106811444
MC/DC100%3/3
$self->_clear($hs * $right_edge, $row) == $left_clear and $left_clear || $right_edge <= $right_mark + 1
$self->_clear($hs * $right_edge, $row) == $left_clear $left_clear $right_edge <= $right_mark + 1
Truth table100%4/4
A and B or C
A= $self->_clear($hs * $right_edge, $row) == $left_clearB= $left_clearC= $right_edge <= $right_mark + 1
ABCresult
0XX0
1000
11X1
1011
62 ($left_clear || $right_edge <= $right_mark + 1))
63 27968 { $right_edge++ }
64 6462 $right_edge--;
65 6462 ▶ if ($left_clear) { $right_edge++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($left_clear) 3845 2617 6462
66  
67 6462 ▶ if ($right_edge >= $rail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_edge >= $rail) 0 6462 6462
68 0 $right_edge = $rail; # Yuck
69 }
70  
71 #print "in quadrant2, $hs $row $left $right_mark $right_edge\n";
72  
73 6462 ▶ if (!$left_clear) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($left_clear) 2617 3845 6462
74 2617 ▶ if ($right_edge > $right_mark) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_edge > $right_mark) 1612 1005 2617
75 1612 ▶ $right_edge = $self->_clear($hs*$right_mark,
Branch: 1 branch
BranchTrueFalseTotal
$self->_clear($hs * $right_mark, $row - ($row <=> 0)) ? : 279 1333 1612
76 $row - ($row <=> 0)) ? $right_mark + 1 : $right_mark;
77 }
78  
79 2617 ▶ for (my $i = $left; $i <= $right_edge; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $right_edge) 9348 2617 11965
80 9348 $self->_see($hs*$i, $row);
81 }
82 2617 $left = $right_edge + 1;
83 2617 next;
84 }
85 #print "in quadrant3, $hs $row $left $right_mark\n";
86  
87 3845 ▶ if ($left != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left != 0) 1557 2288 3845
88 1557 ▶ for (; $left <= $right_edge; $left++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left <= $right_edge) 4235 558 4793
89 4235 ▶ last if $self->_Q_path($hs*$left, $row);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_Q_path($hs * $left, $row) 999 3236 4235
90 }
91  
92 1557 ▶ if ($left >= $rail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left >= $rail) 0 1557 1557
93 # Double yuck
94 0 ▶ if ($left == $rail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left == $rail) 0 0 0
95 0 $self->_see($left*$hs, $row);
96 }
97  
98 0 return;
99 }
100  
101 1557 ▶ if ($left >= $right_edge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left >= $right_edge) 800 757 1557
102 800 $left = $right_edge;
103 800 next;
104 }
105 }
106 #print "in quadrant4, $hs $row $left $right_mark\n";
107  
108 3045 ▶ if ($right_mark < $right_edge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_mark < $right_edge) { } 1064 1981 3045
109 1064 ▶ for ($right = $right_mark; $right <= $right_edge; $right++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right <= $right_edge) 2633 205 2838
110 2633 ▶ last if !$self->_Q_path($hs*$right, $row);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_Q_path($hs * $right, $row) 859 1774 2633
111 }
112 1064 --$right;
113 }
114 1981 else { $right = $right_edge; }
115 #print "in quadrant5, $hs $row $left $right_mark\n";
116 3045 ▶ if ($left <= $right) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left <= $right) 2969 76 3045
117 2969 ▶ if ($left == $right && $left == 0 && !$self->_clear($hs,$row) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($left == $right and $left == 0 and not $self->_clear($hs, $row) and $left != $rail) 66 2903 2969
Condition88%8/9
$left == $right and $left == 0 and not $self->_clear($hs, $row) and $left != $rail
!ll&&!rl&&r
2903066
$left == $right and $left == 0 and not $self->_clear($hs, $row)
!ll&&!rl&&r
266224166
$left == $right and $left == 0
!ll&&!rl&&r
257092307
MC/DC75%3/4
$left == $right and $left == 0 and not $self->_clear($hs, $row) and $left != $rail
$left == $right $left == 0 not $self->_clear($hs, $row) $left != $rail
Truth table80%4/5
A and B and not(C) and D
A= $left == $rightB= $left == 0C= not $self->_clear($hs, $row)D= $left != $rail
ABCDresult
0XXX0
10XX0
110X0
11100
11111
118 ($left != $rail)) {
119 66 $right = 1;
120 }
121  
122 0 ▶ if ($right > $rail) { $right = $rail }
Branch: 1 branch
BranchTrueFalseTotal
if ($right > $rail) 0 2969 2969
123  
124 2969 ▶ for (my $i = $left; $i <= $right; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $right) 13259 2969 16228
125 13259 $self->_see($hs*$i,$row);
126 }
127  
128 2969 $self->_quadrant($hs, $row + ($row <=> 0),$left,$right);
129 2969 $left = $right + 1;
130 }
131 #print "in quadrant6, $hs $row $left $right_mark\n";
132 }
133 }
134  
135 352 ▶sub _trace {
Subroutine _trace: called CC=5 SCAR=16.1
136 352 my $self = shift;
137  
138 352 my ($xl, $xr) = (0, 0);
139  
140 352 $self->_see(0,0);
141  
142 #for my $i (-2 .. 2) { print ($self->_clear($i,0) ? "1" : "0"); }
143 #print "\n";
144  
145 352 ▶ do { $self->_see(--$xl,0) } while $self->_clear($xl,0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_clear($xl, 0) and do { do { $self->_see(--$xl, 0) } } 1618 352 1970
146 352 ▶ do { $self->_see(++$xr,0) } while $self->_clear($xr,0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_clear($xr, 0) and do { do { $self->_see(++$xr, 0) } } 1618 352 1970
147  
148 # Triple yuck
149 352 ▶ $xr-- if $xr + $self->{x} == 80;
Branch: 1 branch
BranchTrueFalseTotal
if $xr + $self->{"x"} == 80 0 352 352
150 352 ▶ $xl++ if $xl + $self->{x} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $xl + $self->{"x"} < 0 0 352 352
151  
152 #print "$xl $xr\n";
153  
154 352 $self->_quadrant(-1,-1,0,-$xl);
155 352 $self->_quadrant(+1,-1,0,$xr);
156 352 $self->_quadrant(-1,+1,0,-$xl);
157 352 $self->_quadrant(+1,+1,0,$xr);
158 }
159  
160 # not handled: swimming, phasing
161 # possibly buggy: everything
162 352 ▶sub calculate_fov {
Subroutine calculate_fov: called CC=2 SCAR=7.5
Pod documented
163 352 my ($startx, $starty, $cb, $cbo) = @_;
164  
165 352 my @visible;
166  
167 352 my $self = bless { x => $startx, y => $starty, cbi => $cb, cbo => $cbo };
168  
169 26899 ▶ $self->{cbo} ||= sub { my ($x, $y) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.5
170 352 ▶ $visible[$x][$y] = 1 unless $x < 0 || $y < 0; };
Branch: 1 branch
BranchTrueFalseTotal
unless $x < 0 or $y < 0 26899 0 26899
Condition33%2/6
$self->{"cbo"} ||= sub { my($x, $y) = @_; $visible[$x][$y] = 1 unless $x < 0 or $y < 0; }
l!l&&r!l&&!r
03520
$x < 0 or $y < 0
l!l&&r!l&&!r
0026899
MC/DC0%0/2
$self->{"cbo"} ||= sub { my($x, $y) = @_; $visible[$x][$y] = 1 unless $x < 0 or $y < 0; }
$self->{"cbo"} sub { my($x, $y) = @_; $visible[$x][$y] = 1 unless $x < 0 or $y < 0; }
MC/DC0%0/2
$x < 0 or $y < 0
$x < 0 $y < 0
Truth table33%1/3
A ||= B
A= $self->{"cbo"}B= sub { my($x, $y) = @_; $visible[$x][$y] = 1 unless $x < 0 or $y < 0; }
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $x < 0B= $y < 0
ABresult
1X1
011
000
171  
172 352 $self->_trace();
173  
174 352 return \@visible;
175 }
176  
177 1 1;
178  
179 =head1 NAME
180  
181 NetHack::FOV - NetHack compatible field of view
182  
183 =head1 SYNOPSIS
184  
185 use NetHack::FOV 'calculate_fov';
186  
187 my $AoA = calculate_fov($x, $y, \&transparent);
188  
189 =head1 DESCRIPTION
190  
191 This package implements field of view (the determination, for every
192 square on the map simultaneously, of whether it is visible to the
193 avatar), in a NetHack compatible way. It is expected to be primarily
194 useful to bot writers.
195  
196 =head1 FUNCTION
197  
198 NetHack::FOV defines and allows import of a single function.
199  
200 =over 4
201  
202 =item B<calculate_fov STARTX, STARTY, INCALLBACK, [OUTCALLBACK]>
203  
204 STARTX and STARTY determine the location of the avatar on the integer
205 plane used by FOV::NetHack. INCALLBACK is used to determine the map's
206 local structure; it is passed two arguments, X and Y coordinates, and
207 must return true iff the specified point is transparent. OUTCALLBACK
208 is used to return the viewable map, one coordinate pair at a time as
209 for INCALLBACK. OUTCALLBACK is optional; if you omit it, calculate_fov
210 will return an array of arrays such that $ret[$x][$y] will be true
211 iff ($x,$y) is visible.
212  
213 Obviously, calculate_fov will hang if passed a map which has lines of
214 sight with infinite length. Also, if the visible part of the map
215 extends beyond the doubly non-negative quadrant, and you are using
216 the array of arrays return method, only the part which lies within
217 said quadrant will be returned. Due to unusual boundary conditions
218 of the NetHack FOV algorithm, this module will misbehave if passed
219 data outside the range of 1 to 79 inclusive in the horizontal
220 dimension; no such restriction exists vertically.
221  
222 You may be wondering why the callbacks exist at all and calculate_fov
223 doesn't just use arrays of arrays both ways. The answer is asymptotic
224 complexity. The algorithm used by calculate_fov takes time proportional
225 to the number of I<visible> tiles. If an array of arrays had to be
226 constructed for the transparency data, any user would suffer time costs
227 proportional to the number of I<total> tiles.
228  
229 =back
230  
231 =head1 AUTHOR
232  
233 Stefan O'Rear <stefanor@cox.net>
234  
235 =head1 COPYRIGHT
236  
237 Copyright 2008 Stefan O'Rear.
238  
239 This program is free software; you can redistribute it and/or modify it
240 under the same terms as Perl itself.
241  
242 =cut
243  
↑ Summary