Skip to content

Commit

Permalink
refactor tiles_adjacent, use jump tables instead of math abs
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed May 21, 2014
1 parent 91c8725 commit 7f5617f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/map_location.hpp
Expand Up @@ -289,10 +289,39 @@ inline bool tiles_adjacent(const map_location& a, const map_location& b)
// or if x and y are each different by 1,
// and the x value of the hex with the greater y value is even.

switch (a.y - b.y) {
case 1 :
switch (a.x - b.x) {
case 1:
case -1:
return is_even(a.x);
case 0:
return true;
default:
return false;
}
case -1 :
switch (a.x - b.x) {
case 1:
case -1:
return is_even(b.x);
case 0:
return true;
default:
return false;
}
case 0 :
return (a.x == b.x + 1) || (a.x == b.x - 1);
default:
return false;
}

/*
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)
Expand Down

0 comments on commit 7f5617f

Please sign in to comment.