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

filter: Statementstmt 90.8% 536 / 590 Branchbran 78.3% 232 / 296 Conditioncond 60.6% 91 / 150 MC/DCmcdc 37.6% 29 / 77 Subroutinesub 98.1% 52 / 53 Podpod 0.0% 0 / 29 totaltotal 78.6% 940 / 1195 CC 256 SCAR 31.8
CC
256
Cov
83%
desertification
197.0
wet
30.0
fix_altitude
28.7
CRAP 24.2
« blib/lib/Game/TextMapper/Command/random.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 'bumps';
61 11 has 'bump';
62 11 has 'bottom';
63 11 has 'arid';
64 11 has 'climate';
65 11 has 'wind';
66  
67 sub place_peak {
68 4 ▶ my $self = shift;
Subroutine place_peak: called CC=4 SCAR=13.9
Pod undocumented
69 4 my $altitude = shift;
70 4 my $count = shift;
71 4 my $current_altitude = shift;
72 4 my @queue;
73 # place some peaks and put them in a queue
74 4 for (1 .. $count) {
75 # try to find an empty hex
76 28 for (1 .. 6) {
77 28 my $x = int(rand($self->width)) + 1;
78 28 my $y = int(rand($self->height)) + 1;
79 28 my $coordinates = coordinates($x, $y);
80 28 ▶ next if $altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} 0 28 28
81 28 $altitude->{$coordinates} = $current_altitude;
82 28 $log->debug("placed $current_altitude at $coordinates");
83 28 push(@queue, $coordinates);
84 28 last;
85 }
86 }
87 4 return @queue;
88 }
89  
90 sub grow_mountains {
91 4 ▶ my $self = shift;
Subroutine grow_mountains: called CC=13 SCAR=25.7
Pod undocumented
92 4 my $altitude = shift;
93 4 my @queue = @_;
94 # go through the queue and add adjacent lower altitude hexes, if possible; the
95 # hexes added are to the end of the queue
96 4 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 1200 4 1204
97 1200 my $coordinates = shift @queue;
98 1200 my $current_altitude = $altitude->{$coordinates};
99 1200 ▶ next unless $current_altitude > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_altitude > 0 0 1200 1200
100 # pick some random neighbors based on variable steepness
101 1200 my $n = $self->steepness;
102 # round up based on fraction
103 1200 ▶ $n += 1 if rand() < $n - int($n);
Branch: 1 branch
BranchTrueFalseTotal
if rand() < $n - int($n) 0 1200 1200
104 1200 $n = int($n);
105 1200 ▶ next if $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 0 1200 1200
106 1200 for (1 .. $n) {
107 # try to find an empty neighbor; make more attempts if we're looking for
108 # more neighbours
109 3600 for my $attempt (1 .. 3 + $n) {
110 17347 my ($x, $y) = $self->neighbor($coordinates, $self->random_neighbor());
111 17347 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 1553 15794 17347
112 15794 my $other = coordinates($x, $y);
113 # if this is taken, look further
114 15794 ▶ if ($altitude->{$other}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$other}) 15279 515 15794
115 15279 ($x, $y) = $self->neighbor2($coordinates, $self->random_neighbor2());
116 15279 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 2013 13266 15279
117 13266 $other = coordinates($x, $y);
118 # if this is also taken, try again – but if we've already had four
119 # attempts, jump!
120 13266 ▶ if ($altitude->{$other}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$other}) 12609 657 13266
121 12609 ▶ $coordinates = $other if $attempt > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt > 4 3791 8818 12609
122 12609 next;
123 }
124 }
125 # if we found an empty neighbor, set its altitude
126 1172 ▶ $altitude->{$other} = $current_altitude > 0 ? $current_altitude - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$current_altitude > 0 ? : 1172 0 1172
127 1172 push(@queue, $other);
128 1172 last;
129 }
130 }
131 }
132 }
133  
134 sub fix_altitude {
135 4 ▶ my $self = shift;
Subroutine fix_altitude: called CC=7 SCAR=33.6
Pod undocumented
136 4 my $altitude = shift;
137 # go through all the hexes
138 4 for my $coordinates (sort keys %$altitude) {
139 # find hexes that we missed and give them the height of a random neighbor
140 1200 ▶ if (not defined $altitude->{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $altitude->{$coordinates}) 0 1200 1200
141 # warn "identified a hex that was skipped: $coordinates\n";
142 # try to find a suitable neighbor
143 0 for (1 .. 6) {
144 0 my ($x, $y) = $self->neighbor($coordinates, $self->random_neighbor());
145 0 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 0 0 0
146 0 my $other = coordinates($x, $y);
147 0 ▶ next unless defined $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $altitude->{$other} 0 0 0
148 0 $altitude->{$coordinates} = $altitude->{$other};
149 0 last;
150 }
151 # if we didn't find one in the last six attempts, just make it hole in the ground
152 0 ▶ if (not defined $altitude->{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $altitude->{$coordinates}) 0 0 0
153 0 $altitude->{$coordinates} = 0;
154 }
155 }
156 }
157 }
158  
159 sub altitude {
160 4 ▶ my $self = shift;
Subroutine altitude: called CC=2 SCAR=6.9
Pod undocumented
161 4 my ($world, $altitude) = @_;
162 4 my @queue = $self->place_peak($altitude, $self->peaks, $self->peak);
163 4 $self->grow_mountains($altitude, @queue);
164 4 $self->fix_altitude($altitude);
165 # note height for debugging purposes
166 4 for my $coordinates (sort keys %$altitude) {
167 1200 $world->{$coordinates} = "height$altitude->{$coordinates}";
168 }
169 }
170  
171 sub bumpiness {
172 4 ▶ my ($self, $world, $altitude) = @_;
Subroutine bumpiness: called CC=12 SCAR=25.2
Pod undocumented
173 4 for (1 .. $self->bumps) {
174 28 for my $delta (-$self->bump, $self->bump) {
175 # six attempts to try and find a good hex
176 56 for (1 .. 6) {
177 58 my $x = int(rand($self->width)) + 1;
178 58 my $y = int(rand($self->height)) + 1;
179 58 my $coordinates = coordinates($x, $y);
180 58 my $current_altitude = $altitude->{$coordinates} + $delta;
181 58 ▶ next if $current_altitude > 10 or $current_altitude < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $current_altitude > 10 or $current_altitude < 0 2 56 58
Condition66%2/3
$current_altitude > 10 or $current_altitude < 0
l!l&&r!l&&!r
2056
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
182 # bump it up or down
183 56 $altitude->{$coordinates} = $current_altitude;
184 56 $world->{$coordinates} = "height$altitude->{$coordinates} zone";
185 56 $log->debug("bumped altitude of $coordinates by $delta to $current_altitude");
186 # if the bump was +2 or -2, bump the neighbours by +1 or -1
187 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
188 56 my $delta = $delta - $delta / abs($delta);
189 56 for my $i ($self->neighbors()) {
190 280 my ($x, $y) = $self->neighbor($coordinates, $i);
191 280 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 20 260 280
192 260 my $other = coordinates($x, $y);
193 260 $current_altitude = $altitude->{$other} + $delta;
194 260 ▶ next if $current_altitude > 10 or $current_altitude < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $current_altitude > 10 or $current_altitude < 0 0 260 260
Condition33%1/3
$current_altitude > 10 or $current_altitude < 0
l!l&&r!l&&!r
00260
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
195 260 $altitude->{$other} = $current_altitude;
196 260 $world->{$other} = "height$altitude->{$other} zone";
197 260 $log->debug("$i bumped altitude of $other by $delta to $current_altitude");
198 }
199 }
200 # if we have found a good hex, don't go through all the other attempts
201 56 last;
202 }
203 }
204 }
205 }
206  
207 sub water {
208 4 ▶ my $self = shift;
Subroutine water: called CC=23 SCAR=31.4
Pod undocumented
209 4 my ($world, $altitude, $water) = @_;
210 # reset in case we run this twice
211 # go through all the hexes
212 4 for my $coordinates (sort keys %$altitude) {
213 1200 ▶ next if $altitude->{$coordinates} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} <= $self->bottom 0 1200 1200
214 # note preferred water flow by identifying lower lying neighbors
215 1200 my ($lowest, $direction);
216 # look at neighbors in random order
217 NEIGHBOR:
218 1200 for my $i (shuffle $self->neighbors()) {
219 6000 my ($x, $y) = $self->neighbor($coordinates, $i);
220 6000 my $legal = $self->legal($x, $y);
221 6000 my $other = coordinates($x, $y);
222 6000 ▶ next if $legal and $altitude->{$other} > $altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $legal and $altitude->{$other} > $altitude->{$coordinates} 1560 4440 6000
Condition100%3/3
$legal and $altitude->{$other} > $altitude->{$coordinates}
!ll&&!rl&&r
47639641560
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
223 # don't point head on to another arrow
224 4440 ▶ 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 193 4247 4440
Condition100%6/6
$legal and $water->{$other} and $water->{$other} == ($i - 3) % 6
!ll&&!rl&&r
28981349193
$legal and $water->{$other}
!ll&&!rl&&r
47624221542
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
225 # don't point into loops
226 4247 my %loop = ($coordinates => 1, $other => 1);
227 4247 my $next = $other;
228 4247 $log->debug("Loop detection starting with $coordinates and $other");
229 4247 ▶ while ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 7777 0 7777
230 # no water flow known is also good;
231 7777 ▶ $log->debug("water for $next: " . ($water->{$next} || "none"));
Condition100%2/2
$water->{$next} || 'none'
l!l
32104567
MC/DC100%1/1
$water->{$next} || 'none'
$water->{$next}
Truth table100%2/2
A
A= $water->{$next}
Aresult
11
00
232 7777 ▶ last unless defined $water->{$next};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $water->{$next} 3733 4044 7777
233 4044 ($x, $y) = $self->neighbor($next, $water->{$next});
234 # leaving the map is good
235 4044 $log->debug("legal for $next: " . $self->legal($x, $y));
236 4044 ▶ last unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 371 3673 4044
237 3673 $next = coordinates($x, $y);
238 # skip this neighbor if this is a loop
239 3673 ▶ $log->debug("is $next in a loop? " . ($loop{$next} || "no"));
Condition100%2/2
$loop{$next} || 'no'
l!l
1433530
MC/DC100%1/1
$loop{$next} || 'no'
$loop{$next}
Truth table100%2/2
A
A= $loop{$next}
Aresult
11
00
240 3673 ▶ next NEIGHBOR if $loop{$next};
Branch: 1 branch
BranchTrueFalseTotal
if $loop{$next} 143 3530 3673
241 3530 $loop{$next} = 1;
242 }
243 4104 ▶ 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) 427 2516 2943
Condition83%10/12
not defined $direction or not $legal and $altitude->{$coordinates} < $lowest or $legal and $altitude->{$other} < $lowest
l!l&&r!l&&!r
11615062437
not defined $direction or not $legal and $altitude->{$coordinates} < $lowest
l!l&&r!l&&!r
116102943
not $legal and $altitude->{$coordinates} < $lowest
!ll&&!rl&&r
25943490
$legal and $altitude->{$other} < $lowest
!ll&&!rl&&r
3492167427
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 table37%3/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
244 or not $legal and $altitude->{$coordinates} < $lowest
245 or $legal and $altitude->{$other} < $lowest) {
246 1588 ▶ $lowest = $legal ? $altitude->{$other} : $altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
$legal ? : 1461 127 1588
247 1588 $direction = $i;
248 1588 $log->debug("Set lowest to $lowest ($direction)");
249 }
250 }
251 1200 ▶ if (defined $direction) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $direction) 1161 39 1200
252 1161 $water->{$coordinates} = $direction;
253 $world->{$coordinates} =~ s/arrow\d/arrow$water->{$coordinates}/
254 1161 ▶ 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}" 1161 0 1161
255 }
256 }
257 }
258  
259 sub mountains {
260 4 ▶ my $self = shift;
Subroutine mountains: called CC=5 SCAR=16.1
Pod undocumented
261 4 my ($world, $altitude) = @_;
262 # place the types
263 4 for my $coordinates (keys %$altitude) {
264 1200 ▶ if ($altitude->{$coordinates} >= 10) {
Branch: 3 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 10) { } 37 1163 1200
elsif ($altitude->{$coordinates} >= 9) { } 90 1073 1163
elsif ($altitude->{$coordinates} >= 8) { } 248 825 1073
265 37 $world->{$coordinates} = "white mountains";
266 } elsif ($altitude->{$coordinates} >= 9) {
267 90 $world->{$coordinates} = "white mountain";
268 } elsif ($altitude->{$coordinates} >= 8) {
269 248 $world->{$coordinates} = "light-grey mountain";
270 }
271 }
272 }
273  
274 sub ocean {
275 4 ▶ my $self = shift;
Subroutine ocean: called CC=7 SCAR=33.2
Pod undocumented
276 4 my ($world, $altitude) = @_;
277 4 for my $coordinates (sort keys %$altitude) {
278 1200 ▶ if ($altitude->{$coordinates} <= $self->bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} <= $self->bottom) 0 1200 1200
279 0 my $ocean = 1;
280 0 for my $i ($self->neighbors()) {
281 0 my ($x, $y) = $self->neighbor($coordinates, $i);
282 0 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 0 0 0
283 0 my $other = coordinates($x, $y);
284 0 ▶ next if $altitude->{$other} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} <= $self->bottom 0 0 0
285 0 $ocean = 0;
286 }
287 0 ▶ $world->{$coordinates} = $ocean ? "ocean" : "water";
Branch: 1 branch
BranchTrueFalseTotal
$ocean ? : 0 0 0
288 }
289 }
290 }
291  
292 sub lakes {
293 4 ▶ my $self = shift;
Subroutine lakes: called CC=4 SCAR=13.9
Pod undocumented
294 4 my ($world, $altitude, $water) = @_;
295 # any areas without water flow are lakes
296 4 for my $coordinates (sort keys %$altitude) {
297 1200 ▶ if (not defined $water->{$coordinates}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $water->{$coordinates} and $world->{$coordinates} ne "ocean") 39 1161 1200
Condition66%2/3
not defined $water->{$coordinates} and $world->{$coordinates} ne "ocean"
!ll&&!rl&&r
1161039
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
298 and $world->{$coordinates} ne "ocean") {
299 39 $world->{$coordinates} = "water";
300 }
301 }
302 }
303  
304 sub swamps {
305 # any area with water flowing to a neighbor at the same altitude is a swamp
306 4 ▶ my ($self, $world, $altitude, $water, $flow, $dry) = @_;
Subroutine swamps: called CC=8 SCAR=20.8
Pod undocumented
307 4 for my $coordinates (keys %$altitude) {
308 # don't turn lakes into swamps and skip bogs
309 1200 ▶ next if $world->{$coordinates} =~ /ocean|water|swamp|grass/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /ocean|water|swamp|grass/u 204 996 1200
310 # swamps require a river
311 996 ▶ next unless $flow->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
unless $flow->{$coordinates} 540 456 996
312 # no swamps when there is a canyon
313 456 ▶ next if $dry->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$coordinates} 62 394 456
314 # look at the neighbor the water would flow to
315 394 my ($x, $y) = $self->neighbor($coordinates, $water->{$coordinates});
316 # skip if water flows off the map
317 394 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 50 344 394
318 344 my $other = coordinates($x, $y);
319 # skip if water flows downhill
320 344 ▶ next if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 259 85 344
321 # if there was no lower neighbor, this is a swamp
322 85 ▶ if ($altitude->{$coordinates} >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 6) { } 48 37 85
323 48 $world->{$coordinates} =~ s/height\d+/grey swamp/;
324 } else {
325 37 $world->{$coordinates} =~ s/height\d+/dark-grey swamp/;
326 }
327 }
328 }
329  
330 sub flood {
331 4 ▶ my $self = shift;
Subroutine flood: called CC=20 SCAR=30.0
Pod undocumented
332 4 my ($world, $altitude, $water) = @_;
333 # backtracking information: $from = $flow{$to}
334 4 my %flow;
335 # allow easy skipping
336 my %seen;
337 # start with a list of hexes to look at; as always, keys is a source of
338 # randomness that's independent of srand which is why we shuffle sort
339 4 my @lakes = shuffle sort grep { not defined $water->{$_} } keys %$world;
340 4 ▶ return unless @lakes;
Branch: 1 branch
BranchTrueFalseTotal
unless @lakes 0 4 4
341 4 my $start = shift(@lakes);
342 4 my @candidates = ($start);
343 4 ▶ while (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates) 387 0 387
344 # Prefer candidates outside the map with altitude 0; reshuffle because
345 # candidates at the same height are all equal and early or late discoveries
346 # should not matter (not shuffling means it matters whether candidates are
347 # pushed or unshifted because this is a stable sort)
348 @candidates = sort {
349 387 ▶ ($altitude->{$a}||0) <=> ($altitude->{$b}||0)
Condition100%4/4
$altitude->{$a} || 0
l!l
3202899
$altitude->{$b} || 0
l!l
3203493
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
350 } shuffle @candidates;
351 387 $log->debug("Candidates @candidates");
352 387 my $coordinates;
353 do {
354 438 $coordinates = shift(@candidates);
355 387 ▶ } until not $coordinates or not $seen{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
not $coordinates && $seen{$coordinates} or do { do { $coordinates = shift @candidates } } 51 387 438
Condition66%2/3
$coordinates && $seen{$coordinates}
!ll&&!rl&&r
038751
MC/DC50%1/2
$coordinates && $seen{$coordinates}
$coordinates $seen{$coordinates}
Truth table66%2/3
A && B
A= $coordinatesB= $seen{$coordinates}
ABresult
0X0
100
111
356 387 ▶ last unless $coordinates;
Branch: 1 branch
BranchTrueFalseTotal
unless $coordinates 0 387 387
357 387 $seen{$coordinates} = 1;
358 387 $log->debug("Looking at $coordinates");
359 387 ▶ if ($self->legal($coordinates) and $world->{$coordinates} ne "ocean") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->legal($coordinates) and $world->{$coordinates} ne "ocean") 355 32 387
Condition66%2/3
$self->legal($coordinates) and $world->{$coordinates} ne "ocean"
!ll&&!rl&&r
320355
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
360 # if we're still on the map, check all the unknown neighbors
361 355 my $from = $coordinates;
362 355 for my $i ($self->neighbors()) {
363 1710 my $to = coordinates($self->neighbor($from, $i));
364 1710 ▶ next if $seen{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$to} 431 1279 1710
365 1279 $log->debug("Adding $to to our candidates");
366 1279 $flow{$to} = $from;
367 # adding to the front as we keep pushing forward (I hope)
368 1279 push(@candidates, $to);
369 }
370 355 next;
371 }
372 32 $log->debug("We left the map at $coordinates");
373 32 my $to = $coordinates;
374 32 my $from = $flow{$to};
375 32 ▶ while ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 228 32 260
376 228 my $i = $self->direction($from, $to);
377 228 ▶ if (not defined $water->{$from}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $water->{$from} or $water->{$from} != $i) { } 140 88 228
Condition100%3/3
not defined $water->{$from} or $water->{$from} != $i
l!l&&r!l&&!r
3910188
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
378 or $water->{$from} != $i) {
379 140 $log->debug("Arrow for $from now points to $to");
380 140 $water->{$from} = $i;
381 $world->{$from} =~ s/arrow\d/arrow$i/
382 140 ▶ or $world->{$from} .= " arrow$i";
Branch: 1 branch
BranchTrueFalseTotal
$world->{$from} =~ s/arrow\d/arrow$i/u or $world->{$from} .= " arrow$i" 39 101 140
383 } else {
384 88 $log->debug("Arrow for $from already points $to");
385 }
386 228 $to = $from;
387 228 $from = $flow{$to};
388 }
389 # pick the next lake
390 do {
391 39 $start = shift(@lakes);
392 39 ▶ $log->debug("Next lake is $start") if $start;
Branch: 1 branch
BranchTrueFalseTotal
if $start 35 4 39
393 32 ▶ } 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 } } 7 32 39
Condition100%3/3
$start && defined $water->{$start}
!ll&&!rl&&r
4287
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
394 32 ▶ last unless $start;
Branch: 1 branch
BranchTrueFalseTotal
unless $start 4 28 32
395 28 %seen = %flow = ();
396 28 @candidates = ($start);
397 }
398 }
399  
400 sub rivers {
401 4 ▶ my ($self, $world, $altitude, $water, $flow, $rivers) = @_;
Subroutine rivers: called CC=7 SCAR=19.5
Pod undocumented
402 # $flow are the sources points of rivers, or 1 if a river flows through them
403 my @growing = map {
404 242 ▶ $world->{$_} = "light-grey forest-hill" unless $world->{$_} =~ /mountain|swamp|grass|water|ocean/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$_} =~ /mountain|swamp|grass|water|ocean/u 95 147 242
405 242 $flow->{$_} = [$_]
406 } sort grep {
407 # these are the potential starting places: up in the mountains below the
408 # ice, or lakes
409 4 ($altitude->{$_} == 7 or $altitude->{$_} == 8
410 or $world->{$_} =~ /water/ and $altitude->{$_} > $self->bottom)
411 and not $flow->{$_}
412 1200 ▶ and $world->{$_} !~ /dry/;
Branch: 1 branch
BranchTrueFalseTotal
$altitude->{$_} == 7 || $altitude->{$_} == 8 || $world->{$_} =~ /water/u && $altitude->{$_} > $self->bottom and not $flow->{$_} and not $world->{$_} =~ /dry/u 655 545 1200
Condition83%10/12
$altitude->{$_} == 7 || $altitude->{$_} == 8 || $world->{$_} =~ /water/u && $altitude->{$_} > $self->bottom and not $flow->{$_}
!ll&&!rl&&r
5450655
$altitude->{$_} == 7 or $altitude->{$_} == 8 or $world->{$_} =~ /water/u and $altitude->{$_} > $self->bottom
l!l&&r!l&&!r
6275721
$altitude->{$_} == 7 or $altitude->{$_} == 8
l!l&&r!l&&!r
379248573
$world->{$_} =~ /water/u and $altitude->{$_} > $self->bottom
!ll&&!rl&&r
545028
MC/DC0%0/5
$altitude->{$_} == 7 || $altitude->{$_} == 8 || $world->{$_} =~ /water/u && $altitude->{$_} > $self->bottom and not $flow->{$_}
$altitude->{$_} == 7 $altitude->{$_} == 8 $world->{$_} =~ /water/u $altitude->{$_} > $self->bottom not $flow->{$_}
Truth table0%0/8
A or B or C and D and not(E)
A= $altitude->{$_} == 7B= $altitude->{$_} == 8C= $world->{$_} =~ /water/uD= $altitude->{$_} > $self->bottomE= not $flow->{$_}
ABCDEresult
000XX0
0010X0
1XXX00
01XX00
001100
1XXX11
01XX11
001111
413 } keys %$altitude;
414 4 $self->grow_rivers(\@growing, $water, $flow, $rivers);
415 }
416  
417 sub grow_rivers {
418 4 ▶ my ($self, $growing, $water, $flow, $rivers) = @_;
Subroutine grow_rivers: called CC=5 SCAR=16.1
Pod undocumented
419 4 ▶ while (@$growing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$growing) 1756 4 1760
420 # warn "Rivers: " . @growing . "\n";
421 # pick a random growing river and grow it
422 1756 my $n = int(rand(scalar @$growing));
423 1756 my $river = $growing->[$n];
424 # warn "Picking @$river\n";
425 1756 my $coordinates = $river->[-1];
426 1756 my $end = 1;
427 1756 ▶ if (defined $water->{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $water->{$coordinates}) { } 1544 212 1756
428 1544 my $other = coordinates($self->neighbor($coordinates, $water->{$coordinates}));
429 1544 ▶ 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 1544 1544
430 # if we flowed into a hex with a river
431 1544 ▶ if (ref $flow->{$other}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $flow->{$other}) { } 30 1514 1544
432 # warn "Prepending @$river to @{$flow->{$other}}\n";
433 # prepend the current river to the other river
434 30 unshift(@{$flow->{$other}}, @$river);
435 # move the source marker
436 30 $flow->{$river->[0]} = $flow->{$other};
437 30 $flow->{$other} = 1;
438 # and remove the current river from the growing list
439 30 splice(@$growing, $n, 1);
440 # warn "Flow at $river->[0]: @{$flow->{$river->[0]}}\n";
441 # warn "Flow at $other: $flow->{$other}\n";
442 } else {
443 1514 $flow->{$coordinates} = 1;
444 1514 push(@$river, $other);
445 }
446 } else {
447 # stop growing this river
448 # warn "Stopped river: @$river\n" if grep(/0914/, @$river);
449 212 push(@$rivers, splice(@$growing, $n, 1));
450 }
451 }
452 }
453  
454 sub canyons {
455 4 ▶ my $self = shift;
Subroutine canyons: called CC=14 SCAR=27.5
Pod undocumented
456 4 my ($world, $altitude, $rivers, $canyons, $dry) = @_;
457 # using a reference to an array so that we can leave pointers in the %seen hash
458 4 my $canyon = [];
459 # remember which canyon flows through which hex
460 4 my %seen;
461 4 for my $river (@$rivers) {
462 212 my $last = $river->[0];
463 212 my $current_altitude = $altitude->{$last};
464 212 $log->debug("Looking at @$river ($current_altitude)");
465 212 for my $coordinates (@$river) {
466 1228 $log->debug("Looking at $coordinates");
467 1228 ▶ if ($seen{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$coordinates}) 71 1157 1228
468 # the rest of this river was already looked at, so there is no need to
469 # do the rest of this river; if we're in a canyon, prepend it to the one
470 # we just found before ending
471 71 ▶ if (@$canyon) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$canyon) 1 70 71
472 1 my @other = @{$seen{$coordinates}};
473 1 ▶ if ($other[0] eq $canyon->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other[0] eq $canyon->[-1]) { } 1 0 1
474 1 $log->debug("Canyon @$canyon of river @$river merging with @other at $coordinates");
475 1 unshift(@{$seen{$coordinates}}, @$canyon[0 .. @$canyon - 2]);
476 } else {
477 0 $log->debug("Canyon @$canyon of river @$river stumbled upon existing canyon @other at $coordinates");
478 0 ▶ while (@other) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other) 0 0 0
479 0 my $other = shift(@other);
480 0 ▶ next if $other ne $coordinates;
Branch: 1 branch
BranchTrueFalseTotal
if $other ne $coordinates 0 0 0
481 0 push(@$canyon, $other, @other);
482 0 last;
483 }
484 0 $log->debug("Canyon @$canyon");
485 0 push(@$canyons, $canyon);
486 }
487 1 $canyon = [];
488 }
489 71 $log->debug("We've seen the rest: @{$seen{$coordinates}}");
490 71 last;
491 }
492 # no canyons through water!
493 1157 ▶ 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) { } 70 1087 1157
Condition100%6/6
$altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates} and not $world->{$coordinates} =~ /water|ocean/u
!ll&&!rl&&r
1086170
$altitude->{$coordinates} and $current_altitude < $altitude->{$coordinates}
!ll&&!rl&&r
12696071
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
494 and $world->{$coordinates} !~ /water|ocean/) {
495 # river is digging a canyon; if this not the start of the river and it
496 # is the start of a canyon, prepend the last step
497 70 ▶ push(@$canyon, $last) unless @$canyon;
Branch: 1 branch
BranchTrueFalseTotal
unless @$canyon 25 45 70
498 70 push(@$canyon, $coordinates);
499 70 ▶ $world->{$coordinates} .= " zone" unless $dry->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
unless $dry->{$coordinates} 70 0 70
500 70 $dry->{$coordinates} = 1;
501 70 $log->debug("Growing canyon @$canyon");
502 70 $seen{$coordinates} = $canyon;
503 } else {
504 # if we just left a canyon, append the current step
505 1087 ▶ if (@$canyon) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$canyon) 24 1063 1087
506 24 push(@$canyon, $coordinates);
507 24 push(@$canyons, $canyon);
508 24 $log->debug("Looking at river @$river");
509 24 $log->debug("Canyon @$canyon");
510 24 $canyon = [];
511 24 last;
512 }
513 # not digging a canyon
514 1063 $last = $coordinates;
515 1063 $current_altitude = $altitude->{$coordinates};
516 }
517 }
518 }
519 }
520  
521 sub wet {
522 0 ▶ my $self = shift;
Subroutine wet: not called CC=5 SCAR=34.0
Pod undocumented
523 # a hex is wet if there is a river, a swamp or a forest within 2 hexes
524 0 my ($coordinates, $world, $flow) = @_;
525 0 for my $i ($self->neighbors()) {
526 0 my ($x, $y) = $self->neighbor($coordinates, $i);
527 0 my $other = coordinates($x, $y);
528 0 ▶ return 0 if $flow->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $flow->{$other} 0 0 0
529 }
530 0 for my $i ($self->neighbors2()) {
531 0 my ($x, $y) = $self->neighbor2($coordinates, $i);
532 0 my $other = coordinates($x, $y);
533 0 ▶ return 0 if $flow->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $flow->{$other} 0 0 0
534 }
535 0 return 1;
536 }
537  
538 sub grow_forest {
539 490 ▶ my ($self, $coordinates, $world, $altitude, $dry) = @_;
Subroutine grow_forest: called CC=25 SCAR=32.2
Pod undocumented
540 490 my @candidates;
541 490 ▶ 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 105 385 490
542 490 my $n = $self->arid;
543 # fractions are allowed
544 490 ▶ $n += 1 if rand() < $self->arid - int($self->arid);
Branch: 1 branch
BranchTrueFalseTotal
if rand() < $self->arid - int($self->arid) 0 490 490
545 490 $n = int($n);
546 490 $log->debug("Arid: $n");
547 490 ▶ if ($n >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1) 490 0 490
548 490 for my $i ($self->neighbors()) {
549 2412 my ($x, $y) = $self->neighbor($coordinates, $i);
550 2412 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 185 2227 2412
551 2227 my $other = coordinates($x, $y);
552 2227 ▶ next if $dry->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$other} 136 2091 2227
553 2091 ▶ next if $altitude->{$coordinates} < $altitude->{$other}; # distance of one unless higher
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < $altitude->{$other} 719 1372 2091
554 1372 ▶ 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 459 913 1372
555 }
556 }
557 490 ▶ if ($n >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 2) 490 0 490
558 490 for my $i ($self->neighbors2()) {
559 4824 my ($x, $y) = $self->neighbor2($coordinates, $i);
560 4824 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 695 4129 4824
561 4129 my $other = coordinates($x, $y);
562 4129 ▶ next if $altitude->{$coordinates} <= $altitude->{$other}; # distance of two only if lower
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} <= $altitude->{$other} 3060 1069 4129
563 1069 my $ok = 0;
564 1069 for my $m ($self->neighbors()) {
565 3357 my ($mx, $my) = $self->neighbor($coordinates, $m);
566 3357 ▶ next unless $self->legal($mx, $my);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($mx, $my) 114 3243 3357
567 3243 my $midway = coordinates($mx, $my);
568 3243 ▶ next if $dry->{$midway};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$midway} 239 3004 3243
569 3004 ▶ next if $self->distance($midway, $other) != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->distance($midway, $other) != 1 1938 1066 3004
570 1066 ▶ next if $altitude->{$coordinates} < $altitude->{$midway};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < $altitude->{$midway} 74 992 1066
571 992 ▶ next if $altitude->{$midway} < $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$midway} < $altitude->{$other} 54 938 992
572 938 $ok = 1;
573 938 last;
574 }
575 1069 ▶ next unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 131 938 1069
576 938 ▶ 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 415 523 938
577 }
578 }
579 490 $log->debug("forest growth: $coordinates: @candidates");
580 490 for $coordinates (@candidates) {
581 979 ▶ if ($altitude->{$coordinates} >= 7) {
Branch: 3 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 7) { } 224 755 979
elsif ($altitude->{$coordinates} >= 6) { } 464 291 755
elsif ($altitude->{$coordinates} >= 4) { } 263 28 291
582 224 $world->{$coordinates} = "light-green fir-forest";
583 } elsif ($altitude->{$coordinates} >= 6) {
584 464 $world->{$coordinates} = "green fir-forest";
585 } elsif ($altitude->{$coordinates} >= 4) {
586 263 $world->{$coordinates} = "green forest";
587 } else {
588 28 $world->{$coordinates} = "dark-green forest";
589 }
590 }
591 }
592  
593 sub forests {
594 4 ▶ my ($self, $world, $altitude, $flow, $dry) = @_;
Subroutine forests: called CC=3 SCAR=11.0
Pod undocumented
595 # Empty hexes with a river flowing through them (and nearby hexes) are forest
596 # filled valleys.
597 4 for my $coordinates (keys %$flow) {
598 560 ▶ next if $dry->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $dry->{$coordinates} 70 490 560
599 490 $self->grow_forest($coordinates, $world, $altitude, $dry);
600 }
601 }
602  
603 sub winds {
604 4 ▶ my $self = shift;
Subroutine winds: called CC=7 SCAR=19.5
Pod undocumented
605 4 my ($world, $altitude, $water, $flow) = @_;
606 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
607 4 $world->{"0101"} .= " wind" . $self->reverse($wind);
608 4 for my $coordinates (keys %$altitude) {
609 # limit ourselves to altitude 7 and 8
610 1200 ▶ next if $altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8 573 627 1200
Condition100%3/3
$altitude->{$coordinates} < 7 or $altitude->{$coordinates} > 8
l!l&&r!l&&!r
446127627
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
611 # look at the neighbor the water would flow to
612 627 my ($x, $y) = $self->neighbor($coordinates, $wind);
613 # skip if off the map
614 627 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 24 603 627
615 603 my $other = coordinates($x, $y);
616 # skip if the other hex is lower
617 603 ▶ next if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 190 413 603
618 # if the other hex was higher, this land is dry
619 413 $log->debug("$coordinates is dry because of $other");
620 413 $world->{$coordinates} .= " dry zone"; # use label for debugging
621 }
622 }
623  
624 sub bogs {
625 4 ▶ my $self = shift;
Subroutine bogs: called CC=6 SCAR=17.9
Pod undocumented
626 4 my ($world, $altitude, $water) = @_;
627 4 for my $coordinates (keys %$altitude) {
628 # limit ourselves to altitude 7
629 1200 ▶ next if $altitude->{$coordinates} != 7;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} != 7 821 379 1200
630 # don't turn lakes into bogs
631 379 ▶ next if $world->{$coordinates} =~ /water|ocean/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /water|ocean/u 11 368 379
632 # look at the neighbor the water would flow to
633 368 my ($x, $y) = $self->neighbor($coordinates, $water->{$coordinates});
634 # skip if water flows off the map
635 368 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 21 347 368
636 347 my $other = coordinates($x, $y);
637 # skip if water flows downhill
638 347 ▶ next if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 182 165 347
639 # if there was no lower neighbor, this is a bog
640 165 $world->{$coordinates} =~ s/height\d+/grey swamp/;
641 }
642 }
643  
644 sub dry {
645 4 ▶ my ($self, $world, $altitude, $rivers) = @_;
Subroutine dry: called CC=16 SCAR=27.8
Pod undocumented
646 4 my @dry;
647 4 for my $coordinates (shuffle sort keys %$world) {
648 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) 118 1082 1200
649 118 ▶ if ($altitude->{$coordinates} >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 7) { } 31 87 118
650 31 $world->{$coordinates} = "light-grey grass";
651 } else {
652 87 $world->{$coordinates} = "light-green bushes";
653 87 push(@dry, $coordinates);
654 }
655 }
656 }
657  
658 BUSHES:
659 4 for my $coordinates (@dry) {
660 87 for my $i ($self->neighbors()) {
661 284 my ($x, $y) = $self->neighbor($coordinates, $i);
662 284 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 51 233 284
663 233 my $other = coordinates($x, $y);
664 233 ▶ next BUSHES if $world->{$other} =~ /forest|firs|trees|swamp/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$other} =~ /forest|firs|trees|swamp/u 62 171 233
665 }
666 25 ▶ if ($altitude->{$coordinates} >= 5) {
Branch: 2 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 5) { } 17 8 25
elsif ($altitude->{$coordinates} >= 3) { } 8 0 8
667 17 $world->{$coordinates} =~ s/light-green bushes/light-grey grass/;
668 } elsif ($altitude->{$coordinates} >= 3) {
669 8 $world->{$coordinates} =~ s/light-green bushes/grey grass/;
670 } else {
671 0 $world->{$coordinates} =~ s/light-green bushes/dark-grey grass/;
672 }
673 }
674  
675 GRASS:
676 4 for my $coordinates (@dry) {
677 87 ▶ next if $self->with_river($rivers, $coordinates);
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_river($rivers, $coordinates) 15 72 87
678 72 for my $i ($self->neighbors()) {
679 159 my ($x, $y) = $self->neighbor($coordinates, $i);
680 159 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 39 120 159
681 120 my $other = coordinates($x, $y);
682 120 ▶ next GRASS if $world->{$other} !~ /grass|desert|water/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$other} =~ /grass|desert|water/u 64 56 120
683 }
684 8 ▶ if ($altitude->{$coordinates} >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} >= 3) { } 8 0 8
685 8 $world->{$coordinates} =~ s/(light-|dark-)?grey grass/light-grey desert/;
686 } else {
687 0 $world->{$coordinates} =~ s/(light-|dark-)?grey grass/dust desert/;
688 }
689 }
690 }
691  
692 sub with_river {
693 87 ▶ my ($self, $rivers, $coordinates) = @_;
Subroutine with_river: called CC=3 SCAR=11.0
Pod undocumented
694 87 for my $river (@$rivers) {
695 3965 ▶ return 1 if grep { $coordinates eq $_ } (@$river);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$coordinates eq $_;} @$river 15 3950 3965
696 }
697 }
698  
699 sub settlements {
700 8 ▶ my $self = shift;
Subroutine settlements: called CC=21 SCAR=30.5
Pod undocumented
701 8 my ($world, $flow) = @_;
702 8 my @settlements;
703 8 my $max = $self->height * $self->width;
704 # do not match forest-hill
705 8 my @candidates = shuffle sort grep { $world->{$_} =~ /\b(fir-forest|forest(?!-hill))\b/ } keys %$world;
706 8 @candidates = $self->remove_closer_than(2, @candidates);
707 8 ▶ @candidates = @candidates[0 .. int($max/10 - 1)] if @candidates > $max/10;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 10 4 4 8
708 8 push(@settlements, @candidates);
709 8 for my $coordinates (@candidates) {
710 $world->{$coordinates} =~ s/fir-forest/firs thorp/
711 147 ▶ 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 63 84 147
712 }
713 8 ▶ @candidates = shuffle sort grep { $world->{$_} =~ /(?<!fir-)forest(?!-hill)/ and $flow->{$_}} keys %$world;
Branch: 1 branch
BranchTrueFalseTotal
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u and $flow->{$_} 85 3515 3600
714 8 @candidates = $self->remove_closer_than(5, @candidates);
715 8 ▶ @candidates = @candidates[0 .. int($max/20 - 1)] if @candidates > $max/20;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 20 0 8 8
716 8 push(@settlements, @candidates);
717 8 for my $coordinates (@candidates) {
718 16 $world->{$coordinates} =~ s/forest/trees village/;
719 }
720 8 ▶ @candidates = shuffle sort grep { $world->{$_} =~ /(?<!fir-)forest(?!-hill)/ and $flow->{$_} } keys %$world;
Branch: 1 branch
BranchTrueFalseTotal
$world->{$_} =~ /(?<!fir-)forest(?!-hill)/u and $flow->{$_} 69 3531 3600
721 8 @candidates = $self->remove_closer_than(10, @candidates);
722 8 ▶ @candidates = @candidates[0 .. int($max/40 - 1)] if @candidates > $max/40;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 40 0 8 8
723 8 push(@settlements, @candidates);
724 8 for my $coordinates (@candidates) {
725 7 $world->{$coordinates} =~ s/forest/trees town/;
726 }
727 8 @candidates = shuffle sort grep { $world->{$_} =~ /white mountain\b/ } keys %$world;
728 8 @candidates = $self->remove_closer_than(10, @candidates);
729 8 ▶ @candidates = @candidates[0 .. int($max/40 - 1)] if @candidates > $max/40;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 40 0 8 8
730 8 push(@settlements, @candidates);
731 8 for my $coordinates (@candidates) {
732 11 $world->{$coordinates} =~ s/white mountain\b/white mountain law/;
733 }
734 8 @candidates = shuffle sort grep { $world->{$_} =~ /swamp/ } keys %$world;
735 8 @candidates = $self->remove_closer_than(10, @candidates);
736 8 ▶ @candidates = @candidates[0 .. int($max/40 - 1)] if @candidates > $max/40;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates > $max / 40 0 8 8
737 8 push(@settlements, @candidates);
738 8 for my $coordinates (@candidates) {
739 18 $world->{$coordinates} =~ s/swamp/swamp2 chaos/;
740 }
741 8 for my $coordinates (@settlements) {
742 199 for my $i ($self->neighbors()) {
743 871 my ($x, $y) = $self->neighbor($coordinates, $i);
744 871 my $other = coordinates($x, $y);
745 871 ▶ next unless $world->{$other} and $world->{$other} =~ /water|ocean/;
Branch: 1 branch
BranchTrueFalseTotal
unless $world->{$other} and $world->{$other} =~ /water|ocean/u 819 52 871
Condition100%3/3
$world->{$other} and $world->{$other} =~ /water|ocean/u
!ll&&!rl&&r
9172852
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
746 # bump ports one size category up
747 52 $world->{$coordinates} =~ s/large-town/city port/;
748 52 $world->{$coordinates} =~ s/town/large-town port/;
749 52 $world->{$coordinates} =~ s/village/town port/;
750 # no bumps for thorps
751 52 last;
752 }
753 }
754 8 for my $coordinates (@settlements) {
755 # thorps and villages don't cut enough wood; make sure to get both "green" and "dark-green"
756 199 ▶ $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 2 197 199
757 199 ▶ $world->{$coordinates} =~ s/\S*green trees/soil/ if $world->{$coordinates} =~ / town/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ / town/u 10 189 199
758 }
759 8 return @settlements;
760 }
761  
762 sub trails {
763 8 ▶ my $self = shift;
Subroutine trails: called CC=11 SCAR=24.0
Pod undocumented
764 8 my ($altitude, $settlements) = @_;
765 # look for a neighbor that is as low as possible and nearby
766 8 my %trails;
767 8 my @from = shuffle @$settlements;
768 8 my @to = shuffle @$settlements;
769 8 for my $from (@from) {
770 199 my ($best, $best_distance, $best_altitude);
771 199 for my $to (@to) {
772 7761 ▶ next if $from eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $to 199 7562 7761
773 7562 my $distance = $self->distance($from, $to);
774 7562 $log->debug("Considering $from-$to: distance $distance, altitude " . $altitude->{$to});
775 7562 ▶ if ($distance <= 3
Branch: 1 branch
BranchTrueFalseTotal
if ($distance <= 3 and !$best_distance || $distance <= $best_distance and !$best || $altitude->{$to} < $best_altitude) 259 7303 7562
Condition100%12/12
$distance <= 3 and !$best_distance || $distance <= $best_distance and !$best || $altitude->{$to} < $best_altitude
!ll&&!rl&&r
7046387129
$distance <= 3 and !$best_distance || $distance <= $best_distance
!ll&&!rl&&r
6870176516
not $best_distance or $distance <= $best_distance
l!l&&r!l&&!r
185331176
not $best or $altitude->{$to} < $best_altitude
l!l&&r!l&&!r
18574257
MC/DC60%3/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 table50%4/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
776 and (not $best_distance or $distance <= $best_distance)
777 and (not $best or $altitude->{$to} < $best_altitude)) {
778 259 $best = $to;
779 259 $best_altitude = $altitude->{$best};
780 259 $best_distance = $distance;
781 }
782 }
783 199 ▶ next if not $best;
Branch: 1 branch
BranchTrueFalseTotal
unless $best 14 185 199
784 # skip if it already exists in the other direction
785 185 ▶ next if $trails{"$best-$from"};
Branch: 1 branch
BranchTrueFalseTotal
if $trails{"$best-$from"} 33 152 185
786 152 $trails{"$from-$best"} = 1;
787 152 $log->debug("Trail $from-$best");
788 }
789 8 return keys %trails;
790 }
791  
792 sub cliffs {
793 4 ▶ my $self = shift;
Subroutine cliffs: called CC=7 SCAR=19.5
Pod undocumented
794 4 my ($world, $altitude) = @_;
795 4 my @neighbors = $self->neighbors();
796 # hexes with altitude difference bigger than 1 have cliffs
797 4 for my $coordinates (keys %$world) {
798 1200 ▶ next if $altitude->{$coordinates} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} <= $self->bottom 0 1200 1200
799 1200 for my $i (@neighbors) {
800 6000 my ($x, $y) = $self->neighbor($coordinates, $i);
801 6000 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 476 5524 6000
802 5524 my $other = coordinates($x, $y);
803 5524 ▶ if ($altitude->{$coordinates} - $altitude->{$other} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} - $altitude->{$other} >= 2) 331 5193 5524
804 331 ▶ if (@neighbors == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@neighbors == 6) { } 207 124 331
805 207 $world->{$coordinates} .= " cliff$i";
806 } else { # square
807 124 $world->{$coordinates} .= " cliffs$i";
808 }
809 }
810 }
811 }
812 }
813  
814 sub marshlands {
815 4 ▶ my ($self, $world, $altitude, $rivers) = @_;
Subroutine marshlands: called CC=9 SCAR=29.0
Pod undocumented
816 4 my %seen;
817 4 for my $river (@$rivers) {
818 212 my $last = $river->[0];
819 212 for my $coordinates (@$river) {
820 777 ▶ last if $seen{$coordinates}; # we've been here before
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$coordinates} 153 624 777
821 624 $seen{$coordinates} = 1;
822 624 ▶ next unless exists $altitude->{$coordinates}; # rivers ending off the map
Branch: 1 branch
BranchTrueFalseTotal
unless exists $altitude->{$coordinates} 59 565 624
823 565 ▶ if ($altitude->{$coordinates} <= $self->bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} <= $self->bottom) 0 565 565
824 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
825 and $world->{$coordinates} =~ /water|ocean/
826 and $altitude->{$coordinates} == $altitude->{$last} - 1) {
827 0 $world->{$coordinates} = "blue-green swamp";
828 } else {
829 0 $world->{$coordinates} =~ s/ocean/water/;
830 0 delete $seen{$coordinates};
831 0 last;
832 }
833 }
834 565 $last = $coordinates;
835 }
836 }
837 }
838  
839 sub desertification {
840 4 ▶ my ($self, $world, $altitude, $rivers) = @_;
Subroutine desertification: called CC=15 SCAR=52.8
Pod undocumented
841 4 ▶ return unless $self->climate eq 'desert';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->climate eq "desert" 4 0 4
842 0 for my $coordinates (keys %$world) {
843 0 ▶ if ($self->with_river($rivers, $coordinates)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->with_river($rivers, $coordinates)) { } 0 0 0
844 $world->{$coordinates} =~ s/light-grey/light-green/
845 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
846 } else {
847 $world->{$coordinates} =~ s/light-green bushes/rock bushes/
848 or $world->{$coordinates} =~ s/light-grey grass/rock bush/
849 or $world->{$coordinates} =~ s/dark-grey grass/dark-soil bush/
850 or $world->{$coordinates} =~ s/^grey grass/rock bush/
851 or $altitude->{$coordinates} >= 4 and $world->{$coordinates} =~ s/light-grey desert/dark-soil desert/
852 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
853 }
854 $world->{$coordinates} =~ s/dark-grey swamp2?/light-green bushes/
855 or $world->{$coordinates} =~ s/^grey swamp2?/light-grey bushes/
856 or $world->{$coordinates} =~ s/fir-forest/trees/
857 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
858 }
859 }
860  
861 sub generate {
862 4 ▶ my ($self, $world, $altitude, $water, $rivers, $settlements, $trails, $canyons, $step) = @_;
Subroutine generate: called CC=3 SCAR=11.0
Pod undocumented
863 # $flow indicates that there is actually a river in this hex
864 4 my $flow = {};
865 # $dry indicates that is a river in this hex, but it cut itself a canyon
866 4 my $dry = {};
867 my @code = (
868 4 ▶ sub { $self->flat($altitude);
Subroutine __ANON__: called CC=1 SCAR=0.0
869 4 $self->altitude($world, $altitude); },
870 4 ▶ sub { $self->bumpiness($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
871 4 ▶ sub { $self->mountains($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
872 4 ▶ sub { $self->ocean($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
873 4 ▶ sub { $self->water($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
874 4 ▶ sub { $self->lakes($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
875 4 ▶ sub { $self->flood($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
876 4 ▶ sub { $self->bogs($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
877 4 ▶ sub { $self->winds($world, $altitude, $water); },
Subroutine __ANON__: called CC=1 SCAR=0.0
878 4 ▶ sub { $self->rivers($world, $altitude, $water, $flow, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
879 4 ▶ sub { $self->canyons($world, $altitude, $rivers, $canyons, $dry); },
Subroutine __ANON__: called CC=1 SCAR=0.0
880 4 ▶ sub { $self->swamps($world, $altitude, $water, $flow, $dry); },
Subroutine __ANON__: called CC=1 SCAR=0.0
881 4 ▶ sub { $self->forests($world, $altitude, $flow, $dry); },
Subroutine __ANON__: called CC=1 SCAR=0.0
882 4 ▶ sub { $self->dry($world, $altitude, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
883 4 ▶ sub { $self->cliffs($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
884 4 ▶ sub { push(@$settlements, $self->settlements($world, $flow)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
885 4 ▶ sub { push(@$trails, $self->trails($altitude, $settlements)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
886 4 ▶ sub { $self->marshlands($world, $altitude, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
887 4 ▶ sub { $self->desertification($world, $altitude, $rivers); },
Subroutine __ANON__: called CC=1 SCAR=0.0
888 # make sure you look at "alpine_document.html.ep" if you change this list!
889 # make sure you look at '/alpine/document' if you add to this list!
890 4 );
891  
892 # $step 0 runs all the code; note that we can't simply cache those results
893 # because we need to start over with the same seed!
894 4 my $i = 1;
895 4 ▶ while (@code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@code) 76 4 80
896 76 shift(@code)->();
897 76 ▶ return if $step == $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $step == $i++ 0 76 76
898 76 $self->fixup($world, $altitude, $i);
899 }
900 }
901  
902 # Remove temporary markers that won't be needed in the next step
903 sub fixup {
904 76 ▶ my ($self, $world, $altitude, $step, $last) = @_;
Subroutine fixup: called CC=4 SCAR=13.9
Pod undocumented
905 # When documenting or debugging, water flow arrows are no longer needed when
906 # the rivers are added.
907 76 ▶ if ($step >= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step >= 10) 44 32 76
908 44 for my $coordinates (keys %$world) {
909 13200 $world->{$coordinates} =~ s/ arrow\d//;
910 }
911 }
912 # Wind direction is only shown once.
913 76 $world->{"0101"} =~ s/ wind\d//;
914 # Remove zone markers.
915 76 for my $coordinates (keys %$world) {
916 22800 $world->{$coordinates} =~ s/ zone//;
917 }
918 }
919  
920 sub generate_map {
921 4 ▶ my $self = shift;
Subroutine generate_map: called CC=18 SCAR=32.4
Pod undocumented
922  
923 # The parameters turn into class variables.
924 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
925 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
926 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
927 4 ▶ $self->peaks(shift // int($self->width * $self->height / 40));
Condition33%1/3
shift() // int $self->width * $self->height / 40
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() // int $self->width * $self->height / 40
shift() int $self->width * $self->height / 40
Truth table33%1/3
A // B
A= shift()B= int $self->width * $self->height / 40
ABresult
1X1
011
000
928 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
929 4 ▶ $self->bumps(shift // int($self->width * $self->height / 40));
Condition33%1/3
shift() // int $self->width * $self->height / 40
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() // int $self->width * $self->height / 40
shift() int $self->width * $self->height / 40
Truth table33%1/3
A // B
A= shift()B= int $self->width * $self->height / 40
ABresult
1X1
011
000
930 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
931 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
932 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
933 4 ▶ $self->climate(shift ? 'desert' : 'temperate');
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 4 4
934 4 $self->wind(shift); # or random
935 4 ▶ my $seed = shift||time;
Condition66%2/3
shift() || time
l!l&&r!l&&!r
220
MC/DC0%0/2
shift() || time
shift() time
Truth table66%2/3
A || B
A= shift()B= time
ABresult
1X1
011
000
936 4 my $url = shift;
937 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
938  
939 # For documentation purposes, I want to be able to set the pseudo-random
940 # number seed using srand and rely on rand to reproduce the same sequence of
941 # pseudo-random numbers for the same seed. The key point to remember is that
942 # the keys function will return keys in random order. So if we look over the
943 # result of keys, we need to look at the code in the loop: If order is
944 # important, that wont do. We need to sort the keys. If we want the keys to be
945 # pseudo-shuffled, use shuffle sort keys.
946 4 srand($seed);
947  
948 # Keys for all hashes are coordinates such as "0101".
949 # %world is the description with values such as "green forest".
950 # %altitude is the altitude with values such as 3.
951 # %water is the preferred direction water would take with values such as 0.
952 # (north west); 0 means we need to use "if defined".
953 # @rivers are the rivers with values such as ["0102", "0202"].
954 # @settlements are are the locations of settlements such as "0101".
955 # @trails are the trails connecting these with values as "0102-0202".
956 # $step is how far we want map generation to go where 0 means all the way.
957 4 my ($world, $altitude, $water, $rivers, $settlements, $trails, $canyons) =
958 ({}, {}, {}, [], [], [], []);
959 4 $self->generate($world, $altitude, $water, $rivers, $settlements, $trails, $canyons, $step);
960  
961 # When documenting or debugging, add altitude as a label.
962 4 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 4 4
963 0 for my $coordinates (keys %$world) {
964 0 $world->{$coordinates} .= ' "' . $altitude->{$coordinates} . '"';
965 }
966 }
967  
968 4 local $" = "-"; # list items separated by -
969 4 my @lines;
970 4 push(@lines, map { $_ . " " . $world->{$_} } sort keys %$world);
971 4 push(@lines, map { "$_ trail" } @$trails);
972 4 push(@lines, map { "@$_ river" } @$rivers);
973 4 push(@lines, map { "@$_ canyon" } @$canyons); # after rivers
974 4 push(@lines, "include gnomeyland.txt");
975  
976 # when documenting or debugging, add some more lines at the end
977 4 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 4 4
978 # visualize height
979 push(@lines,
980 map {
981 0 my $n = int(25.5 * $_);
982 0 qq{height$_ attributes fill="rgb($n,$n,$n)"};
983 } (0 .. 10));
984 # visualize water flow
985 0 push(@lines, $self->arrows());
986 }
987  
988 4 push(@lines, "# Seed: $seed");
989 4 ▶ push(@lines, "# Documentation: " . $url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 2 2 4
990 4 my $map = join("\n", @lines);
991 4 ▶ return $map, $self if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 2 4
992 2 return $map;
993 }
994  
995 11 1;
« blib/lib/Game/TextMapper/Command/random.pm ↑ Summary blib/lib/Game/TextMapper/Smale.pm »