inline get_adjacent, tiles_adjacent, distance_between (map_locations)
This commit is contained in:
parent
d11fa92543
commit
91c8725338
2 changed files with 53 additions and 52 deletions
|
@ -289,58 +289,6 @@ bool map_location::matches_range(const std::string& xloc, const std::string &ylo
|
|||
return true;
|
||||
}
|
||||
|
||||
void get_adjacent_tiles(const map_location& a, map_location* res)
|
||||
{
|
||||
res->x = a.x;
|
||||
res->y = a.y-1;
|
||||
++res;
|
||||
res->x = a.x+1;
|
||||
res->y = a.y - (is_even(a.x) ? 1:0);
|
||||
++res;
|
||||
res->x = a.x+1;
|
||||
res->y = a.y + (is_odd(a.x) ? 1:0);
|
||||
++res;
|
||||
res->x = a.x;
|
||||
res->y = a.y+1;
|
||||
++res;
|
||||
res->x = a.x-1;
|
||||
res->y = a.y + (is_odd(a.x) ? 1:0);
|
||||
++res;
|
||||
res->x = a.x-1;
|
||||
res->y = a.y - (is_even(a.x) ? 1:0);
|
||||
}
|
||||
|
||||
|
||||
bool tiles_adjacent(const map_location& a, const map_location& b)
|
||||
{
|
||||
// Two tiles are adjacent:
|
||||
// if y is different by 1, and x by 0,
|
||||
// or if x is different by 1 and y by 0,
|
||||
// or if x and y are each different by 1,
|
||||
// and the x value of the hex with the greater y value is even.
|
||||
|
||||
const int xdiff = abs(a.x - b.x);
|
||||
const int ydiff = abs(a.y - b.y);
|
||||
return (ydiff == 1 && a.x == b.x) || (xdiff == 1 && a.y == b.y) ||
|
||||
(xdiff == 1 && ydiff == 1 && (a.y > b.y ? is_even(a.x) : is_even(b.x)));
|
||||
}
|
||||
|
||||
size_t distance_between(const map_location& a, const map_location& b)
|
||||
{
|
||||
const size_t hdistance = abs(a.x - b.x);
|
||||
|
||||
const size_t vpenalty = ( (is_even(a.x) && is_odd(b.x) && (a.y < b.y))
|
||||
|| (is_even(b.x) && is_odd(a.x) && (b.y < a.y)) ) ? 1 : 0;
|
||||
|
||||
// For any non-negative integer i, i - i/2 - i%2 == i/2
|
||||
// previously returned (hdistance + vdistance - vsavings)
|
||||
// = hdistance + vdistance - minimum(vdistance,hdistance/2+hdistance%2)
|
||||
// = maximum(hdistance, vdistance+hdistance-hdistance/2-hdistance%2)
|
||||
// = maximum(hdistance,abs(a.y-b.y)+vpenalty+hdistance/2)
|
||||
|
||||
return std::max<int>(hdistance, abs(a.y - b.y) + vpenalty + hdistance/2);
|
||||
}
|
||||
|
||||
std::vector<map_location> parse_location_range(const std::string &x, const std::string &y,
|
||||
bool with_border)
|
||||
{
|
||||
|
|
|
@ -258,5 +258,58 @@ inline map_location map_location::get_direction(
|
|||
}
|
||||
}
|
||||
|
||||
/** inline get_adjacent, and get_distance functions **/
|
||||
|
||||
inline void get_adjacent_tiles(const map_location& a, map_location* res)
|
||||
{
|
||||
res->x = a.x;
|
||||
res->y = a.y-1;
|
||||
++res;
|
||||
res->x = a.x+1;
|
||||
res->y = a.y - (is_even(a.x) ? 1:0);
|
||||
++res;
|
||||
res->x = a.x+1;
|
||||
res->y = a.y + (is_odd(a.x) ? 1:0);
|
||||
++res;
|
||||
res->x = a.x;
|
||||
res->y = a.y+1;
|
||||
++res;
|
||||
res->x = a.x-1;
|
||||
res->y = a.y + (is_odd(a.x) ? 1:0);
|
||||
++res;
|
||||
res->x = a.x-1;
|
||||
res->y = a.y - (is_even(a.x) ? 1:0);
|
||||
}
|
||||
|
||||
inline bool tiles_adjacent(const map_location& a, const map_location& b)
|
||||
{
|
||||
// Two tiles are adjacent:
|
||||
// if y is different by 1, and x by 0,
|
||||
// or if x is different by 1 and y by 0,
|
||||
// or if x and y are each different by 1,
|
||||
// and the x value of the hex with the greater y value is even.
|
||||
|
||||
const int xdiff = abs(a.x - b.x);
|
||||
const int ydiff = abs(a.y - b.y);
|
||||
return (ydiff == 1 && a.x == b.x) || (xdiff == 1 && a.y == b.y) ||
|
||||
(xdiff == 1 && ydiff == 1 && (a.y > b.y ? is_even(a.x) : is_even(b.x)));
|
||||
}
|
||||
|
||||
inline size_t distance_between(const map_location& a, const map_location& b)
|
||||
{
|
||||
const size_t hdistance = abs(a.x - b.x);
|
||||
|
||||
const size_t vpenalty = ( (is_even(a.x) && is_odd(b.x) && (a.y < b.y))
|
||||
|| (is_even(b.x) && is_odd(a.x) && (b.y < a.y)) ) ? 1 : 0;
|
||||
|
||||
// For any non-negative integer i, i - i/2 - i%2 == i/2
|
||||
// previously returned (hdistance + vdistance - vsavings)
|
||||
// = hdistance + vdistance - minimum(vdistance,hdistance/2+hdistance%2)
|
||||
// = maximum(hdistance, vdistance+hdistance-hdistance/2-hdistance%2)
|
||||
// = maximum(hdistance,abs(a.y-b.y)+vpenalty+hdistance/2)
|
||||
|
||||
return std::max<int>(hdistance, abs(a.y - b.y) + vpenalty + hdistance/2);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue