The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
function_table.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2017 by Bartosz Waresiak <dragonking@o2.pl>
3  Part of the Battle for Wesnoth Project http://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[0]);
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  size_t number_of_teams = units_input.num_elements();
277 
278  std::vector< std::vector<int> > scores( number_of_teams );
279 
280  for( 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(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(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  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  size_t current_side = ai_.get_side() - 1 ;
320 
321  std::vector<int> enemies;
322  std::vector<int> allies;
323 
324  for(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(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"
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 
447  map_location adj[6];
448  get_adjacent_tiles(loc, adj);
449 
450  for(int n = 0; n != 6; ++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();
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  size_t range = static_cast<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(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(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 /* // TODO: Not really sure what this was meant to do or if there's any reason left to keep it...
696 DEFINE_FAI_FUNCTION(evaluate_for_position, 2, 2)
697 {
698  variant position = args()[0]->evaluate(variables,add_debug_info(fdb,0,"evaluate_for_position:position"));
699  ai_.store_outcome_position(position);
700  position_callable* pos = position.convert_to<position_callable>();
701  position_callable::swapper swapper(ai_, *pos);
702  return args()[1]->evaluate(variables,add_debug_info(fdb,1,"evaluate_for_position:formula"));
703 }
704 */
705 
706 DEFINE_FAI_FUNCTION(rate_action, 1, 1)
707 {
708  variant act = args()[0]->evaluate(variables,add_debug_info(fdb,0,"rate_action:action"));
709  auto analysis = act.convert_to<ai::attack_analysis>();
710 
711  return variant(analysis->rating(ai_.get_aggression(),ai_)*1000,variant::DECIMAL_VARIANT);
712 }
713 
714 DEFINE_WFL_FUNCTION(recall, 1, 2)
715 {
716  const std::string id = args()[0]->evaluate(variables,add_debug_info(fdb,0,"recall:id")).as_string();
717  map_location loc;
718  if(args().size() >= 2) {
719  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "recall:location")).convert_to<location_callable>()->loc();
720  }
721 
722  return variant(std::make_shared<recall_callable>(loc, id));
723 }
724 
725 DEFINE_WFL_FUNCTION(recruit, 1, 2)
726 {
727  const std::string type = args()[0]->evaluate(variables,add_debug_info(fdb,0,"recruit:type")).as_string();
728  map_location loc;
729  if(args().size() >= 2) {
730  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "recruit:location")).convert_to<location_callable>()->loc();
731  }
732 
733  return variant(std::make_shared<recruit_callable>(loc, type));
734 }
735 
736 DEFINE_FAI_FUNCTION(shortest_path, 2, 3)
737 {
738 
739  std::vector<variant> locations;
740 
741  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "shortest_path:src")).convert_to<location_callable>()->loc();
742  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "shortest_path:dst")).convert_to<location_callable>()->loc();
743  map_location unit_loc;
744 
745  if( src == dst )
746  return variant(locations);
747 
748  if(args().size() > 2)
749  unit_loc = args()[2]->evaluate(variables,add_debug_info(fdb,2,"shortest_path:unit_location")).convert_to<location_callable>()->loc();
750  else
751  unit_loc = src;
752 
753  unit_map::iterator unit_it = resources::gameboard->units().find(unit_loc);
754 
755  if( unit_it == resources::gameboard->units().end() ) {
756  std::ostringstream str;
757  str << "shortest_path function: expected unit at location (" << (unit_loc.wml_x()) << "," << (unit_loc.wml_y()) << ")";
758  throw formula_error( str.str(), "", "", 0);
759  }
760 
761  pathfind::teleport_map allowed_teleports = ai_.get_allowed_teleports(unit_it);
762 
763  pathfind::plain_route route = ai_.shortest_path_calculator( src, dst, unit_it, allowed_teleports );
764 
765  if( route.steps.size() < 2 ) {
766  return variant(locations);
767  }
768 
769  for (std::vector<map_location>::const_iterator loc_iter = route.steps.begin() + 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
770  locations.emplace_back(std::make_shared<location_callable>(*loc_iter));
771  }
772 
773  return variant(locations);
774 }
775 
776 DEFINE_FAI_FUNCTION(simplest_path, 2, 3)
777 {
778  std::vector<variant> locations;
779 
780  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "simplest_path:src")).convert_to<location_callable>()->loc();
781  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "simplest_path:dst")).convert_to<location_callable>()->loc();
782  map_location unit_loc;
783 
784  if( src == dst )
785  return variant(locations);
786 
787  if(args().size() > 2)
788  unit_loc = args()[2]->evaluate(variables, add_debug_info(fdb, 2, "simplest_path:unit_location")).convert_to<location_callable>()->loc();
789  else
790  unit_loc = src;
791 
792  unit_map::iterator unit_it = resources::gameboard->units().find(unit_loc);
793 
794  if( unit_it == resources::gameboard->units().end() ) {
795  std::ostringstream str;
796  str << "simplest_path function: expected unit at location (" << (unit_loc.wml_x()) << "," << (unit_loc.wml_y()) << ")";
797  throw formula_error( str.str(), "", "", 0);
798  }
799 
800  pathfind::teleport_map allowed_teleports = ai_.get_allowed_teleports(unit_it);
801 
803 
804  pathfind::plain_route route = pathfind::a_star_search(src, dst, 1000.0, em_calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
805 
806  if( route.steps.size() < 2 ) {
807  return variant(locations);
808  }
809 
810  for (std::vector<map_location>::const_iterator loc_iter = route.steps.begin() + 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
811  if (unit_it->movement_cost((resources::gameboard->map())[*loc_iter]) < movetype::UNREACHABLE )
812  locations.emplace_back(std::make_shared<location_callable>(*loc_iter));
813  else
814  break;
815  }
816 
817  return variant(locations);
818 }
819 
820 DEFINE_FAI_FUNCTION(next_hop, 2, 3)
821 {
822 
823  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "next_hop:src")).convert_to<location_callable>()->loc();
824  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "next_hop:dst")).convert_to<location_callable>()->loc();
825  map_location unit_loc;
826 
827  if( src == dst )
828  return variant();
829 
830  if(args().size() > 2)
831  unit_loc = args()[2]->evaluate(variables, add_debug_info(fdb, 2, "next_hop:unit_location")).convert_to<location_callable>()->loc();
832  else
833  unit_loc = src;
834 
835  unit_map::iterator unit_it = resources::gameboard->units().find(unit_loc);
836 
837  if( unit_it == resources::gameboard->units().end() ) {
838  std::ostringstream str;
839  str << "next_hop function: expected unit at location (" << (unit_loc.wml_x()) << "," << (unit_loc.wml_y()) << ")";
840  throw formula_error( str.str(), "", "", 0);
841  }
842 
843  pathfind::teleport_map allowed_teleports = ai_.get_allowed_teleports(unit_it);
844 
845  pathfind::plain_route route = ai_.shortest_path_calculator( src, dst, unit_it, allowed_teleports );
846 
847  if( route.steps.size() < 2 ) {
848  return variant();
849  }
850 
852  const ai::moves_map &possible_moves = ai_.get_possible_moves();
853  const ai::moves_map::const_iterator& p_it = possible_moves.find(unit_loc);
854  if (p_it==possible_moves.end() ) {
855  return variant();
856  }
857 
858  for (std::vector<map_location>::const_iterator loc_iter = route.steps.begin() + 1 ; loc_iter !=route.steps.end(); ++loc_iter) {
859 
860  if (p_it->second.destinations.find(*loc_iter) != p_it->second.destinations.end() ) {
861  loc = *loc_iter;
862  } else {
863  break;
864  }
865  }
866  if (loc==map_location::null_location()) {
867  return variant();
868  }
869  return variant(std::make_shared<location_callable>(loc));
870 }
871 
872 DEFINE_WFL_FUNCTION(move, 2, 2)
873 {
874  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "move:src")).convert_to<location_callable>()->loc();
875  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "move:dst")).convert_to<location_callable>()->loc();
876  LOG_AI << "move(): " << src << ", " << dst << ")\n";
877  return variant(std::make_shared<move_callable>(src, dst));
878 }
879 
880 DEFINE_WFL_FUNCTION(move_partial, 2, 2)
881 {
882  const map_location src = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "move_partial:src")).convert_to<location_callable>()->loc();
883  const map_location dst = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "move_partial:dst")).convert_to<location_callable>()->loc();
884  LOG_AI << "move_partial(): " << src << ", " << dst << ")\n";
885  return variant(std::make_shared<move_partial_callable>(src, dst));
886 }
887 
888 DEFINE_WFL_FUNCTION(set_unit_var, 3, 3)
889 {
890  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()));
891 }
892 
893 DEFINE_WFL_FUNCTION(fallback, 0, 1)
894 {
895  (void)fdb;
896  // The parameter is not used, but is accepted for legacy compatibility
897  if(args().size() == 1 && args()[0]->evaluate(variables).as_string() != "human")
898  return variant();
899  return variant(std::make_shared<fallback_callable>());
900 }
901 
902 DEFINE_WFL_FUNCTION(attack, 3, 4)
903 {
904  const map_location move_from = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "attack:move_from")).convert_to<location_callable>()->loc();
905  const map_location src = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "attack:src")).convert_to<location_callable>()->loc();
906  const map_location dst = args()[2]->evaluate(variables, add_debug_info(fdb, 2, "attack:dst")).convert_to<location_callable>()->loc();
907  const int weapon = args().size() == 4 ? args()[3]->evaluate(variables,add_debug_info(fdb,3,"attack:weapon")).as_int() : -1;
908  if(resources::gameboard->units().count(move_from) == 0 || resources::gameboard->units().count(dst) == 0) {
909  ERR_AI << "AI ERROR: Formula produced illegal attack: " << move_from << " -> " << src << " -> " << dst << std::endl;
910  return variant();
911  }
912  return variant(std::make_shared<attack_callable>(move_from, src, dst, weapon));
913 }
914 
915 DEFINE_FAI_FUNCTION(debug_label, 2, 2)
916 {
917  const args_list& arguments = args();
918  const variant var0 = arguments[0]->evaluate(variables,fdb);
919  const variant var1 = arguments[1]->evaluate(variables,fdb);
920 
921  const map_location location = var0.convert_to<location_callable>()->loc();
922  std::string text;
923  if( var1.is_string() )
924  text = var1.as_string();
925  else
926  text = var1.to_debug_string();
927 
929  std::string team_name;
930 
931  color_t color = team::get_side_rgb(ai_.get_side());
932 
933  const terrain_label *res;
934  res = gui->labels().set_label(location, text, ai_.get_side() - 1, team_name, color);
935  if (res && resources::recorder)
937 
938  std::vector<variant> result;
939  result.push_back(var0);
940  result.push_back(var1);
941  return variant(result);
942 }
943 
944 
945 DEFINE_WFL_FUNCTION(is_village, 2, 3)
946 {
947  const gamemap& m = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "is_village:map")).convert_to<gamemap_callable>()->get_gamemap();
948 
949  map_location loc;
950  if(args().size() == 2) {
951  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "is_village:location")).convert_to<location_callable>()->loc();
952  } else {
953  loc = map_location( args()[1]->evaluate(variables,add_debug_info(fdb,1,"is_village:x")).as_int(),
954  args()[2]->evaluate(variables,add_debug_info(fdb,2,"is_village:y")).as_int(), wml_loc());
955  }
956  return variant(m.is_village(loc));
957 }
958 
959 DEFINE_FAI_FUNCTION(is_unowned_village, 2, 3)
960 {
961 
962  const gamemap& m = args()[0]->evaluate(variables, add_debug_info(fdb, 0, "is_unowned_village:map")).convert_to<gamemap_callable>()->get_gamemap();
963  const std::set<map_location>& my_villages = ai_.current_team().villages();
964 
965  map_location loc;
966  if(args().size() == 2) {
967  loc = args()[1]->evaluate(variables, add_debug_info(fdb, 1, "is_unowned_village:location")).convert_to<location_callable>()->loc();
968  } else {
969  loc = map_location( args()[1]->evaluate(variables,add_debug_info(fdb,1,"is_unowned_village:x")).as_int(),
970  args()[2]->evaluate(variables,add_debug_info(fdb,2,"is_unowned_village:y")).as_int(), wml_loc());
971  }
972 
973  if(m.is_village(loc) && (my_villages.count(loc)==0) ) {
974  return variant(true);
975  } else {
976  return variant(false);
977  }
978 }
979 
981 {
982  variant res = args()[0]->evaluate(variables,add_debug_info(fdb,0,"unit_moves:unit_location"));
983  std::vector<variant> vars;
984  if(res.is_null()) {
985  return variant(vars);
986  }
987 
988  const map_location& loc = res.convert_to<location_callable>()->loc();
989  const ai::move_map& srcdst = ai_.get_srcdst();
990  typedef ai::move_map::const_iterator Itor;
991  std::pair<Itor,Itor> range = srcdst.equal_range(loc);
992 
993  for(Itor i = range.first; i != range.second; ++i) {
994  vars.emplace_back(std::make_shared<location_callable>(i->second));
995  }
996 
997  return variant(vars);
998 }
999 
1000 DEFINE_WFL_FUNCTION(units_can_reach, 2, 2)
1001 {
1002  std::vector<variant> vars;
1003  variant dstsrc_var = args()[0]->evaluate(variables,add_debug_info(fdb,0,"units_can_reach:possible_move_list"));
1004  const ai::move_map& dstsrc = dstsrc_var.convert_to<move_map_callable>()->dstsrc();
1005  std::pair<ai::move_map::const_iterator,ai::move_map::const_iterator> range =
1006  dstsrc.equal_range(args()[1]->evaluate(variables, add_debug_info(fdb, 1, "units_can_reach:possible_move_list")).convert_to<location_callable>()->loc());
1007  while(range.first != range.second) {
1008  unit_map::const_iterator un = resources::gameboard->units().find(range.first->second);
1009  assert(un != resources::gameboard->units().end());
1010  vars.emplace_back(std::make_shared<unit_callable>(*un));
1011  ++range.first;
1012  }
1013 
1014  return variant(vars);
1015 }
1016 
1017 DEFINE_FAI_FUNCTION(is_avoided_location, 1, 1)
1018 {
1019  variant res = args()[0]->evaluate(variables,add_debug_info(fdb,0,"is_avoided_location:location"));
1020  if(res.is_null()) {
1021  return variant();
1022  }
1023  const map_location& loc = res.convert_to<location_callable>()->loc();
1024  return variant(ai_.get_avoid().match(loc));
1025 }
1026 
1027 DEFINE_WFL_FUNCTION(max_possible_damage, 2, 2)
1028 {
1029  variant u1 = args()[0]->evaluate(variables,add_debug_info(fdb,0,"max_possible_damage:unit1"));
1030  variant u2 = args()[1]->evaluate(variables,add_debug_info(fdb,1,"max_possible_damage:unit2"));
1031  if(u1.is_null() || u2.is_null()) {
1032  return variant();
1033  }
1034 
1035  unit_adapter u_attacker(u1), u_defender(u2);
1036  int best = 0;
1037  for(const attack_type& atk : u_attacker.attacks()) {
1038  const int dmg = round_damage(atk.damage(), u_defender.damage_from(atk), 100) * atk.num_attacks();
1039  if(dmg > best)
1040  best = dmg;
1041  }
1042  return variant(best);
1043 }
1044 
1045 namespace {
1046  std::pair<int, int> best_melee_and_ranged_attacks(unit_adapter attacker, unit_adapter defender) {
1047  int highest_melee_damage = 0;
1048  int highest_ranged_damage = 0;
1049 
1050  for (const attack_type &attack : attacker.attacks()) {
1051  const int dmg = round_damage(attack.damage(), defender.damage_from(attack), 100) * attack.num_attacks();
1052  if (attack.range() == "melee") {
1053  highest_melee_damage = std::max(highest_melee_damage, dmg);
1054  } else {
1055  highest_ranged_damage = std::max(highest_ranged_damage, dmg);
1056  }
1057  }
1058 
1059  return std::make_pair(highest_melee_damage, highest_ranged_damage);
1060  }
1061 }
1062 
1063 DEFINE_WFL_FUNCTION(max_possible_damage_with_retaliation, 2, 2)
1064 {
1065  variant u1 = args()[0]->evaluate(variables,add_debug_info(fdb,0,"max_possible_damage_with_retaliation:unit1"));
1066  variant u2 = args()[1]->evaluate(variables,add_debug_info(fdb,1,"max_possible_damage_with_retaliation:unit2"));
1067 
1068  if(u1.is_null() || u2.is_null()) {
1069  return variant();
1070  }
1071 
1072  unit_adapter attacker(u1);
1073  unit_adapter defender(u2);
1074 
1075  // find max damage inflicted by attacker and by defender to the attacker
1076  std::pair<int, int> best_attacker_attacks = best_melee_and_ranged_attacks(attacker, defender);
1077  std::pair<int, int> best_defender_attacks = best_melee_and_ranged_attacks(defender, attacker);
1078 
1079  std::vector<variant> vars;
1080  vars.emplace_back(best_attacker_attacks.first);
1081  vars.emplace_back(best_attacker_attacks.second);
1082  vars.emplace_back(best_defender_attacks.first);
1083  vars.emplace_back(best_defender_attacks.second);
1084 
1085  return variant(vars);
1086 }
1087 
1088 template<typename T>
1089 class ai_formula_function : public formula_function {
1090 protected:
1091  formula_ai& ai_;
1092 public:
1093  ai_formula_function(const std::string& name, ai::formula_ai& ai) : formula_function(name), ai_(ai) {}
1094  function_expression_ptr generate_function_expression(const std::vector<expression_ptr>& args) const {
1095  return function_expression_ptr(new T(args, ai_));
1096  }
1097 };
1098 
1099 }
1100 
1101 // This macro is for functions taking an additional formula_ai argument.
1102 // Functions using the other macro could potentially be made core.
1103 #define DECLARE_FAI_FUNCTION(name) \
1104  add_function(#name, std::make_shared<ai_formula_function<name##_function>>(#name, ai))
1105 
1108 {
1109  function_symbol_table& functions_table = *this;
1110  DECLARE_WFL_FUNCTION(outcomes);
1111  //DECLARE_FAI_FUNCTION(evaluate_for_position);
1112  DECLARE_WFL_FUNCTION(move);
1113  DECLARE_WFL_FUNCTION(move_partial);
1114  DECLARE_WFL_FUNCTION(attack);
1115  DECLARE_FAI_FUNCTION(rate_action);
1116  DECLARE_WFL_FUNCTION(recall);
1117  DECLARE_WFL_FUNCTION(recruit);
1118  DECLARE_FAI_FUNCTION(is_avoided_location);
1119  DECLARE_WFL_FUNCTION(is_village);
1120  DECLARE_FAI_FUNCTION(is_unowned_village);
1122  DECLARE_WFL_FUNCTION(set_unit_var);
1123  DECLARE_WFL_FUNCTION(fallback);
1124  DECLARE_WFL_FUNCTION(units_can_reach);
1125  DECLARE_FAI_FUNCTION(debug_label);
1126  DECLARE_WFL_FUNCTION(max_possible_damage);
1127  DECLARE_WFL_FUNCTION(max_possible_damage_with_retaliation);
1128  DECLARE_FAI_FUNCTION(next_hop);
1129  DECLARE_WFL_FUNCTION(castle_locs);
1130  DECLARE_WFL_FUNCTION(timeofday_modifier);
1131  DECLARE_FAI_FUNCTION(distance_to_nearest_unowned_village);
1132  DECLARE_FAI_FUNCTION(shortest_path);
1133  DECLARE_FAI_FUNCTION(simplest_path);
1134  DECLARE_FAI_FUNCTION(nearest_keep);
1135  DECLARE_FAI_FUNCTION(suitable_keep);
1136  DECLARE_WFL_FUNCTION(nearest_loc);
1137  DECLARE_FAI_FUNCTION(find_shroud);
1138  DECLARE_FAI_FUNCTION(close_enemies);
1139  DECLARE_WFL_FUNCTION(calculate_outcome);
1140  DECLARE_FAI_FUNCTION(run_file);
1141  DECLARE_FAI_FUNCTION(calculate_map_ownership);
1142 }
1143 #undef DECLARE_WFL_FUNCTION
1144 
1145 }
Defines formula ai.
#define ERR_AI
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:197
virtual const unit_map & units() const
Definition: game_board.hpp:97
std::vector< double > hp_dist
Resulting probability distribution (might be not as large as max_hp)
unit_iterator end()
Definition: map.hpp:415
std::vector< char_t > string
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
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_
This class represents a single unit of a specific type.
Definition: unit.hpp:100
#define LOG_AI
size_t count(const map_location &loc) const
Definition: map.hpp:400
const combatant & get_attacker_combatant(const combatant *prev_def=nullptr)
Get the simulation results.
Definition: attack.cpp:452
#define a
static const int UNREACHABLE
Magic value that signifies a hex is unreachable.
Definition: movetype.hpp:83
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.hpp:299
formula_debugger * add_debug_info(formula_debugger *fdb, int arg_number, const std::string &f_name)
bool is_village(const map_location &loc) const
Definition: map.cpp:66
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:42
const movetype & movement_type() const
Definition: types.hpp:175
const_attack_itors attacks() const
Definition: types.cpp:492
General purpose widgets.
unit_iterator begin()
Definition: map.hpp:405
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:55
STL namespace.
Replay control code.
int wml_y() const
Definition: location.hpp:116
boost::iterator_range< boost::indirect_iterator< attack_list::const_iterator >> const_attack_itors
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:43
formula_ai & ai_
map_location loc_
#define b
unsigned in
If equal to search_counter, the node is off the list.
std::multimap< map_location, map_location > move_map
The standard way in which a map of possible moves is recorded.
Definition: game_info.hpp:39
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
static color_t get_side_rgb(int side)
Definition: team.hpp:365
std::vector< map_location > steps
Definition: pathfind.hpp:134
int wml_x() const
Definition: location.hpp:115
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
bool valid() const
Definition: location.hpp:72
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
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.hpp:382
int w() const
Effective map width.
Definition: map.hpp:90
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:34
Computes the statistics of a battle between an attacker and a defender unit.
Definition: attack.hpp:170
int h
std::string path
Definition: game_config.cpp:56
map_display and display: classes which take care of displaying the map and game-data on the screen...
replay * recorder
Definition: resources.cpp:29
Function which only uses terrain, ignoring shroud, enemies, etc.
Definition: pathfind.hpp:240
const terrain_type & get_terrain_info(const t_translation::terrain_code &terrain) const
Definition: map.cpp:98
#define DEFINE_FAI_FUNCTION(name, min_args, max_args)
static const map_location & null_location()
Definition: location.hpp:220
const std::set< map_location > & villages() const
Definition: team.hpp:183
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:133
std::string read_file(const std::string &fname)
Basic disk I/O - read file.
Encapsulates the map of the game.
Definition: location.hpp:40
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const size_t width, const size_t height, const teleport_map *teleports, bool border)
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:58
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
static config unit_moves(reports::context &rc, const unit *u)
Definition: reports.cpp:581
int resistance_against(const attack_type &attack) const
Returns the resistance against the indicated attack.
Definition: movetype.hpp:211
int h() const
Effective map height.
Definition: map.hpp:93
virtual const team & current_team() const override
Definition: contexts.hpp:536
std::string get_wml_location(const std::string &filename, const std::string &current_dir=std::string())
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
static bool operator<(const placing_info &a, const placing_info &b)
Definition: game_state.cpp:132
#define DECLARE_FAI_FUNCTION(name)
Default AI contexts.
attack_itors attacks()
Gets an iterator over this unit's attacks.
Definition: unit.hpp:785
size_t i
Definition: function.cpp:933
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
Definition: unit.hpp:1224
Declarations for File-IO.
int w
static lg::log_domain log_formula_ai("ai/engine/fai")
#define DECLARE_WFL_FUNCTION(name)
Declares a function name in the local function table functions_table.
Definition: function.hpp:47
virtual const gamemap & map() const
Definition: game_board.hpp:96
#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:198
To store label data Class implements logic for rendering.
Definition: label.hpp:107
double t
Definition: astarsearch.cpp:64
Definition: contexts.hpp:42
bool find(E event, F functor)
Tests whether an event handler is available.
#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_
bool is_fearless() const
Gets whether this unit is fearless - ie, unaffected by time of day.
Definition: unit.hpp:1038
Standard logging facilities (interface).
unit_type::ALIGNMENT alignment() const
The alignment of this unit.
Definition: unit.hpp:370
Object which contains all the possible locations a unit can move to, with associated best routes to t...
Definition: pathfind.hpp:70
Container associating units to locations.
Definition: map.hpp:99
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
map_labels & labels()
Definition: display.cpp:2534
unit_iterator find(size_t id)
Definition: map.cpp:311
int damage_from(const attack_type &attack, bool attacker, const map_location &loc) const
Calculates the damage this unit would take from a certain attack.
Definition: unit.hpp:828
bool is_castle() const
Definition: terrain.hpp:63
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:157
static map_location::DIRECTION n
This module contains various pathfinding functions and utilities.
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
unit_map * units
Definition: resources.cpp:34
void add_label(const terrain_label *)
Definition: replay.cpp:262
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's damage should be multiplied by due to the current time of day...
Definition: attack.cpp:1578
std::shared_ptr< formula > formula_ptr
Definition: formula_fwd.hpp:21