blib/lib/Games/RolePlay/MapGen/Tools.pm

filter: Statementstmt 94.4% 238 / 252 Branchbran 67.0% 55 / 82 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 97.3% 37 / 38 Podpod 0.0% 0 / 6 totaltotal 87.0% 336 / 386 CC 63 SCAR 19.4
CC
63
Cov
88%
add_rectangle
16.1
interconnect_map
14.0
disconnect_map
7.9
CRAP 7.0
« blib/lib/Games/RolePlay/MapGen/Generator/Perfect.pm ↑ Summary blib/lib/Games/RolePlay/MapGen/Exporter/SVG/_GDSVG.pm »
1 # vi:tw=0 syntax=perl:
2  
3 # package ::_interconnected_map {{{
4 package Games::RolePlay::MapGen::_disallow_autoviv;
5  
6 18 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use parent -norequire => 'Tie::StdArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 1;
12  
13 584 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
14 584 my $class = shift;
15 584 my $this = bless [], $class;
16 584 my $that = shift;
17  
18 584 @$this = @$that;
19  
20 584 $this;
21 }
22  
23 536215 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.1
24 536215 my $this = shift;
25
26 536215 ▶ croak "autovivifing new rows and columns is disabled ($_[0]>$#$this)" if $_[0] > $#$this;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] > $this->$#* 0 536215 536215
27  
28 536215 $this->SUPER::FETCH(@_);
29 }
30  
31 package Games::RolePlay::MapGen::_interconnected_map;
32  
33 18 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 18 1;
37  
38 # interconnect_map {{{
39 24 ▶sub interconnect_map {
Subroutine interconnect_map: called CC=14 SCAR=26.4
40 24 my $map = shift;
41  
42 # This interconnected array stuff is _REALLY_ handy, but it needs to be cleaned up, so it gets it's own class
43  
44 24 for($map, @$map) {
45 584 my @a;
46 584 tie @a, "Games::RolePlay::MapGen::_disallow_autoviv", $_;
47 584 $_ = \@a;
48 }
49  
50 24 for my $i (0 .. $#$map) {
51 560 my $jend = $#{ $map->[$i] };
52  
53 560 for my $j (0 .. $jend) {
54 13850 $map->[$i][$j]->{nb} = {}; # clear it all out
55 13850 ▶ $map->[$i][$j]->{nb}{s} = $map->[$i+1][$j] unless $i == $#$map;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $map->$#* 13290 560 13850
56 13850 ▶ $map->[$i][$j]->{nb}{n} = $map->[$i-1][$j] unless $i == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == 0 13290 560 13850
57 13850 ▶ $map->[$i][$j]->{nb}{e} = $map->[$i][$j+1] unless $j == $jend;
Branch: 1 branch
BranchTrueFalseTotal
unless $j == $jend 13290 560 13850
58 13850 ▶ $map->[$i][$j]->{nb}{w} = $map->[$i][$j-1] unless $j == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $j == 0 13290 560 13850
59 }
60 }
61  
62 # check
63 24 for my $y (0 .. $#$map) {
64 560 for my $x (0 .. $#{ $map->[$y] }) {
65 13850 for my $d (qw(n e s w)) {
66 55400 my $o = {n=>"s", s=>"n", e=>"w", w=>"e"}->{$d};
67  
68 55400 my $t = $map->[$y][$x];
69 55400 my $n = $t->{nb}{$d};
70  
71 55400 ▶ next unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 2240 53160 55400
72  
73 53160 ▶ warn "od issues with ($x, $y):$d-$o" unless $t->{od}{$d} == $n->{od}{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"od"}{$d} == $n->{"od"}{$o} 0 53160 53160
74 53160 ▶ warn "nb issues with ($x, $y):$d-$o" unless $n->{nb}{$o} == $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $n->{"nb"}{$o} == $t 0 53160 53160
75 }
76 }
77 }
78 }
79 # }}}
80 # disconnect_map {{{
81 22 ▶sub disconnect_map {
Subroutine disconnect_map: called CC=7 SCAR=20.7
82 22 my $map = shift;
83  
84 22 local $@;
85  
86 22 eval {
87 22 untie @$_ for grep {tied $_} @$map;
88 22 ▶ untie @$map if tied $map;
Branch: 1 branch
BranchTrueFalseTotal
if tied $map 0 22 22
89  
90 22 for my $i (0 .. $#$map) {
91 530 my $jend = $#{ $map->[$i] };
92  
93 530 for my $j (0 .. $jend) {
94 # Destroying the map wouldn't destroy the tiles if they're self
95 # referencing like this. That's not a problem because of the
96 # global destructor, *whew*; except that each new map generated,
97 # until perl exits, would eat up more memory.
98  
99 13400 delete $map->[$i][$j]{nb}; # So we have to break the self-refs here.
100 }
101 }
102 };
103  
104 22 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 22 22
105 # NOTE: The above emits a fatal under global destruction for some
106 # reason, probably the bless+tie gets cleaned up in the wrong order or
107 # something. It doesn't really matter since we're already exiting perl
108 # anyway. This assumption may be false under win32, where it may
109 # create a memory leak. Does windows clean up when a process exits?
110 # It really aught to, but I have my doubts.
111  
112 0 ▶ die $@ unless $@ =~ m/global destruction/;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /global destruction/u 0 0 0
113 }
114  
115 # You can test to make sure the tiles are dying when a map goes out of
116 # scope by setting the VERBOSE_TILE_DEATH environment variable to a true
117 # value. If they fail to die when they go out of scope, it would say so on
118 # the warning line. If you'd really really like to see that, change the
119 # {nb} above to {nb_borked} and you'll see what I mean.
120  
121 # Lastly, if you'd like to read a lengthy dissertation on this subject,
122 # search for "Two-Phased" in the perlobj man page.
123 }
124 # }}}
125 # new {{{
126 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
127 9 my $class = shift;
128 9 my $arg = shift;
129 9 my $map = bless $arg, $class;
130  
131 9 $map->interconnect_map; # also used by save_map()
132  
133 9 return $map;
134 }
135 # }}}
136 # DESTROY {{{
137 9 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
138 9 my $map = shift;
139  
140 9 $map->disconnect_map; # also used by save_map()
141 }
142 # }}}
143  
144 # }}}
145 # package ::_group; {{{
146 package Games::RolePlay::MapGen::_group;
147  
148 18 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 18 1;
151  
152 # new {{{
153 52 ▶sub new {
Subroutine new: called CC=4 SCAR=20.4
154 52 my $class = shift;
155 52 my $this = bless {name=>"?", type=>"?", loc=>[], size=>[], loc_size=>"n/a"}, $class;
156  
157 52 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 52 52
158 0 my $h = {@_};
159 0 ▶ $this->{name} = $h->{name} if exists $h->{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{"name"} 0 0 0
160 0 ▶ $this->{type} = $h->{type} if exists $h->{type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{"type"} 0 0 0
161 }
162  
163 $this
164 52 }
165 # }}}
166 # name {{{
167 30 ▶sub name {
Subroutine name: called CC=2 SCAR=7.1
168 30 my $this = shift;
169 30 ▶ $this->{name} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 30 0 30
170  
171 30 $this->{name};
172 }
173 # }}}
174 # type {{{
175 51 ▶sub type {
Subroutine type: called CC=2 SCAR=7.1
176 51 my $this = shift;
177 51 ▶ $this->{type} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 51 0 51
178  
179 51 $this->{type};
180 }
181 # }}}
182 # desc {{{
183 0 ▶sub desc {
Subroutine desc: not called CC=1 SCAR=6.9
184 0 my $this = shift;
185  
186 0 $this->{loc_size};
187 }
188 # }}}
189 # add_rectangle {{{
190 58 ▶sub add_rectangle {
Subroutine add_rectangle: called CC=15 SCAR=27.8
191 58 my $this = shift;
192 58 my $loc = shift;
193 58 my $size = shift;
194 58 my $mapo = shift;
195  
196 58 ▶ if( $loc and $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc and $size) 51 7 58
Condition66%2/3
$loc and $size
!ll&&!rl&&r
7051
MC/DC50%1/2
$loc and $size
$loc $size
Truth table66%2/3
A and B
A= $locB= $size
ABresult
0X0
100
111
197 51 push @{$this->{loc}}, $loc;
198 51 push @{$this->{size}}, $size;
199 }
200  
201 58 my @i = map { $_->[0] }
202 0 sort { $b->[1]<=>$a->[2] }
203 58 map { my $t = $this->{size}[$_]; [$_, $t->[0]*$t->[1]] }
204 58 0 .. $#{$this->{loc}};
205  
206 58 my @to_kill; # remove these, they don't say anything
207 my %points; # don't count the same tiles over and over
208 58 my $sloc = [0,0];
209 58 my $mloc = [@{$this->{loc}[0]}];
210 58 my $Mloc = [@{$this->{loc}[0]}];
211 58 my $nloc = 0;
212 58 for my $i (@i) {
213 58 my $l = $this->{loc}[$i];
214 58 my $s = $this->{size}[$i];
215  
216 58 my $x = $l->[0];
217 58 my $y = $l->[1];
218  
219 58 my $i_count = 0;
220  
221 58 for my $xi (0 .. $s->[0]-1) {
222 240 for my $yi (0 .. $s->[1]-1) {
223 1302 my $xc = $x + $xi;
224 1302 my $yc = $y + $yi;
225  
226 1302 ▶ unless( $points{$xc}{$yc} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($points{$xc}{$yc}) 1302 0 1302
227 1302 $points{$xc}{$yc} = 1;
228 1302 $i_count ++;
229  
230 1302 $sloc->[0] += $xc;
231 1302 $sloc->[1] += $yc;
232 1302 $nloc ++;
233  
234 1302 ▶ $mloc->[0] = $xc if $xc < $mloc->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $xc < $mloc->[0] 0 1302 1302
235 1302 ▶ $mloc->[1] = $yc if $yc < $mloc->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $yc < $mloc->[1] 0 1302 1302
236 1302 ▶ $Mloc->[0] = $xc if $xc > $Mloc->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $xc > $Mloc->[0] 182 1120 1302
237 1302 ▶ $Mloc->[1] = $yc if $yc > $Mloc->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $yc > $Mloc->[1] 188 1114 1302
238  
239 1302 ▶ $mapo->[ $yc ][ $xc ]{group} = $this if $mapo;
Branch: 1 branch
BranchTrueFalseTotal
if $mapo 0 1302 1302
240 }
241  
242 }}
243  
244 58 ▶ push @to_kill, $i unless $i_count>0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i_count > 0 0 58 58
245 }
246  
247 0 for my $kill (sort {$b<=>$a} @to_kill) {
248 0 splice @{$this->{loc}}, $kill, 0;
249 0 splice @{$this->{size}}, $kill, 0;
250 }
251  
252 58 my $cloc = [0,0];
253 58 ▶ $cloc = [ int($sloc->[0]/$nloc), int($sloc->[1]/$nloc) ] if $nloc > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nloc > 0 58 0 58
254  
255 58 my $extent = [ $Mloc->[0]-$mloc->[0]+1, $Mloc->[1]-$mloc->[1]+1 ];
256  
257 58 $this->{loc_size} = "($cloc->[0], $cloc->[1]) $extent->[0]x$extent->[1]";
258 58 $this->{extents} = [ @$mloc, @$Mloc ];
259 }
260 # }}}
261 # enumerate_tiles {{{
262 214 ▶sub enumerate_tiles {
Subroutine enumerate_tiles: called CC=5 SCAR=16.1
263 214 my $this = shift;
264  
265 214 my @i = map { $_->[0] }
266 0 sort { $b->[1]<=>$a->[2] }
267 214 map { my $t = $this->{size}[$_]; [$_, $t->[0]*$t->[1]] }
268 214 0 .. $#{$this->{loc}};
269  
270 214 my @ret;
271 my %points; # don't count the same tiles over and over
272 214 for my $i (@i) {
273 214 my $l = $this->{loc}[$i];
274 214 my $s = $this->{size}[$i];
275  
276 214 my $x = $l->[0];
277 214 my $y = $l->[1];
278  
279 214 for my $xi (0 .. $s->[0]-1) {
280 528 for my $yi (0 .. $s->[1]-1) {
281 1462 my $xc = $x + $xi;
282 1462 my $yc = $y + $yi;
283  
284 1462 ▶ unless( $points{$xc}{$yc} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($points{$xc}{$yc}) 1462 0 1462
285 1462 $points{$xc}{$yc} = 1;
286  
287 1462 push @ret, [$xc,$yc];
288 }
289  
290 }}
291 }
292  
293 214 @ret;
294 }
295 # }}}
296 # enumerate_extents {{{
297 122 ▶sub enumerate_extents {
Subroutine enumerate_extents: called CC=1 SCAR=0.0
298 122 my $this = shift;
299  
300 122 @{ $this->{extents} };
301 }
302 # }}}
303  
304 # }}}
305 # package ::_tile; {{{
306 package Games::RolePlay::MapGen::_tile;
307  
308 18 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
309  
310 18 1;
311  
312 1350 ▶sub dup {
Subroutine dup: called CC=3 SCAR=11.0
313 1350 my $that = shift;
314 1350 my $class = $that->{__c};
315 1350 my $this = bless {od=>{n=>1, s=>1, e=>1, w=>1}}, $class;
316  
317 1350 $this->{$_} = $that->{$_} for grep {not ref $that->{$_}} keys %$that;
318 1350 $this->{od}{$_} = $that->{od}{$_} for keys %{ $that->{od} };
319 1350 $this->{group} = $that->{group};
320 1350 $this->{_dup} = 1;
321  
322 1350 return $this;
323 }
324  
325 4426 ▶sub new { my $class = shift; bless { @_, __c=>$class, v=>0, od=>{n=>0, s=>0, e=>0, w=>0} }, $class }
Subroutine new: called CC=1 SCAR=0.0
326 5776 ▶sub DESTROY { warn "tile verbosely dying" if $ENV{VERBOSE_TILE_DEATH} } # search for VERBOSE above...
Subroutine DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"VERBOSE_TILE_DEATH"} 0 5776 5776
327 # }}}
328 # package ::_door; {{{
329 package Games::RolePlay::MapGen::_door;
330  
331 18 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
332  
333 18 1;
334  
335 63 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
336 63 my $class = shift;
337 63 my $this = bless { @_ }, $class;
338  
339 63 ▶ $this->{locked} = 0 unless $this->{locked};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"locked"} 31 32 63
340 63 ▶ $this->{stuck} = 0 unless $this->{stuck};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"stuck"} 54 9 63
341 63 ▶ $this->{secret} = 0 unless $this->{secret};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"secret"} 49 14 63
342 63 ▶ $this->{open_dir} = { major=>undef, minor=>undef } unless ref($this->{open_dir});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this->{"open_dir"} 0 63 63
343 63 ▶ $this->{'open'} = 0 unless $this->{'open'};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"open"} 63 0 63
344  
345 63 return $this;
346 }
347  
348 # }}}
349  
350 package Games::RolePlay::MapGen::Tools;
351  
352 18 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
353 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
354 18 ▶use parent q(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
355  
356 18 our @EXPORT_OK = qw(choice roll random irange range str_eval _group _tile _door);
357  
358 18 1;
359  
360 # helper functions
361 # choice {{{
362 24859 ▶sub choice {
Subroutine choice: called CC=2 SCAR=6.9
Pod undocumented
363 24859 ▶ return $_[&random(int @_)] || "";
Condition100%2/2
$_[&random(int @_)] || ''
l!l
204354424
MC/DC100%1/1
$_[&random(int @_)] || ''
$_[&random(int @_)]
Truth table100%2/2
A
A= $_[&random(int @_)]
Aresult
11
00
364 }
365 # }}}
366 # roll {{{
367 15892 ▶sub roll {
Subroutine roll: called CC=2 SCAR=6.9
Pod undocumented
368 15892 my ($num, $sides) = @_;
369 15892 my $roll = 0;
370
371 15892 $roll += int rand $sides for 1 .. $num;
372 15892 $roll += $num;
373  
374 15892 return $roll;
375 }
376 # }}}
377 # random {{{
378 24876 ▶sub random {
Subroutine random: called CC=1 SCAR=0.0
Pod undocumented
379 24876 return int rand shift;
380 }
381 # }}}
382 # range {{{
383 306045 ▶sub range {
Subroutine range: called CC=6 SCAR=18.1
Pod undocumented
384 306045 my $lhs = shift;
385 306045 my $rhs = shift;
386 306045 my $correlation = shift;
387  
388 306045 ▶ ($lhs, $rhs) = ($rhs, $lhs) if $rhs < $lhs;
Branch: 1 branch
BranchTrueFalseTotal
if $rhs < $lhs 0 306045 306045
389  
390 306045 my $rand;
391 306045 ▶ if( $correlation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($correlation) { } 400 305645 306045
392 400 ▶ croak "correlated range without previous value!!" unless defined $global::last_rand;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $global::last_rand 0 400 400
393  
394 400 ▶ if( $correlation == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($correlation == 1) { } 200 200 400
elsif ($correlation == -1) { } 200 0 200
395 200 $rand = $global::last_rand;
396  
397 } elsif( $correlation == -1 ) {
398 200 $rand = 1000000.0 - $global::last_rand;
399  
400 } else {
401 0 croak "unsupported correlation value";
402 }
403  
404 } else {
405 305645 $rand = rand 1000000.0;
406 305645 $global::last_rand = $rand;
407  
408 }
409  
410 306045 $rand /= 1000000.0;
411  
412 306045 my $diff = $rhs - $lhs;
413 306045 $rand = $rand * $diff;
414  
415 306045 return $lhs + $rand;
416 }
417 # }}}
418 # irange {{{
419 100060 ▶sub irange {
Subroutine irange: called CC=2 SCAR=6.9
Pod undocumented
420 100060 my $il = shift;
421 100060 my $ir = shift;
422  
423 100060 $il -= 0.4999999999;
424 100060 $ir += 0.4999999999;
425  
426 100060 my $s = sprintf('%0.0f', range($il, $ir, @_));
427  
428 100060 ▶ $s = 0 if $s eq "-0";
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "-0" 5005 95055 100060
429  
430 100060 return $s;
431 }
432 # }}}
433 # str_eval {{{
434 37 ▶sub str_eval {
Subroutine str_eval: called CC=4 SCAR=13.9
Pod undocumented
435 37 my $str = shift;
436  
437 37 ▶ return int $str if $str =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\d+$/u 1 36 37
438  
439 36 $str =~ s/^\s*(\d+)d(\d+)\s*$/&roll($1, $2)/eg;
440 36 ▶ $str =~ s/^\s*(\d+)d(\d+)\s*([\+\-])\s*(\d+)$/&roll($1, $2) + ($3 eq "+" ? $4 : 0-$4)/eg;
Branch: 1 branch
BranchTrueFalseTotal
$3 eq '+' ? : 10 6 16
441  
442 36 ▶ return undef if $str =~ m/\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /\D/u 1 35 36
443 35 return int $str;
444 }
445 # }}}
446  
447 52 ▶sub _group { return new Games::RolePlay::MapGen::_group(@_) }
Subroutine _group: called CC=1 SCAR=0.0
448 4426 ▶sub _tile { return new Games::RolePlay::MapGen::_tile(@_) }
Subroutine _tile: called CC=1 SCAR=0.0
449 63 ▶sub _door { return new Games::RolePlay::MapGen::_door(@_) }
Subroutine _door: called CC=1 SCAR=0.0
450  
451 __END__
« blib/lib/Games/RolePlay/MapGen/Generator/Perfect.pm ↑ Summary blib/lib/Games/RolePlay/MapGen/Exporter/SVG/_GDSVG.pm »