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

filter: Statementstmt 95.4% 147 / 154 Branchbran 90.2% 65 / 72 Conditioncond 64.2% 9 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 7 totaltotal 88.3% 243 / 275 CC 58 SCAR 23.9
CC
58
Cov
92%
change
21.0
generate_map
15.2
forests
9.0
CRAP 10.9
« blib/lib/Game/TextMapper/Folkesten.pm ↑ Summary blib/lib/Game/TextMapper/Point/Square.pm »
1 # Copyright (C) 2009-2021 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::Schroeder::Island - generate an island chain
21  
22 =head1 DESCRIPTION
23  
24 This creates an island chain in an ocean, based on the idea of a hotspot moving
25 across the map. All regions atop the hotspot get raised at random; all regions
26 outside the hotspot are eroded at random. This leaves a chain of ever smaller
27 islands behind.
28  
29 The rest of the code, river formation and all that, is based on the Alpine
30 algorithm and therefore it also requires the use of roles.
31  
32 return Game::TextMapper::Schroeder::Island
33 ->with_roles('Game::TextMapper::Schroeder::Square')->new()
34 ->generate_map(@params);
35  
36 =head1 SEE ALSO
37  
38 L<Game::TextMapper::Schroeder::Alpine>
39 L<Game::TextMapper::Schroeder::Base>
40 L<Game::TextMapper::Schroeder::Hex>
41 L<Game::TextMapper::Schroeder::Square>
42  
43 =cut
44  
45 package Game::TextMapper::Schroeder::Island;
46 11 ▶use Game::TextMapper::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 11 ▶use Mojo::Base 'Game::TextMapper::Schroeder::Alpine';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 with 'Game::TextMapper::Schroeder::Base';
51 11 ▶use List::Util qw'shuffle min max';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 11 my $log = Game::TextMapper::Log->get;
54  
55 11 has 'bottom' => 0;
56 11 has 'top' => 10;
57 11 has 'radius' => 5;
58 11 has 'hotspot';
59  
60 sub ocean {
61 4 ▶ my $self = shift;
Subroutine ocean: called CC=7 SCAR=19.5
Pod undocumented
62 4 my ($world, $altitude) = @_;
63 4 for my $coordinates (sort keys %$altitude) {
64 2400 ▶ if ($altitude->{$coordinates} <= $self->bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($altitude->{$coordinates} <= $self->bottom) 2071 329 2400
65 2071 my $ocean = 1;
66 2071 for my $i ($self->neighbors()) {
67 10244 my ($x, $y) = $self->neighbor($coordinates, $i);
68 10244 my $legal = $self->legal($x, $y);
69 10244 my $other = coordinates($x, $y);
70 10244 ▶ next if not $legal or $altitude->{$other} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if not $legal or $altitude->{$other} <= $self->bottom 9451 793 10244
Condition100%3/3
not $legal or $altitude->{$other} <= $self->bottom
l!l&&r!l&&!r
6538798793
MC/DC100%2/2
not $legal or $altitude->{$other} <= $self->bottom
not $legal $altitude->{$other} <= $self->bottom
Truth table100%3/3
not(A) or B
A= not $legalB= $altitude->{$other} <= $self->bottom
ABresult
1X1
011
000
71 793 $ocean = 0;
72 }
73 2071 ▶ $world->{$coordinates} = $ocean ? "ocean" : "water";
Branch: 1 branch
BranchTrueFalseTotal
$ocean ? : 1626 445 2071
74 }
75 }
76 }
77  
78 sub change {
79 128 ▶ my $self = shift;
Subroutine change: called CC=21 SCAR=30.4
Pod undocumented
80 128 ▶ return if $self->hotspot->[0] > $self->width - 2 * $self->radius;
Branch: 1 branch
BranchTrueFalseTotal
if $self->hotspot->[0] > $self->width - 2 * $self->radius 50 78 128
81 78 my $world = shift;
82 78 my $altitude = shift;
83 # advance hotspot
84 78 ▶ if (rand() < 0.2) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < 0.2) { } 10 68 78
85 10 $self->hotspot->[0] += 1.5 * $self->radius;
86 } else {
87 68 $self->hotspot->[0]++;
88 }
89 78 ▶ if (rand() < 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.5") 42 36 78
90 42 ▶ if (rand() > $self->hotspot->[1] / $self->height) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() > $self->hotspot->[1] / $self->height) { } 21 21 42
91 21 $self->hotspot->[1]++;
92 } else {
93 21 $self->hotspot->[1]--;
94 }
95 }
96 # figure out who goes up and who goes down, if the hotspot is active
97 78 my %hot;
98 78 for my $x (max(1, $self->hotspot->[0] - $self->radius) .. min($self->width, $self->hotspot->[0] + $self->radius)) {
99 689 for my $y (max(1, $self->hotspot->[1] - $self->radius) .. min($self->height, $self->hotspot->[1] + $self->radius)) {
100 6132 ▶ if ($self->distance($x, $y, @{$self->hotspot}) <= $self->radius) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->distance($x, $y, @{$self->hotspot;}) <= $self->radius) 4051 2081 6132
101 4051 my $coordinates = coordinates($x, $y);
102 4051 $hot{$coordinates} = 1;
103 }
104 }
105 }
106 # change the land
107 78 for my $coordinates (keys %$altitude) {
108 46800 my $change = 0;
109 46800 ▶ if ($hot{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hot{$coordinates}) { } 4051 42749 46800
110 # on the hotspot the land rises
111 4051 ▶ $change = 1 if rand() < 0.2;
Branch: 1 branch
BranchTrueFalseTotal
if rand() < "0.2" 847 3204 4051
112 } else {
113 # off the hotspot the land sinks
114 42749 ▶ $change = -1 if rand() < 0.2;
Branch: 1 branch
BranchTrueFalseTotal
if rand() < "0.2" 8583 34166 42749
115 }
116 46800 ▶ next unless $change;
Branch: 1 branch
BranchTrueFalseTotal
unless $change 37370 9430 46800
117 # rising from the ocean atop the hotspot
118 9430 $altitude->{$coordinates} += $change;
119 9430 ▶ $altitude->{$coordinates} = $self->bottom if $altitude->{$coordinates} < $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < $self->bottom 8058 1372 9430
120 9430 ▶ $altitude->{$coordinates} = $self->top if $altitude->{$coordinates} > $self->top;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $self->top 0 9430 9430
121 }
122 # land with higher neighbours on the hotspot goes up
123 78 for my $coordinates (keys %hot) {
124 4051 my $change = 0;
125 4051 for my $i ($self->neighbors()) {
126 4139 my ($x, $y) = $self->neighbor($coordinates, $i);
127 4139 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 88 4051 4139
128 4051 my $other = coordinates($x, $y);
129 4051 ▶ $change = 1 if $altitude->{$other} - $altitude->{$coordinates} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} - $altitude->{$coordinates} > 1 203 3848 4051
130 4051 last;
131 }
132 4051 ▶ $altitude->{$coordinates}++ if $change;
Branch: 1 branch
BranchTrueFalseTotal
if $change 203 3848 4051
133 }
134 # note height for debugging purposes
135 78 for my $coordinates (keys %$altitude) {
136 46800 $world->{$coordinates} = "height$altitude->{$coordinates}";
137 }
138 }
139  
140 sub forests {
141 4 ▶ my $self = shift;
Subroutine forests: called CC=9 SCAR=22.0
Pod undocumented
142 4 my ($world, $altitude) = @_;
143 # higher up is forests
144 4 for my $coordinates (keys %$altitude) {
145 2400 ▶ next unless $altitude->{$coordinates}; # skip ocean
Branch: 1 branch
BranchTrueFalseTotal
unless $altitude->{$coordinates} 2071 329 2400
146 329 ▶ next if $world->{$coordinates} =~ /mountain|lake/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /mountain|lake/u 49 280 329
147 280 ▶ if ($altitude->{$coordinates} == 1) {
Branch: 5 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} == 1) { } 158 122 280
elsif ($altitude->{$coordinates} == 2) { } 76 46 122
elsif ($altitude->{$coordinates} == 3) { } 33 13 46
elsif ($altitude->{$coordinates} == 4) { } 10 3 13
elsif ($altitude->{$coordinates} > 4) { } 3 0 3
148 158 $world->{$coordinates} = "light-grey bushes";
149 } elsif ($altitude->{$coordinates} == 2) {
150 76 $world->{$coordinates} = "light-green trees";
151 } elsif ($altitude->{$coordinates} == 3) {
152 33 $world->{$coordinates} = "green forest";
153 } elsif ($altitude->{$coordinates} == 4) {
154 10 $world->{$coordinates} = "dark-green forest";
155 } elsif ($altitude->{$coordinates} > 4) {
156 3 $world->{$coordinates} = "dark-green mountains";
157 }
158 }
159 }
160  
161 sub lakes {
162 4 ▶ my $self = shift;
Subroutine lakes: called CC=6 SCAR=17.9
Pod undocumented
163 4 my ($world, $altitude) = @_;
164 # any areas surrounded by higher land is a lake
165 HEX:
166 4 for my $coordinates (sort keys %$altitude) {
167 2400 for my $i ($self->neighbors()) {
168 3129 my ($x, $y) = $self->neighbor($coordinates, $i);
169 3129 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 182 2947 3129
170 2947 my $other = coordinates($x, $y);
171 2947 ▶ next HEX if $altitude->{$other} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} == 0 2306 641 2947
172 641 ▶ next HEX if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 72 569 641
173 }
174 22 $world->{$coordinates} = "green lake";
175 }
176 }
177  
178 sub islands {
179 4 ▶ my $self = shift;
Subroutine islands: called CC=6 SCAR=17.9
Pod undocumented
180 4 my ($world, $altitude) = @_;
181 # any areas surrounded by water is an island
182 HEX:
183 4 for my $coordinates (sort keys %$altitude) {
184 2400 ▶ next if $altitude->{$coordinates} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} == 0 2071 329 2400
185 329 for my $i ($self->neighbors()) {
186 679 my ($x, $y) = $self->neighbor($coordinates, $i);
187 679 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 3 676 679
188 676 my $other = coordinates($x, $y);
189 676 ▶ next HEX if $altitude->{$other} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} > 0 292 384 676
190 }
191 37 $world->{$coordinates} = "water mountains";
192 }
193 }
194  
195 sub generate {
196 4 ▶ my $self = shift;
Subroutine generate: called CC=4 SCAR=13.9
Pod undocumented
197 4 my ($world, $altitude, $settlements, $trails, $step) = @_;
198 # %flow indicates that there is actually a river in this hex
199 4 my $flow = {};
200  
201 4 $self->hotspot([int($self->radius / 2), int($self->height / 3 + rand() * $self->height / 3)]);
202  
203 4 ▶ my @code = (sub { $self->flat($altitude) });
Subroutine __ANON__: called CC=1 SCAR=0.0
204 4 for (1 .. $self->width - 2 * $self->radius) {
205 128 ▶ push(@code, sub { $self->change($world, $altitude) });
Subroutine __ANON__: called CC=1 SCAR=0.0
206 }
207 4 ▶ push(@code, sub { $self->ocean($world, $altitude) });
Subroutine __ANON__: called CC=1 SCAR=0.0
208  
209 push(@code,
210 4 ▶ sub { $self->lakes($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
211 4 ▶ sub { $self->islands($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
212 4 ▶ sub { $self->forests($world, $altitude); },
Subroutine __ANON__: called CC=1 SCAR=0.0
213 4 ▶ sub { push(@$settlements, $self->settlements($world, $flow)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
214 4 ▶ sub { push(@$trails, $self->trails($altitude, $settlements)); },
Subroutine __ANON__: called CC=1 SCAR=0.0
215 4 );
216  
217 # $step 0 runs all the code; note that we can't simply cache those results
218 # because we need to start over with the same seed!
219 4 my $i = 1;
220 4 ▶ while (@code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@code) 156 4 160
221 156 shift(@code)->();
222 156 ▶ return if $step == $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $step == $i++ 0 156 156
223 }
224 }
225  
226 sub generate_map {
227 4 ▶ my $self = shift;
Subroutine generate_map: called CC=11 SCAR=27.2
Pod undocumented
228 # The parameters turn into class variables.
229 4 ▶ $self->width(shift // 40);
Condition50%1/2
shift() // 40
l!l
04
MC/DC0%0/1
shift() // 40
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
230 4 ▶ $self->height(shift // 15);
Condition50%1/2
shift() // 15
l!l
04
MC/DC0%0/1
shift() // 15
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
231 4 ▶ $self->radius(shift // 4);
Condition50%1/2
shift() // 4
l!l
04
MC/DC0%0/1
shift() // 4
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
232 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
233 4 my $url = shift;
234 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
235  
236 # For documentation purposes, I want to be able to set the pseudo-random
237 # number seed using srand and rely on rand to reproduce the same sequence of
238 # pseudo-random numbers for the same seed. The key point to remember is that
239 # the keys function will return keys in random order. So if we look over the
240 # result of keys, we need to look at the code in the loop: If order is
241 # important, that wont do. We need to sort the keys. If we want the keys to be
242 # pseudo-shuffled, use shuffle sort keys.
243 4 srand($seed);
244  
245 # keys for all hashes are coordinates such as "0101".
246 # %world is the description with values such as "green forest".
247 # %altitude is the altitude with values such as 3.
248 # @settlements are are the locations of settlements such as "0101"
249 # @trails are the trails connecting these with values as "0102-0202"
250 # $step is how far we want map generation to go where 0 means all the way
251 4 my ($world, $altitude, $settlements, $trails) =
252 ({}, {}, [], []);
253 4 $self->generate($world, $altitude, $settlements, $trails, $step);
254  
255 # when documenting or debugging, do this before collecting lines
256 4 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 4 4
257 # add a height label at the very end
258 0 ▶ if ($step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step) 0 0 0
259 0 for my $coordinates (keys %$world) {
260 0 $world->{$coordinates} .= ' "' . $altitude->{$coordinates} . '"';
261 }
262 }
263 }
264  
265 4 local $" = "-"; # list items separated by -
266 4 my @lines;
267 4 push(@lines, map { $_ . " " . $world->{$_} } sort keys %$world);
268 4 push(@lines, map { "$_ trail" } @$trails);
269 4 push(@lines, "include gnomeyland.txt");
270  
271 # when documenting or debugging, add some more lines at the end
272 4 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 4 4
273 # visualize height
274 push(@lines,
275 map {
276 0 my $n = int(25.5 * $_);
277 0 qq{height$_ attributes fill="rgb($n,$n,$n)"};
278 } (0 .. 10));
279 # visualize water flow
280 0 push(@lines, $self->arrows());
281 }
282  
283 4 push(@lines, "# Seed: $seed");
284 4 ▶ push(@lines, "# Documentation: " . $url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 2 2 4
285 4 my $map = join("\n", @lines);
286 4 return $map;
287 }
288  
289 11 1;
« blib/lib/Game/TextMapper/Folkesten.pm ↑ Summary blib/lib/Game/TextMapper/Point/Square.pm »