The Battle for Wesnoth  1.15.0-dev
abilities.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2018 by Dominic Bolin <dominic.bolin@exong.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Manage unit-abilities, like heal, cure, and weapon_specials.
18  */
19 
20 #include "display.hpp"
21 #include "display_context.hpp"
22 #include "font/text_formatting.hpp"
23 #include "game_board.hpp"
24 #include "lexical_cast.hpp"
25 #include "log.hpp"
26 #include "map/map.hpp"
27 #include "resources.hpp"
28 #include "team.hpp"
29 #include "terrain/filter.hpp"
30 #include "units/unit.hpp"
31 #include "units/abilities.hpp"
32 #include "units/filter.hpp"
33 #include "units/map.hpp"
34 #include "filter_context.hpp"
35 #include "formula/callable_objects.hpp"
36 #include "formula/formula.hpp"
39 #include "deprecation.hpp"
40 
41 #include <boost/dynamic_bitset.hpp>
42 #include <boost/algorithm/string/predicate.hpp>
43 
44 static lg::log_domain log_engine("engine");
45 #define ERR_NG LOG_STREAM(err, log_engine)
46 
47 namespace {
48  class temporary_facing
49  {
50  map_location::DIRECTION save_dir_;
51  unit_const_ptr u_;
52  public:
53  temporary_facing(unit_const_ptr u, map_location::DIRECTION new_dir)
54  : save_dir_(u ? u->facing() : map_location::NDIRECTIONS)
55  , u_(u)
56  {
57  if (u_) {
58  u_->set_facing(new_dir);
59  }
60  }
61  ~temporary_facing()
62  {
63  if (u_) {
64  u_->set_facing(save_dir_);
65  }
66  }
67  };
68 }
69 
70 /*
71  *
72  * [abilities]
73  * ...
74  *
75  * [heals]
76  * value=4
77  * max_value=8
78  * cumulative=no
79  * affect_allies=yes
80  * name= _ "heals"
81  * female_name= _ "female^heals"
82  * name_inactive=null
83  * female_name_inactive=null
84  * description= _ "Heals:
85 Allows the unit to heal adjacent friendly units at the beginning of each turn.
86 
87 A unit cared for by a healer may heal up to 4 HP per turn.
88 A poisoned unit cannot be cured of its poison by a healer, and must seek the care of a village or a unit that can cure."
89  * description_inactive=null
90  *
91  * affect_self=yes
92  * [filter] // SUF
93  * ...
94  * [/filter]
95  * [filter_self] // SUF
96  * ...
97  * [/filter_self]
98  * [filter_adjacent] // SUF
99  * adjacent=n,ne,nw
100  * ...
101  * [/filter_adjacent]
102  * [filter_adjacent_location]
103  * adjacent=n,ne,nw
104  * ...
105  * [/filter_adjacent]
106  * [affect_adjacent]
107  * adjacent=n,ne,nw
108  * [filter] // SUF
109  * ...
110  * [/filter]
111  * [/affect_adjacent]
112  * [affect_adjacent]
113  * adjacent=s,se,sw
114  * [filter] // SUF
115  * ...
116  * [/filter]
117  * [/affect_adjacent]
118  *
119  * [/heals]
120  *
121  * ...
122  * [/abilities]
123  *
124  */
125 
126 
127 namespace {
128 
129 bool affects_side(const config& cfg, std::size_t side, std::size_t other_side)
130 {
131  // display::get_singleton() has already been confirmed valid by both callers.
132  const team& side_team = display::get_singleton()->get_disp_context().get_team(side);
133 
134  if(side == other_side || !side_team.is_enemy(other_side)) {
135  return cfg["affect_allies"].to_bool(true);
136  } else {
137  return cfg["affect_enemies"].to_bool();
138  }
139 }
140 
141 }
142 
143 bool unit::get_ability_bool(const std::string& tag_name, const map_location& loc) const
144 {
145  for (const config &i : this->abilities_.child_range(tag_name)) {
146  if (ability_active(tag_name, i, loc) &&
147  ability_affects_self(tag_name, i, loc))
148  {
149  return true;
150  }
151  }
152 
153  assert(display::get_singleton());
154  const unit_map& units = display::get_singleton()->get_units();
155 
156  adjacent_loc_array_t adjacent;
157  get_adjacent_tiles(loc,adjacent.data());
158  for(unsigned i = 0; i < adjacent.size(); ++i) {
159  const unit_map::const_iterator it = units.find(adjacent[i]);
160  if (it == units.end() || it->incapacitated())
161  continue;
162  // Abilities may be tested at locations other than the unit's current
163  // location. This is intentional to allow for less messing with the unit
164  // map during calculations, particularly with regards to movement.
165  // Thus, we need to make sure the adjacent unit (*it) is not actually
166  // ourself.
167  if ( &*it == this )
168  continue;
169  for (const config &j : it->abilities_.child_range(tag_name)) {
170  if (affects_side(j, side(), it->side()) &&
171  it->ability_active(tag_name, j, adjacent[i]) &&
172  ability_affects_adjacent(tag_name, j, i, loc, *it))
173  {
174  return true;
175  }
176  }
177  }
178 
179 
180  return false;
181 }
182 
183 unit_ability_list unit::get_abilities(const std::string& tag_name, const map_location& loc, const_attack_ptr weapon, const_attack_ptr opp_weapon) const
184 {
185  unit_ability_list res(loc_);
186 
187  for(const config& i : this->abilities_.child_range(tag_name)) {
188  if(ability_active(tag_name, i, loc)
189  && ability_affects_self(tag_name, i, loc)
190  && ability_affects_weapon(i, weapon, false)
191  && ability_affects_weapon(i, opp_weapon, true)
192  ) {
193  res.emplace_back(&i, loc);
194  }
195  }
196 
197  assert(display::get_singleton());
198  const unit_map& units = display::get_singleton()->get_units();
199 
200  adjacent_loc_array_t adjacent;
201  get_adjacent_tiles(loc,adjacent.data());
202  for(unsigned i = 0; i < adjacent.size(); ++i) {
203  const unit_map::const_iterator it = units.find(adjacent[i]);
204  if (it == units.end() || it->incapacitated())
205  continue;
206  // Abilities may be tested at locations other than the unit's current
207  // location. This is intentional to allow for less messing with the unit
208  // map during calculations, particularly with regards to movement.
209  // Thus, we need to make sure the adjacent unit (*it) is not actually
210  // ourself.
211  if ( &*it == this )
212  continue;
213  for(const config& j : it->abilities_.child_range(tag_name)) {
214  if(affects_side(j, side(), it->side())
215  && it->ability_active(tag_name, j, adjacent[i])
216  && ability_affects_adjacent(tag_name, j, i, loc, *it) && ability_affects_weapon(j, weapon, false)
217  && ability_affects_weapon(j, opp_weapon, true)
218  ) {
219  res.emplace_back(&j, adjacent[i]);
220  }
221  }
222  }
223 
224 
225  return res;
226 }
227 
228 std::vector<std::string> unit::get_ability_list() const
229 {
230  std::vector<std::string> res;
231 
232  for (const config::any_child &ab : this->abilities_.all_children_range()) {
233  std::string id = ab.cfg["id"];
234  if (!id.empty())
235  res.push_back(std::move(id));
236  }
237  return res;
238 }
239 
240 
241 namespace {
242  // These functions might have wider usefulness than this file, but for now
243  // I'll make them local.
244 
245  /**
246  * Chooses a value from the given config. If the value specified by @a key is
247  * blank, then @a default_key is chosen instead.
248  */
249  inline const config::attribute_value & default_value(
250  const config & cfg, const std::string & key, const std::string & default_key)
251  {
252  const config::attribute_value & value = cfg[key];
253  return !value.blank() ? value : cfg[default_key];
254  }
255 
256  /**
257  * Chooses a value from the given config based on gender. If the value for
258  * the specified gender is blank, then @a default_key is chosen instead.
259  */
260  inline const config::attribute_value & gender_value(
261  const config & cfg, unit_race::GENDER gender, const std::string & male_key,
262  const std::string & female_key, const std::string & default_key)
263  {
264  return default_value(cfg,
265  gender == unit_race::MALE ? male_key : female_key,
266  default_key);
267  }
268 
269  /**
270  * Adds a quadruple consisting of (in order) id, base name,
271  * male or female name as appropriate for the unit, and description.
272  *
273  * @returns Whether name was resolved and quadruple added.
274  */
275  bool add_ability_tooltip(const config::any_child &ab, unit_race::GENDER gender, std::vector<std::tuple<std::string, t_string,t_string,t_string>>& res, bool active)
276  {
277  if (active) {
278  const t_string& name = gender_value(ab.cfg, gender, "name", "female_name", "name").t_str();
279 
280  if (!name.empty()) {
281  res.emplace_back(
282  ab.cfg["id"],
283  ab.cfg["name"].t_str(),
284  name,
285  ab.cfg["description"].t_str() );
286  return true;
287  }
288  }
289  else
290  {
291  // See if an inactive name was specified.
292  const config::attribute_value& inactive_value =
293  gender_value(ab.cfg, gender, "name_inactive",
294  "female_name_inactive", "name_inactive");
295  const t_string& name = !inactive_value.blank() ? inactive_value.t_str() :
296  gender_value(ab.cfg, gender, "name", "female_name", "name").t_str();
297 
298  if (!name.empty()) {
299  res.emplace_back(
300  ab.cfg["id"],
301  default_value(ab.cfg, "name_inactive", "name").t_str(),
302  name,
303  default_value(ab.cfg, "description_inactive", "description").t_str() );
304  return true;
305  }
306  }
307 
308  return false;
309  }
310 }
311 
312 std::vector<std::tuple<std::string, t_string, t_string, t_string>> unit::ability_tooltips() const
313 {
314  std::vector<std::tuple<std::string, t_string,t_string,t_string>> res;
315 
316  for (const config::any_child &ab : this->abilities_.all_children_range())
317  {
318  add_ability_tooltip(ab, gender_, res, true);
319  }
320 
321  return res;
322 }
323 
324 std::vector<std::tuple<std::string, t_string, t_string, t_string>> unit::ability_tooltips(boost::dynamic_bitset<>& active_list, const map_location& loc) const
325 {
326  std::vector<std::tuple<std::string, t_string,t_string,t_string>> res;
327  active_list.clear();
328 
329  for (const config::any_child &ab : this->abilities_.all_children_range())
330  {
331  bool active = ability_active(ab.key, ab.cfg, loc);
332  if (add_ability_tooltip(ab, gender_, res, active))
333  {
334  active_list.push_back(active);
335  }
336  }
337  return res;
338 }
339 
340 bool unit::ability_active(const std::string& ability,const config& cfg,const map_location& loc) const
341 {
342  bool illuminates = ability == "illuminates";
343 
344  if (const config &afilter = cfg.child("filter"))
345  if ( !unit_filter(vconfig(afilter)).set_use_flat_tod(illuminates).matches(*this, loc) )
346  return false;
347 
348  adjacent_loc_array_t adjacent;
349  get_adjacent_tiles(loc,adjacent.data());
350 
351  assert(display::get_singleton());
352  const unit_map& units = display::get_singleton()->get_units();
353 
354  for (const config &i : cfg.child_range("filter_adjacent"))
355  {
356  std::size_t count = 0;
357  unit_filter ufilt{ vconfig(i) };
358  ufilt.set_use_flat_tod(illuminates);
359  std::vector<map_location::DIRECTION> dirs = map_location::parse_directions(i["adjacent"]);
360  for (const map_location::DIRECTION index : dirs)
361  {
363  continue;
364  unit_map::const_iterator unit = units.find(adjacent[index]);
365  if (unit == units.end())
366  return false;
367  if (!ufilt(*unit, *this))
368  return false;
369  if (i.has_attribute("is_enemy")) {
371  if (i["is_enemy"].to_bool() != dc.get_team(unit->side()).is_enemy(side_)) {
372  continue;
373  }
374  }
375  count++;
376  }
377  if (i["count"].empty() && count != dirs.size()) {
378  return false;
379  }
380  if (!in_ranges<int>(count, utils::parse_ranges(i["count"].str()))) {
381  return false;
382  }
383  }
384 
385  for (const config &i : cfg.child_range("filter_adjacent_location"))
386  {
387  std::size_t count = 0;
388  terrain_filter adj_filter(vconfig(i), resources::filter_con);
389  adj_filter.flatten(illuminates);
390 
391  std::vector<map_location::DIRECTION> dirs = map_location::parse_directions(i["adjacent"]);
392  for (const map_location::DIRECTION index : dirs)
393  {
395  continue;
396  }
397  if(!adj_filter.match(adjacent[index])) {
398  return false;
399  }
400  count++;
401  }
402  if (i["count"].empty() && count != dirs.size()) {
403  return false;
404  }
405  if (!in_ranges<int>(count, utils::parse_ranges(i["count"].str()))) {
406  return false;
407  }
408  }
409  return true;
410 }
411 
412 bool unit::ability_affects_adjacent(const std::string& ability, const config& cfg,int dir,const map_location& loc,const unit& from) const
413 {
414  bool illuminates = ability == "illuminates";
415 
416  assert(dir >=0 && dir <= 5);
417  map_location::DIRECTION direction = static_cast<map_location::DIRECTION>(dir);
418 
419  for (const config &i : cfg.child_range("affect_adjacent"))
420  {
421  if (i.has_attribute("adjacent")) { //key adjacent defined
422  std::vector<map_location::DIRECTION> dirs = map_location::parse_directions(i["adjacent"]);
423  if (std::find(dirs.begin(), dirs.end(), direction) == dirs.end()) {
424  continue;
425  }
426  }
427  const config &filter = i.child("filter");
428  if (!filter || //filter tag given
429  unit_filter(vconfig(filter)).set_use_flat_tod(illuminates).matches(*this, loc, from) ) {
430  return true;
431  }
432  }
433  return false;
434 }
435 
436 bool unit::ability_affects_self(const std::string& ability,const config& cfg,const map_location& loc) const
437 {
438  const config &filter = cfg.child("filter_self");
439  bool affect_self = cfg["affect_self"].to_bool(true);
440  if (!filter || !affect_self) return affect_self;
441  return unit_filter(vconfig(filter)).set_use_flat_tod(ability == "illuminates").matches(*this, loc);
442 }
443 
444 bool unit::ability_affects_weapon(const config& cfg, const_attack_ptr weapon, bool is_opp) const
445 {
446  const std::string filter_tag_name = is_opp ? "filter_second_weapon" : "filter_weapon";
447  if(!cfg.has_child(filter_tag_name)) {
448  return true;
449  }
450  const config& filter = cfg.child(filter_tag_name);
451  if(!weapon) {
452  return false;
453  }
454  return weapon->matches_filter(filter);
455 }
456 
457 bool unit::has_ability_type(const std::string& ability) const
458 {
459  return !abilities_.child_range(ability).empty();
460 }
461 
462 namespace {
463 
464 
465 template<typename T, typename TFuncFormula>
466 class get_ability_value_visitor : public boost::static_visitor<T>
467 {
468 public:
469  // Constructor stores the default value.
470  get_ability_value_visitor(T def, const TFuncFormula& formula_handler) : def_(def), formula_handler_(formula_handler) {}
471 
472  T operator()(const boost::blank&) const { return def_; }
473  T operator()(bool) const { return def_; }
474  T operator()(int i) const { return static_cast<T>(i); }
475  T operator()(unsigned long long u) const { return static_cast<T>(u); }
476  T operator()(double d) const { return static_cast<T>(d); }
477  T operator()(const t_string&) const { return def_; }
478  T operator()(const std::string& s) const
479  {
480  if(s.size() >= 2 && s[0] == '(') {
481  return formula_handler_(s);
482  }
483  return lexical_cast_default<T>(s, def_);
484  }
485 
486 private:
487  const T def_;
488  const TFuncFormula& formula_handler_;
489 };
490 template<typename T, typename TFuncFormula>
491 get_ability_value_visitor<T, TFuncFormula> make_get_ability_value_visitor(T def, const TFuncFormula& formula_handler)
492 {
493  return get_ability_value_visitor<T, TFuncFormula>(def, formula_handler);
494 }
495 template<typename T, typename TFuncFormula>
496 T get_single_ability_value(const config::attribute_value& v, T def, const map_location& sender_loc, const map_location& receiver_loc, const TFuncFormula& formula_handler)
497 {
498  return v.apply_visitor(make_get_ability_value_visitor(def, [&](const std::string& s) {
499 
500  try {
501  assert(display::get_singleton());
502  const unit_map& units = display::get_singleton()->get_units();
503 
504  auto u_itor = units.find(sender_loc);
505 
506  if(u_itor == units.end()) {
507  return def;
508  }
509  wfl::map_formula_callable callable(std::make_shared<wfl::unit_callable>(*u_itor));
510  u_itor = units.find(receiver_loc);
511  if(u_itor != units.end()) {
512  callable.add("other", wfl::variant(std::make_shared<wfl::unit_callable>(*u_itor)));
513  }
514  return formula_handler(wfl::formula(s, new wfl::gamestate_function_symbol_table), callable);
515  } catch(const wfl::formula_error& e) {
516  lg::wml_error() << "Formula error in ability or weapon special: " << e.type << " at " << e.filename << ':' << e.line << ")\n";
517  return def;
518  }
519  }));
520 }
521 }
522 
523 template<typename TComp>
524 std::pair<int,map_location> unit_ability_list::get_extremum(const std::string& key, int def, const TComp& comp) const
525 {
526  if ( cfgs_.empty() ) {
527  return std::make_pair(def, map_location());
528  }
529  // The returned location is the best non-cumulative one, if any,
530  // the best absolute cumulative one otherwise.
531  map_location best_loc;
532  bool only_cumulative = true;
533  int abs_max = 0;
534  int flat = 0;
535  int stack = 0;
536  for (const unit_ability& p : cfgs_)
537  {
538  int value = get_single_ability_value((*p.first)[key], def, p.second, loc(),[&](const wfl::formula& formula, wfl::map_formula_callable& callable) {
539  return formula.evaluate(callable).as_int();
540  });
541 
542  if ((*p.first)["cumulative"].to_bool()) {
543  stack += value;
544  if (value < 0) value = -value;
545  if (only_cumulative && !comp(value, abs_max)) {
546  abs_max = value;
547  best_loc = p.second;
548  }
549  } else if (only_cumulative || comp(flat, value)) {
550  only_cumulative = false;
551  flat = value;
552  best_loc = p.second;
553  }
554  }
555  return std::make_pair(flat + stack, best_loc);
556 }
557 
558 template std::pair<int, map_location> unit_ability_list::get_extremum<std::less<int>>(const std::string& key, int def, const std::less<int>& comp) const;
559 template std::pair<int, map_location> unit_ability_list::get_extremum<std::greater<int>>(const std::string& key, int def, const std::greater<int>& comp) const;
560 
561 /*
562  *
563  * [special]
564  * [swarm]
565  * name= _ "swarm"
566  * name_inactive= _ ""
567  * description= _ ""
568  * description_inactive= _ ""
569  * cumulative=no
570  * apply_to=self #self,opponent,defender,attacker,both
571  * #active_on=defense # or offense; omitting this means "both"
572  *
573  * swarm_attacks_max=4
574  * swarm_attacks_min=2
575  *
576  * [filter_self] // SUF
577  * ...
578  * [/filter_self]
579  * [filter_opponent] // SUF
580  * [filter_attacker] // SUF
581  * [filter_defender] // SUF
582  * [filter_adjacent] // SAUF
583  * [filter_adjacent_location] // SAUF + locs
584  * [/swarm]
585  * [/special]
586  *
587  */
588 
589 namespace {
590 
591  struct special_match
592  {
593  std::string tag_name;
594  const config* cfg;
595  };
596 
597  /**
598  * Gets the children of @parent (which should be the specials for an
599  * attack_type) and places the ones whose tag or id= matches @a id into
600  * @a tag_result and @a id_result.
601  *
602  * If @a just_peeking is set to true, then @a tag_result and @a id_result
603  * are not touched; instead the return value is used to indicate if any
604  * matching children were found.
605  *
606  * @returns true if @a just_peeking is true and a match was found;
607  * false otherwise.
608  */
609  bool get_special_children(std::vector<special_match>& tag_result,
610  std::vector<special_match>& id_result,
611  const config& parent, const std::string& id,
612  bool just_peeking=false) {
613  for (const config::any_child &sp : parent.all_children_range())
614  {
615  if (just_peeking && (sp.key == id || sp.cfg["id"] == id)) {
616  return true; // peek succeeded; done
617  }
618 
619  if(sp.key == id) {
620  special_match special = { sp.key, &sp.cfg };
621  tag_result.push_back(special);
622  }
623  if(sp.cfg["id"] == id) {
624  special_match special = { sp.key, &sp.cfg };
625  id_result.push_back(special);
626  }
627  }
628  return false;
629  }
630 }
631 
632 /**
633  * Returns whether or not @a *this has a special with a tag or id equal to
634  * @a special. If @a simple_check is set to true, then the check is merely
635  * for being present. Otherwise (the default), the check is for a special
636  * active in the current context (see set_specials_context), including
637  * specials obtained from the opponent's attack.
638  */
639 bool attack_type::get_special_bool(const std::string& special, bool simple_check) const
640 {
641  {
642  std::vector<special_match> special_tag_matches;
643  std::vector<special_match> special_id_matches;
644  if ( get_special_children(special_tag_matches, special_id_matches, specials_, special, simple_check) ) {
645  return true;
646  }
647  // If we make it to here, then either list.empty() or !simple_check.
648  // So if the list is not empty, then this is not a simple check and
649  // we need to check each special in the list to see if any are active.
650  for(const special_match& entry : special_tag_matches) {
651  if ( special_active(*entry.cfg, AFFECT_SELF, entry.tag_name) ) {
652  return true;
653  }
654  }
655  for(const special_match& entry : special_id_matches) {
656  if ( special_active(*entry.cfg, AFFECT_SELF, entry.tag_name) ) {
657  return true;
658  }
659  }
660  }
661 
662  // Skip checking the opponent's attack?
663  if ( simple_check || !other_attack_ ) {
664  return false;
665  }
666 
667  std::vector<special_match> special_tag_matches;
668  std::vector<special_match> special_id_matches;
669  get_special_children(special_tag_matches, special_id_matches, other_attack_->specials_, special);
670  for(const special_match& entry : special_tag_matches) {
671  if ( other_attack_->special_active(*entry.cfg, AFFECT_OTHER, entry.tag_name) ) {
672  return true;
673  }
674  }
675  for(const special_match& entry : special_id_matches) {
676  if ( other_attack_->special_active(*entry.cfg, AFFECT_OTHER, entry.tag_name) ) {
677  return true;
678  }
679  }
680  return false;
681 }
682 
683 /**
684  * Returns the currently active specials as an ability list, given the current
685  * context (see set_specials_context).
686  */
687 unit_ability_list attack_type::get_specials(const std::string& special) const
688 {
689  //log_scope("get_specials");
690  unit_ability_list res(self_loc_);
691 
692  for(const config& i : specials_.child_range(special)) {
693  if(special_active(i, AFFECT_SELF, special)) {
694  res.emplace_back(&i, self_loc_);
695  }
696  }
697 
698  if(!other_attack_) {
699  return res;
700  }
701 
702  for(const config& i : other_attack_->specials_.child_range(special)) {
703  if(other_attack_->special_active(i, AFFECT_OTHER, special)) {
704  res.emplace_back(&i, other_loc_);
705  }
706  }
707  return res;
708 }
709 
710 /**
711  * Returns a vector of names and descriptions for the specials of *this.
712  * Each std::pair in the vector has first = name and second = description.
713  *
714  * This uses either the active or inactive name/description for each special,
715  * based on the current context (see set_specials_context), provided
716  * @a active_list is not nullptr. Otherwise specials are assumed active.
717  * If the appropriate name is empty, the special is skipped.
718  */
719 std::vector<std::pair<t_string, t_string>> attack_type::special_tooltips(
720  boost::dynamic_bitset<>* active_list) const
721 {
722  //log_scope("special_tooltips");
723  std::vector<std::pair<t_string, t_string>> res;
724  if ( active_list )
725  active_list->clear();
726 
727  for (const config::any_child &sp : specials_.all_children_range())
728  {
729  if ( !active_list || special_active(sp.cfg, AFFECT_EITHER, sp.key) ) {
730  const t_string &name = sp.cfg["name"];
731  if (!name.empty()) {
732  res.emplace_back(name, sp.cfg["description"].t_str() );
733  if ( active_list )
734  active_list->push_back(true);
735  }
736  } else {
737  const t_string& name = default_value(sp.cfg, "name_inactive", "name").t_str();
738  if (!name.empty()) {
739  res.emplace_back(name, default_value(sp.cfg, "description_inactive", "description").t_str() );
740  active_list->push_back(false);
741  }
742  }
743  }
744  return res;
745 }
746 
747 /**
748  * Returns a comma-separated string of active names for the specials of *this.
749  * Empty names are skipped.
750  *
751  * This excludes inactive specials if only_active is true. Whether or not a
752  * special is active depends on the current context (see set_specials_context)
753  * and the @a is_backstab parameter.
754  */
755 std::string attack_type::weapon_specials(bool only_active, bool is_backstab) const
756 {
757  //log_scope("weapon_specials");
758  std::string res;
759  for (const config::any_child &sp : specials_.all_children_range())
760  {
761  const bool active = special_active(sp.cfg, AFFECT_EITHER, sp.key, is_backstab);
762 
763  const std::string& name = sp.cfg["name"].str();
764  if (!name.empty()) {
765  if (!res.empty()) res += ", ";
766  if (only_active && !active) res += font::span_color(font::inactive_details_color);
767  res += name;
768  if (only_active && !active) res += "</span>";
769  }
770  }
771 
772  return res;
773 }
774 
775 
776 /**
777  * Sets the context under which specials will be checked for being active.
778  * This version is appropriate if both units in a combat are known.
779  * @param[in] self A reference to the unit with this weapon.
780  * @param[in] other A reference to the other unit in the combat.
781  * @param[in] unit_loc The location of the unit with this weapon.
782  * @param[in] other_loc The location of the other unit in the combat.
783  * @param[in] attacking Whether or not the unit with this weapon is the attacker.
784  * @param[in] other_attack The attack used by the other unit.
785  */
787  const_attack_ptr other_attack,
788  unit_const_ptr self,
789  unit_const_ptr other,
790  const map_location& unit_loc,
791  const map_location& other_loc,
792  bool attacking)
793  : parent(weapon.shared_from_this())
794 {
795  weapon.self_ = self;
796  weapon.other_ = other;
797  weapon.self_loc_ = unit_loc;
798  weapon.other_loc_ = other_loc;
799  weapon.is_attacker_ = attacking;
800  weapon.other_attack_ = other_attack;
801  weapon.is_for_listing_ = false;
802 }
803 
804 /**
805  * Sets the context under which specials will be checked for being active.
806  * This version is appropriate if there is no specific combat being considered.
807  * @param[in] self A reference to the unit with this weapon.
808  * @param[in] loc The location of the unit with this weapon.
809  * @param[in] attacking Whether or not the unit with this weapon is the attacker.
810  */
812  : parent(weapon.shared_from_this())
813 {
814  weapon.self_ = self;
815  weapon.other_ = nullptr;
816  weapon.self_loc_ = loc;
818  weapon.is_attacker_ = attacking;
819  weapon.other_attack_ = nullptr;
820  weapon.is_for_listing_ = false;
821 }
822 
823 /**
824  * Sets the context under which specials will be checked for being active.
825  * This version is appropriate for theoretical units of a particular type.
826  * @param[in] self_type A reference to the type of the unit with this weapon.
827  * @param[in] loc The location of the unit with this weapon.
828  * @param[in] attacking Whether or not the unit with this weapon is the attacker.
829  */
830 attack_type::specials_context_t::specials_context_t(const attack_type& weapon, const unit_type& self_type, const map_location& loc, bool attacking)
831  : parent(weapon.shared_from_this())
832 {
833  UNUSED(self_type);
834  weapon.self_ = nullptr;
835  weapon.other_ = nullptr;
836  weapon.self_loc_ = loc;
838  weapon.is_attacker_ = attacking;
839  weapon.other_attack_ = nullptr;
840  weapon.is_for_listing_ = false;
841 }
842 
844  : parent(weapon.shared_from_this())
845 {
846  weapon.is_for_listing_ = true;
847  weapon.is_attacker_ = attacking;
848 }
849 
851 {
852  if(was_moved) return;
853  parent->self_ = nullptr;
854  parent->other_ = nullptr;
855  parent->self_loc_ = map_location::null_location();
856  parent->other_loc_ = map_location::null_location();
857  parent->is_attacker_ = false;
858  parent->other_attack_ = nullptr;
859  parent->is_for_listing_ = false;
860 }
861 
863  : parent(other.parent)
864 {
865  other.was_moved = true;
866 }
867 
868 /**
869  * Calculates the number of attacks this weapon has, considering specials.
870  * This returns two numbers because of the swarm special. The actual number of
871  * attacks depends on the unit's health and should be:
872  * min_attacks + (max_attacks - min_attacks) * (current hp) / (max hp)
873  * c.f. swarm_blows()
874  */
875 void attack_type::modified_attacks(bool is_backstab, unsigned & min_attacks,
876  unsigned & max_attacks) const
877 {
878  // Apply [attacks].
879  unit_abilities::effect attacks_effect(get_specials("attacks"),
880  num_attacks(), is_backstab);
881  int attacks_value = attacks_effect.get_composite_value();
882  if ( combat_ability("attacks", attacks_value, is_backstab).second ) {
883  attacks_value = combat_ability("attacks", attacks_value, is_backstab).first;
884  }
885 
886  if ( attacks_value < 0 ) {
887  attacks_value = num_attacks();
888  ERR_NG << "negative number of strikes after applying weapon specials" << std::endl;
889  }
890 
891  // Apply [swarm].
892  unit_ability_list swarm_specials = get_specials("swarm");
893  if ( !swarm_specials.empty() ) {
894  min_attacks = std::max<int>(0, swarm_specials.highest("swarm_attacks_min").first);
895  max_attacks = std::max<int>(0, swarm_specials.highest("swarm_attacks_max", attacks_value).first);
896  } else {
897  min_attacks = max_attacks = attacks_value;
898  }
899 }
900 
901 
902 /**
903  * Returns the damage per attack of this weapon, considering specials.
904  */
905 int attack_type::modified_damage(bool is_backstab) const
906 {
907  unit_abilities::effect dmg_effect(get_specials("damage"), damage(), is_backstab);
908  int damage_value = dmg_effect.get_composite_value();
909  if ( combat_ability("damage", damage_value, is_backstab).second ) {
910  damage_value = combat_ability("damage", damage_value, is_backstab).first;
911  }
912  return damage_value;
913 }
914 
915 
916 namespace { // Helpers for attack_type::special_active()
917 
918  /**
919  * Returns whether or not the given special affects the opponent of the unit
920  * with the special.
921  * @param[in] special a weapon special WML structure
922  * @param[in] is_attacker whether or not the unit with the special is the attacker
923  */
924  bool special_affects_opponent(const config& special, bool is_attacker)
925  {
926  //log_scope("special_affects_opponent");
927  const std::string& apply_to = special["apply_to"];
928  if ( apply_to.empty() )
929  return false;
930  if ( apply_to == "both" )
931  return true;
932  if ( apply_to == "opponent" )
933  return true;
934  if ( is_attacker && apply_to == "defender" )
935  return true;
936  if ( !is_attacker && apply_to == "attacker" )
937  return true;
938  return false;
939  }
940 
941  /**
942  * Returns whether or not the given special affects the unit with the special.
943  * @param[in] special a weapon special WML structure
944  * @param[in] is_attacker whether or not the unit with the special is the attacker
945  */
946  bool special_affects_self(const config& special, bool is_attacker)
947  {
948  //log_scope("special_affects_self");
949  const std::string& apply_to = special["apply_to"];
950  if ( apply_to.empty() )
951  return true;
952  if ( apply_to == "both" )
953  return true;
954  if ( apply_to == "self" )
955  return true;
956  if ( is_attacker && apply_to == "attacker" )
957  return true;
958  if ( !is_attacker && apply_to == "defender")
959  return true;
960  return false;
961  }
962 
963  /**
964  * Determines if a unit/weapon combination matches the specified child
965  * (normally a [filter_*] child) of the provided filter.
966  * @param[in] u A unit to filter.
967  * @param[in] u2 Another unit to filter.
968  * @param[in] loc The presumed location of @a un_it.
969  * @param[in] weapon The attack_type to filter.
970  * @param[in] filter The filter containing the child filter to use.
971  * @param[in] child_tag The tag of the child filter to use.
972  */
973  static bool special_unit_matches(unit_const_ptr & u,
974  unit_const_ptr & u2,
975  const map_location & loc,
976  const_attack_ptr weapon,
977  const config & filter,
978  const bool for_listing,
979  const std::string & child_tag)
980  {
981  if (for_listing && !loc.valid())
982  // The special's context was set to ignore this unit, so assume we pass.
983  // (This is used by reports.cpp to show active specials when the
984  // opponent is not known. From a player's perspective, the special
985  // is active, in that it can be used, even though the player might
986  // need to select an appropriate opponent.)
987  return true;
988 
989  const config & filter_child = filter.child(child_tag);
990  if ( !filter_child )
991  // The special does not filter on this unit, so we pass.
992  return true;
993 
994  // If the primary unit doesn't exist, there's nothing to match
995  if (!u) {
996  return false;
997  }
998 
999  unit_filter ufilt{vconfig(filter_child)};
1000 
1001  // If the other unit doesn't exist, try matching without it
1002  if (!u2) {
1003  return ufilt.matches(*u, loc);
1004  }
1005 
1006  // Check for a unit match.
1007  if (!ufilt.matches(*u, loc, *u2)) {
1008  return false;
1009  }
1010 
1011  // Check for a weapon match.
1012  if ( const config & filter_weapon = filter_child.child("filter_weapon") ) {
1013  if ( !weapon || !weapon->matches_filter(filter_weapon) )
1014  return false;
1015  }
1016 
1017  // Passed.
1018  return true;
1019  }
1020 
1021 }//anonymous namespace
1022 
1023 /**
1024  * Returns whether or not the given special is active for the specified unit,
1025  * based on the current context (see set_specials_context).
1026  * @param[in] special a weapon special WML structure
1027  * @param[in] whom specifies which combatant we care about
1028  * @param[in] tag_name tag name of the special config
1029  * @param[in] include_backstab false if backstab specials should not be active
1030  * (usually true since backstab is usually accounted
1031  * for elsewhere)
1032  */
1033 bool attack_type::special_active(const config& special, AFFECTS whom, const std::string& tag_name,
1034  bool include_backstab) const
1035 {
1036  //log_scope("special_active");
1037 
1038  // Backstab check
1039  if ( !include_backstab )
1040  if ( special["backstab"].to_bool() )
1041  return false;
1042 
1043  // Does this affect the specified unit?
1044  if ( whom == AFFECT_SELF ) {
1045  if ( !special_affects_self(special, is_attacker_) )
1046  return false;
1047  }
1048  if ( whom == AFFECT_OTHER ) {
1049  if ( !special_affects_opponent(special, is_attacker_) )
1050  return false;
1051  }
1052 
1053  // Is this active on attack/defense?
1054  const std::string & active_on = special["active_on"];
1055  if ( !active_on.empty() ) {
1056  if ( is_attacker_ && active_on != "offense" )
1057  return false;
1058  if ( !is_attacker_ && active_on != "defense" )
1059  return false;
1060  }
1061 
1062  // Get the units involved.
1063  assert(display::get_singleton());
1064  const unit_map& units = display::get_singleton()->get_units();
1065 
1066  unit_const_ptr self = self_;
1067  unit_const_ptr other = other_;
1068 
1069  if(self == nullptr) {
1071  if(it.valid()) {
1072  self = it.get_shared_ptr().get();
1073  }
1074  }
1075  if(other == nullptr) {
1077  if(it.valid()) {
1078  other = it.get_shared_ptr().get();
1079  }
1080  }
1081 
1082  // Make sure they're facing each other.
1083  temporary_facing self_facing(self, self_loc_.get_relative_dir(other_loc_));
1084  temporary_facing other_facing(other, other_loc_.get_relative_dir(self_loc_));
1085 
1086  // Filter poison, plague, drain, first strike
1087  if (tag_name == "drains" && other && other->get_state("undrainable")) {
1088  return false;
1089  }
1090  if (tag_name == "plague" && other &&
1091  (other->get_state("unplagueable") ||
1093  return false;
1094  }
1095  if (tag_name == "poison" && other &&
1096  (other->get_state("unpoisonable") || other->get_state(unit::STATE_POISONED))) {
1097  return false;
1098  }
1099  if (tag_name == "firststrike" && !is_attacker_ && other_attack_ &&
1100  other_attack_->get_special_bool("firststrike", false)) {
1101  return false;
1102  }
1103 
1104 
1105  // Translate our context into terms of "attacker" and "defender".
1106  unit_const_ptr & att = is_attacker_ ? self : other;
1107  unit_const_ptr & def = is_attacker_ ? other : self;
1108  const map_location & att_loc = is_attacker_ ? self_loc_ : other_loc_;
1109  const map_location & def_loc = is_attacker_ ? other_loc_ : self_loc_;
1110  const_attack_ptr att_weapon = is_attacker_ ? shared_from_this() : other_attack_;
1111  const_attack_ptr def_weapon = is_attacker_ ? other_attack_ : shared_from_this();
1112 
1113  // Filter the units involved.
1114  if (!special_unit_matches(self, other, self_loc_, shared_from_this(), special, is_for_listing_, "filter_self"))
1115  return false;
1116  if (!special_unit_matches(other, self, other_loc_, other_attack_, special, is_for_listing_, "filter_opponent"))
1117  return false;
1118  if (!special_unit_matches(att, def, att_loc, att_weapon, special, is_for_listing_, "filter_attacker"))
1119  return false;
1120  if (!special_unit_matches(def, att, def_loc, def_weapon, special, is_for_listing_, "filter_defender"))
1121  return false;
1122 
1123  adjacent_loc_array_t adjacent;
1124  get_adjacent_tiles(self_loc_, adjacent.data());
1125 
1126  // Filter the adjacent units.
1127  for (const config &i : special.child_range("filter_adjacent"))
1128  {
1129  std::size_t count = 0;
1130  std::vector<map_location::DIRECTION> dirs = map_location::parse_directions(i["adjacent"]);
1131  unit_filter filter{ vconfig(i) };
1132  for (const map_location::DIRECTION index : dirs)
1133  {
1135  continue;
1136  unit_map::const_iterator unit = units.find(adjacent[index]);
1137  if (unit == units.end() || !filter.matches(*unit, adjacent[index], *self))
1138  return false;
1139  if (i.has_attribute("is_enemy")) {
1141  if (i["is_enemy"].to_bool() != dc.get_team(unit->side()).is_enemy(self->side())) {
1142  continue;
1143  }
1144  }
1145  count++;
1146  }
1147  if (i["count"].empty() && count != dirs.size()) {
1148  return false;
1149  }
1150  if (!in_ranges<int>(count, utils::parse_ranges(i["count"].str()))) {
1151  return false;
1152  }
1153  }
1154 
1155  // Filter the adjacent locations.
1156  for (const config &i : special.child_range("filter_adjacent_location"))
1157  {
1158  std::size_t count = 0;
1159  std::vector<map_location::DIRECTION> dirs = map_location::parse_directions(i["adjacent"]);
1160  terrain_filter adj_filter(vconfig(i), resources::filter_con);
1161  for (const map_location::DIRECTION index : dirs)
1162  {
1164  continue;
1165  if(!adj_filter.match(adjacent[index])) {
1166  return false;
1167  }
1168  count++;
1169  }
1170  if (i["count"].empty() && count != dirs.size()) {
1171  return false;
1172  }
1173  if (!in_ranges<int>(count, utils::parse_ranges(i["count"].str()))) {
1174  return false;
1175  }
1176  }
1177 
1178  return true;
1179 }
1180 
1181 
1182 
1184 {
1185 
1186 void individual_effect::set(value_modifier t, int val, const config *abil, const map_location &l)
1187 {
1188  type=t;
1189  value=val;
1190  ability=abil;
1191  loc=l;
1192 }
1193 
1194 bool filter_base_matches(const config& cfg, int def)
1195 {
1196  if (const config &apply_filter = cfg.child("filter_base_value")) {
1197  config::attribute_value cond_eq = apply_filter["equals"];
1198  config::attribute_value cond_ne = apply_filter["not_equals"];
1199  config::attribute_value cond_lt = apply_filter["less_than"];
1200  config::attribute_value cond_gt = apply_filter["greater_than"];
1201  config::attribute_value cond_ge = apply_filter["greater_than_equal_to"];
1202  config::attribute_value cond_le = apply_filter["less_than_equal_to"];
1203  return (cond_eq.empty() || def == cond_eq.to_int()) &&
1204  (cond_ne.empty() || def != cond_ne.to_int()) &&
1205  (cond_lt.empty() || def < cond_lt.to_int()) &&
1206  (cond_gt.empty() || def > cond_gt.to_int()) &&
1207  (cond_ge.empty() || def >= cond_ge.to_int()) &&
1208  (cond_le.empty() || def <= cond_le.to_int());
1209  }
1210  return true;
1211 }
1212 
1213 effect::effect(const unit_ability_list& list, int def, bool backstab) :
1214  effect_list_(),
1215  composite_value_(0)
1216 {
1217 
1218  int value_set = def;
1219  std::map<std::string,individual_effect> values_add;
1220  std::map<std::string,individual_effect> values_mul;
1221  std::map<std::string,individual_effect> values_div;
1222 
1223  individual_effect set_effect_max;
1224  individual_effect set_effect_min;
1225 
1226  for (const unit_ability & ability : list) {
1227  const config& cfg = *ability.first;
1228  const std::string& effect_id = cfg[cfg["id"].empty() ? "name" : "id"];
1229 
1230  if (!cfg["backstab"].blank()) {
1231  deprecated_message("backstab= in weapon specials", DEP_LEVEL::PREEMPTIVE, {1, 15, 0}, "Use [filter_adjacent] instead.");
1232  }
1233 
1234  if (!backstab && cfg["backstab"].to_bool())
1235  continue;
1236  if (!filter_base_matches(cfg, def))
1237  continue;
1238 
1239  if (const config::attribute_value *v = cfg.get("value")) {
1240  int value = get_single_ability_value(*v, def, ability.second, list.loc(),[&](const wfl::formula& formula, wfl::map_formula_callable& callable) {
1241  callable.add("base_value", wfl::variant(def));
1242  return formula.evaluate(callable).as_int();
1243  });
1244 
1245  int value_cum = cfg["cumulative"].to_bool() ? std::max(def, value) : value;
1246  assert((set_effect_min.type != NOT_USED) == (set_effect_max.type != NOT_USED));
1247  if(set_effect_min.type == NOT_USED) {
1248  set_effect_min.set(SET, value_cum, ability.first, ability.second);
1249  set_effect_max.set(SET, value_cum, ability.first, ability.second);
1250  }
1251  else {
1252  if(value_cum > set_effect_max.value) {
1253  set_effect_max.set(SET, value_cum, ability.first, ability.second);
1254  }
1255  if(value_cum < set_effect_min.value) {
1256  set_effect_min.set(SET, value_cum, ability.first, ability.second);
1257  }
1258  }
1259  }
1260 
1261  if (const config::attribute_value *v = cfg.get("add")) {
1262  int add = get_single_ability_value(*v, def, ability.second, list.loc(),[&](const wfl::formula& formula, wfl::map_formula_callable& callable) {
1263  callable.add("base_value", wfl::variant(def));
1264  return formula.evaluate(callable).as_int();
1265  });
1266  std::map<std::string,individual_effect>::iterator add_effect = values_add.find(effect_id);
1267  if(add_effect == values_add.end() || add > add_effect->second.value) {
1268  values_add[effect_id].set(ADD, add, ability.first, ability.second);
1269  }
1270  }
1271  if (const config::attribute_value *v = cfg.get("sub")) {
1272  int sub = - get_single_ability_value(*v, def, ability.second, list.loc(),[&](const wfl::formula& formula, wfl::map_formula_callable& callable) {
1273  callable.add("base_value", wfl::variant(def));
1274  return formula.evaluate(callable).as_int();
1275  });
1276  std::map<std::string,individual_effect>::iterator sub_effect = values_add.find(effect_id);
1277  if(sub_effect == values_add.end() || sub < sub_effect->second.value) {
1278  values_add[effect_id].set(ADD, sub, ability.first, ability.second);
1279  }
1280  }
1281  if (const config::attribute_value *v = cfg.get("multiply")) {
1282  int multiply = static_cast<int>(get_single_ability_value(*v, static_cast<double>(def), ability.second, list.loc(),[&](const wfl::formula& formula, wfl::map_formula_callable& callable) {
1283  callable.add("base_value", wfl::variant(def));
1284  return formula.evaluate(callable).as_decimal() / 1000.0 ;
1285  }) * 100);
1286  std::map<std::string,individual_effect>::iterator mul_effect = values_mul.find(effect_id);
1287  if(mul_effect == values_mul.end() || multiply > mul_effect->second.value) {
1288  values_mul[effect_id].set(MUL, multiply, ability.first, ability.second);
1289  }
1290  }
1291  if (const config::attribute_value *v = cfg.get("divide")) {
1292  int divide = static_cast<int>(get_single_ability_value(*v, static_cast<double>(def), ability.second, list.loc(),[&](const wfl::formula& formula, wfl::map_formula_callable& callable) {
1293  callable.add("base_value", wfl::variant(def));
1294  return formula.evaluate(callable).as_decimal() / 1000.0 ;
1295  }) * 100);
1296 
1297  if (divide == 0) {
1298  ERR_NG << "division by zero with divide= in ability/weapon special " << effect_id << std::endl;
1299  }
1300  else {
1301  std::map<std::string,individual_effect>::iterator div_effect = values_div.find(effect_id);
1302  if(div_effect == values_div.end() || divide > div_effect->second.value) {
1303  values_div[effect_id].set(DIV, divide, ability.first, ability.second);
1304  }
1305  }
1306  }
1307  }
1308 
1309  if(set_effect_max.type != NOT_USED) {
1310  value_set = std::max(set_effect_max.value, 0) + std::min(set_effect_min.value, 0);
1311  if(set_effect_max.value > def) {
1312  effect_list_.push_back(set_effect_max);
1313  }
1314  if(set_effect_min.value < def) {
1315  effect_list_.push_back(set_effect_min);
1316  }
1317  }
1318 
1319  /* Do multiplication with floating point values rather than integers
1320  * We want two places of precision for each multiplier
1321  * Using integers multiplied by 100 to keep precision causes overflow
1322  * after 3-4 abilities for 32-bit values and ~8 for 64-bit
1323  * Avoiding the overflow by dividing after each step introduces rounding errors
1324  * that may vary depending on the order effects are applied
1325  * As the final values are likely <1000 (always true for mainline), loss of less significant digits is not an issue
1326  */
1327  double multiplier = 1.0;
1328  double divisor = 1.0;
1329 
1330  for(const auto& val : values_mul) {
1331  multiplier *= val.second.value/100.0;
1332  effect_list_.push_back(val.second);
1333  }
1334 
1335  for(const auto& val : values_div) {
1336  divisor *= val.second.value/100.0;
1337  effect_list_.push_back(val.second);
1338  }
1339 
1340  int addition = 0;
1341  for(const auto& val : values_add) {
1342  addition += val.second.value;
1343  effect_list_.push_back(val.second);
1344  }
1345 
1346  composite_value_ = static_cast<int>((value_set + addition) * multiplier / divisor);
1347 }
1348 
1349 } // end namespace unit_abilities
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
bool empty() const
Tests for an attribute that either was never set or was set to "".
std::string weapon_specials(bool only_active=false, bool is_backstab=false) const
Returns a comma-separated string of active names for the specials of *this.
Definition: abilities.cpp:755
bool ability_affects_weapon(const config &cfg, const_attack_ptr weapon, bool is_opp) const
Definition: abilities.cpp:444
std::vector< individual_effect > effect_list_
Definition: abilities.hpp:56
bool empty() const
Definition: unit.hpp:78
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
unit_iterator end()
Definition: map.hpp:415
#define ERR_NG
Definition: abilities.cpp:45
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:88
const_all_children_itors all_children_range() const
In-order iteration over all children.
Definition: config.cpp:921
const team & get_team(int side) const
virtual const display_context & get_disp_context() const =0
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
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition: cursor.cpp:173
DIRECTION get_relative_dir(const map_location &loc, map_location::RELATIVE_DIR_MODE mode) const
Definition: location.cpp:225
This class represents a single unit of a specific type.
Definition: unit.hpp:99
const color_t inactive_details_color
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition: formula.hpp:39
unit_filter & set_use_flat_tod(bool value)
Definition: filter.hpp:121
void emplace_back(T &&... args)
Definition: unit.hpp:87
Variant for storing WML attributes.
std::string filename
Definition: formula.hpp:107
New lexcical_cast header.
int as_int() const
Definition: variant.cpp:292
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:412
void set(value_modifier t, int val, const config *abil, const map_location &l)
Definition: abilities.cpp:1186
child_itors child_range(config_key_type key)
Definition: config.cpp:362
map_location other_loc_
virtual const gamemap & map() const override
Definition: game_board.hpp:109
void modified_attacks(bool is_backstab, unsigned &min_attacks, unsigned &max_attacks) const
Calculates the number of attacks this weapon has, considering specials.
Definition: abilities.cpp:875
The unit is poisoned - it loses health each turn.
Definition: unit.hpp:812
const unit_map & get_units() const
Definition: display.hpp:121
bool has_ability_type(const std::string &ability) const
Check if the unit has an ability of a specific type.
Definition: abilities.cpp:457
unit_const_ptr other_
const std::string & type() const
Definition: attack_type.hpp:42
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:129
#define d
static std::vector< DIRECTION > parse_directions(const std::string &str)
Parse_directions takes a comma-separated list, and filters out any invalid directions.
Definition: location.cpp:123
-file sdl_utils.hpp
std::pair< int, bool > combat_ability(const std::string &ability, int abil_value=0, bool backstab_pos=false) const
Definition: attack.cpp:1748
int num_attacks() const
Definition: attack_type.hpp:51
std::vector< std::tuple< std::string, t_string, t_string, t_string > > ability_tooltips() const
Gets the names and descriptions of this unit&#39;s abilities.
Definition: abilities.cpp:312
A single unit type that the player may recruit.
Definition: types.hpp:42
bool filter_base_matches(const config &cfg, int def)
Definition: abilities.cpp:1194
map_location loc_
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
int as_decimal() const
Returns variant&#39;s internal representation of decimal number: ie, 1.234 is represented as 1234...
Definition: variant.cpp:301
bool get_special_bool(const std::string &special, bool simple_check=false) const
Returns whether or not *this has a special with a tag or id equal to special.
Definition: abilities.cpp:639
std::vector< std::pair< int, int > > parse_ranges(const std::string &str)
std::string deprecated_message(const std::string &elem_name, DEP_LEVEL level, const version_info &version, const std::string &detail)
Definition: deprecation.cpp:29
std::pair< const config *, map_location > unit_ability
Definition: unit.hpp:48
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:252
bool ability_affects_adjacent(const std::string &ability, const config &cfg, int dir, const map_location &loc, const unit &from) const
Check if an ability affects adjacent units.
Definition: abilities.cpp:412
filter_context * filter_con
Definition: resources.cpp:23
bool valid() const
Definition: location.hpp:93
specials_context_t(const attack_type &weapon, bool attacking)
Initialize weapon specials context for listing.
Definition: abilities.cpp:843
std::string type
Definition: formula.hpp:105
bool blank() const
Tests for an attribute that was never set.
game_board * gameboard
Definition: resources.cpp:20
bool is_enemy(int n) const
Definition: team.hpp:243
std::vector< std::pair< t_string, t_string > > special_tooltips(boost::dynamic_bitset<> *active_list=nullptr) const
Returns a vector of names and descriptions for the specials of *this.
Definition: abilities.cpp:719
map_display and display: classes which take care of displaying the map and game-data on the screen...
std::pair< int, map_location > get_extremum(const std::string &key, int def, const TComp &comp) const
Definition: abilities.cpp:524
std::array< map_location, 6 > adjacent_loc_array_t
Definition: location.hpp:170
unit_ability_list get_specials(const std::string &special) const
Returns the currently active specials as an ability list, given the current context (see set_specials...
Definition: abilities.cpp:687
Encapsulates the map of the game.
Definition: location.hpp:42
unit_iterator find(std::size_t id)
Definition: map.cpp:311
#define UNUSED(x)
Definition: global.hpp:34
bool get_ability_bool(const std::string &tag_name, const map_location &loc) const
Checks whether this unit currently possesses or is affected by a given ability.
Definition: abilities.cpp:143
pointer get_shared_ptr() const
This is exactly the same as operator-> but it&#39;s slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:220
bool special_active(const config &special, AFFECTS whom, const std::string &tag_name, bool include_backstab=true) const
Returns whether or not the given special is active for the specified unit, based on the current conte...
Definition: abilities.cpp:1033
std::size_t i
Definition: function.cpp:933
std::stringstream & wml_error()
Use this logger to send errors due to deprecated WML.
Definition: log.cpp:269
bool is_for_listing_
int damage() const
Definition: attack_type.hpp:50
mock_party p
static map_location::DIRECTION s
unit_const_ptr self_
int modified_damage(bool is_backstab) const
Returns the damage per attack of this weapon, considering specials.
Definition: abilities.cpp:905
const display_context & get_disp_context() const
Definition: display.hpp:168
int get_composite_value() const
Definition: abilities.hpp:49
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:44
unit_ability_list get_abilities(const std::string &tag_name, const map_location &loc, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr) const
Gets the unit&#39;s active abilities of a particular type if it were on a specified location.
Definition: abilities.cpp:183
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
bool ability_affects_self(const std::string &ability, const config &cfg, const map_location &loc) const
Check if an ability affects the owning unit.
Definition: abilities.cpp:436
config & cfg
Definition: config.hpp:552
bool is_village(const map_location &loc) const
Definition: map.cpp:65
bool empty() const
Definition: tstring.hpp:182
double t
Definition: astarsearch.cpp:64
bool find(E event, F functor)
Tests whether an event handler is available.
const_attack_ptr other_attack_
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
Standard logging facilities (interface).
V::result_type apply_visitor(const V &visitor) const
Applies a visitor to the underlying variant.
static const map_location & null_location()
Definition: location.hpp:85
Container associating units to locations.
Definition: map.hpp:99
#define e
std::vector< std::string > get_ability_list() const
Get a list of all abilities by ID.
Definition: abilities.cpp:228
std::shared_ptr< const attack_type > parent
bool ability_active(const std::string &ability, const config &cfg, const map_location &loc) const
Check if an ability is active.
Definition: abilities.cpp:340
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
bool valid() const
Definition: map.hpp:276
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:37
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
bool empty() const
Definition: config.cpp:884
map_location self_loc_
static lg::log_domain log_engine("engine")
std::pair< int, map_location > highest(const std::string &key, int def=0) const
Definition: unit.hpp:56