File Coverage

lib/Traveller/Hex.pm
Criterion Covered Total %
statement 3 59 5.0
branch 0 46 0.0
condition 0 6 0.0
subroutine 1 7 14.2
pod 0 6 0.0
total 4 124 3.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # Copyright (C) 2009-2021 Alex Schroeder
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 .
16              
17             package Traveller::Hex;
18 2     2   10 use Mojo::Base -base;
  2         2  
  2         15  
19              
20             has 'name';
21             has 'x';
22             has 'y';
23             has 'starport';
24             has 'size';
25             has 'population';
26             has 'consulate';
27             has 'pirate';
28             has 'TAS';
29             has 'research';
30             has 'naval';
31             has 'scout';
32             has 'gasgiant';
33             has 'travelzone';
34             has 'url';
35             has 'map';
36             has 'comm' => sub { [] };
37             has 'trade' => sub { {} };
38             has 'routes' => sub { [] };
39             has 'culture';
40             has 'colour';
41              
42             sub base {
43 0     0 0   my ($self, $key) = @_;
44 0           $key = uc($key);
45 0 0         ($key eq 'C') ? $self->consulate(1)
    0          
    0          
    0          
    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             sub at {
56 0     0 0   my ($self, $x, $y) = @_;
57 0   0       return $self->x == $x && $self->y == $y;
58             }
59              
60             sub str {
61 0     0 0   my $self = shift;
62 0           sprintf "%-12s %02s%02s ", $self->name, $self->x, $self->y;
63             }
64              
65             sub eliminate {
66 0     0 0   my $from = shift;
67 0           foreach my $to (@_) {
68             # eliminate the communication $from -> $to
69 0           my @ar1 = grep {$_ != $to} @{$from->comm};
  0            
  0            
70 0           $from->comm(\@ar1);
71             # eliminate the communication $to -> $from
72 0           my @ar2 = grep {$_ != $from} @{$to->comm};
  0            
  0            
73 0           $to->comm(\@ar2);
74             }
75             }
76              
77             sub comm_svg {
78 0     0 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}) {
  0            
83 0           my ($x2, $y2) = ($to->x, $to->y);
84 0           $data .= sprintf(qq{ \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             sub system_svg {
96 0     0 0   my $self = shift;
97 0           my $x = $self->x;
98 0           my $y = $self->y;
99 0           my $name = $self->name;
100 0 0         my $display = ($self->population >= 9 ? uc($name) : $name);
101 0           my $starport = $self->starport;
102 0           my $size = $self->size;
103 0           my $url = $self->url;
104 0 0         my $lead = ($url ? ' ' : '');
105 0           my $data = '';
106 0 0         $data .= qq{ \n} if $url;
107 0 0         if ($name) {
108 0           $data .= qq{$lead \n};
109             } else {
110 0           $data .= qq{$lead \n};
111             }
112 0           my $scale = 100;
113             # travel zone red painted first, so it appears at the bottom
114 0 0         $data .= sprintf(qq{$lead \n},
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 0 0       $data .= sprintf(qq{$lead \n},
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 0         $data .= sprintf(qq{$lead \n},
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 0         $data .= sprintf(qq{$lead $starport\n},
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 $display\n},
131             (1 + ($x-1) * 1.5) * $scale,
132             ($y - $x%2/2 + 0.4) * sqrt(3) * $scale);
133 0 0         $data .= sprintf(qq{$lead \n},
134             (0.6 + ($x-1) * 1.5) * $scale,
135             ($y - $x%2/2 + 0.25) * sqrt(3) * $scale)
136             if $self->consulate;
137 0 0         $data .= sprintf(qq{$lead \n},
138             (0.4 + ($x-1) * 1.5) * $scale,
139             ($y - $x%2/2 + 0.1) * sqrt(3) * $scale)
140             if $self->TAS;
141 0 0         $data .= sprintf(qq{$lead \n},
142             (0.4 + ($x-1) * 1.5) * $scale,
143             ($y - $x%2/2 - 0.1) * sqrt(3) * $scale)
144             if $self->scout;
145 0 0         $data .= sprintf(qq{$lead \n},
146             (0.6 + ($x-1) * 1.5) * $scale,
147             ($y - $x%2/2 - 0.25) * sqrt(3) * $scale)
148             if $self->naval;
149 0 0         $data .= sprintf(qq{$lead \n},
150             (1.4 + ($x-1) * 1.5) * $scale,
151             ($y - $x%2/2 - 0.25) * sqrt(3) * $scale)
152             if $self->gasgiant;
153 0 0         $data .= sprintf(qq{$lead π\n},
154             (1.6 + ($x-1) * 1.5) * $scale,
155             ($y - $x%2/2 - 0.1) * sqrt(3) * $scale)
156             if $self->research;
157 0 0         $data .= sprintf(qq{$lead \n},
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 \n};
163 0 0         $data .= qq{ \n} if $url;
164 0           return $data;
165             }
166              
167             1;