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