Games-Roguelike-Caves

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

     |........-|   |...........................-| 
     |........-|   |.......................----|  
     |-........-|  |-.....................-|      
      |---......|   |-...................-|       
         |-.....|    |...................|        
          |....-|    |..................-|        
          |-..-|     |-.............----|         
           |--|       |-------------|             
Ecample 3:
use Games::Roguelike::Caves;
#2 iterations, 50% walls...
my $map = generate_cave(50,20,2,.5,"W",' ');
outline_walls ($map,"W",' ');
for (@$map){
    for (@$_){
        print;
    }    
    print "\n"
}
OUTPUT:
W|---|WW|-----|WW|-|WWWWWW|---|W|----|WWWWWW|--|WW

lib/Games/Roguelike/Caves.pm  view on Meta::CPAN

	outline_walls
);

our $VERSION = '0.01';

#use cellular automata to carve out a decent cave
#initially contain 45% walls at random
#a tile becomes or remains a wall if the 3x3 region centered on it contains at least 5 walls.
#use 1 to represent wall, 0 is space
sub generate_cave{
    my ($w, $h, $iterations, $percentWalls, $wall, $floor) = @_;
    die 'dimensions?' unless ($w and $h);
    $iterations ||= 12;
    $percentWalls ||= .45;
    $percentWalls /= 100 if $percentWalls>1; # in case it's .45 or something
    $wall = ' ' unless defined $wall;
    $floor = '.' unless defined $floor;
    
    my @terrain = ();
    my @nextStep = ();
    for my $x (0..$w-1){
        for my $y (0..$h-1){
            $terrain[$y][$x] = rand()<$percentWalls ? 1 : 0;
        }
    }
    for (1..$iterations){
        for my $x (0..$w-1){
            for my $y (0..$h-1){
                if ( !$x or !$y or $x==$w-1 or $y==$h-1){
                    #we're at edge: be wall.
                    $nextStep[$y][$x] = 1;
                    next;
                }
                my $c=0;
                #count walls in 3x3 square
                $c += $terrain[$y-1][$x-1];



( run in 2.180 seconds using v1.01-cache-2.11-cpan-71847e10f99 )