lib/Traveller/Subsector.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 8 totaltotal 85.1% 103 / 121 CC 16 SCAR 16.1
CC
16
Cov
92%
init
11.2
grow
4.0
compute_digraphs
2.0
CRAP 5.0
« script/traveller ↑ Summary lib/Traveller/Util.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::Subsector;
18 2 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Traveller::Util qw(nearby in);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Traveller::System::Classic::MPTS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Traveller::System::Classic;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Traveller::System;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶has 'systems' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 1310 ▶sub one {
Subroutine one: called CC=1 SCAR=0.0
Pod undocumented
28 1310 my $i = int(rand(scalar @_));
29 1310 return $_[$i];
30 }
31  
32 32 ▶sub compute_digraphs {
Subroutine compute_digraphs: called CC=2 SCAR=6.9
Pod undocumented
33 32 my @first = qw(b c d f g h j k l m n p q r s t v w x y z .
34 sc ng ch gh ph rh sh th wh zh wr qu
35 st sp tr tw fl dr pr dr);
36 # make missing vowel rare
37 32 my @second = qw(a e i o u a e i o u a e i o u .);
38 32 my @d;
39 32 for (1 .. 10+rand(20)) {
40 655 push(@d, one(@first));
41 655 push(@d, one(@second));
42 }
43 32 return \@d;
44 }
45  
46 673 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
47 673 my ($self, $system) = @_;
48 673 push(@{$self->systems}, $system);
49 }
50  
51 2 ▶sub init {
Subroutine init: called CC=10 SCAR=24.1
Pod undocumented
52 2 my ($self, $width, $height, $rules, $density) = @_;
53 2 ▶ $density ||= 0.5;
Condition50%1/2
$density ||= "0.5"
l!l
20
MC/DC0%0/1
$density ||= "0.5"
$density
Truth table50%1/2
A
A= $density
Aresult
11
00
54 2 my $digraphs = $self->compute_digraphs;
55 2 ▶ $width //= 8;
Condition50%1/2
$width //= 8
l!l
20
MC/DC0%0/1
$width //= 8
$width
Truth table50%1/2
A
A= $width
Aresult
11
00
56 2 ▶ $height //= 10;
Condition50%1/2
$height //= 10
l!l
20
MC/DC0%0/1
$height //= 10
$height
Truth table50%1/2
A
A= $height
Aresult
11
00
57 2 for my $x (1..$width) {
58 40 for my $y (1..$height) {
59 1360 ▶ if (rand() < $density) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < $density) 673 687 1360
60 673 my $system;
61 673 ▶ if ($rules eq 'mpts') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rules eq 'mpts') { } 0 673 673
elsif ($rules eq 'ct') { } 0 673 673
62 0 $system = Traveller::System::Classic::MPTS->new();
63 } elsif ($rules eq 'ct') {
64 0 $system = Traveller::System::Classic->new();
65 } else {
66 673 $system = Traveller::System->new();
67 }
68 673 $self->add($system->init($x, $y, $digraphs));
69 }
70 }
71 }
72 # Rename some systems: assume a jump-2 and a jump-1 culture per every
73 # subsector of 8×10×½ systems. Go through the list in random order.
74 2 for my $system (shuffle(grep { rand(20) < 1 } @{$self->systems})) {
75 30 $self->spread(
76 $system,
77 $self->compute_digraphs,
78 1 + int(rand(2)), # jump distance
79 1 + int(rand(3))); # jump number
80 }
81 2 return $self;
82 }
83  
84 30 ▶sub spread {
Subroutine spread: called CC=2 SCAR=6.9
Pod undocumented
85 30 my ($self, $system, $digraphs, $jump_distance, $jump_number) = @_;
86 30 my $culture = $system->compute_name($digraphs);
87 # warn sprintf("%02d%02d %s %d %d\n", $system->x, $system->y, $culture, $jump_distance, $jump_number);
88 30 my $network = [$system];
89 30 $self->grow($system, $jump_distance, $jump_number, $network);
90 30 for my $other (@$network) {
91 803 $other->culture($culture);
92 803 $other->name($other->compute_name($digraphs));
93 }
94 }
95  
96 402 ▶sub grow {
Subroutine grow: called CC=4 SCAR=13.9
Pod undocumented
97 402 my ($self, $system, $jump_distance, $jump_number, $network) = @_;
98 my @new_neighbours =
99 1145 ▶ grep { not $_->culture or int(rand(2)) }
Condition100%3/3
!$_->culture || int rand 2
l!l&&r!l&&!r
420353372
MC/DC100%2/2
!$_->culture || int rand 2
!$_->culture int rand 2
Truth table100%3/3
not(A) || B
A= !$_->cultureB= int rand 2
ABresult
1X1
011
000
100 402 grep { not Traveller::Util::in($_, @$network) }
101 $self->neighbours($system, $jump_distance, $jump_number);
102 # for my $neighbour (@new_neighbours) {
103 # warn sprintf(" added %02d%02d %d %d\n", $neighbour->x, $neighbour->y, $jump_distance, $jump_number);
104 # }
105 402 push(@$network, @new_neighbours);
106 402 ▶ if ($jump_number > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jump_number > 0) 196 206 402
107 196 for my $neighbour (@new_neighbours) {
108 372 $self->grow($neighbour, $jump_distance, $jump_number - 1, $network);
109 }
110 }
111 }
112  
113 402 ▶sub neighbours {
Subroutine neighbours: called CC=1 SCAR=0.0
Pod undocumented
114 402 my ($self, $system, $jump_distance, $jump_number) = @_;
115 402 my @neighbours = nearby($system, $jump_distance, $self->systems);
116 402 return @neighbours;
117 }
118  
119 2 ▶sub str {
Subroutine str: called CC=2 SCAR=6.9
Pod undocumented
120 2 my $self = shift;
121 2 my $subsector;
122 2 foreach my $system (@{$self->systems}) {
123 673 $subsector .= $system->str . "\n";
124 }
125 2 return $subsector;
126 }
127  
128 2 1;
« script/traveller ↑ Summary lib/Traveller/Util.pm »