blib/lib/Game/TextMapper/Schroeder/Alpine.pm

filter: Statementstmt 90.3% 552 / 611 Branchbran 77.7% 227 / 292 Conditioncond 63.5% 101 / 159 MC/DCmcdc 48.1% 39 / 81 Subroutinesub 98.1% 52 / 53 Podpod 0.0% 0 / 29 totaltotal 79.2% 971 / 1225 CC 260 SCAR 31.9
CC
260
Cov
83%
desertification
197.0
wet
30.0
fix_altitude
28.7
CRAP 24.3
« blib/lib/Game/TextMapper/Traveller.pm ↑ Summary blib/lib/Game/TextMapper/Smale.pm »
1 # Copyright (C) 2009-2023 Alex Schroeder <alex@gnu.org>
2 #
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU Affero General Public License as published by the Free
5 # Software Foundation, either version 3 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Affero General Public License along
14 # with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 Game::TextMapper::Alpine - generate an alpine landscape
21  
22 =head1 DESCRIPTION
23  
24 This fills the map with some mountains and then traces the water flow down to
25 the sea and off the map. With water, forests grow; but if the area remains at
26 the same altitude, swamps form.
27  
28 Settlements are placed at random in the habitable zones, but far enough from
29 each other, and connected by trails.
30  
31 In order to support hex and square maps, this class uses roles to implement
32 coordinates, neighbours, and all that. This is why you need to specify the role
33 before creating an instance of this class:
34  
35 return Game::TextMapper::Schroeder::Alpine
36 ->with_roles('Game::TextMapper::Schroeder::Hex')->new()
37 ->generate_map(@params);
38  
39 =head1 SEE ALSO
40  
41 L<Game::TextMapper::Schroeder::Base>
42 L<Game::TextMapper::Schroeder::Hex>
43 L<Game::TextMapper::Schroeder::Square>
44  
45 =cut
46  
47 package Game::TextMapper::Schroeder::Alpine;
48 11 ▶use Game::TextMapper::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 11 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 11 with 'Game::TextMapper::Schroeder::Base';
53 11 ▶use List::Util 'shuffle';
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 11 my $log = Game::TextMapper::Log->get;
56  
57 11 has 'steepness';
58 11 has 'peaks';
59 11 has 'peak';
60 11 has 'peak_min';
61 11 has 'bumps';
62 11 has 'bump';
63 11 has 'bottom';
64 11 has 'arid';
65 11 has 'climate';
66 11 has 'wind';
67  
68 4 ▶sub place_peak {
Subroutine place_peak: called CC=7 SCAR=19.5
Pod undocumented
69 4 my $self = shift;
70 4 my $altitude = shift;
71 4 my $count = shift;
72 4 my $min = shift;
73 4 my $max = shift;
74 # max altitude exactly once
75 4 my $x = int(rand($self->width)) + 1;
76 4 my $y = int(rand($self->height)) + 1;
77 4 my $coordinates = coordinates($x, $y);
78 4 $altitude->{$coordinates} = $max;
79 4 $log->debug("placed max $max at $coordinates");
80 # prepare distribution
81 4 my @distribution;
82 4 my $n = 1;
83 4 for my $i (0 .. $max - $min) {
84 4 push(@distribution, $n);
85 4 $n *= 2;
86 }
87 # this is the "die size"
88 4 $n = $distribution[$#distribution];
89 4 $log->debug("@distribution");
90 4 my @queue;
91 # place some peaks and put them in a queue
92 4 for (1 .. $count) {
93 # try to find an empty hex
94 28 for (1 .. 6) {
95 28 my $x = int(rand($self->width)) + 1;
96 28 my $y = int(rand($self->height)) + 1;
97 28 my $coordinates = coordinates($x, $y);
98 28 ▶ next if $altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} 0 28 28
99 28 my $r = rand($n);
100 28 $log->debug(" rolled $r");
101 28 for my $i (0 .. $#distribution) {
102 28 $log->debug(" $r < $distribution[$i]");
103 28 ▶ if ($r < $distribution[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $distribution[$i]) 28 0 28
104 28 $altitude->{$coordinates} = $max - $i;
105 28 $log->debug("placed $altitude->{$coordinates} at $coordinates");
106 28 push(@queue, $coordinates);
107 28 last;
108 }
109 }
110 28 last;
111 }
112 }
113 4 return @queue;
114 }
115  
116 4 ▶sub grow_mountains {
Subroutine grow_mountains: called CC=13 SCAR=25.7
Pod undocumented
117 4 my $self = shift;
118 4 my $altitude = shift;
119 4 my @queue = @_;
120 # go through the queue and add adjacent lower altitude hexes, if possible; the
121 # hexes added are to the end of the queue
122 4 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 1196 4 1200
123 1196 my $coordinates = shift @queue;
124 1196 my $current_altitude = $altitude->{$coordinates};
125 1196 ▶ next unless $current_altitude > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_altitude > 0 0 1196 1196
126 # pick some random neighbors based on variable steepness
127 1196 my $n = $self->steepness;
128 # round up based on fraction
129 1196 ▶ $n += 1 if rand() < $n - int($n);
Branch: 1 branch
BranchTrueFalseTotal
if rand() < $n - int($n) 0 1196 1196
130 1196 $n = int($n);
131 1196 ▶ next if $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 0 1196 1196
132 1196 for (1 .. $n) {
133 # try to find an empty neighbor; make more attempts if we're looking for
134 # more neighbours
135 3588 for my $attempt (1 .. 3 + $n) {
136 17166 my ($x, $y) = $self->neighbor($coordinates, $self->random_neighbor());
137 17166 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 1561 15605 17166
138 15605 my $other = coordinates($x, $y);
139 # if this is taken, look further
140 15605 ▶ if ($altitude->{$other}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$other}) 15102 503 15605
141 15102 ($x, $y) = $self->neighbor2($coordinates, $self->random_neighbor2());
142 15102 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 1943 13159 15102
143 13159 $other = coordinates($x, $y);
144 # if this is also taken, try again – but if we've already had four
145 # attempts, jump!
146 13159 ▶ if ($altitude->{$other}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$other}) 12494 665 13159
147 12494 ▶ $coordinates = $other if $attempt > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt > 4 3851 8643 12494
148 12494 next;
149 }
150 }
151 # if we found an empty neighbor, set its altitude
152 1168 ▶ $altitude->{$other} = $current_altitude > 0 ? $current_altitude - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$current_altitude > 0 ? : 1168 0 1168
153 1168 push(@queue, $other);
154 1168 last;
155 }
156 }
157 }
158 }
159  
160 4 ▶sub fix_altitude {
Subroutine fix_altitude: called CC=7 SCAR=33.6
Pod undocumented
161 4 my $self = shift;
162 4 my $altitude = shift;
163 # go through all the hexes
164 4 for my $coordinates (sort keys %$altitude) {
165 # find hexes that we missed and give them the height of a random neighbor
166 1200 ▶ if (not defined $altitude->{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $altitude->{$coordinates}) 0 1200 1200
167 # warn "identified a hex that was skipped: $coordinates\n";
168 # try to find a suitable neighbor
169 0 for (1 .. 6) {
170 0 my ($x, $y) = $self->neighbor($coordinates, $self->random_neighbor());
171 0 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 0 0 0
172 0 my $other = coordinates($x, $y);
173 0 ▶ next unless defined $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $altitude->{$other} 0 0 0
174 0 $altitude->{$coordinates} = $altitude->{$other};
175 0 last;
176 }
177 # if we didn't find one in the last six attempts, just make it hole in the ground
178 0 ▶ if (not defined $altitude->{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $altitude->{$coordinates}) 0 0 0
179 0 $altitude->{$coordinates} = 0;
180 }
181 }
182 }
183 }
184  
185 4 ▶sub altitude {
Subroutine altitude: called CC=2 SCAR=6.9
Pod undocumented
186 4 my $self = shift;
187 4 my ($world, $altitude) = @_;
188 4 my @queue = $self->place_peak($altitude, $self->peaks, $self->peak_min, $self->peak);
189 4 $self->grow_mountains($altitude, @queue);
190 4 $self->fix_altitude($altitude);
191 # note height for debugging purposes
192 4 for my $coordinates (sort keys %$altitude) {
193 1200 $world->{$coordinates} = "height$altitude->{$coordinates}";
194 }
195 }
196  
197 4 ▶sub bumpiness {
Subroutine bumpiness: called CC=12 SCAR=25.2
Pod undocumented
198 4 my ($self, $world, $altitude) = @_;
199 4 for (1 .. $self->bumps) {
200 28 for my $delta (-$self->bump, $self->bump) {
201 # six attempts to try and find a good hex
202 56 for (1 .. 6) {
203 61 my $x = int(rand($self->width)) + 1;
204 61 my $y = int(rand($self->height)) + 1;
205 61 my $coordinates = coordinates($x, $y);
206 61 my $current_altitude = $altitude->{$coordinates} + $delta;
207 61 ▶ next if $current_altitude > 10 or $current_altitude < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $current_altitude > 10 or $current_altitude < 0 5 56 61
Condition66%2/3
$current_altitude > 10 or $current_altitude < 0
l!l&&r!l&&!r
5056
MC/DC50%1/2
$current_altitude > 10 or $current_altitude < 0
$current_altitude > 10 $current_altitude < 0
Truth table66%2/3
A or B
A= $current_altitude > 10B= $current_altitude < 0
ABresult
1X1
011
000
208 # bump it up or down
209 56 $altitude->{$coordinates} = $current_altitude;
210 56 $world->{$coordinates} = "height$altitude->{$coordinates} zone";
211 56 $log->debug("bumped altitude of $coordinates by $delta to $current_altitude");
212 # if the bump was +2 or -2, bump the neighbours by +1 or -1
213 56 ▶ if ($delta < -1 or $delta > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < -1 or $delta > 1) 56 0 56
Condition66%2/3
$delta < -1 or $delta > 1
l!l&&r!l&&!r
28280
MC/DC0%0/2
$delta < -1 or $delta > 1
$delta < -1 $delta > 1
Truth table66%2/3
A or B
A= $delta < -1B= $delta > 1
ABresult
1X1
011
000
214 56 my $delta = $delta - $delta / abs($delta);
215 56 for my $i ($self->neighbors()) {
216 280 my ($x, $y) = $self->neighbor($coordinates, $i);
217 280 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 19 261 280
218 261 my $other = coordinates($x, $y);
219 261 $current_altitude = $altitude->{$other} + $delta;
220 261 ▶ next if $current_altitude > 10 or $current_altitude < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $current_altitude > 10 or $current_altitude < 0 0 261 261
Condition33%1/3
$current_altitude > 10 or $current_altitude < 0
l!l&&r!l&&!r
00261
MC/DC0%0/2
$current_altitude > 10 or $current_altitude < 0
$current_altitude > 10 $current_altitude < 0
Truth table33%1/3
A or B
A= $current_altitude > 10B= $current_altitude < 0
ABresult
1X1
011
000
221 261 $altitude->{$other} = $current_altitude;
222 261 $world->{$other} = "height$altitude->{$other} zone";
223 261 $log->debug("$i bumped altitude of $other by $delta to $current_altitude");
224 }
225 }
226 # if we have found a good hex, don't go through all the other attempts
227 56 last;
228 }
229 }
230 }
231 }
232  
233 4 ▶sub water {
Subroutine water: called CC=23 SCAR=31.4
Pod undocumented
234 4 my $self = shift;
235 4 my ($world, $altitude, $water) = @_;
236 # reset in case we run this twice
237 # go through all the hexes
238 4 for my $coordinates (sort keys %$altitude) {
239 1200 ▶ next if $altitude->{$coordinates} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} <= $self->bottom 0 1200 1200
240 # note preferred water flow by identifying lower lying neighbors
241 1200 my ($lowest, $direction);
242 # look at neighbors in random order
243 NEIGHBOR:
244 1200 for my $i (shuffle $self->neighbors()) {
245 6000 my ($x, $y) = $self->neighbor($coordinates, $i);
246 6000 my $legal = $self->legal($x, $y);
247 6000 my $other = coordinates($x, $y);
248 6000 ▶ next if $legal and $altitude->{$other} > $altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $legal and $altitude->{$other} > $altitude->{$coordinates} 1649 4351 6000
Condition100%3/3
$legal and $altitude->{$other} > $altitude->{$coordinates}
!ll&&!rl&&r
47638751649
MC/DC100%2/2
$legal and $altitude->{$other} > $altitude->{$coordinates}
$legal $altitude->{$other} > $altitude->{$coordinates}
Truth table100%3/3
A and B
A= $legalB= $altitude->{$other} > $altitude->{$coordinates}
ABresult
0X0
100
111
249 # don't point head on to another arrow
250 4351 ▶ next if $legal and $water->{$other} and $water->{$other} == ($i-3) % 6;
Branch: 1 branch
BranchTrueFalseTotal
if $legal and $water->{$other} and $water->{$other} == ($i - 3) % 6 225 4126 4351
Condition100%6/6
$legal and $water->{$other} and $water->{$other} == ($i - 3) % 6
!ll&&!rl&&r
28251301225
$legal and $water->{$other}
!ll&&!rl&&r
47623491526
MC/DC100%3/3
$legal and $water->{$other} and $water->{$other} == ($i - 3) % 6
$legal $water->{$other} $water->{$other} == ($i - 3) % 6
Truth table100%4/4
A and B and C
A= $legalB= $water->{$other}C= $water->{$other} == ($i - 3) % 6
ABCresult
0XX0
10X0
1100
1111
251 # don't point into loops
252 4126 my %loop = ($coordinates => 1, $other => 1);
253 4126 my $next = $other;
254 4126 $log->debug("Loop detection starting with $coordinates and $other");
255 4126 ▶ while ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 7247 0 7247
256 # no water flow known is also good;
257 7247 ▶ $log->debug("water for $next: " . ($water->{$next} || "none"));
Condition100%2/2
$water->{$next} || 'none'
l!l
29734274
MC/DC100%1/1
$water->{$next} || 'none'
$water->{$next}
Truth table100%2/2
A
A= $water->{$next}
Aresult
11
00
258 7247 ▶ last unless defined $water->{$next};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $water->{$next} 3440 3807 7247
259 3807 ($x, $y) = $self->neighbor($next, $water->{$next});
260 # leaving the map is good
261 3807 $log->debug("legal for $next: " . $self->legal($x, $y));
262 3807 ▶ last unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 517 3290 3807
263 3290 $next = coordinates($x, $y);
264 # skip this neighbor if this is a loop
265 3290 ▶ $log->debug("is $next in a loop? " . ($loop{$next} || "no"));
Condition100%2/2
$loop{$next} || 'no'
l!l
1693121
MC/DC100%1/1
$loop{$next} || 'no'
$loop{$next}
Truth table100%2/2
A
A= $loop{$next}
Aresult
11
00
266 3290 ▶ next NEIGHBOR if $loop{$next};
Branch: 1 branch
BranchTrueFalseTotal
if $loop{$next} 169 3121 3290
267 3121 $loop{$next} = 1;
268 }
269 3957 ▶ if (not defined $direction
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $direction or not $legal and $altitude->{$coordinates} < $lowest or $legal and $altitude->{$other} < $lowest) 1584 2373 3957
Condition83%10/12
not defined $direction or not $legal and $altitude->{$coordinates} < $lowest or $legal and $altitude->{$other} < $lowest
l!l&&r!l&&!r
11604242373
not defined $direction or not $legal and $altitude->{$coordinates} < $lowest
l!l&&r!l&&!r
116002797
not $legal and $altitude->{$coordinates} < $lowest
!ll&&!rl&&r
24593380
$legal and $altitude->{$other} < $lowest
!ll&&!rl&&r
3382035424
MC/DC40%2/5
not defined $direction or not $legal and $altitude->{$coordinates} < $lowest or $legal and $altitude->{$other} < $lowest
not defined $direction not $legal $altitude->{$coordinates} < $lowest $legal $altitude->{$other} < $lowest
Truth table50%4/8
not(A) or not(B) and C or D and E
A= not defined $directionB= not $legalC= $altitude->{$coordinates} < $lowestD= $legalE= $altitude->{$other} < $lowest
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
270 or not $legal and $altitude->{$coordinates} < $lowest
271 or $legal and $altitude->{$other} < $lowest) {
272 1584 ▶ $lowest = $legal ? $altitude->{$other} : $altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
$legal ? : 1446 138 1584
273 1584 $direction = $i;
274 1584 $log->debug("Set lowest to $lowest ($direction)");
275 }
276 }
277 1200 ▶ if (defined $direction) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $direction) 1160 40 1200
278 1160 $water->{$coordinates} = $direction;
279 $world->{$coordinates} =~ s/arrow\d/arrow$water->{$coordinates}/
280 1160 ▶ or $world->{$coordinates} .= " arrow$water->{$coordinates}";
Branch: 1 branch
BranchTrueFalseTotal
$world->{$coordinates} =~ s/arrow\d/arrow$water->{$coordinates}/u or $world->{$coordinates} .= " arrow$water->{$coordinates}" 0 1160 1160
281 }
282 }
283 }
284  
285 4 ▶sub mountains {
Subroutine mountains: called CC=5 SCAR=16.1
Pod undocumented
286 4 my $self = shift;
287 4 my ($world, $altitude) = @_;
288 # place the types
289 4 for my $coordinates (keys %$altitude) {
290 1200 ▶ if ($altitude->{$coordinates} >= 10) {
Branch: 3 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 10) { } 33 1167 1200
elsif ($altitude->{$coordinates} >= 9) { } 107 1060 1167
elsif ($altitude->{$coordinates} >= 8) { } 274 786 1060
291 33 $world->{$coordinates} = "white mountains";
292 } elsif ($altitude->{$coordinates} >= 9) {
293 107 $world->{$coordinates} = "white mountain";
294 } elsif ($altitude->{$coordinates} >= 8) {
295 274 $world->{$coordinates} = "light-grey mountain";
296 }
297 }
298 }
299  
300 4 ▶sub ocean {
Subroutine ocean: called CC=7 SCAR=33.2
Pod undocumented
301 4 my $self = shift;
302 4 my ($world, $altitude) = @_;
303 4 for my $coordinates (sort keys %$altitude) {
304 1200 ▶ if ($altitude->{$coordinates} <= $self->bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} <= $self->bottom) 0 1200 1200
305 0 my $ocean = 1;
306 0 for my $i ($self->neighbors()) {
307 0 my ($x, $y) = $self->neighbor($coordinates, $i);
308 0 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 0 0 0
309 0 my $other = coordinates($x, $y);
310 0 ▶ next if $altitude->{$other} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} <= $self->bottom 0 0 0
311 0 $ocean = 0;
312 }
313 0 ▶ $world->{$coordinates} = $ocean ? "ocean" : "water";
Branch: 1 branch
BranchTrueFalseTotal
$ocean ? : 0 0 0
314 }
315 }
316 }
317  
318 4 ▶sub lakes {
Subroutine lakes: called CC=4 SCAR=13.9
Pod undocumented
319 4 my $self = shift;
320 4 my ($world, $altitude, $water) = @_;
321 # any areas without water flow are lakes
322 4 for my $coordinates (sort keys %$altitude) {
323 1200 ▶ if (not defined $water->{$coordinates}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $water->{$coordinates} and $world->{$coordinates} ne "ocean") 40 1160 1200
Condition66%2/3
not defined $water->{$coordinates} and $world->{$coordinates} ne "ocean"
!ll&&!rl&&r
1160040
MC/DC50%1/2
not defined $water->{$coordinates} and $world->{$coordinates} ne "ocean"
not defined $water->{$coordinates} $world->{$coordinates} ne "ocean"
Truth table66%2/3
not(A) and B
A= not defined $water->{$coordinates}B= $world->{$coordinates} ne "ocean"
ABresult
0X0
100
111
324 and $world->{$coordinates} ne "ocean") {
325 40 $world->{$coordinates} = "water";
326 }
327 }
328 }
329  
330 4 ▶sub swamps {
Subroutine swamps: called CC=8 SCAR=20.8
Pod undocumented
331 # any area with water flowing to a neighbor at the same altitude is a swamp
332 4 my ($self, $world, $altitude, $water, $flow, $dry) = @_;
333 4 for my $coordinates (keys %$altitude) {
334 # don't turn lakes into swamps and skip bogs
335 1200 ▶ next if $world->{$coordinates} =~ /ocean|water|swamp|grass/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /ocean|water|swamp|grass/u 204 996 1200
336 # swamps require a river
337 996 ▶ next unless $flow->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
unless $flow->{$coordinates} 540 456 996
338 # no swamps when there is a canyon
339 456 ▶ next if $dry->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$coordinates} 43 413 456
340 # look at the neighbor the water would flow to
341 413 my ($x, $y) = $self->neighbor($coordinates, $water->{$coordinates});
342 # skip if water flows off the map
343 413 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 57 356 413
344 356 my $other = coordinates($x, $y);
345 # skip if water flows downhill
346 356 ▶ next if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 265 91 356
347 # if there was no lower neighbor, this is a swamp
348 91 ▶ if ($altitude->{$coordinates} >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 6) { } 67 24 91
349 67 $world->{$coordinates} =~ s/height\d+/grey swamp/;
350 } else {
351 24 $world->{$coordinates} =~ s/height\d+/dark-grey swamp/;
352 }
353 }
354 }
355  
356 4 ▶sub flood {
Subroutine flood: called CC=20 SCAR=30.0
Pod undocumented
357 4 my $self = shift;
358 4 my ($world, $altitude, $water) = @_;
359 # backtracking information: $from = $flow{$to}
360 4 my %flow;
361 # allow easy skipping
362 my %seen;
363 # start with a list of hexes to look at; as always, keys is a source of
364 # randomness that's independent of srand which is why we shuffle sort
365 4 my @lakes = shuffle sort grep { not defined $water->{$_} } keys %$world;
366 4 ▶ return unless @lakes;
Branch: 1 branch
BranchTrueFalseTotal
unless @lakes 0 4 4
367 4 my $start = shift(@lakes);
368 4 my @candidates = ($start);
369 4 ▶ while (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates) 417 0 417
370 # Prefer candidates outside the map with altitude 0; reshuffle because
371 # candidates at the same height are all equal and early or late discoveries
372 # should not matter (not shuffling means it matters whether candidates are
373 # pushed or unshifted because this is a stable sort)
374 @candidates = sort {
375 417 ▶ ($altitude->{$a}||0) <=> ($altitude->{$b}||0)
Condition100%4/4
$altitude->{$a} || 0
l!l
26956111
$altitude->{$b} || 0
l!l
2698285
MC/DC100%1/1
$altitude->{$a} || 0
$altitude->{$a}
MC/DC100%1/1
$altitude->{$b} || 0
$altitude->{$b}
Truth table100%2/2
A
A= $altitude->{$a}
Aresult
11
00
Truth table100%2/2
A
A= $altitude->{$b}
Aresult
11
00
376 } shuffle @candidates;
377 417 $log->debug("Candidates @candidates");
378 417 my $coordinates;
379 do {
380 452 $coordinates = shift(@candidates);
381 417 ▶ } until not $coordinates or not $seen{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
not $coordinates && $seen{$coordinates} or do { do { $coordinates = shift @candidates } } 417 35 452
Condition66%2/3
$coordinates && $seen{$coordinates}
!ll&&!rl&&r
041735
MC/DC50%1/2
$coordinates && $seen{$coordinates}
$coordinates $seen{$coordinates}
Truth table66%2/3
A && B
A= $coordinatesB= $seen{$coordinates}
ABresult
0X0
100
111
382 417 ▶ last unless $coordinates;
Branch: 1 branch
BranchTrueFalseTotal
unless $coordinates 0 417 417
383 417 $seen{$coordinates} = 1;
384 417 $log->debug("Looking at $coordinates");
385 417 ▶ if ($self->legal($coordinates) and $world->{$coordinates} ne "ocean") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->legal($coordinates) and $world->{$coordinates} ne "ocean") 381 36 417
Condition66%2/3
$self->legal($coordinates) and $world->{$coordinates} ne "ocean"
!ll&&!rl&&r
360381
MC/DC50%1/2
$self->legal($coordinates) and $world->{$coordinates} ne "ocean"
$self->legal($coordinates) $world->{$coordinates} ne "ocean"
Truth table66%2/3
A and B
A= $self->legal($coordinates)B= $world->{$coordinates} ne "ocean"
ABresult
0X0
100
111
386 # if we're still on the map, check all the unknown neighbors
387 381 my $from = $coordinates;
388 381 for my $i ($self->neighbors()) {
389 1808 my $to = coordinates($self->neighbor($from, $i));
390 1808 ▶ next if $seen{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$to} 449 1359 1808
391 1359 $log->debug("Adding $to to our candidates");
392 1359 $flow{$to} = $from;
393 # adding to the front as we keep pushing forward (I hope)
394 1359 push(@candidates, $to);
395 }
396 381 next;
397 }
398 36 $log->debug("We left the map at $coordinates");
399 36 my $to = $coordinates;
400 36 my $from = $flow{$to};
401 36 ▶ while ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 266 36 302
402 266 my $i = $self->direction($from, $to);
403 266 ▶ if (not defined $water->{$from}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $water->{$from} or $water->{$from} != $i) { } 152 114 266
Condition100%3/3
not defined $water->{$from} or $water->{$from} != $i
l!l&&r!l&&!r
40112114
MC/DC100%2/2
not defined $water->{$from} or $water->{$from} != $i
not defined $water->{$from} $water->{$from} != $i
Truth table100%3/3
not(A) or B
A= not defined $water->{$from}B= $water->{$from} != $i
ABresult
1X1
011
000
404 or $water->{$from} != $i) {
405 152 $log->debug("Arrow for $from now points to $to");
406 152 $water->{$from} = $i;
407 $world->{$from} =~ s/arrow\d/arrow$i/
408 152 ▶ or $world->{$from} .= " arrow$i";
Branch: 1 branch
BranchTrueFalseTotal
$world->{$from} =~ s/arrow\d/arrow$i/u or $world->{$from} .= " arrow$i" 112 40 152
409 } else {
410 114 $log->debug("Arrow for $from already points $to");
411 }
412 266 $to = $from;
413 266 $from = $flow{$to};
414 }
415 # pick the next lake
416 do {
417 40 $start = shift(@lakes);
418 40 ▶ $log->debug("Next lake is $start") if $start;
Branch: 1 branch
BranchTrueFalseTotal
if $start 36 4 40
419 36 ▶ } until not $start or not defined $water->{$start};
Branch: 1 branch
BranchTrueFalseTotal
not $start && defined $water->{$start} or do { do { $start = shift @lakes; $log->debug("Next lake is $start") if $start } } 36 4 40
Condition100%3/3
$start && defined $water->{$start}
!ll&&!rl&&r
4324
MC/DC100%2/2
$start && defined $water->{$start}
$start defined $water->{$start}
Truth table100%3/3
A && B
A= $startB= defined $water->{$start}
ABresult
0X0
100
111
420 36 ▶ last unless $start;
Branch: 1 branch
BranchTrueFalseTotal
unless $start 4 32 36
421 32 %seen = %flow = ();
422 32 @candidates = ($start);
423 }
424 }
425  
426 4 ▶sub rivers {
Subroutine rivers: called CC=7 SCAR=19.5
Pod undocumented
427 4 my ($self, $world, $altitude, $water, $flow, $rivers) = @_;
428 # $flow are the sources points of rivers, or 1 if a river flows through them
429 my @growing = map {
430 258 ▶ $world->{$_} = "light-grey forest-hill" unless $world->{$_} =~ /mountain|swamp|grass|water|ocean/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$_} =~ /mountain|swamp|grass|water|ocean/u 86 172 258
431 258 $flow->{$_} = [$_]
432 } sort grep {
433 # these are the potential starting places: up in the mountains below the
434 # ice, or lakes
435 4 ($altitude->{$_} == 7 or $altitude->{$_} == 8
436 or $world->{$_} =~ /water/ and $altitude->{$_} > $self->bottom)
437 and not $flow->{$_}
438 1200 ▶ and $world->{$_} !~ /dry/;
Condition86%13/15
($altitude->{$_} == 7 || $altitude->{$_} == 8 || $world->{$_} =~ /water/u && $altitude->{$_} > $self->bottom) && !$flow->{$_} && !($world->{$_} =~ /dry/u)
!ll&&!rl&&r
518424258
$altitude->{$_} == 7 || $altitude->{$_} == 8 || $world->{$_} =~ /water/u && $altitude->{$_} > $self->bottom and not $flow->{$_}
!ll&&!rl&&r
5180682
$altitude->{$_} == 7 or $altitude->{$_} == 8 or $world->{$_} =~ /water/u and $altitude->{$_} > $self->bottom
l!l&&r!l&&!r
65527518
$altitude->{$_} == 7 or $altitude->{$_} == 8
l!l&&r!l&&!r
381274545
$world->{$_} =~ /water/u and $altitude->{$_} > $self->bottom
!ll&&!rl&&r
518027
MC/DC66%4/6
($altitude->{$_} == 7 || $altitude->{$_} == 8 || $world->{$_} =~ /water/u && $altitude->{$_} > $self->bottom) && !$flow->{$_} && !($world->{$_} =~ /dry/u)
$altitude->{$_} == 7 $altitude->{$_} == 8 $world->{$_} =~ /water/u $altitude->{$_} > $self->bottom not $flow->{$_} !($world->{$_} =~ /dry/u)
Truth table54%6/11
A or B or C and D and not(E) && not(F)
A= $altitude->{$_} == 7B= $altitude->{$_} == 8C= $world->{$_} =~ /water/uD= $altitude->{$_} > $self->bottomE= not $flow->{$_}F= !($world->{$_} =~ /dry/u)
ABCDEFresult
000XXX0
0010XX0
1XXX0X0
01XX0X0
00110X0
1XXX100
01XX100
0011100
1XXX111
01XX111
0011111
439 } keys %$altitude;
440 4 $self->grow_rivers(\@growing, $water, $flow, $rivers);
441 }
442  
443 4 ▶sub grow_rivers {
Subroutine grow_rivers: called CC=5 SCAR=16.1
Pod undocumented
444 4 my ($self, $growing, $water, $flow, $rivers) = @_;
445 4 ▶ while (@$growing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$growing) 1620 4 1624
446 # warn "Rivers: " . @growing . "\n";
447 # pick a random growing river and grow it
448 1620 my $n = int(rand(scalar @$growing));
449 1620 my $river = $growing->[$n];
450 # warn "Picking @$river\n";
451 1620 my $coordinates = $river->[-1];
452 1620 my $end = 1;
453 1620 ▶ if (defined $water->{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $water->{$coordinates}) { } 1392 228 1620
454 1392 my $other = coordinates($self->neighbor($coordinates, $water->{$coordinates}));
455 1392 ▶ die "Adding $other leads to an infinite loop in river @$river\n" if grep /$other/, @$river;
Branch: 1 branch
BranchTrueFalseTotal
if grep /$other/u, @$river 0 1392 1392
456 # if we flowed into a hex with a river
457 1392 ▶ if (ref $flow->{$other}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $flow->{$other}) { } 30 1362 1392
458 # warn "Prepending @$river to @{$flow->{$other}}\n";
459 # prepend the current river to the other river
460 30 unshift(@{$flow->{$other}}, @$river);
461 # move the source marker
462 30 $flow->{$river->[0]} = $flow->{$other};
463 30 $flow->{$other} = 1;
464 # and remove the current river from the growing list
465 30 splice(@$growing, $n, 1);
466 # warn "Flow at $river->[0]: @{$flow->{$river->[0]}}\n";
467 # warn "Flow at $other: $flow->{$other}\n";
468 } else {
469 1362 $flow->{$coordinates} = 1;
470 1362 push(@$river, $other);
471 }
472 } else {
473 # stop growing this river
474 # warn "Stopped river: @$river\n" if grep(/0914/, @$river);
475 228 push(@$rivers, splice(@$growing, $n, 1));
476 }
477 }
478 }
479  
480 4 ▶sub canyons {
Subroutine canyons: called CC=14 SCAR=30.4
Pod undocumented
481 4 my $self = shift;
482 4 my ($world, $altitude, $rivers, $canyons, $dry) = @_;
483 # using a reference to an array so that we can leave pointers in the %seen hash
484 4 my $canyon = [];
485 # remember which canyon flows through which hex
486 4 my %seen;
487 4 for my $river (@$rivers) {
488 228 my $last = $river->[0];
489 228 my $current_altitude = $altitude->{$last};
490 228 $log->debug("Looking at @$river ($current_altitude)");
491 228 for my $coordinates (@$river) {
492 1246 $log->debug("Looking at $coordinates");
493 1246 ▶ if ($seen{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$coordinates}) 55 1191 1246
494 # the rest of this river was already looked at, so there is no need to
495 # do the rest of this river; if we're in a canyon, prepend it to the one
496 # we just found before ending
497 55 ▶ if (@$canyon) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$canyon) 0 55 55
498 0 my @other = @{$seen{$coordinates}};
499 0 ▶ if ($other[0] eq $canyon->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other[0] eq $canyon->[-1]) { } 0 0 0
500 0 $log->debug("Canyon @$canyon of river @$river merging with @other at $coordinates");
501 0 unshift(@{$seen{$coordinates}}, @$canyon[0 .. @$canyon - 2]);
502 } else {
503 0 $log->debug("Canyon @$canyon of river @$river stumbled upon existing canyon @other at $coordinates");
504 0 ▶ while (@other) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other) 0 0 0
505 0 my $other = shift(@other);
506 0 ▶ next if $other ne $coordinates;
Branch: 1 branch
BranchTrueFalseTotal
if $other ne $coordinates 0 0 0
507 0 push(@$canyon, $other, @other);
508 0 last;
509 }
510 0 $log->debug("Canyon @$canyon");
511 0 push(@$canyons, $canyon);
512 }
513 0 $canyon = [];
514 }
515 55 $log->debug("We've seen the rest: @{$seen{$coordinates}}");
516 55 last;
517 }
518 # no canyons through water!
519 1191 ▶ if ($altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates}
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates} and not $world->{$coordinates} =~ /water|ocean/u) { } 59 1132 1191
Condition100%6/6
$altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates} and not $world->{$coordinates} =~ /water|ocean/u
!ll&&!rl&&r
1131159
$altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates}
!ll&&!rl&&r
16097160
MC/DC100%3/3
$altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates} and not $world->{$coordinates} =~ /water|ocean/u
$altitude->{$coordinates} $current_altitude < $altitude->{$coordinates} not $world->{$coordinates} =~ /water|ocean/u
Truth table100%4/4
A and B and not(C)
A= $altitude->{$coordinates}B= $current_altitude < $altitude->{$coordinates}C= not $world->{$coordinates} =~ /water|ocean/u
ABCresult
0XX0
10X0
1100
1111
520 and $world->{$coordinates} !~ /water|ocean/) {
521 # river is digging a canyon; if this not the start of the river and it
522 # is the start of a canyon, prepend the last step
523 59 ▶ push(@$canyon, $last) unless @$canyon;
Branch: 1 branch
BranchTrueFalseTotal
unless @$canyon 21 38 59
524 59 push(@$canyon, $coordinates);
525 59 ▶ $world->{$coordinates} .= " zone" unless $dry->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
unless $dry->{$coordinates} 59 0 59
526 59 $dry->{$coordinates} = 1;
527 59 $log->debug("Growing canyon @$canyon");
528 59 $seen{$coordinates} = $canyon;
529 } else {
530 # if we just left a canyon, append the current step
531 1132 ▶ if (@$canyon) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$canyon) 21 1111 1132
532 21 push(@$canyon, $coordinates);
533 21 push(@$canyons, $canyon);
534 21 $log->debug("Looking at river @$river");
535 21 $log->debug("Canyon @$canyon");
536 21 $canyon = [];
537 21 last;
538 }
539 # not digging a canyon
540 1111 $last = $coordinates;
541 1111 $current_altitude = $altitude->{$coordinates};
542 }
543 }
544 }
545 }
546  
547 0 ▶sub wet {
Subroutine wet: not called CC=5 SCAR=34.0
Pod undocumented
548 0 my $self = shift;
549 # a hex is wet if there is a river, a swamp or a forest within 2 hexes
550 0 my ($coordinates, $world, $flow) = @_;
551 0 for my $i ($self->neighbors()) {
552 0 my ($x, $y) = $self->neighbor($coordinates, $i);
553 0 my $other = coordinates($x, $y);
554 0 ▶ return 0 if $flow->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $flow->{$other} 0 0 0
555 }
556 0 for my $i ($self->neighbors2()) {
557 0 my ($x, $y) = $self->neighbor2($coordinates, $i);
558 0 my $other = coordinates($x, $y);
559 0 ▶ return 0 if $flow->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $flow->{$other} 0 0 0
560 }
561 0 return 1;
562 }
563  
564 514 ▶sub grow_forest {
Subroutine grow_forest: called CC=25 SCAR=32.2
Pod undocumented
565 514 my ($self, $coordinates, $world, $altitude, $dry) = @_;
566 514 my @candidates;
567 514 ▶ push(@candidates, $coordinates) if $world->{$coordinates} !~ /mountain|hill|water|ocean|swamp|grass/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$coordinates} =~ /mountain|hill|water|ocean|swamp|grass/u 110 404 514
568 514 my $n = $self->arid;
569 # fractions are allowed
570 514 ▶ $n += 1 if rand() < $self->arid - int($self->arid);
Branch: 1 branch
BranchTrueFalseTotal
if rand() < $self->arid - int($self->arid) 0 514 514
571 514 $n = int($n);
572 514 $log->debug("Arid: $n");
573 514 ▶ if ($n >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1) 514 0 514
574 514 for my $i ($self->neighbors()) {
575 2546 my ($x, $y) = $self->neighbor($coordinates, $i);
576 2546 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 211 2335 2546
577 2335 my $other = coordinates($x, $y);
578 2335 ▶ next if $dry->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$other} 92 2243 2335
579 2243 ▶ next if $altitude->{$coordinates} < $altitude->{$other}; # distance of one unless higher
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < $altitude->{$other} 790 1453 2243
580 1453 ▶ push(@candidates, $other) if $world->{$other} !~ /mountain|hill|water|ocean|swamp|grass/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$other} =~ /mountain|hill|water|ocean|swamp|grass/u 457 996 1453
581 }
582 }
583 514 ▶ if ($n >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 2) 514 0 514
584 514 for my $i ($self->neighbors2()) {
585 5092 my ($x, $y) = $self->neighbor2($coordinates, $i);
586 5092 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 797 4295 5092
587 4295 my $other = coordinates($x, $y);
588 4295 ▶ next if $altitude->{$coordinates} <= $altitude->{$other}; # distance of two only if lower
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} <= $altitude->{$other} 3237 1058 4295
589 1058 my $ok = 0;
590 1058 for my $m ($self->neighbors()) {
591 3196 my ($mx, $my) = $self->neighbor($coordinates, $m);
592 3196 ▶ next unless $self->legal($mx, $my);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($mx, $my) 122 3074 3196
593 3074 my $midway = coordinates($mx, $my);
594 3074 ▶ next if $dry->{$midway};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$midway} 106 2968 3074
595 2968 ▶ next if $self->distance($midway, $other) != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->distance($midway, $other) != 1 1876 1092 2968
596 1092 ▶ next if $altitude->{$coordinates} < $altitude->{$midway};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < $altitude->{$midway} 71 1021 1092
597 1021 ▶ next if $altitude->{$midway} < $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$midway} < $altitude->{$other} 54 967 1021
598 967 $ok = 1;
599 967 last;
600 }
601 1058 ▶ next unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 91 967 1058
602 967 ▶ push(@candidates, $other) if $world->{$other} !~ /mountain|hill|water|ocean|swamp|grass/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$other} =~ /mountain|hill|water|ocean|swamp|grass/u 391 576 967
603 }
604 }
605 514 $log->debug("forest growth: $coordinates: @candidates");
606 514 for $coordinates (@candidates) {
607 958 ▶ if ($altitude->{$coordinates} >= 7) {
Branch: 3 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 7) { } 258 700 958
elsif ($altitude->{$coordinates} >= 6) { } 508 192 700
elsif ($altitude->{$coordinates} >= 4) { } 178 14 192
608 258 $world->{$coordinates} = "light-green fir-forest";
609 } elsif ($altitude->{$coordinates} >= 6) {
610 508 $world->{$coordinates} = "green fir-forest";
611 } elsif ($altitude->{$coordinates} >= 4) {
612 178 $world->{$coordinates} = "green forest";
613 } else {
614 14 $world->{$coordinates} = "dark-green forest";
615 }
616 }
617 }
618  
619 4 ▶sub forests {
Subroutine forests: called CC=3 SCAR=11.0
Pod undocumented
620 4 my ($self, $world, $altitude, $flow, $dry) = @_;
621 # Empty hexes with a river flowing through them (and nearby hexes) are forest
622 # filled valleys.
623 4 for my $coordinates (keys %$flow) {
624 573 ▶ next if $dry->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$coordinates} 59 514 573
625 514 $self->grow_forest($coordinates, $world, $altitude, $dry);
626 }
627 }
628  
629 4 ▶sub winds {
Subroutine winds: called CC=7 SCAR=19.5
Pod undocumented
630 4 my $self = shift;
631 4 my ($world, $altitude, $water, $flow) = @_;
632 4 ▶ my $wind = $self->wind // $self->random_neighbor;
Condition33%1/3
$self->wind // $self->random_neighbor
l!l&&r!l&&!r
040
MC/DC0%0/2
$self->wind // $self->random_neighbor
$self->wind $self->random_neighbor
Truth table33%1/3
A // B
A= $self->windB= $self->random_neighbor
ABresult
1X1
011
000
633 4 $world->{"0101"} .= " wind" . $self->reverse($wind);
634 4 for my $coordinates (keys %$altitude) {
635 # limit ourselves to altitude 7 and 8
636 1200 ▶ next if $altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8 545 655 1200
Condition100%3/3
$altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8
l!l&&r!l&&!r
405140655
MC/DC100%2/2
$altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8
$altitude->{$coordinates} < 7 $altitude->{$coordinates} > 8
Truth table100%3/3
A or B
A= $altitude->{$coordinates} < 7B= $altitude->{$coordinates} > 8
ABresult
1X1
011
000
637 # look at the neighbor the water would flow to
638 655 my ($x, $y) = $self->neighbor($coordinates, $wind);
639 # skip if off the map
640 655 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 48 607 655
641 607 my $other = coordinates($x, $y);
642 # skip if the other hex is lower
643 607 ▶ next if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 183 424 607
644 # if the other hex was higher, this land is dry
645 424 $log->debug("$coordinates is dry because of $other");
646 424 $world->{$coordinates} .= " dry zone"; # use label for debugging
647 }
648 }
649  
650 4 ▶sub bogs {
Subroutine bogs: called CC=6 SCAR=17.9
Pod undocumented
651 4 my $self = shift;
652 4 my ($world, $altitude, $water) = @_;
653 4 for my $coordinates (keys %$altitude) {
654 # limit ourselves to altitude 7
655 1200 ▶ next if $altitude->{$coordinates} != 7;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} != 7 819 381 1200
656 # don't turn lakes into bogs
657 381 ▶ next if $world->{$coordinates} =~ /water|ocean/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /water|ocean/u 13 368 381
658 # look at the neighbor the water would flow to
659 368 my ($x, $y) = $self->neighbor($coordinates, $water->{$coordinates});
660 # skip if water flows off the map
661 368 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 27 341 368
662 341 my $other = coordinates($x, $y);
663 # skip if water flows downhill
664 341 ▶ next if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 177 164 341
665 # if there was no lower neighbor, this is a bog
666 164 $world->{$coordinates} =~ s/height\d+/grey swamp/;
667 }
668 }
669  
670 4 ▶sub dry {
Subroutine dry: called CC=16 SCAR=27.7
Pod undocumented
671 4 my ($self, $world, $altitude, $rivers) = @_;
672 4 my @dry;
673 4 for my $coordinates (shuffle sort keys %$world) {
674 1200 ▶ if ($world->{$coordinates} !~ /mountain|hill|water|ocean|swamp|grass|forest|firs|trees/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($world->{$coordinates} =~ /mountain|hill|water|ocean|swamp|grass|forest|firs|trees/u) 98 1102 1200
675 98 ▶ if ($altitude->{$coordinates} >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 7) { } 21 77 98
676 21 $world->{$coordinates} = "light-grey grass";
677 } else {
678 77 $world->{$coordinates} = "light-green bushes";
679 77 push(@dry, $coordinates);
680 }
681 }
682 }
683  
684 BUSHES:
685 4 for my $coordinates (@dry) {
686 77 for my $i ($self->neighbors()) {
687 264 my ($x, $y) = $self->neighbor($coordinates, $i);
688 264 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 50 214 264
689 214 my $other = coordinates($x, $y);
690 214 ▶ next BUSHES if $world->{$other} =~ /forest|firs|trees|swamp/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$other} =~ /forest|firs|trees|swamp/u 49 165 214
691 }
692 28 ▶ if ($altitude->{$coordinates} >= 5) {
Branch: 2 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 5) { } 22 6 28
elsif ($altitude->{$coordinates} >= 3) { } 5 1 6
693 22 $world->{$coordinates} =~ s/light-green bushes/light-grey grass/;
694 } elsif ($altitude->{$coordinates} >= 3) {
695 5 $world->{$coordinates} =~ s/light-green bushes/grey grass/;
696 } else {
697 1 $world->{$coordinates} =~ s/light-green bushes/dark-grey grass/;
698 }
699 }
700  
701 GRASS:
702 4 for my $coordinates (@dry) {
703 77 ▶ next if $self->with_river($rivers, $coordinates);
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_river($rivers, $coordinates) 13 64 77
704 64 for my $i ($self->neighbors()) {
705 160 my ($x, $y) = $self->neighbor($coordinates, $i);
706 160 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 41 119 160
707 119 my $other = coordinates($x, $y);
708 119 ▶ next GRASS if $world->{$other} !~ /grass|desert|water/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$other} =~ /grass|desert|water/u 51 68 119
709 }
710 13 ▶ if ($altitude->{$coordinates} >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 3) { } 12 1 13
711 12 $world->{$coordinates} =~ s/(light-|dark-)?grey grass/light-grey desert/;
712 } else {
713 1 $world->{$coordinates} =~ s/(light-|dark-)?grey grass/dust desert/;
714 }
715 }
716 }
717  
718 77 ▶sub with_river {
Subroutine with_river: called CC=3 SCAR=11.0
Pod undocumented
719 77 my ($self, $rivers, $coordinates) = @_;
720 77 for my $river (@$rivers) {
721 4061 ▶ return 1 if grep { $coordinates eq $_ } (@$river);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$coordinates eq $_;} @$river 13 4048 4061
722 }
723 }
724  
725 8 ▶sub settlements {
Subroutine settlements: called CC=21 SCAR=30.5
Pod undocumented
726 8 my $self = shift;
727 8 my ($world, $flow) = @_;
728 8 my @settlements;
729 8 my $max = $self->height * $self->width;
730 # do not match forest-hill
731 8 my @candidates = shuffle sort grep { $world->{$_} =~ /\b(fir-forest|forest(?!-hill))\b/ } keys %$world;
732 8 @candidates = $self->remove_closer_than(2, @candidates);
733 8 ▶ @candidates = @candidates[0 .. int($max/10 - 1)] if @candidates > $max/10;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 10 4 4 8
734 8 push(@settlements, @candidates);
735 8 for my $coordinates (@candidates) {
736 $world->{$coordinates} =~ s/fir-forest/firs thorp/
737 130 ▶ or $world->{$coordinates} =~ s/forest(?!-hill)/trees thorp/;
Branch: 1 branch
BranchTrueFalseTotal
$world->{$coordinates} =~ s/fir-forest/firs thorp/u or $world->{$coordinates} =~ s/forest(?!-hill)/trees thorp/u 93 37 130
738 }
739 8 ▶ @candidates = shuffle sort grep { $world->{$_} =~ /(?<!fir-)forest(?!-hill)/ and $flow->{$_}} keys %$world;
Condition100%3/3
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u && $flow->{$_}
!ll&&!rl&&r
35572419
MC/DC100%2/2
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u && $flow->{$_}
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u $flow->{$_}
Truth table100%3/3
A && B
A= $world->{$_} =~ /(?<!fir-)forest(?!-hill)/uB= $flow->{$_}
ABresult
0X0
100
111
740 8 @candidates = $self->remove_closer_than(5, @candidates);
741 8 ▶ @candidates = @candidates[0 .. int($max/20 - 1)] if @candidates > $max/20;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 20 0 8 8
742 8 push(@settlements, @candidates);
743 8 for my $coordinates (@candidates) {
744 12 $world->{$coordinates} =~ s/forest/trees village/;
745 }
746 8 ▶ @candidates = shuffle sort grep { $world->{$_} =~ /(?<!fir-)forest(?!-hill)/ and $flow->{$_} } keys %$world;
Condition100%3/3
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u && $flow->{$_}
!ll&&!rl&&r
3569247
MC/DC100%2/2
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u && $flow->{$_}
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u $flow->{$_}
Truth table100%3/3
A && B
A= $world->{$_} =~ /(?<!fir-)forest(?!-hill)/uB= $flow->{$_}
ABresult
0X0
100
111
747 8 @candidates = $self->remove_closer_than(10, @candidates);
748 8 ▶ @candidates = @candidates[0 .. int($max/40 - 1)] if @candidates > $max/40;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 40 0 8 8
749 8 push(@settlements, @candidates);
750 8 for my $coordinates (@candidates) {
751 5 $world->{$coordinates} =~ s/forest/trees town/;
752 }
753 8 @candidates = shuffle sort grep { $world->{$_} =~ /white mountain\b/ } keys %$world;
754 8 @candidates = $self->remove_closer_than(10, @candidates);
755 8 ▶ @candidates = @candidates[0 .. int($max/40 - 1)] if @candidates > $max/40;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 40 0 8 8
756 8 push(@settlements, @candidates);
757 8 for my $coordinates (@candidates) {
758 11 $world->{$coordinates} =~ s/white mountain\b/white mountain law/;
759 }
760 8 @candidates = shuffle sort grep { $world->{$_} =~ /swamp/ } keys %$world;
761 8 @candidates = $self->remove_closer_than(10, @candidates);
762 8 ▶ @candidates = @candidates[0 .. int($max/40 - 1)] if @candidates > $max/40;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 40 0 8 8
763 8 push(@settlements, @candidates);
764 8 for my $coordinates (@candidates) {
765 16 $world->{$coordinates} =~ s/swamp/swamp2 chaos/;
766 }
767 8 for my $coordinates (@settlements) {
768 174 for my $i ($self->neighbors()) {
769 769 my ($x, $y) = $self->neighbor($coordinates, $i);
770 769 my $other = coordinates($x, $y);
771 769 ▶ next unless $world->{$other} and $world->{$other} =~ /water|ocean/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$other} and $world->{$other} =~ /water|ocean/u 732 37 769
Condition100%3/3
$world->{$other} and $world->{$other} =~ /water|ocean/u
!ll&&!rl&&r
10562737
MC/DC100%2/2
$world->{$other} and $world->{$other} =~ /water|ocean/u
$world->{$other} $world->{$other} =~ /water|ocean/u
Truth table100%3/3
A and B
A= $world->{$other}B= $world->{$other} =~ /water|ocean/u
ABresult
0X0
100
111
772 # bump ports one size category up
773 37 $world->{$coordinates} =~ s/large-town/city port/;
774 37 $world->{$coordinates} =~ s/town/large-town port/;
775 37 $world->{$coordinates} =~ s/village/town port/;
776 # no bumps for thorps
777 37 last;
778 }
779 }
780 8 for my $coordinates (@settlements) {
781 # thorps and villages don't cut enough wood; make sure to get both "green" and "dark-green"
782 174 ▶ $world->{$coordinates} =~ s/\S*green trees/light-soil/ if $world->{$coordinates} =~ /large-town|city/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /large-town|city/u 1 173 174
783 174 ▶ $world->{$coordinates} =~ s/\S*green trees/soil/ if $world->{$coordinates} =~ / town/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ / town/u 7 167 174
784 }
785 8 return @settlements;
786 }
787  
788 8 ▶sub trails {
Subroutine trails: called CC=11 SCAR=24.0
Pod undocumented
789 8 my $self = shift;
790 8 my ($altitude, $settlements) = @_;
791 # look for a neighbor that is as low as possible and nearby
792 8 my %trails;
793 8 my @from = shuffle @$settlements;
794 8 my @to = shuffle @$settlements;
795 8 for my $from (@from) {
796 174 my ($best, $best_distance, $best_altitude);
797 174 for my $to (@to) {
798 6804 ▶ next if $from eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $to 174 6630 6804
799 6630 my $distance = $self->distance($from, $to);
800 6630 $log->debug("Considering $from-$to: distance $distance, altitude " . $altitude->{$to});
801 6630 ▶ if ($distance <= 3
Branch: 1 branch
BranchTrueFalseTotal
if ($distance <= 3 and !$best_distance || $distance <= $best_distance and !$best || $altitude->{$to} < $best_altitude) 209 6421 6630
Condition100%12/12
$distance <= 3 and !$best_distance || $distance <= $best_distance and !$best || $altitude->{$to} < $best_altitude
!ll&&!rl&&r
6215206209
$distance <= 3 and !$best_distance || $distance <= $best_distance
!ll&&!rl&&r
6072143415
not $best_distance or $distance <= $best_distance
l!l&&r!l&&!r
167248143
not $best or $altitude->{$to} < $best_altitude
l!l&&r!l&&!r
16742206
MC/DC80%4/5
$distance <= 3 and !$best_distance || $distance <= $best_distance and !$best || $altitude->{$to} < $best_altitude
$distance <= 3 not $best_distance $distance <= $best_distance not $best $altitude->{$to} < $best_altitude
Truth table62%5/8
A and not(B) or C and not(D) or E
A= $distance <= 3B= not $best_distanceC= $distance <= $best_distanceD= not $bestE= $altitude->{$to} < $best_altitude
ABCDEresult
0XXXX0
100XX0
11X000
101000
11X1X1
11X011
1011X1
101011
802 and (not $best_distance or $distance <= $best_distance)
803 and (not $best or $altitude->{$to} < $best_altitude)) {
804 209 $best = $to;
805 209 $best_altitude = $altitude->{$best};
806 209 $best_distance = $distance;
807 }
808 }
809 174 ▶ next if not $best;
Branch: 1 branch
BranchTrueFalseTotal
unless $best 7 167 174
810 # skip if it already exists in the other direction
811 167 ▶ next if $trails{"$best-$from"};
Branch: 1 branch
BranchTrueFalseTotal
if $trails{"$best-$from"} 31 136 167
812 136 $trails{"$from-$best"} = 1;
813 136 $log->debug("Trail $from-$best");
814 }
815 8 return keys %trails;
816 }
817  
818 4 ▶sub cliffs {
Subroutine cliffs: called CC=7 SCAR=19.5
Pod undocumented
819 4 my $self = shift;
820 4 my ($world, $altitude) = @_;
821 4 my @neighbors = $self->neighbors();
822 # hexes with altitude difference bigger than 1 have cliffs
823 4 for my $coordinates (keys %$world) {
824 1200 ▶ next if $altitude->{$coordinates} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} <= $self->bottom 0 1200 1200
825 1200 for my $i (@neighbors) {
826 6000 my ($x, $y) = $self->neighbor($coordinates, $i);
827 6000 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 476 5524 6000
828 5524 my $other = coordinates($x, $y);
829 5524 ▶ if ($altitude->{$coordinates} - $altitude->{$other} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} - $altitude->{$other} >= 2) 347 5177 5524
830 347 ▶ if (@neighbors == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@neighbors == 6) { } 220 127 347
831 220 $world->{$coordinates} .= " cliff$i";
832 } else { # square
833 127 $world->{$coordinates} .= " cliffs$i";
834 }
835 }
836 }
837 }
838 }
839  
840 4 ▶sub marshlands {
Subroutine marshlands: called CC=9 SCAR=29.0
Pod undocumented
841 4 my ($self, $world, $altitude, $rivers) = @_;
842 4 my %seen;
843 4 for my $river (@$rivers) {
844 228 my $last = $river->[0];
845 228 for my $coordinates (@$river) {
846 804 ▶ last if $seen{$coordinates}; # we've been here before
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$coordinates} 163 641 804
847 641 $seen{$coordinates} = 1;
848 641 ▶ next unless exists $altitude->{$coordinates}; # rivers ending off the map
Branch: 1 branch
BranchTrueFalseTotal
unless exists $altitude->{$coordinates} 65 576 641
849 576 ▶ if ($altitude->{$coordinates} <= $self->bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} <= $self->bottom) 0 576 576
850 0 ▶ if ($altitude->{$coordinates} == $self->bottom
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} == $self->bottom and $world->{$coordinates} =~ /water|ocean/u and $altitude->{$coordinates} == $altitude->{$last} - 1) { } 0 0 0
Condition0%0/6
$altitude->{$coordinates} == $self->bottom and $world->{$coordinates} =~ /water|ocean/u and $altitude->{$coordinates} == $altitude->{$last} - 1
!ll&&!rl&&r
000
$altitude->{$coordinates} == $self->bottom and $world->{$coordinates} =~ /water|ocean/u
!ll&&!rl&&r
000
MC/DC0%0/3
$altitude->{$coordinates} == $self->bottom and $world->{$coordinates} =~ /water|ocean/u and $altitude->{$coordinates} == $altitude->{$last} - 1
$altitude->{$coordinates} == $self->bottom $world->{$coordinates} =~ /water|ocean/u $altitude->{$coordinates} == $altitude->{$last} - 1
Truth table0%0/4
A and B and C
A= $altitude->{$coordinates} == $self->bottomB= $world->{$coordinates} =~ /water|ocean/uC= $altitude->{$coordinates} == $altitude->{$last} - 1
ABCresult
0XX0
10X0
1100
1111
851 and $world->{$coordinates} =~ /water|ocean/
852 and $altitude->{$coordinates} == $altitude->{$last} - 1) {
853 0 $world->{$coordinates} = "blue-green swamp";
854 } else {
855 0 $world->{$coordinates} =~ s/ocean/water/;
856 0 delete $seen{$coordinates};
857 0 last;
858 }
859 }
860 576 $last = $coordinates;
861 }
862 }
863 }
864  
865 4 ▶sub desertification {
Subroutine desertification: called CC=15 SCAR=52.8
Pod undocumented
866 4 my ($self, $world, $altitude, $rivers) = @_;
867 4 ▶ return unless $self->climate eq 'desert';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->climate eq "desert" 4 0 4
868 0 for my $coordinates (keys %$world) {
869 0 ▶ if ($self->with_river($rivers, $coordinates)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_river($rivers, $coordinates)) { } 0 0 0
870 $world->{$coordinates} =~ s/light-grey/light-green/
871 0 ▶ or $world->{$coordinates} =~ s/dark-green/green/
Branch: 1 branch
BranchTrueFalseTotal
$world->{$coordinates} =~ s/light-grey/light-green/u or $world->{$coordinates} =~ s/dark-green/green/u 0 0 0
872 } else {
873 $world->{$coordinates} =~ s/light-green bushes/rock bushes/
874 or $world->{$coordinates} =~ s/light-grey grass/rock bush/
875 or $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/
876 or $world->{$coordinates} =~ s/^grey grass/rock bush/
877 or $altitude->{$coordinates} >= 4 and $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/
878 0 ▶ or $altitude->{$coordinates} >= 2 and $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/
Branch: 1 branch
BranchTrueFalseTotal
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u or $world->{$coordinates} =~ s/light-grey grass/rock bush/u or $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u or $world->{$coordinates} =~ s/^grey grass/rock bush/u or $altitude->{$coordinates} >= 4 and $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/u or $altitude->{$coordinates} >= 2 and $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/u 0 0 0
Condition0%0/21
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u || $world->{$coordinates} =~ s/light-grey grass/rock bush/u || $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u || $world->{$coordinates} =~ s/^grey grass/rock bush/u || $altitude->{$coordinates} >= 4 && $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/u || $altitude->{$coordinates} >= 2 && $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/u
l!l&&r!l&&!r
000
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u or $world->{$coordinates} =~ s/light-grey grass/rock bush/u or $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u or $world->{$coordinates} =~ s/^grey grass/rock bush/u or $altitude->{$coordinates} >= 4 and $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/u
l!l&&r!l&&!r
000
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u or $world->{$coordinates} =~ s/light-grey grass/rock bush/u or $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u or $world->{$coordinates} =~ s/^grey grass/rock bush/u
l!l&&r!l&&!r
000
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u or $world->{$coordinates} =~ s/light-grey grass/rock bush/u or $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u
l!l&&r!l&&!r
000
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u or $world->{$coordinates} =~ s/light-grey grass/rock bush/u
l!l&&r!l&&!r
000
$altitude->{$coordinates} >= 4 and $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/u
!ll&&!rl&&r
000
$altitude->{$coordinates} >= 2 and $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/u
!ll&&!rl&&r
000
MC/DC0%0/8
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u || $world->{$coordinates} =~ s/light-grey grass/rock bush/u || $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u || $world->{$coordinates} =~ s/^grey grass/rock bush/u || $altitude->{$coordinates} >= 4 && $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/u || $altitude->{$coordinates} >= 2 && $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/u
$world->{$coordinates} =~ s/light-green bushes/rock bushes/u $world->{$coordinates} =~ s/light-grey grass/rock bush/u $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/u $world->{$coordinates} =~ s/^grey grass/rock bush/u $altitude->{$coordinates} >= 4 $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/u $altitude->{$coordinates} >= 2 $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/u
Truth table0%0/11
A or B or C or D or E and F || G and H
A= $world->{$coordinates} =~ s/light-green bushes/rock bushes/uB= $world->{$coordinates} =~ s/light-grey grass/rock bush/uC= $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/uD= $world->{$coordinates} =~ s/^grey grass/rock bush/uE= $altitude->{$coordinates} >= 4F= $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/uG= $altitude->{$coordinates} >= 2H= $world->{$coordinates} =~ s/(dust|light-grey) desert/light-grey desert/u
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
000011XX1
00000X111
000010111
00000X0X0
00000X100
0000100X0
000010100
879 }
880 $world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/
881 or $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/
882 or $world->{$coordinates} =~ s/fir-forest/trees/
883 0 ▶ or $world->{$coordinates} =~ s/firs/trees/;
Branch: 1 branch
BranchTrueFalseTotal
$world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/u or $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/u or $world->{$coordinates} =~ s/fir-forest/trees/u or $world->{$coordinates} =~ s/firs/trees/u 0 0 0
Condition0%0/6
$world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/u or $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/u or $world->{$coordinates} =~ s/fir-forest/trees/u
l!l&&r!l&&!r
000
$world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/u or $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/u
l!l&&r!l&&!r
000
MC/DC0%0/3
$world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/u or $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/u or $world->{$coordinates} =~ s/fir-forest/trees/u
$world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/u $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/u $world->{$coordinates} =~ s/fir-forest/trees/u
Truth table0%0/4
A or B or C
A= $world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/uB= $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/uC= $world->{$coordinates} =~ s/fir-forest/trees/u
ABCresult
1XX1
01X1
0011
0000
884 }
885 }
886  
887 4 ▶sub generate {
Subroutine generate: called CC=3 SCAR=11.0
Pod undocumented
888 4 my ($self, $world, $altitude, $water, $rivers, $settlements, $trails, $canyons, $step) = @_;
889 # $flow indicates that there is actually a river in this hex
890 4 my $flow = {};
891 # $dry indicates that is a river in this hex, but it cut itself a canyon
892 4 my $dry = {};
893 my @code = (
894 4 ▶ sub { $self->flat($altitude);
Subroutine __ANON__: called CC=1 SCAR=0.0
895 4 $self->altitude($world, $altitude); },
896 4 ▶ sub { $self->bumpiness($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
897 4 ▶ sub { $self->mountains($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
898 4 ▶ sub { $self->ocean($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
899 4 ▶ sub { $self->water($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
900 4 ▶ sub { $self->lakes($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
901 4 ▶ sub { $self->flood($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
902 4 ▶ sub { $self->bogs($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
903 4 ▶ sub { $self->winds($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
904 4 ▶ sub { $self->rivers($world, $altitude, $water, $flow, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
905 4 ▶ sub { $self->canyons($world, $altitude, $rivers, $canyons, $dry); },
Subroutine __ANON__: called CC=1 SCAR=0.0
906 4 ▶ sub { $self->swamps($world, $altitude, $water, $flow, $dry); },
Subroutine __ANON__: called CC=1 SCAR=0.0
907 4 ▶ sub { $self->forests($world, $altitude, $flow, $dry); },
Subroutine __ANON__: called CC=1 SCAR=0.0
908 4 ▶ sub { $self->dry($world, $altitude, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
909 4 ▶ sub { $self->cliffs($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
910 4 ▶ sub { push(@$settlements, $self->settlements($world, $flow)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
911 4 ▶ sub { push(@$trails, $self->trails($altitude, $settlements)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
912 4 ▶ sub { $self->marshlands($world, $altitude, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
913 4 ▶ sub { $self->desertification($world, $altitude, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
914 # make sure you look at "alpine_document.html.ep" if you change this list!
915 # make sure you look at '/alpine/document' if you add to this list!
916 4 );
917  
918 # $step 0 runs all the code; note that we can't simply cache those results
919 # because we need to start over with the same seed!
920 4 my $i = 1;
921 4 ▶ while (@code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@code) 76 4 80
922 76 shift(@code)->();
923 76 ▶ return if $step == $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $step == $i++ 0 76 76
924 76 $self->fixup($world, $altitude, $i);
925 }
926 }
927  
928 # Remove temporary markers that won't be needed in the next step
929 76 ▶sub fixup {
Subroutine fixup: called CC=4 SCAR=13.9
Pod undocumented
930 76 my ($self, $world, $altitude, $step, $last) = @_;
931 # When documenting or debugging, water flow arrows are no longer needed when
932 # the rivers are added.
933 76 ▶ if ($step >= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step >= 10) 44 32 76
934 44 for my $coordinates (keys %$world) {
935 13200 $world->{$coordinates} =~ s/ arrow\d//;
936 }
937 }
938 # Wind direction is only shown once.
939 76 $world->{"0101"} =~ s/ wind\d//;
940 # Remove zone markers.
941 76 for my $coordinates (keys %$world) {
942 22800 $world->{$coordinates} =~ s/ zone//;
943 }
944 }
945  
946 4 ▶sub generate_map {
Subroutine generate_map: called CC=19 SCAR=32.6
Pod undocumented
947 4 my $self = shift;
948  
949 # The parameters turn into class variables.
950 4 ▶ $self->width(shift // 30);
Condition50%1/2
shift() // 30
l!l
04
MC/DC0%0/1
shift() // 30
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
951 4 ▶ $self->height(shift // 10);
Condition50%1/2
shift() // 10
l!l
04
MC/DC0%0/1
shift() // 10
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
952 4 ▶ $self->steepness(shift // 3);
Condition50%1/2
shift() // 3
l!l
04
MC/DC0%0/1
shift() // 3
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
953 4 ▶ $self->peaks(shift // int($self->width * $self->height / 40));
Condition50%1/2
shift() // int $self->width * $self->height / 40
l!l
04
MC/DC0%0/1
shift() // int $self->width * $self->height / 40
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
954 4 ▶ $self->peak(shift // 10);
Condition50%1/2
shift() // 10
l!l
04
MC/DC0%0/1
shift() // 10
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
955 4 ▶ $self->peak_min(shift // $self->peak);
Condition33%1/3
shift() // $self->peak
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() // $self->peak
shift() $self->peak
Truth table33%1/3
A // B
A= shift()B= $self->peak
ABresult
1X1
011
000
956 4 ▶ $self->bumps(shift // int($self->width * $self->height / 40));
Condition50%1/2
shift() // int $self->width * $self->height / 40
l!l
04
MC/DC0%0/1
shift() // int $self->width * $self->height / 40
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
957 4 ▶ $self->bump(shift // 2);
Condition50%1/2
shift() // 2
l!l
04
MC/DC0%0/1
shift() // 2
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
958 4 ▶ $self->bottom(shift // 0);
Condition50%1/2
shift() // 0
l!l
04
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
959 4 ▶ $self->arid(shift // 2);
Condition50%1/2
shift() // 2
l!l
04
MC/DC0%0/1
shift() // 2
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
960 4 ▶ $self->climate(shift ? 'desert' : 'temperate');
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 4 4
961 4 $self->wind(shift); # or random
962 4 ▶ my $seed = shift||time;
Condition100%2/2
shift() || time
l!l
22
MC/DC100%1/1
shift() || time
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
963 4 my $url = shift;
964 4 ▶ my $step = shift||0;
Condition50%1/2
shift() || 0
l!l
04
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
965  
966 # For documentation purposes, I want to be able to set the pseudo-random
967 # number seed using srand and rely on rand to reproduce the same sequence of
968 # pseudo-random numbers for the same seed. The key point to remember is that
969 # the keys function will return keys in random order. So if we look over the
970 # result of keys, we need to look at the code in the loop: If order is
971 # important, that wont do. We need to sort the keys. If we want the keys to be
972 # pseudo-shuffled, use shuffle sort keys.
973 4 srand($seed);
974  
975 # Keys for all hashes are coordinates such as "0101".
976 # %world is the description with values such as "green forest".
977 # %altitude is the altitude with values such as 3.
978 # %water is the preferred direction water would take with values such as 0.
979 # (north west); 0 means we need to use "if defined".
980 # @rivers are the rivers with values such as ["0102", "0202"].
981 # @settlements are are the locations of settlements such as "0101".
982 # @trails are the trails connecting these with values as "0102-0202".
983 # $step is how far we want map generation to go where 0 means all the way.
984 4 my ($world, $altitude, $water, $rivers, $settlements, $trails, $canyons) =
985 ({}, {}, {}, [], [], [], []);
986 4 $self->generate($world, $altitude, $water, $rivers, $settlements, $trails, $canyons, $step);
987  
988 # When documenting or debugging, add altitude as a label.
989 4 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 4 4
990 0 for my $coordinates (keys %$world) {
991 0 $world->{$coordinates} .= ' "' . $altitude->{$coordinates} . '"';
992 }
993 }
994  
995 4 local $" = "-"; # list items separated by -
996 4 my @lines;
997 4 push(@lines, map { $_ . " " . $world->{$_} } sort keys %$world);
998 4 push(@lines, map { "$_ trail" } @$trails);
999 4 push(@lines, map { "@$_ river" } @$rivers);
1000 4 push(@lines, map { "@$_ canyon" } @$canyons); # after rivers
1001 4 push(@lines, "include gnomeyland.txt");
1002  
1003 # when documenting or debugging, add some more lines at the end
1004 4 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 4 4
1005 # visualize height
1006 push(@lines,
1007 map {
1008 0 my $n = int(25.5 * $_);
1009 0 qq{height$_ attributes fill="rgb($n,$n,$n)"};
1010 } (0 .. 10));
1011 # visualize water flow
1012 0 push(@lines, $self->arrows());
1013 }
1014  
1015 4 push(@lines, "# Seed: $seed");
1016 4 ▶ push(@lines, "# Documentation: " . $url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 2 2 4
1017 4 my $map = join("\n", @lines);
1018 4 ▶ return $map, $self if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 2 4
1019 2 return $map;
1020 }
1021  
1022 11 1;
« blib/lib/Game/TextMapper/Traveller.pm ↑ Summary blib/lib/Game/TextMapper/Smale.pm »