blib/lib/Game/TextMapper/Smale.pm

filter: Statementstmt 81.9% 136 / 166 Branchbran 66.6% 44 / 66 Conditioncond 62.9% 39 / 62 MC/DCmcdc 31.0% 9 / 29 Subroutinesub 93.7% 15 / 16 Podpod 8.3% 1 / 12 totaltotal 69.5% 244 / 351 CC 66 SCAR 27.9
CC
66
Cov
74%
agriculture
50.6
populate_region
21.8
generate_map
21.5
CRAP 16.3
« blib/lib/Game/TextMapper/Schroeder/Alpine.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::Smale - generate fantasy wilderness maps
21  
22 =head1 SYNOPSIS
23  
24 my $text = Game::TextMapper::Smale->new
25 ->generate_map($width, $height, $bw);
26  
27 =head1 DESCRIPTION
28  
29 This generates a wilderness map based on the algorithm by Erin D. Smale. See the
30 blog posts at L<http://www.welshpiper.com/hex-based-campaign-design-part-1/> and
31 L<http://www.welshpiper.com/hex-based-campaign-design-part-2/> for more
32 information.
33  
34 Generally speaking, the idea is that the algorithm picks a random terrain for a
35 hex in the middle of the map. Based on that, the surrounding hexes a bit further
36 away are picked, and finally the remaining hexes are picked. This is why the
37 maps vary so drastically in terrain distribution.
38  
39 =head1 METHODS
40  
41 Note that this module acts as a class with the C<generate_map> method, but none
42 of the other subroutines defined are actual methods. They don't take a C<$self>
43 argument.
44  
45 =cut
46  
47 package Game::TextMapper::Smale;
48 11 ▶use Game::TextMapper::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use Game::TextMapper::Point;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 11 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 11 my $log = Game::TextMapper::Log->get;
54  
55 11 my %world = ();
56  
57 # ATLAS HEX PRIMARY TERRAIN TYPE
58 # Water Swamp Desert Plains Forest Hills Mountains
59 # Water P W W W W W -
60 # Swamp W P - W W - -
61 # Desert W - P W - W W
62 # Plains S [1] S T P [4] S T -
63 # Forest T [2] T - S P [5] W [8] T [11]
64 # Hills W - S [3] T T [6] P [9] S
65 # Mountns - - W - W [7] S [10] P [12]
66 #
67 # 1. Treat as coastal (beach or scrub) if adjacent to water
68 # 2. 66% light forest
69 # 3. 33% rocky desert or high sand dunes
70 # 4. Treat as farmland in settled hexes
71 # 5. 33% heavy forest
72 # 6. 66% forested hills
73 # 7. 66% forested mountains
74 # 8. 33% forested hills
75 # 9. 20% canyon or fissure (not implemented)
76 # 10. 40% chance of a pass (not implemented)
77 # 11. 33% forested mountains
78 # 12. 20% chance of a dominating peak; 10% chance of a mountain pass (not
79 # implemented); 5% volcano (not implemented)
80 #
81 # Notes
82 # water: water
83 # sand: sand or dust
84 # swamp: dark-grey swamp (near trees) or dark-grey marshes (no trees)
85 # plains: light-green grass, bush or bushes near water or forest
86 # forest: green trees (light), green forest, dark-green forest (heavy);
87 # use firs and fir-forest near hills or mountains
88 # hill: light-grey hill, dust hill if sand dunes
89 # mountain: grey mountain, grey mountains (peak)
90  
91 # later, grass land near a settlement might get the colors soil or dark-soil!
92  
93 11 my %primary = ("water" => ["water"],
94 "swamp" => ["dark-grey swamp"],
95 "desert" => ["dust desert"],
96 "plains" => ["light-green grass"],
97 "forest" => ["green forest",
98 "green forest",
99 "dark-green fir-forest"],
100 "hill" => ["light-grey hill"],
101 "mountain" => ["grey mountain",
102 "grey mountain",
103 "grey mountain",
104 "grey mountain",
105 "grey mountains"]);
106  
107 11 my %secondary = ("water" => ["light-green grass",
108 "light-green bush",
109 "light-green bushes"],
110 "swamp" => ["light-green grass"],
111 "desert" => ["light-grey hill",
112 "light-grey hill",
113 "dust hill"],
114 "plains" => ["green forest"],
115 "forest" => ["light-green grass",
116 "light-green bush"],
117 "hill" => ["grey mountain"],
118 "mountain" => ["light-grey hill"]);
119  
120 11 my %tertiary = ("water" => ["green forest",
121 "green trees",
122 "green trees"],
123 "swamp" => ["green forest"],
124 "desert" => ["light-green grass"],
125 "plains" => ["light-grey hill"],
126 "forest" => ["light-grey forest-hill",
127 "light-grey forest-hill",
128 "light-grey hill"],
129 "hill" => ["light-green grass"],
130 "mountain" => ["green fir-forest",
131 "green forest",
132 "green forest-mountains"]);
133  
134 11 my %wildcard = ("water" => ["dark-grey swamp",
135 "dark-grey marsh",
136 "sand desert",
137 "dust desert",
138 "light-grey hill",
139 "light-grey forest-hill"],
140 "swamp" => ["water"],
141 "desert" => ["water",
142 "grey mountain"],
143 "plains" => ["water",
144 "dark-grey swamp",
145 "dust desert"],
146 "forest" => ["water",
147 "water",
148 "water",
149 "dark-grey swamp",
150 "dark-grey swamp",
151 "dark-grey marsh",
152 "grey mountain",
153 "grey forest-mountain",
154 "grey forest-mountains"],
155 "hill" => ["water",
156 "water",
157 "water",
158 "sand desert",
159 "sand desert",
160 "dust desert",
161 "green forest",
162 "green forest",
163 "green forest-hill"],
164 "mountain" => ["sand desert",
165 "dust desert"]);
166  
167  
168 11 my %reverse_lookup = (
169 # primary
170 "water" => "water",
171 "dark-grey swamp" => "swamp",
172 "dust desert" => "desert",
173 "light-green grass" => "plains",
174 "green forest" => "forest",
175 "dark-green fir-forest" => "forest",
176 "light-grey hill" => "hill",
177 "grey mountain" => "mountain",
178 "grey mountains" => "mountain",
179 # secondary
180 "light-green bush" => "plains",
181 "light-green bushes" => "plains",
182 "dust hill" => "hill",
183 # tertiary
184 "green trees" => "forest",
185 "light-grey forest-hill" => "hill",
186 "green fir-forest" => "forest",
187 "green forest-mountains" => "forest",
188 # wildcard
189 "dark-grey marsh" => "swamp",
190 "sand desert" => "desert",
191 "grey forest-mountain" => "mountain",
192 "grey forest-mountains" => "mountain",
193 "green forest-hill" => "forest",
194 # code
195 "light-soil fields" => "plains",
196 "soil fields" => "plains",
197 );
198  
199 11 my %encounters = ("settlement" => ["thorp", "thorp", "thorp", "thorp",
200 "village",
201 "town", "town",
202 "large-town",
203 "city"],
204 "fortress" => ["keep", "tower", "castle"],
205 "religious" => ["shrine", "law", "chaos"],
206 "ruin" => [],
207 "monster" => [],
208 "natural" => []);
209  
210 11 my @needs_fields;
211  
212 sub one {
213 2828 ▶ my @arr = @_;
Subroutine one: called CC=3 SCAR=11.0
Pod undocumented
214 2828 ▶ @arr = @{$arr[0]} if @arr == 1 and ref $arr[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if @arr == 1 and ref $arr[0] eq "ARRAY" 1236 1592 2828
Condition66%2/3
@arr == 1 and ref $arr[0] eq "ARRAY"
!ll&&!rl&&r
159201236
MC/DC50%1/2
@arr == 1 and ref $arr[0] eq "ARRAY"
@arr == 1 ref $arr[0] eq "ARRAY"
Truth table66%2/3
A and B
A= @arr == 1B= ref $arr[0] eq "ARRAY"
ABresult
0X0
100
111
215 2828 return $arr[int(rand(scalar @arr))];
216 }
217  
218 sub member {
219 0 ▶ my $element = shift;
Subroutine member: not called CC=3 SCAR=24.8
Pod undocumented
220 0 foreach (@_) {
221 0 ▶ return 1 if $element eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $element eq $_ 0 0 0
222 }
223 }
224  
225 sub place_major {
226 8 ▶ my ($x, $y, $encounter) = @_;
Subroutine place_major: called CC=7 SCAR=24.4
Pod undocumented
227 8 my $thing = one(@{$encounters{$encounter}});
228 8 ▶ return unless $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless $thing 5 3 8
229 3 $log->debug("placing $thing ($encounter) at ($x,$y)");
230 3 my $hex = one(full_hexes($x, $y));
231 3 $x += $hex->[0];
232 3 $y += $hex->[1];
233 3 my $coordinates = Game::TextMapper::Point::coord($x, $y);
234 3 ▶ my $primary = $reverse_lookup{$world{$coordinates}} || "";
Condition50%1/2
$reverse_lookup{$world{$coordinates}} || ''
l!l
30
MC/DC0%0/1
$reverse_lookup{$world{$coordinates}} || ''
$reverse_lookup{$world{$coordinates}}
Truth table50%1/2
A
A= $reverse_lookup{$world{$coordinates}}
Aresult
11
00
235 3 my ($color, $terrain) = split(' ', $world{$coordinates}, 2);
236 3 ▶ if ($encounter eq 'settlement') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encounter eq "settlement") 0 3 3
237 0 ▶ if ($primary eq 'plains') {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary eq "plains") 0 0 0
238 0 $color = one('light-soil', 'soil');
239 0 $log->debug(" " . $world{$coordinates} . " is $primary and was changed to $color");
240 }
241 0 ▶ if ($primary ne 'plains' or member($thing, 'large-town', 'city')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primary ne "plains" or member($thing, "large-town", "city")) 0 0 0
Condition0%0/3
$primary ne "plains" or member($thing, "large-town", "city")
l!l&&r!l&&!r
000
MC/DC0%0/2
$primary ne "plains" or member($thing, "large-town", "city")
$primary ne "plains" member($thing, "large-town", "city")
Truth table0%0/3
A or B
A= $primary ne "plains"B= member($thing, "large-town", "city")
ABresult
1X1
011
000
242 0 push(@needs_fields, [$x, $y]);
243 }
244 }
245 # ignore $terrain for the moment and replace it with $thing
246 3 $world{$coordinates} = "$color $thing";
247 }
248  
249 sub populate_region {
250 45 ▶ my ($hex, $primary) = @_;
Subroutine populate_region: called CC=15 SCAR=30.8
Pod undocumented
251 45 my $random = rand 100;
252 45 ▶ if ($primary eq 'water' and $random < 10
Branch: 1 branch
BranchTrueFalseTotal
if ($primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20 or $primary eq "grass" and $random < 60 or $primary eq "forest" and $random < 40 or $primary eq "hill" and $random < 40 or $primary eq "mountain" and $random < 20) 8 37 45
Condition64%25/39
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20 or $primary eq "grass" and $random < 60 or $primary eq "forest" and $random < 40 or $primary eq "hill" and $random < 40 or $primary eq "mountain" and $random < 20
l!l&&r!l&&!r
82314
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20 or $primary eq "grass" and $random < 60 or $primary eq "forest" and $random < 40 or $primary eq "hill" and $random < 40
l!l&&r!l&&!r
4437
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20 or $primary eq "grass" and $random < 60 or $primary eq "forest" and $random < 40
l!l&&r!l&&!r
0441
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20 or $primary eq "grass" and $random < 60
l!l&&r!l&&!r
0045
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20
l!l&&r!l&&!r
0045
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20
l!l&&r!l&&!r
0045
$primary eq "water" and $random < 10
!ll&&!rl&&r
4140
$primary eq "swamp" and $random < 20
!ll&&!rl&&r
4320
$primary eq "sand" and $random < 20
!ll&&!rl&&r
4500
$primary eq "grass" and $random < 60
!ll&&!rl&&r
4500
$primary eq "forest" and $random < 40
!ll&&!rl&&r
3474
$primary eq "hill" and $random < 40
!ll&&!rl&&r
3074
$primary eq "mountain" and $random < 20
!ll&&!rl&&r
2980
MC/DC14%2/14
$primary eq "water" and $random < 10 or $primary eq "swamp" and $random < 20 or $primary eq "sand" and $random < 20 or $primary eq "grass" and $random < 60 or $primary eq "forest" and $random < 40 or $primary eq "hill" and $random < 40 or $primary eq "mountain" and $random < 20
$primary eq "water" $random < 10 $primary eq "swamp" $random < 20 $primary eq "sand" $random < 20 $primary eq "grass" $random < 60 $primary eq "forest" $random < 40 $primary eq "hill" $random < 40 $primary eq "mountain" $random < 20
Truth table1%3/255
A and B or C and D or E and F or G and H or I and J or K and L or M and N
A= $primary eq "water"B= $random < 10C= $primary eq "swamp"D= $random < 20E= $primary eq "sand"F= $random < 20G= $primary eq "grass"H= $random < 60I= $primary eq "forest"J= $random < 40K= $primary eq "hill"L= $random < 40M= $primary eq "mountain"N= $random < 20
ABCDEFGHIJKLMNresult
11XXXXXXXXXXXX1
0X11XXXXXXXXXX1
1011XXXXXXXXXX1
0X0X11XXXXXXXX1
0X1011XXXXXXXX1
100X11XXXXXXXX1
101011XXXXXXXX1
0X0X0X11XXXXXX1
0X0X1011XXXXXX1
0X100X11XXXXXX1
0X101011XXXXXX1
100X0X11XXXXXX1
100X1011XXXXXX1
10100X11XXXXXX1
10101011XXXXXX1
0X0X0X0X11XXXX1
0X0X0X1011XXXX1
0X0X100X11XXXX1
0X0X101011XXXX1
0X100X0X11XXXX1
0X100X1011XXXX1
0X10100X11XXXX1
0X10101011XXXX1
100X0X0X11XXXX1
100X0X1011XXXX1
100X100X11XXXX1
100X101011XXXX1
10100X0X11XXXX1
10100X1011XXXX1
1010100X11XXXX1
1010101011XXXX1
0X0X0X0X0X11XX1
0X0X0X0X1011XX1
0X0X0X100X11XX1
0X0X0X101011XX1
0X0X100X0X11XX1
0X0X100X1011XX1
0X0X10100X11XX1
0X0X10101011XX1
0X100X0X0X11XX1
0X100X0X1011XX1
0X100X100X11XX1
0X100X101011XX1
0X10100X0X11XX1
0X10100X1011XX1
0X1010100X11XX1
0X1010101011XX1
100X0X0X0X11XX1
100X0X0X1011XX1
100X0X100X11XX1
100X0X101011XX1
100X100X0X11XX1
100X100X1011XX1
100X10100X11XX1
100X10101011XX1
10100X0X0X11XX1
10100X0X1011XX1
10100X100X11XX1
10100X101011XX1
1010100X0X11XX1
1010100X1011XX1
101010100X11XX1
101010101011XX1
0X0X0X0X0X0X111
0X0X0X0X0X10111
0X0X0X0X100X111
0X0X0X0X1010111
0X0X0X100X0X111
0X0X0X100X10111
0X0X0X10100X111
0X0X0X101010111
0X0X100X0X0X111
0X0X100X0X10111
0X0X100X100X111
0X0X100X1010111
0X0X10100X0X111
0X0X10100X10111
0X0X1010100X111
0X0X10101010111
0X100X0X0X0X111
0X100X0X0X10111
0X100X0X100X111
0X100X0X1010111
0X100X100X0X111
0X100X100X10111
0X100X10100X111
0X100X101010111
0X10100X0X0X111
0X10100X0X10111
0X10100X100X111
0X10100X1010111
0X1010100X0X111
0X1010100X10111
0X101010100X111
0X1010101010111
100X0X0X0X0X111
100X0X0X0X10111
100X0X0X100X111
100X0X0X1010111
100X0X100X0X111
100X0X100X10111
100X0X10100X111
100X0X101010111
100X100X0X0X111
100X100X0X10111
100X100X100X111
100X100X1010111
100X10100X0X111
100X10100X10111
100X1010100X111
100X10101010111
10100X0X0X0X111
10100X0X0X10111
10100X0X100X111
10100X0X1010111
10100X100X0X111
10100X100X10111
10100X10100X111
10100X101010111
1010100X0X0X111
1010100X0X10111
1010100X100X111
1010100X1010111
101010100X0X111
101010100X10111
10101010100X111
101010101010111
0X0X0X0X0X0X0X0
0X0X0X0X0X0X100
0X0X0X0X0X100X0
0X0X0X0X0X10100
0X0X0X0X100X0X0
0X0X0X0X100X100
0X0X0X0X10100X0
0X0X0X0X1010100
0X0X0X100X0X0X0
0X0X0X100X0X100
0X0X0X100X100X0
0X0X0X100X10100
0X0X0X10100X0X0
0X0X0X10100X100
0X0X0X1010100X0
0X0X0X101010100
0X0X100X0X0X0X0
0X0X100X0X0X100
0X0X100X0X100X0
0X0X100X0X10100
0X0X100X100X0X0
0X0X100X100X100
0X0X100X10100X0
0X0X100X1010100
0X0X10100X0X0X0
0X0X10100X0X100
0X0X10100X100X0
0X0X10100X10100
0X0X1010100X0X0
0X0X1010100X100
0X0X101010100X0
0X0X10101010100
0X100X0X0X0X0X0
0X100X0X0X0X100
0X100X0X0X100X0
0X100X0X0X10100
0X100X0X100X0X0
0X100X0X100X100
0X100X0X10100X0
0X100X0X1010100
0X100X100X0X0X0
0X100X100X0X100
0X100X100X100X0
0X100X100X10100
0X100X10100X0X0
0X100X10100X100
0X100X1010100X0
0X100X101010100
0X10100X0X0X0X0
0X10100X0X0X100
0X10100X0X100X0
0X10100X0X10100
0X10100X100X0X0
0X10100X100X100
0X10100X10100X0
0X10100X1010100
0X1010100X0X0X0
0X1010100X0X100
0X1010100X100X0
0X1010100X10100
0X101010100X0X0
0X101010100X100
0X10101010100X0
0X1010101010100
100X0X0X0X0X0X0
100X0X0X0X0X100
100X0X0X0X100X0
100X0X0X0X10100
100X0X0X100X0X0
100X0X0X100X100
100X0X0X10100X0
100X0X0X1010100
100X0X100X0X0X0
100X0X100X0X100
100X0X100X100X0
100X0X100X10100
100X0X10100X0X0
100X0X10100X100
100X0X1010100X0
100X0X101010100
100X100X0X0X0X0
100X100X0X0X100
100X100X0X100X0
100X100X0X10100
100X100X100X0X0
100X100X100X100
100X100X10100X0
100X100X1010100
100X10100X0X0X0
100X10100X0X100
100X10100X100X0
100X10100X10100
100X1010100X0X0
100X1010100X100
100X101010100X0
100X10101010100
10100X0X0X0X0X0
10100X0X0X0X100
10100X0X0X100X0
10100X0X0X10100
10100X0X100X0X0
10100X0X100X100
10100X0X10100X0
10100X0X1010100
10100X100X0X0X0
10100X100X0X100
10100X100X100X0
10100X100X10100
10100X10100X0X0
10100X10100X100
10100X1010100X0
10100X101010100
1010100X0X0X0X0
1010100X0X0X100
1010100X0X100X0
1010100X0X10100
1010100X100X0X0
1010100X100X100
1010100X10100X0
1010100X1010100
101010100X0X0X0
101010100X0X100
101010100X100X0
101010100X10100
10101010100X0X0
10101010100X100
1010101010100X0
101010101010100
253 or $primary eq 'swamp' and $random < 20
254 or $primary eq 'sand' and $random < 20
255 or $primary eq 'grass' and $random < 60
256 or $primary eq 'forest' and $random < 40
257 or $primary eq 'hill' and $random < 40
258 or $primary eq 'mountain' and $random < 20) {
259 8 place_major($hex->[0], $hex->[1], one(keys %encounters));
260 }
261 }
262  
263 # Brute forcing by picking random sub hexes until we found an
264 # unassigned one.
265  
266 sub pick_unassigned {
267 675 ▶ my ($x, $y, @region) = @_;
Subroutine pick_unassigned: called CC=2 SCAR=6.9
Pod undocumented
268 675 my $hex = one(@region);
269 675 my $coordinates = Game::TextMapper::Point::coord($x + $hex->[0], $y + $hex->[1]);
270 675 ▶ while ($world{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($world{$coordinates}) 895 675 1570
271 895 $hex = one(@region);
272 895 $coordinates = Game::TextMapper::Point::coord($x + $hex->[0], $y + $hex->[1]);
273 }
274 675 return $coordinates;
275 }
276  
277 sub pick_remaining {
278 90 ▶ my ($x, $y, @region) = @_;
Subroutine pick_remaining: called CC=3 SCAR=11.0
Pod undocumented
279 90 my @coordinates = ();
280 90 for my $hex (@region) {
281 1395 my $coordinates = Game::TextMapper::Point::coord($x + $hex->[0], $y + $hex->[1]);
282 1395 ▶ push(@coordinates, $coordinates) unless $world{$coordinates};
Branch: 1 branch
BranchTrueFalseTotal
unless $world{$coordinates} 471 924 1395
283 }
284 90 return @coordinates;
285 }
286  
287 # Precomputed for speed
288  
289 sub full_hexes {
290 48 ▶ my ($x, $y) = @_;
Subroutine full_hexes: called CC=2 SCAR=6.9
Pod undocumented
291 48 ▶ if ($x % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 2) { } 28 20 48
292 28 return ([0, -2],
293 [-2, -1], [-1, -1], [0, -1], [1, -1], [2, -1],
294 [-2, 0], [-1, 0], [0, 0], [1, 0], [2, 0],
295 [-2, 1], [-1, 1], [0, 1], [1, 1], [2, 1],
296 [-1, 2], [0, 2], [1, 2]);
297 } else {
298 20 return ([-1, -2], [0, -2], [1, -2],
299 [-2, -1], [-1, -1], [0, -1], [1, -1], [2, -1],
300 [-2, 0], [-1, 0], [0, 0], [1, 0], [2, 0],
301 [-2, 1], [-1, 1], [0, 1], [1, 1], [2, 1],
302 [0, 2]);
303 }
304 }
305  
306 sub half_hexes {
307 45 ▶ my ($x, $y) = @_;
Subroutine half_hexes: called CC=2 SCAR=6.9
Pod undocumented
308 45 ▶ if ($x % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x % 2) { } 27 18 45
309 27 return ([-2, -2], [-1, -2], [1, -2], [2, -2],
310 [-3, 0], [3, 0],
311 [-3, 1], [3, 1],
312 [-2, 2], [2, 2],
313 [-1, 3], [1, 3]);
314 } else {
315 18 return ([-1, -3], [1, -3],
316 [-2, -2], [2, -2],
317 [-3, -1], [3, -1],
318 [-3, 0], [3, 0],
319 [-2, 2], [-1, 2], [1, 2], [2, 2]);
320 }
321 }
322  
323 sub generate_region {
324 45 ▶ my ($x, $y, $primary) = @_;
Subroutine generate_region: called CC=8 SCAR=20.8
Pod undocumented
325 45 $world{Game::TextMapper::Point::coord($x, $y)} = one($primary{$primary});
326  
327 45 my @region = full_hexes($x, $y);
328 45 my $terrain;
329  
330 45 for (1..9) {
331 405 my $coordinates = pick_unassigned($x, $y, @region);
332 405 $terrain = one($primary{$primary});
333 405 $log->debug(" primary $coordinates => $terrain");
334 405 $world{$coordinates} = $terrain;
335 }
336  
337 45 for (1..6) {
338 270 my $coordinates = pick_unassigned($x, $y, @region);
339 270 $terrain = one($secondary{$primary});
340 270 $log->debug(" secondary $coordinates => $terrain");
341 270 $world{$coordinates} = $terrain;
342 }
343  
344 45 for my $coordinates (pick_remaining($x, $y, @region)) {
345 99 ▶ if (rand > 0.1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand > 0.1) { } 90 9 99
346 90 $terrain = one($tertiary{$primary});
347 90 $log->debug(" tertiary $coordinates => $terrain");
348 } else {
349 9 $terrain = one($wildcard{$primary});
350 9 $log->debug(" wildcard $coordinates => $terrain");
351 }
352 99 $world{$coordinates} = $terrain;
353 }
354  
355 45 for my $coordinates (pick_remaining($x, $y, half_hexes($x, $y))) {
356 372 my $random = rand 6;
357 372 ▶ if ($random < 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($random < 3) { } 190 182 372
elsif ($random < 5) { } 118 64 182
358 190 $terrain = one($primary{$primary});
359 190 $log->debug(" halfhex primary $coordinates => $terrain");
360 } elsif ($random < 5) {
361 118 $terrain = one($secondary{$primary});
362 118 $log->debug(" halfhex secondary $coordinates => $terrain");
363 } else {
364 64 $terrain = one($tertiary{$primary});
365 64 $log->debug(" halfhex tertiary $coordinates => $terrain");
366 }
367 372 $world{$coordinates} = $terrain;
368 }
369 }
370  
371 sub seed_region {
372 3 ▶ my ($seeds, $terrain) = @_;
Subroutine seed_region: called CC=9 SCAR=22.0
Pod undocumented
373 3 my $terrain_above;
374 3 for my $hex (@$seeds) {
375 45 $log->debug("seed_region (" . $hex->[0] . "," . $hex->[1] . ") with $terrain");
376 45 generate_region($hex->[0], $hex->[1], $terrain);
377 45 populate_region($hex, $terrain);
378 45 my $random = rand 12;
379 # pick next terrain based on the previous one (to the left); or the one
380 # above if in the first column
381 45 my $next;
382 45 ▶ $terrain = $terrain_above if $hex->[0] == 1 and $terrain_above;
Branch: 1 branch
BranchTrueFalseTotal
if $hex->[0] == 1 and $terrain_above 6 39 45
Condition100%3/3
$hex->[0] == 1 and $terrain_above
!ll&&!rl&&r
3636
MC/DC100%2/2
$hex->[0] == 1 and $terrain_above
$hex->[0] == 1 $terrain_above
Truth table100%3/3
A and B
A= $hex->[0] == 1B= $terrain_above
ABresult
0X0
100
111
383 45 ▶ if ($random < 6) {
Branch: 3 branches
BranchTrueFalseTotal
if ($random < 6) { } 21 24 45
elsif ($random < 9) { } 11 13 24
elsif ($random < 11) { } 9 4 13
384 21 $next = one($primary{$terrain});
385 21 $log->debug("picked primary $next");
386 } elsif ($random < 9) {
387 11 $next = one($secondary{$terrain});
388 11 $log->debug("picked secondary $next");
389 } elsif ($random < 11) {
390 9 $next = one($tertiary{$terrain});
391 9 $log->debug("picked tertiary $next");
392 } else {
393 4 $next = one($wildcard{$terrain});
394 4 $log->debug("picked wildcard $next");
395 }
396 45 ▶ $terrain_above = $terrain if $hex->[0] == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $hex->[0] == 1 9 36 45
397 45 ▶ die "Terrain lacks reverse_lookup: $next\n" unless $reverse_lookup{$next};
Branch: 1 branch
BranchTrueFalseTotal
unless $reverse_lookup{$next} 0 45 45
398 45 $terrain = $reverse_lookup{$next};
399 }
400 }
401  
402 sub agriculture {
403 3 ▶ for my $hex (@needs_fields) {
Subroutine agriculture: called CC=7 SCAR=39.2
Pod undocumented
404 0 $log->debug("looking to plant fields near " . Game::TextMapper::Point::coord($hex->[0], $hex->[1]));
405 0 my $delta = [[[-1, 0], [ 0, -1], [+1, 0], [+1, +1], [ 0, +1], [-1, +1]], # x is even
406 [[-1, -1], [ 0, -1], [+1, -1], [+1, 0], [ 0, +1], [-1, 0]]]; # x is odd
407 0 my @plains;
408 0 for my $i (0 .. 5) {
409 0 my ($x, $y) = ($hex->[0] + $delta->[$hex->[0] % 2]->[$i]->[0],
410 $hex->[1] + $delta->[$hex->[0] % 2]->[$i]->[1]);
411 0 my $coordinates = Game::TextMapper::Point::coord($x, $y);
412 0 ▶ if ($world{$coordinates}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($world{$coordinates}) 0 0 0
413 0 my ($color, $terrain) = split(' ', $world{$coordinates}, 2);
414 0 ▶ next unless $reverse_lookup{$world{$coordinates}};
Branch: 1 branch
BranchTrueFalseTotal
unless $reverse_lookup{$world{$coordinates}} 0 0 0
415 0 $log->debug(" $coordinates is " . $world{$coordinates} . " ie. " . $reverse_lookup{$world{$coordinates}});
416 0 ▶ if ($reverse_lookup{$world{$coordinates}} eq 'plains') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse_lookup{$world{$coordinates}} eq "plains") 0 0 0
417 0 $log->debug(" $coordinates is a candidate");
418 0 push(@plains, $coordinates);
419 }
420 }
421 }
422 0 ▶ next unless @plains;
Branch: 1 branch
BranchTrueFalseTotal
unless @plains 0 0 0
423 0 my $target = one(@plains);
424 0 $world{$target} = one('light-soil fields', 'soil fields');
425 0 $log->debug(" $target planted with " . $world{$target});
426 }
427 }
428  
429 =head2 generate_map WIDTH, HEIGHT, BW
430  
431 WIDTH and HEIGHT default to 20×10.
432  
433 BW stands for "black & white", i.e. a true value skips background colours.
434  
435 =cut
436  
437 sub generate_map {
438 3 ▶ my ($self, $width, $height, $bw) = @_;
Subroutine generate_map: called CC=16 SCAR=30.7
Pod documented
439 3 ▶ $width = 20 if not defined $width or $width < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $width or $width < 1 3 0 3
Condition33%1/3
not defined $width or $width < 1
l!l&&r!l&&!r
300
MC/DC0%0/2
not defined $width or $width < 1
not defined $width $width < 1
Truth table33%1/3
not(A) or B
A= not defined $widthB= $width < 1
ABresult
1X1
011
000
440 3 ▶ $height = 10 if not defined $height or $height < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $height or $height < 1 3 0 3
Condition33%1/3
not defined $height or $height < 1
l!l&&r!l&&!r
300
MC/DC0%0/2
not defined $height or $height < 1
not defined $height $height < 1
Truth table33%1/3
not(A) or B
A= not defined $heightB= $height < 1
ABresult
1X1
011
000
441 3 my $seeds;
442 3 ▶ for (my $y = 1; $y < $height + 3; $y += 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $height + 3) 9 3 12
443 9 ▶ for (my $x = 1; $x < $width + 3; $x += 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $width + 3) 45 9 54
444 # [1,1] [6,3], [11,1], [16,3]
445 45 my $y0 = $y + int(($x % 10) / 3);
446 45 push(@$seeds, [$x, $y0]);
447 }
448 }
449  
450 3 %world = (); # reinitialize!
451  
452 3 my @seed_terrain = keys %primary;
453 3 seed_region($seeds, one(@seed_terrain));
454 3 agriculture();
455  
456 # delete extra hexes we generated to fill the gaps
457 3 for my $coordinates (keys %world) {
458 1191 ▶ $coordinates =~ /(-?\d\d)(-?\d\d)/ or $coordinates =~ /(-?\d\d+)\.(-?\d\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
$coordinates =~ /(-?\d\d)(-?\d\d)/u or $coordinates =~ /(-?\d\d+)\.(-?\d\d+)/u 0 1191 1191
459 1191 ▶ delete $world{$coordinates} if $1 < 1 or $2 < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 < 1 or $2 < 1 201 990 1191
Condition100%3/3
$1 < 1 or $2 < 1
l!l&&r!l&&!r
11190990
MC/DC100%2/2
$1 < 1 or $2 < 1
$1 < 1 $2 < 1
Truth table100%3/3
A or B
A= $1 < 1B= $2 < 1
ABresult
1X1
011
000
460 1191 ▶ delete $world{$coordinates} if $1 > $width or $2 > $height;
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $width or $2 > $height 435 756 1191
Condition100%3/3
$1 > $width or $2 > $height
l!l&&r!l&&!r
156279756
MC/DC100%2/2
$1 > $width or $2 > $height
$1 > $width $2 > $height
Truth table100%3/3
A or B
A= $1 > $widthB= $2 > $height
ABresult
1X1
011
000
461 }
462 3 ▶ if ($bw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bw) 0 3 3
463 0 for my $coordinates (keys %world) {
464 0 my ($color, $rest) = split(' ', $world{$coordinates}, 2);
465 0 ▶ if ($rest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest) { } 0 0 0
466 0 $world{$coordinates} = $rest;
467 } else {
468 0 delete $world{$coordinates};
469 }
470 }
471 }
472  
473 3 return join("\n", map { $_ . " " . $world{$_} } sort keys %world) . "\n"
474 . "include gnomeyland.txt\n";
475 }
476  
477 =head1 SEE ALSO
478  
479 Erin D. Smale described this algorithm in two famous blog posts:
480 L<http://www.welshpiper.com/hex-based-campaign-design-part-1/> and
481 L<http://www.welshpiper.com/hex-based-campaign-design-part-2/>.
482  
483 The map itself uses the I<Gnomeyland> icons by Gregory B. MacKenzie. These are
484 licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
485 To view a copy of this license, visit
486 L<http://creativecommons.org/licenses/by-sa/3.0/>.
487  
488 =cut
489  
490 11 1;
« blib/lib/Game/TextMapper/Schroeder/Alpine.pm ↑ Summary blib/lib/Game/TextMapper/Folkesten.pm »