blib/lib/Game/TextMapper/Folkesten.pm

filter: Statementstmt 83.5% 239 / 286 Branchbran 70.4% 86 / 122 Conditioncond 95.5% 43 / 45 MC/DCmcdc 79.1% 19 / 24 Subroutinesub 93.9% 31 / 33 Podpod 88.2% 15 / 17 totaltotal 82.1% 433 / 527 CC 103 SCAR 27.3
CC
103
Cov
81%
generate_canyons
39.6
generate_ocean
27.9
generate_forest
16.0
CRAP 15.3
« blib/lib/Game/TextMapper/Smale.pm ↑ Summary blib/lib/Game/TextMapper/Schroeder/Island.pm »
1 # Copyright (C) 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::Folkesten - generate fantasy wilderness maps
21  
22 =head1 SYNOPSIS
23  
24 my $text = Game::TextMapper::Folkesten->new
25 ->generate_map();
26  
27 =head1 DESCRIPTION
28  
29 This generates a wilderness map based on the algorithm by Andreas Folkesten. See the
30 blog posts at L<http://arch-brick.blogspot.com/2023/08/hexmap-terrain-generator.html>.
31  
32 =head1 METHODS
33  
34 Note that this module acts as a class with the C<generate_map> method, but none
35 of the other subroutines defined are actual methods. They don't take a C<$self>
36 argument.
37  
38 =cut
39  
40 package Game::TextMapper::Folkesten;
41 11 ▶use Game::TextMapper::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 11 ▶use Game::TextMapper::Point;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 11 ▶use List::Util qw(shuffle any first);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 ▶has 'world' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
48 1 ▶has 'dry' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 ▶has 'wet' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
50 11 has 'width' => 10;
51 11 has 'height' => 10;
52 1 ▶has 'rivers' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 ▶has 'canyons' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
54 has 'altitude' => sub {
55 {
56 1 ▶ 'mountain' => 3,
Subroutine __ANON__: called CC=1 SCAR=0.0
57 'forest-hill' => 2,
58 'green-hills' => 2,
59 'hills' => 2,
60 'plain' => 1,
61 'water' => 0,
62 'ocean' => 0,
63 }
64 11 };
65  
66 11 *coord = \&Game::TextMapper::Point::coord;
67  
68 11 my $log = Game::TextMapper::Log->get;
69  
70 =head2 neighbors
71  
72 The list of directions for neighbours one step away (0 to 5).
73  
74 =cut
75  
76 247 ▶sub neighbors { 0 .. 5 }
Subroutine neighbors: called CC=1 SCAR=0.0
Pod documented
77  
78 =head2 random_neighbor
79  
80 A random direction for a neighbour one step away (a random integer from 0 to 5).
81  
82 =cut
83  
84 0 ▶sub random_neighbor { int(rand(6)) }
Subroutine random_neighbor: not called CC=1 SCAR=6.9
Pod documented
85  
86 =head2 neighbor($hex, $i)
87  
88 say join(",", $map->neighbor("0203", 1));
89 # 2,2
90  
91 Returns the coordinates of a neighbor in a particular direction (0 to 5), one
92 step away.
93  
94 C<$hex> is an array reference of coordinates or a string that can be turned into
95 one using the C<xy> method.
96  
97 C<$i> is a direction (0 to 5).
98  
99 =cut
100  
101 sub neighbor {
102 1482 ▶ my $self = shift;
Subroutine neighbor: called CC=3 SCAR=11.2
Pod documented
103 # $hex is [x,y] or "0x0y" and $i is a number 0 .. 5
104 1482 my ($hex, $i) = @_;
105 1482 ▶ die join(":", caller) . ": undefined direction for $hex\n" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 1482 1482
106 1482 ▶ $hex = [$self->xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 1482 0 1482
107 1482 my $delta_hex = [
108 # x is even
109 [[-1, 0], [ 0, -1], [+1, 0], [+1, +1], [ 0, +1], [-1, +1]],
110 # x is odd
111 [[-1, -1], [ 0, -1], [+1, -1], [+1, 0], [ 0, +1], [-1, 0]]];
112 1482 return ($hex->[0] + $delta_hex->[$hex->[0] % 2]->[$i]->[0],
113 $hex->[1] + $delta_hex->[$hex->[0] % 2]->[$i]->[1]);
114 }
115  
116 =head2 xy($coordinates)
117  
118 C<$coordinates> is a string with four digites and interpreted as coordinates and
119 returned, e.g. returns (2, 3) for "0203".
120  
121 =cut
122  
123  
124 sub xy {
125 2994 ▶ my $self = shift;
Subroutine xy: called CC=1 SCAR=0.0
Pod documented
126 2994 my $coordinates = shift;
127 2994 return (substr($coordinates, 0, 2), substr($coordinates, 2));
128 }
129  
130 =head2 legal($x, $y) or $legal($coordinates)
131  
132 say "legal" if $map->legal(10,10);
133  
134 Turn $coordinates into ($x, $y), assuming each to be two digits, i.e. "0203"
135 turns into (2, 3).
136  
137 Return ($x, $y) if the coordinates are legal, i.e. on the map.
138  
139 =cut
140  
141 sub legal {
142 1512 ▶ my $self = shift;
Subroutine legal: called CC=6 SCAR=17.9
Pod documented
143 1512 my ($x, $y) = @_;
144 1512 ▶ ($x, $y) = $self->xy($x) if not defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 1512 0 1512
145 1512 ▶ return @_ if $x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height;
Branch: 1 branch
BranchTrueFalseTotal
if $x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height 1368 144 1512
Condition100%9/9
$x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height
!ll&&!rl&&r
98461368
$x > 0 and $x <= $self->width and $y > 0
!ll&&!rl&&r
45531414
$x > 0 and $x <= $self->width
!ll&&!rl&&r
4051467
MC/DC100%4/4
$x > 0 and $x <= $self->width and $y > 0 and $y <= $self->height
$x > 0 $x <= $self->width $y > 0 $y <= $self->height
Truth table100%5/5
A and B and C and D
A= $x > 0B= $x <= $self->widthC= $y > 0D= $y <= $self->height
ABCDresult
0XXX0
10XX0
110X0
11100
11111
146 }
147  
148 =head2 neighbors($hex)
149  
150 say join(" ", $map->neighbors("0203"));
151 # 0202 0303 0304 0204 0104 0103 0102
152  
153 Returns the list of legal neighbours, one step away. This could be just two
154 neighbours (e.g. around 0101).
155  
156 C<$hex> is an array reference of coordinates or a string that can be turned into
157 one using the C<xy> method.
158  
159 =cut
160  
161 sub all_neighbors {
162 200 ▶ my $self = shift;
Subroutine all_neighbors: called CC=1 SCAR=0.0
Pod undocumented
163 200 my $hex = shift;
164 200 return grep { $self->legal($_) } map { coord($self->neighbor($hex, $_)) } $self->neighbors;
165 }
166  
167 =head2 generate_plains
168  
169 All hexes are plains.
170  
171 =cut
172  
173 sub generate_plains {
174 1 ▶ my $self = shift;
Subroutine generate_plains: called CC=3 SCAR=11.0
Pod documented
175 1 for my $x (1 .. $self->width) {
176 10 for my $y (1 .. $self->height) {
177 100 $self->world->{coord($x,$y)} = 'plain';
178 }
179 }
180 }
181  
182 =head2 generate_ocean
183  
184 1d6-2 edges of the map are ocean. Randomly determine which ones. Every hex on
185 these edges is ocean. Every hex bordering an ocean hex has a 50% chance to be
186 ocean. Every hex bordering one of these secondary ocean hexes has a 33% chance
187 to be ocean, unless it has already been rolled for.
188  
189 =cut
190  
191 sub generate_ocean {
192 1 ▶ my $self = shift;
Subroutine generate_ocean: called CC=22 SCAR=33.3
Pod documented
193 1 my $edges = int(rand(6))-2;
194 1 ▶ return if $edges < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $edges < 0 0 1 1
195 1 my @edges = shuffle(qw(north east south west));
196 1 for my $edge (@edges[0..$edges]) {
197 1 ▶ if ($edge eq 'west') {
Branch: 4 branches
BranchTrueFalseTotal
if ($edge eq 'west') { } 1 0 1
elsif ($edge eq 'east') { } 0 0 0
elsif ($edge eq 'north') { } 0 0 0
elsif ($edge eq 'south') { } 0 0 0
198 1 for my $y (1 .. $self->height) {
199 10 $self->world->{coord(1,$y)} = 'ocean';
200 }
201 } elsif ($edge eq 'east') {
202 0 for my $y (1 .. $self->height) {
203 0 $self->world->{coord($self->width,$y)} = 'ocean';
204 }
205 } elsif ($edge eq 'north') {
206 0 for my $x (1 .. $self->width) {
207 0 $self->world->{coord($x,1)} = 'ocean';
208 }
209 } elsif ($edge eq 'south') {
210 0 for my $x (1 .. $self->width) {
211 0 $self->world->{coord($x,$self->height)} = 'ocean';
212 }
213 }
214 }
215 1 my @secondary;
216 1 for my $hex (grep { $self->world->{$_} eq 'ocean' } sort keys %{$self->world}) {
217 10 for my $other ($self->all_neighbors($hex)) {
218 37 ▶ if ($self->world->{$other} ne 'ocean' and rand() < 1/3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->world->{$other} ne "ocean" and rand() < "0.333333333333333") 5 32 37
Condition100%3/3
$self->world->{$other} ne "ocean" and rand() < "0.333333333333333"
!ll&&!rl&&r
20125
MC/DC100%2/2
$self->world->{$other} ne "ocean" and rand() < "0.333333333333333"
$self->world->{$other} ne "ocean" rand() < "0.333333333333333"
Truth table100%3/3
A and B
A= $self->world->{$other} ne "ocean"B= rand() < "0.333333333333333"
ABresult
0X0
100
111
219 5 push(@secondary, $other);
220 5 $self->world->{$other} = 'ocean';
221 }
222 }
223 }
224 1 my %seen;
225 1 for my $hex (@secondary) {
226 5 for my $other ($self->all_neighbors($hex)) {
227 30 ▶ next if $seen{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$other} 6 24 30
228 24 $seen{$other} = 1;
229 24 ▶ if ($self->world->{$other} ne 'ocean' and rand() < 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->world->{$other} ne "ocean" and rand() < "0.5") 5 19 24
Condition100%3/3
$self->world->{$other} ne "ocean" and rand() < "0.5"
!ll&&!rl&&r
1185
MC/DC100%2/2
$self->world->{$other} ne "ocean" and rand() < "0.5"
$self->world->{$other} ne "ocean" rand() < "0.5"
Truth table100%3/3
A and B
A= $self->world->{$other} ne "ocean"B= rand() < "0.5"
ABresult
0X0
100
111
230 5 $self->world->{$other} = 'ocean';
231 }
232 }
233 }
234 1 for my $hex (grep { $self->world->{$_} eq 'ocean' } sort keys %{$self->world}) {
235 20 ▶ if (any { $self->world->{$_} ne 'ocean' and $self->world->{$_} ne 'water' } $self->all_neighbors($hex)) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $self->world->{$_} ne "ocean" and $self->world->{$_} ne "water"; } , $self->all_neighbors($hex))) 15 5 20
$self->world->{$_} ne "ocean" and $self->world->{$_} ne "water" 29 25 54
236 15 $self->world->{$hex} = 'water';
237 }
238 }
239 }
240  
241 =head2 generate_mountains
242  
243 Place 1d6 mountain hexes. Roll two d10s for each to determine its coordinates.
244 If you end up in an ocean hex or a previous mountain hex, roll again. Every
245 plains hex adjacent to a mountain hex has a 4 in 6 chance to be mountains as
246 well; otherwise, it is hills. Repeat, but now with a 2 in 6 chance. Every plains
247 hex adjacent to a hill hex has a 3 in 6 chance to be hills.
248  
249 =cut
250  
251 sub generate_mountains {
252 1 ▶ my $self = shift;
Subroutine generate_mountains: called CC=12 SCAR=24.9
Pod documented
253 1 my $m = int(rand(6))+1;
254 1 my $n = 0;
255 1 my @mountains;
256 1 ▶ while ($n < $m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $m) 4 1 5
257 4 my $x = int(rand($self->width))+1;
258 4 my $y = int(rand($self->height))+1;
259 4 my $coord = coord($x, $y);
260 4 ▶ if ($self->world->{$coord} eq 'plain') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->world->{$coord} eq "plain") 4 0 4
261 4 push(@mountains, $coord);
262 4 $self->world->{$coord} = 'mountain';
263 4 $n++;
264 }
265 }
266 1 for my $chance (2/3, 1/3, 0) {
267 3 for my $hex (grep { $self->world->{$_} eq 'mountain' } sort keys %{$self->world}) {
268 45 for my $other ($self->all_neighbors($hex)) {
269 239 ▶ if ($self->world->{$other} eq 'plain') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->world->{$other} eq "plain") 50 189 239
270 50 ▶ if ($chance and rand() < $chance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chance and rand() < $chance) { } 21 29 50
Condition100%3/3
$chance and rand() < $chance
!ll&&!rl&&r
131621
MC/DC100%2/2
$chance and rand() < $chance
$chance rand() < $chance
Truth table100%3/3
A and B
A= $chanceB= rand() < $chance
ABresult
0X0
100
111
271 21 $self->world->{$other} = 'mountain';
272 } else {
273 29 $self->world->{$other} = 'hills';
274 }
275 }
276 }
277 }
278 }
279 1 for my $hex (grep { $self->world->{$_} eq 'hills' } sort keys %{$self->world}) {
280 29 for my $other ($self->all_neighbors($hex)) {
281 168 ▶ if ($self->world->{$other} eq 'plain') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->world->{$other} eq "plain") 16 152 168
282 16 $self->world->{$other} = 'hills';
283 }
284 }
285 }
286 }
287  
288 =head2 rivers
289  
290 The original instructions are: "Roll 1d6 to determine how many major rivers
291 there are: 1 none, 2-4 one, 5 two, 6 two rivers joining into one. Each river has
292 a 3 in 6 chance to be flowing out of a mountain or hill hex; otherwise, it
293 enters from the edge of the map (if there is a land edge). If there is an ocean
294 on the map, the rivers will flow into it."
295  
296 Instead of doing that, let's try this: "A river starts in ever mountain and
297 every hill, flowing downwards if possible: from mountains to hills, from hills
298 to plains and from plains into the ocean or off the map. Pick the lowest lying
299 neighbour. We can mark these as canyons, later. When a river meets another
300 river, then merge them (same tail) or subsume them (if meerging with the
301 beginning of an existing river)."
302  
303 =cut
304  
305 sub generate_rivers {
306 1 ▶ my $self = shift;
Subroutine generate_rivers: called CC=12 SCAR=26.1
Pod undocumented
307 1 my %seen;
308 1 local $" = "-";
309 1 for my $hex (grep { $self->world->{$_} eq 'mountain' } sort keys %{$self->world}) {
310 25 ▶ next if $seen{$hex};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$hex} 1 24 25
311 24 my $river = [$hex];
312 24 $seen{$hex} = $river;
313 24 push(@{$self->rivers}, $river);
314 24 $self->wet->{$hex} = 1;
315 24 $log->debug("River starting at $hex");
316 24 while(1) {
317 47 my @neighbours = map { coord($self->neighbor($hex, $_)) } shuffle $self->neighbors;
318 47 ▶ my $end = first { not $self->legal($_) or $self->world->{$_} eq 'water' } @neighbours;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
not $self->legal($_) or $self->world->{$_} eq "water" 210 10 220
319 47 ▶ if ($end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end) 16 31 47
320 16 $log->debug(" ends at $end");
321 16 push(@$river, $end);
322 16 last;
323 }
324 # $log->debug(" neighbours: " . join(", ", map { "$_: " . $self->world->{$_} } @neighbours));
325 31 @neighbours = sort { $self->altitude->{$self->world->{$a}} <=> $self->altitude->{$self->world->{$b}} } @neighbours;
326 31 my $next = shift(@neighbours);
327 31 ▶ if ($seen{$next}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$next}) 8 23 31
328 8 my @other = @{$seen{$next}};
329 8 $log->debug(" found river at $next: @other");
330 # avoid loops
331 8 ▶ while ($other[0] eq $river->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other[0] eq $river->[0]) 0 8 8
332 0 $next = shift(@neighbours);
333 0 ▶ if ($seen{$next}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$next}) { } 0 0 0
334 0 @other = @{$seen{$next}};
335 0 $log->debug(" nope, try again at $next: @other");
336 # check again
337 } else {
338 0 @other = ();
339 0 $log->debug(" nope, try again at $next (no river)");
340 0 last;
341 }
342 }
343 8 ▶ if (@other > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other > 0) 8 0 8
344 8 ▶ if ($other[0] eq $next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other[0] eq $next) { } 1 7 8
345 1 $log->debug(" flows into @other");
346 # append the other river hexes to this river and remove the other river from the list
347 1 push(@$river, @other);
348 1 $self->rivers([grep { $_->[0] ne $next } @{$self->rivers}]);
349 } else {
350 7 $log->debug(" merges into @other");
351 # copy the downstream hexes of the other river
352 7 ▶ shift(@other) while $other[0] and $other[0] ne $next;
Branch: 1 branch
BranchTrueFalseTotal
$other[0] and $other[0] ne $next and do { shift @other } 11 7 18
Condition66%2/3
$other[0] and $other[0] ne $next
!ll&&!rl&&r
0711
MC/DC50%1/2
$other[0] and $other[0] ne $next
$other[0] $other[0] ne $next
Truth table66%2/3
A and B
A= $other[0]B= $other[0] ne $next
ABresult
0X0
100
111
353 7 push(@$river, @other);
354 }
355 8 last;
356 }
357 0 ▶ if (not $next) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next) 0 0 0
358 # with no other neighbour found, the river goes underground!?
359 0 $log->debug(" disappears");
360 0 last;
361 }
362 # if the neighbour is not a a river and exists, fall through
363 }
364 23 $hex = $next;
365 23 $log->debug(" flows to $hex");
366 23 push(@$river, $hex);
367 23 $seen{$hex} = $river;
368 23 $self->wet->{$hex} = 1;
369 }
370 }
371 }
372  
373 =head2 generate_canyons
374  
375 Check all the rivers: if it flows "uphill", add a canyon
376  
377 =cut
378  
379 sub generate_canyons {
380 1 ▶ my $self = shift;
Subroutine generate_canyons: called CC=13 SCAR=36.8
Pod documented
381 1 local $" = "-";
382 1 my %seen;
383 1 my $canyon = [];
384 1 for my $river (@{$self->rivers}) {
385 23 ▶ next unless @$river > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$river > 2 8 15 23
386 15 my $last = $river->[0];
387 15 my $current_altitude = $self->altitude->{$self->world->{$last}};
388 15 $log->debug("Looking at @$river ($current_altitude)");
389 15 for my $hex (@$river) {
390 54 ▶ if ($seen{$hex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$hex}) 8 46 54
391 8 ▶ if (@$canyon == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (@$canyon == 0) { } 8 0 8
elsif ($seen{$hex} == 1) { } 0 0 0
elsif ($seen{$hex} > 1) { } 0 0 0
392 8 last;
393 } elsif ($seen{$hex} == 1) {
394 0 push(@$canyon, $hex);
395 0 push(@{$self->canyons}, $canyon);
396 0 $canyon = [];
397 0 $log->debug(" ending cayon at known $hex");
398 0 $current_altitude = $self->altitude->{$self->world->{$hex}};
399 0 next;
400 } elsif ($seen{$hex} > 1) {
401 0 push(@{$self->canyons}, $canyon);
402 0 $canyon = [];
403 0 $log->debug(" merging cayon at $hex");
404 # FIXME
405 0 last;
406 }
407 }
408 46 $seen{$hex}++;
409 46 ▶ if ($self->legal($hex) and $self->altitude->{$self->world->{$hex}} > $current_altitude) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->legal($hex) and $self->altitude->{$self->world->{$hex}} > $current_altitude) { } 0 46 46
elsif (@$canyon > 0) { } 0 46 46
elsif ($self->legal($hex)) { } 43 3 46
Condition66%2/3
$self->legal($hex) and $self->altitude->{$self->world->{$hex}} > $current_altitude
!ll&&!rl&&r
3430
MC/DC0%0/2
$self->legal($hex) and $self->altitude->{$self->world->{$hex}} > $current_altitude
$self->legal($hex) $self->altitude->{$self->world->{$hex}} > $current_altitude
Truth table66%2/3
A and B
A= $self->legal($hex)B= $self->altitude->{$self->world->{$hex}} > $current_altitude
ABresult
0X0
100
111
410 0 ▶ if (@$canyon > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$canyon > 0) { } 0 0 0
411 0 push(@$canyon, $hex);
412 0 $log->debug(" extending cayon to $hex");
413 } else {
414 0 $canyon = [$last, $hex];
415 0 $log->debug("Starting cayon @$canyon");
416 }
417 0 $seen{$hex}++; # more than 1 means this is inside a canyon
418 } elsif (@$canyon > 0) {
419 0 push(@$canyon, $hex);
420 0 push(@{$self->canyons}, $canyon);
421 0 $canyon = [];
422 0 $log->debug(" ending cayon at $hex");
423 0 $current_altitude = $self->altitude->{$self->world->{$hex}};
424 } elsif ($self->legal($hex)) {
425 43 $current_altitude = $self->altitude->{$self->world->{$hex}};
426 }
427 46 $last = $hex;
428 }
429 }
430 }
431  
432 =head2 generate_dry
433  
434 The wind blows from west or east. Follow the wind in straight horizontal lines.
435 Once the line hits a mountain, all the following hexes are dry hills or dry
436 plains except if it has a river.
437  
438 =cut
439  
440 sub generate_dry {
441 1 ▶ my $self = shift;
Subroutine generate_dry: called CC=15 SCAR=27.3
Pod documented
442 1 ▶ my $dir = rand() < 0.5 ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
rand() < 0.5 ? : 0 1 1
443 1 ▶ my $start = $dir == 1 ? 1 : $self->width;
Branch: 1 branch
BranchTrueFalseTotal
$dir == 1 ? : 1 0 1
444 1 ▶ my $end = $dir == 1 ? $self->width : 1;
Branch: 1 branch
BranchTrueFalseTotal
$dir == 1 ? : 1 0 1
445 1 for my $y (1 .. $self->height) {
446 10 my $dry = 0;
447 10 ▶ for (my $x = $start; $dir == 1 ? $x <= $end : $x >= $end; $x += $dir) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir == 1 ? $x <= $end : $x >= $end) 100 10 110
$dir == 1 ? : 110 0 110
448 100 my $hex = coord($x, $y);
449 100 ▶ if (not $dry and $self->world->{$hex} eq 'mountain') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $dry and $self->world->{$hex} eq 'mountain') { } 10 90 100
elsif ($dry) { } 53 37 90
Condition100%3/3
not $dry and $self->world->{$hex} eq "mountain"
!ll&&!rl&&r
533710
MC/DC100%2/2
not $dry and $self->world->{$hex} eq "mountain"
not $dry $self->world->{$hex} eq "mountain"
Truth table100%3/3
not(A) and B
A= not $dryB= $self->world->{$hex} eq "mountain"
ABresult
0X0
100
111
450 10 ▶ $log->debug("Going " . ($dir == 1 ? 'east' : 'west') . " from $hex is dry");
Branch: 1 branch
BranchTrueFalseTotal
$dir == 1 ? : 10 0 10
451 10 $dry = $x;
452 } elsif ($dry) {
453 53 my @hexes = ($hex);
454 # $dry contains the $x of the mountain. If $x something like 0306, we
455 # want to check 0405 (-1!) and 0406; if $x is something like 0607, we
456 # want to check 0707 and 0708 (+1). That is to say, it depends on
457 # whether the initial $x is even or odd. Also, it's always two hexes to
458 # check if the difference between the two $x coordinates is odd.
459 53 ▶ push(@hexes, coord($x, $y + ($dry % 2 ? -1 : +1))) if abs($x - $dry) % 2;
Branch: 2 branches
BranchTrueFalseTotal
if abs($x - $dry) % 2 28 25 53
$dry % 2 ? : 9 19 28
460 53 for my $hex2 (@hexes) {
461 81 ▶ next if $self->wet->{$hex2};
Branch: 1 branch
BranchTrueFalseTotal
if $self->wet->{$hex2} 42 39 81
462 39 $log->debug(" $hex2 is dry");
463 39 $self->dry->{$hex2} = 1;
464 }
465 }
466 }
467 }
468 }
469  
470 =head2 generate_forest
471  
472 Every hex with a river has a 50% chance to be forested. Every hills or plains
473 hex without a river that isn’t dry or next to a dry hex has a 1 in 6 chance to
474 be forested; 2 in 6 if it is next to a forested river hex.
475  
476 =cut
477  
478 sub generate_forest {
479 1 ▶ my $self = shift;
Subroutine generate_forest: called CC=16 SCAR=27.7
Pod documented
480 1 ▶ my @land_hexes = grep { $self->world->{$_} ne 'water' and $self->world->{$_} ne 'ocean' } sort keys %{$self->world};
Branch: 1 branch
BranchTrueFalseTotal
$self->world->{$_} ne "water" and $self->world->{$_} ne "ocean" 85 15 100
481 1 my %forest_hexes;
482 1 for my $hex (@land_hexes) {
483 80 ▶ if ($self->wet->{$hex} and rand() < 0.5
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wet->{$hex} and rand() < "0.5" or not $self->dry->{$hex} || &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex)) and rand() < "0.166666666666667") 30 50 80
Condition100%12/12
$self->wet->{$hex} and rand() < "0.5" or not $self->dry->{$hex} || &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex)) and rand() < "0.166666666666667"
l!l&&r!l&&!r
231146
$self->wet->{$hex} and rand() < "0.5"
!ll&&!rl&&r
332423
not $self->dry->{$hex} || &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex)) and rand() < "0.166666666666667"
!ll&&!rl&&r
34167
$self->dry->{$hex} || &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex))
l!l&&r!l&&!r
231123
MC/DC60%3/5
$self->wet->{$hex} and rand() < "0.5" or not $self->dry->{$hex} || &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex)) and rand() < "0.166666666666667"
$self->wet->{$hex} rand() < "0.5" $self->dry->{$hex} &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex)) rand() < "0.166666666666667"
Truth table55%5/9
A and B or not(C || D) and E
A= $self->wet->{$hex}B= rand() < "0.5"C= $self->dry->{$hex}D= &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex))E= rand() < "0.166666666666667"
ABCDEresult
11XXX1
0X0011
100011
0X1XX0
0X01X0
0X0000
101XX0
1001X0
100000
484 or not $self->dry->{$hex}
485 156 ▶ and not any { $self->dry->{$_} } $self->all_neighbors($hex)
Subroutine __ANON__: called CC=1 SCAR=0.0
486 and rand() < 1/6) {
487 30 ▶ if ($self->world->{$hex} eq 'plain' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->world->{$hex} eq 'plain') { } 2 28 30
elsif ($self->world->{$hex} eq 'hills') { } 14 14 28
488 2 $self->world->{$hex} = 'forest';
489 2 $forest_hexes{$hex} = 1;
490 } elsif ($self->world->{$hex} eq 'hills' ) {
491 14 $self->world->{$hex} = 'forest-hill';
492 14 $forest_hexes{$hex} = 1;
493 }
494 }
495 }
496 # since this pass relies on neighbours being forested
497 1 for my $hex (@land_hexes) {
498 80 ▶ if (not $self->dry->{$hex}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->dry->{$hex} and &any(sub { $forest_hexes{$_}; } , $self->all_neighbors($hex)) and rand() < "0.333333333333333") 15 65 80
Condition100%6/6
not $self->dry->{$hex} and &any(sub { $forest_hexes{$_}; } , $self->all_neighbors($hex)) and rand() < "0.333333333333333"
!ll&&!rl&&r
372815
not $self->dry->{$hex} and &any(sub { $forest_hexes{$_}; } , $self->all_neighbors($hex))
!ll&&!rl&&r
231443
MC/DC100%3/3
not $self->dry->{$hex} and &any(sub { $forest_hexes{$_}; } , $self->all_neighbors($hex)) and rand() < "0.333333333333333"
not $self->dry->{$hex} &any(sub { $forest_hexes{$_}; } , $self->all_neighbors($hex)) rand() < "0.333333333333333"
Truth table100%4/4
not(A) and B and C
A= not $self->dry->{$hex}B= &any(sub { $forest_hexes{$_}; } , $self->all_neighbors($hex))C= rand() < "0.333333333333333"
ABCresult
0XX0
10X0
1100
1111
499 192 ▶ and any { $forest_hexes{$_} } $self->all_neighbors($hex)
Subroutine __ANON__: called CC=1 SCAR=0.0
500 and rand() < 2/6) {
501 15 ▶ if ($self->world->{$hex} eq 'plain' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->world->{$hex} eq 'plain') { } 1 14 15
elsif ($self->world->{$hex} eq 'hills') { } 2 12 14
502 1 $self->world->{$hex} = 'forest';
503 } elsif ($self->world->{$hex} eq 'hills' ) {
504 2 $self->world->{$hex} = 'forest-hill';
505 }
506 }
507 }
508 }
509  
510 =head2 generate_swamp
511  
512 A 1 in 6 chance on every plain river hex that isn't next to a dry hex.
513  
514 =cut
515  
516 sub generate_swamp {
517 1 ▶ my $self = shift;
Subroutine generate_swamp: called CC=5 SCAR=22.7
Pod documented
518 1 ▶ for my $hex (grep { $self->world->{$_} eq 'plain' and $self->wet->{$_} } sort keys %{$self->world}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->world->{$_} eq "plain" and $self->wet->{$_} 7 93 100
519 0 ▶ next if any { $self->dry->{$_} } $self->all_neighbors($hex);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $self->dry->{$_}; } , $self->all_neighbors($hex)) 0 0 0
520 0 ▶ if (rand() < 1/6) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.166666666666667") 0 0 0
521 0 $self->world->{$hex} = 'swamp';
522 }
523 }
524 }
525  
526 =head2 generate_islands
527  
528 Every ocean hex has a 1 in 6 chance of having an island.
529  
530 =cut
531  
532 sub generate_islands {
533 1 ▶ my $self = shift;
Subroutine generate_islands: called CC=4 SCAR=13.9
Pod documented
534 1 ▶ for my $hex (grep { $self->world->{$_} eq 'water' or $self->world->{$_} eq 'ocean' } sort keys %{$self->world}) {
Branch: 1 branch
BranchTrueFalseTotal
$self->world->{$_} eq "water" or $self->world->{$_} eq "ocean" 85 15 100
535 20 ▶ if (rand() < 1/6) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.166666666666667") 2 18 20
536 2 $self->world->{$hex} .= " island";
537 }
538 }
539 }
540  
541 =head2 string
542  
543 Create the string output.
544  
545 =cut
546  
547 sub string {
548 1 ▶ my $self = shift;
Subroutine string: called CC=1 SCAR=0.0
Pod documented
549 100 return join("\n", map { $_ . " " . $self->world->{$_} } sort keys %{$self->world}) . "\n"
550 23 . join("\n", map { join("-", @$_) . " river" } @{$self->rivers}) . "\n"
551 1 . join("\n", map { join("-", @$_) . " canyon" } @{$self->canyons}) . "\n";
552 }
553  
554 =head2 generate_map
555  
556 Start with a 10 by 10 hexmap.
557  
558 =cut
559  
560 sub generate_map {
561 1 ▶ my $self = shift;
Subroutine generate_map: called CC=1 SCAR=0.0
Pod documented
562 1 $self->generate_plains();
563 1 $self->generate_ocean();
564 1 $self->generate_mountains();
565 1 $self->generate_rivers();
566 1 $self->generate_canyons();
567 1 $self->generate_dry();
568 1 $self->generate_forest();
569 1 $self->generate_swamp();
570 1 $self->generate_islands();
571 1 return $self->string() . "\n"
572 . "include bright.txt\n";
573 }
574  
575 =head1 SEE ALSO
576  
577 Andreas Folkesten described this algorithm in the following blog post:
578 L<http://arch-brick.blogspot.com/2023/08/hexmap-terrain-generator.html>.
579  
580 The map itself uses the I<Light> icons by Alex Schroeder. These are
581 dedicated to the public domain. See
582 L<http://creativecommons.org/licenses/by-sa/3.0/>.
583  
584 =cut
585  
586 11 1;
« blib/lib/Game/TextMapper/Smale.pm ↑ Summary blib/lib/Game/TextMapper/Schroeder/Island.pm »