« blib/lib/Games/RolePlay/MapGen/Exporter/XML.pm
↑ Summary
blib/lib/Games/RolePlay/MapGen/Tools.pm »
| 1 | # vi:tw=0 syntax=perl: | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | package Games::RolePlay::MapGen::Generator::Perfect; | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use common::sense; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use parent q(Games::RolePlay::MapGen::Generator); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Games::RolePlay::MapGen::Tools qw( _group _tile choice roll ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 9 | 1; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | # create_tiles {{{ | ||||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | sub create_tiles { | ||||||||||||||||||||||||||||||
|
Subroutine
create_tiles: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 14 | 9 | my $this = shift; | |||||||||||||||||||||||||||||||
| 15 | 9 | my $opts = shift; | |||||||||||||||||||||||||||||||
| 16 | 9 | my @map = (); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 9 | for my $i (0 .. $opts->{y_size}-1) { | |||||||||||||||||||||||||||||||
| 19 | 195 | my $a = []; | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 195 | for my $j (0 .. $opts->{x_size}-1) { | |||||||||||||||||||||||||||||||
| 22 | 4425 | ▶ | $opts->{t_cb}->() if exists $opts->{t_cb}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 4425 | push @$a, &_tile(x=>$j, y=>$i); | |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 195 | push @map, $a; | |||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 9 | return @map; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | # }}} | ||||||||||||||||||||||||||||||||
| 33 | # generate_perfect_maze {{{ | ||||||||||||||||||||||||||||||||
| 34 | 9 | ▶ | sub generate_perfect_maze { | ||||||||||||||||||||||||||||||
|
Subroutine
generate_perfect_maze: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 35 | 9 | my $this = shift; | |||||||||||||||||||||||||||||||
| 36 | 9 | my $opts = shift; | |||||||||||||||||||||||||||||||
| 37 | 9 | my $map = new Games::RolePlay::MapGen::_interconnected_map(shift); | |||||||||||||||||||||||||||||||
| 38 | # This object interconnects the map; but, also ensures that the self-refs are broken when it goes out of scope! | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 9 | my @dirs = (qw(n s e w)); | |||||||||||||||||||||||||||||||
| 41 | 9 | my $cur = &choice(map(@$_, @$map)); | |||||||||||||||||||||||||||||||
| 42 | 9 | my @togo = @dirs; | |||||||||||||||||||||||||||||||
| 43 | 9 | my $dir = &choice(@togo); | |||||||||||||||||||||||||||||||
| 44 | 9 | my @visited = ( $cur ); | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 9 | $cur->{type} = "corridor"; | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | # open DEBUG, ">debug.log" or die $!; | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 9 | for(;;) { | |||||||||||||||||||||||||||||||
| 51 | 17704 | my $nex = $cur->{nb}{$dir}; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $show = sub { my $n = shift; sprintf '(%2d, %2d)', $n->{x}, $n->{y} }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 17704 | ▶ | $opts->{t_cb}->() if exists $opts->{t_cb}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # printf DEBUG '@visited=%3d; $cur=%s; $nex=%s;%s', int @visited, $show->($cur), $show->($nex); | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 17704 | ▶ | if( $nex and not $nex->{visited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$nex and not $nex->{"visited"}
MC/DC100%2/2
$nex and not $nex->{"visited"}
$nex
not $nex->{"visited"}
Truth table100%3/3
A and not(B)
A= $nexB= not $nex->{"visited"}
| |||||||||||||||||||||||||||||||||
| 60 | # print DEBUG " NEXT"; | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 4424 | $cur->{od}{$dir} = 1; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 4424 | $cur = $nex; | |||||||||||||||||||||||||||||||
| 65 | 4424 | $cur->{visited} = 1; | |||||||||||||||||||||||||||||||
| 66 | 4424 | push @visited, $cur; | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 4424 | $cur->{od}{$Games::RolePlay::MapGen::opp{$dir}} = 1; | |||||||||||||||||||||||||||||||
| 69 | 4424 | $cur->{type} = 'corridor'; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 4424 | ▶ | @togo = grep { !$cur->{od}{$_} and !$cur->{_pud}{$_} } @dirs; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
MC/DC100%2/2
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
$cur->{'od'}{$_}
$cur->{'_pud'}{$_}
Truth table100%3/3
A || B
A= $cur->{'od'}{$_}B= $cur->{'_pud'}{$_}
| |||||||||||||||||||||||||||||||||
| 72 | 4424 | ▶ | $dir = &choice(@togo) if &roll(1, 100) > $opts->{same_way_percent}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | # $opts->{same_way_percent} of the time, we won't change the direction | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | } elsif( @togo ) { | ||||||||||||||||||||||||||||||||
| 76 | # print DEBUG " TOGO"; | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 8855 | $cur->{_pud}{$dir} = 1; # perfect's used dir | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | # $opts->{same_node_percent} of the time, we try to use the same node | ||||||||||||||||||||||||||||||||
| 81 | 8855 | ▶ | if( @visited>1 and (&roll(1, 100) > $opts->{same_node_percent}) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@visited > 1 and &roll(1, 100) > $opts->{"same_node_percent"}
MC/DC100%2/2
@visited > 1 and &roll(1, 100) > $opts->{"same_node_percent"}
@visited > 1
&roll(1, 100) > $opts->{"same_node_percent"}
Truth table100%3/3
A and B
A= @visited > 1B= &roll(1, 100) > $opts->{"same_node_percent"}
| |||||||||||||||||||||||||||||||||
| 82 | # print DEBUG " SAME"; | ||||||||||||||||||||||||||||||||
| 83 | # Pick a new node with a random direction that makes sense. | ||||||||||||||||||||||||||||||||
| 84 | 6160 | $cur = &choice(@visited); | |||||||||||||||||||||||||||||||
| 85 | 6160 | ▶ | @togo = grep { !$cur->{od}{$_} and !$cur->{_pud}{$_} } @dirs; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
MC/DC100%2/2
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
$cur->{'od'}{$_}
$cur->{'_pud'}{$_}
Truth table100%3/3
A || B
A= $cur->{'od'}{$_}B= $cur->{'_pud'}{$_}
| |||||||||||||||||||||||||||||||||
| 86 | 6160 | $dir = &choice(@togo); # whenever we switch nodes, we pick a random direction though | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||
| 89 | # print DEBUG " DIFF"; | ||||||||||||||||||||||||||||||||
| 90 | # Try a different direction at this same node. | ||||||||||||||||||||||||||||||||
| 91 | 2695 | ▶ | @togo = grep { !$cur->{od}{$_} and !$cur->{_pud}{$_} } @dirs; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
MC/DC100%2/2
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
$cur->{'od'}{$_}
$cur->{'_pud'}{$_}
Truth table100%3/3
A || B
A= $cur->{'od'}{$_}B= $cur->{'_pud'}{$_}
| |||||||||||||||||||||||||||||||||
| 92 | 2695 | $dir = &choice(@togo); | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||||||
| 96 | # print DEBUG " DULL"; | ||||||||||||||||||||||||||||||||
| 97 | # This node is so boring, we don't want to accidentally try it again | ||||||||||||||||||||||||||||||||
| 98 | 4425 | @visited = grep {$_ != $cur} @visited; | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 4425 | ▶ | last unless @visited; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | # Pick a new node with a random direction that makes sense. | ||||||||||||||||||||||||||||||||
| 103 | 4416 | $cur = &choice(@visited); | |||||||||||||||||||||||||||||||
| 104 | 4416 | ▶ | @togo = grep { !$cur->{od}{$_} and !$cur->{_pud}{$_} } @dirs; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
MC/DC100%2/2
$cur->{'od'}{$_} || $cur->{'_pud'}{$_}
$cur->{'od'}{$_}
$cur->{'_pud'}{$_}
Truth table100%3/3
A || B
A= $cur->{'od'}{$_}B= $cur->{'_pud'}{$_}
| |||||||||||||||||||||||||||||||||
| 105 | 4416 | $dir = &choice(@togo); | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | # print DEBUG "\n"; | ||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 9 | delete $_->{_pud} for (map(@$_, @$map)) | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | # }}} | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # genmap {{{ | ||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | sub genmap { | ||||||||||||||||||||||||||||||
|
Subroutine
genmap: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 118 | 9 | my $this = shift; | |||||||||||||||||||||||||||||||
| 119 | 9 | my $opts = shift; | |||||||||||||||||||||||||||||||
| 120 | 9 | my @map = $this->create_tiles( $opts ); | |||||||||||||||||||||||||||||||
| 121 | 9 | my @groups = (); | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 9 | $this->generate_perfect_maze($opts, \@map); | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 9 | return (\@map, \@groups); | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | # }}} | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||||||