Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef MAP_LOCATION_H_INCLUDED
00019 #define MAP_LOCATION_H_INCLUDED
00020
00021 class config;
00022 class variable_set;
00023
00024 #include <string>
00025 #include <vector>
00026 #include <set>
00027 #include <boost/unordered_map.hpp>
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 struct map_location {
00038
00039 enum DIRECTION { NORTH, NORTH_EAST, SOUTH_EAST, SOUTH,
00040 SOUTH_WEST, NORTH_WEST, NDIRECTIONS };
00041
00042 static DIRECTION parse_direction(const std::string& str);
00043
00044
00045
00046
00047
00048 static std::vector<DIRECTION> parse_directions(const std::string& str);
00049 static std::string write_direction(DIRECTION dir);
00050
00051 map_location() : x(-1000), y(-1000) {}
00052 map_location(int x, int y) : x(x), y(y) {}
00053 map_location(const config& cfg, const variable_set *variables);
00054
00055 void write(config& cfg) const;
00056
00057 inline bool valid() const { return x >= 0 && y >= 0; }
00058
00059 inline bool valid(const int parWidth, const int parHeight) const
00060 {
00061 return ((x >= 0) && (y >= 0) && (x < parWidth) && (y < parHeight));
00062 }
00063
00064 int x, y;
00065 bool matches_range(const std::string& xloc, const std::string& yloc) const;
00066
00067
00068 bool operator<(const map_location& a) const { return x < a.x || (x == a.x && y < a.y); }
00069 bool operator==(const map_location& a) const { return x == a.x && y == a.y; }
00070 bool operator!=(const map_location& a) const { return !operator==(a); }
00071
00072
00073 int do_compare(const map_location& a) const {return x == a.x ? y - a.y : x - a.x; }
00074
00075
00076
00077
00078
00079
00080 map_location legacy_negation() const;
00081 map_location legacy_sum(const map_location &a) const;
00082 map_location& legacy_sum_assign(const map_location &a);
00083 map_location legacy_difference(const map_location &a) const;
00084
00085
00086
00087
00088
00089 map_location vector_negation() const;
00090 map_location vector_sum(const map_location &a) const;
00091 map_location& vector_sum_assign(const map_location &a);
00092 map_location& vector_difference_assign(const map_location &a);
00093
00094
00095 map_location get_direction(DIRECTION d, int n = 1) const;
00096 DIRECTION get_relative_dir(map_location loc) const;
00097 static DIRECTION get_opposite_dir(DIRECTION d);
00098
00099 static const map_location null_location;
00100
00101 friend std::size_t hash_value(map_location const &a);
00102 };
00103
00104
00105 bool tiles_adjacent(const map_location& a, const map_location& b);
00106
00107
00108
00109
00110
00111 void get_adjacent_tiles(const map_location& a, map_location* res);
00112
00113
00114
00115
00116
00117
00118 size_t distance_between(const map_location& a, const map_location& b);
00119
00120
00121 std::vector<map_location> parse_location_range(const std::string& xvals,
00122 const std::string &yvals, bool with_border = false);
00123
00124
00125
00126
00127
00128 void write_location_range(const std::set<map_location>& locs, config& cfg);
00129
00130
00131 void read_locations(const config& cfg, std::vector<map_location>& locs);
00132
00133
00134
00135 void write_locations(const std::vector<map_location>& locs, config& cfg);
00136
00137
00138 std::ostream &operator<<(std::ostream &s, map_location const &l);
00139
00140 std::ostream &operator<<(std::ostream &s, std::vector<map_location> const &v);
00141
00142
00143 inline std::size_t hash_value(map_location const & a){
00144 boost::hash<size_t> h;
00145 return h( (a.x << 16) ^ a.y );
00146 }
00147
00148 #endif