The Battle for Wesnoth  1.15.11+dev
function_table.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Bartosz Waresiak <dragonking@o2.pl>
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 #include <queue>
16 #include <set>
17 #include <utility>
18 #include <vector>
19 
20 #include "ai/formula/ai.hpp"
23 
24 #include "ai/default/contexts.hpp"
25 
27 #include "attack_prediction.hpp"
28 #include "filesystem.hpp"
29 #include "game_board.hpp"
30 #include "global.hpp"
31 #include "display.hpp"
32 #include "log.hpp"
33 #include "map/label.hpp"
34 #include "map/map.hpp"
35 #include "pathfind/teleport.hpp"
36 #include "replay.hpp"
37 #include "resources.hpp"
38 #include "color.hpp"
39 #include "terrain/filter.hpp"
40 #include "units/unit.hpp"
41 #include "units/types.hpp"
42 #include "pathfind/pathfind.hpp"
43 
44 static lg::log_domain log_formula_ai("ai/engine/fai");
45 #define LOG_AI LOG_STREAM(info, log_formula_ai)
46 #define WRN_AI LOG_STREAM(warn, log_formula_ai)
47 #define ERR_AI LOG_STREAM(err, log_formula_ai)
48 
49 namespace wfl {
50 using ai::formula_ai;
51 
52 namespace {
53 
54 /*
55  * unit adapters let us treat unit and unit_type the same if we want to get access to attacks or movement cost
56  */
57 class unit_adapter {
58  public:
59  unit_adapter(const variant& arg) : unit_type_(), unit_() {
60  auto unit = arg.try_convert<unit_callable>();
61 
62  if (unit) {
63  unit_ = &unit->get_unit();
64  } else {
65  unit_type_ = &(arg.convert_to<unit_type_callable>()->get_unit_type());
66  }
67  }
68 
69  int damage_from(const attack_type& attack) const {
70  if(unit_type_ != nullptr) {
72  } else {
73  return unit_->damage_from(attack, false, map_location());
74  }
75  }
76 
77  const_attack_itors attacks() const {
78  if(unit_type_ != nullptr) {
79  return unit_type_->attacks();
80  } else {
81  return unit_->attacks();
82  }
83  }
84 
85  int movement_cost(const t_translation::terrain_code & terrain) const {
86  if(unit_type_ != nullptr) {
87  return unit_type_->movement_type().movement_cost(terrain);
88  } else {
89  return unit_->movement_cost(terrain);
90  }
91  }
92 
93  private:
95  const unit* unit_;
96 };
97 
98 #define DEFINE_FAI_FUNCTION(name, min_args, max_args) \
99  class name##_function : public function_expression \
100  { \
101  public: \
102  explicit name##_function(const args_list& args, const formula_ai& ai) \
103  : function_expression(#name, args, min_args, max_args), ai_(ai) \
104  { \
105  } \
106  \
107  private: \
108  const formula_ai& ai_; \
109  variant execute(const formula_callable& variables, formula_debugger* fdb) const; \
110  }; \
111  \
112  variant name##_function::execute(const formula_callable& variables, formula_debugger* fdb) const
113 
114 DEFINE_FAI_FUNCTION(distance_to_nearest_unowned_village, 1, 1)
115 {
116  const map_location loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "distance_to_nearest_unowned_village:location")).convert_to<location_callable>()->loc();
117  int best = 1000000;
118  const std::vector<map_location>& villages = resources::gameboard->map().villages();
119  const std::set<map_location>& my_villages = ai_.current_team().villages();
120  for(std::vector<map_location>::const_iterator i = villages.begin(); i != villages.end(); ++i) {
121  int distance = distance_between(loc, *i);
122  if(distance < best) {
123  if(my_villages.count(*i) == 0) {
124  best = distance;
125  }
126  }
127  }
128 
129  return variant(best);
130 }
131 
132 static unsigned search_counter;
133 
134 namespace {
135  struct indexer {
136  int w, h;
137  indexer(int a, int b) : w(a), h(b) { }
138  int operator()(const map_location& loc) const {
139  return loc.y * w + loc.x;
140  }
141  };
142 
143  struct node {
146 
147  /**
148  * If equal to search_counter, the node is off the list.
149  * If equal to search_counter + 1, the node is on the list.
150  * Otherwise it is outdated.
151  */
152  unsigned in;
153 
154  node(int moves, const map_location &loc)
155  : movement_cost_(moves)
156  , loc_(loc)
157  , in(0)
158  {
159  }
160 
161  node()
162  : movement_cost_(0)
163  , loc_()
164  , in(0)
165  {
166  }
167 
168  bool operator<(const node& o) const {
169  return movement_cost_ < o.movement_cost_;
170  }
171  };
172 
173  struct comp {
174  const std::vector<node>& nodes;
175  comp(const std::vector<node>& n) : nodes(n) { }
176  bool operator()(int l, int r) const {
177  return nodes[r] < nodes[l];
178  }
179  };
180 
181  void find_movemap(const unit_adapter& u, const map_location& loc,
182  std::vector<int>& scores, bool allow_teleport, const formula_ai& ai_)
183  {
184  const std::set<map_location>& teleports = allow_teleport ? ai_.current_team().villages() : std::set<map_location>();
185 
186  const gamemap& map = resources::gameboard->map();
187 
188  std::vector<map_location> locs(6 + teleports.size());
189  std::copy(teleports.begin(), teleports.end(), locs.begin() + 6);
190 
191  search_counter += 2;
192  if (search_counter == 0) search_counter = 2;
193 
194  static std::vector<node> nodes;
195  nodes.resize(map.w() * map.h());
196 
197  indexer index(map.w(), map.h());
198  comp node_comp(nodes);
199 
200  nodes[index(loc)] = node(0, loc);
201  std::vector<int> pq;
202  pq.push_back(index(loc));
203  while (!pq.empty()) {
204  node& n = nodes[pq.front()];
205  std::pop_heap(pq.begin(), pq.end(), node_comp);
206  pq.pop_back();
207  n.in = search_counter;
208 
209  get_adjacent_tiles(n.loc_, locs.data());
210  for (int i = teleports.count(n.loc_) ? locs.size() : 6; i-- > 0;) {
211  if (!locs[i].valid(map.w(), map.h())) continue;
212 
213  node& next = nodes[index(locs[i])];
214  bool next_visited = next.in - search_counter <= 1u;
215 
216  // test if the current path to locs[i] is better than this one could possibly be.
217  // we do this a couple more times below
218  if (next_visited && !(n < next)) continue;
219  const int move_cost = u.movement_cost(map[locs[i]]);
220 
221  node t = node(n.movement_cost_ + move_cost, locs[i]);
222 
223  if (next_visited && !(t < next)) continue;
224 
225  bool in_list = next.in == search_counter + 1;
226  t.in = search_counter + 1;
227  next = t;
228 
229  // if already in the priority queue then we just update it, else push it.
230  if (in_list) {
231  std::push_heap(pq.begin(), std::find(pq.begin(), pq.end(), index(locs[i])) + 1, node_comp);
232  }
233  else {
234  pq.push_back(index(locs[i]));
235  std::push_heap(pq.begin(), pq.end(), node_comp);
236  }
237  }
238  }
239 
240  for (int x = 0; x < map.w(); ++x) {
241  for (int y = 0; y < map.h(); ++y)
242  {
243  int i = y * map.w() + x;
244  const node &n = nodes[i];
245  scores[i] = scores[i] + n.movement_cost_;
246  //std::cout << x << "," << y << ":" << n.movement_cost << std::endl;
247  }
248  }
249  }
250 }
251 
252 DEFINE_FAI_FUNCTION(calculate_map_ownership, 2, 5)
253 {
254  int w = resources::gameboard->map().w();
255  int h = resources::gameboard->map().h();
256 
257  const variant units_input = args()[0]->evaluate(variables,fdb);
258  const variant leaders_input = args()[1]->evaluate(variables,fdb);
259 
260  int enemy_tolerance = 3;
261  if( args().size() > 2 )
262  enemy_tolerance = args()[2]->evaluate(variables,fdb).as_int();
263 
264  int enemy_border_tolerance = 5;
265  if( args().size() > 3 )
266  enemy_border_tolerance = args()[3]->evaluate(variables,fdb).as_int();
267 
268  int ally_tolerance = 3;
269  if( args().size() > 4 )
270  ally_tolerance = args()[4]->evaluate(variables,fdb).as_int();
271 
272  if( !units_input.is_list() )
273  return variant();
274 
275  std::size_t number_of_teams = units_input.num_elements();
276 
277  std::vector< std::vector<int>> scores( number_of_teams );
278 
279  for( std::size_t i = 0; i< number_of_teams; ++i)
280  scores[i].resize(w*h);
281 
282  /* // TODO: Do we need this?
283  for(unit_map::const_iterator i = resources::gameboard->units().begin(); i != resources::gameboard->units().end(); ++i) {
284  unit_counter[i->second.side()-1]++;
285  unit_adapter unit(i->second);
286  find_movemap( resources::gameboard->map(), resources::gameboard->units(), unit, i->first, scores[i->second.side()-1], ai_.resources::gameboard->teams() , true );
287  }
288  */
289 
290  for(std::size_t side = 0 ; side < units_input.num_elements() ; ++side) {
291  if( leaders_input[side].is_empty() )
292  continue;
293 
294  const map_location loc = leaders_input[side][0].convert_to<location_callable>()->loc();
295  const variant units_of_side = units_input[side];
296 
297  for(std::size_t unit_it = 0 ; unit_it < units_of_side.num_elements() ; ++unit_it) {
298  unit_adapter unit(units_of_side[unit_it]);
299  find_movemap( unit, loc, scores[side], true, ai_ );
300  }
301  }
302 
303  std::size_t index = 0;
304  for( std::vector< std::vector<int>>::iterator i = scores.begin() ; i != scores.end() ; ++i) {
305  for( std::vector<int>::iterator j = i->begin() ; j != i->end() ; ++j ) {
306  if(units_input[index].num_elements() != 0) {
307  *j /= units_input[index].num_elements();
308  } else {
309  *j = 0;
310  }
311  }
312 
313  ++index;
314  }
315  //std::vector<variant> res;
316  std::map<variant, variant> res;
317 
318  std::size_t current_side = ai_.get_side() - 1 ;
319 
320  std::vector<int> enemies;
321  std::vector<int> allies;
322 
323  for(std::size_t side = 0 ; side < units_input.num_elements() ; ++side) {
324  if( side == current_side)
325  continue;
326 
327  if( ai_.current_team().is_enemy(side+1) ) {
328  if( !leaders_input[side].is_empty() )
329  enemies.push_back(side);
330  } else {
331  if( !leaders_input[side].is_empty() )
332  allies.push_back(side);
333  }
334  }
335 
336  //calculate_map_ownership( recruits_of_side, map(units_of_side, 'units', map( filter(units, leader), loc) ) )
337  //map(, debug_label(key,value))
338  for (int x = 0; x < w; ++x) {
339  for (int y = 0; y < h; ++y)
340  {
341  int i = y * w + x;
342  bool valid = true;
343  bool enemy_border = false;
344 
345  if( scores[current_side][i] > 98 )
346  continue;
347 
348  for (int side : enemies) {
349  int diff = scores[current_side][i] - scores[side][i];
350  if ( diff > enemy_tolerance) {
351  valid = false;
352  break;
353  } else if( std::abs(diff) < enemy_border_tolerance )
354  enemy_border = true;
355  }
356 
357  if( valid ) {
358  for (int side : allies) {
359  if ( scores[current_side][i] - scores[side][i] > ally_tolerance ) {
360  valid = false;
361  break;
362  }
363  }
364  }
365 
366  if( valid ) {
367  if( enemy_border )
368  res.emplace(variant(std::make_shared<location_callable>(map_location(x, y))), variant(scores[0][i] + 10000));
369  else
370  res.emplace(variant(std::make_shared<location_callable>(map_location(x, y))), variant(scores[0][i]));
371  }
372  }
373  }
374  return variant(res);
375 }
376 
377 DEFINE_WFL_FUNCTION(nearest_loc, 2, 2)
378 {
379  const map_location loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "nearest_loc:location")).convert_to<location_callable>()->loc();
380  variant items = args()[1]->evaluate(variables,add_debug_info(fdb,1,"nearest_loc:locations"));
381  int best = 1000000;
382  int best_i = -1;
383 
384  for(std::size_t i = 0; i < items.num_elements(); ++i) {
385 
386  const map_location move_loc = items[i].convert_to<location_callable>()->loc();
387  int distance = distance_between(loc, move_loc);
388 
389  if(distance < best) {
390  best = distance;
391  best_i = i;
392  }
393  }
394 
395  if( best_i != -1)
396  return variant(std::make_shared<location_callable>(items[best_i].convert_to<location_callable>()->loc()));
397  else
398  return variant();
399 }
400 
401 /** FormulaAI function to run fai script from file. Usable from in-game console.
402 * arguments[0] - required file name, follows the usual wml convention
403 */
404 DEFINE_FAI_FUNCTION(run_file, 1, 1)
405 {
406  const args_list& arguments = args();
407  const variant var0 = arguments[0]->evaluate(variables,add_debug_info(fdb,0,"run_file:file"));
408  const std::string filename = var0.string_cast();
409 
410  //NOTE: get_wml_location also filters file path to ensure it doesn't contain things like "../../top/secret"
411  std::string path = filesystem::get_wml_location(filename);
412  if(path.empty()) {
413  ERR_AI << "run_file : not found [" << filename <<"]"<< std::endl;
414  return variant(); //no suitable file
415  }
416 
417  std::string formula_string = filesystem::read_file(path);
418  //need to get function_table from somewhere or delegate to someone who has access to it
419  formula_ptr parsed_formula = ai_.create_optional_formula(formula_string);
420  if(parsed_formula == formula_ptr()) {
421  ERR_AI << "run_file : unable to create formula"<< std::endl;
422  return variant(); //was unable to create a formula from file
423  }
424  return parsed_formula->evaluate(variables,add_debug_info(fdb,-1,"run_file:formula_from_file"));
425 }
426 
427 DEFINE_WFL_FUNCTION(castle_locs, 1, 1)
428 {
429  const map_location starting_loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "castle_locs:location")).convert_to<location_callable>()->loc();
430 
431  //looks like reimplementing a generic graph search algorithm to me
432  std::set< map_location > visited_locs;
433  std::queue< map_location > queued_locs;
434 
435  queued_locs.push(starting_loc);
436 
437  while( !queued_locs.empty() ) {
438  const map_location loc = queued_locs.front();
439  queued_locs.pop();
440 
441  if ( visited_locs.find( loc ) != visited_locs.end() )
442  continue;
443 
444  visited_locs.insert(loc);
445 
446  for(const map_location& adj : get_adjacent_tiles(loc)) {
447  if (resources::gameboard->map().on_board(adj) && visited_locs.find( adj ) == visited_locs.end() ) {
448  if (resources::gameboard->map().get_terrain_info(adj).is_keep() ||
450  queued_locs.push(adj);
451  }
452  }
453  }
454  }
455 
456  if ( !resources::gameboard->map().get_terrain_info(starting_loc).is_keep() &&
457  !resources::gameboard->map().get_terrain_info(starting_loc).is_castle() )
458  visited_locs.erase(starting_loc);
459 
460  std::vector<variant> res;
461  for (const map_location& ml : visited_locs) {
462  res.emplace_back(std::make_shared<location_callable>( ml ));
463  }
464 
465  return variant(res);
466 }
467 
468 /**
469  * timeofday_modifer formula function. Returns combat modifier, taking
470  * alignment, illuminate, time of day and fearless trait into account.
471  * 'leadership' and 'slowed' are not taken into account.
472  * arguments[0] - unit
473  * arguments[1] - location (optional, defaults to unit's current location.
474  */
475 DEFINE_WFL_FUNCTION(timeofday_modifier, 1, 2)
476 {
477  variant u = args()[0]->evaluate(variables,add_debug_info(fdb,0,"timeofday_modifier:unit"));
478 
479  if( u.is_null() ) {
480  return variant();
481  }
482 
483  auto u_call = u.try_convert<unit_callable>();
484 
485  if(!u_call) {
486  return variant();
487  }
488 
489  const unit& un = u_call->get_unit();
490 
491  map_location loc;
492 
493  if(args().size()==2) {
494  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "timeofday_modifier:location")).convert_to<location_callable>()->loc();
495  }
496 
497  if(!loc.valid()) {
498  loc = u_call->get_location();
499  }
500 
501  return variant(combat_modifier(resources::gameboard->units(), resources::gameboard->map(), loc, un.alignment(), un.is_fearless()));
502 }
503 
504 DEFINE_FAI_FUNCTION(nearest_keep, 1, 1)
505 {
506  const map_location loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "nearest_keep:location")).convert_to<location_callable>()->loc();
507  int best = 1000000;
508  int best_i = -1;
509 
510  ai_.get_keeps();
511  int size = ai_.get_keeps_cache().num_elements();
512 
513  for( int i = 0 ; i < size; ++i) {
514  int distance = distance_between(loc, ai_.get_keeps_cache()[i].convert_to<location_callable>()->loc() );
515  if(distance < best)
516  {
517  best = distance;
518  best_i = i;
519  }
520  }
521 
522  if( best_i != -1)
523  return variant(std::make_shared<location_callable>(ai_.get_keeps_cache()[best_i].convert_to<location_callable>()->loc()));
524  else
525  return variant();
526 }
527 
528 /**
529 * Find suitable keep for unit at location
530 * arguments[0] - location for unit on which the suitable keep is to be found
531 */
532 DEFINE_FAI_FUNCTION(suitable_keep, 1, 1)
533 {
534  const map_location loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "suitable_keep:location")).convert_to<location_callable>()->loc();
535  const unit_map& units = resources::gameboard->units();
536  const unit_map::const_iterator u = units.find(loc);
537  if (u == units.end()){
538  return variant();
539  }
540  const pathfind::paths unit_paths(*u, false, true, ai_.current_team());
541  return variant(std::make_shared<location_callable>(ai_.suitable_keep(loc,unit_paths)));
542 }
543 
544 DEFINE_FAI_FUNCTION(find_shroud, 0, 1)
545 {
546  std::vector<variant> vars;
547  int w,h;
548 
549  if(args().size()==1) {
550  const gamemap& m = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "find_shroud:gamemap")).convert_to<gamemap_callable>()->get_gamemap();
551  w = m.w();
552  h = m.h();
553  } else {
554  w = resources::gameboard->map().w();
555  h = resources::gameboard->map().h();
556  }
557 
558  for(int i = 0; i < w; ++i)
559  for(int j = 0; j < h; ++j) {
560  if(ai_.current_team().shrouded(map_location(i,j)))
561  vars.emplace_back(std::make_shared<location_callable>(map_location(i, j)));
562  }
563 
564  return variant(vars);
565 }
566 
567 DEFINE_FAI_FUNCTION(close_enemies, 2, 2)
568 {
569  std::vector<variant> vars;
570  const map_location loc = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "close_enemies:location")).convert_to<location_callable>()->loc();
571  int range_s = args()[1]->evaluate(variables,add_debug_info(fdb,1,"close_enemies:distance")).as_int();
572  if (range_s < 0) {
573  WRN_AI << "close_enemies_function: range is negative (" << range_s << ")" << std::endl;
574  range_s = 0;
575  }
576  std::size_t range = static_cast<std::size_t>(range_s);
579  while (un != end) {
580  if (distance_between(loc, un->get_location()) <= range) {
581  if (un->side() != ai_.get_side()) {//fixme: ignores allied units
582  vars.emplace_back(std::make_shared<unit_callable>(*un));
583  }
584  }
585  ++un;
586  }
587  return variant(vars);
588 }
589 
590 DEFINE_WFL_FUNCTION(calculate_outcome, 3, 4)
591 {
592  std::vector<variant> vars;
593  int weapon;
594  if (args().size() > 3) weapon = args()[3]->evaluate(variables,add_debug_info(fdb,3,"calculate_outcome:weapon")).as_int();
595  else weapon = -1;
596 
597  const unit_map& units = resources::gameboard->units();
598  map_location attacker_location =
599  args()[0]->evaluate(variables, add_debug_info(fdb, 0, "calculate_outcome:attacker_current_location")).convert_to<location_callable>()->loc();
600  if(units.count(attacker_location) == 0) {
601  ERR_AI << "Performing calculate_outcome() with non-existent attacker at (" <<
602  attacker_location.wml_x() << "," << attacker_location.wml_y() << ")\n";
603  return variant();
604  }
605 
606  map_location defender_location =
607  args()[2]->evaluate(variables,add_debug_info(fdb, 2, "calculate_outcome:defender_location")).convert_to<location_callable>()->loc();
608  if(units.count(defender_location) == 0) {
609  ERR_AI << "Performing calculate_outcome() with non-existent defender at (" <<
610  defender_location.wml_x() << "," << defender_location.wml_y() << ")\n";
611  return variant();
612  }
613 
614  battle_context bc(units, args()[1]->evaluate(variables, add_debug_info(fdb, 1, "calculate_outcome:attacker_attack_location")).convert_to<location_callable>()->loc(),
615  defender_location, weapon, -1, 1.0, nullptr, units.find(attacker_location).get_shared_ptr());
616  std::vector<double> hp_dist = bc.get_attacker_combatant().hp_dist;
617  std::vector<double>::iterator it = hp_dist.begin();
618  int i = 0;
619  std::vector<variant> hitLeft;
620  std::vector<variant> prob;
621  while (it != hp_dist.end()) {
622  if (*it != 0) {
623  hitLeft.emplace_back(i);
624  prob.emplace_back(static_cast<int>(*it*10000));
625  }
626  ++it;
627  ++i;
628  }
629  std::vector<variant> status;
630  if (bc.get_attacker_combatant().poisoned != 0)
631  status.emplace_back("Poisoned");
632  if (bc.get_attacker_combatant().slowed != 0)
633  status.emplace_back("Slowed");
634  if (bc.get_defender_stats().petrifies && static_cast<unsigned int>(hitLeft[0].as_int()) != bc.get_attacker_stats().hp)
635  status.emplace_back("Stoned");
636  if (bc.get_defender_stats().plagues && hitLeft[0].as_int() == 0)
637  status.emplace_back("Zombiefied");
638  vars.emplace_back(std::make_shared<outcome_callable>(hitLeft, prob, status));
639  hitLeft.clear();
640  prob.clear();
641  status.clear();
642  hp_dist = bc.get_defender_combatant().hp_dist;
643  it = hp_dist.begin();
644  i = 0;
645  while (it != hp_dist.end()) {
646  if (*it != 0) {
647  hitLeft.emplace_back(i);
648  prob.emplace_back(static_cast<int>(*it*10000));
649  }
650  ++it;
651  ++i;
652  }
653  if (bc.get_defender_combatant().poisoned != 0)
654  status.emplace_back("Poisoned");
655  if (bc.get_defender_combatant().slowed != 0)
656  status.emplace_back("Slowed");
657  if (bc.get_attacker_stats().petrifies && static_cast<unsigned int>(hitLeft[0].as_int()) != bc.get_defender_stats().hp)
658  status.emplace_back("Stoned");
659  if (bc.get_attacker_stats().plagues && hitLeft[0].as_int() == 0)
660  status.emplace_back("Zombiefied");
661  vars.emplace_back(std::make_shared<outcome_callable>(hitLeft, prob, status));
662  return variant(vars);
663 }
664 
665 DEFINE_WFL_FUNCTION(outcomes, 1, 1)
666 {
667  variant attack = args()[0]->evaluate(variables,add_debug_info(fdb,0,"outcomes:attack"));
668  auto analysis = attack.convert_to<ai::attack_analysis>();
669  //unit_map units_with_moves(resources::gameboard->units());
670  //typedef std::pair<map_location, map_location> mv;
671  //for(const mv &m : analysis->movements) {
672  // units_with_moves.move(m.first, m.second);
673  //}
674 
675  std::vector<variant> vars;
676  if(analysis->chance_to_kill > 0.0) {
677  //unit_map units(units_with_moves);
678  //units.erase(analysis->target);
679  vars.emplace_back(std::make_shared<position_callable>(/*&units,*/ static_cast<int>(analysis->chance_to_kill*100)));
680 
681  }
682 
683  if(analysis->chance_to_kill < 1.0) {
684  //unit_map units(units_with_moves);
685  vars.emplace_back(std::make_shared<position_callable>(/*&units,*/ static_cast<int>(100 - analysis->chance_to_kill*100)));
686  }
687 
688  return variant(vars);
689 }
690 
691 DEFINE_FAI_FUNCTION(rate_action, 1, 1)
692 {
693  variant act = args()[0]->evaluate(variables,add_debug_info(fdb,0,"rate_action:action"));
694  auto analysis = act.convert_to<ai::attack_analysis>();
695 
696  return variant(analysis->rating(ai_.get_aggression(),ai_)*1000,variant::DECIMAL_VARIANT);
697 }
698 
699 DEFINE_WFL_FUNCTION(recall, 1, 2)
700 {
701  const std::string id = args()[0]->evaluate(variables,add_debug_info(fdb,0,"recall:id")).as_string();
702  map_location loc;
703  if(args().size() >= 2) {
704  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "recall:location")).convert_to<location_callable>()->loc();
705  }
706 
707  return variant(std::make_shared<recall_callable>(loc, id));
708 }
709 
710 DEFINE_WFL_FUNCTION(recruit, 1, 2)
711 {
712  const std::string type = args()[0]->evaluate(variables,add_debug_info(fdb,0,"recruit:type")).as_string();
713  map_location loc;
714  if(args().size() >= 2) {
715  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "recruit:location")).convert_to<location_callable>()->loc();
716  }
717 
718  return variant(std::make_shared<recruit_callable>(loc, type));
719 }
720 
721 DEFINE_FAI_FUNCTION(shortest_path, 2, 3)
722 {
723 
724  std::vector<variant> locations;
725 
726  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "shortest_path:src")).convert_to<location_callable>()->loc();
727  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "shortest_path:dst")).convert_to<location_callable>()->loc();
728  map_location unit_loc;
729 
730  if( src == dst )
731  return variant(locations);
732 
733  if(args().size() > 2)
734  unit_loc = args()[2]->evaluate(variables,add_debug_info(fdb,2,"shortest_path:unit_location")).convert_to<location_callable>()->loc();
735  else
736  unit_loc = src;
737 
738  unit_map::iterator unit_it = resources::gameboard->units().find(unit_loc);
739 
740  if( unit_it == resources::gameboard->units().end() ) {
741  std::ostringstream str;
742  str << "shortest_path function: expected unit at location (" << (unit_loc.wml_x()) << "," << (unit_loc.wml_y()) << ")";
743  throw formula_error( str.str(), "", "", 0);
744  }
745 
746  pathfind::teleport_map allowed_teleports = ai_.get_allowed_teleports(unit_it);
747 
748  pathfind::plain_route route = ai_.shortest_path_calculator( src, dst, unit_it, allowed_teleports );
749 
750  if( route.steps.size() < 2 ) {
751  return variant(locations);
752  }
753 
754  for (std::vector<map_location>::const_iterator loc_iter = route.steps.begin() + 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
755  locations.emplace_back(std::make_shared<location_callable>(*loc_iter));
756  }
757 
758  return variant(locations);
759 }
760 
761 DEFINE_FAI_FUNCTION(simplest_path, 2, 3)
762 {
763  std::vector<variant> locations;
764 
765  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "simplest_path:src")).convert_to<location_callable>()->loc();
766  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "simplest_path:dst")).convert_to<location_callable>()->loc();
767  map_location unit_loc;
768 
769  if( src == dst )
770  return variant(locations);
771 
772  if(args().size() > 2)
773  unit_loc = args()[2]->evaluate(variables, add_debug_info(fdb, 2, "simplest_path:unit_location")).convert_to<location_callable>()->loc();
774  else
775  unit_loc = src;
776 
777  unit_map::iterator unit_it = resources::gameboard->units().find(unit_loc);
778 
779  if( unit_it == resources::gameboard->units().end() ) {
780  std::ostringstream str;
781  str << "simplest_path function: expected unit at location (" << (unit_loc.wml_x()) << "," << (unit_loc.wml_y()) << ")";
782  throw formula_error( str.str(), "", "", 0);
783  }
784 
785  pathfind::teleport_map allowed_teleports = ai_.get_allowed_teleports(unit_it);
786 
788 
789  pathfind::plain_route route = pathfind::a_star_search(src, dst, 1000.0, em_calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
790 
791  if( route.steps.size() < 2 ) {
792  return variant(locations);
793  }
794 
795  for (std::vector<map_location>::const_iterator loc_iter = route.steps.begin() + 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
796  if(unit_it->movement_cost(static_cast<const game_board*>(resources::gameboard)->map()[*loc_iter]) < movetype::UNREACHABLE) {
797  locations.emplace_back(std::make_shared<location_callable>(*loc_iter));
798  } else {
799  break;
800  }
801  }
802 
803  return variant(locations);
804 }
805 
806 DEFINE_FAI_FUNCTION(next_hop, 2, 3)
807 {
808 
809  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "next_hop:src")).convert_to<location_callable>()->loc();
810  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "next_hop:dst")).convert_to<location_callable>()->loc();
811  map_location unit_loc;
812 
813  if( src == dst )
814  return variant();
815 
816  if(args().size() > 2)
817  unit_loc = args()[2]->evaluate(variables, add_debug_info(fdb, 2, "next_hop:unit_location")).convert_to<location_callable>()->loc();
818  else
819  unit_loc = src;
820 
821  unit_map::iterator unit_it = resources::gameboard->units().find(unit_loc);
822 
823  if( unit_it == resources::gameboard->units().end() ) {
824  std::ostringstream str;
825  str << "next_hop function: expected unit at location (" << (unit_loc.wml_x()) << "," << (unit_loc.wml_y()) << ")";
826  throw formula_error( str.str(), "", "", 0);
827  }
828 
829  pathfind::teleport_map allowed_teleports = ai_.get_allowed_teleports(unit_it);
830 
831  pathfind::plain_route route = ai_.shortest_path_calculator( src, dst, unit_it, allowed_teleports );
832 
833  if( route.steps.size() < 2 ) {
834  return variant();
835  }
836 
838  const ai::moves_map &possible_moves = ai_.get_possible_moves();
839  const ai::moves_map::const_iterator& p_it = possible_moves.find(unit_loc);
840  if (p_it==possible_moves.end() ) {
841  return variant();
842  }
843 
844  for (std::vector<map_location>::const_iterator loc_iter = route.steps.begin() + 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
845 
846  if (p_it->second.destinations.find(*loc_iter) != p_it->second.destinations.end() ) {
847  loc = *loc_iter;
848  } else {
849  break;
850  }
851  }
852  if (loc==map_location::null_location()) {
853  return variant();
854  }
855  return variant(std::make_shared<location_callable>(loc));
856 }
857 
858 DEFINE_WFL_FUNCTION(move, 2, 2)
859 {
860  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "move:src")).convert_to<location_callable>()->loc();
861  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "move:dst")).convert_to<location_callable>()->loc();
862  LOG_AI << "move(): " << src << ", " << dst << ")\n";
863  return variant(std::make_shared<move_callable>(src, dst));
864 }
865 
866 DEFINE_WFL_FUNCTION(move_partial, 2, 2)
867 {
868  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "move_partial:src")).convert_to<location_callable>()->loc();
869  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "move_partial:dst")).convert_to<location_callable>()->loc();
870  LOG_AI << "move_partial(): " << src << ", " << dst << ")\n";
871  return variant(std::make_shared<move_partial_callable>(src, dst));
872 }
873 
874 DEFINE_WFL_FUNCTION(set_unit_var, 3, 3)
875 {
876  return variant(std::make_shared<set_unit_var_callable>(args()[0]->evaluate(variables,add_debug_info(fdb,0,"set_unit_var:key")).as_string(), args()[1]->evaluate(variables,add_debug_info(fdb,1,"set_unit_var:value")), args()[2]->evaluate(variables,add_debug_info(fdb,2,"set_unit_var:unit_location")).convert_to<location_callable>()->loc()));
877 }
878 
879 DEFINE_WFL_FUNCTION(fallback, 0, 1)
880 {
881  UNUSED(fdb);
882  // The parameter is not used, but is accepted for legacy compatibility
883  if(args().size() == 1 && args()[0]->evaluate(variables).as_string() != "human")
884  return variant();
885  return variant(std::make_shared<fallback_callable>());
886 }
887 
888 DEFINE_WFL_FUNCTION(attack, 3, 4)
889 {
890  const map_location move_from = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "attack:move_from")).convert_to<location_callable>()->loc();
891  const map_location src = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "attack:src")).convert_to<location_callable>()->loc();
892  const map_location dst = args()[2]->evaluate(variables, add_debug_info(fdb, 2, "attack:dst")).convert_to<location_callable>()->loc();
893  const int weapon = args().size() == 4 ? args()[3]->evaluate(variables,add_debug_info(fdb,3,"attack:weapon")).as_int() : -1;
894  if(resources::gameboard->units().count(move_from) == 0 || resources::gameboard->units().count(dst) == 0) {
895  ERR_AI << "AI ERROR: Formula produced illegal attack: " << move_from << " -> " << src << " -> " << dst << std::endl;
896  return variant();
897  }
898  return variant(std::make_shared<attack_callable>(move_from, src, dst, weapon));
899 }
900 
901 DEFINE_FAI_FUNCTION(debug_label, 2, 2)
902 {
903  const args_list& arguments = args();
904  const variant var0 = arguments[0]->evaluate(variables,fdb);
905  const variant var1 = arguments[1]->evaluate(variables,fdb);
906 
907  const map_location location = var0.convert_to<location_callable>()->loc();
908  std::string text;
909  if( var1.is_string() )
910  text = var1.as_string();
911  else
912  text = var1.to_debug_string();
913 
915  std::string team_name;
916 
917  color_t color = team::get_side_color(ai_.get_side());
918 
919  const terrain_label *res;
920  res = gui->labels().set_label(location, text, ai_.get_side() - 1, team_name, color);
921  if (res && resources::recorder)
923 
924  std::vector<variant> result;
925  result.push_back(var0);
926  result.push_back(var1);
927  return variant(result);
928 }
929 
930 DEFINE_WFL_FUNCTION(is_village, 2, 3)
931 {
932  const gamemap& m = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "is_village:map")).convert_to<gamemap_callable>()->get_gamemap();
933 
934  map_location loc;
935  if(args().size() == 2) {
936  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "is_village:location")).convert_to<location_callable>()->loc();
937  } else {
938  loc = map_location( args()[1]->evaluate(variables,add_debug_info(fdb,1,"is_village:x")).as_int(),
939  args()[2]->evaluate(variables,add_debug_info(fdb,2,"is_village:y")).as_int(), wml_loc());
940  }
941  return variant(m.is_village(loc));
942 }
943 
944 DEFINE_FAI_FUNCTION(is_unowned_village, 2, 3)
945 {
946 
947  const gamemap& m = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "is_unowned_village:map")).convert_to<gamemap_callable>()->get_gamemap();
948  const std::set<map_location>& my_villages = ai_.current_team().villages();
949 
950  map_location loc;
951  if(args().size() == 2) {
952  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "is_unowned_village:location")).convert_to<location_callable>()->loc();
953  } else {
954  loc = map_location( args()[1]->evaluate(variables,add_debug_info(fdb,1,"is_unowned_village:x")).as_int(),
955  args()[2]->evaluate(variables,add_debug_info(fdb,2,"is_unowned_village:y")).as_int(), wml_loc());
956  }
957 
958  if(m.is_village(loc) && (my_villages.count(loc)==0) ) {
959  return variant(true);
960  } else {
961  return variant(false);
962  }
963 }
964 
966 {
967  variant res = args()[0]->evaluate(variables,add_debug_info(fdb,0,"unit_moves:unit_location"));
968  std::vector<variant> vars;
969  if(res.is_null()) {
970  return variant(vars);
971  }
972 
973  const map_location& loc = res.convert_to<location_callable>()->loc();
974  const ai::move_map& srcdst = ai_.get_srcdst();
975  typedef ai::move_map::const_iterator Itor;
976  std::pair<Itor,Itor> range = srcdst.equal_range(loc);
977 
978  for(Itor i = range.first; i != range.second; ++i) {
979  vars.emplace_back(std::make_shared<location_callable>(i->second));
980  }
981 
982  return variant(vars);
983 }
984 
985 DEFINE_WFL_FUNCTION(units_can_reach, 2, 2)
986 {
987  std::vector<variant> vars;
988  variant dstsrc_var = args()[0]->evaluate(variables,add_debug_info(fdb,0,"units_can_reach:possible_move_list"));
989  const ai::move_map& dstsrc = dstsrc_var.convert_to<move_map_callable>()->dstsrc();
990  std::pair<ai::move_map::const_iterator,ai::move_map::const_iterator> range =
991  dstsrc.equal_range(args()[1]->evaluate(variables, add_debug_info(fdb, 1, "units_can_reach:possible_move_list")).convert_to<location_callable>()->loc());
992  while(range.first != range.second) {
993  unit_map::const_iterator un = resources::gameboard->units().find(range.first->second);
994  assert(un != resources::gameboard->units().end());
995  vars.emplace_back(std::make_shared<unit_callable>(*un));
996  ++range.first;
997  }
998 
999  return variant(vars);
1000 }
1001 
1002 DEFINE_FAI_FUNCTION(is_avoided_location, 1, 1)
1003 {
1004  variant res = args()[0]->evaluate(variables,add_debug_info(fdb,0,"is_avoided_location:location"));
1005  if(res.is_null()) {
1006  return variant();
1007  }
1008  const map_location& loc = res.convert_to<location_callable>()->loc();
1009  return variant(ai_.get_avoid().match(loc));
1010 }
1011 
1012 DEFINE_WFL_FUNCTION(max_possible_damage, 2, 2)
1013 {
1014  variant u1 = args()[0]->evaluate(variables,add_debug_info(fdb,0,"max_possible_damage:unit1"));
1015  variant u2 = args()[1]->evaluate(variables,add_debug_info(fdb,1,"max_possible_damage:unit2"));
1016  if(u1.is_null() || u2.is_null()) {
1017  return variant();
1018  }
1019 
1020  unit_adapter u_attacker(u1), u_defender(u2);
1021  int best = 0;
1022  for(const attack_type& atk : u_attacker.attacks()) {
1023  const int dmg = round_damage(atk.damage(), u_defender.damage_from(atk), 100) * atk.num_attacks();
1024  if(dmg > best)
1025  best = dmg;
1026  }
1027  return variant(best);
1028 }
1029 
1030 namespace {
1031  std::pair<int, int> best_melee_and_ranged_attacks(unit_adapter attacker, unit_adapter defender) {
1032  int highest_melee_damage = 0;
1033  int highest_ranged_damage = 0;
1034 
1035  for (const attack_type &attack : attacker.attacks()) {
1036  const int dmg = round_damage(attack.damage(), defender.damage_from(attack), 100) * attack.num_attacks();
1037  if (attack.range() == "melee") {
1038  highest_melee_damage = std::max(highest_melee_damage, dmg);
1039  } else {
1040  highest_ranged_damage = std::max(highest_ranged_damage, dmg);
1041  }
1042  }
1043 
1044  return std::pair(highest_melee_damage, highest_ranged_damage);
1045  }
1046 }
1047 
1048 DEFINE_WFL_FUNCTION(max_possible_damage_with_retaliation, 2, 2)
1049 {
1050  variant u1 = args()[0]->evaluate(variables,add_debug_info(fdb,0,"max_possible_damage_with_retaliation:unit1"));
1051  variant u2 = args()[1]->evaluate(variables,add_debug_info(fdb,1,"max_possible_damage_with_retaliation:unit2"));
1052 
1053  if(u1.is_null() || u2.is_null()) {
1054  return variant();
1055  }
1056 
1057  unit_adapter attacker(u1);
1058  unit_adapter defender(u2);
1059 
1060  // find max damage inflicted by attacker and by defender to the attacker
1061  std::pair<int, int> best_attacker_attacks = best_melee_and_ranged_attacks(attacker, defender);
1062  std::pair<int, int> best_defender_attacks = best_melee_and_ranged_attacks(defender, attacker);
1063 
1064  std::vector<variant> vars;
1065  vars.emplace_back(best_attacker_attacks.first);
1066  vars.emplace_back(best_attacker_attacks.second);
1067  vars.emplace_back(best_defender_attacks.first);
1068  vars.emplace_back(best_defender_attacks.second);
1069 
1070  return variant(vars);
1071 }
1072 
1073 template<typename T>
1074 class ai_formula_function : public formula_function {
1075 protected:
1076  formula_ai& ai_;
1077 public:
1078  ai_formula_function(const std::string& name, ai::formula_ai& ai) : formula_function(name), ai_(ai) {}
1079  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const {
1080  return std::make_shared<T>(args, ai_);
1081  }
1082 };
1083 
1084 }
1085 
1086 // This macro is for functions taking an additional formula_ai argument.
1087 // Functions using the other macro could potentially be made core.
1088 #define DECLARE_FAI_FUNCTION(name) \
1089  add_function(#name, std::make_shared<ai_formula_function<name##_function>>(#name, ai))
1090 
1093 {
1094  function_symbol_table& functions_table = *this;
1095  DECLARE_WFL_FUNCTION(outcomes);
1096  //DECLARE_FAI_FUNCTION(evaluate_for_position);
1097  DECLARE_WFL_FUNCTION(move);
1098  DECLARE_WFL_FUNCTION(move_partial);
1099  DECLARE_WFL_FUNCTION(attack);
1100  DECLARE_FAI_FUNCTION(rate_action);
1101  DECLARE_WFL_FUNCTION(recall);
1102  DECLARE_WFL_FUNCTION(recruit);
1103  DECLARE_FAI_FUNCTION(is_avoided_location);
1104  DECLARE_WFL_FUNCTION(is_village);
1105  DECLARE_FAI_FUNCTION(is_unowned_village);
1107  DECLARE_WFL_FUNCTION(set_unit_var);
1108  DECLARE_WFL_FUNCTION(fallback);
1109  DECLARE_WFL_FUNCTION(units_can_reach);
1110  DECLARE_FAI_FUNCTION(debug_label);
1111  DECLARE_WFL_FUNCTION(max_possible_damage);
1112  DECLARE_WFL_FUNCTION(max_possible_damage_with_retaliation);
1113  DECLARE_FAI_FUNCTION(next_hop);
1114  DECLARE_WFL_FUNCTION(castle_locs);
1115  DECLARE_WFL_FUNCTION(timeofday_modifier);
1116  DECLARE_FAI_FUNCTION(distance_to_nearest_unowned_village);
1117  DECLARE_FAI_FUNCTION(shortest_path);
1118  DECLARE_FAI_FUNCTION(simplest_path);
1119  DECLARE_FAI_FUNCTION(nearest_keep);
1120  DECLARE_FAI_FUNCTION(suitable_keep);
1121  DECLARE_WFL_FUNCTION(nearest_loc);
1122  DECLARE_FAI_FUNCTION(find_shroud);
1123  DECLARE_FAI_FUNCTION(close_enemies);
1124  DECLARE_WFL_FUNCTION(calculate_outcome);
1125  DECLARE_FAI_FUNCTION(run_file);
1126  DECLARE_FAI_FUNCTION(calculate_map_ownership);
1127 }
1128 #undef DECLARE_WFL_FUNCTION
1129 
1130 }
Defines formula ai.
#define ERR_AI
bool is_castle() const
Definition: terrain.hpp:141
std::vector< double > hp_dist
Resulting probability distribution (might be not as large as max_hp)
unit_iterator end()
Definition: map.hpp:428
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:90
const std::vector< node > & nodes
int movement_cost_
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:474
virtual const unit_map & units() const override
Definition: game_board.hpp:111
This class represents a single unit of a specific type.
Definition: unit.hpp:120
#define LOG_AI
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit&#39;s movement cost on a particular terrain.
Definition: unit.hpp:1431
const combatant & get_attacker_combatant(const combatant *prev_def=nullptr)
Get the simulation results.
Definition: attack.cpp:462
#define a
static const int UNREACHABLE
Magic value that signifies a hex is unreachable.
Definition: movetype.hpp:175
formula_debugger * add_debug_info(formula_debugger *fdb, int arg_number, const std::string &f_name)
General purpose widgets.
virtual const gamemap & map() const override
Definition: game_board.hpp:101
unit_iterator begin()
Definition: map.hpp:418
int wml_x() const
Definition: location.hpp:152
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:97
STL namespace.
int resistance_against(const attack_type &attack) const
Returns the resistance against the indicated attack.
Definition: movetype.hpp:295
Replay control code.
const std::vector< std::string > items
const unit * unit_
ai_function_symbol_table(ai::formula_ai &ai)
#define WRN_AI
A single unit type that the player may recruit.
Definition: types.hpp:44
formula_ai & ai_
map_location loc_
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
#define b
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
unsigned in
If equal to search_counter, the node is off the list.
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
int damage_from(const attack_type &attack, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr) const
Calculates the damage this unit would take from a certain attack.
Definition: unit.hpp:963
std::vector< map_location > steps
Definition: pathfind.hpp:134
int w() const
Effective map width.
Definition: map.hpp:49
const movetype & movement_type() const
Definition: types.hpp:182
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
const terrain_label * set_label(const map_location &loc, const t_string &text, const int creator=-1, const std::string &team="", const color_t color=font::NORMAL_COLOR, const bool visible_in_fog=true, const bool visible_in_shroud=false, const bool immutable=false, const std::string &category="", const t_string &tooltip="")
Definition: label.cpp:146
int wml_y() const
Definition: location.hpp:153
bool valid() const
Definition: location.hpp:88
game_board * gameboard
Definition: resources.cpp:20
std::shared_ptr< function_expression > function_expression_ptr
Definition: function.hpp:172
Encapsulates the map of the game.
Definition: map.hpp:170
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:172
int h
std::string path
Definition: game_config.cpp:38
map_display and display: classes which take care of displaying the map and game-data on the screen...
replay * recorder
Definition: resources.cpp:28
Function which only uses terrain, ignoring shroud, enemies, etc.
Definition: pathfind.hpp:240
std::size_t count(const map_location &loc) const
Definition: map.hpp:413
#define DEFINE_FAI_FUNCTION(name, min_args, max_args)
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Definition: filesystem.cpp:995
Encapsulates the map of the game.
Definition: location.hpp:37
int round_damage(int base_damage, int bonus, int divisor)
round (base_damage * bonus / divisor) to the closest integer, but up or down towards base_damage ...
Definition: math.hpp:79
unit_iterator find(std::size_t id)
Definition: map.cpp:309
#define UNUSED(x)
Definition: global.hpp:30
pointer get_shared_ptr() const
This is exactly the same as operator-> but it&#39;s slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:217
std::size_t i
Definition: function.cpp:940
std::string get_wml_location(const std::string &filename, const std::string &current_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn&#39;t pres...
static bool operator<(const placing_info &a, const placing_info &b)
Definition: game_state.cpp:139
#define DECLARE_FAI_FUNCTION(name)
Default AI contexts.
attack_itors attacks()
Gets an iterator over this unit&#39;s attacks.
Definition: unit.hpp:918
Declarations for File-IO.
int w
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
static lg::log_domain log_formula_ai("ai/engine/fai")
int movement_cost(const t_translation::terrain_code &terrain, bool slowed=false) const
Returns the cost to move through the indicated terrain.
Definition: movetype.hpp:281
#define DECLARE_WFL_FUNCTION(name)
Declares a function name in the local function table functions_table.
Definition: function.hpp:47
#define next(ls)
Definition: llex.cpp:32
static const unit_type & get_unit_type(const std::string &type_id)
Converts a string ID to a unit_type.
Definition: unit.cpp:186
bool is_village(const map_location &loc) const
Definition: map.cpp:65
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
To store label data Class implements logic for rendering.
Definition: label.hpp:109
bool is_fearless() const
Gets whether this unit is fearless - ie, unaffected by time of day.
Definition: unit.hpp:1238
double t
Definition: astarsearch.cpp:64
UNIT_ALIGNMENT alignment() const
The alignment of this unit.
Definition: unit.hpp:468
Definition: contexts.hpp:43
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:236
static color_t get_side_color(int side)
Definition: team.cpp:947
#define DEFINE_WFL_FUNCTION(name, min_args, max_args)
Helper macro to declare an associated class for a WFL function.
Definition: function.hpp:27
const unit_type * unit_type_
Standard logging facilities (interface).
static config unit_moves(reports::context &rc, const unit *u, bool is_visible_unit)
Definition: reports.cpp:649
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
map_labels & labels()
Definition: display.cpp:2524
const_attack_itors attacks() const
Definition: types.cpp:512
boost::iterator_range< boost::indirect_iterator< attack_list::const_iterator > > const_attack_itors
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
static map_location::DIRECTION n
int h() const
Effective map height.
Definition: map.hpp:52
This module contains various pathfinding functions and utilities.
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
void add_label(const terrain_label *)
Definition: replay.cpp:269
int combat_modifier(const unit_map &units, const gamemap &map, const map_location &loc, unit_type::ALIGNMENT alignment, bool is_fearless)
Returns the amount that a unit&#39;s damage should be multiplied by due to the current time of day...
Definition: attack.cpp:1595
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:21