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