Ok, so I finished the function for getting a list of hexes in the jump-neighborhood of a given hex. The hexes returned are
not fixed if they are negative, they are just the raw hex locations. Presumably, a calling function could decide what to do with such locations, eg if they are outside of the sector, map them into another sector's coordinates.
As I mentioned, this uses a lookup table, and is implemented in Perl:
</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">sub neighborhood {
my $location = shift; # a hex location, eg '0101'
my $distance = shift; # a number between 1 and 6
$distance--;
my $inclusive = shift || undef; # if false, get all hexes at $distance
# if true, get all hexes within $distance
my %HEXOFFSET;
%HEXOFFSET = (
0 => [
[[ 0, 1], [ 0,-1], [ 1, 0], [-1, 0], [ 1, 1], [-1, 1]],
[[ 0, 2], [ 0,-2], [ 2, 0], [-2, 0], [ 2, 1], [-2, 1],
[ 2,-1], [-2,-1], [ 1,-1], [-1,-1], [ 1, 2], [-1, 2]],
[[ 0, 3], [ 0,-3], [ 3, 0], [-3, 0], [ 3, 1], [-3, 1],
[ 3,-1], [-3,-1], [ 2, 2], [-2, 2], [2, -2], [-2,-2],
[ 3, 2], [-3, 2], [ 1,-2], [-1,-2], [ 1, 3], [-1, 3]],
[[ 0, 4], [ 0,-4], [ 4, 0], [-4, 0], [ 4, 2], [-4, 2],
[ 4,-2], [-4,-2], [ 4, 1], [-4, 1], [ 4,-1], [-4,-1],
[ 2, 3], [-2, 3], [ 2,-3], [-2,-3], [ 3, 3], [-3, 3],
[ 3,-2], [-3,-2], [ 1, 4], [-1, 4], [ 1,-3], [-1,-3]],
[[ 0, 5], [ 0,-5], [ 5, 0], [-5, 0], [ 5, 2], [-5, 2],
[ 5,-2], [-5,-2], [ 5, 1], [-5, 1], [ 5,-1], [-5,-1],
[ 4, 3], [-4, 3], [ 4,-3], [-4,-3], [ 2, 4], [-2, 4],
[ 2,-4], [-2,-4], [ 5, 3], [-5, 3], [ 3, 4], [-3, 4],
[ 3,-3], [-3,-3], [ 1, 5], [-1, 5], [ 1,-4], [-1,-4]],
[[ 0, 6], [ 0,-6], [ 6, 0], [-6, 0], [ 6, 1], [-6, 1],
[ 6,-1], [-6,-1], [ 6, 2], [-6, 2], [ 6,-2], [-6,-2],
[ 6, 3], [-6, 3], [ 6,-3], [-6,-3], [ 4, 4], [-4, 4],
[ 4,-4], [-4,-4], [ 2, 5], [-2, 5], [ 2,-5], [-2,-5],
[ 1,-5], [-1,-5], [ 1, 6], [-1, 6], [ 3,-4], [-3,-4],
[ 3, 5], [-3, 5], [ 5,-3], [-5,-3], [ 5, 4], [-5, 4]],
],
1 => [
[[ 0, 1], [ 0,-1], [ 1, 0], [-1, 0], [ 1,-1], [-1,-1]],
[[ 0, 2], [ 0,-2], [ 2, 0], [-2, 0], [ 2, 1], [-2, 1],
[ 2,-1], [-2,-1], [ 1, 1], [-1, 1], [ 1,-2], [-1,-2]],
[[ 0, 3], [ 0,-3], [ 3, 0], [-3, 0], [ 3, 1], [-3, 1],
[ 3,-1], [-3,-1], [ 2, 2], [-2, 2], [2, -2], [-2,-2],
[ 3,-2], [-3,-2], [ 1, 2], [-1, 2], [ 1,-3], [-1,-3]],
[[ 0, 4], [ 0,-4], [ 4, 0], [-4, 0], [ 4, 2], [-4, 2],
[ 4,-2], [-4,-2], [ 4, 1], [-4, 1], [ 4,-1], [-4,-1],
[ 2, 3], [-2, 3], [ 2,-3], [-2,-3], [ 3,-3], [-3,-3],
[ 3, 2], [-3, 2], [ 1,-4], [-1,-4], [ 1, 3], [-1, 3]],
[[ 0, 5], [ 0,-5], [ 5, 0], [-5, 0], [ 5, 2], [-5, 2],
[ 5,-2], [-5,-2], [ 5, 1], [-5, 1], [ 5,-1], [-5,-1],
[ 4, 3], [-4, 3], [ 4,-3], [-4,-3], [ 2, 4], [-2, 4],
[ 2,-4], [-2,-4], [ 5,-3], [-5,-3], [ 3,-4], [-3,-4],
[ 3, 3], [-3, 3], [ 1,-5], [-1,-5], [ 1, 4], [-1, 4]],
[[ 0, 6], [ 0,-6], [ 6, 0], [-6, 0], [ 6, 1], [-6, 1],
[ 6,-1], [-6,-1], [ 6, 2], [-6, 2], [ 6,-2], [-6,-2],
[ 6, 3], [-6, 3], [ 6,-3], [-6,-3], [ 4, 4], [-4, 4],
[ 4,-4], [-4,-4], [ 2, 5], [-2, 5], [ 2,-5], [-2,-5],
[ 1, 5], [-1, 5], [ 1,-6], [-1,-6], [ 3, 4], [-3, 4],
[ 3,-5], [-3,-5], [ 5, 3], [-5, 3], [ 5,-4], [-5,-4]],
],
) unless scalar %HEXOFFSET;
my @neighbors;
my ($hx, $hy) = ($1, $2) if $location =~ /(\d\d)(\d\d)/;
foreach my $n (reverse (0 .. $distance)) {
my $ar = $HEXOFFSET{$hx % 2}[$n];
my $hex;
foreach (@$ar) {
push @neighbors, sprintf ("%02d%02d", ($hx + @$_[0]), ($hy + @$_[1]));
}
last unless $inclusive;
}
return @neighbors;
}</pre>[/QUOTE]