Caves.pm

filter: Statementstmt 100% 74 / 74 Branchbran 80.9% 34 / 42 Conditioncond 84.0% 21 / 25 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 88.8% 143 / 161 CC 40 SCAR 29.6
CC
40
Cov
91%
generate_cave
22.8
outline_walls
19.1
BEGIN
1.0
CRAP 19.3
↑ Summary demo.pl »
1 package Games::Roguelike::Caves;
2  
3 #use 5.008000;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #require Exporter;
8  
9 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our @EXPORT = qw(
12 generate_cave
13 outline_walls
14 );
15  
16 1 our $VERSION = '0.01';
17  
18 #use cellular automata to carve out a decent cave
19 #initially contain 45% walls at random
20 #a tile becomes or remains a wall if the 3x3 region centered on it contains at least 5 walls.
21 #use 1 to represent wall, 0 is space
22 1 ▶sub generate_cave{
Subroutine generate_cave: called CC=22 SCAR=31.3
Pod undocumented
23 1 my ($w, $h, $iterations, $percentWalls, $wall, $floor) = @_;
24 1 ▶ die 'dimensions?' unless ($w and $h);
Branch: 1 branch
BranchTrueFalseTotal
unless $w and $h 0 1 1
Condition33%1/3
$w and $h
!ll&&!rl&&r
001
MC/DC0%0/2
$w and $h
$w $h
Truth table33%1/3
A and B
A= $wB= $h
ABresult
0X0
100
111
25 1 ▶ $iterations ||= 12;
Condition50%1/2
$iterations ||= 12
l!l
01
MC/DC0%0/1
$iterations ||= 12
$iterations
Truth table50%1/2
A
A= $iterations
Aresult
11
00
26 1 ▶ $percentWalls ||= .45;
Condition50%1/2
$percentWalls ||= "0.45"
l!l
01
MC/DC0%0/1
$percentWalls ||= "0.45"
$percentWalls
Truth table50%1/2
A
A= $percentWalls
Aresult
11
00
27 1 ▶ $percentWalls /= 100 if $percentWalls>1; # in case it's .45 or something
Branch: 1 branch
BranchTrueFalseTotal
if $percentWalls > 1 0 1 1
28 1 ▶ $wall = ' ' unless defined $wall;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wall 1 0 1
29 1 ▶ $floor = '.' unless defined $floor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $floor 1 0 1
30
31 1 my @terrain = ();
32 1 my @nextStep = ();
33 1 for my $x (0..$w-1){
34 50 for my $y (0..$h-1){
35 1000 ▶ $terrain[$y][$x] = rand()<$percentWalls ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
rand() < $percentWalls ? : 454 546 1000
36 }
37 }
38 1 for (1..$iterations){
39 12 for my $x (0..$w-1){
40 600 for my $y (0..$h-1){
41 12000 ▶ if ( !$x or !$y or $x==$w-1 or $y==$h-1){
Branch: 1 branch
BranchTrueFalseTotal
if (not $x && $y or $x == $w - 1 or $y == $h - 1) 1632 10368 12000
Condition100%9/9
not $x && $y or $x == $w - 1 or $y == $h - 1
l!l&&r!l&&!r
105657610368
not $x && $y or $x == $w - 1
l!l&&r!l&&!r
82822810944
$x && $y
!ll&&!rl&&r
24058811172
MC/DC100%4/4
not $x && $y or $x == $w - 1 or $y == $h - 1
$x $y $x == $w - 1 $y == $h - 1
Truth table100%5/5
not(A && B) or C or D
A= $xB= $yC= $x == $w - 1D= $y == $h - 1
ABCDresult
0XXX1
10XX1
111X1
11011
11000
42 #we're at edge: be wall.
43 1632 $nextStep[$y][$x] = 1;
44 1632 next;
45 }
46 10368 my $c=0;
47 #count walls in 3x3 square
48 10368 $c += $terrain[$y-1][$x-1];
49 10368 $c += $terrain[$y-1][$x];
50 10368 $c += $terrain[$y-1][$x+1];
51 10368 $c += $terrain[$y] [$x-1];
52 10368 $c += $terrain[$y] [$x];
53 10368 $c += $terrain[$y] [$x+1];
54 10368 $c += $terrain[$y+1][$x-1];
55 10368 $c += $terrain[$y+1][$x];
56 10368 $c += $terrain[$y+1][$x+1];
57 10368 ▶ $nextStep[$y][$x] = $c>4 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$c > 4 ? : 5099 5269 10368
58 }
59 }
60 #swap arrays using typeglobs
61 #(*terrain,*nextStep) = (*nextStep,*terrain)
62 12 my @tmp = @terrain;
63 12 @terrain = @nextStep;
64 12 @nextStep = @tmp;
65 }
66 #translate to cave wall or floor
67 1 for my $x (0..$w-1){
68 50 for my $y (0..$h-1){
69 1000 ▶ $terrain[$y][$x] = $terrain[$y][$x] ? $wall : $floor;
Branch: 1 branch
BranchTrueFalseTotal
$terrain[$y][$x] ? : 574 426 1000
70 #print STDOUT $terrain[$y][$x];
71 }
72 #print STDOUT "\n";
73 }
74 1 return \@terrain;
75 }
76  
77 1 ▶sub outline_walls{
Subroutine outline_walls: called CC=19 SCAR=29.5
Pod undocumented
78 1 my ($terrain, $wall, $floor) = @_;
79 1 my $h = $#$terrain + 1;
80 1 ▶ die 'empty map' unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 1 1
81 1 my $w = $#{$terrain->[0]} + 1;
82 1 ▶ die 'empty row' unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 1 1
83 1 ▶ $floor = '.' unless defined $floor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $floor 1 0 1
84 1 ▶ $wall = ' ' unless defined $wall;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wall 1 0 1
85
86 1 ▶ no warnings; #yeah. sometimes this checks tiles outide of $terrain.
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 for my $x (0..$w-1){
88 50 for my $y (0..$h-1){
89 1000 ▶ next if $terrain->[$y][$x] eq $floor; #is floor
Branch: 1 branch
BranchTrueFalseTotal
if $terrain->[$y][$x] eq $floor 426 574 1000
90 574 my ($v,$h)=(0,0); #vert/horiz weighting
91 574 ▶ $v++ if $terrain->[$y][$x-1] eq $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $terrain->[$y][$x - 1] eq $floor 59 515 574
92 574 ▶ $v++ if $terrain->[$y][$x+1] eq $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $terrain->[$y][$x + 1] eq $floor 59 515 574
93 574 ▶ $h++ if $terrain->[$y-1][$x] eq $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $$terrain[$y - 1][$x] eq $floor 74 500 574
94 574 ▶ $h++ if $terrain->[$y+1][$x] eq $floor;
Branch: 1 branch
BranchTrueFalseTotal
if $$terrain[$y + 1][$x] eq $floor 74 500 574
95 574 ▶ if ($h>$v){
Branch: 3 branches
BranchTrueFalseTotal
if ($h > $v) { } 76 498 574
elsif ($v > $h) { } 48 450 498
elsif ($v) { } 70 380 450
96 76 $terrain->[$y][$x] = '-';
97 }
98 elsif ($v>$h){
99 48 $terrain->[$y][$x] = '|';
100 }
101 elsif($v){ #maybe a corner. either will do
102 70 $terrain->[$y][$x] = '-'
103 }
104 else{ #might border nothing
105 380 ▶ if ($terrain->[$y-1][$x-1] eq $floor or
Branch: 1 branch
BranchTrueFalseTotal
if ($$terrain[$y - 1][$x - 1] eq $floor or $$terrain[$y - 1][$x + 1] eq $floor or $$terrain[$y + 1][$x - 1] eq $floor or $$terrain[$y + 1][$x + 1] eq $floor) 84 296 380
Condition100%9/9
$$terrain[$y - 1][$x - 1] eq $floor or $$terrain[$y - 1][$x + 1] eq $floor or $$terrain[$y + 1][$x - 1] eq $floor or $$terrain[$y + 1][$x + 1] eq $floor
l!l&&r!l&&!r
6420296
$$terrain[$y - 1][$x - 1] eq $floor or $$terrain[$y - 1][$x + 1] eq $floor or $$terrain[$y + 1][$x - 1] eq $floor
l!l&&r!l&&!r
4222316
$$terrain[$y - 1][$x - 1] eq $floor or $$terrain[$y - 1][$x + 1] eq $floor
l!l&&r!l&&!r
1527338
MC/DC100%4/4
$$terrain[$y - 1][$x - 1] eq $floor or $$terrain[$y - 1][$x + 1] eq $floor or $$terrain[$y + 1][$x - 1] eq $floor or $$terrain[$y + 1][$x + 1] eq $floor
$$terrain[$y - 1][$x - 1] eq $floor $$terrain[$y - 1][$x + 1] eq $floor $$terrain[$y + 1][$x - 1] eq $floor $$terrain[$y + 1][$x + 1] eq $floor
Truth table100%5/5
A or B or C or D
A= $$terrain[$y - 1][$x - 1] eq $floorB= $$terrain[$y - 1][$x + 1] eq $floorC= $$terrain[$y + 1][$x - 1] eq $floorD= $$terrain[$y + 1][$x + 1] eq $floor
ABCDresult
1XXX1
01XX1
001X1
00011
00000
106 $terrain->[$y-1][$x+1] eq $floor or
107 $terrain->[$y+1][$x-1] eq $floor or
108 $terrain->[$y+1][$x+1] eq $floor)
109 {
110 84 $terrain->[$y][$x] = '|';
111 }
112 } #else it stays as ' '
113 }
114 }
115 1 delete $terrain->[$h]; #this last row autovivified
116 }
117  
118 1 1;
119 __END__
↑ Summary demo.pl »