The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action_wml.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Implementations of action WML tags, other than those implemented in Lua, and
18  * excluding conditional action WML.
19  */
20 
23 #include "game_events/pump.hpp"
24 
25 #include "actions/attack.hpp"
26 #include "actions/create.hpp"
27 #include "actions/move.hpp"
28 #include "actions/vision.hpp"
29 #include "ai/manager.hpp"
30 #include "fake_unit_ptr.hpp"
31 #include "filesystem.hpp"
32 #include "game_classification.hpp"
33 #include "game_display.hpp"
34 #include "preferences/game.hpp"
35 #include "gettext.hpp"
37 #include "gui/widgets/window.hpp"
38 #include "log.hpp"
39 #include "map/map.hpp"
40 #include "map/exception.hpp"
41 #include "map/label.hpp"
42 #include "pathfind/teleport.hpp"
43 #include "pathfind/pathfind.hpp"
44 #include "persist_var.hpp"
45 #include "play_controller.hpp"
46 #include "recall_list_manager.hpp"
47 #include "replay.hpp"
48 #include "random.hpp"
49 #include "mouse_handler_base.hpp" // for events::commands_disabled
50 #include "resources.hpp"
52 #include "side_filter.hpp"
53 #include "sound.hpp"
54 #include "soundsource.hpp"
55 #include "synced_context.hpp"
56 #include "synced_user_choice.hpp"
57 #include "team.hpp"
58 #include "terrain/filter.hpp"
59 #include "units/unit.hpp"
61 #include "units/udisplay.hpp"
62 #include "units/filter.hpp"
63 #include "wml_exception.hpp"
64 #include "whiteboard/manager.hpp"
65 
66 #include <boost/regex.hpp>
67 
68 static lg::log_domain log_engine("engine");
69 #define DBG_NG LOG_STREAM(debug, log_engine)
70 #define LOG_NG LOG_STREAM(info, log_engine)
71 #define WRN_NG LOG_STREAM(warn, log_engine)
72 #define ERR_NG LOG_STREAM(err, log_engine)
73 
74 static lg::log_domain log_display("display");
75 #define DBG_DP LOG_STREAM(debug, log_display)
76 #define LOG_DP LOG_STREAM(info, log_display)
77 
78 static lg::log_domain log_wml("wml");
79 #define LOG_WML LOG_STREAM(info, log_wml)
80 #define WRN_WML LOG_STREAM(warn, log_wml)
81 #define ERR_WML LOG_STREAM(err, log_wml)
82 
83 static lg::log_domain log_config("config");
84 #define ERR_CF LOG_STREAM(err, log_config)
85 
86 
87 // This file is in the game_events namespace.
88 namespace game_events
89 {
90 
91 // This must be defined before any WML actions are.
92 // (So keep it at the rop of this file?)
94 
95 namespace { // Support functions
96 
97  /**
98  * Converts a vconfig to a location (based on x,y=).
99  * The default parameter values cause the default return value (if neither
100  * x nor y is specified) to equal map_location::null_location().
101  */
102  map_location cfg_to_loc(const vconfig& cfg, int defaultx = -999, int defaulty = -999)
103  {
104  return map_location(cfg["x"].to_int(defaultx), cfg["y"].to_int(defaulty), wml_loc());
105  }
106 
107  fake_unit_ptr create_fake_unit(const vconfig& cfg)
108  {
109  std::string type = cfg["type"];
110  std::string variation = cfg["variation"];
111  std::string img_mods = cfg["image_mods"];
112 
113  size_t side_num = cfg["side"].to_int(1);
114  if (!resources::gameboard->has_team(side_num)) {
115  side_num = 1;
116  }
117 
118  unit_race::GENDER gender = string_gender(cfg["gender"]);
119  const unit_type *ut = unit_types.find(type);
120  if (!ut) return fake_unit_ptr();
121  fake_unit_ptr fake = fake_unit_ptr(unit_ptr(new unit(*ut, side_num, false, gender)));
122 
123  if(!variation.empty()) {
124  config mod;
125  config &effect = mod.add_child("effect");
126  effect["apply_to"] = "variation";
127  effect["name"] = variation;
128  fake->add_modification("variation",mod);
129  }
130 
131  if(!img_mods.empty()) {
132  config mod;
133  config &effect = mod.add_child("effect");
134  effect["apply_to"] = "image_mod";
135  effect["add"] = img_mods;
136  fake->add_modification("image_mod",mod);
137  }
138 
139  return fake;
140  }
141 
142  std::vector<map_location> fake_unit_path(const unit& fake_unit, const std::vector<std::string>& xvals, const std::vector<std::string>& yvals)
143  {
144  const gamemap *game_map = & resources::gameboard->map();
145  std::vector<map_location> path;
146  map_location src;
147  map_location dst;
148  for(size_t i = 0; i != std::min(xvals.size(),yvals.size()); ++i) {
149  if(i==0){
150  try {
151  src.set_wml_x(std::stoi(xvals[i]));
152  src.set_wml_y(std::stoi(yvals[i]));
153  } catch(std::invalid_argument) {
154  ERR_CF << "Invalid move_unit_fake source: " << xvals[i] << ", " << yvals[i] << '\n';
155  continue;
156  }
157  if (!game_map->on_board(src)) {
158  ERR_CF << "Invalid move_unit_fake source: " << src << '\n';
159  break;
160  }
161  path.push_back(src);
162  continue;
163  }
164  pathfind::shortest_path_calculator calc(fake_unit,
165  resources::gameboard->get_team(fake_unit.side()),
166  resources::gameboard->teams(),
167  *game_map);
168 
169  try {
170  dst.set_wml_x(std::stoi(xvals[i]));
171  dst.set_wml_y(std::stoi(yvals[i]));
172  } catch(std::invalid_argument) {
173  ERR_CF << "Invalid move_unit_fake destination: " << xvals[i] << ", " << yvals[i] << '\n';
174  }
175  if (!game_map->on_board(dst)) {
176  ERR_CF << "Invalid move_unit_fake destination: " << dst << '\n';
177  break;
178  }
179 
180  pathfind::plain_route route = pathfind::a_star_search(src, dst, 10000, calc,
181  game_map->w(), game_map->h());
182 
183  if (route.steps.empty()) {
184  WRN_NG << "Could not find move_unit_fake route from " << src << " to " << dst << ": ignoring complexities" << std::endl;
185  pathfind::emergency_path_calculator emergency_calc(fake_unit, *game_map);
186 
187  route = pathfind::a_star_search(src, dst, 10000, emergency_calc,
188  game_map->w(), game_map->h());
189  if(route.steps.empty()) {
190  // This would occur when trying to do a MUF of a unit
191  // over locations which are unreachable to it (infinite movement
192  // costs). This really cannot fail.
193  WRN_NG << "Could not find move_unit_fake route from " << src << " to " << dst << ": ignoring terrain" << std::endl;
194  pathfind::dummy_path_calculator dummy_calc(fake_unit, *game_map);
195  route = a_star_search(src, dst, 10000, dummy_calc, game_map->w(), game_map->h());
196  assert(!route.steps.empty());
197  }
198  }
199  // we add this section to the end of the complete path
200  // skipping section's head because already included
201  // by the previous iteration
202  path.insert(path.end(),
203  route.steps.begin()+1, route.steps.end());
204 
205  src = dst;
206  }
207  return path;
208  }
209 } // end anonymous namespace (support functions)
210 
211 /**
212  * Using this constructor for a static object outside action_wml.cpp
213  * will likely lead to a static initialization fiasco.
214  * @param[in] tag The WML tag for this action.
215  * @param[in] function The callback for this action.
216  */
217 wml_action::wml_action(const std::string & tag, handler function)
218 {
219  registry_[tag] = function;
220 }
221 
222 
223 /**
224  * WML_HANDLER_FUNCTION macro handles auto registration for wml handlers
225  *
226  * @param pname wml tag name
227  * @param pei the variable name of the queued_event object inside the function
228  * @param pcfg the variable name of the config object inside the function
229  *
230  * You are warned! This is evil macro magic!
231  *
232  * The following code registers a [foo] tag:
233  * \code
234  * // comment out unused parameters to prevent compiler warnings
235  * WML_HANDLER_FUNCTION(foo, event_info, cfg)
236  * {
237  * // code for foo
238  * }
239  * \endcode
240  *
241  * Generated code looks like this:
242  * \code
243  * void wml_func_foo(...);
244  * static wml_action wml_action_foo("foo", &wml_func_foo);
245  * void wml_func_foo(...)
246  * {
247  * // code for foo
248  * }
249  * \endcode
250  */
251 #define WML_HANDLER_FUNCTION(pname, pei, pcfg) \
252  static void wml_func_##pname(const queued_event &pei, const vconfig &pcfg); \
253  static wml_action wml_action_##pname(#pname, &wml_func_##pname); \
254  static void wml_func_##pname(const queued_event& pei, const vconfig& pcfg)
255 
256 
257 /// Experimental data persistence
258 /// @todo Finish experimenting.
260 {
261  if (!resources::controller->is_replay())
263 }
264 
265 static void on_replay_error(const std::string& message, bool /*b*/)
266 {
267  ERR_NG << "Error via [do_command]:" << std::endl;
268  ERR_NG << message << std::endl;
269 }
270 
271 // This tag exposes part of the code path used to handle [command]'s in replays
272 // This allows to perform scripting in WML that will use the same code path as player actions, for example.
273 WML_HANDLER_FUNCTION(do_command,, cfg)
274 {
275  // Doing this in a whiteboard applied context will cause bugs
276  // Note that even though game_events::wml_event_pump() will always apply the real unit map
277  // It is still possible get a wml commands to run in a whiteboard applied context
278  // With the theme_items lua callbacks
279  if(resources::whiteboard->has_planned_unit_map())
280  {
281  ERR_NG << "[do_command] called while whiteboard is applied, ignoring" << std::endl;
282  return;
283  }
284 
285  static const std::set<std::string> allowed_tags {"attack", "move", "recruit", "recall", "disband", "fire_event", "lua_ai"};
286 
287  const bool is_too_early = resources::gamedata->phase() != game_data::START && resources::gamedata->phase() != game_data::PLAY;
288  const bool is_unsynced_too_early = resources::gamedata->phase() != game_data::PLAY;
289  const bool is_unsynced = synced_context::get_synced_state() == synced_context::UNSYNCED;
290  if(is_too_early)
291  {
292  ERR_NG << "[do_command] called too early, only allowed at START or later" << std::endl;
293  return;
294  }
295  if(is_unsynced && resources::controller->is_lingering())
296  {
297  ERR_NG << "[do_command] cannot be used in linger mode" << std::endl;
298  return;
299  }
300  if(is_unsynced && resources::controller->gamestate().init_side_done())
301  {
302  ERR_NG << "[do_command] cannot be used before the turn has started" << std::endl;
303  return;
304  }
305  if(is_unsynced && is_unsynced_too_early)
306  {
307  ERR_NG << "[do_command] called too early" << std::endl;
308  return;
309  }
310  if(is_unsynced && events::commands_disabled)
311  {
312  ERR_NG << "[do_command] cannot invoke synced commands while commands are blocked" << std::endl;
313  return;
314  }
315  if(is_unsynced && !resources::controller->current_team().is_local())
316  {
317  ERR_NG << "[do_command] can only be used from clients that control the currently playing side" << std::endl;
318  return;
319  }
320  for(vconfig::all_children_iterator i = cfg.ordered_begin(); i != cfg.ordered_end(); ++i)
321  {
322  if(allowed_tags.find( i.get_key()) == allowed_tags.end()) {
323  ERR_NG << "unsupported tag [" << i.get_key() << "] in [do_command]" << std::endl;
324  std::stringstream o;
325  std::copy(allowed_tags.begin(), allowed_tags.end(), std::ostream_iterator<std::string>(o, " "));
326  ERR_NG << "allowed tags: " << o.str() << std::endl;
327  continue;
328  }
329  // TODO: afaik run_in_synced_context_if_not_already thows exceptions when the executed action end the scenario or the turn.
330  // This could cause problems, specially when its unclear whether that excetion is caught by lua or not...
331 
332  //Note that this fires related events and everthing else that also happen normally.
333  //have to watch out with the undo stack, therefore forbid [auto_shroud] and [update_shroud] here...
335  /*commandname*/ i.get_key(),
336  /*data*/ i.get_child().get_parsed_config(),
337  /*use_undo*/ true,
338  /*show*/ true,
339  /*error_handler*/ &on_replay_error
340  );
341  }
342 }
343 
344 /// Experimental data persistence
345 /// @todo Finish experimenting.
347 {
349 }
350 
351 WML_HANDLER_FUNCTION(modify_turns,, cfg)
352 {
353  config::attribute_value value = cfg["value"];
354  std::string add = cfg["add"];
355  config::attribute_value current = cfg["current"];
357  if(!add.empty()) {
358  tod_man.modify_turns_by_wml(add);
359  } else if(!value.empty()) {
360  tod_man.set_number_of_turns_by_wml(value.to_int(-1));
361  }
362  // change current turn only after applying mods
363  if(!current.empty()) {
364  const unsigned int current_turn_number = tod_man.turn();
365  int new_turn_number = current.to_int(current_turn_number);
366  const unsigned int new_turn_number_u = static_cast<unsigned int>(new_turn_number);
367  if(new_turn_number_u < 1 || (new_turn_number > tod_man.number_of_turns() && tod_man.number_of_turns() != -1)) {
368  ERR_NG << "attempted to change current turn number to one out of range (" << new_turn_number << ")" << std::endl;
369  } else if(new_turn_number_u != current_turn_number) {
370  tod_man.set_turn_by_wml(new_turn_number_u, resources::gamedata);
372  }
373  }
374 }
375 
376 /// Moving a 'unit' - i.e. a dummy unit
377 /// that is just moving for the visual effect
378 WML_HANDLER_FUNCTION(move_unit_fake,, cfg)
379 {
380  events::command_disabler command_disabler;
381  fake_unit_ptr dummy_unit(create_fake_unit(cfg));
382  if(!dummy_unit.get())
383  return;
384 
385  const bool force_scroll = cfg["force_scroll"].to_bool(true);
386 
387  const std::string x = cfg["x"];
388  const std::string y = cfg["y"];
389 
390  const std::vector<std::string> xvals = utils::split(x);
391  const std::vector<std::string> yvals = utils::split(y);
392 
393  const std::vector<map_location>& path = fake_unit_path(*dummy_unit, xvals, yvals);
394  if (!path.empty()) {
395  // Always scroll.
396  unit_display::move_unit(path, dummy_unit.get_unit_ptr(), true, map_location::NDIRECTIONS, force_scroll);
397  }
398 }
399 
400 WML_HANDLER_FUNCTION(move_units_fake,, cfg)
401 {
402  events::command_disabler command_disabler;
403  LOG_NG << "Processing [move_units_fake]\n";
404 
405  const vconfig::child_list unit_cfgs = cfg.get_children("fake_unit");
406  size_t num_units = unit_cfgs.size();
407  std::vector<fake_unit_ptr > units;
408  units.reserve(num_units);
409  std::vector<std::vector<map_location> > paths;
410  paths.reserve(num_units);
411 
412  LOG_NG << "Moving " << num_units << " units\n";
413 
414  size_t longest_path = 0;
415 
416  for (const vconfig& config : unit_cfgs) {
417  const std::vector<std::string> xvals = utils::split(config["x"]);
418  const std::vector<std::string> yvals = utils::split(config["y"]);
419  int skip_steps = config["skip_steps"];
420  fake_unit_ptr u = create_fake_unit(config);
421  units.push_back(u);
422  paths.push_back(fake_unit_path(*u, xvals, yvals));
423  if(skip_steps > 0)
424  paths.back().insert(paths.back().begin(), skip_steps, paths.back().front());
425  longest_path = std::max(longest_path, paths.back().size());
426  DBG_NG << "Path " << paths.size() - 1 << " has length " << paths.back().size() << '\n';
427 
428  u->set_location(paths.back().front());
430  }
431 
432  LOG_NG << "Units placed, longest path is " << longest_path << " long\n";
433 
434  std::vector<map_location> path_step(2);
435  path_step.resize(2);
436  for(size_t step = 1; step < longest_path; ++step) {
437  DBG_NG << "Doing step " << step << "...\n";
438  for(size_t un = 0; un < num_units; ++un) {
439  if(step >= paths[un].size() || paths[un][step - 1] == paths[un][step])
440  continue;
441  DBG_NG << "Moving unit " << un << ", doing step " << step << '\n';
442  path_step[0] = paths[un][step - 1];
443  path_step[1] = paths[un][step];
444  unit_display::move_unit(path_step, units[un].get_unit_ptr());
445  units[un]->set_location(path_step[1]);
446  units[un]->anim_comp().set_standing();
447  }
448  }
449 
450  LOG_NG << "Units moved\n";
451 }
452 
453 /// If we should recall units that match a certain description.
454 // If you change attributes specific to [recall] (that is, not a Standard Unit Filter)
455 // be sure to update data/lua/wml_tag, auto_recall feature for [role] to reflect your changes.
456 WML_HANDLER_FUNCTION(recall,, cfg)
457 {
458  events::command_disabler command_disabler;
459  LOG_NG << "recalling unit...\n";
460  config temp_config(cfg.get_config());
461  // Prevent the recall unit filter from using the location as a criterion
462 
463  /**
464  * @todo FIXME: we should design the WML to avoid these types of
465  * collisions; filters should be named consistently and always have a
466  * distinct scope.
467  */
468  temp_config["x"] = "recall";
469  temp_config["y"] = "recall";
470  vconfig unit_filter_cfg(temp_config);
471  const vconfig & leader_filter = cfg.child("secondary_unit");
472 
473  for(int index = 0; index < int(resources::gameboard->teams().size()); ++index) {
474  LOG_NG << "for side " << index + 1 << "...\n";
475  const std::string player_id = resources::gameboard->teams()[index].save_id();
476 
477  if(resources::gameboard->teams()[index].recall_list().size() < 1) {
478  DBG_NG << "recall list is empty when trying to recall!\n"
479  << "player_id: " << player_id << " side: " << index+1 << "\n";
480  continue;
481  }
482 
483  recall_list_manager & avail = resources::gameboard->teams()[index].recall_list();
484  std::vector<unit_map::unit_iterator> leaders = resources::gameboard->units().find_leaders(index + 1);
485 
486  const unit_filter ufilt(unit_filter_cfg, resources::filter_con);
487  const unit_filter lfilt(leader_filter, resources::filter_con); // Note that if leader_filter is null, this correctly gives a null filter that matches all units.
488  for(std::vector<unit_ptr>::iterator u = avail.begin(); u != avail.end(); ++u) {
489  DBG_NG << "checking unit against filter...\n";
490  scoped_recall_unit auto_store("this_unit", player_id, u - avail.begin());
491  if (ufilt(*(*u), map_location())) {
492  DBG_NG << (*u)->id() << " matched the filter...\n";
493  const unit_ptr to_recruit = *u;
494  const unit* pass_check = to_recruit.get();
495  if(!cfg["check_passability"].to_bool(true)) pass_check = nullptr;
496  const map_location cfg_loc = cfg_to_loc(cfg);
497 
498  /// @todo fendrin: comment this monster
499  for (unit_map::const_unit_iterator leader : leaders) {
500  DBG_NG << "...considering " + leader->id() + " as the recalling leader...\n";
501  map_location loc = cfg_loc;
502  if ( lfilt(*leader) &&
503  unit_filter(vconfig(leader->recall_filter()), resources::filter_con).matches( *(*u),map_location() ) ) {
504  DBG_NG << "...matched the leader filter and is able to recall the unit.\n";
505  if(!resources::gameboard->map().on_board(loc))
506  loc = leader->get_location();
507  if(pass_check || (resources::gameboard->units().count(loc) > 0))
508  loc = pathfind::find_vacant_tile(loc, pathfind::VACANT_ANY, pass_check);
509  if(resources::gameboard->map().on_board(loc)) {
510  DBG_NG << "...valid location for the recall found. Recalling.\n";
511  avail.erase(u); // Erase before recruiting, since recruiting can fire more events
512  actions::place_recruit(to_recruit, loc, leader->get_location(), 0, true,
513  map_location::parse_direction(cfg["facing"]),
514  cfg["show"].to_bool(true), cfg["fire_event"].to_bool(false),
515  true, true);
516  return;
517  }
518  }
519  }
520  if (resources::gameboard->map().on_board(cfg_loc)) {
521  map_location loc = cfg_loc;
522  if(pass_check || (resources::gameboard->units().count(loc) > 0))
523  loc = pathfind::find_vacant_tile(loc, pathfind::VACANT_ANY, pass_check);
524  // Check if we still have a valid location
525  if (resources::gameboard->map().on_board(loc)) {
526  DBG_NG << "No usable leader found, but found usable location. Recalling.\n";
527  avail.erase(u); // Erase before recruiting, since recruiting can fire more events
528  map_location null_location = map_location::null_location();
529  actions::place_recruit(to_recruit, loc, null_location, 0, true,
530  map_location::parse_direction(cfg["facing"]),
531  cfg["show"].to_bool(true), cfg["fire_event"].to_bool(false),
532  true, true);
533  return;
534  }
535  }
536  }
537  }
538  }
539  LOG_WML << "A [recall] tag with the following content failed:\n" << cfg.get_config().debug();
540 }
541 
542 namespace {
543  struct map_choice : public mp_sync::user_choice
544  {
545  map_choice(const std::string& filename) : filename_(filename) {}
547  virtual config query_user(int /*side*/) const
548  {
549  //Do a regex check for the file format to prevent sending aribitary files to other clients.
550  //Note: this allows only the new format.
551  static const std::string s_simple_terrain = R"""([A-Za-z\\|/]{1,4})""";
552  static const std::string s_terrain = s_simple_terrain + R"""((\^)""" + s_simple_terrain + ")?";
553  static const std::string s_sep = "(, |\\n)";
554  static const std::string s_prefix = R"""((\d+ )?)""";
555  static const std::string s_all = "(" + s_prefix + s_terrain + s_sep + ")+";
556  static const boost::regex r_all(s_all);
557 
558  const std::string& mapfile = filesystem::get_wml_location(filename_);
559  std::string res = "";
560  if(filesystem::file_exists(mapfile)) {
561  res = filesystem::read_file(mapfile);
562  }
563  config retv;
564  if(boost::regex_match(res, r_all))
565  {
566  retv["map_data"] = res;
567  }
568  return retv;
569  }
570  virtual config random_choice(int /*side*/) const
571  {
572  return config();
573  }
574  virtual std::string description() const
575  {
576  return "Map Data";
577  }
578 
579  };
580 }
581 
582 /// Experimental map replace
583 /// @todo Finish experimenting.
584 WML_HANDLER_FUNCTION(replace_map,, cfg)
585 {
586  /*
587  * When a hex changes from a village terrain to a non-village terrain, and
588  * a team owned that village it loses that village. When a hex changes from
589  * a non-village terrain to a village terrain and there is a unit on that
590  * hex it does not automatically capture the village. The reason for not
591  * capturing villages it that there are too many choices to make; should a
592  * unit loose its movement points, should capture events be fired. It is
593  * easier to do this as wanted by the author in WML.
594  */
595 
596  const gamemap * game_map = & resources::gameboard->map();
597  gamemap map(*game_map);
598 
599  try {
600  if(!cfg["map_file"].empty()) {
601  config file_cfg = mp_sync::get_user_choice("map_data", map_choice(cfg["map_file"].str()));
602  map.read(file_cfg["map_data"].str(), false);
603  } else {
604  map.read(cfg["map"], false);
605  }
606  } catch(incorrect_map_format_error&) {
607  const std::string log_map_name = cfg["map"].empty() ? cfg["file"] : std::string("from inline data");
608  lg::wml_error() << "replace_map: Unable to load map " << log_map_name << std::endl;
609  return;
610  } catch(wml_exception& e) {
611  e.show(resources::screen->video());
612  return;
613  }
614 
615  if (map.total_width() > game_map->total_width()
616  || map.total_height() > game_map->total_height()) {
617  if (!cfg["expand"].to_bool()) {
618  lg::wml_error() << "replace_map: Map dimension(s) increase but expand is not set" << std::endl;
619  return;
620  }
621  }
622 
623  if (map.total_width() < game_map->total_width()
624  || map.total_height() < game_map->total_height()) {
625  if (!cfg["shrink"].to_bool()) {
626  lg::wml_error() << "replace_map: Map dimension(s) decrease but shrink is not set" << std::endl;
627  return;
628  }
629  }
630 
631  boost::optional<std::string> errmsg = resources::gameboard->replace_map(map);
632 
633  if (errmsg) {
634  lg::wml_error() << *errmsg << std::endl;
635  }
636 
640 }
641 
642 /// Experimental data persistence
643 /// @todo Finish experimenting.
645 {
646  if (!resources::controller->is_replay())
648 }
649 
650 WML_HANDLER_FUNCTION(set_variables,, cfg)
651 {
652  const t_string& name = cfg["name"];
654  if(name.empty()) {
655  ERR_NG << "trying to set a variable with an empty name:\n" << cfg.get_config().debug();
656  return;
657  }
658 
659  std::vector<config> data;
660  if(cfg.has_attribute("to_variable"))
661  {
662  try
663  {
665  for (const config& c : tovar.as_array())
666  {
667  data.push_back(c);
668  }
669  }
670  catch(const invalid_variablename_exception&)
671  {
672  ERR_NG << "Cannot do [set_variables] with invalid to_variable variable: " << cfg["to_variable"] << " with " << cfg.get_config().debug() << std::endl;
673  }
674  } else {
675  typedef std::pair<std::string, vconfig> vchild;
676  for (const vchild& p : cfg.all_ordered()) {
677  if(p.first == "value") {
678  data.push_back(p.second.get_parsed_config());
679  } else if(p.first == "literal") {
680  data.push_back(p.second.get_config());
681  } else if(p.first == "split") {
682  const vconfig & split_element = p.second;
683 
684  std::string split_string=split_element["list"];
685  std::string separator_string=split_element["separator"];
686  std::string key_name=split_element["key"];
687  if(key_name.empty())
688  {
689  key_name="value";
690  }
691 
692  bool remove_empty = split_element["remove_empty"].to_bool();
693 
694  char* separator = separator_string.empty() ? nullptr : &separator_string[0];
695 
696  std::vector<std::string> split_vector;
697 
698  //if no separator is specified, explode the string
699  if(separator == nullptr)
700  {
701  for(std::string::iterator i=split_string.begin(); i!=split_string.end(); ++i)
702  {
703  split_vector.push_back(std::string(1, *i));
704  }
705  }
706  else {
707  split_vector=utils::split(split_string, *separator, remove_empty ? utils::REMOVE_EMPTY | utils::STRIP_SPACES : utils::STRIP_SPACES);
708  }
709 
710  for(std::vector<std::string>::iterator i=split_vector.begin(); i!=split_vector.end(); ++i)
711  {
712  data.emplace_back(config {key_name, *i});
713  }
714  }
715  }
716  }
717  try
718  {
719  const std::string& mode = cfg["mode"];
720  if(mode == "merge")
721  {
722  if(dest.explicit_index() && data.size() > 1)
723  {
724  //merge children into one
725  config merged_children;
726  for (const config &ch : data) {
727  merged_children.append(ch);
728  }
729  data = {merged_children};
730  }
731  dest.merge_array(data);
732  }
733  else if(mode == "insert")
734  {
735  dest.insert_array(data);
736  }
737  else if(mode == "append")
738  {
739  dest.append_array(data);
740  }
741  else /*default if(mode == "replace")*/
742  {
743  dest.replace_array(data);
744  }
745  }
746  catch(const invalid_variablename_exception&)
747  {
748  ERR_NG << "Cannot do [set_variables] with invalid destination variable: " << name << " with " << cfg.get_config().debug() << std::endl;
749  }
750 }
751 
752 /// Store the relative direction from one hex to another in a WML variable.
753 /// This is mainly useful as a diagnostic tool, but could be useful
754 /// for some kind of scenario.
755 WML_HANDLER_FUNCTION(store_relative_direction,, cfg)
756 {
757  if (!cfg.child("source")) {
758  WRN_NG << "No source in [store_relative_direction]" << std::endl;
759  return;
760  }
761  if (!cfg.child("destination")) {
762  WRN_NG << "No destination in [store_relative_direction]" << std::endl;
763  return;
764  }
765  if (!cfg.has_attribute("variable")) {
766  WRN_NG << "No variable in [store_relative_direction]" << std::endl;
767  return;
768  }
769 
770  const map_location src = cfg_to_loc(cfg.child("source"));
771  const map_location dst = cfg_to_loc(cfg.child("destination"));
772 
773  std::string variable = cfg["variable"];
774  map_location::RELATIVE_DIR_MODE mode = static_cast<map_location::RELATIVE_DIR_MODE> (cfg["mode"].to_int(0));
775  try
776  {
778 
780  }
781  catch(const invalid_variablename_exception&)
782  {
783  ERR_NG << "Cannot do [store_relative_direction] with invalid destination variable: " << variable << " with " << cfg.get_config().debug() << std::endl;
784  }
785 }
786 
787 /// Store the rotation of one hex around another in a WML variable.
788 /// In increments of 60 degrees, clockwise.
789 /// This is mainly useful as a diagnostic tool, but could be useful
790 /// for some kind of scenario.
791 WML_HANDLER_FUNCTION(store_rotate_map_location,, cfg)
792 {
793  if (!cfg.child("source")) {
794  WRN_NG << "No source in [store_rotate_map_location]" << std::endl;
795  return;
796  }
797  if (!cfg.child("destination")) {
798  WRN_NG << "No destination in [store_rotate_map_location]" << std::endl;
799  return;
800  }
801  if (!cfg.has_attribute("variable")) {
802  WRN_NG << "No variable in [store_rotate_map_location]" << std::endl;
803  return;
804  }
805 
806  const map_location src = cfg_to_loc(cfg.child("source"));
807  const map_location dst = cfg_to_loc(cfg.child("destination"));
808 
809  std::string variable = cfg["variable"];
810  int angle = cfg["angle"].to_int(1);
811 
812  try
813  {
815 
816  dst.rotate_right_around_center(src,angle).write(store.as_container());
817  }
818  catch(const invalid_variablename_exception&)
819  {
820  ERR_NG << "Cannot do [store_rotate_map_location] with invalid destination variable: " << variable << " with " << cfg.get_config().debug() << std::endl;
821  }
822 }
823 
824 
825 /// Store time of day config in a WML variable. This is useful for those who
826 /// are too lazy to calculate the corresponding time of day for a given turn,
827 /// or if the turn / time-of-day sequence mutates in a scenario.
828 WML_HANDLER_FUNCTION(store_time_of_day,, cfg)
829 {
830  const map_location loc = cfg_to_loc(cfg);
831  int turn = cfg["turn"];
832  // using 0 will use the current turn
833  const time_of_day& tod = resources::tod_manager->get_time_of_day(loc,turn);
834 
835  std::string variable = cfg["variable"];
836  if(variable.empty()) {
837  variable = "time_of_day";
838  }
839  try
840  {
842  tod.write(store.as_container());
843  }
844  catch(const invalid_variablename_exception&)
845  {
846  ERR_NG << "Found invalid variablename " << variable << " in [store_time_of_day] with " << cfg.get_config().debug() << "\n";
847  }
848 }
849 
850 /// Creating a mask of the terrain
852 {
853  map_location loc = cfg_to_loc(cfg, 1, 1);
854 
855  gamemap mask_map(resources::gameboard->map().tdata(), "");
856 
857  try {
858  if(!cfg["mask_file"].empty()) {
859  const std::string& maskfile = filesystem::get_wml_location(cfg["mask_file"].str());
860 
861  if(filesystem::file_exists(maskfile)) {
862  mask_map.read(filesystem::read_file(maskfile), false);
863  } else {
864  throw incorrect_map_format_error("Invalid file path");
865  }
866  } else {
867  mask_map.read(cfg["mask"], false);
868  }
869  } catch(incorrect_map_format_error&) {
870  ERR_NG << "terrain mask is in the incorrect format, and couldn't be applied" << std::endl;
871  return;
872  } catch(wml_exception& e) {
873  e.show(resources::screen->video());
874  return;
875  }
876 
877  if (!cfg["border"].to_bool(true)) {
878  mask_map.add_fog_border();
879  }
880 
881  resources::gameboard->overlay_map(mask_map, cfg.get_parsed_config(), loc);
883 }
884 
885 WML_HANDLER_FUNCTION(tunnel,, cfg)
886 {
887  const bool remove = cfg["remove"].to_bool(false);
888  const bool delay = cfg["delayed_variable_substitution"].to_bool(true);
889  if (remove) {
890  const std::vector<std::string> ids = utils::split(cfg["id"]);
891  for (const std::string &id : ids) {
893  }
894  } else if (cfg.get_children("source").empty() ||
895  cfg.get_children("target").empty() ||
896  cfg.get_children("filter").empty()) {
897  ERR_WML << "[tunnel] is missing a mandatory tag:\n"
898  << cfg.get_config().debug();
899  } else {
900  pathfind::teleport_group tunnel(delay ? cfg : vconfig(cfg.get_parsed_config()), false);
901  resources::tunnels->add(tunnel);
902 
903  if(cfg["bidirectional"].to_bool(true)) {
904  tunnel = pathfind::teleport_group(delay ? cfg : vconfig(cfg.get_parsed_config()), true);
905  resources::tunnels->add(tunnel);
906  }
907  }
908 }
909 
910 /// If we should spawn a new unit on the map somewhere
912 {
913  events::command_disabler command_disabler;
914  config parsed_cfg = cfg.get_parsed_config();
915 
916  config::attribute_value to_variable = cfg["to_variable"];
917  if (!to_variable.blank())
918  {
919  parsed_cfg.remove_attribute("to_variable");
920  unit new_unit(parsed_cfg, true, &cfg);
921  try
922  {
923  config &var = resources::gamedata->get_variable_cfg(to_variable);
924  var.clear();
925  new_unit.write(var);
926  if (const config::attribute_value *v = parsed_cfg.get("x")) var["x"] = *v;
927  if (const config::attribute_value *v = parsed_cfg.get("y")) var["y"] = *v;
928  }
929  catch(const invalid_variablename_exception&)
930  {
931  ERR_NG << "Cannot do [unit] with invalid to_variable: " << to_variable << " with " << cfg.get_config().debug() << std::endl;
932  }
933  return;
934 
935  }
936 
937  int side = parsed_cfg["side"].to_int(1);
938 
939 
940  if ((side<1)||(side > static_cast<int>(resources::gameboard->teams().size()))) {
941  ERR_NG << "wrong side in [unit] tag - no such side: "<<side<<" ( number of teams :"<<resources::gameboard->teams().size()<<")"<<std::endl;
942  DBG_NG << parsed_cfg.debug();
943  return;
944  }
945  team &tm = resources::gameboard->get_team(side);
946 
947  unit_creator uc(tm,resources::gameboard->map().starting_position(side));
948 
949  uc
950  .allow_add_to_recall(true)
951  .allow_discover(true)
952  .allow_get_village(true)
953  .allow_invalidate(true)
954  .allow_rename_side(true)
955  .allow_show(true);
956 
957  uc.add_unit(parsed_cfg, &cfg);
958 
959 }
960 
961 WML_HANDLER_FUNCTION(on_undo, event_info, cfg)
962 {
963  if(cfg["delayed_variable_substitution"].to_bool(false)) {
964  synced_context::add_undo_commands(cfg.get_config(), event_info);
965  } else {
966  synced_context::add_undo_commands(cfg.get_parsed_config(), event_info);
967  }
968 }
969 
971 {
972 }
973 
974 } // end namespace game_events
975 
void set_wml_y(int v)
Definition: location.hpp:119
int total_width() const
Real width of the map, including borders.
Definition: map.hpp:99
void verify_and_get_global_variable(const vconfig &pcfg)
play_controller * controller
Definition: resources.cpp:21
static void add_undo_commands(const config &commands, const game_events::queued_event &ctx)
std::map< std::string, handler > map
Definition: action_wml.hpp:51
unit_creator & allow_rename_side(bool b)
config get_user_choice(const std::string &name, const user_choice &uch, int side=0)
#define ERR_NG
Definition: action_wml.cpp:72
static void get_global_variable(persist_context &ctx, const vconfig &pcfg)
Definition: persist_var.cpp:59
static DIRECTION parse_direction(const std::string &str)
Definition: location.cpp:64
void overlay_map(const gamemap &o, const config &cfg, map_location loc)
Definition: game_board.cpp:289
::tod_manager * tod_manager
Definition: resources.cpp:30
PHASE phase() const
Definition: game_data.hpp:76
REMOVE_EMPTY: remove empty elements.
std::vector< unit_iterator > find_leaders(int side)
Definition: map.cpp:357
virtual const unit_map & units() const
Definition: game_board.hpp:97
std::vector< char_t > string
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
Function which doesn't take anything into account.
Definition: pathfind.hpp:254
maybe_const_t< config::attribute_value, V > & as_scalar() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
#define LOG_NG
Definition: action_wml.cpp:70
This class represents a single unit of a specific type.
Definition: unit.hpp:101
wml_action(const std::string &tag, handler function)
Using this constructor for a static object outside action_wml.cpp will likely lead to a static initia...
Definition: action_wml.cpp:217
void append(const config &cfg)
Append data from another config object to this one.
Definition: config.cpp:250
Various functions implementing vision (through fog of war and shroud).
unit_creator & allow_invalidate(bool b)
static void set_global_variable(persist_context &ctx, const vconfig &pcfg)
Definition: persist_var.cpp:94
unit_creator & allow_show(bool b)
Various functions that implement attacks and attack calculations.
void remove(const std::string &id)
Definition: teleport.cpp:298
Variant for storing WML attributes.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
internal_ptr get_unit_ptr()
Get a copy of the internal unit pointer.
config & get_variable_cfg(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:79
vconfig child(const std::string &key) const
Returns a child of *this whose key is key.
Definition: variable.cpp:252
static void raise_map_changed()
Notifies all observers of 'ai_map_changed' event.
Definition: manager.cpp:453
void place_on_fake_unit_manager(fake_unit_manager *d)
Place this on manager's fake_units_ dequeue.
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
Definition: tod_manager.hpp:54
game_display * screen
Definition: resources.cpp:27
bool empty() const
Tests for an attribute that either was never set or was set to "".
#define LOG_WML
Definition: action_wml.cpp:79
map_location find_vacant_tile(const map_location &loc, VACANT_TILE_TYPE vacancy, const unit *pass_check, const team *shroud_check, const game_board *board)
Function that will find a location on the board that is as near to loc as possible, but which is unoccupied by any units.
Definition: pathfind.cpp:54
This file contains the window object, this object is a top level container which has the event manage...
unit_creator & allow_discover(bool b)
static void on_replay_error(const std::string &message, bool)
Definition: action_wml.cpp:265
WML_HANDLER_FUNCTION(clear_global_variable,, pcfg)
Experimental data persistence.
Definition: action_wml.cpp:259
void modify_turns_by_wml(const std::string &mod)
unit_type_data unit_types
Definition: types.cpp:1455
void set_wml_x(int v)
Definition: location.hpp:118
Define actions for the game's events mechanism.
virtual const std::vector< team > & teams() const
Definition: game_board.hpp:92
Replay control code.
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:225
static lg::log_domain log_config("config")
void verify_and_set_global_variable(const vconfig &pcfg)
Additional functionality for a non-const variable_info.
void clear()
Definition: config.cpp:742
std::string debug() const
Definition: config.cpp:1131
-file sdl_utils.hpp
void remove_attribute(config_key_type key)
Definition: config.cpp:218
unit_creator & allow_add_to_recall(bool b)
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.
static synced_state get_synced_state()
void add_fog_border()
Definition: map.cpp:453
void new_turn()
Update lighting settings.
A single unit type that the player may recruit.
Definition: types.hpp:43
std::string terrain_mask
game_data * gamedata
Definition: resources.cpp:22
void show(CVideo &video)
Shows the error in a dialog.
int total_height() const
Real height of the map, including borders.
Definition: map.hpp:102
std::string filename_
Definition: action_wml.cpp:546
config::child_itors replace_array(std::vector< config > children) const
#define WRN_NG
Definition: action_wml.cpp:71
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:94
void merge_array(std::vector< config > children) const
#define ERR_WML
Definition: action_wml.cpp:81
std::vector< map_location > steps
Definition: pathfind.hpp:134
#define DBG_NG
Definition: action_wml.cpp:69
void write(config &cfg) const
Serializes the current unit metadata values.
Definition: unit.cpp:1380
void set_number_of_turns_by_wml(int num)
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:141
filter_context * filter_con
Definition: resources.cpp:23
variable_access_create get_variable_access_write(const std::string &varname)
returns a variable_access that can be used to change the game variables
Definition: game_data.hpp:53
int w() const
Effective map width.
Definition: map.hpp:90
game_board * gameboard
Definition: resources.cpp:20
Interface for querying local choices.
iterator begin()
begin iterator
Encapsulates the map of the game.
Definition: map.hpp:34
unit_race::GENDER string_gender(const std::string &str, unit_race::GENDER def)
Definition: race.cpp:140
config::child_itors append_array(std::vector< config > children) const
fake_unit_manager * fake_units
Definition: resources.cpp:31
std::string path
Definition: game_config.cpp:56
Managing the AIs lifecycle - headers.
bool explicit_index() const NOEXCEPT
Function which only uses terrain, ignoring shroud, enemies, etc.
Definition: pathfind.hpp:240
void read(const std::string &data, const bool allow_invalid=true)
Definition: map.cpp:122
iterator end()
end iterator
static const map_location & null_location()
Definition: location.hpp:220
void needs_rebuild(bool b)
Sets whether the screen (map visuals) needs to be rebuilt. This is typically after the map has been c...
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
void set_turn_by_wml(const int num, game_data *vars=nullptr, const bool increase_limit_if_needed=true)
Dynamically change the current turn number.
bool blank() const
Tests for an attribute that was never set.
Encapsulates the map of the game.
Definition: location.hpp:40
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const size_t width, const size_t height, const teleport_map *teleports, bool border)
#define ERR_CF
Definition: action_wml.cpp:84
Various functions related to moving units.
Domain specific events.
Definition: action_wml.cpp:88
Helper class, don't construct this directly.
static map registry_
Tracks the known action handlers.
Definition: action_wml.hpp:64
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:35
Various functions related to the creation of units (recruits, recalls, and placed units)...
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
Define conditionals for the game's events mechanism, a.k.a.
unit * get()
Get a raw pointer to the underlying unit.
int to_int(int def=0) const
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:269
int h() const
Effective map height.
Definition: map.hpp:93
std::string get_wml_location(const std::string &filename, const std::string &current_dir=std::string())
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
unit_creator & allow_get_village(bool b)
mock_party p
bool empty() const
Definition: variable.hpp:100
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
int turn() const
Define the game's event mechanism.
maybe_const_t< config::child_itors, V > as_array() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
#define i
variable_access_const get_variable_access_read(const std::string &varname) const
returns a variable_access that cannot be used to change the game variables
Definition: game_data.hpp:47
static lg::log_domain log_display("display")
Declarations for File-IO.
config & add_child(config_key_type key)
Definition: config.cpp:408
config::child_itors insert_array(std::vector< config > children) const
virtual const gamemap & map() const
Definition: game_board.hpp:96
iterator erase(iterator it)
Erase an iterator to this object.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:369
void add_unit(const config &cfg, const vconfig *vcfg=nullptr)
adds a unit on map without firing any events (so, usable during team construction in gamestatus) ...
void add(const teleport_group &group)
Definition: teleport.cpp:294
boost::optional< std::string > replace_map(const gamemap &r)
Definition: game_board.cpp:253
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
Information on a WML variable.
static bool run_in_synced_context_if_not_already(const std::string &commandname, const config &data, bool use_undo=true, bool show=true, synced_command::error_handler_function error_handler=default_error_function)
checks whether we are currently running in a synced context, and if not we enters it...
void reload_map()
Updates internals that cache map size.
Definition: display.cpp:495
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:624
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
Standard logging facilities (interface).
static lg::log_domain log_engine("engine")
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1273
#define e
static void clear_global_variable(persist_context &ctx, const vconfig &pcfg)
Definition: persist_var.cpp:88
place_recruit_result place_recruit(unit_ptr u, const map_location &recruit_location, const map_location &recruited_from, int cost, bool is_recall, map_location::DIRECTION facing, bool show, bool fire_event, bool full_movement, bool wml_triggered)
Place a unit into the game.
Definition: create.cpp:610
std::vector< vconfig > child_list
Definition: variable.hpp:78
static lg::log_domain log_wml("wml")
void write(config &cfg) const
Definition: time_of_day.cpp:54
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mock_char c
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
This module contains various pathfinding functions and utilities.
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:139
pathfind::manager * tunnels
Definition: resources.cpp:32
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
unit_map * units
Definition: resources.cpp:34
int side() const
The side this unit belongs to.
Definition: unit.hpp:245
bool empty() const
Definition: tstring.hpp:182
Display units performing various actions: moving, attacking, and dying.
void move_unit(const std::vector< map_location > &path, unit_ptr u, bool animate, map_location::DIRECTION dir, bool force_scroll)
Display a unit moving along a given path.
Definition: udisplay.cpp:495
void verify_and_clear_global_variable(const vconfig &pcfg)
int number_of_turns() const