| 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::Point - a point on the map | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | This is a simple class to hold points. Points have coordinates and know how to | ||||||||||||||||||||||||||||||||||
| 25 | print them. | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | package Game::TextMapper::Point; | ||||||||||||||||||||||||||||||||||
| 30 | 11 | ▶ | use Modern::Perl '2018'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 31 | 11 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | =head2 Attributes | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | C<x>, C<y>, C<z> are coordinates. | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | C<type>, C<label>, C<size> are used to draw the SVG. These are used by the | ||||||||||||||||||||||||||||||||||
| 38 | actual implementations, L<Game::TextMapper::Point::Hex> and | ||||||||||||||||||||||||||||||||||
| 39 | L<Game::TextMapper::Point::Square>. | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | C<map> is a reference to L<Game::TextMapper::Mapper> from which to get | ||||||||||||||||||||||||||||||||||
| 42 | C<text_attributes> (for the coordinates), and both C<label_attributes> and | ||||||||||||||||||||||||||||||||||
| 43 | C<glow_attributes> (for the label). | ||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||
| 47 | 11 | has 'x'; | |||||||||||||||||||||||||||||||||
| 48 | 11 | has 'y'; | |||||||||||||||||||||||||||||||||
| 49 | 11 | has 'z'; | |||||||||||||||||||||||||||||||||
| 50 | 11 | has 'type'; | |||||||||||||||||||||||||||||||||
| 51 | 11 | has 'label'; | |||||||||||||||||||||||||||||||||
| 52 | 11 | has 'size'; | |||||||||||||||||||||||||||||||||
| 53 | 11 | has 'map'; | |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | =head2 Methods | ||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||
| 57 | =head3 str | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | Returns "(1,2,3)" or "(1,2)" depending on whether the z coordinate is defined or | ||||||||||||||||||||||||||||||||||
| 60 | not; use this for log output. | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub str { | ||||||||||||||||||||||||||||||||
|
Subroutine
str: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 65 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (defined $self->z) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | 0 | return '(' . $self->x . ',' . $self->y . ',' . $self->z . ')'; | |||||||||||||||||||||||||||||||||
| 68 | } else { | ||||||||||||||||||||||||||||||||||
| 69 | 0 | return '(' . $self->x . ',' . $self->y . ')'; | |||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | =head3 equal($other) | ||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | True if all three coordinates match. | ||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 1721 | ▶ | sub equal { | ||||||||||||||||||||||||||||||||
|
Subroutine
equal: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 80 | 1721 | my ($self, $other) = @_; | |||||||||||||||||||||||||||||||||
| 81 | 1721 | ▶ | return $self->x == $other->x && $self->y == $other->y && $self->z == $other->z; | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
$self->x == $other->x && $self->y == $other->y && $self->z == $other->z
$self->x == $other->x and $self->y == $other->y
MC/DC66%2/3
$self->x == $other->x && $self->y == $other->y && $self->z == $other->z
$self->x == $other->x
$self->y == $other->y
$self->z == $other->z
Truth table75%3/4
A and B && C
A= $self->x == $other->xB= $self->y == $other->yC= $self->z == $other->z
| |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | =head3 cmp($other) | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | Return -1, 0, or 1 depending on the three coordinates. | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub cmp { | ||||||||||||||||||||||||||||||||
|
Subroutine
cmp: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 91 | 0 | my ($a, $b) = @_; | |||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return $a->x <=> $b->x || $a->y <=> $b->y || $a->z <=> $b->z; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$a->x <=> $b->x || $a->y <=> $b->y || $a->z <=> $b->z
$a->x <=> $b->x or $a->y <=> $b->y
MC/DC0%0/3
$a->x <=> $b->x || $a->y <=> $b->y || $a->z <=> $b->z
$a->x <=> $b->x
$a->y <=> $b->y
$a->z <=> $b->z
Truth table0%0/4
A or B || C
A= $a->x <=> $b->xB= $a->y <=> $b->yC= $a->z <=> $b->z
| |||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | =head3 coordinates | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | Return "1,1,1" or "1,1" for coordinates in scalar context, depending on whether | ||||||||||||||||||||||||||||||||||
| 98 | the z coordinate is defined or not, or it returns the three coordinates in list | ||||||||||||||||||||||||||||||||||
| 99 | context. | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub coordinates { | ||||||||||||||||||||||||||||||||
|
Subroutine
coordinates: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return $self->x, $self->y, $self->z if wantarray; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | return $self->x . "," . $self->y . "," . $self->z if defined $self->z; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 0 | return $self->x . "," . $self->y; | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | =head3 coord($x, $y, $separator) | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | Return "0101" or "-01-01" for coordinates. Often this is what we want in text. | ||||||||||||||||||||||||||||||||||
| 113 | this works up to 99. | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 91464 | ▶ | sub coord { | ||||||||||||||||||||||||||||||||
|
Subroutine
coord: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 118 | 91464 | my ($x, $y, $separator) = @_; | |||||||||||||||||||||||||||||||||
| 119 | 91464 | ▶ | $separator //= ($x > 99 || $y > 99 ? "." : ""); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$separator //= $x > 99 || $y > 99 ? "." : ""
$x > 99 or $y > 99
MC/DC100%1/1
$separator //= $x > 99 || $y > 99 ? "." : ""
$separator
Truth table100%2/2
A
A= $separator
| |||||||||||||||||||||||||||||||||||
| 120 | # print (1,1) as 0101; print (-1,-1) as -01-01; print (100,100) as 100.100 | ||||||||||||||||||||||||||||||||||
| 121 | 91464 | ▶ | return sprintf("%0*d$separator%0*d", | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 122 | ($x < 0 ? 3 : 2), $x, | ||||||||||||||||||||||||||||||||||
| 123 | ($y < 0 ? 3 : 2), $y); | ||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | =head2 Abstract methods | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | These methods must be implemented by derived classes. The $offset argument is an | ||||||||||||||||||||||||||||||||||
| 129 | array with the offsets to add to the C<y> based on C<z> coordinate. The idea is | ||||||||||||||||||||||||||||||||||
| 130 | that if we have two dungeon levels, for example, and we want to generate a | ||||||||||||||||||||||||||||||||||
| 131 | single SVG document, then the first level is at the top of the page, as usual, | ||||||||||||||||||||||||||||||||||
| 132 | and the next level is further down on the page: all the C<y> coordinates were | ||||||||||||||||||||||||||||||||||
| 133 | increased by the offset. | ||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | =head3 svg_region($attributes, $offset) | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | This returns an SVG fragment, a string with a C<polygon> or C<rect> element, for | ||||||||||||||||||||||||||||||||||
| 138 | example. | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | This is used for the group containing the regions in the resulting SVG. | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | =head3 svg($offset) | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | This returns an SVG fragment, a string with a C<use> element. | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | This is used for the group containing the background colours in the resulting | ||||||||||||||||||||||||||||||||||
| 147 | SVG. | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | =head3 svg_coordinates($offset) | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | This returns an SVG fragment, a string with a C<text> element. | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | This is used for the group containing the coordinates in the resulting SVG. | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | =head3 svg_label($url, $offset) | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | This returns an SVG fragment, a string with a C<g> element containing two | ||||||||||||||||||||||||||||||||||
| 158 | C<text> elements, and possibly an C<a> element: the "glow", the label itself, | ||||||||||||||||||||||||||||||||||
| 159 | and possibly a link to the URL. | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | This is used for C<g#labels> (the group containing the labels) in the resulting | ||||||||||||||||||||||||||||||||||
| 162 | SVG. | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | L<Game::TextMapper::Mapper> uses this class. Internally, it calls C<make_region> | ||||||||||||||||||||||||||||||||||
| 167 | which is implemented by either L<Game::TextMapper::Mapper::Hex> or | ||||||||||||||||||||||||||||||||||
| 168 | L<Game::TextMapper::Mapper::Square>. Depending on the implementation, | ||||||||||||||||||||||||||||||||||
| 169 | L<Game::TextMapper::Point::Hex> or L<Game::TextMapper::Point::Square> are used | ||||||||||||||||||||||||||||||||||
| 170 | to implement this class. | ||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | 11 | 1; | |||||||||||||||||||||||||||||||||