blib/lib/Game/TextMapper/Schroeder/Square.pm

filter: Statementstmt 88.8% 32 / 36 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 9 / 9 totaltotal 85.2% 58 / 68 CC 7 SCAR 8.4
CC
7
Cov
81%
neighbor2
4.2
neighbor
3.1
distance
2.0
CRAP 2.3
« blib/lib/Game/TextMapper/Line/Hex.pm ↑ Summary script/text-mapper »
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::Schroeder::Square - a role for square map generators
21  
22 =head1 SYNOPSIS
23  
24 # create a map
25 package World;
26 use Modern::Perl;
27 use Mojo::Base -base;
28 use Role::Tiny::With;
29 with 'Game::TextMapper::Schroeder::Base';
30 with 'Game::TextMapper::Schroeder::Square';
31 # use it
32 package main;
33 my $map = World->new(height => 10, width => 10);
34  
35 =head1 DESCRIPTION
36  
37 This role provides basic functionality for map generation with square maps: the
38 number of neighbours within one or two regions distance, how to pick a random
39 neighbour direction, how to compute the coordinates of these neighbours, how to
40 draw arrows towards these neighbours.
41  
42 This inherits attributes and methods from L<Game::TextMapper::Schroeder::Base>,
43 such as C<width> and C<height>.
44  
45 =cut
46  
47 package Game::TextMapper::Schroeder::Square;
48 3 ▶use Modern::Perl '2018';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 =head1 METHODS
52  
53 =head2 reverse
54  
55 Reverses a direction.
56  
57 =cut
58  
59 2 ▶sub reverse {
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
60 2 my ($self, $i) = @_;
61 2 return ($i + 2) % 4;
62 }
63  
64 =head2 neighbors
65  
66 The list of directions for neighbours one step away (0 to 3).
67  
68 =cut
69  
70 5998 ▶sub neighbors { 0 .. 3 }
Subroutine neighbors: called CC=1 SCAR=0.0
Pod documented
71  
72 =head2 neighbors2
73  
74 The list of directions for neighbours two steps away (0 to 7).
75  
76 =cut
77  
78 269 ▶sub neighbors2 { 0 .. 7 }
Subroutine neighbors2: called CC=1 SCAR=0.0
Pod documented
79  
80 =head2 random_neighbor
81  
82 A random direction for a neighbour one step away (a random integer from 0 to 3).
83  
84 =cut
85  
86 8574 ▶sub random_neighbor { int(rand(4)) }
Subroutine random_neighbor: called CC=1 SCAR=0.0
Pod documented
87  
88 =head2 random_neighbor2
89  
90 A random direction for a neighbour two steps away (a random integer from 0 to
91 7).
92  
93 =cut
94  
95 7709 ▶sub random_neighbor2 { int(rand(8)) }
Subroutine random_neighbor2: called CC=1 SCAR=0.0
Pod documented
96  
97 3 my $delta_square = [[-1, 0], [ 0, -1], [+1, 0], [ 0, +1]];
98  
99 =head2 neighbor($square, $i)
100  
101 say join(",", $map->neighbor("0203", 1));
102 # 2,2
103  
104 Returns the coordinates of a neighbor in a particular direction (0 to 3), one
105 step away.
106  
107 C<$square> is an array reference of coordinates or a string that can be turned
108 into one using the C<xy> method from L<Game::TextMapper::Schroeder::Base>.
109  
110 C<$i> is a direction (0 to 3).
111  
112 =cut
113  
114 27723 ▶sub neighbor {
Subroutine neighbor: called CC=3 SCAR=11.3
Pod documented
115 27723 my $self = shift;
116 # $hex is [x,y] or "0x0y" and $i is a number 0 .. 3
117 27723 my ($hex, $i) = @_;
118 27723 ▶ die join(":", caller) . ": undefined direction for $hex\n" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 27723 27723
119 27723 ▶ $hex = [$self->xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 27723 0 27723
120 27723 return ($hex->[0] + $delta_square->[$i]->[0],
121 $hex->[1] + $delta_square->[$i]->[1]);
122 }
123  
124 3 my $delta_square2 = [
125 [-2, 0], [-1, -1], [ 0, -2], [+1, -1],
126 [+2, 0], [+1, +1], [ 0, +2], [-1, +1]];
127  
128 =head2 neighbor2($square, $i)
129  
130 say join(",", $map->neighbor2("0203", 1));
131 # 1, 2
132  
133 Returns the coordinates of a neighbor in a particular direction (0 to 7), two
134 steps away.
135  
136 C<$square> is an array reference of coordinates or a string that can be turned
137 into one using the C<xy> method from L<Game::TextMapper::Schroeder::Base>.
138  
139 C<$i> is a direction (0 to 3).
140  
141 =cut
142  
143 9861 ▶sub neighbor2 {
Subroutine neighbor2: called CC=4 SCAR=14.5
Pod documented
144 9861 my $self = shift;
145 # $hex is [x,y] or "0x0y" and $i is a number 0 .. 7
146 9861 my ($hex, $i) = @_;
147 9861 ▶ die join(":", caller) . ": undefined direction for $hex\n" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 9861 9861
148 9861 ▶ die join(":", caller) . ": direction $i not supported for square $hex\n" if $i > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 7 0 9861 9861
149 9861 ▶ $hex = [$self->xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 9861 0 9861
150 9861 return ($hex->[0] + $delta_square2->[$i]->[0],
151 $hex->[1] + $delta_square2->[$i]->[1]);
152 }
153  
154 =head2 distance($x1, $y1, $x2, $y2) or distance($coords1, $coords2)
155  
156 say $map->distance("0203", "0003");
157 # 2
158  
159 Returns the distance between two coordinates.
160  
161 =cut
162  
163 11152 ▶sub distance {
Subroutine distance: called CC=2 SCAR=6.9
Pod documented
164 11152 my $self = shift;
165 11152 my ($x1, $y1, $x2, $y2) = @_;
166 11152 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 4789 6363 11152
167 4789 ($x1, $y1, $x2, $y2) = map { $self->xy($_) } @_;
168 }
169 11152 return abs($x2 - $x1) + abs($y2 - $y1);
170 }
171  
172 =head2 arrows
173  
174 A helper that returns the SVG fragments for arrows in four directions, to be
175 used in a C<defs> element.
176  
177 =cut
178  
179 0 ▶sub arrows {
Subroutine arrows: not called CC=1 SCAR=6.9
Pod documented
180 0 my $self = shift;
181 return
182 qq{<marker id="arrow" markerWidth="6" markerHeight="6" refX="6" refY="3" orient="auto"><path d="M6,0 V6 L0,3 Z" style="fill: black;" /></marker>},
183 qq{<marker id="wind" markerWidth="6" markerHeight="6" refX="6" refY="3" orient="auto"><path d="M6,0 V6 L0,3 Z" style="fill: purple;" /></marker>},
184 map {
185 0 my $angle = 90 * $_;
186 0 qq{<path id="arrow$_" transform="rotate($angle)" d="M-15,0 H30" style="stroke: black; stroke-width: 3px; fill: none; marker-start: url(#arrow);"/>},
187 qq{<path id="wind$_" transform="translate(40,40) rotate($angle)" d="M-15,0 H30" style="stroke: purple; stroke-width: 3px; fill: none; marker-start: url(#wind);"/>},
188 } ($self->neighbors());
189 }
190  
191 =head1 SEE ALSO
192  
193 L<Game::TextMapper::Schroeder::Base>
194 L<Game::TextMapper::Schroeder::Hex>
195  
196 =cut
197  
198 3 1;
« blib/lib/Game/TextMapper/Line/Hex.pm ↑ Summary script/text-mapper »