The Battle for Wesnoth  1.15.12+dev
pathfind.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <dave@whitevine.net>
3  Copyright (C) 2005 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Various pathfinding functions and utilities.
19  */
20 
21 #include "pathfind/pathfind.hpp"
22 #include "pathfind/teleport.hpp"
23 
24 #include "display.hpp"
25 #include "game_board.hpp"
26 #include "gettext.hpp"
27 #include "log.hpp"
28 #include "map/map.hpp"
29 #include "resources.hpp"
30 #include "team.hpp"
31 #include "units/unit.hpp"
32 #include "units/map.hpp"
33 #include "wml_exception.hpp"
34 
35 #include <iostream>
36 #include <vector>
37 #include <algorithm>
38 
39 static lg::log_domain log_engine("engine");
40 #define ERR_PF LOG_STREAM(err, log_engine)
41 
42 namespace pathfind {
43 
44 
45 /**
46  * Function that will find a location on the board that is as near
47  * to @a loc as possible, but which is unoccupied by any units.
48  * If no valid location can be found, it will return a null location.
49  * If @a pass_check is provided, the found location must have a terrain
50  * that this unit can enter.
51  * If @a shroud_check is provided, only locations not covered by this
52  * team's shroud will be considered.
53  */
55  const unit* pass_check, const team* shroud_check, const game_board* board)
56 {
57  if (!board) {
58  board = resources::gameboard;
59  assert(board);
60  }
61  const gamemap & map = board->map();
62  const unit_map & units = board->units();
63 
64  if (!map.on_board(loc)) return map_location();
65 
66  const bool do_shroud = shroud_check && shroud_check->uses_shroud();
67  std::set<map_location> pending_tiles_to_check, tiles_checked;
68  pending_tiles_to_check.insert(loc);
69  // Iterate out 50 hexes from loc
70  for (int distance = 0; distance < 50; ++distance) {
71  if (pending_tiles_to_check.empty())
72  return map_location();
73  //Copy over the hexes to check and clear the old set
74  std::set<map_location> tiles_checking;
75  tiles_checking.swap(pending_tiles_to_check);
76  //Iterate over all the hexes we need to check
77  for (const map_location &l : tiles_checking)
78  {
79  // Skip shrouded locations.
80  if ( do_shroud && shroud_check->shrouded(l) )
81  continue;
82  //If this area is not a castle but should, skip it.
83  if ( vacancy == VACANT_CASTLE && !map.is_castle(l) ) continue;
84  const bool pass_check_and_unreachable = pass_check
85  && pass_check->movement_cost(map[l]) == movetype::UNREACHABLE;
86  //If the unit can't reach the tile and we have searched
87  //an area of at least radius 10 (arbitrary), skip the tile.
88  //Necessary for cases such as an unreachable
89  //starting hex surrounded by 6 other unreachable hexes, in which case
90  //the algorithm would not even search distance==1
91  //even if there's a reachable hex for distance==2.
92  if (pass_check_and_unreachable && distance > 10) continue;
93  //If the hex is empty and we do either no pass check or the hex is reachable, return it.
94  if (units.find(l) == units.end() && !pass_check_and_unreachable) return l;
95 
96  for(const map_location& l2 : get_adjacent_tiles(l)) {
97  if (!map.on_board(l2)) continue;
98  // Add the tile to be checked if it hasn't already been and
99  // isn't being checked.
100  if (tiles_checked.find(l2) == tiles_checked.end() &&
101  tiles_checking.find(l2) == tiles_checking.end())
102  {
103  pending_tiles_to_check.insert(l2);
104  }
105  }
106  }
107  tiles_checked.swap(tiles_checking);
108  }
109  return map_location();
110 }
111 
112 /**
113  * Wrapper for find_vacant_tile() when looking for a vacant castle tile
114  * near a leader.
115  * If no valid location can be found, it will return a null location.
116  */
118 {
120  nullptr, &resources::gameboard->get_team(leader.side()));
121 }
122 
123 
124 /**
125  * Determines if a given location is in an enemy zone of control.
126  *
127  * @param current_team The moving team (only ZoC of enemies of this team are considered).
128  * @param loc The location to check.
129  * @param viewing_team Only units visible to this team are considered.
130  * @param see_all If true, all units are considered (and viewing_team is ignored).
131  *
132  * @return true iff a visible enemy exerts zone of control over loc.
133  */
134 bool enemy_zoc(const team& current_team, const map_location& loc,
135  const team& viewing_team, bool see_all)
136 {
137  // Check the adjacent tiles.
138  for(const map_location& adj : get_adjacent_tiles(loc)) {
139  const unit *u = resources::gameboard->get_visible_unit(adj, viewing_team, see_all);
140  if ( u && current_team.is_enemy(u->side()) && u->emits_zoc() )
141  return true;
142  }
143 
144  // No adjacent tiles had an enemy exerting ZoC over loc.
145  return false;
146 }
147 
148 
149 namespace {
150  /**
151  * Nodes used by find_routes().
152  * These store the information necessary for extending the path
153  * and for tracing the route back to the source.
154  */
155  struct findroute_node {
158  // search_num is used to detect which nodes have been collected
159  // in the current search. (More than just a boolean value so
160  // that nodes can be stored between searches.)
161  unsigned search_num;
162 
163  // Constructors.
164  findroute_node(int moves, int turns, const map_location &prev_loc, unsigned search_count)
165  : moves_left(moves)
166  , turns_left(turns)
167  , prev(prev_loc)
168  , search_num(search_count)
169  { }
170  findroute_node()
171  : moves_left(0)
172  , turns_left(0)
173  , prev()
174  , search_num(0)
175  { }
176 
177  // Compare these nodes based on movement consumed.
178  bool operator<(const findroute_node& o) const
179  {
180  return std::tie(turns_left, moves_left) > std::tie(o.turns_left, o.moves_left);
181  }
182  };
183 
184  /**
185  * Converts map locations to and from integer indices.
186  */
187  struct findroute_indexer {
188  int w, h; // Width and height of the map.
189 
190  // Constructor:
191  findroute_indexer(int a, int b) : w(a), h(b) { }
192  // Convert to an index: (throws on out of bounds)
193  unsigned operator()(int x, int y) const {
194  VALIDATE(this->on_board(x,y),
195  "Pathfind: Location not on board");
196  return x + static_cast<unsigned>(y)*w;
197  }
198  unsigned operator()(const map_location& loc) const {
199  return (*this)(loc.x, loc.y);
200  }
201  // Convert from an index:
202  map_location operator()(unsigned index) const {
203  return map_location(
204  static_cast<int>(index%w),
205  static_cast<int>(index/w));
206  }
207  // Check if location is on board
208  inline bool on_board(const map_location& loc) const {
209  return this->on_board(loc.x, loc.y);
210  }
211  inline bool on_board(int x, int y) const {
212  return (x >= 0) && (x < w) && (y >= 0) && (y < h);
213  }
214  };
215 
216  /**
217  * A function object for comparing indices.
218  */
219  struct findroute_comp {
220  const std::vector<findroute_node>& nodes;
221 
222  // Constructor:
223  findroute_comp(const std::vector<findroute_node>& n) : nodes(n) { }
224  // Binary predicate evaluating the order of its arguments:
225  bool operator()(int l, int r) const {
226  return nodes[r] < nodes[l];
227  }
228  };
229 }
230 
231 /**
232  * Creates a list of routes that a unit can traverse from the provided location.
233  * (This is called when creating pathfind::paths and descendant classes.)
234  *
235  * @param[in] origin The location at which to begin the routes.
236  * @param[in] costs The costs to use for route finding.
237  * @param[in] slowed Whether or not to use the slowed costs.
238  * @param[in] moves_left The number of movement points left for the current turn.
239  * @param[in] max_moves The number of movement points in each future turn.
240  * @param[in] turns_left The number of future turns of movement to calculate.
241  * @param[out] destinations The traversable routes.
242  * @param[out] edges The hexes (possibly off-map) adjacent to those in
243  * destinations. (It is permissible for this to contain
244  * some hexes that are also in destinations.)
245  *
246  * @param[in] teleporter If not nullptr, teleportation will be considered, using
247  * this unit's abilities.
248  * @param[in] current_team If not nullptr, enemies of this team can obstruct routes
249  * both by occupying hexes and by exerting zones of control.
250  * In addition, the presence of units can affect
251  * teleportation options.
252  * @param[in] skirmisher If not nullptr, use this to determine where ZoC can and
253  * cannot be ignored (due to this unit having or not
254  * having the skirmisher ability).
255  * If nullptr, then ignore all zones of control.
256  * (No effect if current_team is nullptr).
257  * @param[in] viewing_team If not nullptr, use this team's vision when detecting
258  * enemy units and teleport destinations.
259  * If nullptr, then "see all".
260  * (No effect if teleporter and current_team are both nullptr.)
261  * @param[in] jamming_map The relevant "jamming" of the costs being used
262  * (currently only used with vision costs).
263  * @param[out] full_cost_map If not nullptr, build a cost_map instead of destinations.
264  * Destinations is ignored.
265  * full_cost_map is a vector of pairs. The first entry is the
266  * cost itself, the second how many units already visited this hex
267  * @param[in] check_vision If true, use vision check for teleports, that is, ignore
268  * units potentially blocking the teleport exit
269  */
270 static void find_routes(
271  const map_location & origin, const movetype::terrain_costs & costs,
272  bool slowed, int moves_left, int max_moves, int turns_left,
273  paths::dest_vect & destinations, std::set<map_location> * edges,
274  const unit * teleporter, const team * current_team,
275  const unit * skirmisher, const team * viewing_team,
276  const std::map<map_location, int> * jamming_map=nullptr,
277  std::vector<std::pair<int, int>> * full_cost_map=nullptr, bool check_vision=false)
278 {
279  const gamemap& map = resources::gameboard->map();
280 
281  const bool see_all = viewing_team == nullptr;
282  // When see_all is true, the viewing team never matters, but we still
283  // need to supply one to some functions.
284  if ( viewing_team == nullptr )
285  viewing_team = &resources::gameboard->teams().front();
286 
287  // Build a teleport map, if needed.
288  const teleport_map teleports = teleporter ?
289  get_teleport_locations(*teleporter, *viewing_team, see_all, current_team == nullptr, check_vision) :
290  teleport_map();
291 
292  // Since this is called so often, keep memory reserved for the node list.
293  static std::vector<findroute_node> nodes;
294  static unsigned search_counter = 0;
295  // Incrementing search_counter means we ignore results from earlier searches.
296  ++search_counter;
297  // Whenever the counter cycles, trash the contents of nodes and restart at 1.
298  if ( search_counter == 0 ) {
299  nodes.resize(0);
300  search_counter = 1;
301  }
302  // Initialize the nodes for this search.
303  nodes.resize(map.w() * map.h());
304  findroute_comp node_comp(nodes);
305  findroute_indexer index(map.w(), map.h());
306 
307  assert(index.on_board(origin));
308 
309  // Check if full_cost_map has the correct size.
310  // If not, ignore it. If yes, initialize the start position.
311  if ( full_cost_map ) {
312  if ( full_cost_map->size() != static_cast<unsigned>(map.w() * map.h()) )
313  full_cost_map = nullptr;
314  else {
315  if ( (*full_cost_map)[index(origin)].second == 0 )
316  (*full_cost_map)[index(origin)].first = 0;
317  (*full_cost_map)[index(origin)].second += 1;
318  }
319  }
320 
321  // Used to optimize the final collection of routes.
322  int xmin = origin.x, xmax = origin.x, ymin = origin.y, ymax = origin.y;
323  int nb_dest = 1;
324 
325  // Record the starting location.
326  nodes[index(origin)] = findroute_node(moves_left, turns_left,
328  search_counter);
329  // Begin the search at the starting location.
330  std::vector<unsigned> hexes_to_process(1, index(origin)); // Will be maintained as a heap.
331 
332  while ( !hexes_to_process.empty() ) {
333  // Process the hex closest to the origin.
334  const unsigned cur_index = hexes_to_process.front();
335  const map_location cur_hex = index(cur_index);
336  const findroute_node& current = nodes[cur_index];
337  // Remove from the heap.
338  std::pop_heap(hexes_to_process.begin(), hexes_to_process.end(), node_comp);
339  hexes_to_process.pop_back();
340 
341  // Get the locations adjacent to current.
342  std::vector<map_location> adj_locs(6);
343  get_adjacent_tiles(cur_hex, adj_locs.data());
344 
345  // Sort adjacents by on-boardness
346  auto off_board_it = std::partition(adj_locs.begin(), adj_locs.end(), [&index](map_location loc){
347  return index.on_board(loc);
348  });
349  // Store off-board edges if needed
350  if(edges != nullptr){
351  edges->insert(off_board_it, adj_locs.end());
352  }
353  // Remove off-board map locations
354  adj_locs.erase(off_board_it, adj_locs.end());
355 
356  if ( teleporter ) {
357  auto allowed_teleports = teleports.get_adjacents(cur_hex);
358  adj_locs.insert(adj_locs.end(), allowed_teleports.begin(), allowed_teleports.end());
359  }
360  for ( int i = adj_locs.size()-1; i >= 0; --i ) {
361  // Get the node associated with this location.
362  const map_location & next_hex = adj_locs[i];
363  const unsigned next_index = index(next_hex);
364  findroute_node & next = nodes[next_index];
365 
366  // Skip nodes we have already collected.
367  // (Since no previously checked routes were longer than
368  // the current one, the current route cannot be shorter.)
369  // (Significant difference from classic Dijkstra: we have
370  // vertex weights, not edge weights.)
371  if ( next.search_num == search_counter )
372  continue;
373 
374  // If we go to next, it will be from current.
375  next.prev = cur_hex;
376 
377  // Calculate the cost of entering next_hex.
378  int cost = costs.cost(map[next_hex], slowed);
379  if ( jamming_map ) {
380  const std::map<map_location, int>::const_iterator jam_it =
381  jamming_map->find(next_hex);
382  if ( jam_it != jamming_map->end() )
383  cost += jam_it->second;
384  }
385 
386  // Calculate movement remaining after entering next_hex.
387  next.moves_left = current.moves_left - cost;
388  next.turns_left = current.turns_left;
389  if ( next.moves_left < 0 ) {
390  // Have to delay until the next turn.
391  next.turns_left--;
392  next.moves_left = max_moves - cost;
393  }
394  if ( next.moves_left < 0 || next.turns_left < 0 ) {
395  // Either can never enter this hex or out of turns.
396  if ( edges != nullptr )
397  edges->insert(next_hex);
398  continue;
399  }
400 
401  if ( current_team ) {
402  // Account for enemy units.
403  const unit *v = resources::gameboard->get_visible_unit(next_hex, *viewing_team, see_all);
404  if ( v && current_team->is_enemy(v->side()) ) {
405  // Cannot enter enemy hexes.
406  if ( edges != nullptr )
407  edges->insert(next_hex);
408  continue;
409  }
410 
411  if ( skirmisher && next.moves_left > 0 &&
412  enemy_zoc(*current_team, next_hex, *viewing_team, see_all) &&
413  !skirmisher->get_ability_bool("skirmisher", next_hex) ) {
414  next.moves_left = 0;
415  }
416  }
417 
418  if ( viewing_team && current_team && viewing_team != current_team && viewing_team->shrouded(next_hex) ) {
419  // bug #2199: in "Show Enemy Moves", don't pathfind enemy units through the player's shroud
420  continue;
421  }
422 
423  // Update full_cost_map
424  if ( full_cost_map ) {
425  if ( (*full_cost_map)[next_index].second == 0 )
426  (*full_cost_map)[next_index].first = 0;
427  int summed_cost = (turns_left - next.turns_left + 1) * max_moves - next.moves_left;
428  (*full_cost_map)[next_index].first += summed_cost;
429  (*full_cost_map)[next_index].second += 1;
430  }
431 
432  // Mark next as being collected.
433  next.search_num = search_counter;
434 
435  // Add this node to the heap.
436  hexes_to_process.push_back(next_index);
437  std::push_heap(hexes_to_process.begin(), hexes_to_process.end(), node_comp);
438 
439  // Bookkeeping (for later).
440  ++nb_dest;
441  if ( next_hex.x < xmin )
442  xmin = next_hex.x;
443  else if ( xmax < next_hex.x )
444  xmax = next_hex.x;
445  if ( next_hex.y < ymin )
446  ymin = next_hex.y;
447  else if ( ymax < next_hex.y )
448  ymax = next_hex.y;
449  }//for (i)
450  }//while (hexes_to_process)
451 
452  // Currently the only caller who uses full_cost_map doesn't need the
453  // destinations. We can skip this part.
454  if ( full_cost_map ) {
455  return;
456  }
457 
458  // Build the routes for every map_location that we reached.
459  // The ordering must be compatible with map_location::operator<.
460  destinations.reserve(nb_dest);
461  for (int x = xmin; x <= xmax; ++x) {
462  for (int y = ymin; y <= ymax; ++y)
463  {
464  const findroute_node &n = nodes[index(x,y)];
465  if ( n.search_num == search_counter ) {
466  paths::step s =
467  { map_location(x,y), n.prev, n.moves_left + n.turns_left*max_moves };
468  destinations.push_back(s);
469  }
470  }
471  }
472 }
473 
474 static bool step_compare(const paths::step& a, const map_location& b) {
475  return a.curr < b;
476 }
477 
478 paths::dest_vect::const_iterator paths::dest_vect::find(const map_location &loc) const
479 {
480  const_iterator i = std::lower_bound(begin(), end(), loc, step_compare);
481  if (i != end() && i->curr != loc) return end();
482  return i;
483 }
484 
486 {
487  iterator i = std::lower_bound(begin(), end(), loc, step_compare);
488  if (i != end() && i->curr == loc) return;
489  paths::step s { loc, map_location(), 0 };
491 }
492 
493 /**
494  * Returns the path going from the source point (included) to the
495  * destination point @a j (excluded).
496  */
497 std::vector<map_location> paths::dest_vect::get_path(const const_iterator &j) const
498 {
499  std::vector<map_location> path;
500  if (!j->prev.valid()) {
501  path.push_back(j->curr);
502  } else {
503  const_iterator i = j;
504  do {
505  i = find(i->prev);
506  assert(i != end());
507  path.push_back(i->curr);
508  } while (i->prev.valid());
509  }
510  std::reverse(path.begin(), path.end());
511  return path;
512 }
513 
515 {
516  return find(loc) != end();
517 }
518 
519 /**
520  * Construct a list of paths for the specified unit.
521  *
522  * This function is used for several purposes, including showing a unit's
523  * potential moves and generating currently possible paths.
524  * @param u The unit whose moves and movement type will be used.
525  * @param force_ignore_zoc Set to true to completely ignore zones of control.
526  * @param allow_teleport Set to true to consider teleportation abilities.
527  * @param viewing_team Usually the current team, except for "show enemy moves", etc.
528  * @param additional_turns The number of turns to account for, in addition to the current.
529  * @param see_all Set to true to remove unit visibility from consideration.
530  * @param ignore_units Set to true if units should never obstruct paths (implies ignoring ZoC as well).
531  */
532 paths::paths(const unit& u, bool force_ignore_zoc,
533  bool allow_teleport, const team &viewing_team,
534  int additional_turns, bool see_all, bool ignore_units)
535  : destinations()
536 {
537  try {
538  find_routes(
539  u.get_location(),
542  u.movement_left(),
543  u.total_movement(),
544  additional_turns,
545  destinations,
546  nullptr,
547  allow_teleport ? &u : nullptr,
548  ignore_units ? nullptr : &resources::gameboard->get_team(u.side()),
549  force_ignore_zoc ? nullptr : &u,
550  see_all ? nullptr : &viewing_team
551  );
552  } catch(const std::out_of_range&) {
553  // Invalid unit side.
554  }
555 }
556 
557 /**
558  * Virtual destructor to support child classes.
559  */
561 {
562 }
563 
564 /**
565  * Constructs a list of vision paths for a unit.
566  *
567  * This is used to construct a list of hexes that the indicated unit can see.
568  * It differs from pathfinding in that it will only ever go out one turn,
569  * and that it will also collect a set of border hexes (the "one hex beyond"
570  * movement to which vision extends).
571  * @param viewer The unit doing the viewing.
572  * @param loc The location from which the viewing occurs
573  * (does not have to be the unit's location).
574  * @param jamming_map The relevant "jamming" of the costs being used.
575  */
576 vision_path::vision_path(const unit& viewer, const map_location& loc,
577  const std::map<map_location, int>& jamming_map)
578  : paths(), edges()
579 {
580  const int sight_range = viewer.vision();
581 
582  // The three nullptr parameters indicate (in order):
583  // ignore units, ignore ZoC (no effect), and don't build a cost_map.
584  const team& viewing_team = resources::gameboard->teams()[display::get_singleton()->viewing_team()];
585  find_routes(loc, viewer.movement_type().get_vision(),
586  viewer.get_state(unit::STATE_SLOWED), sight_range, sight_range,
587  0, destinations, &edges, &viewer, nullptr, nullptr, &viewing_team, &jamming_map, nullptr, true);
588 }
589 
590 /**
591  * Constructs a list of vision paths for a unit.
592  *
593  * This constructor is provided so that only the relevant portion of a unit's
594  * data is required to construct the vision paths.
595  * @param view_costs The vision costs of the unit doing the viewing.
596  * @param slowed Whether or not the unit is slowed.
597  * @param sight_range The vision() of the unit.
598  * @param loc The location from which the viewing occurs
599  * (does not have to be the unit's location).
600  * @param jamming_map The relevant "jamming" of the costs being used.
601  */
603  int sight_range, const map_location & loc,
604  const std::map<map_location, int>& jamming_map)
605  : paths(), edges()
606 {
607  // The three nullptr parameters indicate (in order):
608  // ignore units, ignore ZoC (no effect), and don't build a cost_map.
609  const team& viewing_team = resources::gameboard->teams()[display::get_singleton()->viewing_team()];
611  find_routes(loc, view_costs, slowed, sight_range, sight_range, 0,
612  destinations, &edges, u.valid() ? &*u : nullptr, nullptr, nullptr, &viewing_team, &jamming_map, nullptr, true);
613 }
614 
615 /** Default destructor */
617 {
618 }
619 
620 
621 /**
622  * Constructs a list of jamming paths for a unit.
623  *
624  * This is used to construct a list of hexes that the indicated unit can jam.
625  * It differs from pathfinding in that it will only ever go out one turn.
626  * @param jammer The unit doing the jamming.
627  * @param loc The location from which the jamming occurs
628  * (does not have to be the unit's location).
629  */
630 jamming_path::jamming_path(const unit& jammer, const map_location& loc)
631  : paths()
632 {
633  const int jamming_range = jammer.jamming();
634 
635  // The five nullptr parameters indicate (in order): no edges, no teleports,
636  // ignore units, ignore ZoC (no effect), and see all (no effect).
637  find_routes(loc, jammer.movement_type().get_jamming(),
638  jammer.get_state(unit::STATE_SLOWED), jamming_range, jamming_range,
639  0, destinations, nullptr, nullptr, nullptr, nullptr, nullptr);
640 }
641 
642 /** Default destructor */
644 {
645 }
646 
647 marked_route mark_route(const plain_route &rt, bool update_move_cost)
648 {
649  marked_route res;
650 
651  if (rt.steps.empty()) return marked_route();
652  res.route = rt;
653 
655  if (it == resources::gameboard->units().end()) return marked_route();
656  const unit& u = *it;
657 
658  int turns = 0;
659  int total_costs = 0;
660  int movement = u.movement_left();
661  const team& unit_team = resources::gameboard->get_team(u.side());
662  bool zoc = false;
663 
664  std::vector<map_location>::const_iterator i = rt.steps.begin();
665 
666  for (; i !=rt.steps.end(); ++i) {
667  bool last_step = (i+1 == rt.steps.end());
668 
669  // move_cost of the next step is irrelevant for the last step
670  assert(last_step || resources::gameboard->map().on_board(*(i+1)));
671  const int move_cost = last_step ? 0 : u.movement_cost(static_cast<const game_board*>(resources::gameboard)->map()[*(i+1)]);
672 
673  const team& viewing_team = resources::gameboard->teams()[display::get_singleton()->viewing_team()];
674 
675  if (last_step || zoc || move_cost > movement) {
676  // check if we stop an a village and so maybe capture it
677  // if it's an enemy unit and a fogged village, we assume a capture
678  // (if he already owns it, we can't know that)
679  // if it's not an enemy, we can always know if he owns the village
680  bool capture = resources::gameboard->map().is_village(*i) && ( !unit_team.owns_village(*i)
681  || (viewing_team.is_enemy(u.side()) && viewing_team.fogged(*i)) );
682 
683  ++turns;
684 
685  bool invisible = u.invisible(*i, false);
686 
687  res.marks[*i] = marked_route::mark(turns, zoc, capture, invisible);
688 
689  if(last_step) {
690  if(capture) {
691  total_costs += movement;
692  }
693  break; // finished and we used dummy move_cost
694  }
695 
696  total_costs += movement;
697  movement = u.total_movement();
698  if(move_cost > movement) {
699  return res; //we can't reach destination
700  }
701  }
702 
703  zoc = enemy_zoc(unit_team, *(i + 1), viewing_team)
704  && !u.get_ability_bool("skirmisher", *(i+1));
705 
706  if (zoc) {
707  total_costs += movement;
708  movement = 0;
709  } else {
710  movement -= move_cost;
711  total_costs += move_cost;
712  }
713  }
714 
715  if(update_move_cost) {
716  res.move_cost = total_costs;
717  }
718  return res;
719 }
720 
722  const std::vector<team>& teams, const gamemap& map,
723  bool ignore_unit, bool ignore_defense, bool see_all)
724  : unit_(u), viewing_team_(t), teams_(teams), map_(map),
725  movement_left_(unit_.movement_left()),
726  total_movement_(unit_.total_movement()),
727  ignore_unit_(ignore_unit), ignore_defense_(ignore_defense),
728  see_all_(see_all)
729 {}
730 
731 double shortest_path_calculator::cost(const map_location& loc, const double so_far) const
732 {
733  assert(map_.on_board(loc));
734 
735  // loc is shrouded, consider it impassable
736  // NOTE: This is why AI must avoid to use shroud
737  if (!see_all_ && viewing_team_.shrouded(loc))
738  return getNoPathValue();
739 
740  const t_translation::terrain_code terrain = map_[loc];
741  const int terrain_cost = unit_.movement_cost(terrain);
742  // Pathfinding heuristic: the cost must be at least 1
743  VALIDATE(terrain_cost >= 1, _("Terrain with a movement cost less than 1 encountered."));
744 
745  // Compute how many movement points are left in the game turn
746  // needed to reach the previous hex.
747  // total_movement_ is not zero, thanks to the pathfinding heuristic
748  int remaining_movement = movement_left_ - static_cast<int>(so_far);
749  if (remaining_movement < 0) {
750  remaining_movement = total_movement_ - (-remaining_movement) % total_movement_;
751  }
752 
753  if (terrain_cost >= movetype::UNREACHABLE || (total_movement_ < terrain_cost && remaining_movement < terrain_cost)) {
754  return getNoPathValue();
755  }
756 
757  int other_unit_subcost = 0;
758  if (!ignore_unit_) {
759  const unit *other_unit =
761 
762  // We can't traverse visible enemy and we also prefer empty hexes
763  // (less blocking in multi-turn moves and better when exploring fog,
764  // because we can't stop on a friend)
765 
766  if (other_unit)
767  {
768  if (teams_[unit_.side() - 1].is_enemy(other_unit->side()))
769  return getNoPathValue();
770  else
771  // This value will be used with the defense_subcost (see below)
772  // The 1 here means: consider occupied hex as a -1% defense
773  // (less important than 10% defense because friends may move)
774  other_unit_subcost = 1;
775  }
776  }
777 
778  // this will sum all different costs of this move
779  int move_cost = 0;
780 
781  // Suppose that we have only 2 remaining MP and want to move onto a hex
782  // costing 3 MP. We don't have enough MP now, so we must end our turn here,
783  // thus spend our remaining MP by waiting (next turn, with full MP, we will
784  // be able to move on that hex)
785  if (remaining_movement < terrain_cost) {
786  move_cost += remaining_movement;
787  remaining_movement = total_movement_; // we consider having full MP now
788  }
789 
790  // check ZoC
791  if (!ignore_unit_ && remaining_movement != terrain_cost
793  && !unit_.get_ability_bool("skirmisher", loc)) {
794  // entering ZoC cost all remaining MP
795  move_cost += remaining_movement;
796  } else {
797  // empty hex, pay only the terrain cost
798  move_cost += terrain_cost;
799  }
800 
801  // We will add a tiny cost based on terrain defense, so the pathfinding
802  // will prefer good terrains between 2 with the same MP cost
803  // Keep in mind that defense_modifier is inverted (= 100 - defense%)
804  const int defense_subcost = ignore_defense_ ? 0 : unit_.defense_modifier(terrain);
805 
806  // We divide subcosts by 100 * 100, because defense is 100-based and
807  // we don't want any impact on move cost for less then 100-steps path
808  // (even ~200 since mean defense is around ~50%)
809  return move_cost + (defense_subcost + other_unit_subcost) / 10000.0;
810 }
811 
812 move_type_path_calculator::move_type_path_calculator(const movetype& mt, int movement_left, int total_movement, const team& t, const gamemap& map)
813  : movement_type_(mt), movement_left_(movement_left),
814  total_movement_(total_movement), viewing_team_(t), map_(map)
815 {}
816 
817 // This is an simplified version of shortest_path_calculator (see above for explanation)
818 double move_type_path_calculator::cost(const map_location& loc, const double so_far) const
819 {
820  assert(map_.on_board(loc));
821  if (viewing_team_.shrouded(loc))
822  return getNoPathValue();
823 
824  const t_translation::terrain_code terrain = map_[loc];
825  const int terrain_cost = movement_type_.movement_cost(terrain);
826 
827  if (total_movement_ < terrain_cost)
828  return getNoPathValue();
829 
830  int remaining_movement = movement_left_ - static_cast<int>(so_far);
831  if (remaining_movement < 0)
832  remaining_movement = total_movement_ - (-remaining_movement) % total_movement_;
833 
834  int move_cost = 0;
835 
836  if (remaining_movement < terrain_cost) {
837  move_cost += remaining_movement;
838  }
839 
840  move_cost += terrain_cost;
841 
842  return move_cost;
843 }
844 
845 
847  : unit_(u), map_(map)
848 {}
849 
850 double emergency_path_calculator::cost(const map_location& loc, const double) const
851 {
852  assert(map_.on_board(loc));
853 
854  return unit_.movement_cost(map_[loc]);
855 }
856 
858 {}
859 
860 double dummy_path_calculator::cost(const map_location&, const double) const
861 {
862  return 1.0;
863 }
864 
865 /**
866  * Constructs a cost-map. For a unit each hex is mapped to the cost the
867  * unit will need to reach this hex. Considers movement-loss caused by
868  * turn changes.
869  * Can also used with multiple units to accumulate their costs efficiently.
870  * Will also count how many units could reach a hex for easy normalization.
871  * @param u the unit
872  * @param force_ignore_zoc Set to true to completely ignore zones of control.
873  * @param allow_teleport Set to true to consider teleportation abilities.
874  * @param viewing_team Usually the current team, except for "show enemy moves", etc.
875  * @param see_all Set to true to remove unit visibility from consideration.
876  * @param ignore_units Set to true if units should never obstruct paths (implies ignoring ZoC as well).
877  */
878 full_cost_map::full_cost_map(const unit& u, bool force_ignore_zoc,
879  bool allow_teleport, const team &viewing_team,
880  bool see_all, bool ignore_units)
881  :force_ignore_zoc_(force_ignore_zoc), allow_teleport_(allow_teleport),
882  viewing_team_(viewing_team), see_all_(see_all), ignore_units_(ignore_units)
883 {
884  const gamemap& map = resources::gameboard->map();
885  cost_map = std::vector<std::pair<int, int>>(map.w() * map.h(), std::pair(-1, 0));
886  add_unit(u);
887 }
888 
889 /**
890  * Same as other constructor but without unit. Use this when working
891  * with add_unit().
892  */
893 full_cost_map::full_cost_map(bool force_ignore_zoc,
894  bool allow_teleport, const team &viewing_team,
895  bool see_all, bool ignore_units)
896  :force_ignore_zoc_(force_ignore_zoc), allow_teleport_(allow_teleport),
897  viewing_team_(viewing_team), see_all_(see_all), ignore_units_(ignore_units)
898 {
899  const gamemap& map = resources::gameboard->map();
900  cost_map = std::vector<std::pair<int, int>>(map.w() * map.h(), std::pair(-1, 0));
901 }
902 
903 /**
904  * Adds a units cost map to cost_map (increments the elements in cost_map)
905  * @param u a real existing unit on the map
906  * @param use_max_moves whether to use the unit's max movement or the unit's remaining movement
907  */
908 void full_cost_map::add_unit(const unit& u, bool use_max_moves)
909 {
910  try {
911  // We don't need the destinations, but find_routes() wants to have this parameter
913 
914  find_routes(
915  u.get_location(),
918  (use_max_moves) ? u.total_movement() : u.movement_left(),
919  u.total_movement(),
920  99,
921  dummy,
922  nullptr,
923  allow_teleport_ ? &u : nullptr,
924  ignore_units_ ? nullptr : &resources::gameboard->get_team(u.side()),
925  force_ignore_zoc_ ? nullptr : &u,
926  see_all_ ? nullptr : &viewing_team_,
927  nullptr,
928  &cost_map
929  );
930  } catch(const std::out_of_range&) {
931  // Invalid unit side.
932  }
933 }
934 
935 /**
936  * Adds a units cost map to cost_map (increments the elements in cost_map)
937  * This function can be used to generate a cost_map with a non existing unit.
938  * @param origin the location on the map from where the calculations shall start
939  * @param ut the unit type we are interested in
940  * @param side the side of the unit. Important for zocs.
941  */
942 void full_cost_map::add_unit(const map_location& origin, const unit_type* const ut, int side)
943 {
944  if (!ut) {
945  return;
946  }
947  unit_ptr u = unit::create(*ut, side, false);
948  u->set_location(origin);
949  add_unit(*u);
950 }
951 
952 /**
953  * Accessor for the cost/reach-amount pairs.
954  * Read comment in pathfind.hpp to cost_map.
955  *
956  * @return the entry of the cost_map at (x, y)
957  * or (-1, 0) if value is not set or (x, y) is invalid.
958  */
959 std::pair<int, int> full_cost_map::get_pair_at(map_location loc) const
960 {
961  const gamemap& map = resources::gameboard->map();
962  assert(cost_map.size() == static_cast<unsigned>(map.w() * map.h()));
963 
964  if (!map.on_board(loc)) {
965  return std::pair(-1, 0); // invalid
966  }
967 
968  return cost_map[loc.x + (loc.y * map.w())];
969 }
970 
971 /**
972  * Accessor for the costs.
973  *
974  * @return the value of the cost_map at (x, y)
975  * or -1 if value is not set or (x, y) is invalid.
976  */
978 {
979  return get_pair_at(loc).first;
980 }
981 
982 /**
983  * Accessor for the costs.
984  *
985  * @return The average cost of all added units for this hex
986  * or -1 if no unit can reach the hex.
987  */
989 {
990  auto p = get_pair_at(loc);
991  if (p.second == 0) {
992  return -1;
993  } else {
994  return static_cast<double>(p.first) /p.second;
995  }
996 }
997 }//namespace pathfind
bool enemy_zoc(const team &current_team, const map_location &loc, const team &viewing_team, bool see_all)
Determines if a given location is in an enemy zone of control.
Definition: pathfind.cpp:134
Game board class.
Definition: game_board.hpp:50
A const-only interface for how many (movement, vision, or "jamming") points a unit needs for each hex...
Definition: movetype.hpp:52
virtual double cost(const map_location &loc, const double so_far) const
Definition: pathfind.cpp:818
marked_route mark_route(const plain_route &rt, bool update_move_cost)
Add marks on a route rt assuming that the unit located at the first hex of rt travels along it...
Definition: pathfind.cpp:647
unit_iterator end()
Definition: map.hpp:428
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:91
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:84
std::vector< std::pair< int, int > > cost_map
Definition: pathfind.hpp:295
virtual double cost(const map_location &loc, const double so_far) const
Definition: pathfind.cpp:850
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:474
virtual const unit_map & units() const override
Definition: game_board.hpp:111
jamming_path(const unit &jammer, const map_location &loc)
Construct a list of jammed hexes for a unit.
Definition: pathfind.cpp:630
emergency_path_calculator(const unit &u, const gamemap &map)
Definition: pathfind.cpp:846
int jamming() const
Gets the unit&#39;s jamming points.
Definition: unit.hpp:1397
int vision() const
Gets the unit&#39;s vision points.
Definition: unit.hpp:1391
This class represents a single unit of a specific type.
Definition: unit.hpp:120
int dummy
Definition: lstrlib.cpp:1347
bool is_castle(const map_location &loc) const
Definition: map.cpp:69
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit&#39;s movement cost on a particular terrain.
Definition: unit.hpp:1431
map_location find_vacant_castle(const unit &leader)
Wrapper for find_vacant_tile() when looking for a vacant castle tile near a leader.
Definition: pathfind.cpp:117
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
Definition: unit.cpp:1274
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
const terrain_costs & get_jamming() const
Definition: movetype.hpp:271
int cost(const t_translation::terrain_code &terrain, bool slowed=false) const
Returns the cost associated with the given terrain.
Definition: movetype.hpp:68
#define a
static const int UNREACHABLE
Magic value that signifies a hex is unreachable.
Definition: movetype.hpp:175
const movetype & movement_type() const
Get the unit&#39;s movement type.
Definition: unit.hpp:1421
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
virtual const gamemap & map() const override
Definition: game_board.hpp:101
dest_vect destinations
Definition: pathfind.hpp:100
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
static bool step_compare(const paths::step &a, const map_location &b)
Definition: pathfind.cpp:474
The basic "size" of the unit - flying, small land, large land, etc.
Definition: movetype.hpp:43
void add_unit(const unit &u, bool use_max_moves=true)
Adds a units cost map to cost_map (increments the elements in cost_map)
Definition: pathfind.cpp:908
const unit * unit_
static std::string _(const char *str)
Definition: gettext.hpp:92
virtual double cost(const map_location &loc, const double so_far) const
Definition: pathfind.cpp:860
virtual ~jamming_path()
Default destructor.
Definition: pathfind.cpp:643
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:25
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:189
A single unit type that the player may recruit.
Definition: types.hpp:44
double get_average_cost_at(map_location loc) const
Accessor for the costs.
Definition: pathfind.cpp:988
std::set< map_location > get_adjacents(map_location loc) const
Definition: teleport.cpp:229
static double getNoPathValue()
Definition: pathfind.hpp:64
#define b
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit&#39;s defense on a given terrain.
Definition: unit.cpp:1568
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:96
map_location prev
Definition: pathfind.cpp:157
std::vector< map_location > steps
Definition: pathfind.hpp:134
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
int w() const
Effective map width.
Definition: map.hpp:49
bool uses_shroud() const
Definition: team.hpp:326
move_type_path_calculator(const movetype &mt, int movement_left, int total_movement, const team &t, const gamemap &map)
Definition: pathfind.cpp:812
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:210
const bool allow_teleport_
Definition: pathfind.hpp:299
map_location curr
Definition: pathfind.hpp:88
const terrain_costs & get_movement() const
Definition: movetype.hpp:269
int h
Definition: pathfind.cpp:188
static lg::log_domain log_engine("engine")
game_board * gameboard
Definition: resources.cpp:20
const terrain_costs & get_vision() const
Definition: movetype.hpp:270
virtual double cost(const map_location &loc, const double so_far) const
Definition: pathfind.cpp:731
VACANT_TILE_TYPE
Definition: pathfind.hpp:38
static IdxT partition(lua_State *L, IdxT lo, IdxT up)
Definition: ltablib.cpp:296
dummy_path_calculator(const unit &u, const gamemap &map)
Definition: pathfind.cpp:857
Encapsulates the map of the game.
Definition: map.hpp:170
bool is_enemy(int n) const
Definition: team.hpp:251
std::string path
Definition: game_config.cpp:38
map_display and display: classes which take care of displaying the map and game-data on the screen...
int moves_left
Definition: pathfind.cpp:156
virtual ~vision_path()
Default destructor.
Definition: pathfind.cpp:616
full_cost_map(const unit &u, bool force_ignore_zoc, bool allow_teleport, const team &viewing_team, bool see_all=true, bool ignore_units=true)
Constructs a cost-map.
Definition: pathfind.cpp:878
Structure which holds a single route and marks for special events.
Definition: pathfind.hpp:140
std::vector< map_location > get_path(const const_iterator &) const
Returns the path going from the source point (included) to the destination point j (excluded)...
Definition: pathfind.cpp:497
Encapsulates the map of the game.
Definition: location.hpp:37
unit_iterator find(std::size_t id)
Definition: map.cpp:309
bool invisible(const map_location &loc, bool see_all=true) const
Definition: unit.cpp:2380
virtual ~paths()
Virtual destructor (default processing).
Definition: pathfind.cpp:560
bool shrouded(const map_location &loc) const
Definition: team.cpp:651
bool get_ability_bool(const std::string &tag_name, const map_location &loc) const
Checks whether this unit currently possesses or is affected by a given ability.
Definition: abilities.cpp:145
static std::string mark
Definition: tstring.cpp:70
std::size_t i
Definition: function.cpp:940
Ordered vector of possible destinations.
Definition: pathfind.hpp:93
const std::vector< team > & teams_
Definition: pathfind.hpp:214
mock_party p
static map_location::DIRECTION s
static void find_routes(const map_location &origin, const movetype::terrain_costs &costs, bool slowed, int moves_left, int max_moves, int turns_left, paths::dest_vect &destinations, std::set< map_location > *edges, const unit *teleporter, const team *current_team, const unit *skirmisher, const team *viewing_team, const std::map< map_location, int > *jamming_map=nullptr, std::vector< std::pair< int, int >> *full_cost_map=nullptr, bool check_vision=false)
Creates a list of routes that a unit can traverse from the provided location.
Definition: pathfind.cpp:270
static bool operator<(const placing_info &a, const placing_info &b)
Definition: game_state.cpp:139
Structure which uses find_routes() to build a cost map This maps each hex to a the movements a unit w...
Definition: pathfind.hpp:266
const team & viewing_team_
Definition: pathfind.hpp:300
const bool ignore_units_
Definition: pathfind.hpp:302
shortest_path_calculator(const unit &u, const team &t, const std::vector< team > &teams, const gamemap &map, bool ignore_unit=false, bool ignore_defense_=false, bool see_all=false)
Definition: pathfind.cpp:721
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:380
std::pair< int, int > get_pair_at(map_location loc) const
Accessor for the cost/reach-amount pairs.
Definition: pathfind.cpp:959
std::string & insert(std::string &str, const std::size_t pos, const std::string &insert)
Insert a UTF-8 string at the specified position.
Definition: unicode.cpp:99
const bool force_ignore_zoc_
Definition: pathfind.hpp:298
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
int movement_cost(const t_translation::terrain_code &terrain, bool slowed=false) const
Returns the cost to move through the indicated terrain.
Definition: movetype.hpp:281
#define next(ls)
Definition: llex.cpp:32
int w
Definition: pathfind.cpp:188
bool is_village(const map_location &loc) const
Definition: map.cpp:65
int turns()
Definition: game.cpp:553
double t
Definition: astarsearch.cpp:64
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1348
std::size_t viewing_team() const
The viewing team is the team currently viewing the game.
Definition: display.hpp:105
std::set< map_location > edges
The edges are the non-destination hexes bordering the destinations.
Definition: pathfind.hpp:116
bool contains(const map_location &) const
Definition: pathfind.cpp:514
Standard logging facilities (interface).
const teleport_map get_teleport_locations(const unit &u, const team &viewing_team, bool see_all, bool ignore_units, bool check_vision)
Definition: teleport.cpp:265
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:71
static const map_location & null_location()
Definition: location.hpp:80
Container associating units to locations.
Definition: map.hpp:97
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1257
bool fogged(const map_location &loc) const
Definition: team.cpp:660
unsigned search_num
Definition: pathfind.cpp:161
bool emits_zoc() const
Tests whether the unit has a zone-of-control, considering incapacitated.
Definition: unit.hpp:1329
int side() const
The side this unit belongs to.
Definition: unit.hpp:333
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:203
const_iterator find(const map_location &) const
Definition: pathfind.cpp:478
const std::vector< findroute_node > & nodes
Definition: pathfind.cpp:220
bool owns_village(const map_location &loc) const
Definition: team.hpp:193
bool valid() const
Definition: map.hpp:273
static map_location::DIRECTION n
int h() const
Effective map height.
Definition: map.hpp:52
vision_path(const unit &viewer, const map_location &loc, const std::map< map_location, int > &jamming_map)
Constructs a list of vision paths for a unit.
Definition: pathfind.cpp:576
This module contains various pathfinding functions and utilities.
int turns_left
Definition: pathfind.cpp:156
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
Definition: unit.hpp:1273
void insert(const map_location &)
Definition: pathfind.cpp:485
int get_cost_at(map_location loc) const
Accessor for the costs.
Definition: pathfind.cpp:977