The Battle for Wesnoth  1.15.2+dev
ca.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project https://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  * Default AI (Testing)
17  * @file
18  */
19 
20 #include "ai/default/ca.hpp"
21 #include "ai/actions.hpp"
22 #include "ai/manager.hpp"
23 #include "ai/composite/engine.hpp"
24 #include "ai/composite/rca.hpp"
25 #include "ai/composite/stage.hpp"
26 #include "game_board.hpp"
27 #include "game_classification.hpp"
28 #include "game_data.hpp"
29 #include "log.hpp"
30 #include "map/map.hpp"
31 #include "resources.hpp"
32 #include "team.hpp"
33 #include "units/unit.hpp"
34 #include "pathfind/pathfind.hpp"
35 #include "pathfind/teleport.hpp"
36 
37 #include <numeric>
38 #include <boost/dynamic_bitset.hpp>
39 
40 #include <SDL2/SDL_timer.h>
41 
42 static lg::log_domain log_ai_testing_ai_default("ai/ca/testing_ai_default");
43 #define DBG_AI_TESTING_AI_DEFAULT LOG_STREAM(debug, log_ai_testing_ai_default)
44 #define LOG_AI_TESTING_AI_DEFAULT LOG_STREAM(info, log_ai_testing_ai_default)
45 #define WRN_AI_TESTING_AI_DEFAULT LOG_STREAM(warn, log_ai_testing_ai_default)
46 #define ERR_AI_TESTING_AI_DEFAULT LOG_STREAM(err, log_ai_testing_ai_default)
47 
48 
49 namespace ai {
50 
51 namespace ai_default_rca {
52 
53 //==============================================================
54 
55 goto_phase::goto_phase( rca_context &context, const config &cfg )
56  : candidate_action(context,cfg)
57  , move_()
58 {
59 }
60 
62 {
63 }
64 
66 {
67  // Execute goto-movements - first collect gotos in a list
68  std::vector<map_location> gotos;
69  unit_map &units_ = resources::gameboard->units();
70  const gamemap &map_ = resources::gameboard->map();
71 
72  for(unit_map::iterator ui = units_.begin(); ui != units_.end(); ++ui) {
73  if (ui->get_goto() == ui->get_location()) {
74  ui->set_goto(map_location());
75  } else if (ui->side() == get_side() && map_.on_board(ui->get_goto())) {
76  gotos.push_back(ui->get_location());
77  }
78  }
79 
80  for(std::vector<map_location>::const_iterator g = gotos.begin(); g != gotos.end(); ++g) {
81  unit_map::const_iterator ui = units_.find(*g);
82  // passive_leader: never moves or attacks
83  if(ui->can_recruit() && get_passive_leader() && !get_passive_leader_shares_keep()){
84  continue;
85  }
86  // end of passive_leader
87 
88  if(!is_allowed_unit(*ui)){
89  continue;
90  }
91 
93 
95 
97  route = pathfind::a_star_search(ui->get_location(), ui->get_goto(), 10000.0, calc, map_.w(), map_.h(), &allowed_teleports);
98 
99  if (!route.steps.empty()){
100  move_ = check_move_action(ui->get_location(), route.steps.back(), true, true);
101  } else {
102  // there is no direct path (yet)
103  // go to the nearest hex instead.
104  // maybe a door will open later or something
105 
106  int closest_distance = -1;
107  std::pair<map_location,map_location> closest_move;
108  for(move_map::const_iterator i = get_dstsrc().begin(); i != get_dstsrc().end(); ++i) {
109  if(i->second != ui->get_location()) {
110  continue;
111  }
112  int distance = distance_between(i->first,ui->get_goto());
113  if(closest_distance == -1 || distance < closest_distance) {
114  closest_distance = distance;
115  closest_move = *i;
116  }
117  }
118  if(closest_distance != -1) {
119  move_ = check_move_action(ui->get_location(), closest_move.first);
120  } else {
121  continue;
122  }
123  }
124 
125  if (move_->is_ok()) {
126  return get_score();
127  }
128  }
129 
130  return BAD_SCORE;
131 }
132 
134 {
135  if (!move_) {
136  return;
137  }
138 
139  move_->execute();
140  if (!move_->is_ok()){
141  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok" << std::endl;
142  }
143 
144  // In some situations, a theoretically possible path is blocked by allies,
145  // resulting in the unit not moving. In this case, we remove all remaining
146  // movement from the unit in order to prevent blacklisting of the CA.
147  if (!move_->is_gamestate_changed()){
148  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute did not move unit; removing moves instead" << std::endl;
149  stopunit_result_ptr stopunit = check_stopunit_action(move_->get_unit_location(), true, false);
150  stopunit->execute();
151  }
152 }
153 
154 
155 //==============================================================
156 
158  : candidate_action(context,cfg),best_analysis_(),choice_rating_(-1000.0)
159 {
160 }
161 
163 {
164 }
165 
167 {
168  const unit_map &units_ = resources::gameboard->units();
169  std::vector<std::string> options = get_recruitment_pattern();
170 
171  choice_rating_ = -1000.0;
172  int ticks = SDL_GetTicks();
173 
174  const std::vector<attack_analysis> analysis = get_attacks(); //passive_leader: in aspect_attacks::analyze_targets()
175 
176  int time_taken = SDL_GetTicks() - ticks;
177  LOG_AI_TESTING_AI_DEFAULT << "took " << time_taken << " ticks for " << analysis.size()
178  << " positions. Analyzing...\n";
179 
180  ticks = SDL_GetTicks();
181 
182  const int max_sims = 50000;
183  int num_sims = analysis.empty() ? 0 : max_sims/analysis.size();
184  if(num_sims < 20)
185  num_sims = 20;
186  if(num_sims > 40)
187  num_sims = 40;
188 
189  LOG_AI_TESTING_AI_DEFAULT << "simulations: " << num_sims << "\n";
190 
191  const int max_positions = 30000;
192  const int skip_num = analysis.size()/max_positions;
193 
194  std::vector<attack_analysis>::const_iterator choice_it = analysis.end();
195  for(std::vector<attack_analysis>::const_iterator it = analysis.begin();
196  it != analysis.end(); ++it) {
197 
198  if(skip_num > 0 && ((it - analysis.begin())%skip_num) && it->movements.size() > 1)
199  continue;
200 
201  // This is somewhat inefficient. It would be faster to exclude these attacks
202  // in get_attacks() above, but the CA filter information is not available inside
203  // the attacks aspect code. Providing the filtering here is only done for consistency
204  // with other CAs though, the recommended method of filtering attacks is via
205  // 'filter_own' of the attacks aspect.
206  bool skip_attack = false;
207  for(std::size_t i = 0; i != it->movements.size(); ++i) {
208  const unit_map::const_iterator u = units_.find(it->movements[i].first);
209  if (!is_allowed_unit(*u)) {
210  skip_attack = true;
211  break;
212  }
213  }
214  if (skip_attack)
215  continue;
216 
217  const double rating = it->rating(get_aggression(),*this);
218  LOG_AI_TESTING_AI_DEFAULT << "attack option rated at " << rating << " ("
219  << (it->uses_leader ? get_leader_aggression() : get_aggression()) << ")\n";
220 
221  if(rating > choice_rating_) {
222  choice_it = it;
223  choice_rating_ = rating;
224  }
225  }
226 
227  time_taken = SDL_GetTicks() - ticks;
228  LOG_AI_TESTING_AI_DEFAULT << "analysis took " << time_taken << " ticks\n";
229 
230 
231  // suokko tested the rating against current_team().caution()
232  // Bad mistake -- the AI became extremely reluctant to attack anything.
233  // Documenting this in case someone has this bright idea again...*don't*...
234  if(choice_rating_ > 0.0) {
235  best_analysis_ = *choice_it;
236  return get_score();
237  } else {
238  return BAD_SCORE;
239  }
240 }
241 
243 {
244  assert(choice_rating_ > 0.0);
245  map_location from = best_analysis_.movements[0].first;
246  map_location to = best_analysis_.movements[0].second;
247  map_location target_loc = best_analysis_.target;
248 
249  if (from!=to) {
250  move_result_ptr move_res = execute_move_action(from,to,false);
251  if (!move_res->is_ok()) {
252  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok, move failed" << std::endl;
253  return;
254  }
255  }
256 
257  attack_result_ptr attack_res = check_attack_action(to, target_loc, -1);
258  if (!attack_res->is_ok()) {
259  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok, attack cancelled" << std::endl;
260  } else {
261  attack_res->execute();
262  if (!attack_res->is_ok()) {
263  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok, attack failed" << std::endl;
264  }
265  }
266 
267 }
268 
269 //==============================================================
270 
272  : candidate_action(context,cfg), auto_remove_(), dst_(), id_(), move_()
273 {
274 }
275 
277 {
278 }
279 
281 {
282 
283  const config &goal = get_leader_goal();
284  //passive leader can reach a goal
285  if (!goal) {
286  LOG_AI_TESTING_AI_DEFAULT << get_name() << "No goal found\n";
287  return BAD_SCORE;
288  }
289 
290  if (goal.empty()) {
291  LOG_AI_TESTING_AI_DEFAULT << get_name() << "Empty goal found\n";
292  return BAD_SCORE;
293  }
294 
295  double max_risk = goal["max_risk"].to_double(1 - get_caution());
296  auto_remove_ = goal["auto_remove"].to_bool();
297 
299  if (!dst_.valid()) {
300  ERR_AI_TESTING_AI_DEFAULT << "Invalid goal: "<<std::endl<<goal;
301  return BAD_SCORE;
302  }
303 
304  const unit_map &units_ = resources::gameboard->units();
305  const std::vector<unit_map::const_iterator> leaders = units_.find_leaders(get_side());
306  if (leaders.empty()) {
307  return BAD_SCORE;
308  }
309 
310  const unit* leader = nullptr;
311  for (const unit_map::const_iterator& l_itor : leaders) {
312  if (!l_itor->incapacitated() && l_itor->movement_left() > 0 && is_allowed_unit(*l_itor)) {
313  leader = &(*l_itor);
314  break;
315  }
316  }
317 
318  if (leader == nullptr) {
319  WRN_AI_TESTING_AI_DEFAULT << "Leader not found" << std::endl;
320  return BAD_SCORE;
321  }
322 
323  id_ = goal["id"].str();
324  if (leader->get_location() == dst_) {
325  //goal already reached
326  if (auto_remove_ && !id_.empty()) {
327  remove_goal(id_);
328  } else {
329  move_ = check_move_action(leader->get_location(), leader->get_location(), !auto_remove_);//we do full moves if we don't want to remove goal
330  if (move_->is_ok()) {
331  return get_score();
332  } else {
333  return BAD_SCORE;
334  }
335  }
336  }
337 
339  const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*leader, current_team());
340  pathfind::plain_route route = a_star_search(leader->get_location(), dst_, 1000.0, calc,
341  resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
342  if(route.steps.empty()) {
343  LOG_AI_TESTING_AI_DEFAULT << "route empty";
344  return BAD_SCORE;
345  }
346 
347  const pathfind::paths leader_paths(*leader, false, true, current_team());
348 
349  std::map<map_location,pathfind::paths> possible_moves;
350  possible_moves.emplace(leader->get_location(), leader_paths);
351 
352  map_location loc;
353  for (const map_location &l : route.steps)
354  {
355  if (leader_paths.destinations.contains(l) &&
356  power_projection(l, get_enemy_dstsrc()) < leader->hitpoints() * max_risk)
357  {
358  loc = l;
359  }
360  }
361 
362  if(loc.valid()) {
363  move_ = check_move_action(leader->get_location(), loc, false);
364  if (move_->is_ok()) {
365  return get_score();
366  }
367  }
368  return BAD_SCORE;
369 
370 }
371 
373 {
374  move_->execute();
375  if (!move_->is_ok()){
376  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok" << std::endl;
377  }
378  if (move_->get_unit_location()==dst_) {
379  //goal already reached
380  if (auto_remove_ && !id_.empty()) {
381  remove_goal(id_);
382  }
383  }
384 }
385 
386 void move_leader_to_goals_phase::remove_goal(const std::string &id)
387 {
388  config mod_ai;
389  mod_ai["side"] = get_side();
390  mod_ai["path"] = "aspect[leader_goal].facet["+id+"]";
391  mod_ai["action"] = "delete";
393 }
394 
395 //==============================================================
396 
398  : candidate_action(context,cfg),move_()
399 {
400 
401 }
402 
404 {
405 
406 }
407 
409 {
410  if (get_leader_ignores_keep()) {
411  return BAD_SCORE;
412  }
414  return BAD_SCORE;
415  }
416 
417  // 1. Collect all leaders in a list
418  // 2. Get the suitable_keep for each leader
419  // 3. Choose the leader with the nearest suitable_keep (and which still have moves)
420  // 4. If leader can reach this keep in 1 turn -> set move_ to there
421  // 5. If not -> Calculate the best move_ (use a-star search)
422  // 6. Save move_ for execution
423 
424  // 1.
425  const unit_map &units_ = resources::gameboard->units();
426  const std::vector<unit_map::const_iterator> leaders = units_.find_leaders(get_side());
427  if (leaders.empty()) {
428  return BAD_SCORE;
429  }
430 
431  // 2. + 3.
432  const unit* best_leader = nullptr;
433  map_location best_keep;
434  int shortest_distance = 99999;
435 
436  for (const unit_map::const_iterator& leader : leaders) {
437  if (leader->incapacitated() || leader->movement_left() == 0 || !is_allowed_unit(*leader)) {
438  continue;
439  }
440 
441  // Find where the leader can move
442  const ai::moves_map &possible_moves = get_possible_moves();
443  const ai::moves_map::const_iterator& p_it = possible_moves.find(leader->get_location());
444  if (p_it == possible_moves.end()) {
445  return BAD_SCORE;
446  }
447  const pathfind::paths leader_paths = p_it->second;
448 
449  const map_location& keep = suitable_keep(leader->get_location(), leader_paths);
450  if (keep == map_location::null_location() || keep == leader->get_location()) {
451  continue;
452  }
453 
455 
456  const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*leader, current_team());
457 
458  pathfind::plain_route route;
459  route = pathfind::a_star_search(leader->get_location(), keep, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
460 
461  if (!route.steps.empty() || route.move_cost < shortest_distance) {
462  best_leader = &(*leader);
463  best_keep = keep;
464  shortest_distance = route.move_cost;
465  }
466  }
467 
468  if (best_leader == nullptr) {
469  return BAD_SCORE;
470  }
471 
472  // 4.
473  const unit* leader = best_leader;
474  const map_location keep = best_keep;
475  const pathfind::paths leader_paths(*leader, false, true, current_team());
477  const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*leader, current_team());
478 
479  if (leader_paths.destinations.contains(keep) && units_.count(keep) == 0) {
480  move_ = check_move_action(leader->get_location(), keep, false);
481  if (move_->is_ok()) {
482  return get_score();
483  }
484  }
485 
486  // 5.
487  // The leader can't move to his keep, try to move to the closest location
488  // to the keep where there are no enemies in range.
489  // Make a map of the possible locations the leader can move to,
490  // ordered by the distance from the keep.
491  typedef std::multimap<int, map_location> ordered_locations;
492  ordered_locations moves_toward_keep;
493 
494  pathfind::plain_route route;
495  route = pathfind::a_star_search(leader->get_location(), keep, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
496 
497  // find next hop
499  int next_hop_cost = 0;
500  for (const map_location& step : route.steps) {
501  if (leader_paths.destinations.contains(step)) {
502  next_hop = step;
503  next_hop_cost += leader->movement_cost(resources::gameboard->map().get_terrain(step));
504  } else {
505  break;
506  }
507  }
508  if (next_hop == map_location::null_location()) {
509  return BAD_SCORE;
510  }
511  //define the next hop to have the lowest cost (0)
512  moves_toward_keep.emplace(0, next_hop);
513 
514  for (const pathfind::paths::step &dest : leader_paths.destinations) {
515  if (!units_.find(dest.curr).valid()) {
516  route = pathfind::a_star_search(dest.curr, next_hop, 10000.0, calc,
517  resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
518  if (route.move_cost < next_hop_cost) {
519  moves_toward_keep.emplace(route.move_cost, dest.curr);
520  }
521  }
522  }
523 
524  // Find the first location which we can move to,
525  // without the threat of enemies.
526  for (const ordered_locations::value_type& pair : moves_toward_keep) {
527  const map_location& loc = pair.second;
528  if (get_enemy_dstsrc().count(loc) == 0) {
529  move_ = check_move_action(leader->get_location(), loc, true);
530  if (move_->is_ok()) {
531  return get_score();
532  }
533  }
534  }
535  return BAD_SCORE;
536 }
537 
539 {
540  move_->execute();
541  if (!move_->is_ok()) {
542  LOG_AI_TESTING_AI_DEFAULT << get_name() <<"::execute not ok" << std::endl;
543  }
544 }
545 
546 //==============================================================
547 
549  : candidate_action(context,cfg)
550  , keep_loc_()
551  , leader_loc_()
552  , best_leader_loc_()
553  , debug_(false)
554  , moves_()
555 {
556 }
557 
559 {
560 }
561 
563 {
564  moves_.clear();
567  if (!moves_.empty()) {
568  return get_score();
569  }
570  return BAD_SCORE;
571 }
572 
573 
575 {
576  unit_map &units_ = resources::gameboard->units();
577  unit_map::const_iterator leader = units_.find_leader(get_side());
578  // Move all the units to get villages, however move the leader last,
579  // so that the castle will be cleared if it wants to stop to recruit along the way.
580  std::pair<map_location,map_location> leader_move;
581 
582  for(tmoves::const_iterator i = moves_.begin(); i != moves_.end(); ++i) {
583 
584  if(leader != units_.end() && leader->get_location() == i->second) {
585  leader_move = *i;
586  } else {
587  if (resources::gameboard->find_visible_unit(i->first, current_team()) == units_.end()) {
588  move_result_ptr move_res = execute_move_action(i->second,i->first,true);
589  if (!move_res->is_ok()) {
590  return;
591  }
592 
593  const map_location loc = move_res->get_unit_location();
594  leader = units_.find_leader(get_side());
595  const unit_map::const_iterator new_unit = units_.find(loc);
596 
597  if (new_unit != units_.end() &&
598  power_projection(i->first, get_enemy_dstsrc()) >= new_unit->hitpoints() / 4.0)
599  {
600  LOG_AI_TESTING_AI_DEFAULT << "found support target... " << new_unit->get_location() << '\n';
601  }
602  }
603  }
604  }
605 
606  if(leader_move.second.valid()) {
607  if((resources::gameboard->find_visible_unit(leader_move.first , current_team()) == units_.end())
608  && resources::gameboard->map().is_village(leader_move.first)) {
609  move_result_ptr move_res = execute_move_action(leader_move.second,leader_move.first,true);
610  if (!move_res->is_ok()) {
611  return;
612  }
613  }
614  }
615 
616  return;
617 }
618 
620  const move_map& dstsrc, const move_map& enemy_dstsrc,
621  unit_map::const_iterator &leader)
622 {
623  DBG_AI_TESTING_AI_DEFAULT << "deciding which villages we want...\n";
624  unit_map &units_ = resources::gameboard->units();
625  const int ticks = SDL_GetTicks();
627  if(leader != units_.end()) {
628  keep_loc_ = nearest_keep(leader->get_location());
629  leader_loc_ = leader->get_location();
630  } else {
633  }
634 
636 
637  // Find our units who can move.
638  treachmap reachmap;
639  for(unit_map::const_iterator u_itor = units_.begin();
640  u_itor != units_.end(); ++u_itor) {
641  if(u_itor->can_recruit() && get_passive_leader()){
642  continue;
643  }
644  if(u_itor->side() == get_side() && u_itor->movement_left() && is_allowed_unit(*u_itor)) {
645  reachmap.emplace(u_itor->get_location(), std::vector<map_location>());
646  }
647  }
648 
649 
650  DBG_AI_TESTING_AI_DEFAULT << reachmap.size() << " units found who can try to capture a village.\n";
651 
652  find_villages(reachmap, moves_, dstsrc, enemy_dstsrc);
653 
654  treachmap::iterator itor = reachmap.begin();
655  while(itor != reachmap.end()) {
656  if(itor->second.empty()) {
657  itor = remove_unit(reachmap, moves_, itor);
658  } else {
659  ++itor;
660  }
661  }
662 
663  if(!reachmap.empty()) {
664  DBG_AI_TESTING_AI_DEFAULT << reachmap.size() << " units left after removing the ones who "
665  "can't reach a village, send the to the dispatcher.\n";
666 
667  dump_reachmap(reachmap);
668 
669  dispatch(reachmap, moves_);
670  } else {
671  DBG_AI_TESTING_AI_DEFAULT << "No more units left after removing the ones who can't reach a village.\n";
672  }
673 
674  LOG_AI_TESTING_AI_DEFAULT << "Village assignment done: " << (SDL_GetTicks() - ticks)
675  << " ms, resulted in " << moves_.size() << " units being dispatched.\n";
676 
677 }
678 
680  treachmap& reachmap,
681  tmoves& moves,
682  const std::multimap<map_location,map_location>& dstsrc,
683  const std::multimap<map_location,map_location>& enemy_dstsrc)
684 
685 {
686  std::map<map_location, double> vulnerability;
687 
688  const bool passive_leader = get_passive_leader();
689 
690  std::size_t min_distance = 100000;
691  const gamemap &map_ = resources::gameboard->map();
692  std::vector<team> &teams_ = resources::gameboard->teams();
693 
694  // When a unit is dispatched we need to make sure we don't
695  // dispatch this unit a second time, so store them here.
696  std::vector<map_location> dispatched_units;
697  for(std::multimap<map_location, map_location>::const_iterator
698  j = dstsrc.begin();
699  j != dstsrc.end(); ++j) {
700 
701  const map_location &current_loc = j->first;
702 
703  if(j->second == leader_loc_) {
704  if(passive_leader) {
705  continue;
706  }
707 
708  const std::size_t distance = distance_between(keep_loc_, current_loc);
709  if(distance < min_distance) {
710  min_distance = distance;
711  best_leader_loc_ = current_loc;
712  }
713  }
714 
715  if(std::find(dispatched_units.begin(), dispatched_units.end(),
716  j->second) != dispatched_units.end()) {
717  continue;
718  }
719 
720  if(map_.is_village(current_loc) == false) {
721  continue;
722  }
723 
724  bool want_village = true, owned = false;
725  for(std::size_t n = 0; n != teams_.size(); ++n) {
726  owned = teams_[n].owns_village(current_loc);
727  if(owned && !current_team().is_enemy(n+1)) {
728  want_village = false;
729  }
730 
731  if(owned) {
732  break;
733  }
734  }
735 
736  if(want_village == false) {
737  continue;
738  }
739 
740  // If it is a neutral village, and we have no leader,
741  // then the village is of no use to us, and we don't want it.
742  if(!owned && leader_loc_ == map_location::null_location()) {
743  continue;
744  }
745 
746  double threat = 0.0;
747  const std::map<map_location,double>::const_iterator vuln = vulnerability.find(current_loc);
748  if(vuln != vulnerability.end()) {
749  threat = vuln->second;
750  } else {
751  threat = power_projection(current_loc,enemy_dstsrc);
752  vulnerability.emplace(current_loc, threat);
753  }
754 
756  if (u == resources::gameboard->units().end() || u->get_state("guardian") || !is_allowed_unit(*u)) {
757  continue;
758  }
759 
760  const unit &un = *u;
761  //FIXME: suokko turned this 2:1 to 1.5:1.0.
762  //and dropped the second term of the multiplication. Is that better?
763  //const double threat_multipler = (current_loc == leader_loc?2:1) * current_team().caution() * 10;
764  if(un.hitpoints() < (threat*2*un.defense_modifier(map_.get_terrain(current_loc)))/100) {
765  continue;
766  }
767 
768  // If the next and previous destination differs from our current destination,
769  // we're the only one who can reach the village -> dispatch.
770  std::multimap<map_location, map_location>::const_iterator next = j;
771  ++next; // j + 1 fails
772  const bool at_begin = (j == dstsrc.begin());
773  std::multimap<map_location, map_location>::const_iterator prev = j; //FIXME seems not to work
774  if(!at_begin) {
775  --prev;
776  }
777 #if 1
778  if((next == dstsrc.end() || next->first != current_loc)
779  && (at_begin || prev->first != current_loc)) {
780 
781  move_result_ptr move_check_res = check_move_action(j->second,j->first,true);
782  if (move_check_res->is_ok()) {
783  DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << j->second << " to village " << j->first << '\n';
784  moves.emplace_back(j->first, j->second);
785  }
786  reachmap.erase(j->second);
787  dispatched_units.push_back(j->second);
788  continue;
789  }
790 #endif
791  reachmap[j->second].push_back(current_loc);
792  }
793 
794  DBG_AI_TESTING_AI_DEFAULT << moves.size() << " units already dispatched, "
795  << reachmap.size() << " left to evaluate.\n";
796 }
797 
799 {
800  DBG_AI_TESTING_AI_DEFAULT << "Starting simple dispatch.\n";
801 
802  // we now have a list with units with the villages they can reach.
803  // keep trying the following steps as long as one of them changes
804  // the state.
805  // 1. Dispatch units who can reach 1 village (if more units can reach that
806  // village only one can capture it, so use the first in the list.)
807  // 2. Villages which can only be reached by one unit get that unit dispatched
808  // to them.
809  std::size_t village_count = 0;
810  bool dispatched = true;
811  while(dispatched) {
812  dispatched = false;
813 
814  if(dispatch_unit_simple(reachmap, moves)) {
815  dispatched = true;
816  } else {
817  if(reachmap.empty()) {
818  DBG_AI_TESTING_AI_DEFAULT << "dispatch_unit_simple() found a final solution.\n";
819  break;
820  } else {
821  DBG_AI_TESTING_AI_DEFAULT << "dispatch_unit_simple() couldn't dispatch more units.\n";
822  }
823  }
824 
825  if(dispatch_village_simple(reachmap, moves, village_count)) {
826  dispatched = true;
827  } else {
828  if(reachmap.empty()) {
829  DBG_AI_TESTING_AI_DEFAULT << "dispatch_village_simple() found a final solution.\n";
830  break;
831  } else {
832  DBG_AI_TESTING_AI_DEFAULT << "dispatch_village_simple() couldn't dispatch more units.\n";
833  }
834  }
835 
836  if(!reachmap.empty() && dispatched) {
837  DBG_AI_TESTING_AI_DEFAULT << reachmap.size() << " unit(s) left restarting simple dispatching.\n";
838 
839  dump_reachmap(reachmap);
840  }
841  }
842 
843  if(reachmap.empty()) {
844  DBG_AI_TESTING_AI_DEFAULT << "No units left after simple dispatcher.\n";
845  return;
846  }
847 
848  DBG_AI_TESTING_AI_DEFAULT << reachmap.size() << " units left for complex dispatch with "
849  << village_count << " villages left.\n";
850 
851  dump_reachmap(reachmap);
852 
853  dispatch_complex(reachmap, moves, village_count);
854 }
855 
856 // Returns need further processing
857 // false Nothing has been modified or no units left
859 {
860  bool result = false;
861 
862  treachmap::iterator itor = reachmap.begin();
863  while(itor != reachmap.end()) {
864  if(itor->second.size() == 1) {
865  const map_location village = itor->second[0];
866  result = true;
867 
868  DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << itor->first << " to village " << village << '\n';
869  moves.emplace_back(village, itor->first);
870  reachmap.erase(itor++);
871 
872  if(remove_village(reachmap, moves, village)) {
873  itor = reachmap.begin();
874  }
875 
876  } else {
877  ++itor;
878  }
879  }
880 
881  // Test special cases.
882  if(reachmap.empty()) {
883  // We're done.
884  return false;
885  }
886 
887  if(reachmap.size() == 1) {
888  // One unit left.
889  DBG_AI_TESTING_AI_DEFAULT << "Dispatched _last_ unit at " << reachmap.begin()->first
890  << " to village " << reachmap.begin()->second[0] << '\n';
891 
892  moves.emplace_back(reachmap.begin()->second[0], reachmap.begin()->first);
893 
894  reachmap.clear();
895  // We're done.
896  return false;
897  }
898 
899  return result;
900 }
901 
903  treachmap& reachmap, tmoves& moves, std::size_t& village_count)
904 {
905 
906  bool result = false;
907  bool dispatched = true;
908  while(dispatched) {
909  dispatched = false;
910 
911  // build the reverse map
912  std::map<map_location /*village location*/,
913  std::vector<map_location /* units that can reach it*/>>reversemap;
914 
915  treachmap::const_iterator itor = reachmap.begin();
916  for(;itor != reachmap.end(); ++itor) {
917 
918  for(std::vector<map_location>::const_iterator
919  v_itor = itor->second.begin();
920  v_itor != itor->second.end(); ++v_itor) {
921 
922  reversemap[*v_itor].push_back(itor->first);
923 
924  }
925  }
926 
927  village_count = reversemap.size();
928 
929  itor = reversemap.begin();
930  while(itor != reversemap.end()) {
931  if(itor->second.size() == 1) {
932  // One unit can reach this village.
933  const map_location village = itor->first;
934  dispatched = true;
935  result = true;
936 
937  DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << itor->second[0] << " to village " << itor->first << '\n';
938  moves.emplace_back(itor->first, itor->second[0]);
939 
940  reachmap.erase(itor->second[0]);
941  remove_village(reachmap, moves, village);
942  // Get can go to some trouble to remove the unit from the other villages
943  // instead we abort this loop end do a full rebuild on the map.
944  break;
945  } else {
946  ++itor;
947  }
948  }
949  }
950 
951  return result;
952 }
953 
955  treachmap& reachmap, tmoves& moves, const map_location& village)
956 {
957  bool result = false;
958  treachmap::iterator itor = reachmap.begin();
959  while(itor != reachmap.end()) {
960  itor->second.erase(std::remove(itor->second.begin(), itor->second.end(), village), itor->second.end());
961  if(itor->second.empty()) {
962  result = true;
963  itor = remove_unit(reachmap, moves, itor);
964  } else {
965  ++itor;
966  }
967  }
968  return result;
969 }
970 
972  treachmap& reachmap, tmoves& moves, treachmap::iterator unit)
973 {
974  assert(unit->second.empty());
975 
976  if(unit->first == leader_loc_ && best_leader_loc_ != map_location::null_location()) {
977  DBG_AI_TESTING_AI_DEFAULT << "Dispatch leader at " << leader_loc_ << " closer to the keep at "
978  << best_leader_loc_ << '\n';
979 
980  moves.emplace_back(best_leader_loc_, leader_loc_);
981  }
982 
983  reachmap.erase(unit++);
984  return unit;
985 }
986 
988  treachmap& reachmap, tmoves& moves, const std::size_t village_count)
989 {
990  // ***** ***** Init and dispatch if every unit can reach every village.
991 
992  const std::size_t unit_count = reachmap.size();
993  // The maximum number of villages we can capture with the available units.
994  const std::size_t max_result = unit_count < village_count ? unit_count : village_count;
995 
996  assert(unit_count >= 2 && village_count >= 2);
997 
998  // Every unit can reach every village.
999  if(unit_count == 2 && village_count == 2) {
1000  DBG_AI_TESTING_AI_DEFAULT << "Every unit can reach every village for 2 units, dispatch them.\n";
1001  full_dispatch(reachmap, moves);
1002  return;
1003  }
1004 
1005  std::vector<map_location> units(unit_count);
1006  std::vector<std::size_t> villages_per_unit(unit_count);
1007  std::vector<map_location> villages;
1008  std::vector<std::size_t> units_per_village(village_count);
1009 
1010  // We want to test the units, the ones who can reach the least
1011  // villages first so this is our lookup map.
1012  std::multimap<std::size_t /* villages_per_unit value*/,
1013  std::size_t /*villages_per_unit index*/> unit_lookup;
1014 
1015  std::vector</*unit*/boost::dynamic_bitset</*village*/>> matrix(reachmap.size(), boost::dynamic_bitset<>(village_count));
1016 
1017  treachmap::const_iterator itor = reachmap.begin();
1018  for(std::size_t u = 0; u < unit_count; ++u, ++itor) {
1019  units[u] = itor->first;
1020  villages_per_unit[u] = itor->second.size();
1021  unit_lookup.emplace(villages_per_unit[u], u);
1022 
1023  assert(itor->second.size() >= 2);
1024 
1025  for(std::size_t v = 0; v < itor->second.size(); ++v) {
1026 
1027  std::size_t v_index;
1028  // find the index of the v in the villages
1029  std::vector<map_location>::const_iterator v_itor =
1030  std::find(villages.begin(), villages.end(), itor->second[v]);
1031  if(v_itor == villages.end()) {
1032  v_index = villages.size(); // will be the last element after push_back.
1033  villages.push_back(itor->second[v]);
1034  } else {
1035  v_index = v_itor - villages.begin();
1036  }
1037 
1038  units_per_village[v_index]++;
1039 
1040  matrix[u][v_index] = true;
1041  }
1042  }
1043  for(std::vector<std::size_t>::const_iterator upv_it = units_per_village.begin();
1044  upv_it != units_per_village.end(); ++upv_it) {
1045 
1046  assert(*upv_it >=2);
1047  }
1048 
1049  if(debug_) {
1050  // Print header
1051  std::cerr << "Reach matrix:\n\nvillage";
1052  std::size_t u, v;
1053  for(v = 0; v < village_count; ++v) {
1054  std::cerr << '\t' << villages[v];
1055  }
1056  std::cerr << "\ttotal\nunit\n";
1057 
1058  // Print data
1059  for(u = 0; u < unit_count; ++u) {
1060  std::cerr << units[u];
1061 
1062  for(v = 0; v < village_count; ++v) {
1063  std::cerr << '\t' << matrix[u][v];
1064  }
1065  std::cerr << "\t" << villages_per_unit[u] << '\n';
1066  }
1067 
1068  // Print footer
1069  std::cerr << "total";
1070  for(v = 0; v < village_count; ++v) {
1071  std::cerr << '\t' << units_per_village[v];
1072  }
1073  std::cerr << '\n';
1074  }
1075 
1076  // Test the special case, everybody can reach all villages
1077  const bool reach_all = ((village_count == unit_count)
1078  && (std::accumulate(villages_per_unit.begin(), villages_per_unit.end(), std::size_t())
1079  == (village_count * unit_count)));
1080 
1081  if(reach_all) {
1082  DBG_AI_TESTING_AI_DEFAULT << "Every unit can reach every village, dispatch them\n";
1083  full_dispatch(reachmap, moves);
1084  reachmap.clear();
1085  return;
1086  }
1087 
1088  // ***** ***** Find a square
1089  std::multimap<std::size_t /* villages_per_unit value*/, std::size_t /*villages_per_unit index*/>
1090  ::const_iterator src_itor = unit_lookup.begin();
1091 
1092  while(src_itor != unit_lookup.end() && src_itor->first == 2) {
1093 
1094  for(std::multimap<std::size_t, std::size_t>::const_iterator
1095  dst_itor = unit_lookup.begin();
1096  dst_itor != unit_lookup.end(); ++ dst_itor) {
1097 
1098  // avoid comparing us with ourselves.
1099  if(src_itor == dst_itor) {
1100  continue;
1101  }
1102 
1103  boost::dynamic_bitset<> result = matrix[src_itor->second] & matrix[dst_itor->second];
1104  std::size_t matched = result.count();
1105 
1106  // we found a solution, dispatch
1107  if(matched == 2) {
1108  // Collect data
1109  std::size_t first = result.find_first();
1110  std::size_t second = result.find_next(first);
1111 
1112  const map_location village1 = villages[first];
1113  const map_location village2 = villages[second];
1114 
1115  const bool perfect = (src_itor->first == 2 &&
1116  dst_itor->first == 2 &&
1117  units_per_village[first] == 2 &&
1118  units_per_village[second] == 2);
1119 
1120  // Dispatch
1121  DBG_AI_TESTING_AI_DEFAULT << "Found a square.\nDispatched unit at " << units[src_itor->second]
1122  << " to village " << village1 << '\n';
1123  moves.emplace_back(village1, units[src_itor->second]);
1124 
1125  DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << units[dst_itor->second]
1126  << " to village " << village2 << '\n';
1127  moves.emplace_back(village2, units[dst_itor->second]);
1128 
1129  // Remove the units
1130  reachmap.erase(units[src_itor->second]);
1131  reachmap.erase(units[dst_itor->second]);
1132 
1133  // Evaluate and start correct function.
1134  if(perfect) {
1135  // We did a perfect dispatch 2 units who could visit 2 villages.
1136  // This means we didn't change the assertion for this functions
1137  // so call ourselves recursively, and finish afterwards.
1138  DBG_AI_TESTING_AI_DEFAULT << "Perfect dispatch, do complex again.\n";
1139  dispatch_complex(reachmap, moves, village_count - 2);
1140  return;
1141  } else {
1142  // We did a not perfect dispatch but we did modify things
1143  // so restart dispatching.
1144  DBG_AI_TESTING_AI_DEFAULT << "NON Perfect dispatch, do dispatch again.\n";
1145  remove_village(reachmap, moves, village1);
1146  remove_village(reachmap, moves, village2);
1147  dispatch(reachmap, moves);
1148  return;
1149  }
1150  }
1151  }
1152 
1153  ++src_itor;
1154  }
1155 
1156  // ***** ***** Do all permutations.
1157  // Now walk through all possible permutations
1158  // - test whether the suggestion is possible
1159  // - does it result in max_villages
1160  // - dispatch and ready
1161  // - is it's result better as the last best
1162  // - store
1163  std::vector<std::pair<map_location, map_location>> best_result;
1164 
1165  // Bruteforcing all possible permutations can result in a slow game.
1166  // So there needs to be a balance between the best possible result and
1167  // not too slow. From the test (at the end of the file) a good number is
1168  // picked. In general we shouldn't reach this point too often if we do
1169  // there are a lot of villages which are unclaimed and a lot of units
1170  // to claim them.
1171  const std::size_t max_options = 8;
1172  if(unit_count >= max_options && village_count >= max_options) {
1173 
1174  DBG_AI_TESTING_AI_DEFAULT << "Too many units " << unit_count << " and villages "
1175  << village_count<<" found, evaluate only the first "
1176  << max_options << " options;\n";
1177 
1178  std::vector<std::size_t> perm (max_options, 0);
1179  for(std::size_t i =0; i < max_options; ++i) {
1180  perm[i] = i;
1181  }
1182  while(std::next_permutation(perm.begin(), perm.end())) {
1183 
1184  // Get result for current permutation.
1185  std::vector<std::pair<map_location,map_location>> result;
1186  for(std::size_t u = 0; u < max_options; ++u) {
1187  if(matrix[u][perm[u]]) {
1188  result.emplace_back(villages[perm[u]], units[u]);
1189 
1190  }
1191  }
1192  if(result.size() == max_result) {
1193  best_result.swap(result);
1194  break;
1195  }
1196 
1197  if(result.size() > best_result.size()) {
1198  best_result.swap(result);
1199  }
1200  }
1201  // End of loop no optimal found, assign the best
1202  moves.insert(moves.end(), best_result.begin(), best_result.end());
1203 
1204  // Clean up the reachmap for dispatched units.
1205  for(const auto& unit_village_pair : best_result) {
1206  reachmap.erase(unit_village_pair.second);
1207  }
1208 
1209  // Try to dispatch whatever is left
1210  dispatch(reachmap, moves);
1211  return;
1212 
1213  } else if(unit_count <= village_count) {
1214 
1215  DBG_AI_TESTING_AI_DEFAULT << "Unit major\n";
1216 
1217  std::vector<std::size_t> perm (unit_count, 0);
1218  for(std::size_t i =0; i < unit_count; ++i) {
1219  perm[i] = i;
1220  }
1221  while(std::next_permutation(perm.begin(), perm.end())) {
1222  // Get result for current permutation.
1223  std::vector<std::pair<map_location,map_location>> result;
1224  for(std::size_t u = 0; u < unit_count; ++u) {
1225  if(matrix[u][perm[u]]) {
1226  result.emplace_back(villages[perm[u]], units[u]);
1227 
1228  }
1229  }
1230  if(result.size() == max_result) {
1231  moves.insert(moves.end(), result.begin(), result.end());
1232  reachmap.clear();
1233  return;
1234  }
1235 
1236  if(result.size() > best_result.size()) {
1237  best_result.swap(result);
1238  }
1239  }
1240  // End of loop no optimal found, assign the best
1241  moves.insert(moves.end(), best_result.begin(), best_result.end());
1242 
1243  // clean up the reachmap we need to test whether the leader is still there
1244  // and if so remove him manually to get him dispatched.
1245  for(const auto& unit_village_pair : best_result) {
1246  reachmap.erase(unit_village_pair.second);
1247  }
1248  treachmap::iterator unit = reachmap.find(leader_loc_);
1249  if(unit != reachmap.end()) {
1250  unit->second.clear();
1251  remove_unit(reachmap, moves, unit);
1252  }
1253  reachmap.clear();
1254 
1255  } else {
1256 
1257  DBG_AI_TESTING_AI_DEFAULT << "Village major\n";
1258 
1259  std::vector<std::size_t> perm (village_count, 0);
1260  for(std::size_t i =0; i < village_count; ++i) {
1261  perm[i] = i;
1262  }
1263  while(std::next_permutation(perm.begin(), perm.end())) {
1264  // Get result for current permutation.
1265  std::vector<std::pair<map_location,map_location>> result;
1266  for(std::size_t v = 0; v < village_count; ++v) {
1267  if(matrix[perm[v]][v]) {
1268  result.emplace_back(villages[v], units[perm[v]]);
1269 
1270  }
1271  }
1272  if(result.size() == max_result) {
1273  moves.insert(moves.end(), result.begin(), result.end());
1274  reachmap.clear();
1275  return;
1276  }
1277 
1278  if(result.size() > best_result.size()) {
1279  best_result.swap(result);
1280  }
1281  }
1282  // End of loop no optimal found, assigne the best
1283  moves.insert(moves.end(), best_result.begin(), best_result.end());
1284 
1285  // clean up the reachmap we need to test whether the leader is still there
1286  // and if so remove him manually to get him dispatched.
1287  for(const auto& unit_village_pair : best_result) {
1288  reachmap.erase(unit_village_pair.second);
1289  }
1290  treachmap::iterator unit = reachmap.find(leader_loc_);
1291  if(unit != reachmap.end()) {
1292  unit->second.clear();
1293  remove_unit(reachmap, moves, unit);
1294  }
1295  reachmap.clear();
1296  }
1297 }
1298 
1300 {
1301  treachmap::const_iterator itor = reachmap.begin();
1302  for(std::size_t i = 0; i < reachmap.size(); ++i, ++itor) {
1303  DBG_AI_TESTING_AI_DEFAULT << "Dispatched unit at " << itor->first
1304  << " to village " << itor->second[i] << '\n';
1305  moves.emplace_back(itor->second[i], itor->first);
1306  }
1307 }
1308 
1310 {
1311  if(!debug_) {
1312  return;
1313  }
1314 
1315  for(treachmap::const_iterator itor =
1316  reachmap.begin(); itor != reachmap.end(); ++itor) {
1317 
1318  std::cerr << "Reachlist for unit at " << itor->first;
1319 
1320  if(itor->second.empty()) {
1321  std::cerr << "\tNone";
1322  }
1323 
1324  for(std::vector<map_location>::const_iterator
1325  v_itor = itor->second.begin();
1326  v_itor != itor->second.end(); ++v_itor) {
1327 
1328  std::cerr << '\t' << *v_itor;
1329  }
1330  std::cerr << '\n';
1331 
1332  }
1333 }
1334 
1335 //==============================================================
1336 
1338  : candidate_action(context,cfg),move_()
1339 {
1340 }
1341 
1343 {
1344 }
1345 
1347 {
1348  // Find units in need of healing.
1349  unit_map &units_ = resources::gameboard->units();
1350  unit_map::iterator u_it = units_.begin();
1351  for(; u_it != units_.end(); ++u_it) {
1352  unit &u = *u_it;
1353 
1354  if(u.can_recruit() && get_passive_leader()){
1355  continue;
1356  }
1357 
1358  // If the unit is on our side, has lost as many or more than
1359  // 1/2 round worth of healing, and doesn't regenerate itself,
1360  // then try to find a vacant village for it to rest in.
1361  if(u.side() == get_side() &&
1364  !u.get_ability_bool("regenerate") && is_allowed_unit(*u_it))
1365  {
1366  // Look for the village which is the least vulnerable to enemy attack.
1367  typedef std::multimap<map_location,map_location>::const_iterator Itor;
1368  std::pair<Itor,Itor> it = get_srcdst().equal_range(u_it->get_location());
1369  double best_vulnerability = 100000.0;
1370  // Make leader units more unlikely to move to vulnerable villages
1371  const double leader_penalty = (u.can_recruit()?2.0:1.0);
1372  Itor best_loc = it.second;
1373  while(it.first != it.second) {
1374  const map_location& dst = it.first->second;
1375  if (resources::gameboard->map().gives_healing(dst) && (units_.find(dst) == units_.end() || dst == u_it->get_location())) {
1376  const double vuln = power_projection(dst, get_enemy_dstsrc());
1377  DBG_AI_TESTING_AI_DEFAULT << "found village with vulnerability: " << vuln << "\n";
1378  if(vuln < best_vulnerability) {
1379  best_vulnerability = vuln;
1380  best_loc = it.first;
1381  DBG_AI_TESTING_AI_DEFAULT << "chose village " << dst << '\n';
1382  }
1383  }
1384 
1385  ++it.first;
1386  }
1387 
1388  // If we have found an eligible village,
1389  // and we can move there without expecting to get whacked next turn:
1390  if(best_loc != it.second && best_vulnerability*leader_penalty < u.hitpoints()) {
1391  move_ = check_move_action(best_loc->first,best_loc->second,true);
1392  if (move_->is_ok()) {
1393  return get_score();
1394  }
1395  }
1396  }
1397  }
1398 
1399  return BAD_SCORE;
1400 }
1401 
1403 {
1404  LOG_AI_TESTING_AI_DEFAULT << "moving unit to village for healing...\n";
1405  move_->execute();
1406  if (!move_->is_ok()){
1407  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok" << std::endl;
1408  }
1409 }
1410 
1411 //==============================================================
1412 
1414  : candidate_action(context,cfg), move_()
1415 {
1416 }
1417 
1419 {
1420 }
1421 
1423 {
1424 
1425 
1426  // Get versions of the move map that assume that all units are at full movement
1427  const unit_map& units_ = resources::gameboard->units();
1428 
1429  //unit_map::const_iterator leader = units_.find_leader(get_side());
1430  std::vector<unit_map::const_iterator> leaders = units_.find_leaders(get_side());
1431  std::map<map_location,pathfind::paths> dummy_possible_moves;
1432 
1433  move_map fullmove_srcdst;
1434  move_map fullmove_dstsrc;
1435  calculate_possible_moves(dummy_possible_moves, fullmove_srcdst, fullmove_dstsrc,
1436  false, true, &get_avoid());
1437 
1438  /*adjacent_loc_array_t leader_adj;
1439  if(leader != units_.end()) {
1440  get_adjacent_tiles(leader->get_location(), leader_adj.data());
1441  }*/
1442  //int leader_adj_count = 0;
1443  std::vector<map_location> leaders_adj_v;
1444  for (unit_map::const_iterator leader : leaders) {
1445  adjacent_loc_array_t tmp_leader_adj;
1446  get_adjacent_tiles(leader->get_location(), tmp_leader_adj.data());
1447  for (map_location &loc : tmp_leader_adj) {
1448  bool found = false;
1449  for (map_location &new_loc : leaders_adj_v) {
1450  if(new_loc == loc){
1451  found = true;
1452  break;
1453  }
1454  }
1455  if(!found){
1456  leaders_adj_v.push_back(loc);
1457  }
1458  }
1459  }
1460  //leader_adj_count = leaders_adj_v.size();
1461 
1462 
1463  for(unit_map::const_iterator i = units_.begin(); i != units_.end(); ++i) {
1464  if (i->side() == get_side() &&
1465  i->movement_left() == i->total_movement() &&
1466  //leaders.find(*i) == leaders.end() && //unit_map::const_iterator(i) != leader &&
1467  std::find(leaders.begin(), leaders.end(), i) == leaders.end() &&
1468  !i->incapacitated() && is_allowed_unit(*i))
1469  {
1470  // This unit still has movement left, and is a candidate to retreat.
1471  // We see the amount of power of each side on the situation,
1472  // and decide whether it should retreat.
1473  if(should_retreat(i->get_location(), i, fullmove_srcdst, fullmove_dstsrc, get_caution())) {
1474 
1475  bool can_reach_leader = false;
1476 
1477  // Time to retreat. Look for the place where the power balance
1478  // is most in our favor.
1479  // If we can't find anywhere where we like the power balance,
1480  // just try to get to the best defensive hex.
1481  typedef move_map::const_iterator Itor;
1482  std::pair<Itor,Itor> itors = get_srcdst().equal_range(i->get_location());
1483  map_location best_pos, best_defensive(i->get_location());
1484 
1485  double best_rating = -1000.0;
1486  int best_defensive_rating = i->defense_modifier(resources::gameboard->map().get_terrain(i->get_location()))
1487  - (resources::gameboard->map().is_village(i->get_location()) ? 10 : 0);
1488  while(itors.first != itors.second) {
1489 
1490  //if(leader != units_.end() && std::count(leader_adj,
1491  // leader_adj + 6, itors.first->second)) {
1492  if(std::find(leaders_adj_v.begin(), leaders_adj_v.end(), itors.first->second) != leaders_adj_v.end()){
1493 
1494  can_reach_leader = true;
1495  break;
1496  }
1497 
1498  // We rate the power balance of a hex based on our power projection
1499  // compared to theirs, multiplying their power projection by their
1500  // chance to hit us on the hex we're planning to flee to.
1501  const map_location& hex = itors.first->second;
1502  const int defense = i->defense_modifier(resources::gameboard->map().get_terrain(hex));
1503  const double our_power = power_projection(hex,get_dstsrc());
1504  const double their_power = power_projection(hex,get_enemy_dstsrc()) * static_cast<double>(defense)/100.0;
1505  const double rating = our_power - their_power;
1506  if(rating > best_rating) {
1507  best_pos = hex;
1508  best_rating = rating;
1509  }
1510 
1511  // Give a bonus for getting to a village.
1512  const int modified_defense = defense - (resources::gameboard->map().is_village(hex) ? 10 : 0);
1513 
1514  if(modified_defense < best_defensive_rating) {
1515  best_defensive_rating = modified_defense;
1516  best_defensive = hex;
1517  }
1518 
1519  ++itors.first;
1520  }
1521 
1522  // If the unit is in range of its leader, it should
1523  // never retreat -- it has to defend the leader instead.
1524  if(can_reach_leader) {
1525  continue;
1526  }
1527 
1528  if(!best_pos.valid()) {
1529  best_pos = best_defensive;
1530  }
1531 
1532  if(best_pos.valid()) {
1533  move_ = check_move_action(i->get_location(), best_pos, true);
1534  if (move_->is_ok()) {
1535  return get_score();
1536  }
1537  }
1538  }
1539  }
1540  }
1541 
1542  return BAD_SCORE;
1543 }
1544 
1546 {
1547  move_->execute();
1548  if (!move_->is_ok()){
1549  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok" << std::endl;
1550  }
1551 }
1552 
1553 
1554 
1555 bool retreat_phase::should_retreat(const map_location& loc, const unit_map::const_iterator& un, const move_map &srcdst, const move_map &dstsrc, double caution)
1556 {
1557  const move_map &enemy_dstsrc = get_enemy_dstsrc();
1558 
1559  if(caution <= 0.0) {
1560  return false;
1561  }
1562 
1563  double optimal_terrain = best_defensive_position(un->get_location(), dstsrc,
1564  srcdst, enemy_dstsrc).chance_to_hit/100.0;
1565  const double proposed_terrain =
1566  un->defense_modifier(resources::gameboard->map().get_terrain(loc)) / 100.0;
1567 
1568  // The 'exposure' is the additional % chance to hit
1569  // this unit receives from being on a sub-optimal defensive terrain.
1570  const double exposure = proposed_terrain - optimal_terrain;
1571 
1572  const double our_power = power_projection(loc,dstsrc);
1573  const double their_power = power_projection(loc,enemy_dstsrc);
1574  return caution*their_power*(1.0+exposure) > our_power;
1575 }
1576 
1577 
1578 //==============================================================
1579 
1581  : candidate_action(context,cfg)
1582 {
1583 }
1584 
1585 
1587 {
1588 }
1589 
1591 {
1592  ERR_AI_TESTING_AI_DEFAULT << get_name() << ": evaluate - not yet implemented" << std::endl;
1593  return BAD_SCORE;
1594 }
1595 
1596 
1597 
1599 {
1600  ERR_AI_TESTING_AI_DEFAULT << get_name() << ": execute - not yet implemented" << std::endl;
1601 }
1602 
1603 //==============================================================
1604 
1606  :candidate_action(context, cfg)
1607 {
1608 }
1609 
1611 {
1612 }
1613 
1615 {
1617  return BAD_SCORE;
1618  }
1619  bool allied_leaders_available = false;
1620  for(team &tmp_team : resources::gameboard->teams()) {
1621  if(!current_team().is_enemy(tmp_team.side())){
1622  std::vector<unit_map::unit_iterator> allied_leaders = resources::gameboard->units().find_leaders(get_side());
1623  if (!allied_leaders.empty()){
1624  allied_leaders_available = true;
1625  break;
1626  }
1627  }
1628  }
1629  if(allied_leaders_available){
1630  return get_score();
1631  }
1632  return BAD_SCORE;
1633 }
1634 
1636 {
1637  //get all AI leaders
1638  std::vector<unit_map::unit_iterator> ai_leaders = resources::gameboard->units().find_leaders(get_side());
1639 
1640  //calculate all possible moves (AI + allies)
1641  typedef std::map<map_location, pathfind::paths> path_map;
1642  path_map possible_moves;
1643  move_map friends_srcdst, friends_dstsrc;
1644  calculate_moves(resources::gameboard->units(), possible_moves, friends_srcdst, friends_dstsrc, false, true);
1645 
1646  //check for each ai leader if he should move away from his keep
1647  for (unit_map::unit_iterator &ai_leader : ai_leaders) {
1648  if(!ai_leader.valid() || !is_allowed_unit(*ai_leader)) {
1649  //This can happen if wml killed or moved a leader during a movement events of another leader
1650  continue;
1651  }
1652  //only if leader is on a keep
1653  const map_location &keep = ai_leader->get_location();
1654  if ( !resources::gameboard->map().is_keep(keep) ) {
1655  continue;
1656  }
1657  map_location recruit_loc = pathfind::find_vacant_castle(*ai_leader);
1658  if(!resources::gameboard->map().on_board(recruit_loc)){
1659  continue;
1660  }
1661  bool friend_can_reach_keep = false;
1662 
1663  //for each leader, check if he's allied and can reach our keep
1664  for(path_map::const_iterator i = possible_moves.begin(); i != possible_moves.end(); ++i){
1665  const unit_map::const_iterator itor = resources::gameboard->units().find(i->first);
1666  assert(itor.valid());
1667  team &leader_team = resources::gameboard->get_team(itor->side());
1668  if(itor != resources::gameboard->units().end() && itor->can_recruit() && itor->side() != get_side() && (leader_team.total_income() + leader_team.gold() > leader_team.minimum_recruit_price())){
1669  pathfind::paths::dest_vect::const_iterator tokeep = i->second.destinations.find(keep);
1670  if(tokeep != i->second.destinations.end()){
1671  friend_can_reach_keep = true;
1672  break;
1673  }
1674  }
1675  }
1676  //if there's no allied leader who can reach the keep, check next ai leader
1677  if(friend_can_reach_keep){
1678  //determine the best place the ai leader can move to
1679  map_location best_move;
1680  int defense_modifier = 100;
1681  for(pathfind::paths::dest_vect::const_iterator i = possible_moves[keep].destinations.begin()
1682  ; i != possible_moves[keep].destinations.end()
1683  ; ++i){
1684 
1685  //calculate_moves() above uses max. moves -> need to check movement_left of leader here
1686  if(distance_between(i->curr, keep) <= 3
1687  && static_cast<int>(distance_between(i->curr, keep)) <= ai_leader->movement_left()){
1688 
1689  int tmp_def_mod = ai_leader->defense_modifier(resources::gameboard->map().get_terrain(i->curr));
1690  if(tmp_def_mod < defense_modifier){
1691  defense_modifier = tmp_def_mod;
1692  best_move = i->curr;
1693  }
1694  }
1695  }
1696  //only move if there's a place with a good defense
1697  if(defense_modifier < 100){
1698  move_result_ptr move = check_move_action(keep, best_move, true);
1699  if(move->is_ok()){
1700  move->execute();
1701  if (!move->is_ok()){
1702  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok" << std::endl;
1703  }else{
1704  ai_leader->set_goto(keep);
1705  }
1706  // This is needed for sides with multiple leaders, in case a WML event does something
1707  // or to account for a leader having previously been moved by this CA execution
1708  possible_moves.clear();
1709  calculate_moves(resources::gameboard->units(), possible_moves, friends_srcdst, friends_dstsrc, false, true);
1710  }else{
1711  LOG_AI_TESTING_AI_DEFAULT << get_name() << "::execute not ok" << std::endl;
1712  }
1713  }
1714  }
1715  ai_leader->remove_movement_ai();
1716  }
1717  //ERR_AI_TESTING_AI_DEFAULT << get_name() << ": evaluate - not yet implemented" << std::endl;
1718 }
1719 
1720 
1721 //==============================================================
1722 
1723 
1724 } //end of namespace testing_ai_default
1725 
1726 } //end of namespace ai
void remove()
Removes a tip.
Definition: tooltip.cpp:189
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:242
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:372
virtual attack_result_ptr check_attack_action(const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon) override
Definition: contexts.hpp:539
int h() const
Effective map height, in hexes.
Definition: map.hpp:128
std::vector< unit_iterator > find_leaders(int side)
Definition: map.cpp:357
unit_iterator end()
Definition: map.hpp:415
move_result_ptr move_
Definition: ca.hpp:44
virtual const attacks_vector & get_attacks() const override
Definition: contexts.hpp:657
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:92
std::vector< std::pair< map_location, map_location > > tmoves
Definition: ca.hpp:133
std::shared_ptr< stopunit_result > stopunit_result_ptr
Definition: game_info.hpp:86
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:114
virtual const map_location & nearest_keep(const map_location &loc) const override
Definition: contexts.hpp:919
This class represents a single unit of a specific type.
Definition: unit.hpp:99
void dispatch(treachmap &reachmap, tmoves &moves)
Dispatches all units to their best location.
Definition: ca.cpp:798
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit&#39;s movement cost on a particular terrain.
Definition: unit.hpp:1396
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:119
goto_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:55
static manager & get_singleton()
Definition: manager.hpp:151
map_location best_leader_loc_
The best possible location for our leader if it can&#39;t reach a village.
Definition: ca.hpp:124
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
Definition: unit.cpp:1343
virtual stopunit_result_ptr check_stopunit_action(const map_location &unit_location, bool remove_movement=true, bool remove_attacks=false) override
Definition: contexts.hpp:563
unit_iterator find_leader(int side)
Definition: map.cpp:329
void dump_reachmap(treachmap &reachmap)
Shows which villages every unit can reach (debug function).
Definition: ca.cpp:1309
Managing the AI-Game interaction - AI actions and their results.
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:466
leader_shares_keep_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1605
map_location keep_loc_
Location of the keep the closest to our leader.
Definition: ca.hpp:118
virtual const move_map & get_srcdst() const override
Definition: contexts.hpp:827
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:166
std::shared_ptr< move_result > move_result_ptr
Definition: game_info.hpp:84
map_location target
Definition: contexts.hpp:85
virtual const gamemap & map() const override
Definition: game_board.hpp:109
dest_vect destinations
Definition: pathfind.hpp:99
unit_iterator begin()
Definition: map.hpp:405
The unit is poisoned - it loses health each turn.
Definition: unit.hpp:832
Composite AI stages.
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1346
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:408
AI Support engine - creating specific ai components from config.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:377
-file sdl_utils.hpp
std::shared_ptr< attack_result > attack_result_ptr
Definition: game_info.hpp:81
move_leader_to_keep_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:397
retreat_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1413
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:562
#define ERR_AI_TESTING_AI_DEFAULT
Definition: ca.cpp:46
Default AI (Testing)
bool remove_village(treachmap &reachmap, tmoves &moves, const map_location &village)
Removes a village for all units, returns true if anything is deleted.
Definition: ca.cpp:954
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1635
game_data * gamedata
Definition: resources.cpp:22
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1422
t_translation::terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition: map.cpp:309
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:42
std::map< map_location, pathfind::paths > moves_map
The standard way in which a map of possible movement routes to location is recorded.
Definition: game_info.hpp:45
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit&#39;s defense on a given terrain.
Definition: unit.cpp:1637
const config & options()
Definition: game.cpp:593
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
static lg::log_domain log_ai_testing_ai_default("ai/ca/testing_ai_default")
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
team & get_team(int i)
Definition: game_board.hpp:104
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:280
std::vector< map_location > steps
Definition: pathfind.hpp:134
bool should_retreat(const map_location &loc, const unit_map::const_iterator &un, const move_map &srcdst, const move_map &dstsrc, double caution)
Definition: ca.cpp:1555
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:538
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
void dispatch_complex(treachmap &reachmap, tmoves &moves, const std::size_t village_count)
Dispatches the units to a village after the simple dispatching failed.
Definition: ca.cpp:987
map_location curr
Definition: pathfind.hpp:87
const defensive_position & best_defensive_position(const map_location &unit, const move_map &dstsrc, const move_map &srcdst, const move_map &enemy_dstsrc) const override
Definition: contexts.hpp:607
bool is_allowed_unit(const unit &u) const
Flag indicating whether unit may be used by this candidate action.
Definition: rca.cpp:93
bool valid() const
Definition: location.hpp:93
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1590
virtual double get_leader_aggression() const override
Definition: contexts.hpp:742
game_board * gameboard
Definition: resources.cpp:20
boost::dynamic_bitset<> dynamic_bitset
virtual bool get_passive_leader() const override
Definition: contexts.hpp:767
Encapsulates the map of the game.
Definition: map.hpp:36
virtual bool get_passive_leader_shares_keep() const override
Definition: contexts.hpp:773
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:1614
virtual const move_map & get_enemy_dstsrc() const override
Definition: contexts.hpp:688
leader_control_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1580
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
virtual const std::vector< std::string > get_recruitment_pattern() const override
Definition: contexts.hpp:809
std::size_t count(const map_location &loc) const
Definition: map.hpp:400
void modify_active_ai_for_side(ai::side_number side, const config &cfg)
Modifies AI parameters for active AI of the given side.
Definition: manager.cpp:662
std::array< map_location, 6 > adjacent_loc_array_t
Definition: location.hpp:170
logger & debug()
Definition: log.cpp:96
bool dont_log(const log_domain &domain) const
Definition: log.hpp:123
int move_cost
Movement cost for reaching the end of the route.
Definition: pathfind.hpp:136
#define DBG_AI_TESTING_AI_DEFAULT
Definition: ca.cpp:43
bool dispatch_village_simple(treachmap &reachmap, tmoves &moves, std::size_t &village_count)
Definition: ca.cpp:902
Encapsulates the map of the game.
Definition: location.hpp:42
map_location leader_loc_
Locaton of our leader.
Definition: ca.hpp:121
unit_iterator find(std::size_t id)
Definition: map.cpp:311
get_healing_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:1337
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:144
bool debug_
debug log level for AI enabled?
Definition: ca.hpp:127
int w() const
Effective map width, in hexes.
Definition: map.hpp:125
virtual const map_location & suitable_keep(const map_location &leader_location, const pathfind::paths &leader_paths) const override
get most suitable keep for leader - nearest free that can be reached in 1 turn, if none - return near...
Definition: contexts.hpp:956
std::size_t i
Definition: function.cpp:933
bool dispatch_unit_simple(treachmap &reachmap, tmoves &moves)
Dispatches all units who can reach one village.
Definition: ca.cpp:858
virtual const team & current_team() const override
Definition: contexts.hpp:524
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:472
double g
Definition: astarsearch.cpp:64
std::vector< std::pair< map_location, map_location > > movements
Definition: contexts.hpp:86
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:583
virtual double power_projection(const map_location &loc, const move_map &dstsrc) const override
Function which finds how much &#39;power&#39; a side can attack a certain location with.
Definition: contexts.hpp:785
std::string id_
Definition: rca.hpp:144
#define LOG_AI_TESTING_AI_DEFAULT
Definition: ca.cpp:44
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:574
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:465
virtual move_result_ptr check_move_action(const map_location &from, const map_location &to, bool remove_movement=true, bool unreach_is_ok=false) override
Definition: contexts.hpp:544
std::map< map_location, std::vector< map_location > > treachmap
Definition: ca.hpp:130
virtual bool get_leader_ignores_keep() const override
Definition: contexts.hpp:755
virtual std::string get_name() const
Get the name of the candidate action (useful for debug purposes)
Definition: rca.hpp:99
void get_villages(const move_map &dstsrc, const move_map &enemy_dstsrc, unit_map::const_iterator &leader)
Definition: ca.cpp:619
#define next(ls)
Definition: llex.cpp:32
void full_dispatch(treachmap &reachmap, tmoves &moves)
Dispatches all units to a village, every unit can reach every village.
Definition: ca.cpp:1299
treachmap::iterator remove_unit(treachmap &reachmap, tmoves &moves, treachmap::iterator unit)
Removes a unit which can&#39;t reach any village anymore.
Definition: ca.cpp:971
bool is_village(const map_location &loc) const
Definition: map.cpp:65
virtual const move_map & get_dstsrc() const override
Definition: contexts.hpp:682
std::size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
Definition: location.cpp:557
get_villages_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:548
virtual double get_aggression() const override
Definition: contexts.hpp:626
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1598
virtual void calculate_possible_moves(std::map< map_location, pathfind::paths > &possible_moves, move_map &srcdst, move_map &dstsrc, bool enemy, bool assume_full_movement=false, const terrain_filter *remove_destinations=nullptr) const override
Definition: contexts.hpp:573
bool find(E event, F functor)
Tests whether an event handler is available.
map_location prev
Definition: astarsearch.cpp:65
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1313
bool contains(const map_location &) const
Definition: pathfind.cpp:520
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:260
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:70
static const map_location & null_location()
Definition: location.hpp:85
static const double BAD_SCORE
Definition: rca.hpp:32
Container associating units to locations.
Definition: map.hpp:99
virtual void calculate_moves(const unit_map &units, std::map< map_location, pathfind::paths > &possible_moves, move_map &srcdst, move_map &dstsrc, bool enemy, bool assume_full_movement=false, const terrain_filter *remove_destinations=nullptr, bool see_all=false) const override
Definition: contexts.hpp:581
int side() const
The side this unit belongs to.
Definition: unit.hpp:304
virtual const terrain_filter & get_avoid() const override
Definition: contexts.hpp:670
virtual double evaluate()
Evaluate the candidate action, resetting the internal state of the action.
Definition: ca.cpp:65
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1402
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
virtual move_result_ptr execute_move_action(const map_location &from, const map_location &to, bool remove_movement=true, bool unreach_is_ok=false) override
Definition: contexts.hpp:1006
combat_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:157
bool valid() const
Definition: map.hpp:276
virtual double get_caution() const override
Definition: contexts.hpp:676
static map_location::DIRECTION n
double get_score() const
Get the usual score of the candidate action without re-evaluation.
Definition: rca.cpp:77
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:133
virtual void execute()
Execute the candidate action.
Definition: ca.cpp:1545
#define WRN_AI_TESTING_AI_DEFAULT
Definition: ca.cpp:45
This module contains various pathfinding functions and utilities.
void remove_goal(const std::string &id)
Definition: ca.cpp:386
virtual const moves_map & get_possible_moves() const override
Definition: contexts.hpp:779
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
virtual config get_leader_goal() const override
Definition: contexts.hpp:749
bool empty() const
Definition: config.cpp:884
void find_villages(treachmap &reachmap, tmoves &moves, const std::multimap< map_location, map_location > &dstsrc, const std::multimap< map_location, map_location > &enemy_dstsrc)
Definition: ca.cpp:679
move_leader_to_goals_phase(rca_context &context, const config &cfg)
Definition: ca.cpp:271
candidate action framework
attack_analysis best_analysis_
Definition: ca.hpp:60