30 #include <boost/functional/hash_fwd.hpp> 33 #define ERR_CF LOG_STREAM(err, log_config) 39 std::ostream &operator<<(std::ostream &s, const std::vector<map_location>& v) {
40 std::vector<map_location>::const_iterator
i = v.begin();
41 for(; i!= v.end(); ++
i) {
42 s <<
"(" << *i <<
") ";
59 std::hash<std::size_t>
h;
60 return h( (static_cast<uint32_t>(a.
x) << 16) ^ static_cast<uint32_t>(a.
y) );
75 const std::size_t open = str.find_first_of(
'('), close = str.find_last_of(
')');
76 if (open != std::string::npos && close != std::string::npos) {
77 std::string sub = str.substr(open + 1, close - open - 1);
84 const std::size_t
start = str[0] ==
'-' ? 1 : 0;
85 const std::size_t end = str.find_first_of(
':');
86 const std::string& main_dir = str.substr(start, end - start);
89 if (main_dir ==
"n") {
91 }
else if (main_dir ==
"ne") {
93 }
else if (main_dir ==
"se") {
95 }
else if (main_dir ==
"s") {
97 }
else if (main_dir ==
"sw") {
99 }
else if (main_dir ==
"nw") {
109 if (end != std::string::npos) {
110 const std::string rel_dir = str.substr(end + 1);
111 if (rel_dir ==
"cw") {
113 }
else if (rel_dir ==
"ccw") {
126 std::vector<map_location::DIRECTION> to_return;
128 std::vector<std::string>::const_iterator
i, i_end=dir_strs.end();
129 for(i = dir_strs.begin(); i != i_end; ++
i) {
133 to_return.push_back(temp);
143 return std::string(
"n");
145 return std::string(
"ne");
147 return std::string(
"nw");
149 return std::string(
"s");
151 return std::string(
"se");
153 return std::string(
"sw");
155 return std::string();
166 return _(
"North East");
168 return _(
"North West");
172 return _(
"South East");
174 return _(
"South West");
176 return std::string();
185 std::string xs = cfg[
"x"], ys = cfg[
"y"];
193 if(xs.empty() ==
false && xs !=
"recall") {
195 x = std::stoi(xs) - 1;
196 }
catch(
const std::invalid_argument&) {
197 ERR_CF <<
"Invalid map coordinate: " << xs <<
"\n";
201 if(ys.empty() ==
false && ys !=
"recall") {\
203 y = std::stoi(ys) - 1;
204 }
catch(
const std::invalid_argument&) {
205 ERR_CF <<
"Invalid map coordinate: " << ys <<
"\n";
232 if (loc.
x%2==0 &&
x%2==1) dy--;
236 int dist = std::abs(dx);
237 int dist_diag_SW_NE = std::abs(dy + (dx + (dy>0?0:1) )/2);
238 int dist_diag_SE_NW = std::abs(dy - (dx - (dy>0?0:1) )/2);
240 if (dy > 0) dir =
SOUTH;
243 if (dist_diag_SE_NW < dist) {
246 dist = dist_diag_SE_NW;
248 if (dist_diag_SW_NE < dist) {
276 }
else if (temp.
x >
x) {
278 }
else if (temp.
x <
x) {
288 std::pair<int, int> ret;
300 assert(temp == *
this);
318 if(
std::find(xloc.begin(),xloc.end(),
',') != xloc.end()
319 ||
std::find(yloc.begin(),yloc.end(),
',') != yloc.end()) {
324 for(size = xlocs.size(); size < ylocs.size(); ++
size) {
325 xlocs.emplace_back();
327 while(size > ylocs.size()) {
328 ylocs.emplace_back();
330 for(std::size_t
i = 0;
i !=
size; ++
i) {
337 const std::string::const_iterator dash =
339 if(dash != xloc.begin() && dash != xloc.end()) {
340 const std::string beg(xloc.begin(),dash);
341 const std::string end(dash+1,xloc.end());
343 int top = -1, bot = -1;
346 bot = std::stoi(beg) - 1;
347 top = std::stoi(end) - 1;
348 }
catch(
const std::invalid_argument&) {
349 ERR_CF <<
"Invalid map coordinate: " << end <<
", " << beg <<
"\n";
352 if(x < bot || x > top)
358 xval = std::stoi(xloc) - 1;
359 }
catch(
const std::invalid_argument&) {
360 ERR_CF <<
"Invalid map coordinate: " << xloc <<
"\n";
368 const std::string::const_iterator dash =
371 if(dash != yloc.begin() && dash != yloc.end()) {
372 const std::string beg(yloc.begin(),dash);
373 const std::string end(dash+1,yloc.end());
375 int top = -1, bot = -1;
378 bot = std::stoi(beg) - 1;
379 top = std::stoi(end) - 1;
380 }
catch(
const std::invalid_argument&) {
381 ERR_CF <<
"Invalid map coordinate: " << end <<
", " << beg <<
"\n";
384 if(y < bot || y > top)
390 yval = std::stoi(yloc) - 1;
391 }
catch(
const std::invalid_argument&) {
392 ERR_CF <<
"Invalid map coordinate: " << yloc <<
"\n";
416 int x_factor = (
static_cast<unsigned int> (dir) <= 2u) ? 1 : -1;
418 unsigned int tmp_y = dir - 2;
419 int y_factor = (tmp_y <= 2u) ? 1 : -1;
422 return map_location(
x + x_factor * n,
y + y_factor * ((n + ((
x & 1) == 1)) / 2));
424 return map_location(
x + x_factor * n,
y + y_factor * ((n + ((
x & 1) == 0)) / 2));
452 std::stringstream
x,
y;
453 std::set<map_location>::const_iterator
461 for(++i; i != locs.end(); ++
i) {
462 if(i->wml_x() != first->wml_x() || i->wml_y() - 1 != last->wml_y()) {
463 if (last->wml_y() != first->wml_y()) {
464 y <<
"-" << (last->wml_y());
466 x <<
"," << (i->wml_x());
467 y <<
"," << (i->wml_y());
473 if(last->wml_y() != first->wml_y())
474 y <<
"-" << (last->wml_y());
487 const std::vector<std::string> xvals =
utils::split(cfg[
"x"]);
488 const std::vector<std::string> yvals =
utils::split(cfg[
"y"]);
490 if (xvals.size() != yvals.size()) {
491 throw std::invalid_argument(
"Number of x and y coordinates do not match.");
494 std::transform(xvals.begin(), xvals.end(), yvals.begin(), std::back_inserter(locs), &
read_locations_helper);
499 std::stringstream
x,
y;
501 std::vector<map_location>::const_iterator
i = locs.begin(),
504 for(; i != end; ++
i) {
523 res->
y = a.
y - (((a.
x & 1)==0) ? 1:0);
526 res->
y = a.
y + (((a.
x & 1)==1) ? 1:0);
532 res->
y = a.
y + (((a.
x & 1)==1) ? 1:0);
535 res->
y = a.
y - (((a.
x & 1)==0) ? 1:0);
570 return (a.
x & 1) == 0;
580 return (b.
x & 1) == 0;
587 return ((a.
x - b.
x) == 1) || ((a.
x - b.
x) == - 1);
602 const std::size_t hdistance = std::abs(a.
x - b.
x);
604 const std::size_t vpenalty = ( (((a.
x & 1)==0) && ((b.
x & 1)==1) && (a.
y < b.
y))
605 || (((b.
x & 1)==0) && ((a.
x & 1)==1) && (b.
y < a.
y)) ) ? 1 : 0;
617 return std::max<int>(hdistance, std::abs(a.
y - b.
y) + vpenalty + hdistance/2);
void read_locations(const config &cfg, std::vector< map_location > &locs)
Parse x,y keys of a config into a vector of locations.
static DIRECTION parse_direction(const std::string &str)
map_location & vector_difference_assign(const map_location &a)
static std::string write_translated_direction(DIRECTION dir)
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
std::pair< int, int > get_in_basis_N_NE() const
static bool is_vertically_higher_than(const map_location &m1, const map_location &m2)
static lg::log_domain log_config("config")
static DIRECTION rotate_right(DIRECTION d, unsigned int k=1u)
static std::vector< DIRECTION > parse_directions(const std::string &str)
Parse_directions takes a comma-separated list, and filters out any invalid directions.
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
map_location get_direction(DIRECTION dir, unsigned int n=1u) const
std::ostream & operator<<(std::ostream &s, const map_location &l)
Dumps a position on a stream, for debug purposes.
static UNUSEDNOWARN std::string _(const char *str)
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
map_location rotate_right_around_center(const map_location ¢er, int k) const
General math utility functions.
static const std::vector< DIRECTION > & default_dirs()
Default list of directions.
Encapsulates the map of the game.
bool tiles_adjacent(const map_location &a, const map_location &b)
Function which tells if two locations are adjacent.
void write_location_range(const std::set< map_location > &locs, config &cfg)
Write a set of locations into a config using ranges, adding keys x=x1,..,xn and y=y1a-y1b,..,yna-ynb.
static map_location::DIRECTION s
DIRECTION
Valid directions which can be moved in our hexagonal world.
bool matches_range(const std::string &xloc, const std::string &yloc) const
static DIRECTION get_opposite_dir(DIRECTION d)
std::size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
bool find(E event, F functor)
Tests whether an event handler is available.
Standard logging facilities (interface).
static const map_location & null_location()
EXIT_STATUS start(const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
A config object defines a single node in a WML file, with access to child nodes.
static map_location::DIRECTION n
static std::string write_direction(DIRECTION dir)
static map_location read_locations_helper(const std::string &xi, const std::string &yi)
void write(config &cfg) const
void write_locations(const std::vector< map_location > &locs, config &cfg)
Write a vector of locations into a config adding keys x=x1,x2,..,xn and y=y1,y2,..,yn.
friend std::size_t hash_value(const map_location &a)