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