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