Small tweak to reduce code complexity, with also a 5-8% performance gain

This commit is contained in:
András Salamon 2005-05-04 21:20:05 +00:00
parent 6bc4c0885b
commit 0d0542d26f

View file

@ -7,12 +7,16 @@ size_t distance_between(const gamemap::location& a, const gamemap::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;
const size_t vdistance = abs(a.y - b.y) + vpenalty;
const size_t vsavings = minimum<int>(vdistance,hdistance/2 + hdistance%2);
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;
return hdistance + vdistance - vsavings;
// 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 maximum<int>(hdistance, abs(a.y - b.y) + vpenalty + hdistance/2);
}
void get_adjacent_tiles(const gamemap::location& a, gamemap::location* res)