| 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
Condition33%1/3
$w and $h
MC/DC0%0/2
$w and $h
$w
$h
Truth table33%1/3
A and B
A= $wB= $h
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | $iterations ||= 12; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$iterations ||= 12
MC/DC0%0/1
$iterations ||= 12
$iterations
Truth table50%1/2
A
A= $iterations
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | $percentWalls ||= .45; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$percentWalls ||= "0.45"
MC/DC0%0/1
$percentWalls ||= "0.45"
$percentWalls
Truth table50%1/2
A
A= $percentWalls
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | $percentWalls /= 100 if $percentWalls>1; # in case it's .45 or something | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | $wall = ' ' unless defined $wall; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | $floor = '.' unless defined $floor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%9/9
not $x && $y or $x == $w - 1 or $y == $h - 1
not $x && $y or $x == $w - 1
$x && $y
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | my $w = $#{$terrain->[0]} + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | die 'empty row' unless $w; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | $floor = '.' unless defined $floor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | $wall = ' ' unless defined $wall; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 574 | my ($v,$h)=(0,0); #vert/horiz weighting | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 574 | ▶ | $v++ if $terrain->[$y][$x-1] eq $floor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 574 | ▶ | $v++ if $terrain->[$y][$x+1] eq $floor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 574 | ▶ | $h++ if $terrain->[$y-1][$x] eq $floor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 574 | ▶ | $h++ if $terrain->[$y+1][$x] eq $floor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 574 | ▶ | if ($h>$v){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
$$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 or $$terrain[$y - 1][$x + 1] eq $floor
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||