Imagine hexagonal space with 3 dimensions.
Each tile has coordinates XYZ. I need to select a given cell neighbors in the same plane. With SQL it's looks like:
$tbDir = $y % 2 == 0 ? -1 : 1;
$result = db_query('SELECT x,y,z FROM {cells} WHERE
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d OR
x = %d AND y = %d AND z = %d ',
$x, $y, $z,
$x-1, $y, $z,
$x+1, $y, $z,
$x, $y-1, $z,
$x, $y+1, $z,
$x+$tbDir, $y-1, $z,
$x+$tbDir, $y+1, $z);
But, i don't like this way. Perhaps someone know more optimal algorithms? Thank you!