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

filter: Statementstmt 95.4% 147 / 154 Branchbran 90.2% 65 / 72 Conditioncond 69.2% 9 / 13 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 7 totaltotal 89.3% 244 / 273 CC 58 SCAR 23.8
CC
58
Cov
92%
change
21.0
generate_map
14.7
forests
9.0
CRAP 10.8
« blib/lib/Game/TextMapper/Smale.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 4 ▶sub ocean {
Subroutine ocean: called CC=7 SCAR=19.5
Pod undocumented
61 4 my $self = shift;
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) 2134 266 2400
65 2134 my $ocean = 1;
66 2134 for my $i ($self->neighbors()) {
67 10646 my ($x, $y) = $self->neighbor($coordinates, $i);
68 10646 my $legal = $self->legal($x, $y);
69 10646 my $other = coordinates($x, $y);
70 10646 ▶ next if not $legal or $altitude->{$other} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if not $legal or $altitude->{$other} <= $self->bottom 9887 759 10646
Condition100%3/3
not $legal or $altitude->{$other} <= $self->bottom
l!l&&r!l&&!r
6459242759
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 759 $ocean = 0;
72 }
73 2134 ▶ $world->{$coordinates} = $ocean ? "ocean" : "water";
Branch: 1 branch
BranchTrueFalseTotal
$ocean ? : 1663 471 2134
74 }
75 }
76 }
77  
78 128 ▶sub change {
Subroutine change: called CC=21 SCAR=30.4
Pod undocumented
79 128 my $self = shift;
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 59 69 128
81 69 my $world = shift;
82 69 my $altitude = shift;
83 # advance hotspot
84 69 ▶ if (rand() < 0.2) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < 0.2) { } 12 57 69
85 12 $self->hotspot->[0] += 1.5 * $self->radius;
86 } else {
87 57 $self->hotspot->[0]++;
88 }
89 69 ▶ if (rand() < 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.5") 33 36 69
90 33 ▶ if (rand() > $self->hotspot->[1] / $self->height) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() > $self->hotspot->[1] / $self->height) { } 17 16 33
91 17 $self->hotspot->[1]++;
92 } else {
93 16 $self->hotspot->[1]--;
94 }
95 }
96 # figure out who goes up and who goes down, if the hotspot is active
97 69 my %hot;
98 69 for my $x (max(1, $self->hotspot->[0] - $self->radius) .. min($self->width, $self->hotspot->[0] + $self->radius)) {
99 610 for my $y (max(1, $self->hotspot->[1] - $self->radius) .. min($self->height, $self->hotspot->[1] + $self->radius)) {
100 5400 ▶ if ($self->distance($x, $y, @{$self->hotspot}) <= $self->radius) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->distance($x, $y, @{$self->hotspot;}) <= $self->radius) 3329 2071 5400
101 3329 my $coordinates = coordinates($x, $y);
102 3329 $hot{$coordinates} = 1;
103 }
104 }
105 }
106 # change the land
107 69 for my $coordinates (keys %$altitude) {
108 41400 my $change = 0;
109 41400 ▶ if ($hot{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hot{$coordinates}) { } 3329 38071 41400
110 # on the hotspot the land rises
111 3329 ▶ $change = 1 if rand() < 0.2;
Branch: 1 branch
BranchTrueFalseTotal
if rand() < "0.2" 643 2686 3329
112 } else {
113 # off the hotspot the land sinks
114 38071 ▶ $change = -1 if rand() < 0.2;
Branch: 1 branch
BranchTrueFalseTotal
if rand() < "0.2" 7577 30494 38071
115 }
116 41400 ▶ next unless $change;
Branch: 1 branch
BranchTrueFalseTotal
unless $change 33180 8220 41400
117 # rising from the ocean atop the hotspot
118 8220 $altitude->{$coordinates} += $change;
119 8220 ▶ $altitude->{$coordinates} = $self->bottom if $altitude->{$coordinates} < $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} < $self->bottom 7167 1053 8220
120 8220 ▶ $altitude->{$coordinates} = $self->top if $altitude->{$coordinates} > $self->top;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $self->top 0 8220 8220
121 }
122 # land with higher neighbours on the hotspot goes up
123 69 for my $coordinates (keys %hot) {
124 3329 my $change = 0;
125 3329 for my $i ($self->neighbors()) {
126 3364 my ($x, $y) = $self->neighbor($coordinates, $i);
127 3364 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 35 3329 3364
128 3329 my $other = coordinates($x, $y);
129 3329 ▶ $change = 1 if $altitude->{$other} - $altitude->{$coordinates} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} - $altitude->{$coordinates} > 1 117 3212 3329
130 3329 last;
131 }
132 3329 ▶ $altitude->{$coordinates}++ if $change;
Branch: 1 branch
BranchTrueFalseTotal
if $change 117 3212 3329
133 }
134 # note height for debugging purposes
135 69 for my $coordinates (keys %$altitude) {
136 41400 $world->{$coordinates} = "height$altitude->{$coordinates}";
137 }
138 }
139  
140 4 ▶sub forests {
Subroutine forests: called CC=9 SCAR=22.0
Pod undocumented
141 4 my $self = shift;
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} 2134 266 2400
146 266 ▶ next if $world->{$coordinates} =~ /mountain|lake/;
Branch: 1 branch
BranchTrueFalseTotal
if $world->{$coordinates} =~ /mountain|lake/u 61 205 266
147 205 ▶ if ($altitude->{$coordinates} == 1) {
Branch: 5 branches
BranchTrueFalseTotal
if ($altitude->{$coordinates} == 1) { } 146 59 205
elsif ($altitude->{$coordinates} == 2) { } 43 16 59
elsif ($altitude->{$coordinates} == 3) { } 14 2 16
elsif ($altitude->{$coordinates} == 4) { } 1 1 2
elsif ($altitude->{$coordinates} > 4) { } 1 0 1
148 146 $world->{$coordinates} = "light-grey bushes";
149 } elsif ($altitude->{$coordinates} == 2) {
150 43 $world->{$coordinates} = "light-green trees";
151 } elsif ($altitude->{$coordinates} == 3) {
152 14 $world->{$coordinates} = "green forest";
153 } elsif ($altitude->{$coordinates} == 4) {
154 1 $world->{$coordinates} = "dark-green forest";
155 } elsif ($altitude->{$coordinates} > 4) {
156 1 $world->{$coordinates} = "dark-green mountains";
157 }
158 }
159 }
160  
161 4 ▶sub lakes {
Subroutine lakes: called CC=6 SCAR=17.9
Pod undocumented
162 4 my $self = shift;
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 2966 my ($x, $y) = $self->neighbor($coordinates, $i);
169 2966 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 185 2781 2966
170 2781 my $other = coordinates($x, $y);
171 2781 ▶ next HEX if $altitude->{$other} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} == 0 2354 427 2781
172 427 ▶ next HEX if $altitude->{$coordinates} > $altitude->{$other};
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$coordinates} > $altitude->{$other} 31 396 427
173 }
174 15 $world->{$coordinates} = "green lake";
175 }
176 }
177  
178 4 ▶sub islands {
Subroutine islands: called CC=6 SCAR=17.9
Pod undocumented
179 4 my $self = shift;
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 2134 266 2400
185 266 for my $i ($self->neighbors()) {
186 657 my ($x, $y) = $self->neighbor($coordinates, $i);
187 657 ▶ next unless $self->legal($x, $y);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->legal($x, $y) 5 652 657
188 652 my $other = coordinates($x, $y);
189 652 ▶ next HEX if $altitude->{$other} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $altitude->{$other} > 0 218 434 652
190 }
191 48 $world->{$coordinates} = "water mountains";
192 }
193 }
194  
195 4 ▶sub generate {
Subroutine generate: called CC=4 SCAR=13.9
Pod undocumented
196 4 my $self = shift;
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 4 ▶sub generate_map {
Subroutine generate_map: called CC=11 SCAR=26.9
Pod undocumented
227 4 my $self = shift;
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;
Condition100%2/2
shift() || time
l!l
22
MC/DC100%1/1
shift() || time
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
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/Smale.pm ↑ Summary blib/lib/Game/TextMapper/Point/Square.pm »