| 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::Line::Hex - a line implementation for hex maps | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | The line connects two points on a hex map. This class knows how to compute all | ||||||||||||||||||||||||||||
| 25 | the regions between these two points, how to compute the next region along the | ||||||||||||||||||||||||||||
| 26 | line, and how to output SVG. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | L<Game::TextMapper::Point> | ||||||||||||||||||||||||||||
| 31 | L<Game::TextMapper::Line::Line> | ||||||||||||||||||||||||||||
| 32 | L<Game::TextMapper::Line::Square> | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | package Game::TextMapper::Line::Hex; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | ▶ | use Game::TextMapper::Constants qw($dx $dy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 11 | ▶ | use Game::TextMapper::Point; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | ▶ | use Modern::Perl '2018'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 11 | ▶ | use Mojo::Base 'Game::TextMapper::Line'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 4090 | ▶ | sub pixels { | ||||||||||||||||||||||||||
|
Subroutine
pixels: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 4090 | my ($self, $point) = @_; | |||||||||||||||||||||||||||
| 46 | 4090 | my ($x, $y) = ($point->x * $dx * 3/2, ($point->y + $self->offset->[$point->z]) * $dy - $point->x % 2 * $dy/2); | |||||||||||||||||||||||||||
| 47 | 4090 | ▶ | return ($x, $y) if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | return sprintf("%.1f,%.1f", $x, $y); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Brute forcing the "next" step by trying all the neighbors. The | ||||||||||||||||||||||||||||
| 52 | # connection data to connect to neighboring hexes. | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | # Example Map Index for the array | ||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||
| 56 | # 0201 2 | ||||||||||||||||||||||||||||
| 57 | # 0102 0302 1 3 | ||||||||||||||||||||||||||||
| 58 | # 0202 0402 | ||||||||||||||||||||||||||||
| 59 | # 0103 0303 6 4 | ||||||||||||||||||||||||||||
| 60 | # 0203 0403 5 | ||||||||||||||||||||||||||||
| 61 | # 0104 0304 | ||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||
| 63 | # Note that the arithmetic changes when x is odd. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 916 | ▶ | sub one_step { | ||||||||||||||||||||||||||
|
Subroutine
one_step: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 916 | my ($self, $from, $to) = @_; | |||||||||||||||||||||||||||
| 67 | 916 | my $delta = [[[-1, 0], [ 0, -1], [+1, 0], [+1, +1], [ 0, +1], [-1, +1]], # x is even | |||||||||||||||||||||||||||
| 68 | [[-1, -1], [ 0, -1], [+1, -1], [+1, 0], [ 0, +1], [-1, 0]]]; # x is odd | ||||||||||||||||||||||||||||
| 69 | 916 | my ($min, $best); | |||||||||||||||||||||||||||
| 70 | 916 | for my $i (0 .. 5) { | |||||||||||||||||||||||||||
| 71 | # make a new guess | ||||||||||||||||||||||||||||
| 72 | 5496 | my ($x, $y) = ($from->x + $delta->[$from->x % 2]->[$i]->[0], | |||||||||||||||||||||||||||
| 73 | $from->y + $delta->[$from->x % 2]->[$i]->[1]); | ||||||||||||||||||||||||||||
| 74 | 5496 | my $d = ($to->x - $x) * ($to->x - $x) | |||||||||||||||||||||||||||
| 75 | + ($to->y - $y) * ($to->y - $y); | ||||||||||||||||||||||||||||
| 76 | 5496 | ▶ | if (!defined($min) || $d < $min) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $min or $d < $min
MC/DC100%2/2
not defined $min or $d < $min
not defined $min
$d < $min
Truth table100%3/3
not(A) or B
A= not defined $minB= $d < $min
| |||||||||||||||||||||||||||||
| 77 | 2419 | $min = $d; | |||||||||||||||||||||||||||
| 78 | 2419 | $best = Game::TextMapper::Point->new(x => $x, y => $y, z => $from->z); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 916 | return $best; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 11 | 1; | |||||||||||||||||||||||||||