Area/Wilderness.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 152 CC 13 SCAR 49.4
CC
13
Cov
0%
generate
182.0
BEGIN
2.0
BEGIN
2.0
CRAP 139.6
1 package Games::Roguelike::Area::Wilderness;
2  
3 use base 'Games::Roguelike::Area';
4  
5 use strict;
6 use Carp;
7 use Games::Roguelike::Utils qw(:all);
8  
9 =head1 NAME
10  
11 Games::Roguelike::Area::Wilderness
12  
13 =head1 SYNOPSIS
14  
15 use Games::Roguelike::World;
16  
17 $w = new Games::Roguelike::World(w=>40, h=>20);;
18 $a = new Games::Roguelike::Area(world=>$w, name=>'upland');
19 $a->generate('wilderness',
20 {sym=>'T', color=>'green on black', weight=>2},
21 {sym=>'"', color=>'white on green', weight=>2},
22 {sym=>'~', color=>'white on blue', weight=>1},
23 {sym=>'*', color=>'red on black', city=>1},
24 city=>4
25 );
26 $w->drawmap();
27 $w->getch();
28  
29 =head1 DESCRIPTION
30  
31 Area "generator" module.
32  
33 Each argument to generate(...) is a terrain reference containing:
34  
35 sym => symbol to use for that terrain
36 color => color to use for that terrain
37  
38 And optionally:
39  
40 weight => weight of that terrain element in the generator, default 1, must be an integer
41 ckey => elements with same numbers support each other's presence)
42  
43 =head1 SEE ALSO
44  
45 <L>Games::Roguelike::Area
46  
47 =cut
48  
49 sub generate {
50 my $self = shift;
51 my @terrain = @_;
52
53 croak "terrain must be supplied" unless @terrain;
54
55 my %opts;
56 for (my $i=0; $i < @terrain; ++$i) {
57 if (!ref($terrain[$i])) {
58 my ($a, $b) = splice(@terrain, $i, 2);
59 $opts{$a} = $b;
60 }
61 }
62
63
64 # don't autoexplore over mountains and water
65 $self->{nomove} = '~^';
66  
67 my $edge = $terrain[0];
68  
69 # this algorithm only works on up to 7 distinct terrain ckey's
70  
71 my @exterr;
72  
73 my $map = [];
74  
75 my $dex = 0;
76 my %ck;
77 for (@terrain) {
78 $_->{index} = $dex++;
79 $_->{ckey} = $_->{index};
80 $_->{weight} ||= 1;
81 $_->{weight} = 0 if $_->{city};
82 for (my $i = 0; $i < $_->{weight}; ++$i) {
83 push @exterr, $_;
84 }
85 $ck{$_->{ckey}} = 1;
86 };
87  
88 my $numck = keys(%ck);
89  
90 my $terr_count = @exterr;
91  
92 # random weighted terrain
93 for (my $x = 0; $x < $self->{w}; ++$x) {
94 for (my $y = 0; $y < $self->{h}; ++$y) {
95 my $t = $exterr[int(rand()*$terr_count)];
96 $map->[$x][$y] = {%{$t}};
97 }
98 }
99  
100 # clustering algorithm
101 my $iter = 3;
102 for (1..$iter) {
103 for (my $x = 1; $x < ($self->{w}-1); ++$x) {
104 for (my $y = 1; $y < ($self->{h}-1); ++$y) {
105 my $t = $map->[$x][$y];
106 my $cnt = 0;
107 my %cnt;
108 for (@DD[0..7]) {
109 my ($tx, $ty) = ($x, $y);
110 my ($dx, $dy) = @$_;
111 $tx += $dx;
112 $ty += $dy;
113 $cnt += 1 if $map->[$tx][$ty]->{ckey} == $t->{ckey};
114 ++$cnt{$map->[$tx][$ty]->{index}};
115 }
116  
117 # sorted by most popular in region
118 my @pop = sort {$cnt{$a}<=>$cnt{$b}} (keys(%cnt));
119  
120 # fewer than 8/num-unique-ckeys?
121 if ($cnt < (8/$numck)) {
122 $map->[$x][$y] = $terrain[$pop[$#pop]];
123 }
124 }
125 }
126 }
127  
128 # copy to self->map
129 # some day this should be redundant
130 for (my $x = 0; $x < $self->{w}; ++$x) {
131 for (my $y = 0; $y < $self->{h}; ++$y) {
132 if (!$x || !$y || $x == ($self->{w}-1) || $y == ($self->{h}-1)) {
133 $self->setmap($x, $y, $edge);
134 }
135  
136 $self->setmap($x, $y, $map->[$x][$y]);
137 }
138 }
139  
140 if ($opts{city}) {
141 my ($water, $city, $city_color) = ('~', '*');
142 for (@terrain) {
143 $water = $_->{sym} if $_->{water};
144 $city = $_->{sym} if $_->{city};
145 $city_color = $_->{color} if $_->{city};
146 }
147 # todo: document this
148 for (1..$opts{city}) {
149 my ($x, $y) = $self->findrandmap($water);
150 my $dx = $self->{w}/2 - $x;
151 my $dy = $self->{h}/2 - $y;
152 $dx = $dx/abs($dx) if $dx;
153 $dy = $dy/abs($dy) if $dy;
154  
155 while ($self->{map}->[$x][$y] eq $water) {
156 $x+=$dx;
157 $y+=$dy;
158 }
159  
160 $self->addfeature($city, $x, $y);
161 $self->setmap($x, $y, $city, $city_color);
162 }
163 }
164 }
165  
166 1;