The Battle for Wesnoth  1.15.0-dev
contexts.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  * Helper functions for the object which operates in the context of AI for specific side
17  * This is part of AI interface
18  * @file
19  */
20 
21 #include "ai/contexts.hpp"
22 
23 #include "actions/attack.hpp"
24 
25 #include "ai/actions.hpp" // for actions
26 #include "ai/composite/aspect.hpp" // for typesafe_aspect, aspect, etc
27 #include "ai/composite/engine.hpp" // for engine, engine_factory, etc
28 #include "ai/composite/goal.hpp" // for goal
29 #include "ai/composite/stage.hpp" // for ministage
30 #include "ai/game_info.hpp" // for typesafe_aspect_ptr, etc
32 #include "ai/manager.hpp" // for manager
33 
34 #include "chat_events.hpp" // for chat_handler, etc
35 #include "config.hpp" // for config, etc
36 #include "display_chat_manager.hpp"
37 #include "game_board.hpp" // for game_board
38 #include "game_config.hpp" // for debug
39 #include "game_display.hpp" // for game_display
40 #include "log.hpp" // for LOG_STREAM, logger, etc
41 #include "map/map.hpp" // for gamemap
42 #include "pathfind/pathfind.hpp" // for paths::dest_vect, paths, etc
43 #include "resources.hpp" // for units, gameboard, etc
44 #include "serialization/string_utils.hpp" // for split, etc
45 #include "team.hpp" // for team
46 #include "terrain/filter.hpp" // for terrain_filter
47 #include "terrain/translation.hpp" // for terrain_code
48 #include "time_of_day.hpp" // for time_of_day
49 #include "tod_manager.hpp" // for tod_manager
50 #include "units/unit.hpp" // for unit
51 #include "units/map.hpp" // for unit_map::iterator_base, etc
52 #include "units/ptr.hpp" // for unit_ptr
53 #include "units/types.hpp" // for attack_type, unit_type, etc
54 #include "formula/variant.hpp" // for variant
55 
56 #include <algorithm> // for find, count, max, fill_n
57 #include <cmath> // for sqrt
58 #include <cstdlib> // for abs
59 #include <ctime> // for time
60 #include <iterator> // for back_inserter
61 #include <ostream> // for operator<<, basic_ostream, etc
62 
63 static lg::log_domain log_ai("ai/general");
64 #define DBG_AI LOG_STREAM(debug, log_ai)
65 #define LOG_AI LOG_STREAM(info, log_ai)
66 #define WRN_AI LOG_STREAM(warn, log_ai)
67 #define ERR_AI LOG_STREAM(err, log_ai)
68 
69 // =======================================================================
70 //
71 // =======================================================================
72 namespace ai {
73 
75 {
77 }
78 
79 
81 {
83 }
84 
85 
87 {
89 }
90 
91 
93 {
95 }
96 
97 
99 {
101 }
102 
103 
105 {
107 }
108 
109 attack_result_ptr readwrite_context_impl::execute_attack_action(const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon){
111  double m_aggression = i.valid() && i->can_recruit() ? get_leader_aggression() : get_aggression();
112  const unit_advancements_aspect& m_advancements = get_advancements();
113  return actions::execute_attack_action(get_side(),true,attacker_loc,defender_loc,attacker_weapon, m_aggression, m_advancements);
114 }
115 
116 
117 attack_result_ptr readonly_context_impl::check_attack_action(const map_location& attacker_loc, const map_location& defender_loc, int attacker_weapon){
119  double m_aggression = i.valid() && i->can_recruit() ? get_leader_aggression() : get_aggression();
120  const unit_advancements_aspect& m_advancements = get_advancements();
121  return actions::execute_attack_action(get_side(),false,attacker_loc,defender_loc,attacker_weapon, m_aggression, m_advancements);
122 }
123 
124 
125 move_result_ptr readwrite_context_impl::execute_move_action(const map_location& from, const map_location& to, bool remove_movement, bool unreach_is_ok){
126  return actions::execute_move_action(get_side(),true,from,to,remove_movement,unreach_is_ok);
127 }
128 
129 
130 move_result_ptr readonly_context_impl::check_move_action(const map_location& from, const map_location& to, bool remove_movement, bool unreach_is_ok){
131  return actions::execute_move_action(get_side(),false,from,to,remove_movement,unreach_is_ok);
132 }
133 
134 
136  return actions::execute_recall_action(get_side(),true,id,where,from);
137 }
138 
139 
141  return actions::execute_recruit_action(get_side(),true,unit_name,where,from);
142 }
143 
144 
146  return actions::execute_recall_action(get_side(),false,id,where,from);
147 }
148 
149 
151  return actions::execute_recruit_action(get_side(),false,unit_name,where,from);
152 }
153 
154 
155 stopunit_result_ptr readwrite_context_impl::execute_stopunit_action(const map_location& unit_location, bool remove_movement, bool remove_attacks){
156  return actions::execute_stopunit_action(get_side(),true,unit_location,remove_movement,remove_attacks);
157 }
158 
159 
160 stopunit_result_ptr readonly_context_impl::check_stopunit_action(const map_location& unit_location, bool remove_movement, bool remove_attacks){
161  return actions::execute_stopunit_action(get_side(),false,unit_location,remove_movement,remove_attacks);
162 }
163 
164 
166  return actions::execute_synced_command_action(get_side(),true,lua_code,location);
167 }
168 
169 
171  return actions::execute_synced_command_action(get_side(),false,lua_code,location);
172 }
173 
174 template<typename T>
176 {
177  known_aspects_.emplace(name, std::make_shared<typesafe_known_aspect<T>>(name, where, aspects_));
178 }
179 
181  : cfg_(cfg),
182  engines_(),
183  known_aspects_(),
184  advancements_(),
185  aggression_(),
186  aspects_(),
187  attacks_(),
188  avoid_(),
189  caution_(),
190  defensive_position_cache_(),
191  dstsrc_(),enemy_dstsrc_(),
192  enemy_possible_moves_(),
193  enemy_srcdst_(),
194  grouping_(),
195  goals_(),
196  keeps_(),
197  leader_aggression_(),
198  leader_goal_(),
199  leader_ignores_keep_(),
200  leader_value_(),
201  move_maps_enemy_valid_(false),
202  move_maps_valid_(false),
203  dst_src_valid_lua_(false),
204  dst_src_enemy_valid_lua_(false),
205  src_dst_valid_lua_(false),
206  src_dst_enemy_valid_lua_(false),
207  passive_leader_(),
208  passive_leader_shares_keep_(),
209  possible_moves_(),
210  recruitment_diversity_(),
211  recruitment_instructions_(),
212  recruitment_more_(),
213  recruitment_pattern_(),
214  recruitment_randomness_(),
215  recruitment_save_gold_(),
217  scout_village_targeting_(),
218  simple_targeting_(),
219  srcdst_(),
220  support_villages_(),
221  unit_stats_cache_(),
222  village_value_(),
223  villages_per_scout_()
224  {
225  init_side_context_proxy(context);
227 
228  add_known_aspect("advancements", advancements_);
229  add_known_aspect("aggression",aggression_);
230  add_known_aspect("attacks",attacks_);
231  add_known_aspect("avoid",avoid_);
232  add_known_aspect("caution",caution_);
233  add_known_aspect("grouping",grouping_);
234  add_known_aspect("leader_aggression",leader_aggression_);
235  add_known_aspect("leader_goal",leader_goal_);
236  add_known_aspect("leader_ignores_keep",leader_ignores_keep_);
237  add_known_aspect("leader_value",leader_value_);
238  add_known_aspect("passive_leader",passive_leader_);
239  add_known_aspect("passive_leader_shares_keep",passive_leader_shares_keep_);
240  add_known_aspect("recruitment_diversity",recruitment_diversity_);
241  add_known_aspect("recruitment_instructions",recruitment_instructions_);
242  add_known_aspect("recruitment_more",recruitment_more_);
243  add_known_aspect("recruitment_pattern",recruitment_pattern_);
244  add_known_aspect("recruitment_randomness",recruitment_randomness_);
245  add_known_aspect("recruitment_save_gold",recruitment_save_gold_);
246  add_known_aspect("scout_village_targeting",scout_village_targeting_);
247  add_known_aspect("simple_targeting",simple_targeting_);
248  add_known_aspect("support_villages",support_villages_);
249  add_known_aspect("village_value",village_value_);
250  add_known_aspect("villages_per_scout",villages_per_scout_);
252 
253  }
254 
256  //init the composite ai engines
257  for(const config &cfg_element : cfg_.child_range("engine")) {
258  engine::parse_engine_from_config(*this,cfg_element,std::back_inserter(engines_));
259  }
260 
261  // init the composite ai aspects
262  for(const config &cfg_element : cfg_.child_range("aspect")) {
263  std::vector<aspect_ptr> aspects;
264  engine::parse_aspect_from_config(*this,cfg_element,cfg_element["id"],std::back_inserter(aspects));
265  add_aspects(aspects);
266  }
267 
268  // init the composite ai goals
269  for(const config &cfg_element : cfg_.child_range("goal")) {
270  engine::parse_goal_from_config(*this,cfg_element,std::back_inserter(get_goals()));
271  }
272 }
273 
274 
276 {
277  return config();
278 }
279 
281 {
282  return config();
283 }
284 
285 
287 {
288  config cfg;
289  for(const engine_ptr e : engines_) {
290  cfg.add_child("engine",e->to_config());
291  }
292  for(const aspect_map::value_type a : aspects_) {
293  cfg.add_child("aspect",a.second->to_config());
294  }
295  for(const goal_ptr g : goals_) {
296  cfg.add_child("goal",g->to_config());
297  }
298  return cfg;
299 }
300 
302 {
304 }
305 
306 void readonly_context_impl::handle_generic_event(const std::string& /*event_name*/)
307 {
309 }
310 
311 
314 }
315 
316 
319 }
320 
321 void readonly_context_impl::diagnostic(const std::string& msg)
322 {
323  if(game_config::debug) {
325  }
326 }
327 
328 
330 {
332 }
333 
334 
335 void readonly_context_impl::log_message(const std::string& msg)
336 {
337  if(game_config::debug) {
339  std::time(nullptr), "ai", get_side(), msg, events::chat_handler::MESSAGE_PUBLIC, false);
340  }
341 }
342 
343 
344 void readonly_context_impl::calculate_possible_moves(std::map<map_location,pathfind::paths>& res, move_map& srcdst,
345  move_map& dstsrc, bool enemy, bool assume_full_movement,
346  const terrain_filter* remove_destinations) const
347 {
348  calculate_moves(resources::gameboard->units(),res,srcdst,dstsrc,enemy,assume_full_movement,remove_destinations);
349 }
350 
351 void readonly_context_impl::calculate_moves(const unit_map& units, std::map<map_location,pathfind::paths>& res, move_map& srcdst,
352  move_map& dstsrc, bool enemy, bool assume_full_movement,
353  const terrain_filter* remove_destinations,
354  bool see_all
355  ) const
356 {
357 
358  for(unit_map::const_iterator un_it = units.begin(); un_it != units.end(); ++un_it) {
359  // If we are looking for the movement of enemies, then this unit must be an enemy unit.
360  // If we are looking for movement of our own units, it must be on our side.
361  // If we are assuming full movement, then it may be a unit on our side, or allied.
362  if ((enemy && current_team().is_enemy(un_it->side()) == false) ||
363  (!enemy && !assume_full_movement && un_it->side() != get_side()) ||
364  (!enemy && assume_full_movement && current_team().is_enemy(un_it->side()))) {
365  continue;
366  }
367  // Discount incapacitated units
368  if (un_it->incapacitated() ||
369  (!assume_full_movement && un_it->movement_left() == 0)) {
370  continue;
371  }
372 
373  // We can't see where invisible enemy units might move.
374  if (enemy && un_it->invisible(un_it->get_location()) && !see_all) {
375  continue;
376  }
377  // If it's an enemy unit, reset its moves while we do the calculations.
378  const unit_movement_resetter move_resetter(*un_it,enemy || assume_full_movement);
379 
380  // Insert the trivial moves of staying on the same map location.
381  if (un_it->movement_left() > 0) {
382  std::pair<map_location,map_location> trivial_mv(un_it->get_location(), un_it->get_location());
383  srcdst.insert(trivial_mv);
384  dstsrc.insert(trivial_mv);
385  }
386  /**
387  * @todo This is where support for a speculative unit map is incomplete.
388  * There are several places (deep) within the paths constructor
389  * where resources::gameboard->units() is assumed to be the unit map. Rather
390  * than introduce a new parameter to numerous functions, a better
391  * solution may be for the creator of the speculative map (if one
392  * is used in the future) to cause resources::gameboard->units() to point to
393  * that map (and restore the "real" pointer when the speculating
394  * is completed). If that approach is adopted, calculate_moves()
395  * and calculate_possible_moves() become redundant, and one of
396  * them should probably be eliminated.
397  */
398  res.emplace(un_it->get_location(), pathfind::paths(*un_it, false, true, current_team(), 0, see_all));
399  }
400 
401  // deactivate terrain filtering if it's just the dummy 'matches nothing'
402  static const config only_not_tag("not");
403  if(remove_destinations && remove_destinations->to_config() == only_not_tag) {
404  remove_destinations = nullptr;
405  }
406 
407  for(std::map<map_location,pathfind::paths>::iterator m = res.begin(); m != res.end(); ++m) {
408  for(const pathfind::paths::step &dest : m->second.destinations)
409  {
410  const map_location& src = m->first;
411  const map_location& dst = dest.curr;
412 
413  if(remove_destinations != nullptr && remove_destinations->match(dst)) {
414  continue;
415  }
416 
417  bool friend_owns = false;
418 
419  // Don't take friendly villages
420  if(!enemy && resources::gameboard->map().is_village(dst)) {
421  for(std::size_t n = 0; n != resources::gameboard->teams().size(); ++n) {
422  if(resources::gameboard->teams()[n].owns_village(dst)) {
423  int side = n + 1;
424  if (get_side() != side && !current_team().is_enemy(side)) {
425  friend_owns = true;
426  }
427 
428  break;
429  }
430  }
431  }
432 
433  if(friend_owns) {
434  continue;
435  }
436 
437  if(src != dst && (resources::gameboard->find_visible_unit(dst, current_team()) == resources::gameboard->units().end()) ) {
438  srcdst.emplace(src, dst);
439  dstsrc.emplace(dst, src);
440  }
441  }
442  }
443 }
444 
445 
446 void readonly_context_impl::add_aspects(std::vector< aspect_ptr > &aspects )
447 {
448  for(aspect_ptr a : aspects) {
449  const std::string id = a->get_id();
451  if (i != known_aspects_.end()) {
452  i->second->set(a);
453  } else {
454  ERR_AI << "when adding aspects, unknown aspect id["<<id<<"]"<<std::endl;
455  }
456  }
457 }
458 
459 void readonly_context_impl::add_facet(const std::string &id, const config &cfg) const
460 {
461  known_aspect_map::const_iterator i = known_aspects_.find(id);
462  if (i != known_aspects_.end()) {
463  i->second->add_facet(cfg);
464  } else {
465  ERR_AI << "when adding aspects, unknown aspect id["<<id<<"]"<<std::endl;
466  }
467 }
468 
470  const move_map& dstsrc, const move_map& srcdst, const move_map& enemy_dstsrc) const
471 {
473  if(itor == resources::gameboard->units().end()) {
474  static defensive_position pos;
475  pos.chance_to_hit = 0;
476  pos.vulnerability = pos.support = 0;
477  return pos;
478  }
479 
480  const std::map<map_location,defensive_position>::const_iterator position =
481  defensive_position_cache_.find(loc);
482 
483  if(position != defensive_position_cache_.end()) {
484  return position->second;
485  }
486 
487  defensive_position pos;
488  pos.chance_to_hit = 100;
489  pos.vulnerability = 10000.0;
490  pos.support = 0.0;
491 
492  typedef move_map::const_iterator Itor;
493  const std::pair<Itor,Itor> itors = srcdst.equal_range(loc);
494  for(Itor i = itors.first; i != itors.second; ++i) {
495  const int defense = itor->defense_modifier(resources::gameboard->map().get_terrain(i->second));
496  if(defense > pos.chance_to_hit) {
497  continue;
498  }
499 
500  const double vulnerability = power_projection(i->second,enemy_dstsrc);
501  const double support = power_projection(i->second,dstsrc);
502 
503  if(defense < pos.chance_to_hit || support - vulnerability > pos.support - pos.vulnerability) {
504  pos.loc = i->second;
505  pos.chance_to_hit = defense;
506  pos.vulnerability = vulnerability;
507  pos.support = support;
508  }
509  }
510 
511  defensive_position_cache_.emplace(loc, pos);
512  return defensive_position_cache_[loc];
513 }
514 
515 
516 std::map<map_location,defensive_position>& readonly_context_impl::defensive_position_cache() const
517 {
519 }
520 
521 
523 {
524  if (advancements_) {
525  return advancements_->get();
526  }
527 
529  return uaa;
530 }
531 
532 
534 {
535  if (aggression_) {
536  return aggression_->get();
537  }
538  return 0;
539 }
540 
541 
543 {
544  return aspects_;
545 }
546 
547 
549 {
550  return aspects_;
551 }
552 
553 
555 {
556  if (attacks_) {
557  return attacks_->get();
558  }
559  static attacks_vector av;
560  return av;
561 }
562 
563 
565 {
566  if (attacks_) {
567  return attacks_->get_variant();
568  }
569  static wfl::variant v;///@todo 1.9: replace with variant::null_variant;
570  return v;
571 }
572 
573 const terrain_filter& readonly_context_impl::get_avoid() const
574 {
575  if (avoid_) {
576  return avoid_->get();
577  }
578  config cfg;
579  cfg.add_child("not");
580  static terrain_filter tf(vconfig(cfg, true), resources::filter_con);
581  return tf;
582 }
583 
584 
586 {
587  if (caution_) {
588  return caution_->get();
589  }
590  return 0;
591 }
592 
594 {
595  if (!move_maps_valid_) {
597  }
598  return dstsrc_;
599 }
600 
601 
603 {
604  if (!move_maps_enemy_valid_) {
606  }
607  return enemy_dstsrc_;
608 }
609 
610 
612 {
613  if (!move_maps_enemy_valid_) {
615  }
616  return enemy_possible_moves_;
617 }
618 
619 
621 {
622  if (!move_maps_enemy_valid_) {
624  }
625  return enemy_srcdst_;
626 }
627 
628 
630 {
631  std::string engine_name = cfg["engine"];
632  if (engine_name.empty()) {
633  engine_name="cpp";//default engine
634  }
635 
637  while (en!=engines_.end() && ((*en)->get_name()!=engine_name) && ((*en)->get_id()!=engine_name)) {
638  ++en;
639  }
640 
641  if (en != engines_.end()){
642  return *en;
643  }
644 
645  //TODO: fix, removing some code duplication
647  if (eng == engine_factory::get_list().end()){
648  ERR_AI << "side "<<get_side()<<" : UNABLE TO FIND engine["<<
649  engine_name <<"]" << std::endl;
650  DBG_AI << "config snippet contains: " << std::endl << cfg << std::endl;
651  return engine_ptr();
652  }
653 
654  engine_ptr new_engine = eng->second->get_new_instance(*this,engine_name);
655  if (!new_engine) {
656  ERR_AI << "side "<<get_side()<<" : UNABLE TO CREATE engine["<<
657  engine_name <<"] " << std::endl;
658  DBG_AI << "config snippet contains: " << std::endl << cfg << std::endl;
659  return engine_ptr();
660  }
661  engines_.push_back(new_engine);
662  return engines_.back();
663 }
664 
665 
666 const std::vector<engine_ptr>& readonly_context_impl::get_engines() const
667 {
668  return engines_;
669 }
670 
671 
672 std::vector<engine_ptr>& readonly_context_impl::get_engines()
673 {
674  return engines_;
675 }
676 
677 
679 {
680  if (grouping_) {
681  return grouping_->get();
682  }
683  return std::string();
684 }
685 
686 
687 const std::vector<goal_ptr>& readonly_context_impl::get_goals() const
688 {
689  return goals_;
690 }
691 
692 
693 std::vector<goal_ptr>& readonly_context_impl::get_goals()
694 {
695  return goals_;
696 }
697 
698 
699 
701 {
702  if (leader_aggression_) {
703  return leader_aggression_->get();
704  }
705  return 0;
706 }
707 
708 
710 {
711  if (leader_goal_) {
712  return leader_goal_->get();
713  }
714  return config();
715 }
716 
717 
719 {
720  if (leader_ignores_keep_) {
721  return leader_ignores_keep_->get();
722  }
723  return false;
724 }
725 
726 
728 {
729  if (leader_value_) {
730  return leader_value_->get();
731  }
732  return 0;
733 }
734 
735 
737 {
738  if (passive_leader_) {
739  return passive_leader_->get();
740  }
741  return false;
742 }
743 
744 
746 {
748  return passive_leader_shares_keep_->get();
749  }
750  return false;
751 }
752 
753 
755 {
756  if (!move_maps_valid_) {
758  }
759  return possible_moves_;
760 }
761 
762 
764 {
766  return recruitment_diversity_->get();
767  }
768  return 0.;
769 }
770 
771 
773 {
775  return recruitment_instructions_->get();
776  }
777  return config();
778 }
779 
780 
781 const std::vector<std::string> readonly_context_impl::get_recruitment_more() const
782 {
783  if (recruitment_more_) {
784  return recruitment_more_->get();
785  }
786  return std::vector<std::string>();
787 }
788 
789 
790 const std::vector<std::string> readonly_context_impl::get_recruitment_pattern() const
791 {
792  if (recruitment_pattern_) {
793  return recruitment_pattern_->get();
794  }
795  return std::vector<std::string>();
796 }
797 
798 
800 {
802  return recruitment_randomness_->get();
803  }
804  return 0;
805 }
806 
807 
809 {
811  return recruitment_save_gold_->get();
812  }
813  return config();
814 }
815 
816 
818 {
820  return scout_village_targeting_->get();
821  }
822  return 1;
823 }
824 
825 
827 {
828  if (simple_targeting_) {
829  return simple_targeting_->get();
830  }
831  return false;
832 }
833 
834 
836 {
837  if (!move_maps_valid_) {
839  }
840  return srcdst_;
841 }
842 
843 
845 {
846  if (support_villages_) {
847  return support_villages_->get();
848  }
849  return false;
850 }
851 
852 
854 {
855  if (village_value_) {
856  return village_value_->get();
857  }
858  return 0;
859 }
860 
861 
863 {
864  if (villages_per_scout_) {
865  return villages_per_scout_->get();
866  }
867  return 0;
868 }
869 
870 
872 {
873  return dst_src_valid_lua_;
874 }
875 
877 {
879 }
880 
882 {
883  return src_dst_valid_lua_;
884 }
885 
887 {
889 }
890 
892 {
894 }
895 
896 
898 {
899  keeps_.clear();
900 }
901 
902 
903 void keeps_cache::handle_generic_event(const std::string &/*event_name*/)
904 {
905  clear();
906 }
907 
908 
910 {
911  move_maps_valid_ = false;
912  move_maps_enemy_valid_ = false;
913 
914  dst_src_valid_lua_ = false;
915  dst_src_enemy_valid_lua_ = false;
916 
917  src_dst_valid_lua_ = false;
918  src_dst_enemy_valid_lua_ = false;
919 }
920 
921 
922 const std::set<map_location>& readonly_context_impl::keeps() const
923 {
924  return keeps_.get();
925 }
926 
927 
929  : map_(nullptr)
930  , keeps_()
931 {
934 }
935 
936 
938 {
941 }
942 
944 {
945  keeps_.clear();
946 }
947 
948 
949 void keeps_cache::init(const gamemap &map)
950 {
951  map_ = &map;
952 }
953 
954 const std::set<map_location>& keeps_cache::get()
955 {
956  if(keeps_.empty()) {
957  // Generate the list of keeps:
958  // iterate over the entire map and find all keeps.
959  for(int x = 0; x != map_->w(); ++x) {
960  for(int y = 0; y != map_->h(); ++y) {
961  const map_location loc(x,y);
962  if(map_->is_keep(loc)) {
964  get_adjacent_tiles(loc,adj.data());
965  for(std::size_t n = 0; n < adj.size(); ++n) {
966  if(map_->is_castle(adj[n])) {
967  keeps_.insert(loc);
968  break;
969  }
970  }
971  }
972  }
973  }
974  }
975 
976  return keeps_;
977 }
978 
979 
981 {
982  const unit_map::iterator leader = resources::gameboard->units().find_leader(get_side());
983  if(leader == resources::gameboard->units().end() || leader->incapacitated()) {
984  return false;
985  }
986 
987  const map_location &start_pos = nearest_keep(leader->get_location());
988  if(start_pos.valid() == false) {
989  return false;
990  }
991 
992  if (leader->get_location() == start_pos) {
993  return true;
994  }
995 
996  // Find where the leader can move
997  const pathfind::paths leader_paths(*leader, false, true, current_team());
998 
999  return leader_paths.destinations.contains(start_pos);
1000 }
1001 
1002 
1004 {
1005  std::set<map_location> avoided_locations;
1006  get_avoid().get_locations(avoided_locations);
1007  const std::set<map_location>& keeps = this->keeps();
1008  if(keeps.empty()) {
1009  static const map_location dummy;
1010  return dummy;
1011  }
1012 
1013  const map_location* res = nullptr;
1014  int closest = -1;
1015  for(std::set<map_location>::const_iterator i = keeps.begin(); i != keeps.end(); ++i) {
1016  if (avoided_locations.find(*i)!=avoided_locations.end()) {
1017  continue;
1018  }
1019  const int distance = distance_between(*i,loc);
1020  if(res == nullptr || distance < closest) {
1021  closest = distance;
1022  res = &*i;
1023  }
1024  }
1025  if (res) {
1026  return *res;
1027  } else {
1028  return map_location::null_location();
1029  }
1030 }
1031 
1032 
1033 double readonly_context_impl::power_projection(const map_location& loc, const move_map& dstsrc) const
1034 {
1035  map_location used_locs[6];
1036  int ratings[6];
1037  std::fill_n(ratings, 0, 6);
1038  int num_used_locs = 0;
1039 
1040  adjacent_loc_array_t locs;
1041  get_adjacent_tiles(loc,locs.data());
1042 
1043  const gamemap& map_ = resources::gameboard->map();
1044  unit_map& units_ = resources::gameboard->units();
1045 
1046  int res = 0;
1047 
1048  bool changed = false;
1049  for (int i = 0;; ++i) {
1050  if (i == 6) {
1051  if (!changed) break;
1052  // Loop once again, in case a unit found a better spot
1053  // and freed the place for another unit.
1054  changed = false;
1055  i = 0;
1056  }
1057 
1058  if (map_.on_board(locs[i]) == false) {
1059  continue;
1060  }
1061 
1062  const t_translation::terrain_code terrain = map_[locs[i]];
1063 
1064  typedef move_map::const_iterator Itor;
1065  typedef std::pair<Itor,Itor> Range;
1066  Range its = dstsrc.equal_range(locs[i]);
1067 
1068  map_location* const beg_used = used_locs;
1069  map_location* end_used = used_locs + num_used_locs;
1070 
1071  int best_rating = 0;
1072  map_location best_unit;
1073 
1074  for(Itor it = its.first; it != its.second; ++it) {
1075  const unit_map::const_iterator u = units_.find(it->second);
1076 
1077  // Unit might have been killed, and no longer exist
1078  if(u == units_.end()) {
1079  continue;
1080  }
1081 
1082  const unit& un = *u;
1083 
1084  // The unit might play on the next turn
1085  int attack_turn = resources::tod_manager->turn();
1086  if(un.side() < get_side()) {
1087  ++attack_turn;
1088  }
1089  // Considering the unit location would be too slow, we only apply the bonus granted by the global ToD
1090  const int lawful_bonus = resources::tod_manager->get_time_of_day(attack_turn).lawful_bonus;
1091  int tod_modifier = 0;
1092  if(un.alignment() == unit_type::ALIGNMENT::LAWFUL) {
1093  tod_modifier = lawful_bonus;
1094  } else if(un.alignment() == unit_type::ALIGNMENT::CHAOTIC) {
1095  tod_modifier = -lawful_bonus;
1096  } else if(un.alignment() == unit_type::ALIGNMENT::LIMINAL) {
1097  tod_modifier = -(std::abs(lawful_bonus));
1098  }
1099 
1100  // The 0.5 power avoids underestimating too much the damage of a wounded unit.
1101  int64_t hp = static_cast<int>(std::sqrt(static_cast<double>(un.hitpoints()) / un.max_hitpoints()) * 1000);
1102  int64_t most_damage = 0;
1103  for(const attack_type &att : un.attacks())
1104  {
1105  int damage = att.damage() * att.num_attacks() * (100 + tod_modifier);
1106  if (damage > most_damage) {
1107  most_damage = damage;
1108  }
1109  }
1110 
1111  int64_t village_bonus = map_.is_village(terrain) ? 3 : 2;
1112  int64_t defense = 100 - un.defense_modifier(terrain);
1113  int64_t rating_64 = hp * defense * most_damage * village_bonus / 200;
1114  int rating = rating_64;
1115  if(static_cast<int64_t>(rating) != rating_64) {
1116  WRN_AI << "overflow in ai attack calculation\n";
1117  }
1118  if(rating > best_rating) {
1119  map_location *pos = std::find(beg_used, end_used, it->second);
1120  // Check if the spot is the same or better than an older one.
1121  if (pos == end_used || rating >= ratings[pos - beg_used]) {
1122  best_rating = rating;
1123  best_unit = it->second;
1124  }
1125  }
1126  }
1127 
1128  if (!best_unit.valid()) continue;
1129  map_location *pos = std::find(beg_used, end_used, best_unit);
1130  int index = pos - beg_used;
1131  if (index == num_used_locs)
1132  ++num_used_locs;
1133  else if (best_rating == ratings[index])
1134  continue;
1135  else {
1136  // The unit was in another spot already, so remove its older rating
1137  // from the final result, and require a new run to fill its old spot.
1138  res -= ratings[index];
1139  changed = true;
1140  }
1141  used_locs[index] = best_unit;
1142  ratings[index] = best_rating;
1143  res += best_rating;
1144  }
1145 
1146  return res / 100000.;
1147 }
1148 
1150 {
1151  dstsrc_ = move_map();
1152  possible_moves_ = moves_map();
1153  srcdst_ = move_map();
1154  calculate_possible_moves(possible_moves_,srcdst_,dstsrc_,false,false,&get_avoid());
1155  if (get_passive_leader()||get_passive_leader_shares_keep()) {
1157  if (i.valid()) {
1158  map_location loc = i->get_location();
1159  srcdst_.erase(loc);
1160  for(move_map::iterator it = dstsrc_.begin(); it != dstsrc_.end(); ) {
1161  if(it->second == loc) {
1162  it = dstsrc_.erase(it);
1163  } else {
1164  ++it;
1165  }
1166  }
1167  ///@todo: shall possible moves be modified as well ?
1168  }
1169  }
1170  move_maps_valid_ = true;
1171 
1172  // invalidate lua cache
1173  dst_src_valid_lua_ = false;
1174  src_dst_valid_lua_ = false;
1175 }
1176 
1177 
1179 {
1180  enemy_dstsrc_ = move_map();
1181  enemy_srcdst_ = move_map();
1182  enemy_possible_moves_ = moves_map();
1183  calculate_possible_moves(enemy_possible_moves_,enemy_srcdst_,enemy_dstsrc_,true);
1184  move_maps_enemy_valid_ = true;
1185 
1186  // invalidate lua cache
1187  dst_src_enemy_valid_lua_ = false;
1188  src_dst_enemy_valid_lua_ = false;
1189 }
1190 
1192 {
1193  dst_src_valid_lua_ = true;
1194 }
1195 
1197 {
1198  dst_src_enemy_valid_lua_ = true;
1199 }
1200 
1202 {
1203  src_dst_valid_lua_ = true;
1204 }
1205 
1207 {
1208  src_dst_enemy_valid_lua_ = true;
1209 }
1210 
1211 const map_location& readonly_context_impl::suitable_keep(const map_location& leader_location, const pathfind::paths& leader_paths) const {
1212  if (resources::gameboard->map().is_keep(leader_location)) {
1213  return leader_location; //if leader already on keep, then return leader_location
1214  }
1215 
1216  map_location const* best_free_keep = &map_location::null_location();
1217  double move_left_at_best_free_keep = 0.0;
1218 
1219  map_location const* best_occupied_keep = &map_location::null_location();
1220  double move_left_at_best_occupied_keep = 0.0;
1221 
1222  for(const pathfind::paths::step &dest : leader_paths.destinations)
1223  {
1224  const map_location &loc = dest.curr;
1225  if (keeps().find(loc)!=keeps().end()){
1226 
1227  const int move_left_at_loc = dest.move_left;
1228  if (resources::gameboard->units().count(loc) == 0) {
1229  if ((*best_free_keep==map_location::null_location())||(move_left_at_loc>move_left_at_best_free_keep)){
1230  best_free_keep = &loc;
1231  move_left_at_best_free_keep = move_left_at_loc;
1232  }
1233  } else {
1234  if ((*best_occupied_keep==map_location::null_location())||(move_left_at_loc>move_left_at_best_occupied_keep)){
1235  best_occupied_keep = &loc;
1236  move_left_at_best_occupied_keep = move_left_at_loc;
1237  }
1238  }
1239  }
1240  }
1241 
1242  if (*best_free_keep != map_location::null_location()){
1243  return *best_free_keep; // if there is a free keep reachable during current turn, return it
1244  }
1245 
1246  if (*best_occupied_keep != map_location::null_location()){
1247  return *best_occupied_keep; // if there is an occupied keep reachable during current turn, return it
1248  }
1249 
1250  return nearest_keep(leader_location); // return nearest keep
1251 }
1252 
1253 
1254  /** Weapon choice cache, to speed simulations. */
1256 {
1257  return unit_stats_cache_;
1258 }
1259 
1260 
1261 bool readonly_context_impl::is_active(const std::string &time_of_day, const std::string &turns) const
1262 {
1263  if(time_of_day.empty() == false) {
1264  const std::vector<std::string>& times = utils::split(time_of_day);
1265  if(std::count(times.begin(),times.end(),resources::tod_manager->get_time_of_day().id) == 0) {
1266  return false;
1267  }
1268  }
1269 
1270  if(turns.empty() == false) {
1271  int turn = resources::tod_manager->turn();
1272  const std::vector<std::string>& turns_list = utils::split(turns);
1273  for(std::vector<std::string>::const_iterator j = turns_list.begin(); j != turns_list.end() ; ++j ) {
1274  const std::pair<int,int> range = utils::parse_range(*j);
1275  if(turn >= range.first && turn <= range.second) {
1276  return true;
1277  }
1278  }
1279  return false;
1280  }
1281  return true;
1282 }
1283 
1284 } //of namespace ai
static void parse_aspect_from_config(readonly_context &context, const config &cfg, const std::string &id, std::back_insert_iterator< std::vector< aspect_ptr >> b)
Definition: engine.cpp:51
typesafe_aspect_ptr< terrain_filter > avoid_
Definition: contexts.hpp:1483
virtual bool get_support_villages() const override
Definition: contexts.cpp:844
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:1080
bool is_keep(const map_location &loc) const
Definition: map.cpp:71
virtual const std::vector< std::string > get_recruitment_pattern() const override
Definition: contexts.cpp:790
virtual int get_villages_per_scout() const override
Definition: contexts.cpp:862
virtual const attacks_vector & get_attacks() const override
Definition: contexts.cpp:554
::tod_manager * tod_manager
Definition: resources.cpp:29
int h() const
Effective map height.
Definition: map.hpp:128
virtual int get_recursion_count() const override
Get the value of the recursion counter.
Definition: contexts.cpp:80
unit_iterator end()
Definition: map.hpp:415
std::pair< int, int > parse_range(const std::string &str)
typesafe_aspect_ptr< bool > simple_targeting_
Definition: contexts.hpp:1514
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:88
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:92
std::shared_ptr< stopunit_result > stopunit_result_ptr
Definition: game_info.hpp:86
virtual const std::vector< std::string > get_recruitment_more() const override
Definition: contexts.cpp:781
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:517
virtual const unit_map & units() const override
Definition: game_board.hpp:114
static attack_result_ptr execute_attack_action(side_number side, bool execute, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect &advancements=unit_advancements_aspect())
Ask the game to attack an enemy defender using our unit attacker from attackers current location...
Definition: actions.cpp:998
recruit_result_ptr check_recruit_action(const std::string &unit_name, const map_location &where=map_location::null_location(), const map_location &from=map_location::null_location()) override
Check if it is possible to recruit a unit for us on specified location.
Definition: contexts.cpp:150
This class represents a single unit of a specific type.
Definition: unit.hpp:99
void init(const gamemap &map)
Definition: contexts.cpp:949
int dummy
Definition: lstrlib.cpp:1125
virtual const move_map & get_enemy_dstsrc() const override
Definition: contexts.cpp:602
bool is_castle(const map_location &loc) const
Definition: map.cpp:69
virtual void handle_generic_event(const std::string &event_name) override
Handle generic event.
Definition: contexts.cpp:306
virtual const config get_recruitment_instructions() const override
Definition: contexts.cpp:772
typesafe_aspect_ptr< config > leader_goal_
Definition: contexts.hpp:1494
static manager & get_singleton()
Definition: manager.hpp:151
void raise_gamestate_changed()
Notifies all observers of &#39;ai_gamestate_changed&#39; event.
Definition: manager.cpp:425
virtual team & current_team_w() override
Return a reference to the &#39;team&#39; object for the AI.
Definition: contexts.cpp:104
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
Calculate the moves units may possibly make.
Definition: contexts.cpp:344
Various functions that implement attacks and attack calculations.
unit_iterator find_leader(int side)
Definition: map.cpp:329
virtual stopunit_result_ptr execute_stopunit_action(const map_location &unit_location, bool remove_movement=true, bool remove_attacks=false) override
Ask the game to remove unit movements and/or attack.
Definition: contexts.cpp:155
virtual bool is_active(const std::string &time_of_day, const std::string &turns) const override
Definition: contexts.cpp:1261
typesafe_aspect_ptr< double > caution_
Definition: contexts.hpp:1484
void add_known_aspect(const std::string &name, typesafe_aspect_ptr< T > &where)
Definition: contexts.cpp:175
#define a
Managing the AI-Game interaction - AI actions and their results.
game_info & get_active_ai_info_for_side(side_number side)
Gets AI info for active AI of the given side.
Definition: manager.cpp:708
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:453
virtual const moves_map & get_enemy_possible_moves() const override
Definition: contexts.cpp:611
virtual void set_src_dst_enemy_valid_lua() override
Definition: contexts.cpp:1206
void log_message(const std::string &msg) override
Display a debug message as a chat message.
Definition: contexts.cpp:335
child_itors child_range(config_key_type key)
Definition: config.cpp:366
std::string id
Definition: time_of_day.hpp:91
virtual recruit_result_ptr execute_recruit_action(const std::string &unit_name, const map_location &where=map_location::null_location(), const map_location &from=map_location::null_location()) override
Ask the game to recruit a unit for us on specified location.
Definition: contexts.cpp:140
virtual void invalidate_keeps_cache() const override
Definition: contexts.cpp:897
readonly_context_impl(side_context &context, const config &cfg)
Constructor.
Definition: contexts.cpp:180
void add_turn_started_observer(events::observer *event_observer)
Adds an observer of &#39;ai_turn_started&#39; event.
Definition: manager.cpp:379
std::shared_ptr< move_result > move_result_ptr
Definition: game_info.hpp:84
void clear(const std::string &key)
Definition: general.cpp:205
virtual const gamemap & map() const override
Definition: game_board.hpp:109
int lawful_bonus
The % bonus lawful units receive.
Definition: time_of_day.hpp:84
dest_vect destinations
Definition: pathfind.hpp:99
unit_iterator begin()
Definition: map.hpp:405
move_result_ptr check_move_action(const map_location &from, const map_location &to, bool remove_movement=true, bool unreach_is_ok=false) override
Check if it is possible to move our unit from location &#39;from&#39; to location &#39;to&#39;.
Definition: contexts.cpp:130
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:50
static void parse_engine_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< engine_ptr >> b)
Definition: engine.cpp:69
stopunit_result_ptr check_stopunit_action(const map_location &unit_location, bool remove_movement=true, bool remove_attacks=false) override
Check if it is possible to remove unit movements and/or attack.
Definition: contexts.cpp:160
Composite AI stages.
AI Support engine - creating specific ai components from config.
std::vector< goal_ptr > goals_
Definition: contexts.hpp:1491
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
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.cpp:469
std::shared_ptr< recruit_result > recruit_result_ptr
Definition: game_info.hpp:83
virtual config get_leader_goal() const override
Definition: contexts.cpp:709
virtual const std::set< map_location > & keeps() const override
Definition: contexts.cpp:922
void init_side_context_proxy(side_context &target)
Definition: contexts.hpp:460
static config unit_name(const unit *u)
Definition: reports.cpp:147
static void parse_goal_from_config(readonly_context &context, const config &cfg, std::back_insert_iterator< std::vector< goal_ptr >> b)
Definition: engine.cpp:79
virtual double get_scout_village_targeting() const override
Definition: contexts.cpp:817
-file sdl_utils.hpp
std::shared_ptr< attack_result > attack_result_ptr
Definition: game_info.hpp:81
virtual const aspect_map & get_aspects() const override
Definition: contexts.cpp:542
typesafe_aspect_ptr< double > leader_value_
Definition: contexts.hpp:1496
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
unit_type::ALIGNMENT alignment() const
The alignment of this unit.
Definition: unit.hpp:429
std::map< map_location, defensive_position > defensive_position_cache_
Definition: contexts.hpp:1485
typesafe_aspect_ptr< bool > passive_leader_shares_keep_
Definition: contexts.hpp:1504
void add_chat_message(const std::time_t &time, const std::string &speaker, int side, const std::string &msg, events::chat_handler::MESSAGE_TYPE type, bool bell)
typesafe_aspect_ptr< int > villages_per_scout_
Definition: contexts.hpp:1519
virtual bool leader_can_reach_keep() const override
Definition: contexts.cpp:980
std::shared_ptr< aspect > aspect_ptr
Definition: game_info.hpp:94
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
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.cpp:1033
std::vector< attack_analysis > attacks_vector
Definition: game_info.hpp:50
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
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
virtual void on_readonly_context_create() override
Definition: contexts.cpp:255
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit&#39;s defense on a given terrain.
Definition: unit.cpp:1596
#define ERR_AI
Definition: contexts.cpp:67
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
void remove_gamestate_observer(events::observer *event_observer)
Removes an observer of game events except ai_user_interact event and ai_sync_network event...
Definition: manager.cpp:349
std::shared_ptr< typesafe_aspect< T > > typesafe_aspect_ptr
Definition: game_info.hpp:57
team & get_team(int i)
Definition: game_board.hpp:104
static recall_result_ptr execute_recall_action(side_number side, bool execute, const std::string &unit_id, const map_location &where, const map_location &from)
Ask the game to recall a unit for us on specified location.
Definition: actions.cpp:1023
std::shared_ptr< recall_result > recall_result_ptr
Definition: game_info.hpp:82
void raise_gamestate_changed() const override
Notifies all interested observers of the event respectively.
Definition: contexts.cpp:98
virtual const move_map & get_enemy_srcdst() const override
Definition: contexts.cpp:620
const std::set< map_location > & get()
Definition: contexts.cpp:954
std::shared_ptr< goal > goal_ptr
Definition: game_info.hpp:99
virtual void add_aspects(std::vector< aspect_ptr > &aspects) override
Definition: contexts.cpp:446
virtual const config get_recruitment_save_gold() const override
Definition: contexts.cpp:808
virtual int get_recruitment_randomness() const override
Definition: contexts.cpp:799
map_location curr
Definition: pathfind.hpp:87
void remove_turn_started_observer(events::observer *event_observer)
Deletes an observer of &#39;ai_turn_started&#39; event.
Definition: manager.cpp:397
filter_context * filter_con
Definition: resources.cpp:23
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
Definition: tod_manager.hpp:54
virtual const std::vector< engine_ptr > & get_engines() const override
Definition: contexts.cpp:666
bool valid() const
Definition: location.hpp:93
virtual const std::vector< goal_ptr > & get_goals() const override
Definition: contexts.cpp:687
game_board * gameboard
Definition: resources.cpp:20
virtual const unit_advancements_aspect & get_advancements() const override
Definition: contexts.cpp:522
virtual ~readonly_context_impl()
Destructor.
Definition: contexts.cpp:301
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
A more fundamental version of calculate_possible_moves which allows the use of a speculative unit map...
Definition: contexts.cpp:351
static factory_map & get_list()
Definition: engine.hpp:124
typesafe_aspect_ptr< std::vector< std::string > > recruitment_pattern_
Definition: contexts.hpp:1509
static lg::log_domain log_ai("ai/general")
virtual config to_readwrite_context_config() const override
serialize this context to config
Definition: contexts.cpp:280
virtual synced_command_result_ptr execute_synced_command_action(const std::string &lua_code, const map_location &location=map_location::null_location()) override
Ask the game to run Lua code.
Definition: contexts.cpp:165
virtual bool is_src_dst_enemy_valid_lua() const override
Definition: contexts.cpp:886
Encapsulates the map of the game.
Definition: map.hpp:36
virtual std::map< map_location, defensive_position > & defensive_position_cache() const override
Definition: contexts.cpp:516
virtual const wfl::variant & get_attacks_as_variant() const override
Definition: contexts.cpp:564
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.cpp:1211
#define DBG_AI
Definition: contexts.cpp:64
bool is_enemy(int n) const
Definition: team.hpp:243
Object which temporarily resets a unit&#39;s movement.
Definition: unit.hpp:1845
const team & current_team() const override
Return a reference to the &#39;team&#39; object for the AI.
Definition: contexts.cpp:329
Managing the AIs lifecycle - headers.
recall_result_ptr check_recall_action(const std::string &id, const map_location &where=map_location::null_location(), const map_location &from=map_location::null_location()) override
Check if it is possible to recall a unit for us on specified location.
Definition: contexts.cpp:145
virtual const move_map & get_srcdst() const override
Definition: contexts.cpp:835
typesafe_aspect_ptr< double > scout_village_targeting_
Definition: contexts.hpp:1513
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
Ask the game to move our unit from location &#39;from&#39; to location &#39;to&#39;, optionally - doing a partial mov...
Definition: contexts.cpp:125
std::array< map_location, 6 > adjacent_loc_array_t
Definition: location.hpp:170
typesafe_aspect_ptr< config > recruitment_instructions_
Definition: contexts.hpp:1507
virtual engine_ptr get_engine_by_cfg(const config &cfg) override
get engine by cfg, creating it if it is not created yet but known
Definition: contexts.cpp:629
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:130
virtual game_info & get_info_w() override
Functions to retrieve the &#39;info&#39; object.
Definition: contexts.cpp:317
std::set< map_location > keeps_
Definition: contexts.hpp:120
virtual std::string get_grouping() const override
Definition: contexts.cpp:678
recursion_counter recursion_counter_
Definition: contexts.hpp:1104
virtual bool get_passive_leader_shares_keep() const override
Definition: contexts.cpp:745
std::shared_ptr< synced_command_result > synced_command_result_ptr
Definition: game_info.hpp:87
virtual const moves_map & get_possible_moves() const override
Definition: contexts.cpp:754
Encapsulates the map of the game.
Definition: location.hpp:42
unit_iterator find(std::size_t id)
Definition: map.cpp:311
virtual void recalculate_move_maps() const override
Definition: contexts.cpp:1149
virtual int get_recursion_count() const override
Get the value of the recursion counter.
Definition: contexts.cpp:74
virtual recall_result_ptr execute_recall_action(const std::string &id, const map_location &where=map_location::null_location(), const map_location &from=map_location::null_location()) override
Ask the game to recall a unit for us on specified location.
Definition: contexts.cpp:135
int w() const
Effective map width.
Definition: map.hpp:125
virtual void set_dst_src_valid_lua() override
Definition: contexts.cpp:1191
typesafe_aspect_ptr< bool > leader_ignores_keep_
Definition: contexts.hpp:1495
virtual bool is_dst_src_enemy_valid_lua() const override
Definition: contexts.cpp:876
virtual void set_dst_src_enemy_valid_lua() override
Definition: contexts.cpp:1196
virtual void set_src_dst_valid_lua() override
Definition: contexts.cpp:1201
void raise_user_interact()
Notifies all observers of &#39;ai_user_interact&#39; event.
Definition: manager.cpp:402
virtual bool get_simple_targeting() const override
Definition: contexts.cpp:826
virtual unit_stats_cache_t & unit_stats_cache() const override
Weapon choice cache, to speed simulations.
Definition: contexts.cpp:1255
virtual double get_recruitment_diversity() const override
Definition: contexts.cpp:763
std::vector< engine_ptr > engines_
AI Support Engines.
Definition: contexts.hpp:1475
std::size_t i
Definition: function.cpp:933
void raise_user_interact() const override
Function which should be called frequently to allow the user to interact with the interface...
Definition: contexts.cpp:92
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:459
typesafe_aspect_ptr< bool > support_villages_
Definition: contexts.hpp:1516
Game information for the AI.
void set_diagnostic(const std::string &msg)
Definition: display.cpp:1605
virtual bool get_leader_ignores_keep() const override
Definition: contexts.cpp:718
synced_command_result_ptr check_synced_command_action(const std::string &lua_code, const map_location &location=map_location::null_location()) override
Check if it is possible to run Lua code.
Definition: contexts.cpp:170
virtual double get_leader_value() const override
Definition: contexts.cpp:727
double g
Definition: astarsearch.cpp:64
typesafe_aspect_ptr< double > leader_aggression_
Definition: contexts.hpp:1493
attack_itors attacks()
Gets an iterator over this unit&#39;s attacks.
Definition: unit.hpp:874
virtual bool get_passive_leader() const override
Definition: contexts.cpp:736
static recruit_result_ptr execute_recruit_action(side_number side, bool execute, const std::string &unit_name, const map_location &where, const map_location &from)
Ask the game to recruit a unit for us on specified location.
Definition: actions.cpp:1034
virtual config to_readonly_context_config() const override
serialize to config
Definition: contexts.cpp:286
virtual side_number get_side() const override
Get the side number.
Definition: contexts.hpp:465
static synced_command_result_ptr execute_synced_command_action(side_number side, bool execute, const std::string &lua_code, const map_location &location)
Ask the game to run Lua code.
Definition: actions.cpp:1056
typesafe_aspect_ptr< attacks_vector > attacks_
Definition: contexts.hpp:1482
const bool & debug
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
typesafe_aspect_ptr< unit_advancements_aspect > advancements_
Definition: contexts.hpp:1479
typesafe_aspect_ptr< std::vector< std::string > > recruitment_more_
Definition: contexts.hpp:1508
std::map< std::pair< map_location, const unit_type * >, std::pair< battle_context_unit_stats, battle_context_unit_stats > > unit_stats_cache_t
Definition: contexts.hpp:397
virtual void invalidate_defensive_position_cache() const override
Definition: contexts.cpp:891
Helper functions for the object which operates in the context of AI for specific side this is part of...
typesafe_aspect_ptr< std::string > grouping_
Definition: contexts.hpp:1490
config & add_child(config_key_type key)
Definition: config.cpp:480
virtual double get_aggression() const override
Definition: contexts.cpp:533
static stopunit_result_ptr execute_stopunit_action(side_number side, bool execute, const map_location &unit_location, bool remove_movement, bool remove_attacks)
Ask the game to remove unit movements and/or attack.
Definition: actions.cpp:1045
virtual const terrain_filter & get_avoid() const override
Definition: contexts.cpp:573
virtual bool is_src_dst_valid_lua() const override
Definition: contexts.cpp:881
display_chat_manager & get_chat_manager()
int turns()
Definition: game.cpp:558
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:600
std::map< std::string, aspect_ptr > aspect_map
Definition: game_info.hpp:103
virtual void invalidate_move_maps() const override
Definition: contexts.cpp:909
virtual double get_caution() const override
Definition: contexts.cpp:585
#define WRN_AI
Definition: contexts.cpp:66
bool find(E event, F functor)
Tests whether an event handler is available.
virtual attack_result_ptr execute_attack_action(const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon) override
Ask the game to attack an enemy defender using our unit attacker from attackers current location...
Definition: contexts.cpp:109
void add_gamestate_observer(events::observer *event_observer)
Adds observer of game events except ai_user_interact event and ai_sync_network event.
Definition: manager.cpp:342
known_aspect_map known_aspects_
Definition: contexts.hpp:1477
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
bool contains(const map_location &) const
Definition: pathfind.cpp:520
Standard logging facilities (interface).
const gamemap * map_
Definition: contexts.hpp:119
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
Container associating units to locations.
Definition: map.hpp:99
int get_count() const
Get the current value of the recursion counter.
Definition: contexts.hpp:70
typesafe_aspect_ptr< int > recruitment_randomness_
Definition: contexts.hpp:1510
void handle_generic_event(const std::string &event_name)
Definition: contexts.cpp:903
virtual config to_side_context_config() const override
serialize this context to config
Definition: contexts.cpp:275
typesafe_aspect_ptr< double > recruitment_diversity_
Definition: contexts.hpp:1506
virtual int get_recursion_count() const override
Get the value of the recursion counter.
Definition: contexts.cpp:86
int turn() const
#define e
int side() const
The side this unit belongs to.
Definition: unit.hpp:303
virtual const map_location & nearest_keep(const map_location &loc) const override
Definition: contexts.cpp:1003
typesafe_aspect_ptr< double > aggression_
Definition: contexts.hpp:1480
std::shared_ptr< engine > engine_ptr
Definition: game_info.hpp:98
typesafe_aspect_ptr< bool > passive_leader_
Definition: contexts.hpp:1503
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
virtual const game_info & get_info() const override
Definition: contexts.cpp:312
bool valid() const
Definition: map.hpp:276
virtual double get_village_value() const override
Definition: contexts.cpp:853
attack_result_ptr check_attack_action(const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon) override
Check if it is possible to attack enemy defender using our unit attacker from attackers current locat...
Definition: contexts.cpp:117
virtual void recalculate_move_maps_enemy() const override
Definition: contexts.cpp:1178
static map_location::DIRECTION n
typesafe_aspect_ptr< config > recruitment_save_gold_
Definition: contexts.hpp:1511
This module contains various pathfinding functions and utilities.
virtual const move_map & get_dstsrc() const override
Definition: contexts.cpp:593
typesafe_aspect_ptr< double > village_value_
Definition: contexts.hpp:1518
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
void diagnostic(const std::string &msg) override
Show a diagnostic message on the screen.
Definition: contexts.cpp:321
static move_result_ptr execute_move_action(side_number side, bool execute, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok=false)
Ask the game to move our unit from location &#39;from&#39; to location &#39;to&#39;, optionally - doing a partial mov...
Definition: actions.cpp:1011
virtual bool is_dst_src_valid_lua() const override
Definition: contexts.cpp:871
virtual double get_leader_aggression() const override
Definition: contexts.cpp:700
void add_map_changed_observer(events::observer *event_observer)
Adds an observer of &#39;ai_map_changed&#39; event.
Definition: manager.cpp:367
static game_display * get_singleton()
virtual void add_facet(const std::string &id, const config &cfg) const override
Definition: contexts.cpp:459
void remove_map_changed_observer(events::observer *event_observer)
Deletes an observer of &#39;ai_map_changed&#39; event.
Definition: manager.cpp:391