lib/Traveller/Mapper.pm

filter: Statementstmt 8.3% 24 / 286 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 57 Subroutinesub 19.2% 5 / 26 Podpod 0.0% 0 / 20 totaltotal 5.0% 29 / 575 CC 108 SCAR 55.1
CC
108
Cov
5%
trade
552.0
initialize_map
462.0
minimal_spanning_tree
272.0
CRAP 247.1
« lib/Traveller/System/Classic.pm ↑ Summary lib/Traveller/Hex.pm »
1 # Copyright (C) 2009-2021 Alex Schroeder <alex@gnu.org>
2 # Copyright (C) 2020 Christian Carey
3 #
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program. If not, see <http://www.gnu.org/licenses/>.
15 #
16 # Algorithms based on Traveller ©2008 Mongoose Publishing.
17  
18 package Traveller::Mapper;
19 2 ▶use List::Util qw(shuffle reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Traveller::Util qw(nearby distance in);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Traveller::Hex;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶has 'hexes' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 0 ▶has 'routes' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 2 has 'comm_set';
27 2 has 'trade_set';
28 2 has 'source';
29 2 has 'width';
30 2 has 'height';
31  
32 2 my $colour_re = qr/#([0-9a-f]{3}){1,2}/i;
33  
34 2 my $example = q!Inedgeus 0101 D7A5579-8 G Fl Ni A
35 Geaan 0102 E66A999-7 G Hi Wa A
36 Orgemaso 0103 C555875-5 SG Ga Lt
37 Veesso 0105 C5A0369-8 G De Lo A
38 Ticezale 0106 B769799-7 T SG Ri A
39 Maatonte 0107 C6B3544-8 C G Fl Ni A
40 Diesra 0109 D510522-8 SG Ni
41 Esarra 0204 E869100-8 G Lo A
42 Rience 0205 C687267-8 G Ga Lo
43 Rearreso 0208 C655432-5 C G Ga Lt Ni
44 Laisbe 0210 E354663-3 Ag Lt Ni
45 Biveer 0302 C646576-9 C G Ag Ga Ni
46 Labeveri 0303 A796100-9 CT N G Ga Lo A
47 Sotexe 0408 E544778-3 G Ag Ga Lt A
48 Zamala 0409 A544658-13 T N G Ag Ga Ht Ni
49 Sogeeran 0502 A200443-14 CT N G Ht Ni Va
50 Aanbi 0503 E697102-7 G Ga Lo A
51 Bemaat 0504 C643384-9 C R G Lo Po
52 Diare 0505 A254430-11 TRN G Ni A
53 Esgeed 0507 A8B1579-11 RN G Fl Ni A
54 Leonbi 0510 B365789-9 T SG Ag Ri A
55 Reisbeon 0604 C561526-8 R G Ni
56 Atcevein 0605 A231313-11 CT G Lo Po
57 Usmabe 0607 A540A84-15 T G De Hi Ht In Po
58 Onbebior 0608 B220530-10 G De Ni Po A
59 Raraxema 0609 B421768-8 T NSG Na Po
60 Xeerri 0610 C210862-9 G Na
61 Onreon 0702 D8838A9-2 S Lt Ri A
62 Ismave 0703 E272654-4 Lt Ni
63 Lara 0704 C0008D9-5 SG As Lt Na Va A
64 Lalala 0705 C140473-9 R G De Ni Po
65 Maxereis 0707 A55A747-12 CT NSG Ht Wa
66 Requbire 0802 C9B4200-10 G Fl Lo A
67 Azaxe 0804 B6746B9-8 C G Ag Ga Ni A
68 Rieddige 0805 B355578-7 G Ag Ni A
69 Usorce 0806 E736110-3 G Lo Lt A
70 Solacexe 0810 D342635-4 P S Lt Ni Po R
71 !;
72  
73 1 ▶sub example {
Subroutine example: called CC=1 SCAR=0.0
Pod undocumented
74 1 return $example;
75 }
76  
77 # The empty hex is centered around 0,0 and has a side length of 1,
78 # a maximum diameter of 2, and a minimum diameter of √3.
79 2 my @hex = ( -1, 0,
80 -0.5, sqrt(3)/2,
81 0.5, sqrt(3)/2,
82 1, 0,
83 0.5, -sqrt(3)/2,
84 -0.5, -sqrt(3)/2);
85  
86 0 ▶sub header {
Subroutine header: not called CC=3 SCAR=24.8
Pod undocumented
87 0 my ($self, $width, $height) = @_;
88 # TO DO: support an option for North American “A” paper dimensions (width 215.9 mm, length 279.4 mm)
89 0 ▶ $width //= 210;
Condition0%0/2
$width //= 210
l!l
00
MC/DC0%0/1
$width //= 210
$width
Truth table0%0/2
A
A= $width
Aresult
11
00
90 0 ▶ $height //= 297;
Condition0%0/2
$height //= 297
l!l
00
MC/DC0%0/1
$height //= 297
$height
Truth table0%0/2
A
A= $height
Aresult
11
00
91 0 my $template = <<EOT;
92 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
93 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
94 xmlns:xlink="http://www.w3.org/1999/xlink"
95 width="${width}mm"
96 height="${height}mm"
97 viewBox="%s %s %s %s">
98 <desc>Traveller Subsector</desc>
99 <defs>
100 <style type="text/css"><![CDATA[
101 text {
102 font-size: 16pt;
103 font-family: Optima, "Optima Regular", Optima-Regular, Helvetica, sans-serif;
104 text-anchor: middle;
105 }
106 text a {
107 fill: blue;
108 text-decoration: underline;
109 }
110 .coordinates {
111 fill-opacity: 0.5;
112 }
113 .starport, .base {
114 font-size: 20pt;
115 }
116 .direction {
117 font-size: 24pt;
118 }
119 .legend {
120 text-anchor: start;
121 font-size: 14pt;
122 }
123 tspan.comm {
124 fill: #ff6347; /* tomato */
125 }
126 line.comm {
127 stroke-width: 10pt;
128 stroke: #ff6347; /* tomato */
129 }
130 tspan.trade {
131 fill: #afeeee; /* pale turquoise */
132 }
133 line.trade {
134 stroke-width: 6pt;
135 stroke: #afeeee; /* pale turquoise */
136 fill: none;
137 }
138 .travelzone {
139 opacity: 0.3;
140 }
141 .amber {
142 fill: none;
143 stroke-width: 1pt;
144 stroke: black;
145 }
146 .red {
147 fill: red;
148 }
149 #hex {
150 stroke-width: 3pt;
151 fill: none;
152 stroke: black;
153 }
154 #background {
155 fill: inherit;
156 }
157 #bg {
158 fill: inherit;
159 }
160 ]]></style>
161 <polygon id="hex" points="%s,%s %s,%s %s,%s %s,%s %s,%s %s,%s" />
162 <polygon id="bg" points="%s,%s %s,%s %s,%s %s,%s %s,%s %s,%s" />
163 </defs>
164 <rect fill="white" stroke="black" stroke-width="10" id="frame"
165 x="%s" y="%s" width="%s" height="%s" />
166  
167 EOT
168 0 my $scale = 100;
169 return sprintf($template,
170 0 map { sprintf("%.3f", $_ * $scale) }
171 # viewport
172 -0.5, -0.5, 3 + ($self->width - 1) * 1.5, ($self->height + 1.5) * sqrt(3),
173 # empty hex, once for the backgrounds and once for the stroke
174 @hex,
175 @hex,
176 # framing rectangle
177 -0.5, -0.5, 3 + ($self->width - 1) * 1.5, ($self->height + 1.5) * sqrt(3));
178 }
179  
180 0 ▶sub colour {
Subroutine colour: not called CC=2 SCAR=17.9
Pod undocumented
181 0 my $self = shift;
182 0 ▶ my $culture = shift or return "white";
Branch: 1 branch
BranchTrueFalseTotal
my $culture = shift() or return "white" 0 0 0
183 # The same colours result from the same names.
184 0 my @colours = ("#d3d3d3", "#f5f5f5", "#eaeaea", "#fffeb0", "#fff0f5", "#eee0e5", "#ffe1ff",
185 "#eed2ee", "#c6e2ff", "#fdf5e6", "#e0ffff", "#d1eeee", "#c5fff5", "#eeeee0",
186 "#fff68f", "#eee685", "#fffacd", "#eee9bf", "#ffe7ba", "#ffefdb", "#ffe4e1",
187 "#eed5d2", "#e6e6fa", "#f0ffff", "#c5ffd5", "#e6ffe6", "#d5ffc5", "#f5f5dc");
188 0 my $i = unpack("%32W*", lc $culture) % @colours; # checksum
189 0 return $colours[$i];
190 }
191  
192 0 ▶sub background {
Subroutine background: not called CC=2 SCAR=17.9
Pod undocumented
193 0 my $self = shift;
194 0 my $scale = 100;
195 return join("\n", map {
196 0 my $hex = $_;
197 0 my $x = $hex->x;
198 0 my $y = $hex->y;
199 0 ▶ my $c = $hex->colour || $self->colour($hex->culture);
Condition0%0/3
$hex->colour || $self->colour($hex->culture)
l!l&&r!l&&!r
000
MC/DC0%0/2
$hex->colour || $self->colour($hex->culture)
$hex->colour $self->colour($hex->culture)
Truth table0%0/3
A || B
A= $hex->colourB= $self->colour($hex->culture)
ABresult
1X1
011
000
200 0 sprintf(qq{ <use xlink:href="#bg" x="%.3f" y="%.3f" fill="$c"/>},
201 (1 + ($x-1) * 1.5) * $scale,
202 ($y - $x%2/2) * sqrt(3) * $scale);
203 0 } @{$self->hexes});
204 }
205  
206 0 ▶sub grid {
Subroutine grid: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my $self = shift;
208 0 my $scale = 100;
209 0 my $doc;
210 $doc .= join("\n",
211 map {
212 0 my $n = shift;
213 0 my $x = int($_/$self->height+1);
214 0 my $y = $_ % $self->height + 1;
215 0 my $svg = sprintf(qq{ <use xlink:href="#hex" x="%.3f" y="%.3f"/>\n},
216 (1 + ($x-1) * 1.5) * $scale,
217 ($y - $x%2/2) * sqrt(3) * $scale);
218 0 $svg .= sprintf(qq{ <text class="coordinates" x="%.3f" y="%.3f">}
219 . qq{%02d%02d</text>\n},
220 (1 + ($x-1) * 1.5) * $scale,
221 ($y - $x%2/2) * sqrt(3) * $scale - 0.6 * $scale,
222 $x, $y);
223 } (0 .. $self->width * $self->height - 1));
224 0 return $doc;
225 }
226  
227 0 ▶sub legend {
Subroutine legend: not called CC=2 SCAR=17.9
Pod undocumented
228 0 my $self = shift;
229 0 my $scale = 100;
230 0 my $doc;
231 0 my $uwp = '';
232 0 ▶ if ($self->source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->source) 0 0 0
233 0 $uwp = ' – <a xlink:href="' . $self->source . '">UWP</a>';
234 }
235 0 $doc .= sprintf(qq{ <text class="legend" x="%.3f" y="%.3f">◉ gas giant}
236 . qq{ – ■ Imperial consulate – ☼ TAS facility – ▲ scout base}
237 . qq{ – ★ naval base – π research station – ☠ pirate base}
238 . qq{ – <tspan class="comm">▮</tspan> communication}
239 . qq{ – <tspan class="trade">▮</tspan> trade$uwp</text>\n},
240 -10, ($self->height + 1) * sqrt(3) * $scale);
241 0 $doc .= sprintf(qq{ <text class="direction" x="%.3f" y="%.3f">coreward</text>\n},
242 $self->width/2 * 1.5 * $scale, -0.13 * $scale);
243 0 $doc .= sprintf(qq{ <text transform="translate(%.3f,%.3f) rotate(90)"}
244 . qq{ class="direction">trailing</text>\n},
245 ($self->width + 0.4) * 1.5 * $scale, $self->height/2 * sqrt(3) * $scale);
246 0 $doc .= sprintf(qq{ <text class="direction" x="%.3f" y="%.3f">rimward</text>\n},
247 $self->width/2 * 1.5 * $scale, ($self->height + 0.7) * sqrt(3) * $scale);
248 0 $doc .= sprintf(qq{ <text transform="translate(%.3f,%.3f) rotate(-90)"}
249 . qq{ class="direction">spinward</text>\n},
250 -0.1 * $scale, $self->height/2 * sqrt(3) * $scale);
251 0 return $doc;
252 }
253  
254 0 ▶sub footer {
Subroutine footer: not called CC=2 SCAR=17.9
Pod undocumented
255 0 my $self = shift;
256 0 my $doc;
257 0 my $y = 10;
258 0 my $debug = ''; # for developers
259 0 for my $line (split(/\n/, $debug)) {
260 0 $doc .= qq{<text xml:space="preserve" class="legend" y="$y" stroke="red">}
261 . $line . qq{</text>\n};
262 0 $y += 20;
263 }
264 0 $doc .= qq{</svg>\n};
265 0 return $doc;
266 }
267  
268 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
269 0 my ($self, $map, $wiki, $source) = @_;
270 0 $self->source($source);
271 0 $self->width(0);
272 0 $self->height(0);
273 0 my @lines = split(/\n/, $map);
274 0 $self->initialize_map($wiki, \@lines);
275 0 $self->initialize_routes(\@lines);
276 }
277  
278 0 ▶sub initialize_map {
Subroutine initialize_map: not called CC=21 SCAR=61.4
Pod undocumented
279 0 my ($self, $wiki, $lines) = @_;
280 0 foreach (@$lines) {
281 # parse Traveller UWP, with optional name
282 0 my ($name, $x, $y,
283 $starport, $size, $atmosphere, $hydrographic, $population,
284 $government, $law, $tech, $bases, $rest) =
285 /(?:([^>\r\n\t]*?)\s+)?(\d\d)(\d\d)\s+([A-EX])([\dA])([\dA-F])([\dA])([\dA-C])([\dA-F])([\dA-L])-(\d{1,2}|[\dA-HJ-NP-Z])(?:\s+([PCTRNSG ]+)\b)?(.*)/;
286 # alternative super simple name, coordinates, optional size (0-9), optional bases (PCTRNSG), optional travel zones (AR)
287 0 ▶ ($name, $x, $y, $size, $bases, $rest) =
Branch: 1 branch
BranchTrueFalseTotal
unless $x and $y 0 0 0
Condition0%0/3
$x and $y
!ll&&!rl&&r
000
MC/DC0%0/2
$x and $y
$x $y
Truth table0%0/3
A and B
A= $xB= $y
ABresult
0X0
100
111
288 /([^>\r\n\t]*?)\s+(\d\d)(\d\d)(?:\s+(\d)\b)?(?:\s+([PCTRNSG ]+)\b)?(.*)/
289 unless $x and $y;
290 0 ▶ next unless $x and $y;
Branch: 1 branch
BranchTrueFalseTotal
unless $x and $y 0 0 0
Condition0%0/3
$x and $y
!ll&&!rl&&r
000
MC/DC0%0/2
$x and $y
$x $y
Truth table0%0/3
A and B
A= $xB= $y
ABresult
0X0
100
111
291 0 ▶ $self->width($x) if $x > $self->width;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $self->width 0 0 0
292 0 ▶ $self->height($y) if $y > $self->height;
Branch: 1 branch
BranchTrueFalseTotal
if $y > $self->height 0 0 0
293 0 my @tokens = split(' ', $rest);
294 0 my @colours = grep(/^$colour_re$/, @tokens);
295 0 my %trade = map { $_ => 1 } grep(/^[A-Z][A-Za-z]$/, @tokens);
296 0 my ($culture) = grep /^\[.*\]$/, @tokens; # culture in square brackets
297 0 my ($travelzone) = grep /^([AR])$/, @tokens; # amber or red travel zone
298 # avoid uninitialized values warnings in the rest of the code
299 0 ▶ map { $$_ //= '' } (\$size,
Condition0%0/2
$$_ //= ""
l!l
00
MC/DC0%0/1
$$_ //= ""
$$_
Truth table0%0/2
A
A= $$_
Aresult
11
00
300 \$atmosphere,
301 \$hydrographic,
302 \$population,
303 \$government,
304 \$law,
305 \$starport,
306 \$travelzone);
307 # get "hex" values, but accept letters beyond F! (excepting I and O)
308 0 ▶ map { $$_ = $$_ ge 'P' and $$_ le 'Z' ? 23 + ord($$_) - 80
Branch: 4 branches
BranchTrueFalseTotal
$$_ le 'Z' ? : 0 0 0
$$_ le 'N' ? : 0 0 0
$$_ le 'H' ? : 0 0 0
$$_ eq '' ? : 0 0 0
Condition0%0/9
($$_ = $$_ ge 'P') && ($$_ le 'Z' ? 23 + ord($$_) - 80 : $$_ ge 'J') && ($$_ le 'N' ? 18 + ord($$_) - 74 : $$_ ge 'A') && ($$_ le 'H' ? 10 + ord($$_) - 65 : ($$_ eq '' ? 0 : $$_))
!ll&&!rl&&r
000
$$_ = $$_ ge "P" and $$_ le "Z" ? 23 + ord($$_) - 80 : $$_ ge "J" and $$_ le "N" ? 18 + ord($$_) - 74 : $$_ ge "A"
!ll&&!rl&&r
000
$$_ = $$_ ge "P" and $$_ le "Z" ? 23 + ord($$_) - 80 : $$_ ge "J"
!ll&&!rl&&r
000
MC/DC0%0/4
($$_ = $$_ ge 'P') && ($$_ le 'Z' ? 23 + ord($$_) - 80 : $$_ ge 'J') && ($$_ le 'N' ? 18 + ord($$_) - 74 : $$_ ge 'A') && ($$_ le 'H' ? 10 + ord($$_) - 65 : ($$_ eq '' ? 0 : $$_))
$$_ = $$_ ge "P" $$_ le "Z" ? 23 + ord($$_) - 80 : $$_ ge "J" $$_ le "N" ? 18 + ord($$_) - 74 : $$_ ge "A" ($$_ le 'H' ? 10 + ord($$_) - 65 : ($$_ eq '' ? 0 : $$_))
Truth table0%0/5
A and B and C && D
A= $$_ = $$_ ge "P"B= $$_ le "Z" ? 23 + ord($$_) - 80 : $$_ ge "J"C= $$_ le "N" ? 18 + ord($$_) - 74 : $$_ ge "A"D= ($$_ le 'H' ? 10 + ord($$_) - 65 : ($$_ eq '' ? 0 : $$_))
ABCDresult
0XXX0
10XX0
110X0
11100
11111
309 : $$_ ge 'J' and $$_ le 'N' ? 18 + ord($$_) - 74
310 : $$_ ge 'A' and $$_ le 'H' ? 10 + ord($$_) - 65
311 : $$_ eq '' ? 0
312 : $$_ } (\$size,
313 \$atmosphere,
314 \$hydrographic,
315 \$population,
316 \$government,
317 \$law);
318 0 ▶ my $hex = Traveller::Hex->new(
Condition0%0/5
$culture // ""
l!l
00
shift @colours || $self->colour($culture)
l!l&&r!l&&!r
000
MC/DC0%0/1
$culture // ""
$culture
MC/DC0%0/2
shift @colours || $self->colour($culture)
shift @colours $self->colour($culture)
Truth table0%0/2
A
A= $culture
Aresult
11
00
Truth table0%0/3
A || B
A= shift @coloursB= $self->colour($culture)
ABresult
1X1
011
000
319 name => $name,
320 x => $x,
321 y => $y,
322 starport => $starport,
323 population => $population,
324 size => $size,
325 travelzone => $travelzone,
326 trade => \%trade,
327 culture => $culture // '',
328 colour => shift(@colours) || $self->colour($culture));
329 0 ▶ $hex->url("$wiki$name") if $wiki;
Branch: 1 branch
BranchTrueFalseTotal
if $wiki 0 0 0
330 0 ▶ if ($bases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bases) 0 0 0
331 0 for my $base (split(//, $bases)) {
332 0 $hex->base($base);
333 }
334 }
335 0 $self->add($hex);
336 }
337 }
338  
339 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
340 0 my ($self, $hex) = @_;
341 0 push(@{$self->hexes}, $hex);
342 }
343  
344 0 ▶sub initialize_routes {
Subroutine initialize_routes: not called CC=4 SCAR=30.0
Pod undocumented
345 0 my ($self, $lines) = @_;
346 0 foreach (@$lines) {
347 # parse non-standard routes
348 0 my ($from, $to, $type, $colour) = /^(\d\d\d\d)-(\d\d\d\d)\s+(C|T)\b\s*($colour_re)?/i;
349 0 ▶ next unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
350 0 ▶ if (lc($type) eq 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $type eq 'c') { } 0 0 0
351 0 $self->comm_set(1); # at least one hex here has comm
352 0 push(@{$self->at($from)->comm}, $self->at($to)); # a property of the hex
353 } else {
354 0 $self->trade_set(1); # at least one hex here has trade
355 0 my $from_hex = $self->at($from);
356 0 my $to_hex = $self->at($to);
357  
358 0 push(@{$self->routes}, [$from_hex, $to_hex]); # a property of the mapper
359 }
360 }
361 }
362  
363 0 ▶sub at {
Subroutine at: not called CC=4 SCAR=30.0
Pod undocumented
364 0 my ($self, $coord) = @_;
365 0 my ($x, $y) = $coord =~ /(\d\d)(\d\d)/;
366 0 foreach my $hex (@{$self->hexes}) {
367 0 ▶ return $hex if $hex->x == $x and $hex->y == $y;
Branch: 1 branch
BranchTrueFalseTotal
if $hex->x == $x and $hex->y == $y 0 0 0
Condition0%0/3
$hex->x == $x and $hex->y == $y
!ll&&!rl&&r
000
MC/DC0%0/2
$hex->x == $x and $hex->y == $y
$hex->x == $x $hex->y == $y
Truth table0%0/3
A and B
A= $hex->x == $xB= $hex->y == $y
ABresult
0X0
100
111
368 }
369 }
370  
371 0 ▶sub communications {
Subroutine communications: not called CC=14 SCAR=53.5
Pod undocumented
372 # connect all the class A starports, naval bases, and Imperial
373 # consulates
374 0 my ($self) = @_;
375 0 ▶ return if $self->comm_set;
Branch: 1 branch
BranchTrueFalseTotal
if $self->comm_set 0 0 0
376 0 my @candidates = ();
377 0 foreach my $hex (@{$self->hexes}) {
378 0 ▶ push(@candidates, $hex)
Branch: 1 branch
BranchTrueFalseTotal
if $hex->starport eq "A" or $hex->naval or $hex->consulate 0 0 0
Condition0%0/6
$hex->starport eq "A" or $hex->naval or $hex->consulate
l!l&&r!l&&!r
000
$hex->starport eq "A" or $hex->naval
l!l&&r!l&&!r
000
MC/DC0%0/3
$hex->starport eq "A" or $hex->naval or $hex->consulate
$hex->starport eq "A" $hex->naval $hex->consulate
Truth table0%0/4
A or B or C
A= $hex->starport eq "A"B= $hex->navalC= $hex->consulate
ABCresult
1XX1
01X1
0011
0000
379 if $hex->starport eq 'A'
380 or $hex->naval
381 or $hex->consulate;
382 }
383 # every system has a link to its neighbours
384 0 foreach my $hex (@candidates) {
385 0 my @ar = nearby($hex, 2, \@candidates);
386 0 $hex->comm(\@ar);
387 }
388 # eliminate all but the best connections if the system has
389 # amber or red travel zone
390 0 foreach my $hex (@candidates) {
391 0 ▶ next unless $hex->travelzone;
Branch: 1 branch
BranchTrueFalseTotal
unless $hex->travelzone 0 0 0
392 0 my $best;
393 0 foreach my $other (@{$hex->comm}) {
394 0 ▶ if (not $best
Branch: 1 branch
BranchTrueFalseTotal
if (not $best or $other->starport lt $best->starport or $other->starport eq $best->starport and distance($hex, $other) < distance($hex, $best)) 0 0 0
Condition0%0/9
not $best or $other->starport lt $best->starport or $other->starport eq $best->starport and distance($hex, $other) < distance($hex, $best)
l!l&&r!l&&!r
000
not $best or $other->starport lt $best->starport
l!l&&r!l&&!r
000
$other->starport eq $best->starport and distance($hex, $other) < distance($hex, $best)
!ll&&!rl&&r
000
MC/DC0%0/4
not $best or $other->starport lt $best->starport or $other->starport eq $best->starport and distance($hex, $other) < distance($hex, $best)
not $best $other->starport lt $best->starport $other->starport eq $best->starport distance($hex, $other) < distance($hex, $best)
Truth table0%0/5
not(A) or B or C and D
A= not $bestB= $other->starport lt $best->starportC= $other->starport eq $best->starportD= distance($hex, $other) < distance($hex, $best)
ABCDresult
1XXX1
01XX1
00111
000X0
00100
395 or $other->starport lt $best->starport
396 or $other->starport eq $best->starport
397 and distance($hex, $other) < distance($hex, $best)) {
398 0 $best = $other;
399 }
400 }
401 0 $hex->eliminate(grep { $_ != $best } @{$hex->comm});
402 }
403 }
404  
405 0 ▶sub trade {
Subroutine trade: not called CC=23 SCAR=63.1
Pod undocumented
406 # connect In or Ht with As, De, Ic, Ni
407 # connect Hi or Ri with Ag, Ga, Wa
408 0 my ($self) = @_;
409 0 ▶ return if $self->trade_set;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trade_set 0 0 0
410 # candidates need to be on a travel route, i.e. must have fuel
411 # available; skip worlds with a red travel zone
412 0 my @candidates = ();
413 0 foreach my $hex (@{$self->hexes}) {
414 push(@candidates, $hex)
415 if ($hex->starport =~ /^[A-D]$/
416 or $hex->gasgiant
417 or $hex->trade->{Wa})
418 0 ▶ and $hex->travelzone ne 'R';
Branch: 1 branch
BranchTrueFalseTotal
if $hex->starport =~ /^[A-D]$/u || $hex->gasgiant || $hex->trade->{"Wa"} and $hex->travelzone ne "R" 0 0 0
Condition0%0/9
$hex->starport =~ /^[A-D]$/u || $hex->gasgiant || $hex->trade->{"Wa"} and $hex->travelzone ne "R"
!ll&&!rl&&r
000
$hex->starport =~ /^[A-D]$/u or $hex->gasgiant or $hex->trade->{"Wa"}
l!l&&r!l&&!r
000
$hex->starport =~ /^[A-D]$/u or $hex->gasgiant
l!l&&r!l&&!r
000
MC/DC0%0/4
$hex->starport =~ /^[A-D]$/u || $hex->gasgiant || $hex->trade->{"Wa"} and $hex->travelzone ne "R"
$hex->starport =~ /^[A-D]$/u $hex->gasgiant $hex->trade->{"Wa"} $hex->travelzone ne "R"
Truth table0%0/7
A or B or C and D
A= $hex->starport =~ /^[A-D]$/uB= $hex->gasgiantC= $hex->trade->{"Wa"}D= $hex->travelzone ne "R"
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
419 }
420 # every system has a link to its partners
421 0 foreach my $hex (@candidates) {
422 0 my @routes;
423 0 ▶ if ($hex->trade->{In} or $hex->trade->{Ht}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hex->trade->{'In'} or $hex->trade->{'Ht'}) { } 0 0 0
elsif ($hex->trade->{'Hi'} or $hex->trade->{'Ri'}) { } 0 0 0
Condition0%0/3
$hex->trade->{"In"} or $hex->trade->{"Ht"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hex->trade->{"In"} or $hex->trade->{"Ht"}
$hex->trade->{"In"} $hex->trade->{"Ht"}
Truth table0%0/3
A or B
A= $hex->trade->{"In"}B= $hex->trade->{"Ht"}
ABresult
1X1
011
000
424 0 foreach my $other (nearby($hex, 4, \@candidates)) {
425 0 ▶ if ($other->trade->{As}
Branch: 1 branch
BranchTrueFalseTotal
if ($other->trade->{"As"} or $other->trade->{"De"} or $other->trade->{"Ic"} or $other->trade->{"Ni"}) 0 0 0
Condition0%0/9
$other->trade->{"As"} or $other->trade->{"De"} or $other->trade->{"Ic"} or $other->trade->{"Ni"}
l!l&&r!l&&!r
000
$other->trade->{"As"} or $other->trade->{"De"} or $other->trade->{"Ic"}
l!l&&r!l&&!r
000
$other->trade->{"As"} or $other->trade->{"De"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$other->trade->{"As"} or $other->trade->{"De"} or $other->trade->{"Ic"} or $other->trade->{"Ni"}
$other->trade->{"As"} $other->trade->{"De"} $other->trade->{"Ic"} $other->trade->{"Ni"}
Truth table0%0/5
A or B or C or D
A= $other->trade->{"As"}B= $other->trade->{"De"}C= $other->trade->{"Ic"}D= $other->trade->{"Ni"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
426 or $other->trade->{De}
427 or $other->trade->{Ic}
428 or $other->trade->{Ni}) {
429 0 my @route = $self->route($hex, $other, 4, \@candidates);
430 0 ▶ push(@routes, \@route) if @route;
Branch: 1 branch
BranchTrueFalseTotal
if @route 0 0 0
431 }
432 }
433 ▶ } elsif ($hex->trade->{Hi} or $hex->trade->{Ri}) {
Condition0%0/3
$hex->trade->{"Hi"} or $hex->trade->{"Ri"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hex->trade->{"Hi"} or $hex->trade->{"Ri"}
$hex->trade->{"Hi"} $hex->trade->{"Ri"}
Truth table0%0/3
A or B
A= $hex->trade->{"Hi"}B= $hex->trade->{"Ri"}
ABresult
1X1
011
000
434 0 foreach my $other (nearby($hex, 4, \@candidates)) {
435 0 ▶ if ($other->trade->{Ag}
Branch: 1 branch
BranchTrueFalseTotal
if ($other->trade->{"Ag"} or $other->trade->{"Ga"} or $other->trade->{"Wa"}) 0 0 0
Condition0%0/6
$other->trade->{"Ag"} or $other->trade->{"Ga"} or $other->trade->{"Wa"}
l!l&&r!l&&!r
000
$other->trade->{"Ag"} or $other->trade->{"Ga"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$other->trade->{"Ag"} or $other->trade->{"Ga"} or $other->trade->{"Wa"}
$other->trade->{"Ag"} $other->trade->{"Ga"} $other->trade->{"Wa"}
Truth table0%0/4
A or B or C
A= $other->trade->{"Ag"}B= $other->trade->{"Ga"}C= $other->trade->{"Wa"}
ABCresult
1XX1
01X1
0011
0000
436 or $other->trade->{Ga}
437 or $other->trade->{Wa}) {
438 0 my @route = $self->route($hex, $other, 4, \@candidates);
439 0 ▶ push(@routes, \@route) if @route;
Branch: 1 branch
BranchTrueFalseTotal
if @route 0 0 0
440 }
441 }
442 }
443 0 $hex->routes(\@routes);
444 }
445 0 $self->routes($self->minimal_spanning_tree($self->edges(@candidates)));
446 }
447  
448 0 ▶sub edges {
Subroutine edges: not called CC=6 SCAR=37.4
Pod undocumented
449 0 my $self = shift;
450 0 my @edges;
451 my %seen;
452 0 foreach my $hex (@_) {
453 0 foreach my $route (@{$hex->routes}) {
454 0 my ($start, @route) = @{$route};
455 0 foreach my $end (@route) {
456 # keep everything unidirectional
457 0 ▶ next if exists $seen{$start}{$end} or exists $seen{$end}{$start};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$start}{$end} or exists $seen{$end}{$start} 0 0 0
Condition0%0/3
exists $seen{$start}{$end} or exists $seen{$end}{$start}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $seen{$start}{$end} or exists $seen{$end}{$start}
exists $seen{$start}{$end} exists $seen{$end}{$start}
Truth table0%0/3
A or B
A= exists $seen{$start}{$end}B= exists $seen{$end}{$start}
ABresult
1X1
011
000
458 0 push(@edges, [$start, $end, distance($start,$end)]);
459 0 $seen{$start}{$end} = 1;
460 0 $start = $end;
461 }
462 }
463 }
464 0 return @edges;
465 }
466  
467 0 ▶sub minimal_spanning_tree {
Subroutine minimal_spanning_tree: not called CC=16 SCAR=56.1
Pod undocumented
468 # http://en.wikipedia.org/wiki/Kruskal%27s_algorithm
469 0 my $self = shift;
470 # Initialize a priority queue Q to contain all edges in G, using the
471 # weights as keys.
472 0 my @Q = sort { @{$a}[2] <=> @{$b}[2] } @_;
473 # Define a forest T ← Ø; T will ultimately contain the edges of the MST
474 0 my @T;
475 # Define an elementary cluster C(v) ← {v}.
476 my %C;
477 0 my $id;
478 0 foreach my $edge (@Q) {
479 # edge u,v is the minimum weighted route from u to v
480 0 my ($u, $v) = @{$edge};
481 # $u = $u->name;
482 # $v = $v->name;
483 # prevent cycles in T; add u,v only if T does not already contain
484 # a path between u and v; also silence warnings
485 0 ▶ if (not $C{$u} or not $C{$v} or $C{$u} != $C{$v}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $C{$u} && $C{$v} or $C{$u} != $C{$v}) 0 0 0
Condition0%0/6
not $C{$u} && $C{$v} or $C{$u} != $C{$v}
l!l&&r!l&&!r
000
$C{$u} && $C{$v}
!ll&&!rl&&r
000
MC/DC0%0/3
not $C{$u} && $C{$v} or $C{$u} != $C{$v}
$C{$u} $C{$v} $C{$u} != $C{$v}
Truth table0%0/4
not(A && B) or C
A= $C{$u}B= $C{$v}C= $C{$u} != $C{$v}
ABCresult
0XX1
10X1
1111
1100
486 # Add edge (v,u) to T.
487 0 push(@T, $edge);
488 # Merge C(v) and C(u) into one cluster, that is, union C(v) and C(u).
489 0 ▶ if ($C{$u} and $C{$v}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($C{$u} and $C{$v}) { } 0 0 0
elsif ($C{$v} and not $C{$u}) { } 0 0 0
elsif ($C{$u} and not $C{$v}) { } 0 0 0
elsif (not $C{$u} || $C{$v}) { } 0 0 0
Condition0%0/3
$C{$u} and $C{$v}
!ll&&!rl&&r
000
MC/DC0%0/2
$C{$u} and $C{$v}
$C{$u} $C{$v}
Truth table0%0/3
A and B
A= $C{$u}B= $C{$v}
ABresult
0X0
100
111
490 0 my @group;
491 0 foreach (keys %C) {
492 0 ▶ push(@group, $_) if $C{$_} == $C{$v};
Branch: 1 branch
BranchTrueFalseTotal
if $C{$_} == $C{$v} 0 0 0
493 }
494 0 $C{$_} = $C{$u} foreach @group;
495 ▶ } elsif ($C{$v} and not $C{$u}) {
Condition0%0/3
$C{$v} and not $C{$u}
!ll&&!rl&&r
000
MC/DC0%0/2
$C{$v} and not $C{$u}
$C{$v} not $C{$u}
Truth table0%0/3
A and not(B)
A= $C{$v}B= not $C{$u}
ABresult
0X0
100
111
496 0 $C{$u} = $C{$v};
497 ▶ } elsif ($C{$u} and not $C{$v}) {
Condition0%0/3
$C{$u} and not $C{$v}
!ll&&!rl&&r
000
MC/DC0%0/2
$C{$u} and not $C{$v}
$C{$u} not $C{$v}
Truth table0%0/3
A and not(B)
A= $C{$u}B= not $C{$v}
ABresult
0X0
100
111
498 0 $C{$v} = $C{$u};
499 ▶ } elsif (not $C{$u} and not $C{$v}) {
Condition0%0/3
$C{$u} || $C{$v}
l!l&&r!l&&!r
000
MC/DC0%0/2
$C{$u} || $C{$v}
$C{$u} $C{$v}
Truth table0%0/3
A || B
A= $C{$u}B= $C{$v}
ABresult
1X1
011
000
500 0 $C{$v} = $C{$u} = ++$id;
501 }
502 }
503 }
504 0 return \@T;
505 }
506  
507 0 ▶sub route {
Subroutine route: not called CC=13 SCAR=52.0
Pod undocumented
508 # Compute the shortest route between two hexes no longer than a
509 # certain distance and choosing intermediary steps from the array of
510 # possible candidates.
511 0 my ($self, $from, $to, $distance, $candidatesref, @seen) = @_;
512 # my $indent = ' ' x (4-$distance);
513 0 my @options;
514 0 ▶ foreach my $hex (nearby($from, $distance < 2 ? $distance : 2, $candidatesref)) {
Branch: 1 branch
BranchTrueFalseTotal
$distance < 2 ? : 0 0 0
515 0 ▶ push (@options, $hex) unless in($hex, @seen);
Branch: 1 branch
BranchTrueFalseTotal
unless in($hex, @seen) 0 0 0
516 }
517 0 ▶ return unless @options and $distance;
Branch: 1 branch
BranchTrueFalseTotal
unless @options and $distance 0 0 0
Condition0%0/3
@options and $distance
!ll&&!rl&&r
000
MC/DC0%0/2
@options and $distance
@options $distance
Truth table0%0/3
A and B
A= @optionsB= $distance
ABresult
0X0
100
111
518 0 ▶ if (in($to, @options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (in($to, @options)) 0 0 0
519 0 return @seen, $from, $to;
520 }
521 0 my @routes;
522 0 foreach my $hex (@options) {
523 0 my @route = $self->route($hex, $to, $distance - distance($from, $hex),
524 $candidatesref, @seen, $from);
525 0 ▶ if (@route) {
Branch: 1 branch
BranchTrueFalseTotal
if (@route) 0 0 0
526 0 push(@routes, \@route);
527 }
528 }
529 0 ▶ return unless @routes;
Branch: 1 branch
BranchTrueFalseTotal
unless @routes 0 0 0
530 # return the shortest one
531 0 my @shortest;
532 0 foreach my $route (@routes) {
533 0 ▶ if ($#{$route} < $#shortest or not @shortest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$route;} < $#shortest or not @shortest) 0 0 0
Condition0%0/3
$#{$route;} < $#shortest or not @shortest
l!l&&r!l&&!r
000
MC/DC0%0/2
$#{$route;} < $#shortest or not @shortest
$#{$route;} < $#shortest not @shortest
Truth table0%0/3
A or not(B)
A= $#{$route;} < $#shortestB= not @shortest
ABresult
1X1
011
000
534 0 @shortest = @{$route};
535 }
536 }
537 0 return @shortest;
538 }
539  
540 0 ▶sub trade_svg {
Subroutine trade_svg: not called CC=2 SCAR=17.9
Pod undocumented
541 0 my $self = shift;
542 0 my $data = '';
543 0 my $scale = 100;
544 0 foreach my $edge (@{$self->routes}) {
545 0 my $u = @{$edge}[0];
546 0 my $v = @{$edge}[1];
547 0 my ($x1, $y1) = ($u->x, $u->y);
548 0 my ($x2, $y2) = ($v->x, $v->y);
549 0 $data .= sprintf(qq{ <line class="trade" x1="%.3f" y1="%.3f" x2="%.3f" y2="%.3f" />\n},
550 (1 + ($x1-1) * 1.5) * $scale, ($y1 - $x1%2/2) * sqrt(3) * $scale,
551 (1 + ($x2-1) * 1.5) * $scale, ($y2 - $x2%2/2) * sqrt(3) * $scale);
552 }
553 0 return $data;
554 }
555  
556 0 ▶sub svg {
Subroutine svg: not called CC=3 SCAR=24.8
Pod undocumented
557 0 my ($self, $width, $height) = @_;
558 0 my $data = $self->header($width, $height);
559 0 $data .= qq{ <g id='background'>\n};
560 0 $data .= $self->background;
561 0 $data .= qq{ </g>\n\n};
562 0 $data .= qq{ <g id='comm'>\n};
563 0 foreach my $hex (@{$self->hexes}) {
564 0 $data .= $hex->comm_svg();
565 }
566 0 $data .= qq{ </g>\n\n};
567 0 $data .= qq{ <g id='routes'>\n};
568 0 $data .= $self->trade_svg();
569 0 $data .= qq{ </g>\n\n};
570 0 $data .= qq{ <g id='grid'>\n};
571 0 $data .= $self->grid;
572 0 $data .= qq{ </g>\n\n};
573 0 $data .= qq{ <g id='legend'>\n};
574 0 $data .= $self->legend();
575 0 $data .= qq{ </g>\n\n};
576 0 $data .= qq{ <g id='system'>\n};
577 0 foreach my $hex (@{$self->hexes}) {
578 0 $data .= $hex->system_svg();
579 }
580 0 $data .= qq{ </g>\n};
581 0 $data .= $self->footer();
582 0 return $data;
583 }
584  
585 0 ▶sub text {
Subroutine text: not called CC=6 SCAR=37.4
Pod undocumented
586 0 my ($self) = @_;
587 0 my $data = "Trade Routes:\n";
588 0 foreach my $edge (@{$self->routes}) {
589 0 my $u = @{$edge}[0];
590 0 my $v = @{$edge}[1];
591 0 $data .= $u->name . " - " . $v->name . "\n";
592 }
593 0 $data .= "\n";
594 0 $data .= "Raw Data:\n";
595 0 foreach my $hex (@{$self->hexes}) {
596 0 foreach my $routeref (@{$hex->routes}) {
597 0 $data .= join(' - ', map {$_->name} @{$routeref}) . "\n";
598 }
599 }
600 0 $data .= "\n";
601 0 $data .= "Communications:\n";
602 0 foreach my $hex (@{$self->hexes}) {
603 0 foreach my $comm (@{$hex->comm}) {
604 0 $data .= $hex->name . " - " . $comm->name . "\n";;
605 }
606 }
607 0 return $data;
608 }
609  
610 2 1;
« lib/Traveller/System/Classic.pm ↑ Summary lib/Traveller/Hex.pm »