| 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::Util; | ||||||||||||||
| 18 | 2 | ▶ | use Modern::Perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 2 | require Exporter; | |||||||||||||
| 20 | 2 | ▶ | use POSIX qw(ceil); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 2 | our @ISA = qw(Exporter); | |||||||||||||
| 22 | 2 | our @EXPORT_OK = qw(in distance nearby flush d); | |||||||||||||
| 23 | |||||||||||||||
| 24 | # These global functions work on things that have x and y members. | ||||||||||||||
| 25 | |||||||||||||||
| 26 | 3205 | ▶ | sub in { | ||||||||||||
|
Subroutine
in: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 27 | 3205 | my $item = shift; | |||||||||||||
| 28 | 3205 | foreach (@_) { | |||||||||||||
| 29 | 63385 | ▶ | return $item if $item == $_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | } | ||||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | # Some functions cache their result. You must use the flush function to clear | ||||||||||||||
| 34 | # the cache! | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 2 | my %cache; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 402 | ▶ | sub nearby { | ||||||||||||
|
Subroutine
nearby: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 39 | 402 | my ($start, $distance, $candidates) = @_; | |||||||||||||
| 40 | 402 | ▶ | return @{$cache{"@_"}} if exists $cache{"@_"}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 319 | ▶ | $distance = 1 unless $distance; # default | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | 319 | my @result = (); | |||||||||||||
| 43 | 319 | foreach my $candidate (@$candidates) { | |||||||||||||
| 44 | 196296 | ▶ | next if $candidate == $start; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 195977 | ▶ | if (Traveller::Util::distance($start, $candidate) <= $distance) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 2397 | push(@result, $candidate); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | 319 | $cache{"@_"} = \@result; | |||||||||||||
| 50 | 319 | return @result; | |||||||||||||
| 51 | }; | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 195977 | ▶ | sub distance { | ||||||||||||
|
Subroutine
distance: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 54 | 195977 | my ($from, $to) = @_; | |||||||||||||
| 55 | 195977 | ▶ | return $cache{"@_"} if exists $cache{"@_"}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 171923 | my ($x1, $y1, $x2, $y2) = ($from->x, $from->y, $to->x, $to->y); | |||||||||||||
| 57 | # transform the Traveller coordinate system into a decent system with one axis | ||||||||||||||
| 58 | # tilted by 60° | ||||||||||||||
| 59 | 171923 | $y1 = $y1 - POSIX::ceil($x1/2); | |||||||||||||
| 60 | 171923 | $y2 = $y2 - POSIX::ceil($x2/2); | |||||||||||||
| 61 | 171923 | my $d = d($x1, $y1, $x2, $y2); | |||||||||||||
| 62 | 171923 | $cache{"@_"} = $d; | |||||||||||||
| 63 | 171923 | return $d; | |||||||||||||
| 64 | }; | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 268592 | ▶ | sub d { | ||||||||||||
|
Subroutine
d: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 67 | 268592 | my ($x1, $y1, $x2, $y2) = @_; | |||||||||||||
| 68 | 268592 | ▶ | if ($x1 > $x2) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 69 | # only consider moves from left to right and transpose start and | ||||||||||||||
| 70 | # end point to make it so | ||||||||||||||
| 71 | 96669 | return d($x2, $y2, $x1, $y1); | |||||||||||||
| 72 | } elsif ($y2>=$y1) { | ||||||||||||||
| 73 | # if it the move has a downwards component add Δx and Δy | ||||||||||||||
| 74 | 72553 | return $x2-$x1 + $y2-$y1; | |||||||||||||
| 75 | } else { | ||||||||||||||
| 76 | # else just take the larger of Δx and Δy | ||||||||||||||
| 77 | 99370 | ▶ | return $x2-$x1 > $y1-$y2 ? $x2-$x1 : $y1-$y2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | # In order to prevent memory leaks, flush the cache after generating a | ||||||||||||||
| 82 | # sector or subsector. | ||||||||||||||
| 83 | 1 | ▶ | sub flush { | ||||||||||||
|
Subroutine
flush: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 84 | 1 | %cache = (); | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 2 | 1; | |||||||||||||