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