lib/Traveller/Hex.pm

filter: Statementstmt 29.7% 25 / 84 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 10.0% 1 / 10 Podpod 0.0% 0 / 6 totaltotal 16.6% 26 / 156 CC 28 SCAR 52.1
CC
28
Cov
18%
system_svg
342.0
base
72.0
at
6.0
CRAP 183.1
« lib/Traveller/Mapper.pm ↑ Summary lib/Traveller/System.pm »
1 #!/usr/bin/perl
2 # Copyright (C) 2009-2021 Alex Schroeder <alex@gnu.org>
3 # Copyright (C) 2020 Christian Carey
4 #
5 # This program is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # this program. If not, see <http://www.gnu.org/licenses/>.
16  
17 package Traveller::Hex;
18 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 has 'name';
21 2 has 'x';
22 2 has 'y';
23 2 has 'starport';
24 2 has 'size';
25 2 has 'population';
26 2 has 'consulate';
27 2 has 'pirate';
28 2 has 'TAS';
29 2 has 'research';
30 2 has 'naval';
31 2 has 'scout';
32 2 has 'gasgiant';
33 2 has 'travelzone';
34 2 has 'url';
35 2 has 'map';
36 0 ▶has 'comm' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
37 0 ▶has 'trade' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
38 0 ▶has 'routes' => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
39 2 has 'culture';
40 2 has 'colour';
41  
42 0 ▶sub base {
Subroutine base: not called CC=8 SCAR=42.8
Pod undocumented
43 0 my ($self, $key) = @_;
44 0 $key = uc($key);
45 0 ▶ ($key eq 'C') ? $self->consulate(1)
Branch: 7 branches
BranchTrueFalseTotal
$key eq 'C' ? : 0 0 0
$key eq 'P' ? : 0 0 0
$key eq 'T' ? : 0 0 0
$key eq 'R' ? : 0 0 0
$key eq 'N' ? : 0 0 0
$key eq 'S' ? : 0 0 0
$key eq 'G' ? : 0 0 0
46 : ($key eq 'P') ? $self->pirate(1)
47 : ($key eq 'T') ? $self->TAS(1)
48 : ($key eq 'R') ? $self->research(1)
49 : ($key eq 'N') ? $self->naval(1)
50 : ($key eq 'S') ? $self->scout(1)
51 : ($key eq 'G') ? $self->gasgiant(1)
52 : undef;
53 }
54  
55 0 ▶sub at {
Subroutine at: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my ($self, $x, $y) = @_;
57 0 ▶ return $self->x == $x && $self->y == $y;
Condition0%0/3
$self->x == $x && $self->y == $y
!ll&&!rl&&r
000
MC/DC0%0/2
$self->x == $x && $self->y == $y
$self->x == $x $self->y == $y
Truth table0%0/3
A && B
A= $self->x == $xB= $self->y == $y
ABresult
0X0
100
111
58 }
59  
60 0 ▶sub str {
Subroutine str: not called CC=1 SCAR=6.9
Pod undocumented
61 0 my $self = shift;
62 0 sprintf "%-12s %02s%02s ", $self->name, $self->x, $self->y;
63 }
64  
65 0 ▶sub eliminate {
Subroutine eliminate: not called CC=2 SCAR=17.9
Pod undocumented
66 0 my $from = shift;
67 0 foreach my $to (@_) {
68 # eliminate the communication $from -> $to
69 0 my @ar1 = grep {$_ != $to} @{$from->comm};
70 0 $from->comm(\@ar1);
71 # eliminate the communication $to -> $from
72 0 my @ar2 = grep {$_ != $from} @{$to->comm};
73 0 $to->comm(\@ar2);
74 }
75 }
76  
77 0 ▶sub comm_svg {
Subroutine comm_svg: not called CC=2 SCAR=17.9
Pod undocumented
78 0 my $self = shift;
79 0 my $data = '';
80 0 my $scale = 100;
81 0 my ($x1, $y1) = ($self->x, $self->y);
82 0 foreach my $to (@{$self->comm}) {
83 0 my ($x2, $y2) = ($to->x, $to->y);
84 0 $data .= sprintf(qq{ <line class="comm" x1="%.3f" y1="%.3f" x2="%.3f" y2="%.3f" />\n},
85 (1 + ($x1-1) * 1.5) * $scale, ($y1 - $x1%2/2) * sqrt(3) * $scale,
86 (1 + ($x2-1) * 1.5) * $scale, ($y2 - $x2%2/2) * sqrt(3) * $scale);
87 }
88 0 return $data;
89 }
90  
91 # The empty hex is centered around 0,0 and has a side length of 1, a
92 # maximum diameter of 2, and a minimum diameter of √3. The subsector
93 # is 10 hexes high and eight hexes wide. The 0101 corner is at the top
94 # left.
95 0 ▶sub system_svg {
Subroutine system_svg: not called CC=18 SCAR=58.3
Pod undocumented
96 0 my $self = shift;
97 0 my $x = $self->x;
98 0 my $y = $self->y;
99 0 my $name = $self->name;
100 0 ▶ my $display = ($self->population >= 9 ? uc($name) : $name);
Branch: 1 branch
BranchTrueFalseTotal
$self->population >= 9 ? : 0 0 0
101 0 my $starport = $self->starport;
102 0 my $size = $self->size;
103 0 my $url = $self->url;
104 0 ▶ my $lead = ($url ? ' ' : '');
Branch: 1 branch
BranchTrueFalseTotal
$url ? : 0 0 0
105 0 my $data = '';
106 0 ▶ $data .= qq{ <a xlink:href="$url">\n} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
107 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
108 0 $data .= qq{$lead <g id="$name">\n};
109 } else {
110 0 $data .= qq{$lead <g>\n};
111 }
112 0 my $scale = 100;
113 # travel zone red painted first, so it appears at the bottom
114 0 ▶ $data .= sprintf(qq{$lead <circle class="travelzone red" cx="%.3f" cy="%.3f" r="%.3f" />\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->travelzone eq "R" 0 0 0
115 (1 + ($x-1) * 1.5) * $scale,
116 ($y - $x%2/2) * sqrt(3) * $scale, 0.52 * $scale)
117 if $self->travelzone eq 'R';
118 0 ▶ $data .= sprintf(qq{$lead <circle cx="%.3f" cy="%.3f" r="%.3f" />\n},
Branch: 1 branch
BranchTrueFalseTotal
if $name or $starport ne "X" 0 0 0
Condition0%0/3
$name or $starport ne "X"
l!l&&r!l&&!r
000
MC/DC0%0/2
$name or $starport ne "X"
$name $starport ne "X"
Truth table0%0/3
A or B
A= $nameB= $starport ne "X"
ABresult
1X1
011
000
119 (1 + ($x-1) * 1.5) * $scale,
120 ($y - $x%2/2) * sqrt(3) * $scale, 11 + $size)
121 if $name or $starport ne 'X';
122 0 ▶ $data .= sprintf(qq{$lead <circle class="travelzone amber" cx="%.3f" cy="%.3f" r="%.3f" />\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->travelzone eq "A" 0 0 0
123 (1 + ($x-1) * 1.5) * $scale,
124 ($y - $x%2/2) * sqrt(3) * $scale, 0.52 * $scale)
125 if $self->travelzone eq 'A';
126 0 ▶ $data .= sprintf(qq{$lead <text class="starport" x="%.3f" y="%.3f">$starport</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
127 (1 + ($x-1) * 1.5) * $scale,
128 ($y - $x%2/2 - 0.17) * sqrt(3) * $scale)
129 if $name;
130 0 $data .= sprintf(qq{$lead <text class="name" x="%.3f" y="%.3f">$display</text>\n},
131 (1 + ($x-1) * 1.5) * $scale,
132 ($y - $x%2/2 + 0.4) * sqrt(3) * $scale);
133 0 ▶ $data .= sprintf(qq{$lead <text class="consulate base" x="%.3f" y="%.3f">■</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->consulate 0 0 0
134 (0.6 + ($x-1) * 1.5) * $scale,
135 ($y - $x%2/2 + 0.25) * sqrt(3) * $scale)
136 if $self->consulate;
137 0 ▶ $data .= sprintf(qq{$lead <text class="TAS base" x="%.3f" y="%.3f">☼</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->TAS 0 0 0
138 (0.4 + ($x-1) * 1.5) * $scale,
139 ($y - $x%2/2 + 0.1) * sqrt(3) * $scale)
140 if $self->TAS;
141 0 ▶ $data .= sprintf(qq{$lead <text class="scout base" x="%.3f" y="%.3f">▲</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->scout 0 0 0
142 (0.4 + ($x-1) * 1.5) * $scale,
143 ($y - $x%2/2 - 0.1) * sqrt(3) * $scale)
144 if $self->scout;
145 0 ▶ $data .= sprintf(qq{$lead <text class="naval base" x="%.3f" y="%.3f">★</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->naval 0 0 0
146 (0.6 + ($x-1) * 1.5) * $scale,
147 ($y - $x%2/2 - 0.25) * sqrt(3) * $scale)
148 if $self->naval;
149 0 ▶ $data .= sprintf(qq{$lead <text class="gasgiant base" x="%.3f" y="%.3f">◉</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->gasgiant 0 0 0
150 (1.4 + ($x-1) * 1.5) * $scale,
151 ($y - $x%2/2 - 0.25) * sqrt(3) * $scale)
152 if $self->gasgiant;
153 0 ▶ $data .= sprintf(qq{$lead <text class="research base" x="%.3f" y="%.3f">π</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->research 0 0 0
154 (1.6 + ($x-1) * 1.5) * $scale,
155 ($y - $x%2/2 - 0.1) * sqrt(3) * $scale)
156 if $self->research;
157 0 ▶ $data .= sprintf(qq{$lead <text class="pirate base" x="%.3f" y="%.3f">☠</text>\n},
Branch: 1 branch
BranchTrueFalseTotal
if $self->pirate 0 0 0
158 (1.6 + ($x-1) * 1.5) * $scale,
159 ($y - $x%2/2 + 0.1) * sqrt(3) * $scale)
160 if $self->pirate;
161 # last slot unused
162 0 $data .= qq{$lead </g>\n};
163 0 ▶ $data .= qq{ </a>\n} if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
164 0 return $data;
165 }
166  
167 2 1;
« lib/Traveller/Mapper.pm ↑ Summary lib/Traveller/System.pm »