Move some simple common map_location functions from pathutils.hpp...

...to map_location.hpp where they more clearly belongs. Allow to
simplify some includes.
This commit is contained in:
Ali El Gariani 2009-08-31 12:59:22 +00:00
parent 15747118ea
commit 0984328cb0
13 changed files with 67 additions and 76 deletions

View file

@ -41,7 +41,6 @@
#include "../game_preferences.hpp"
#include "../log.hpp"
#include "../mouse_handler_base.hpp"
#include "../pathfind.hpp"
#include "play_controller.hpp"
#include "../replay.hpp"
#include "resources.hpp"

View file

@ -26,7 +26,6 @@
#include "../map.hpp"
#include "../map_location.hpp"
#include "../pathfind.hpp"
#include "../team.hpp"
#include <memory>

View file

@ -36,7 +36,6 @@
#include "map.hpp"
#include "map_label.hpp"
#include "minimap.hpp"
#include "pathutils.hpp"
#include "preferences.hpp"
#include "sdl_utils.hpp"
#include "tooltips.hpp"

View file

@ -23,7 +23,6 @@
#include "../gettext.hpp"
#include "../map_exception.hpp"
#include "../map_label.hpp"
#include "../pathutils.hpp"
#include "../wml_exception.hpp"

View file

@ -27,7 +27,6 @@
#include "gettext.hpp"
#include "log.hpp"
#include "map_exception.hpp"
#include "pathfind.hpp"
#include "serialization/parser.hpp"
#include "wml_exception.hpp"

View file

@ -298,6 +298,58 @@ 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,
const gamemap *const map)
{

View file

@ -102,7 +102,21 @@ struct map_location {
static const map_location null_location;
};
/** Function which tells if two locations are adjacent. */
bool tiles_adjacent(const map_location& a, const map_location& b);
/**
* Function which, given a location, will place all adjacent locations in res.
* res must point to an array of 6 location objects.
*/
void get_adjacent_tiles(const map_location& a, map_location* res);
/**
* Function which gives the number of hexes between two tiles
* (i.e. the minimum number of hexes that have to be traversed
* to get from one hex to the other).
*/
size_t distance_between(const map_location& a, const map_location& b);
/** Parses ranges of locations into a vector of locations. */
std::vector<map_location> parse_location_range(const std::string& xvals,

View file

@ -26,6 +26,7 @@
#include "map.hpp"
#include "mapgen.hpp"
#include "pathfind.hpp"
#include "pathutils.hpp"
#include "race.hpp"
#include "wml_exception.hpp"
#include "formula_string_utils.hpp"

View file

@ -24,7 +24,6 @@ class unit;
class unit_map;
#include "map_location.hpp"
#include "pathutils.hpp"
#include "team.hpp"
#include <map>

View file

@ -21,43 +21,6 @@
#include "pathutils.hpp"
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);
}
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);
}
void get_tile_ring(const map_location& a, const int r, std::vector<map_location>& res)
{
if(r <= 0) {
@ -82,21 +45,6 @@ void get_tiles_in_radius(const map_location& a, const int r, std::vector<map_loc
}
}
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)));
}
static void get_tiles_radius_internal(const map_location& a, size_t radius,
std::set<map_location>& res, std::map<map_location,int>& visited)
{

View file

@ -21,15 +21,6 @@
#include "map.hpp"
#include <set>
/** Function which tells if two locations are adjacent. */
bool tiles_adjacent(const map_location& a, const map_location& b);
/**
* Function which, given a location, will place all adjacent locations in res.
* res must point to an array of 6 location objects.
*/
void get_adjacent_tiles(const map_location& a, map_location* res);
/**
* Function which, given a location, will place all locations in a ring of
* distance r in res. res must be a std::vector of location
@ -58,12 +49,5 @@ void get_tiles_radius(const gamemap& map, const std::vector<map_location>& locs,
*/
void get_tiles_in_radius(const map_location& a, const int r, std::vector<map_location>& res);
/**
* Function which gives the number of hexes between two tiles
* (i.e. the minimum number of hexes that have to be traversed
* to get from one hex to the other).
*/
size_t distance_between(const map_location& a, const map_location& b);
#endif

View file

@ -18,7 +18,6 @@
#include "display.hpp"
#include "foreach.hpp"
#include "log.hpp"
#include "pathutils.hpp"
#include "sound.hpp"
#include "soundsource.hpp"

View file

@ -19,7 +19,6 @@
#include "gamestatus.hpp"
#include "log.hpp"
#include "map.hpp"
#include "pathutils.hpp"
#include "unit.hpp"
#include "unit_abilities.hpp"