line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
499
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
52
|
|
2
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
97
|
|
3
|
|
|
|
|
|
|
package Math::HexGrid::Hex; |
4
|
|
|
|
|
|
|
$Math::HexGrid::Hex::VERSION = '0.03'; |
5
|
|
|
|
|
|
|
use overload |
6
|
2
|
|
|
|
|
11
|
'""' => 'to_string', |
7
|
2
|
|
|
2
|
|
16
|
fallback => 1; |
|
2
|
|
|
|
|
4
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
0
|
121
|
sub to_string { $_[0]->{id} } |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new |
13
|
|
|
|
|
|
|
{ |
14
|
39
|
|
|
39
|
1
|
450
|
my ($class, $q, $r, $s) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# if s wasnt provided, calculate it |
17
|
39
|
|
100
|
|
|
116
|
$s ||= - $q - $r; |
18
|
|
|
|
|
|
|
|
19
|
39
|
50
|
33
|
|
|
301
|
die 'Invalid coordinates!' |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
20
|
|
|
|
|
|
|
unless defined $q && defined $r && defined $s |
21
|
|
|
|
|
|
|
&& $q + $r + $s == 0; |
22
|
|
|
|
|
|
|
|
23
|
39
|
|
|
|
|
268
|
bless { q => $q, r => $r, s => $s, id => "$q,$r" }, $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
0
|
1
|
0
|
sub id { $_[0]->{id} } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub hex_equal |
31
|
|
|
|
|
|
|
{ |
32
|
1
|
|
|
1
|
1
|
2
|
my ($self, $hex) = @_; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
$self->{q} == $hex->{q} |
35
|
|
|
|
|
|
|
&& $self->{r} == $hex->{r} |
36
|
1
|
50
|
33
|
|
|
19
|
&& $self->{s} == $hex->{s}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
0
|
1
|
|
sub is_colliding { $_[0]->hex_equal($_[1]) } |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub hex_add |
44
|
|
|
|
|
|
|
{ |
45
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
46
|
|
|
|
|
|
|
Math::HexGrid::Hex->new( |
47
|
|
|
|
|
|
|
$self->{q} + $hex->{q}, |
48
|
|
|
|
|
|
|
$self->{r} + $hex->{r}, |
49
|
|
|
|
|
|
|
$self->{s} + $hex->{s}, |
50
|
0
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub hex_subtract |
55
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
57
|
|
|
|
|
|
|
Math::HexGrid::Hex->new( |
58
|
|
|
|
|
|
|
$self->{q} - $hex->{q}, |
59
|
|
|
|
|
|
|
$self->{r} - $hex->{r}, |
60
|
|
|
|
|
|
|
$self->{s} - $hex->{s}, |
61
|
0
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub hex_multiply |
66
|
|
|
|
|
|
|
{ |
67
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
68
|
|
|
|
|
|
|
Math::HexGrid::Hex->new( |
69
|
|
|
|
|
|
|
$self->{q} * $hex->{q}, |
70
|
|
|
|
|
|
|
$self->{r} * $hex->{r}, |
71
|
|
|
|
|
|
|
$self->{s} * $hex->{s}, |
72
|
0
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub hex_length |
76
|
|
|
|
|
|
|
{ |
77
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
78
|
0
|
|
|
|
|
|
int((abs($self->{q}) + abs($self->{r}) + abs($self->{s})) / 2); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub hex_distance |
83
|
|
|
|
|
|
|
{ |
84
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
85
|
0
|
|
|
|
|
|
$self->hex_subtract($hex)->hex_length; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my @hex_directions = ( |
89
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(1,0,-1), |
90
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(1,-1,0), |
91
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(0,-1,1), |
92
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(-1,0,1), |
93
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(-1,1,0), |
94
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(0,1,-1), |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _hex_direction |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
# this will handle directions > 6 and < 0 |
100
|
0
|
|
|
0
|
|
|
$hex_directions[(6 + ($_[0] % 6)) % 6]; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub hex_neighbor |
105
|
|
|
|
|
|
|
{ |
106
|
0
|
|
|
0
|
1
|
|
my ($self, $direction) = @_; |
107
|
0
|
|
|
|
|
|
$self->hex_add(_hex_direction($direction)); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |