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

filter: Statementstmt 25.2% 27 / 107 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 33.3% 5 / 15 Podpod 0.0% 0 / 5 totaltotal 16.2% 32 / 197 CC 30 SCAR 44.4
CC
30
Cov
17%
generate_map
210.0
ocean
56.0
eruption
42.0
CRAP 84.4
« blib/lib/Game/TextMapper/Mapper.pm ↑ Summary blib/lib/Game/TextMapper/Folkesten.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::Archipelago - work in progress
21  
22 =head1 DESCRIPTION
23  
24 This is an unfinished idea.
25  
26 =cut
27  
28 package Game::TextMapper::Schroeder::Archipelago;
29 11 ▶use Game::TextMapper::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 11 with 'Game::TextMapper::Schroeder::Base';
34 11 ▶use List::Util qw'shuffle min max';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 11 my $log = Game::TextMapper::Log->get;
37  
38 11 has 'bottom' => 0;
39 11 has 'top' => 10;
40 11 has 'radius' => 5;
41 11 has 'width' => 30;
42 11 has 'height' => 10;
43 11 has 'concentration' => 0.1;
44 11 has 'eruptions' => 0.03;
45 0 ▶has 'world' => sub { { } };
Subroutine __ANON__: not called CC=1 SCAR=1.2
46 0 ▶has 'altitude' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
47  
48 0 ▶sub flat {
Subroutine flat: not called CC=3 SCAR=24.8
Pod undocumented
49 0 my $self = shift;
50 0 $log->debug("initializing altitude map");
51 # initialize the altitude map; this is required so that we have a list of
52 # legal hex coordinates somewhere
53 0 for my $y (1 .. $self->height) {
54 0 for my $x (1 .. $self->width) {
55 0 my $coordinates = coordinates($x, $y);
56 0 $self->altitude->{$coordinates} = 0;
57 0 $self->world->{$coordinates} = "height0";
58 }
59 }
60 }
61  
62 0 ▶sub ocean {
Subroutine ocean: not called CC=7 SCAR=40.3
Pod undocumented
63 0 my $self = shift;
64 0 $log->debug("placing ocean and water");
65 0 for my $coordinates (sort keys %{$self->altitude}) {
66 0 ▶ if ($self->altitude->{$coordinates} <= $self->bottom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->altitude->{$coordinates} <= $self->bottom) 0 0 0
67 0 my $ocean = 1;
68 0 for my $i ($self->neighbors()) {
69 0 my ($x, $y) = $self->neighbor($coordinates, $i);
70 0 my $legal = $self->legal($x, $y);
71 0 my $other = coordinates($x, $y);
72 0 ▶ next if not $legal or $self->altitude->{$other} <= $self->bottom;
Branch: 1 branch
BranchTrueFalseTotal
if not $legal or $self->altitude->{$other} <= $self->bottom 0 0 0
Condition0%0/3
not $legal or $self->altitude->{$other} <= $self->bottom
l!l&&r!l&&!r
000
MC/DC0%0/2
not $legal or $self->altitude->{$other} <= $self->bottom
not $legal $self->altitude->{$other} <= $self->bottom
Truth table0%0/3
not(A) or B
A= not $legalB= $self->altitude->{$other} <= $self->bottom
ABresult
1X1
011
000
73 0 $ocean = 0;
74 }
75 0 ▶ $self->world->{$coordinates} = $ocean ? "ocean" : "water";
Branch: 1 branch
BranchTrueFalseTotal
$ocean ? : 0 0 0
76 }
77 }
78 }
79  
80 0 ▶sub eruption {
Subroutine eruption: not called CC=6 SCAR=37.4
Pod undocumented
81 0 my $self = shift;
82 0 my $cx = int $self->width * rand();
83 0 my $cy = int $self->height * (rand() + rand()) / 2;
84 0 $log->debug("eruption at " . $self->coordinates($cx, $cy));
85 0 my $top = 1 + int($self->top * $cx / $self->width);
86 0 ▶ $top-- if $top > 2 and rand() < 0.6;
Branch: 1 branch
BranchTrueFalseTotal
if $top > 2 and rand() < "0.6" 0 0 0
Condition0%0/3
$top > 2 and rand() < "0.6"
!ll&&!rl&&r
000
MC/DC0%0/2
$top > 2 and rand() < "0.6"
$top > 2 rand() < "0.6"
Truth table0%0/3
A and B
A= $top > 2B= rand() < "0.6"
ABresult
0X0
100
111
87 0 for my $coordinates (keys %{$self->altitude}) {
88 0 my $d = $self->distance($self->xy($coordinates), $cx, $cy);
89 0 ▶ if ($d <= $top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d <= $top) 0 0 0
90 0 my $h = $top - $d;
91 0 ▶ $self->altitude->{$coordinates} = $h if $h > $self->altitude->{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $h > $self->altitude->{$coordinates} 0 0 0
92 0 $self->world->{$coordinates} = "height" . $self->altitude->{$coordinates};
93 }
94 }
95 }
96  
97 0 ▶sub generate {
Subroutine generate: not called CC=4 SCAR=30.0
Pod undocumented
98 0 my $self = shift;
99 0 my $step = shift;
100 0 ▶ my @code = (sub { $self->flat() });
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 for (1 .. $self->width * $self->height * $self->eruptions) {
102 0 ▶ push(@code, sub { $self->eruption() });
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 }
104 0 ▶ push(@code, sub { $self->ocean() });
Subroutine __ANON__: not called CC=1 SCAR=6.9
105  
106 # $step 0 runs all the code; note that we can't simply cache those results
107 # because we need to start over with the same seed!
108 0 my $i = 1;
109 0 ▶ while (@code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@code) 0 0 0
110 0 shift(@code)->();
111 0 ▶ return if $step == $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $step == $i++ 0 0 0
112 }
113 }
114  
115 0 ▶sub generate_map {
Subroutine generate_map: not called CC=14 SCAR=53.5
Pod undocumented
116 0 my $self = shift;
117 # If provided, the arguments override the defaults
118 0 ▶ $self->width(shift // $self->width);
Condition0%0/3
shift() // $self->width
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->width
shift() $self->width
Truth table0%0/3
A // B
A= shift()B= $self->width
ABresult
1X1
011
000
119 0 ▶ $self->height(shift // $self->height);
Condition0%0/3
shift() // $self->height
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->height
shift() $self->height
Truth table0%0/3
A // B
A= shift()B= $self->height
ABresult
1X1
011
000
120 0 ▶ $self->concentration(shift // $self->concentration);
Condition0%0/3
shift() // $self->concentration
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->concentration
shift() $self->concentration
Truth table0%0/3
A // B
A= shift()B= $self->concentration
ABresult
1X1
011
000
121 0 ▶ $self->eruptions(shift // $self->eruptions);
Condition0%0/3
shift() // $self->eruptions
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->eruptions
shift() $self->eruptions
Truth table0%0/3
A // B
A= shift()B= $self->eruptions
ABresult
1X1
011
000
122 0 ▶ $self->top(shift // $self->top);
Condition0%0/3
shift() // $self->top
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->top
shift() $self->top
Truth table0%0/3
A // B
A= shift()B= $self->top
ABresult
1X1
011
000
123 0 ▶ $self->bottom(shift // $self->bottom);
Condition0%0/3
shift() // $self->bottom
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->bottom
shift() $self->bottom
Truth table0%0/3
A // B
A= shift()B= $self->bottom
ABresult
1X1
011
000
124 0 ▶ my $seed = shift||time;
Condition0%0/2
shift() || time
l!l
00
MC/DC0%0/1
shift() || time
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
125 0 my $url = shift;
126 0 ▶ my $step = shift||0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
127  
128 # For documentation purposes, I want to be able to set the pseudo-random
129 # number seed using srand and rely on rand to reproduce the same sequence of
130 # pseudo-random numbers for the same seed. The key point to remember is that
131 # the keys function will return keys in random order. So if we loop over the
132 # result of keys, we need to look at the code in the loop: If order is
133 # important, that wont do. We need to sort the keys. If we want the keys to be
134 # pseudo-shuffled, use shuffle sort keys.
135 0 srand($seed);
136  
137 # keys for all hashes are coordinates such as "0101".
138 0 $self->generate($step);
139  
140 # when documenting or debugging, do this before collecting lines
141 0 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 0 0
142 # add a height label at the very end
143 0 ▶ if ($step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step) 0 0 0
144 0 for my $coordinates (keys %{$self->altitude}) {
145 0 $self->world->{$coordinates} .= ' "' . $self->altitude->{$coordinates} . '"';
146 }
147 }
148 }
149  
150 0 local $" = "-"; # list items separated by -
151 0 my @lines;
152 0 push(@lines, map { $_ . " " . $self->world->{$_} } sort keys %{$self->world});
153 # push(@lines, map { "$_ trail" } @$trails);
154 0 push(@lines, "include gnomeyland.txt");
155  
156 # when documenting or debugging, add some more lines at the end
157 0 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) 0 0 0
158 # visualize height
159 push(@lines,
160 map {
161 0 my $n = int(255 / $self->top * $_);
162 0 qq{height$_ attributes fill="rgb($n,$n,$n)"};
163 } (0 .. $self->top));
164 # visualize water flow
165 0 push(@lines, $self->arrows());
166 }
167  
168 0 push(@lines, "# Seed: $seed");
169 0 ▶ push(@lines, "# Documentation: " . $url) if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
170 0 my $map = join("\n", @lines);
171 0 return $map;
172 }
173  
174 11 1;
« blib/lib/Game/TextMapper/Mapper.pm ↑ Summary blib/lib/Game/TextMapper/Folkesten.pm »