The Battle for Wesnoth  1.19.13+dev
unit.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "movetype.hpp"
20 #include "units/id.hpp"
21 #include "units/ptr.hpp"
22 #include "units/attack_type.hpp"
23 #include "units/race.hpp"
24 #include "utils/optional_fwd.hpp"
25 #include "utils/variant.hpp"
26 
27 #include <bitset>
28 #include "utils/optional_fwd.hpp"
29 
30 class team;
32 class vconfig;
33 struct color_t;
34 
35 /** Data typedef for unit_ability_list. */
37 {
42  {
43  }
44 
45  /**
46  * Used by the formula in the ability.
47  * The REAL location of the student (not the 'we are assuming the student is at this position' location)
48  * once unit_ability_list can contain abilities from different 'students', as it contains abilities from
49  * a unit aswell from its opponents (abilities with apply_to= opponent)
50  */
52  /**
53  * The location of the teacher, that is the unit who owns the ability tags
54  * (different from student because of [affect_adjacent])
55  */
57  /** The contents of the ability tag, never nullptr. */
59 };
60 
62 {
63 public:
65 
66  // Implemented in unit_abilities.cpp
67  std::pair<int, map_location> highest(const std::string& key, int def=0) const
68  {
69  return get_extremum(key, def, std::less<int>());
70  }
71  std::pair<int, map_location> lowest(const std::string& key, int def=0) const
72  {
73  return get_extremum(key, def, std::greater<int>());
74  }
75 
76  template<typename TComp>
77  std::pair<int, map_location> get_extremum(const std::string& key, int def, const TComp& comp) const;
78 
79  // The following make this class usable with standard library algorithms and such
81  typedef std::vector<unit_ability>::const_iterator const_iterator;
82 
83  iterator begin() { return cfgs_.begin(); }
84  const_iterator begin() const { return cfgs_.begin(); }
85  iterator end() { return cfgs_.end(); }
86  const_iterator end() const { return cfgs_.end(); }
87 
88  // Vector access
89  bool empty() const { return cfgs_.empty(); }
90  unit_ability& front() { return cfgs_.front(); }
91  const unit_ability& front() const { return cfgs_.front(); }
92  unit_ability& back() { return cfgs_.back(); }
93  const unit_ability& back() const { return cfgs_.back(); }
94  std::size_t size() { return cfgs_.size(); }
95 
96  iterator erase(const iterator& erase_it) { return cfgs_.erase(erase_it); }
97  iterator erase(const iterator& first, const iterator& last) { return cfgs_.erase(first, last); }
98 
99  template<typename... T>
100  void emplace_back(T&&... args) { cfgs_.emplace_back(args...); }
101 
102  const map_location& loc() const { return loc_; }
103 
104  /** Appends the abilities from @a other to @a this, ignores other.loc() */
105  void append(const unit_ability_list& other)
106  {
107  std::copy(other.begin(), other.end(), std::back_inserter(cfgs_ ));
108  }
109 
110  /**
111  * Appends any abilities from @a other for which the given condition returns true to @a this, ignores other.loc().
112  *
113  * @param other where to copy the elements from
114  * @param predicate a single-argument function that takes a reference to an element and returns a bool
115  */
116  template<typename Predicate>
117  void append_if(const unit_ability_list& other, const Predicate& predicate)
118  {
119  std::copy_if(other.begin(), other.end(), std::back_inserter(cfgs_ ), predicate);
120  }
121 
122 private:
123  // Data
124  std::vector<unit_ability> cfgs_;
126 };
127 
128 /**
129  * This class represents a *single* unit of a specific type.
130  */
131 class unit : public std::enable_shared_from_this<unit>
132 {
133 public:
134  /**
135  * Clear this unit status cache for all units. Currently only the hidden
136  * status of units is cached this way.
137  */
138  static void clear_status_caches();
139 
140  /** The path to the leader crown overlay. */
141  static const std::string& leader_crown();
142 
143 private:
144  void init(const config& cfg, bool use_traits = false, const vconfig* vcfg = nullptr);
145 
146  void init(const unit_type& t, int side, bool real_unit, unit_race::GENDER gender = unit_race::NUM_GENDERS, const std::string& variation = "");
147 
148  // Copy constructor
149  unit(const unit& u);
150 
151  struct unit_ctor_t {};
152 
153 public:
154  //private default ctor, butusing constructor to allow calling make_shared<unit> in create().
155  unit(unit_ctor_t);
156  unit() = delete;
157 
158 private:
160  {
174  //note that UA_ATTACKS only tracks added/deleted attacks, not modified attacks.
181  UA_COUNT
182  };
183 
185  {
186  changed_attributes_[int(attr)] = true;
187  }
188 
189  bool get_attacks_changed() const;
190 
192  {
193  return changed_attributes_[int(attr)];
194  }
195 
197 
198 public:
199  /** Initializes a unit from a config */
200  static unit_ptr create(const config& cfg, bool use_traits = false, const vconfig* vcfg = nullptr)
201  {
202  unit_ptr res = std::make_shared<unit>(unit_ctor_t());
203  res->init(cfg, use_traits, vcfg);
204  return res;
205  }
206 
207  /**
208  * Initializes a unit from a unit type.
209  *
210  * Only real_unit-s should have random traits, name and gender (to prevent OOS caused by RNG calls)
211  */
212  static unit_ptr create(const unit_type& t, int side, bool real_unit, unit_race::GENDER gender = unit_race::NUM_GENDERS, const std::string& variation = "")
213  {
214  unit_ptr res = std::make_shared<unit>(unit_ctor_t());
215  res->init(t, side, real_unit, gender, variation);
216 
217  return res;
218  }
219 
220  unit_ptr clone() const
221  {
222  return std::shared_ptr<unit>(new unit(*this));
223  }
224 
225  virtual ~unit();
226 
227  unit& operator=(const unit&) = delete;
228 
229  /**
230  * @defgroup unit_advance Advancement functions
231  * @{
232  */
233 
234  /** Advances this unit to another type */
235  void advance_to(const unit_type& t, bool use_traits = false);
236 
237  using advances_to_t = std::vector<std::string>;
238  /**
239  * Gets the possible types this unit can advance to on level-up.
240  *
241  * @returns A list of type IDs this unit may advance to.
242  */
243  const advances_to_t& advances_to() const
244  {
245  return advances_to_;
246  }
247 
248  /**
249  * Gets the names of the possible types this unit can advance to on level-up.
250  *
251  * @returns A list of the names of the types this unit may advance to.
252  */
253  const std::vector<std::string> advances_to_translated() const;
254 
255  /**
256  * Sets this unit's advancement options.
257  *
258  * @param advances_to A list of new type IDs this unit may advance to.
259  */
260  void set_advances_to(const std::vector<std::string>& advances_to);
261 
262  /**
263  * Checks whether this unit has any options to advance to.
264  *
265  * This considers both whether it has types to advance to OR whether any modifications
266  * specify non-type advancement options.
267  *
268  * Note this does not consider unit experience at all, it only checks option availability.
269  * See @ref advances if an experience check is necessary.
270  */
271  bool can_advance() const
272  {
273  return !advances_to_.empty() || !get_modification_advances().empty();
274  }
275 
276  /**
277  * Checks whether this unit is eligible for level-up.
278  *
279  * @retval true This unit has sufficient experience to level up and has advancement
280  * options available.
281  */
282  bool advances() const
283  {
284  return experience_ >= max_experience() && can_advance();
285  }
286 
287  /**
288  * Gets and image path and and associated description for each advancement option.
289  *
290  * Covers both type and modification-based advancements.
291  *
292  * @returns A data map, in image/description format. If the option is a unit type,
293  * advancement, the key is the type's image and the value the type ID.
294  *
295  * If the option is a modification, the key and value are set from config data
296  * (see @ref get_modification_advances).
297  */
298  std::map<std::string, std::string> advancement_icons() const;
299 
300  /**
301  * Gets any non-typed advanced options set by modifications.
302  *
303  * These are usually used to give a unit special advancement options that don't invole transforming to a
304  * new type.
305  *
306  * Note this is not the raw option data. Parsing is performed to ensure each option appears only once.
307  * Use @ref modification_advancements is the raw data is needed.
308  *
309  * @returns A config list of options data. Each option is unique.
310  */
311  std::vector<config> get_modification_advances() const;
312 
313  /**
314  * Gets the image and description data for modification advancements.
315  *
316  * @returns A list of pairs of the image paths(first) and descriptions (second) for
317  * each advancement option.
318  */
319  std::vector<std::pair<std::string, std::string>> amla_icons() const;
320 
321  /** The raw, unparsed data for modification advancements. */
322  const std::vector<config>& modification_advancements() const
323  {
324  return advancements_;
325  }
326 
327  /** Sets the raw modification advancement option data */
328  void set_advancements(std::vector<config> advancements);
329 
330  /**
331  * @}
332  * @defgroup unit_access Basic data setters and getters
333  * @{
334  **/
335 
336 public:
337  /**
338  * The side this unit belongs to.
339  *
340  * Note that side numbers starts from 1, not 0, so be sure to subtract 1 if using as a container index.
341  */
342  int side() const
343  {
344  return side_;
345  }
346 
347  /** Sets the side this unit belongs to. */
348  void set_side(unsigned int new_side)
349  {
350  side_ = new_side;
351  }
352 
353  /** This unit's type, accounting for gender and variation. */
354  const unit_type& type() const
355  {
356  return *type_;
357  }
358 
359  /**
360  * The id of this unit's type.
361  *
362  * If you are dealing with creating units (e.g. recruitment), this is not what you want, as a
363  * variation can change this; use type().parent_id() instead.
364  */
365  const std::string& type_id() const;
366 
367  /** Gets the translatable name of this unit's type. */
368  const t_string& type_name() const
369  {
370  return type_name_;
371  }
372 
373  /**
374  * Gets this unit's id.
375  *
376  * This is a unique string usually set by WML. It should *not* be used for internal tracking in
377  * the unit_map. Use @ref underlying_id for that.
378  */
379  const std::string& id() const
380  {
381  return id_;
382  }
383 
384  /** Sets this unit's string ID. */
385  void set_id(const std::string& id)
386  {
387  id_ = id;
388  }
389 
390  /** This unit's unique internal ID. This should *not* be used for user-facing operations. */
391  std::size_t underlying_id() const
392  {
393  return underlying_id_.value;
394  }
395 
396 private:
397  /** Sets the internal ID. */
398  void set_underlying_id(n_unit::id_manager& id_manager);
399 
400 public:
401  /** Gets this unit's translatable display name. */
402  const t_string& name() const
403  {
404  return name_;
405  }
406 
407  /**
408  * Sets this unit's translatable display name.
409  *
410  * This should only be used internally since it ignores the 'unrenamable' flag.
411  */
412  void set_name(const t_string& name)
413  {
414  name_ = name;
415  }
416 
417  /**
418  * Attempts to rename this unit's translatable display name, taking the 'unrenamable' flag into account.
419  *
420  * If a direct rename is desired, use @ref set_name.
421  * @todo should this also take a t_string?
422  */
423  void rename(const std::string& name)
424  {
425  if(!unrenamable_) {
426  name_ = name;
427  }
428  }
429 
430  /**
431  * Whether this unit can be renamed.
432  *
433  * This flag is considered by @ref rename, but not @ref set_name.
434  */
435  bool unrenamable() const
436  {
437  return unrenamable_;
438  }
439 
440  /**
441  * Sets the 'unrenamable' flag. Usually used for scenario-specific units which should not be renamed.
442  */
444  {
446  }
447 
448  /**
449  * Whether this unit can be dismissed.
450  *
451  * This flag is used by the Unit Recall dialog.
452  */
453  bool dismissable() const
454  {
455  return dismissable_;
456  }
457 
458  /** A message of why this unit cannot be dismissed. */
460  {
461  return dismiss_message_;
462  }
463 
464  /** A detailed description of this unit. */
466  {
467  return description_;
468  }
469 
470  /** A detailed description of this unit. */
471  void set_unit_description(const t_string& new_desc)
472  {
473  description_ = new_desc;
474  }
475 
476  /** The unit's special notes. */
477  std::vector<t_string> unit_special_notes() const;
478 
479  /** The gender of this unit. */
481  {
482  return gender_;
483  }
484 
485  /**
486  * The alignment of this unit.
487  *
488  * This affects the time of day during which this unit's attacks do the most damage.
489  */
491  {
492  return alignment_;
493  }
494 
495  /** Sets the alignment of this unit. */
497  {
500  }
501 
502  /**
503  * Gets this unit's race.
504  *
505  * @returns A pointer to a unit_race object - never nullptr, but it may point
506  * to the null race.
507  */
508  const unit_race* race() const
509  {
510  return race_;
511  }
512 
513  /** The current number of hitpoints this unit has. */
514  int hitpoints() const
515  {
516  return hit_points_;
517  }
518 
519  /** The max number of hitpoints this unit can have. */
520  int max_hitpoints() const
521  {
522  return max_hit_points_;
523  }
524 
525  void set_max_hitpoints(int value)
526  {
528  max_hit_points_ = value;
529  }
530 
531  /** Sets the current hitpoint amount. */
532  void set_hitpoints(int hp)
533  {
534  hit_points_ = hp;
535  }
536 
537  /** The current number of experience points this unit has. */
538  int experience() const
539  {
540  return experience_;
541  }
542 
543  /** The max number of experience points this unit can have. */
544  int max_experience() const
545  {
546  return max_experience_;
547  }
548 
549  void set_max_experience(int value)
550  {
552  max_experience_ = value;
553  }
554 
555  /** The number of experience points this unit needs to level up, or 0 if current XP > max XP. */
556  unsigned int experience_to_advance() const
557  {
558  return std::max(0, max_experience_ - experience_);
559  }
560 
561  /** The number of experience points over max this unit has, or 0 if current XP < max XP. */
562  unsigned int experience_overflow() const
563  {
564  return std::max(0, experience_ - max_experience_);
565  }
566 
567  /** Sets the current experience point amount. */
568  void set_experience(int xp)
569  {
570  experience_ = xp;
571  }
572 
573  /** The current level of this unit. */
574  int level() const
575  {
576  return level_;
577  }
578 
579  /** Sets the current level of this unit. */
580  void set_level(int level)
581  {
583  level_ = level;
584  }
585 
586  /** The ID of the variation of this unit's type. */
587  const std::string& variation() const
588  {
589  return variation_;
590  }
591 
592  /** The ID of the undead variation (ie, dwarf, swimmer) of this unit. */
593  void set_undead_variation(const std::string& value)
594  {
596  undead_variation_ = value;
597  }
598  const std::string& undead_variation() const
599  {
600  return undead_variation_;
601  }
602 
603  /**
604  * An optional profile image to display in Help.
605  *
606  * @returns The specified image, this unit's type's sprite image if empty
607  * or 'unit_image' was set.
608  */
609  std::string small_profile() const;
610 
611  void set_small_profile(const std::string& value)
612  {
614  small_profile_ = value;
615  }
616  /**
617  * An optional profile image displays when this unit is 'speaking' via [message].
618  *
619  * @returns The specified image, this unit's type's sprite image if empty
620  * or 'unit_image' was set.
621  */
622  std::string big_profile() const;
623 
624  void set_big_profile(const std::string& value);
625 
626  /** Whether this unit can recruit other units - ie, are they a leader unit. */
627  bool can_recruit() const
628  {
629  return canrecruit_;
630  }
631 
632  /** Sets whether this unit can recruit other units. */
633  void set_can_recruit(bool canrecruit)
634  {
635  canrecruit_ = canrecruit;
636  }
637 
638  /** The type IDs of the other units this unit may recruit, if possible. */
639  const std::vector<std::string>& recruits() const
640  {
641  return recruit_list_;
642  }
643 
644  /** Sets the recruit list. */
645  void set_recruits(const std::vector<std::string>& recruits);
646 
647  /** How much gold is required to recruit this unit. */
648  int cost() const
649  {
650  return unit_value_;
651  }
652 
653  /** How much gold it costs to recall this unit, or -1 if the side's default
654  * recall cost is used. */
655  int recall_cost() const
656  {
657  return recall_cost_;
658  }
659 
660  /** Sets the cost of recalling this unit. */
662  {
664  }
665 
666  /** Gets the filter constraints upon which units this unit may recall, if able. */
667  const config& recall_filter() const
668  {
669  return filter_recall_;
670  }
671 
672  /** Sets the filter constraints upon which units this unit may recall, if able. */
674  {
676  }
677 
678  /**
679  * Gets this unit's role.
680  *
681  * A role is a special string flag usually used to represent a unit's purpose in a scenario.
682  * It can be filtered on.
683  */
684  const std::string& get_role() const
685  {
686  return role_;
687  }
688 
689  /** Sets a unit's role */
690  void set_role(const std::string& role)
691  {
692  role_ = role;
693  }
694 
695  /**
696  * Gets this unit's usage. This is relevant to the AI.
697  *
698  * Usage refers to how the AI may consider utilizing this unit in combat.
699  * @todo document further
700  */
701  std::string usage() const
702  {
703  return usage_.value_or("");
704  }
705 
706  /** Sets this unit's usage. */
707  void set_usage(const std::string& usage)
708  {
709  usage_ = usage;
710  }
711 
712  /**
713  * Gets any user-defined variables this unit 'owns'.
714  *
715  * These are accessible via WML if the unit's data is serialized to a variable. They're strictly
716  * user-facing; internal engine calculations shouldn't use this.
717  */
719  {
720  return variables_;
721  }
722 
723  /** Const overload of @ref variables. */
724  const config& variables() const
725  {
726  return variables_;
727  }
728 
729  /**
730  * Gets whether this unit is currently hidden on the map.
731  *
732  * Hidden units are not drawn on the main map or the minimap. They are
733  * an implementation detail. For the [hides] ability, see invisible().
734  */
735  bool get_hidden() const
736  {
737  return hidden_;
738  }
739 
740  /** Sets whether the unit is hidden on the map. */
741  void set_hidden(bool state) const;
742 
743  /**
744  * The factor by which the HP bar should be scaled.
745  * Convenience wrapper around the unit_type value.
746  */
747  double hp_bar_scaling() const;
748  /**
749  * The factor by which the XP bar should be scaled.
750  * Convenience wrapper around the unit_type value.
751  */
752  double xp_bar_scaling() const;
753 
754  /**
755  * Whether the unit has been instructed to hold its position.
756  * This excludes it from the unit cycling function.
757  * @return true if it is holding position
758  */
759  bool hold_position() const
760  {
761  return hold_position_;
762  }
763 
764  /**
765  * Toggle the unit's hold position status.
766  */
768  {
770  if(hold_position_) {
771  end_turn_ = true;
772  }
773  }
774 
775  /**
776  * Set whether the user ended their turn
777  * @todo Verify meaning and explain better
778  */
779  void set_user_end_turn(bool value = true)
780  {
781  end_turn_ = value;
782  }
783 
784  /**
785  * Toggle whether the user ended their turn
786  * @todo Verify meaning and explain better
787  */
789  {
790  end_turn_ = !end_turn_;
791  if(!end_turn_) {
792  hold_position_ = false;
793  }
794  }
795 
796  /**
797  * Check whether the user ended their turn
798  * @todo Verify meaning and explain better
799  */
800  bool user_end_turn() const
801  {
802  return end_turn_;
803  }
804 
805  /**
806  * Refresh unit for the beginning of a turn
807  */
808  void new_turn();
809 
810  /**
811  * Refresh unit for the end of a turn
812  */
813  void end_turn();
814 
815  /**
816  * Refresh unit for the beginning of a new scenario
817  */
818  void new_scenario();
819 
820  /**
821  * Damage the unit.
822  * @returns true if the unit dies as a result
823  */
824  bool take_hit(int damage)
825  {
826  hit_points_ -= damage;
827  return hit_points_ <= 0;
828  }
829 
830  /**
831  * Heal the unit
832  * @param amount The number of hitpoints to gain
833  */
834  void heal(int amount);
835 
836  /**
837  * Fully heal the unit, restoring it to max hitpoints
838  */
839  void heal_fully()
840  {
842  }
843 
844  /**
845  * Get the status effects currently affecting the unit.
846  * @return A set of status keys
847  */
848  const std::set<std::string> get_states() const;
849 
850  /**
851  * Check if the unit is affected by a status effect
852  * @param state The status effect to check
853  * @returns true if the unit is affected by the status effect
854  */
855  bool get_state(const std::string& state) const;
856 
857  /**
858  * Set whether the unit is affected by a status effect
859  * @param state The status effect to change
860  * @param value Whether the unit should be affected by the status
861  */
862  void set_state(const std::string& state, bool value);
863 
864  /**
865  * Built-in status effects known to the engine
866  */
867  enum state_t {
868  STATE_SLOWED = 0, /** The unit is slowed - it moves slower and does less damage */
869  STATE_POISONED, /** The unit is poisoned - it loses health each turn */
870  STATE_PETRIFIED, /** The unit is petrified - it cannot move or be attacked */
871  STATE_UNCOVERED, /** The unit is uncovered - it was hiding but has been spotted */
872  STATE_NOT_MOVED, /** The unit has not moved @todo Explain better */
873  STATE_UNHEALABLE, /** The unit cannot be healed */
874  STATE_GUARDIAN, /** The unit is a guardian - it won't move unless a target is sighted */
875  STATE_INVULNERABLE, /** The unit is invulnerable - it cannot be hit by any attack */
876  NUMBER_OF_STATES, /** To set the size of known_boolean_states_ */
877  STATE_UNKNOWN = -1 /** A status effect not known to the engine */
878  };
879 
880  /**
881  * Set whether the unit is affected by a status effect
882  * @param state The status effect to change
883  * @param value Whether the unit should be affected by the status
884  */
885  void set_state(state_t state, bool value);
886 
887  /**
888  * Check if the unit is affected by a status effect
889  * @param state The status effect to check
890  * @returns true if the unit is affected by the status effect
891  */
892  bool get_state(state_t state) const;
893 
894  /**
895  * Convert a string status effect ID to a built-in status effect ID
896  * @returns the state_t representing the status, or STATE_UNKNOWN if it's not built-in
897  */
898  static state_t get_known_boolean_state_id(const std::string& state);
899 
900  /**
901  * Convert a built-in status effect ID to a string status effect ID
902  * @returns the string representing the status, or an empty string for STATE_UNKNOWN
903  */
904  static std::string get_known_boolean_state_name(state_t state);
905 
906  /**
907  * Check if the unit has been poisoned
908  * @returns true if it's poisoned
909  */
910  bool poisoned() const
911  {
912  return get_state(STATE_POISONED);
913  }
914 
915  /**
916  * Check if the unit has been petrified
917  * @returns true if it's petrified
918  */
919  bool incapacitated() const
920  {
921  return get_state(STATE_PETRIFIED);
922  }
923 
924  /**
925  * Check if the unit has been slowed
926  * @returns true if it's slowed
927  */
928  bool slowed() const
929  {
930  return get_state(STATE_SLOWED);
931  }
932 
933  /**
934  * @}
935  * @defgroup unit_atk Attack and resistance functions
936  * @{
937  */
938 
939 public:
940  /** Gets an iterator over this unit's attacks. */
942  {
943  return make_attack_itors(attacks_);
944  }
945 
946  /** Const overload of @ref attacks. */
948  {
949  return make_attack_itors(attacks_);
950  }
951 
952  /**
953  * Adds a new attack to the unit.
954  * @param position An iterator pointing to the attack before which to insert the new one.
955  * @param args The arguments for constructing the attack
956  */
957  template<typename... Args>
958  attack_ptr add_attack(attack_itors::iterator position, Args&&... args)
959  {
961  return *attacks_.emplace(position.base(), new attack_type(std::forward<Args>(args)...));
962  }
963 
964  /**
965  * Remove an attack from the unit
966  * @param atk A pointer to the attack to remove
967  * @return true if the attack was removed, false if it didn't exist on the unit
968  */
969  bool remove_attack(const attack_ptr& atk);
970 
971  /**
972  * Set the unit to have no attacks left for this turn.
973  */
974  void remove_attacks_ai();
975 
976  /**
977  * Calculates the damage this unit would take from a certain attack.
978  *
979  * @param attack The attack to consider.
980  * @param attacker Whether this unit should be considered the attacker.
981  * @param loc The unit's location (to resolve [resistance] abilities)
982  * @param weapon The weapon to check for any abilities or weapon specials
983  *
984  * @returns The expected damage.
985  */
986  int damage_from(const attack_type& attack, bool attacker, const map_location& loc, const_attack_ptr weapon = nullptr) const
987  {
988  return resistance_against(attack, attacker, loc, weapon);
989  }
990 
991  /** The maximum number of attacks this unit may perform per turn, usually 1. */
992  int max_attacks() const
993  {
994  return max_attacks_;
995  }
996 
997  void set_max_attacks(int value)
998  {
1000  max_attacks_ = value;
1001  }
1002 
1003  /**
1004  * Gets the remaining number of attacks this unit can perform this turn.
1005  *
1006  * If the 'incapacitated' status is set, this will always be 0.
1007  */
1008  int attacks_left() const
1009  {
1010  return (attacks_left_ == 0 || incapacitated()) ? 0 : attacks_left_;
1011  }
1012 
1013  /**
1014  * Gets the remaining number of attacks this unit can perform this turn.
1015  *
1016  * @param base_value If false, consider the `incapacitated` flag.
1017  *
1018  * @returns If @a base_value is true, the raw value is returned.
1019  */
1020  int attacks_left(bool base_value) const
1021  {
1022  return base_value ? attacks_left_ : attacks_left();
1023  }
1024 
1025  /**
1026  * Sets the number of attacks this unit has left this turn.
1027  * @param left The number of attacks left
1028  */
1029  void set_attacks(int left)
1030  {
1031  attacks_left_ = std::max<int>(0, left);
1032  }
1033 
1034  /**
1035  * The unit's defense on a given terrain
1036  * @param terrain The terrain to check
1037  */
1038  int defense_modifier(const t_translation::terrain_code& terrain) const;
1039 
1040  /**
1041  * For the provided list of resistance abilities, determine the damage resistance based on which are active and any max_value that's present.
1042  *
1043  * @param resistance_list A list of resistance abilities that the unit has.
1044  * @param damage_name The name of the damage type, for example "blade".
1045  * @return The resistance value for a unit with the provided resistance abilities to the provided damage type.
1046  */
1047  int resistance_value(unit_ability_list resistance_list, const std::string& damage_name) const;
1048 
1049  /**
1050  * The unit's resistance against a given damage type
1051  * @param damage_name The damage type
1052  * @param attacker True if this unit is on the offensive (to resolve [resistance] abilities)
1053  * @param loc The unit's location (to resolve [resistance] abilities)
1054  * @param weapon The weapon to check for any abilities or weapon specials
1055  * @param opp_weapon The opponent's weapon to check for any abilities or weapon specials
1056  */
1057  int resistance_against(const std::string& damage_name, bool attacker, const map_location& loc, const_attack_ptr weapon = nullptr, const const_attack_ptr& opp_weapon = nullptr) const;
1058 
1059  /**
1060  * The unit's resistance against a given attack
1061  * @param atk The attack
1062  * @param attacker True if this unit is on the offensive (to resolve [resistance] abilities)
1063  * @param loc The unit's location (to resolve [resistance] abilities)
1064  * @param weapon The weapon to check for any abilities or weapon specials
1065  */
1066  int resistance_against(const attack_type& atk, bool attacker, const map_location& loc, const_attack_ptr weapon = nullptr) const
1067  {
1068  return resistance_against(atk.type(), attacker, loc , weapon, atk.shared_from_this());
1069  }
1070 
1071  /** Gets resistances without any abilities applied. */
1073  {
1074  return movement_type_.damage_table();
1075  }
1076 
1077 private:
1078  bool resistance_filter_matches(const config& cfg, const std::string& damage_name, int res) const;
1079 
1080  /**
1081  * @}
1082  * @defgroup unit_trait Trait and upkeep functions
1083  * @{
1084  */
1085 public:
1086  /**
1087  * Applies mandatory traits (e.g. undead, mechanical) to a unit and then fills in the remaining traits
1088  * traits until no more are available (leaders have a restricted set of available traits) or the unit has
1089  * its maximum number of traits.
1090  *
1091  * This routine does not apply the effects of added traits to a unit; that must be done by the caller.
1092  *
1093  * Note that random numbers used in config files don't work in multiplayer, so leaders should be barred
1094  * from all random traits until that is fixed. Later the restrictions will be based on play balance.
1095  *
1096  * @param must_have_only Whether random or optional traits should be included or not. If false only
1097  * mandatory traits will be used.
1098  */
1099  void generate_traits(bool must_have_only = false);
1100 
1101  /**
1102  * Gets the names of the currently registered traits.
1103  *
1104  * @returns A list of translatable trait names.
1105  */
1106  const std::vector<t_string>& trait_names() const
1107  {
1108  return trait_names_;
1109  }
1110 
1111  /**
1112  * Gets the descriptions of the currently registered traits.
1113  *
1114  * @returns A list of translatable trait descriptions.
1115  */
1116  const std::vector<t_string>& trait_descriptions() const
1117  {
1118  return trait_descriptions_;
1119  }
1120 
1121  /**
1122  * Gets the ids of the traits corresponding to those returned by trait_names() and
1123  * trait_descriptions(). Omits hidden traits, which are those with an empty name.
1124  *
1125  * @returns A list of trait IDs.
1126  */
1127  std::vector<std::string> trait_nonhidden_ids() const
1128  {
1129  return trait_nonhidden_ids_;
1130  }
1131 
1132  /** Gets a list of the modification this unit currently has.
1133  * @param mod_type type of modification.
1134  * @returns A list of modification IDs.
1135  */
1136  std::vector<std::string> get_modifications_list(const std::string& mod_type) const;
1137 
1138  /**
1139  * Gets a list of the traits this unit currently has, including hidden traits.
1140  *
1141  * @returns A list of trait IDs.
1142  */
1143  std::vector<std::string> get_traits_list() const
1144  {
1145  return get_modifications_list("trait");
1146  }
1147 
1148  std::vector<std::string> get_objects_list() const
1149  {
1150  return get_modifications_list("object");
1151  }
1152 
1153  std::vector<std::string> get_advancements_list() const
1154  {
1155  return get_modifications_list("advancement");
1156  }
1157 
1158  /**
1159  * Register a trait's name and its description for the UI's use.
1160  *
1161  * The resulting data can be fetched with @ref trait_names and @ref trait_descriptions.
1162  *
1163  * @param trait A config containing the trait's attributes.
1164  * @param description The translatable description of the trait.
1165  */
1166  void add_trait_description(const config& trait, const t_string& description);
1167 
1168  /**
1169  * Gets the amount of gold this unit costs a side per turn.
1170  *
1171  * This fetches an actual numeric gold value:
1172  * - If can_recruit is true, no upkeep is paid (0 is returned).
1173  * - If a special upkeep flag is set, the associated gold amount is returned (see @ref upkeep_value_visitor).
1174  * - If a numeric value is already set, it is returned directly.
1175  *
1176  * @returns A gold value, evaluated based on the state of @ref upkeep_raw.
1177  */
1178  int upkeep() const;
1179 
1181  {
1182  static std::string type() { static std::string v = "full"; return v; }
1183  };
1184 
1186  {
1187  static std::string type() { static std::string v = "loyal"; return v; }
1188  };
1189 
1190  using upkeep_t = utils::variant<upkeep_full, upkeep_loyal, int>;
1191 
1192  /** Visitor helper class to fetch the appropriate upkeep value. */
1194 #ifdef USING_BOOST_VARIANT
1195  : public boost::static_visitor<int>
1196 #endif
1197  {
1198  public:
1199  explicit upkeep_value_visitor(const unit& unit) : u_(unit) {}
1200 
1201  /** Full upkeep equals the unit's level. */
1202  int operator()(const upkeep_full&) const
1203  {
1204  return u_.level();
1205  }
1206 
1207  /** Loyal units cost no upkeep. */
1208  int operator()(const upkeep_loyal&) const
1209  {
1210  return 0;
1211  }
1212 
1213  int operator()(int v) const
1214  {
1215  return v;
1216  }
1217 
1218  private:
1219  const unit& u_;
1220  };
1221 
1222  /** Visitor helper struct to fetch the upkeep type flag if applicable, or the the value otherwise. */
1224 #ifdef USING_BOOST_VARIANT
1225  : public boost::static_visitor<std::string>
1226 #endif
1227  {
1228  template<typename T>
1229  std::enable_if_t<!std::is_same_v<int, T>, std::string>
1230  operator()(T&) const
1231  {
1232  // Any special upkeep type should have an associated @ref type getter in its helper struct.
1233  return T::type();
1234  }
1235 
1236  std::string operator()(int v) const
1237  {
1238  return std::to_string(v);
1239  }
1240  };
1241 
1242  /** Visitor helper class to parse the upkeep value from a config. */
1244 #ifdef USING_BOOST_VARIANT
1245  : public boost::static_visitor<upkeep_t>
1246 #endif
1247  {
1248  public:
1249  template<typename N>
1250  std::enable_if_t<std::is_arithmetic_v<N>, upkeep_t>
1251  operator()(N n) const
1252  {
1253  if(n == 0) return upkeep_loyal();
1254  if(n < 0) throw std::invalid_argument(std::to_string(n));
1255  return static_cast<int>(n);
1256  }
1257 
1258  template<typename B>
1259  std::enable_if_t<std::is_convertible_v<B, bool> && !std::is_arithmetic_v<B>, upkeep_t>
1260  operator()(B b) const
1261  {
1262  throw std::invalid_argument(b.str());
1263  }
1264 
1265  upkeep_t operator()(utils::monostate) const
1266  {
1267  return upkeep_full();
1268  }
1269 
1270  upkeep_t operator()(const std::string& s) const
1271  {
1272  if(s == "loyal" || s == "free")
1273  return upkeep_loyal();
1274  if(s == "full")
1275  return upkeep_full();
1276  throw std::invalid_argument(s);
1277  }
1278  };
1279 
1280  /**
1281  * Gets the raw variant controlling the upkeep value.
1282  *
1283  * This should not usually be called directly. To get an actual numeric value of upkeep use @ref upkeep.
1284  */
1286  {
1287  return upkeep_;
1288  }
1289 
1290  /** Sets the upkeep value to a specific value value. Does not necessarily need to be numeric */
1292  {
1293  upkeep_ = v;
1294  }
1295 
1296  /** Gets whether this unit is loyal - ie, it costs no upkeep. */
1297  bool loyal() const;
1298 
1299  void set_loyal(bool loyal);
1300 
1301  /** Gets whether this unit is fearless - ie, unaffected by time of day. */
1302  bool is_fearless() const
1303  {
1304  return is_fearless_;
1305  }
1306 
1307  /** Gets whether this unit is healthy - ie, always rest heals. */
1308  bool is_healthy() const
1309  {
1310  return is_healthy_;
1311  }
1312 
1313  /** Gets if this unit own ability of @a tag_name type with [affect_adjacent] subtags. */
1314  utils::optional<std::size_t> affect_distant(const std::string& tag_name) const
1315  {
1316  auto iter = affect_distant_.find(tag_name);
1317  return iter != affect_distant_.end() ? iter->second : utils::nullopt;
1318  }
1319 
1320  /** Gets if this unit own ability with [affect_adjacent] subtags. */
1321  utils::optional<std::size_t> has_ability_distant() const
1322  {
1323  return has_ability_distant_;
1324  }
1325 
1326  /** Gets if this unit own ability with [affect_adjacent] subtags in same time what halo_image or overlay_image attributes. */
1327  utils::optional<std::size_t> has_ability_distant_image() const
1328  {
1330  }
1331 
1332  /**
1333  * @}
1334  * @defgroup unit_mvmt Movement and location functions
1335  * @{
1336  */
1337 
1338 public:
1339  /** The maximum moves this unit has. */
1340  int total_movement() const
1341  {
1342  return max_movement_;
1343  }
1344 
1345  void set_total_movement(int value)
1346  {
1348  max_movement_ = value;
1349  }
1350 
1351  /**
1352  * Gets how far a unit can move, considering the `incapacitated` flag.
1353  *
1354  * @returns The remaining movement, or zero if incapacitated.
1355  */
1356  int movement_left() const
1357  {
1358  return (movement_ == 0 || incapacitated()) ? 0 : movement_;
1359  }
1360 
1361  /**
1362  * Gets how far a unit can move.
1363  *
1364  * @param base_value If false, consider the `incapacitated` flag.
1365  *
1366  * @returns If @a base_value is true, the raw value is returned.
1367  */
1368  int movement_left(bool base_value) const
1369  {
1370  return base_value ? movement_ : movement_left();
1371  }
1372 
1373  /**
1374  * Set this unit's remaining movement to @a moves.
1375  *
1376  * This does not affect maximum movement.
1377  *
1378  * @param moves The new number of moves
1379  * @param unit_action If to true, the "end turn" and "hold position" flags will be cleared
1380  * (as they should be if a unit acts, as opposed to the movement being set
1381  * by the engine for other reasons).
1382  */
1383  void set_movement(int moves, bool unit_action = false);
1384 
1385  /** Checks if this unit has moved. */
1386  bool has_moved() const
1387  {
1388  return movement_left() != total_movement();
1389  }
1390 
1391  /** Sets the unit to have no moves left for this turn. */
1392  void remove_movement_ai();
1393 
1394  /**
1395  * Checks whether this unit is 'resting'.
1396  *
1397  * Resting refers to whether this unit has not moved yet this turn. Note that this can be true even
1398  * if @ref movement_left is not equal to @ref total_movement.
1399  */
1400  bool resting() const
1401  {
1402  return resting_;
1403  }
1404 
1405  /** Sets this unit's resting status. */
1406  void set_resting(bool rest)
1407  {
1408  resting_ = rest;
1409  }
1410 
1411  /** Tests whether the unit has a zone-of-control, considering @ref incapacitated. */
1412  bool emits_zoc() const
1413  {
1414  return emit_zoc_ && !incapacitated();
1415  }
1416 
1417  /** Gets the raw zone-of-control flag, disregarding @ref incapacitated. */
1418  bool get_emit_zoc() const
1419  {
1420  return emit_zoc_;
1421  }
1422 
1423  /** Sets the raw zone-of-control flag. */
1424  void set_emit_zoc(bool val)
1425  {
1427  emit_zoc_ = val;
1428  }
1429 
1430  /** The current map location this unit is at. */
1432  {
1433  return loc_;
1434  }
1435 
1436  /**
1437  * Sets this unit's map location.
1438  *
1439  * Note this should only be called by unit_map or for temporary units.
1440  */
1442  {
1443  loc_ = loc;
1444  }
1445 
1446  /** The current direction this unit is facing within its hex. */
1448  {
1449  return facing_;
1450  }
1451 
1452  /** The this unit's facing. */
1453  void set_facing(map_location::direction dir) const;
1454 
1455  /** Gets whether this unit has a multi-turn destination set. */
1456  bool has_goto() const
1457  {
1458  return get_goto().valid();
1459  }
1460 
1461  /** The map location to which this unit is moving over multiple turns, if any. */
1462  const map_location& get_goto() const
1463  {
1464  return goto_;
1465  }
1466 
1467  /** Sets this unit's long term destination. */
1468  void set_goto(const map_location& new_goto)
1469  {
1470  goto_ = new_goto;
1471  }
1472 
1473  /** Gets the unit's vision points. */
1474  int vision() const
1475  {
1476  return vision_ < 0 ? max_movement_ : vision_;
1477  }
1478 
1479  /** Gets the unit's jamming points. */
1480  int jamming() const
1481  {
1482  return jamming_;
1483  }
1484 
1485  /** Check whether the unit's move has been interrupted. */
1486  bool move_interrupted() const
1487  {
1488  return movement_left() > 0 && interrupted_move_.x >= 0 && interrupted_move_.y >= 0;
1489  }
1490 
1491  /** Get the target location of the unit's interrupted move. */
1493  {
1494  return interrupted_move_;
1495  }
1496 
1497  /** Set the target location of the unit's interrupted move. */
1498  void set_interrupted_move(const map_location& interrupted_move)
1499  {
1500  interrupted_move_ = interrupted_move;
1501  }
1502 
1503  /** Get the unit's movement type. */
1504  const movetype& movement_type() const
1505  {
1506  return movement_type_;
1507  }
1508 
1509  /**
1510  * Get the unit's movement cost on a particular terrain
1511  * @param terrain The terrain to check
1512  * @returns the number of movement points to enter that terrain
1513  */
1514  int movement_cost(const t_translation::terrain_code& terrain) const
1515  {
1517  }
1518 
1519  /**
1520  * Get the unit's vision cost on a particular terrain
1521  * @param terrain The terrain to check
1522  * @returns the number of vision points to see into that terrain
1523  */
1524  int vision_cost(const t_translation::terrain_code& terrain) const
1525  {
1527  }
1528 
1529  /**
1530  * Get the unit's jamming cost on a particular terrain
1531  * @param terrain The terrain to check
1532  * @returns the number of jamming points to jam that terrain
1533  */
1534  int jamming_cost(const t_translation::terrain_code& terrain) const
1535  {
1537  }
1538 
1539  /** Check if the unit is a flying unit. */
1540  bool is_flying() const
1541  {
1542  return movement_type_.is_flying();
1543  }
1544 
1545  /**
1546  * @}
1547  * @defgroup unit_mod Modification functions
1548  * @{
1549  */
1550 
1551 public:
1552  /** Get the raw modifications. */
1554  {
1555  return modifications_;
1556  }
1557 
1558  /** Set the raw modifications. */
1559  const config& get_modifications() const
1560  {
1561  return modifications_;
1562  }
1563 
1564  /**
1565  * Count modifications of a particular type.
1566  * @param type The type of modification to count.
1567  * Valid values are "advancement", "trait", "object"
1568  * @param id The ID of the modification to count
1569  * @return The total number of modifications of that type and ID.
1570  */
1571  std::size_t modification_count(const std::string& type, const std::string& id) const;
1572 
1573  /**
1574  * Count modifications of a particular type.
1575  * @param type The type of modification to count.
1576  * Valid values are "advancement", "trait", "object"
1577  * @return The total number of modifications of that type.
1578  */
1579  std::size_t modification_count(const std::string& type) const;
1580 
1581  std::size_t traits_count() const
1582  {
1583  return modification_count("trait");
1584  }
1585 
1586  std::size_t objects_count() const
1587  {
1588  return modification_count("object");
1589  }
1590 
1591  std::size_t advancements_count() const
1592  {
1593  return modification_count("advancement");
1594  }
1595 
1596  /**
1597  * Add a new modification to the unit.
1598  * @param type The type of modification to add.
1599  * Valid values are "advancement", "trait", "object"
1600  * @param modification The details of the modification
1601  * @param no_add If true, apply the modification but don't save it for unit rebuild time.
1602  * Defaults to false.
1603  */
1604  void add_modification(const std::string& type, const config& modification, bool no_add = false);
1605 
1606  /**
1607  * Clears those modifications whose duration has expired.
1608  *
1609  * @param duration If empty, all temporary modifications (those not lasting forever) expire.
1610  * Otherwise, modifications whose duration equals @a duration expire.
1611  */
1612  void expire_modifications(const std::string& duration);
1613 
1614  static const std::set<std::string> builtin_effects;
1615 
1616  /**
1617  * Apply a builtin effect to the unit.
1618  * @param type The effect to apply. Must be one of the effects in @ref builtin_effects.
1619  * @param effect The details of the effect
1620  */
1621  void apply_builtin_effect(const std::string& type, const config& effect);
1622 
1623  /**
1624  * Construct a string describing a built-in effect.
1625  * @param type The effect to describe. Must be one of the effects in @ref builtin_effects.
1626  * @param effect The details of the effect
1627  */
1628  std::string describe_builtin_effect(const std::string& type, const config& effect);
1629 
1630  /** Re-apply all saved modifications. */
1631  void apply_modifications();
1632 
1633  /**
1634  * @}
1635  * @defgroup unit_img Image and animations functions
1636  * @{
1637  */
1638 
1639 public:
1640  /** @todo Document this */
1642  {
1643  return *anim_comp_;
1644  }
1645 
1646  /** The name of the file to game_display (used in menus). */
1647  std::string absolute_image() const;
1648 
1649  /** The default image to use for animation frames with no defined image. */
1650  std::string default_anim_image() const;
1651 
1652  /** Get the unit's halo image. */
1653  std::string image_halo() const
1654  {
1655  return halo_.value_or("");
1656  }
1657 
1658  std::vector<std::string> halo_or_icon_abilities(const std::string& image_type) const;
1659 
1660  /** Get the [halo] abilities halo image(s). */
1661  std::vector<std::string> halo_abilities() const
1662  {
1663  return halo_or_icon_abilities("halo");
1664  }
1665 
1666  /** Set the unit's halo image. */
1667  void set_image_halo(const std::string& halo);
1668 
1669  /** Get the unit's ellipse image. */
1670  std::string image_ellipse() const
1671  {
1672  return ellipse_.value_or("");
1673  }
1674 
1675  /** Set the unit's ellipse image. */
1676  void set_image_ellipse(const std::string& ellipse)
1677  {
1678  appearance_changed_ = true;
1679  ellipse_ = ellipse;
1680  }
1681 
1682  /**
1683  * Get the source color palette to use when recoloring the unit's image.
1684  */
1685  const std::string& flag_rgb() const;
1686 
1687  /** Constructs a recolor (RC) IPF string for this unit's team color. */
1688  std::string TC_image_mods() const;
1689 
1690  /** Gets any IPF image mods applied by effects. */
1691  const std::string& effect_image_mods() const
1692  {
1693  return image_mods_;
1694  }
1695 
1696  /**
1697  * Gets an IPF string containing all IPF image mods.
1698  *
1699  * @returns An amalgamation of @ref effect_image_mods followed by @ref TC_image_mods.
1700  */
1701  std::string image_mods() const;
1702 
1703  /** Get the unit's overlay images. */
1704  const std::vector<std::string>& overlays() const
1705  {
1706  return overlays_;
1707  }
1708 
1709  /** Get the [overlay] ability overlay images. */
1710  std::vector<std::string> overlays_abilities() const
1711  {
1712  return halo_or_icon_abilities("overlay");
1713  }
1714 
1715  /**
1716  * Color for this unit's *current* hitpoints.
1717  *
1718  * @returns A color between green and red representing how wounded this unit is.
1719  * The maximum_hitpoints are considered as base.
1720  */
1721  color_t hp_color() const;
1722  static color_t hp_color_max();
1723 
1724  /**
1725  * Color for this unit's hitpoints.
1726  *
1727  * @param hitpoints The number of hitpoints the color represents.
1728  * @returns The color considering the current hitpoints as base.
1729  */
1730  color_t hp_color(int hitpoints) const;
1731 
1732  /**
1733  * Color for this unit's XP. See also @ref hp_color
1734  */
1735  color_t xp_color() const;
1736  static color_t xp_color(int xp_to_advance, bool can_advance, bool has_amla);
1737 
1738  /**
1739  * @}
1740  * @defgroup unit_abil Ability functions
1741  * @{
1742  */
1743 
1744 public:
1745  /**
1746  * Checks whether this unit currently possesses or is affected by a given ability.
1747  *
1748  * This means that the ability could be owned by this unit itself or by an adjacent unit, should
1749  * the ability affect an AoE in which this unit happens to be.
1750  *
1751  * @param tag_name The name of the ability to check for.
1752  * @param loc The location around which to check for affected units. This may or
1753  * may not be the location of this unit.
1754  */
1755  bool get_ability_bool(const std::string& tag_name, const map_location& loc) const;
1756 
1757  /**
1758  * Checks whether this unit currently possesses or is affected by a given ability.
1759  *
1760  * This means that the ability could be owned by this unit itself or by an adjacent unit, should
1761  * the ability affect an AoE in which this unit happens to be.
1762  *
1763  * This overload uses the location of this unit for calculations.
1764  *
1765  * @param tag_name The name of the ability to check for.
1766  */
1767  bool get_ability_bool(const std::string& tag_name) const
1768  {
1769  return get_ability_bool(tag_name, loc_);
1770  }
1771 
1772  /** Checks whether this unit currently possesses a given ability, and that that ability is active.
1773  * @return True if the ability @a tag_name is active.
1774  * @param cfg the const config to one of abilities @a tag_name checked.
1775  * @param ability name of ability type checked.
1776  * @param loc location of the unit checked.
1777  */
1778  bool get_self_ability_bool(const config& cfg, const std::string& ability, const map_location& loc) const;
1779  /** Checks whether this unit currently possesses a given ability of leadership type
1780  * @return True if the ability @a tag_name is active.
1781  * @param special the const config to one of abilities @a tag_name checked.
1782  * @param tag_name name of ability type checked.
1783  * @param loc location of the unit checked.
1784  * @param weapon the attack used by unit checked in this function.
1785  * @param opp_weapon the attack used by opponent to unit checked.
1786  */
1787  bool get_self_ability_bool_weapon(const config& special, const std::string& tag_name, const map_location& loc, const const_attack_ptr& weapon = nullptr, const const_attack_ptr& opp_weapon = nullptr) const;
1788  /** Checks whether this unit is affected by a given ability, and that that ability is active.
1789  * @return True if the ability @a tag_name is active.
1790  * @param cfg the const config to one of abilities @a ability checked.
1791  * @param ability name of ability type checked.
1792  * @param loc location of the unit checked.
1793  * @param from unit distant to @a this is checked in case of [affect_adjacent] abilities.
1794  * @param from_loc the 'other unit' location.
1795  * @param dist distance between unit distant and @a this.
1796  * @param dir direction to research a unit distant to @a this.
1797  */
1798  bool get_adj_ability_bool(const config& cfg, const std::string& ability, std::size_t dist, int dir, const map_location& loc, const unit& from, const map_location& from_loc) const;
1799  /** Checks whether this unit is affected by a given ability of leadership type
1800  * @return True if the ability @a tag_name is active.
1801  * @param special the const config to one of abilities @a tag_name checked.
1802  * @param tag_name name of ability type checked.
1803  * @param loc location of the unit checked.
1804  * @param from unit adjacent to @a this is checked in case of [affect_adjacent] abilities.
1805  * @param from_loc location of the @a from unit.
1806  * @param weapon the attack used by unit checked in this function.
1807  * @param opp_weapon the attack used by opponent to unit checked.
1808  * @param dist distance between unit distant and @a this.
1809  * @param dir direction to research a unit distant to @a this.
1810  */
1811  bool get_adj_ability_bool_weapon(const config& special, const std::string& tag_name, std::size_t dist, int dir, const map_location& loc, const unit& from, const map_location& from_loc, const const_attack_ptr& weapon, const const_attack_ptr& opp_weapon) const;
1812 
1813  /**
1814  * Gets the unit's active abilities of a particular type if it were on a specified location.
1815  * @param tag_name The type of ability to check for
1816  * @param loc The location to use for resolving abilities
1817  * @return A list of active abilities, paired with the location they are active on
1818  */
1819  unit_ability_list get_abilities(const std::string& tag_name, const map_location& loc) const;
1820 
1821  /**
1822  * Gets the unit's active abilities of a particular type.
1823  * @param tag_name The type of ability to check for
1824  * @return A list of active abilities, paired with the location they are active on
1825  */
1826  unit_ability_list get_abilities(const std::string& tag_name) const
1827  {
1828  return get_abilities(tag_name, loc_);
1829  }
1830 
1831  unit_ability_list get_abilities_weapons(const std::string& tag_name, const map_location& loc, const_attack_ptr weapon = nullptr, const_attack_ptr opp_weapon = nullptr) const;
1832 
1833  unit_ability_list get_abilities_weapons(const std::string& tag_name, const_attack_ptr weapon = nullptr, const_attack_ptr opp_weapon = nullptr) const
1834  {
1835  return get_abilities_weapons(tag_name, loc_, weapon, opp_weapon);
1836  }
1837 
1838  const config &abilities() const { return abilities_; }
1839 
1840  const std::set<std::string>& checking_tags() const { return checking_tags_; };
1841 
1842  /**
1843  * Gets the names and descriptions of this unit's abilities. Location-independent variant
1844  * with all abilities shown as active.
1845  *
1846  * @returns A list of quadruples consisting of (in order) id, base name,
1847  * male or female name as appropriate for the unit, and description.
1848  */
1849  std::vector<std::tuple<std::string, t_string, t_string, t_string>>
1850  ability_tooltips() const;
1851 
1852  /**
1853  * Gets the names and descriptions of this unit's abilities.
1854  *
1855  * @param active_list This vector will be the same length as the returned one and will
1856  * indicate whether or not the corresponding ability is active.
1857  *
1858  * @param loc The location on which to resolve the ability.
1859  *
1860  * @returns A list of quadruples consisting of (in order) id, base name,
1861  * male or female name as appropriate for the unit, and description.
1862  */
1863  std::vector<std::tuple<std::string, t_string, t_string, t_string>>
1864  ability_tooltips(boost::dynamic_bitset<>& active_list, const map_location& loc) const;
1865 
1866  /** Get a list of all abilities by ID. */
1867  std::vector<std::string> get_ability_list() const;
1868 
1869  /**
1870  * Check if the unit has an ability of a specific type.
1871  * @param ability The type of ability (tag name) to check for.
1872  * @returns true if the ability is present
1873  */
1874  bool has_ability_type(const std::string& ability) const;
1875 
1876  /**
1877  * Check if the unit has an ability of a specific ID.
1878  * @param ability The ID of ability to check for.
1879  * @returns true if the ability is present
1880  */
1881  bool has_ability_by_id(const std::string& ability) const;
1882 
1883  /**
1884  * Removes a unit's abilities with a specific ID.
1885  * @param ability The type of ability (tag name) to remove.
1886  */
1887  void remove_ability_by_id(const std::string& ability);
1888 
1889  /**
1890  * Removes a unit's abilities with a specific ID or other attribute.
1891  * @param filter the config of ability to remove.
1892  */
1894 
1895  /**
1896  * Verify what abilities attributes match with filter.
1897  * @param cfg the config of ability to check.
1898  * @param tag_name the tag name of ability to check.
1899  * @param filter the filter used for checking.
1900  */
1901  bool ability_matches_filter(const config & cfg, const std::string& tag_name, const config & filter) const;
1902 
1903 
1904 private:
1905 
1906  /**
1907  * Helper similar to std::unique_lock for detecting when calculations such as abilities
1908  * have entered infinite recursion.
1909  *
1910  * This assumes that there's only a single thread accessing the unit, it's a lightweight
1911  * increment/decrement counter rather than a mutex.
1912  */
1914  friend class unit;
1915  /**
1916  * Only expected to be called in update_variables_recursion(), which handles some of the checks.
1917  */
1918  explicit recursion_guard(const unit& u, const config& ability);
1919  public:
1920  /**
1921  * Construct an empty instance, only useful for extending the lifetime of a
1922  * recursion_guard returned from unit.update_variables_recursion() by
1923  * std::moving it to an instance declared in a larger scope.
1924  */
1925  explicit recursion_guard();
1926 
1927  /**
1928  * Returns true if a level of recursion was available at the time when update_variables_recursion()
1929  * created this object.
1930  */
1931  operator bool() const;
1932 
1933  recursion_guard(recursion_guard&& other) noexcept;
1934  recursion_guard(const recursion_guard& other) = delete;
1936  recursion_guard& operator=(const recursion_guard&) = delete;
1937  ~recursion_guard();
1938  private:
1939  std::shared_ptr<const unit> parent;
1940  };
1941 
1942  recursion_guard update_variables_recursion(const config& ability) const;
1943 
1944  const std::set<std::string> checking_tags_{"disable", "attacks", "damage", "chance_to_hit", "berserk", "swarm", "drains", "heal_on_hit", "plague", "slow", "petrifies", "firststrike", "poison", "damage_type"};
1945  /**
1946  * Check if an ability is active. Includes checks to prevent excessive recursion.
1947  * @param ability The type (tag name) of the ability
1948  * @param cfg an ability WML structure
1949  * @param loc The location on which to resolve the ability
1950  * @returns true if it is active
1951  */
1952  bool ability_active(const std::string& ability, const config& cfg, const map_location& loc) const;
1953  /**
1954  * Check if an ability is active. The caller is responsible for preventing excessive recursion, so must hold a recursion_guard.
1955  * @param ability The type (tag name) of the ability
1956  * @param cfg an ability WML structure
1957  * @param loc The location on which to resolve the ability
1958  * @returns true if it is active
1959  */
1960  bool ability_active_impl(const std::string& ability, const config& cfg, const map_location& loc) const;
1961 
1962  /**
1963  * Check if an ability affects distant units.
1964  * @param ability The type (tag name) of the ability
1965  * @param cfg an ability WML structure
1966  * @param loc The location on which to resolve the ability
1967  * @param from The "other unit" for filter matching
1968  * @param dist distance between unit distant and @a this.
1969  * @param dir direction to research a unit distant to @a this.
1970  */
1971  bool ability_affects_adjacent(const std::string& ability, const config& cfg, std::size_t dist, int dir, const map_location& loc, const unit& from) const;
1972  /**
1973  * Check if an ability affects the owning unit.
1974  * @param ability The type (tag name) of the ability
1975  * @param cfg an ability WML structure
1976  * @param loc The location on which to resolve the ability
1977  */
1978  bool ability_affects_self(const std::string& ability, const config& cfg, const map_location& loc) const;
1979 
1980  /**
1981  * filters the weapons that condition the use of abilities for combat ([resistance],[leadership] or abilities used like specials
1982  * (deprecated in two last cases)
1983  */
1984  bool ability_affects_weapon(const config& cfg, const const_attack_ptr& weapon, bool is_opp) const;
1985 
1986 public:
1987  /** Generates a random race-appropriate name if one has not already been provided. */
1988  void generate_name();
1989 
1990  // Only see_all = true use caching
1991  bool invisible(const map_location& loc, bool see_all = true) const;
1992 
1993  bool is_visible_to_team(const team& team, bool const see_all = true) const;
1994  /** Return true if the unit would be visible to team if its location were loc. */
1995  bool is_visible_to_team(const map_location& loc, const team& team, bool const see_all = true) const;
1996 
1997  /**
1998  * Serializes the current unit metadata values.
1999  *
2000  * @param cfg The config to write to.
2001  * @param write_all set this to false to not write unchanged attributes.
2002  */
2003  void write(config& cfg, bool write_all = true) const;
2004 
2005  /**
2006  * Mark this unit as clone so it can be inserted to unit_map.
2007  *
2008  * @returns self (for convenience)
2009  */
2010  unit& mark_clone(bool is_temporary);
2011 
2012 
2013  void set_appearance_changed(bool value) { appearance_changed_ = value; }
2014  bool appearance_changed() const { return appearance_changed_; }
2015 
2016 protected:
2017 
2018 private:
2020 
2021  std::vector<std::string> advances_to_;
2022 
2023  /** Never nullptr. Adjusted for gender and variation. */
2025 
2026  /** The displayed name of this unit type. */
2028 
2029  /** Never nullptr, but may point to the null race. */
2031 
2032  std::string id_;
2035 
2036  std::string undead_variation_;
2037  std::string variation_;
2038 
2043 
2044  int level_;
2045 
2048  std::vector<std::string> recruit_list_;
2050 
2051  std::string flag_rgb_;
2052  std::string image_mods_;
2053 
2057 
2058  int side_;
2059 
2061 
2064  int vision_;
2066 
2068 
2071  bool resting_;
2072 
2075 
2076  std::set<std::string> states_;
2077 
2078  static const std::size_t num_bool_states = state_t::NUMBER_OF_STATES;
2079 
2080  std::bitset<num_bool_states> known_boolean_states_;
2081  static std::map<std::string, state_t> known_boolean_state_names_;
2082 
2086 
2088 
2089  std::vector<std::string> overlays_;
2090 
2091  std::string role_;
2093  /**
2094  * While processing a recursive match, all the filters that are currently being checked, oldest first.
2095  * Each will have an instance of recursion_guard that is currently allocated permission to recurse, and
2096  * which will pop the config off this stack when the recursion_guard is finalized.
2097  */
2098  mutable std::vector<const config*> open_queries_;
2099 
2100 protected:
2101  // TODO: I think we actually consider this to be part of the gamestate, so it might be better if it's not mutable,
2102  // but it's not easy to separate this guy from the animation code right now.
2104 
2105 private:
2106  std::vector<t_string> trait_names_;
2107  std::vector<t_string> trait_descriptions_;
2108  std::vector<std::string> trait_nonhidden_ids_;
2109 
2112 
2114 
2115  // Unit list/recall list favorite unit marking
2117 
2118 public:
2119  bool favorite() const { return is_favorite_; }
2120 
2122 
2123 private:
2124 
2126 
2127  // Animations:
2129 
2130  std::unique_ptr<unit_animation_component> anim_comp_;
2131 
2132  mutable bool hidden_;
2133 
2136 
2137  std::vector<config> advancements_;
2138 
2140  std::vector<t_string> special_notes_;
2141 
2142  utils::optional<std::string> usage_;
2143  utils::optional<std::string> halo_;
2144  utils::optional<std::string> ellipse_;
2145 
2148 
2150 
2151  std::string profile_;
2152  std::string small_profile_;
2153 
2154  //Used to check whether the moving units during a move needs to be updated
2155  mutable bool appearance_changed_ = true;
2156  std::bitset<UA_COUNT> changed_attributes_;
2157 
2160 
2161  /**
2162  * Hold the visibility status cache for a unit, when not uncovered.
2163  * This is mutable since it is a cache.
2164  */
2165  mutable std::map<map_location, bool> invisibility_cache_;
2166 
2167  /**
2168  * Clears the cache.
2169  *
2170  * Since we don't change the state of the object we're marked const (also
2171  * required since the objects in the cache need to be marked const).
2172  */
2174  {
2175  invisibility_cache_.clear();
2176  }
2177 
2178  /**
2179  * Used for easing checking if unit own a ability of specified type with [affect_adjacent] sub tag.
2180  *
2181  */
2182  std::map<std::string, utils::optional<std::size_t>> affect_distant_;
2183  /**
2184  * Used for easing checking if unit own a ability with [affect_adjacent] sub tag.
2185  *
2186  */
2187  utils::optional<std::size_t> has_ability_distant_;
2188  /**
2189  * used if ability own halo_image or overlay_image attributes in same time what [affect_adjacent].
2190  */
2191  utils::optional<std::size_t> has_ability_distant_image_;
2192  void set_has_ability_distant();
2193 };
2194 
2195 /**
2196  * Object which temporarily resets a unit's movement.
2197  *
2198  * @warning A unit whose movement is reset may not be deleted while held in a
2199  * @ref unit_movement_resetter object, so it's best to use thus only in a small scope.
2200  */
2202 {
2205 
2206  unit_movement_resetter(const unit& u, bool operate = true);
2208 
2209 private:
2211  int moves_;
2212 };
2213 
2215 {
2216 /**
2217  * Optional parameter for get_checksum to use the algorithm of an older version of Wesnoth,
2218  * thus preventing spurious OOS warnings while watching old replays.
2219  */
2221  current,
2222  version_1_16_or_older /**< Included some of the flavortext from weapon specials. */
2223 };
2224 
2225 } // namespace backwards_compatibility
2226 
2227 /**
2228  * Gets a checksum for a unit.
2229  *
2230  * In MP games the descriptions are locally generated and might differ, so it
2231  * should be possible to discard them. Not sure whether replays suffer the
2232  * same problem.
2233  *
2234  * @param u this unit
2235  * @param version allows the checksum expected in older replays to be used
2236  *
2237  * @returns the checksum for a unit
2238  */
2239 std::string get_checksum(const unit& u,
map_location loc
Definition: move.cpp:172
double t
Definition: astarsearch.cpp:63
boost::iterator_range< boost::indirect_iterator< attack_list::iterator > > attack_itors
std::vector< attack_ptr > attack_list
boost::iterator_range< boost::indirect_iterator< attack_list::const_iterator > > const_attack_itors
attack_itors make_attack_itors(attack_list &atks)
const std::string & type() const
Definition: attack_type.hpp:44
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
The basic "size" of the unit - flying, small land, large land, etc.
Definition: movetype.hpp:44
int jamming_cost(const t_translation::terrain_code &terrain, bool slowed=false) const
Returns the cost to "jam" through the indicated terrain.
Definition: movetype.hpp:284
int vision_cost(const t_translation::terrain_code &terrain, bool slowed=false) const
Returns the cost to see through the indicated terrain.
Definition: movetype.hpp:281
int movement_cost(const t_translation::terrain_code &terrain, bool slowed=false) const
Returns the cost to move through the indicated terrain.
Definition: movetype.hpp:278
utils::string_map_res damage_table() const
Returns a map from damage types to resistances.
Definition: movetype.hpp:295
bool is_flying() const
Returns whether or not *this is flagged as a flying movement type.
Definition: movetype.hpp:273
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
Helper similar to std::unique_lock for detecting when calculations such as abilities have entered inf...
Definition: unit.hpp:1913
Visitor helper class to parse the upkeep value from a config.
Definition: unit.hpp:1247
Visitor helper class to fetch the appropriate upkeep value.
Definition: unit.hpp:1197
void append(const unit_ability_list &other)
Appends the abilities from other to this, ignores other.loc()
Definition: unit.hpp:105
iterator erase(const iterator &erase_it)
Definition: unit.hpp:96
unit_ability & back()
Definition: unit.hpp:92
iterator erase(const iterator &first, const iterator &last)
Definition: unit.hpp:97
std::pair< int, map_location > lowest(const std::string &key, int def=0) const
Definition: unit.hpp:71
const_iterator begin() const
Definition: unit.hpp:84
unit_ability & front()
Definition: unit.hpp:90
std::vector< unit_ability > cfgs_
Definition: unit.hpp:124
const unit_ability & back() const
Definition: unit.hpp:93
const unit_ability & front() const
Definition: unit.hpp:91
std::vector< unit_ability >::const_iterator const_iterator
Definition: unit.hpp:81
iterator begin()
Definition: unit.hpp:83
iterator end()
Definition: unit.hpp:85
const map_location & loc() const
Definition: unit.hpp:102
std::vector< unit_ability >::iterator iterator
Definition: unit.hpp:80
void emplace_back(T &&... args)
Definition: unit.hpp:100
std::pair< int, map_location > highest(const std::string &key, int def=0) const
Definition: unit.hpp:67
const_iterator end() const
Definition: unit.hpp:86
std::size_t size()
Definition: unit.hpp:94
bool empty() const
Definition: unit.hpp:89
unit_ability_list(const map_location &loc=map_location())
Definition: unit.hpp:64
map_location loc_
Definition: unit.hpp:125
void append_if(const unit_ability_list &other, const Predicate &predicate)
Appends any abilities from other for which the given condition returns true to this,...
Definition: unit.hpp:117
std::pair< int, map_location > get_extremum(const std::string &key, int def, const TComp &comp) const
Definition: abilities.cpp:755
@ NUM_GENDERS
Definition: race.hpp:28
A single unit type that the player may recruit.
Definition: types.hpp:43
This class represents a single unit of a specific type.
Definition: unit.hpp:132
unit_ptr clone() const
Definition: unit.hpp:220
unit & operator=(const unit &)=delete
UNIT_ATTRIBUTE
Definition: unit.hpp:160
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:778
void set_attr_changed(UNIT_ATTRIBUTE attr)
Definition: unit.hpp:184
virtual ~unit()
Definition: unit.cpp:812
bool get_attr_changed(UNIT_ATTRIBUTE attr) const
Definition: unit.hpp:191
void clear_changed_attributes()
Definition: unit.cpp:2856
void init(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Definition: unit.cpp:440
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:200
static const std::string & leader_crown()
The path to the leader crown overlay.
Definition: unit.cpp:1177
bool get_attacks_changed() const
Definition: unit.cpp:1542
unit()=delete
static unit_ptr create(const unit_type &t, int side, bool real_unit, unit_race::GENDER gender=unit_race::NUM_GENDERS, const std::string &variation="")
Initializes a unit from a unit type.
Definition: unit.hpp:212
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
const config * cfg
int hit_points_
Definition: unit.hpp:2039
unit_ability_list get_abilities(const std::string &tag_name) const
Gets the unit's active abilities of a particular type.
Definition: unit.hpp:1826
int movement_
Definition: unit.hpp:2062
void generate_name()
Generates a random race-appropriate name if one has not already been provided.
Definition: unit.cpp:830
std::vector< t_string > trait_names_
Definition: unit.hpp:2106
int unit_value_
Definition: unit.hpp:2110
int attacks_left_
Definition: unit.hpp:2073
bool ability_affects_adjacent(const std::string &ability, const config &cfg, std::size_t dist, int dir, const map_location &loc, const unit &from) const
Check if an ability affects distant units.
Definition: abilities.cpp:559
bool generate_name_
Definition: unit.hpp:2147
void clear_visibility_cache() const
Clears the cache.
Definition: unit.hpp:2173
movetype movement_type_
Definition: unit.hpp:2067
bool get_adj_ability_bool(const config &cfg, const std::string &ability, std::size_t dist, int dir, const map_location &loc, const unit &from, const map_location &from_loc) const
Checks whether this unit is affected by a given ability, and that that ability is active.
Definition: abilities.cpp:1709
config variables_
Definition: unit.hpp:2083
bool unrenamable_
Definition: unit.hpp:2054
int experience_
Definition: unit.hpp:2041
int vision_
Definition: unit.hpp:2064
void remove_ability_by_attribute(const config &filter)
Removes a unit's abilities with a specific ID or other attribute.
Definition: unit.cpp:1529
std::string undead_variation_
Definition: unit.hpp:2036
bool ability_active(const std::string &ability, const config &cfg, const map_location &loc) const
Check if an ability is active.
Definition: abilities.cpp:459
t_string type_name_
The displayed name of this unit type.
Definition: unit.hpp:2027
map_location interrupted_move_
Definition: unit.hpp:2111
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:596
unit_movement_resetter(const unit_movement_resetter &)=delete
bool random_traits_
Definition: unit.hpp:2146
void write(config &cfg, bool write_all=true) const
Serializes the current unit metadata values.
Definition: unit.cpp:1553
std::bitset< UA_COUNT > changed_attributes_
Definition: unit.hpp:2156
std::vector< const config * > open_queries_
While processing a recursive match, all the filters that are currently being checked,...
Definition: unit.hpp:2098
std::string small_profile_
Definition: unit.hpp:2152
void write_upkeep(config::attribute_value &upkeep) const
Definition: unit.cpp:2851
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:211
void set_favorite(bool favorite)
Definition: unit.hpp:2121
std::string id_
Definition: unit.hpp:2032
std::vector< t_string > special_notes_
Definition: unit.hpp:2140
void set_has_ability_distant()
Definition: unit.cpp:416
bool canrecruit_
Definition: unit.hpp:2047
std::string image_mods_
Definition: unit.hpp:2052
bool get_adj_ability_bool_weapon(const config &special, const std::string &tag_name, std::size_t dist, int dir, const map_location &loc, const unit &from, const map_location &from_loc, const const_attack_ptr &weapon, const const_attack_ptr &opp_weapon) const
Checks whether this unit is affected by a given ability of leadership type.
Definition: abilities.cpp:1724
std::string flag_rgb_
Definition: unit.hpp:2051
static std::map< std::string, state_t > known_boolean_state_names_
Definition: unit.hpp:2081
@ UA_IS_HEALTHY
Definition: unit.hpp:165
@ UA_SMALL_PROFILE
Definition: unit.hpp:178
@ UA_MAX_MP
Definition: unit.hpp:162
@ UA_ATTACKS
Definition: unit.hpp:175
@ UA_ZOC
Definition: unit.hpp:169
@ UA_MOVEMENT_TYPE
Definition: unit.hpp:168
@ UA_PROFILE
Definition: unit.hpp:177
@ UA_LEVEL
Definition: unit.hpp:167
@ UA_MAX_XP
Definition: unit.hpp:164
@ UA_IS_FEARLESS
Definition: unit.hpp:166
@ UA_ADVANCE_TO
Definition: unit.hpp:170
@ UA_MAX_AP
Definition: unit.hpp:163
@ UA_COUNT
Definition: unit.hpp:181
@ UA_ADVANCEMENTS
Definition: unit.hpp:171
@ UA_MAX_HP
Definition: unit.hpp:161
@ UA_ABILITIES
Definition: unit.hpp:179
@ UA_UNDEAD_VARIATION
Definition: unit.hpp:173
@ UA_UPKEEP
Definition: unit.hpp:180
@ UA_NOTES
Definition: unit.hpp:176
@ UA_ALIGNMENT
Definition: unit.hpp:172
void set_appearance_changed(bool value)
Definition: unit.hpp:2013
std::vector< std::tuple< std::string, t_string, t_string, t_string > > ability_tooltips() const
Gets the names and descriptions of this unit's abilities.
Definition: abilities.cpp:358
config events_
Definition: unit.hpp:2084
utils::optional< std::size_t > has_ability_distant_
Used for easing checking if unit own a ability with [affect_adjacent] sub tag.
Definition: unit.hpp:2187
bool hidden_
Definition: unit.hpp:2132
bool is_healthy_
Definition: unit.hpp:2113
bool is_fearless_
Definition: unit.hpp:2113
config abilities_
Definition: unit.hpp:2135
utils::optional< std::string > ellipse_
Definition: unit.hpp:2144
unit_ability_list get_abilities_weapons(const std::string &tag_name, const map_location &loc, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr) const
Definition: abilities.cpp:293
const unit_type * type_
Never nullptr.
Definition: unit.hpp:2024
std::bitset< num_bool_states > known_boolean_states_
Definition: unit.hpp:2080
utils::optional< std::string > halo_
Definition: unit.hpp:2143
bool is_favorite_
Definition: unit.hpp:2116
map_location::direction facing_
Definition: unit.hpp:2103
int side_
Definition: unit.hpp:2058
std::shared_ptr< const unit > parent
Definition: unit.hpp:1939
const unit_race * race_
Never nullptr, but may point to the null race.
Definition: unit.hpp:2030
bool appearance_changed_
Definition: unit.hpp:2155
int operator()(const upkeep_loyal &) const
Loyal units cost no upkeep.
Definition: unit.hpp:1208
bool appearance_changed() const
Definition: unit.hpp:2014
unit_alignments::type alignment_
Definition: unit.hpp:2049
bool get_ability_bool(const std::string &tag_name) const
Checks whether this unit currently possesses or is affected by a given ability.
Definition: unit.hpp:1767
bool dismissable_
Definition: unit.hpp:2055
bool invisible(const map_location &loc, bool see_all=true) const
Definition: unit.cpp:2611
bool is_visible_to_team(const team &team, bool const see_all=true) const
Definition: unit.cpp:2654
std::enable_if_t<!std::is_same_v< int, T >, std::string > operator()(T &) const
Definition: unit.hpp:1230
n_unit::unit_id underlying_id_
Definition: unit.hpp:2034
std::string variation_
Definition: unit.hpp:2037
unit & mark_clone(bool is_temporary)
Mark this unit as clone so it can be inserted to unit_map.
Definition: unit.cpp:2712
bool has_ability_type(const std::string &ability) const
Check if the unit has an ability of a specific type.
Definition: abilities.cpp:623
std::enable_if_t< std::is_convertible_v< B, bool > &&!std::is_arithmetic_v< B >, upkeep_t > operator()(B b) const
Definition: unit.hpp:1260
static const std::size_t num_bool_states
Definition: unit.hpp:2078
config filter_recall_
Definition: unit.hpp:2085
std::map< std::string, utils::optional< std::size_t > > affect_distant_
Used for easing checking if unit own a ability of specified type with [affect_adjacent] sub tag.
Definition: unit.hpp:2182
int max_experience_
Definition: unit.hpp:2042
unit_ability_list get_abilities(const std::string &tag_name, const map_location &loc) const
Gets the unit's active abilities of a particular type if it were on a specified location.
Definition: abilities.cpp:251
unit_race::GENDER gender_
Definition: unit.hpp:2060
int operator()(int v) const
Definition: unit.hpp:1213
bool get_self_ability_bool_weapon(const config &special, const std::string &tag_name, const map_location &loc, const const_attack_ptr &weapon=nullptr, const const_attack_ptr &opp_weapon=nullptr) const
Checks whether this unit currently possesses a given ability of leadership type.
Definition: abilities.cpp:1719
t_string description_
Definition: unit.hpp:2139
int level_
Definition: unit.hpp:2044
std::string role_
Definition: unit.hpp:2091
std::map< map_location, bool > invisibility_cache_
Hold the visibility status cache for a unit, when not uncovered.
Definition: unit.hpp:2165
recursion_guard()
Construct an empty instance, only useful for extending the lifetime of a recursion_guard returned fro...
upkeep_t operator()(const std::string &s) const
Definition: unit.hpp:1270
bool end_turn_
Definition: unit.hpp:2070
std::vector< std::string > advances_to_
Definition: unit.hpp:2021
std::unique_ptr< unit_animation_component > anim_comp_
Definition: unit.hpp:2130
utils::optional< std::size_t > has_ability_distant_image_
used if ability own halo_image or overlay_image attributes in same time what [affect_adjacent].
Definition: unit.hpp:2191
std::enable_if_t< std::is_arithmetic_v< N >, upkeep_t > operator()(N n) const
Definition: unit.hpp:1251
bool get_self_ability_bool(const config &cfg, const std::string &ability, const map_location &loc) const
Checks whether this unit currently possesses a given ability, and that that ability is active.
Definition: abilities.cpp:1699
recursion_guard & operator=(recursion_guard &&) noexcept
Definition: abilities.cpp:443
int recall_cost_
Definition: unit.hpp:2046
static std::string type()
Definition: unit.hpp:1187
const std::set< std::string > checking_tags_
Definition: unit.hpp:1944
attack_list attacks_
Definition: unit.hpp:2092
std::vector< std::string > get_ability_list() const
Get a list of all abilities by ID.
Definition: abilities.cpp:302
bool ability_active_impl(const std::string &ability, const config &cfg, const map_location &loc) const
Check if an ability is active.
Definition: abilities.cpp:544
unit_ability_list get_abilities_weapons(const std::string &tag_name, const_attack_ptr weapon=nullptr, const_attack_ptr opp_weapon=nullptr) const
Definition: unit.hpp:1833
void remove_ability_by_id(const std::string &ability)
Removes a unit's abilities with a specific ID.
Definition: unit.cpp:1516
utils::optional< std::string > usage_
Definition: unit.hpp:2142
map_location loc_
Definition: unit.hpp:2019
static std::string type()
Definition: unit.hpp:1182
std::vector< std::string > overlays_
Definition: unit.hpp:2089
config modifications_
Definition: unit.hpp:2134
recursion_guard update_variables_recursion(const config &ability) const
Definition: abilities.cpp:418
bool has_ability_by_id(const std::string &ability) const
Check if the unit has an ability of a specific ID.
Definition: unit.cpp:1505
bool hold_position_
Definition: unit.hpp:2069
const config & abilities() const
Definition: unit.hpp:1838
bool favorite() const
Definition: unit.hpp:2119
upkeep_value_visitor(const unit &unit)
Definition: unit.hpp:1199
std::string operator()(int v) const
Definition: unit.hpp:1236
void parse_upkeep(const config::attribute_value &upkeep)
Definition: unit.cpp:2837
std::vector< std::string > recruit_list_
Definition: unit.hpp:2048
const std::set< std::string > & checking_tags() const
Definition: unit.hpp:1840
recursion_guard(const recursion_guard &other)=delete
std::vector< config > advancements_
Definition: unit.hpp:2137
utils::string_map modification_descriptions_
Definition: unit.hpp:2125
upkeep_t operator()(utils::monostate) const
Definition: unit.hpp:1265
unit_checksum_version
Optional parameter for get_checksum to use the algorithm of an older version of Wesnoth,...
Definition: unit.hpp:2220
std::vector< std::string > trait_nonhidden_ids_
Definition: unit.hpp:2108
upkeep_t upkeep_
Definition: unit.hpp:2149
int operator()(const upkeep_full &) const
Full upkeep equals the unit's level.
Definition: unit.hpp:1202
std::set< std::string > states_
Definition: unit.hpp:2076
std::string profile_
Definition: unit.hpp:2151
t_string dismiss_message_
Definition: unit.hpp:2056
int jamming_
Definition: unit.hpp:2065
map_location goto_
Definition: unit.hpp:2111
t_string name_
Definition: unit.hpp:2033
int max_attacks_
Definition: unit.hpp:2074
bool ability_matches_filter(const config &cfg, const std::string &tag_name, const config &filter) const
Verify what abilities attributes match with filter.
Definition: abilities.cpp:2137
bool ability_affects_weapon(const config &cfg, const const_attack_ptr &weapon, bool is_opp) const
filters the weapons that condition the use of abilities for combat ([resistance],[leadership] or abil...
Definition: abilities.cpp:604
int max_hit_points_
Definition: unit.hpp:2040
int max_movement_
Definition: unit.hpp:2063
bool resting_
Definition: unit.hpp:2071
std::vector< t_string > trait_descriptions_
Definition: unit.hpp:2107
bool emit_zoc_
Definition: unit.hpp:2087
unit_movement_resetter & operator=(const unit_movement_resetter &)=delete
@ version_1_16_or_older
Included some of the flavortext from weapon specials.
void set_big_profile(const std::string &value)
Definition: unit.cpp:1963
int max_hitpoints() const
The max number of hitpoints this unit can have.
Definition: unit.hpp:520
void heal(int amount)
Heal the unit.
Definition: unit.cpp:1390
bool user_end_turn() const
Check whether the user ended their turn.
Definition: unit.hpp:800
void set_role(const std::string &role)
Sets a unit's role.
Definition: unit.hpp:690
unit_alignments::type alignment() const
The alignment of this unit.
Definition: unit.hpp:490
void toggle_user_end_turn()
Toggle whether the user ended their turn.
Definition: unit.hpp:788
bool incapacitated() const
Check if the unit has been petrified.
Definition: unit.hpp:919
void set_state(const std::string &state, bool value)
Set whether the unit is affected by a status effect.
Definition: unit.cpp:1482
int level() const
The current level of this unit.
Definition: unit.hpp:574
std::string usage() const
Gets this unit's usage.
Definition: unit.hpp:701
const std::string & get_role() const
Gets this unit's role.
Definition: unit.hpp:684
const t_string & type_name() const
Gets the translatable name of this unit's type.
Definition: unit.hpp:368
void new_turn()
Refresh unit for the beginning of a turn.
Definition: unit.cpp:1351
const std::vector< std::string > & recruits() const
The type IDs of the other units this unit may recruit, if possible.
Definition: unit.hpp:639
void set_max_experience(int value)
Definition: unit.hpp:549
void set_max_hitpoints(int value)
Definition: unit.hpp:525
void set_hitpoints(int hp)
Sets the current hitpoint amount.
Definition: unit.hpp:532
bool unrenamable() const
Whether this unit can be renamed.
Definition: unit.hpp:435
const config & recall_filter() const
Gets the filter constraints upon which units this unit may recall, if able.
Definition: unit.hpp:667
int recall_cost() const
How much gold it costs to recall this unit, or -1 if the side's default recall cost is used.
Definition: unit.hpp:655
std::string big_profile() const
An optional profile image displays when this unit is 'speaking' via [message].
Definition: unit.cpp:1155
static state_t get_known_boolean_state_id(const std::string &state)
Convert a string status effect ID to a built-in status effect ID.
Definition: unit.cpp:1451
void set_level(int level)
Sets the current level of this unit.
Definition: unit.hpp:580
void rename(const std::string &name)
Attempts to rename this unit's translatable display name, taking the 'unrenamable' flag into account.
Definition: unit.hpp:423
void set_hidden(bool state) const
Sets whether the unit is hidden on the map.
Definition: unit.cpp:2808
void set_recall_filter(const config &filter)
Sets the filter constraints upon which units this unit may recall, if able.
Definition: unit.hpp:673
const std::string & variation() const
The ID of the variation of this unit's type.
Definition: unit.hpp:587
int hitpoints() const
The current number of hitpoints this unit has.
Definition: unit.hpp:514
int cost() const
How much gold is required to recruit this unit.
Definition: unit.hpp:648
bool hold_position() const
Whether the unit has been instructed to hold its position.
Definition: unit.hpp:759
static std::string get_known_boolean_state_name(state_t state)
Convert a built-in status effect ID to a string status effect ID.
Definition: unit.cpp:1461
bool slowed() const
Check if the unit has been slowed.
Definition: unit.hpp:928
bool get_state(const std::string &state) const
Check if the unit is affected by a status effect.
Definition: unit.cpp:1423
unsigned int experience_overflow() const
The number of experience points over max this unit has, or 0 if current XP < max XP.
Definition: unit.hpp:562
std::string small_profile() const
An optional profile image to display in Help.
Definition: unit.cpp:1164
void heal_fully()
Fully heal the unit, restoring it to max hitpoints.
Definition: unit.hpp:839
void set_undead_variation(const std::string &value)
The ID of the undead variation (ie, dwarf, swimmer) of this unit.
Definition: unit.hpp:593
void toggle_hold_position()
Toggle the unit's hold position status.
Definition: unit.hpp:767
const std::string & type_id() const
The id of this unit's type.
Definition: unit.cpp:1958
void set_alignment(unit_alignments::type alignment)
Sets the alignment of this unit.
Definition: unit.hpp:496
bool get_hidden() const
Gets whether this unit is currently hidden on the map.
Definition: unit.hpp:735
void set_name(const t_string &name)
Sets this unit's translatable display name.
Definition: unit.hpp:412
void set_can_recruit(bool canrecruit)
Sets whether this unit can recruit other units.
Definition: unit.hpp:633
const std::set< std::string > get_states() const
Get the status effects currently affecting the unit.
Definition: unit.cpp:1406
void set_side(unsigned int new_side)
Sets the side this unit belongs to.
Definition: unit.hpp:348
void new_scenario()
Refresh unit for the beginning of a new scenario.
Definition: unit.cpp:1375
void end_turn()
Refresh unit for the end of a turn.
Definition: unit.cpp:1361
bool take_hit(int damage)
Damage the unit.
Definition: unit.hpp:824
const config & variables() const
Const overload of variables.
Definition: unit.hpp:724
const std::string & undead_variation() const
Definition: unit.hpp:598
const unit_race * race() const
Gets this unit's race.
Definition: unit.hpp:508
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:354
int experience() const
The current number of experience points this unit has.
Definition: unit.hpp:538
t_string block_dismiss_message() const
A message of why this unit cannot be dismissed.
Definition: unit.hpp:459
bool can_recruit() const
Whether this unit can recruit other units - ie, are they a leader unit.
Definition: unit.hpp:627
void set_experience(int xp)
Sets the current experience point amount.
Definition: unit.hpp:568
void set_user_end_turn(bool value=true)
Set whether the user ended their turn.
Definition: unit.hpp:779
const std::string & id() const
Gets this unit's id.
Definition: unit.hpp:379
void set_underlying_id(n_unit::id_manager &id_manager)
Sets the internal ID.
Definition: unit.cpp:2695
int side() const
The side this unit belongs to.
Definition: unit.hpp:342
bool dismissable() const
Whether this unit can be dismissed.
Definition: unit.hpp:453
bool poisoned() const
Check if the unit has been poisoned.
Definition: unit.hpp:910
unsigned int experience_to_advance() const
The number of experience points this unit needs to level up, or 0 if current XP > max XP.
Definition: unit.hpp:556
state_t
Built-in status effects known to the engine.
Definition: unit.hpp:867
double xp_bar_scaling() const
The factor by which the XP bar should be scaled.
Definition: unit.cpp:2825
void set_unit_description(const t_string &new_desc)
A detailed description of this unit.
Definition: unit.hpp:471
void set_unrenamable(bool unrenamable)
Sets the 'unrenamable' flag.
Definition: unit.hpp:443
void set_recruits(const std::vector< std::string > &recruits)
Sets the recruit list.
Definition: unit.cpp:1264
std::vector< t_string > unit_special_notes() const
The unit's special notes.
Definition: unit.cpp:2864
void set_id(const std::string &id)
Sets this unit's string ID.
Definition: unit.hpp:385
config & variables()
Gets any user-defined variables this unit 'owns'.
Definition: unit.hpp:718
void set_recall_cost(int recall_cost)
Sets the cost of recalling this unit.
Definition: unit.hpp:661
std::size_t underlying_id() const
This unit's unique internal ID.
Definition: unit.hpp:391
double hp_bar_scaling() const
The factor by which the HP bar should be scaled.
Definition: unit.cpp:2821
void set_usage(const std::string &usage)
Sets this unit's usage.
Definition: unit.hpp:707
int max_experience() const
The max number of experience points this unit can have.
Definition: unit.hpp:544
void set_small_profile(const std::string &value)
Definition: unit.hpp:611
unit_race::GENDER gender() const
The gender of this unit.
Definition: unit.hpp:480
const t_string & name() const
Gets this unit's translatable display name.
Definition: unit.hpp:402
t_string unit_description() const
A detailed description of this unit.
Definition: unit.hpp:465
@ STATE_SLOWED
Definition: unit.hpp:868
@ STATE_UNKNOWN
To set the size of known_boolean_states_.
Definition: unit.hpp:877
@ STATE_NOT_MOVED
The unit is uncovered - it was hiding but has been spotted.
Definition: unit.hpp:872
@ STATE_GUARDIAN
The unit cannot be healed.
Definition: unit.hpp:874
@ STATE_INVULNERABLE
The unit is a guardian - it won't move unless a target is sighted.
Definition: unit.hpp:875
@ STATE_PETRIFIED
The unit is poisoned - it loses health each turn.
Definition: unit.hpp:870
@ NUMBER_OF_STATES
The unit is invulnerable - it cannot be hit by any attack.
Definition: unit.hpp:876
@ STATE_UNHEALABLE
The unit has not moved.
Definition: unit.hpp:873
@ STATE_POISONED
The unit is slowed - it moves slower and does less damage.
Definition: unit.hpp:869
@ STATE_UNCOVERED
The unit is petrified - it cannot move or be attacked.
Definition: unit.hpp:871
std::vector< std::string > advances_to_t
Definition: unit.hpp:237
std::vector< config > get_modification_advances() const
Gets any non-typed advanced options set by modifications.
Definition: unit.cpp:1887
std::vector< std::pair< std::string, std::string > > amla_icons() const
Gets the image and description data for modification advancements.
Definition: unit.cpp:1870
const advances_to_t & advances_to() const
Gets the possible types this unit can advance to on level-up.
Definition: unit.hpp:243
bool can_advance() const
Checks whether this unit has any options to advance to.
Definition: unit.hpp:271
bool advances() const
Checks whether this unit is eligible for level-up.
Definition: unit.hpp:282
void set_advancements(std::vector< config > advancements)
Sets the raw modification advancement option data.
Definition: unit.cpp:1952
void set_advances_to(const std::vector< std::string > &advances_to)
Sets this unit's advancement options.
Definition: unit.cpp:1285
const std::vector< config > & modification_advancements() const
The raw, unparsed data for modification advancements.
Definition: unit.hpp:322
std::map< std::string, std::string > advancement_icons() const
Gets and image path and and associated description for each advancement option.
Definition: unit.cpp:1830
const std::vector< std::string > advances_to_translated() const
Gets the names of the possible types this unit can advance to on level-up.
Definition: unit.cpp:1270
void advance_to(const unit_type &t, bool use_traits=false)
Advances this unit to another type.
Definition: unit.cpp:1007
void remove_attacks_ai()
Set the unit to have no attacks left for this turn.
Definition: unit.cpp:2790
attack_ptr add_attack(attack_itors::iterator position, Args &&... args)
Adds a new attack to the unit.
Definition: unit.hpp:958
bool remove_attack(const attack_ptr &atk)
Remove an attack from the unit.
Definition: unit.cpp:2779
int resistance_against(const attack_type &atk, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr) const
The unit's resistance against a given attack.
Definition: unit.hpp:1066
void set_max_attacks(int value)
Definition: unit.hpp:997
int attacks_left(bool base_value) const
Gets the remaining number of attacks this unit can perform this turn.
Definition: unit.hpp:1020
int damage_from(const attack_type &attack, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr) const
Calculates the damage this unit would take from a certain attack.
Definition: unit.hpp:986
int defense_modifier(const t_translation::terrain_code &terrain) const
The unit's defense on a given terrain.
Definition: unit.cpp:1754
bool resistance_filter_matches(const config &cfg, const std::string &damage_name, int res) const
Definition: unit.cpp:1769
attack_itors attacks()
Gets an iterator over this unit's attacks.
Definition: unit.hpp:941
int resistance_against(const std::string &damage_name, bool attacker, const map_location &loc, const_attack_ptr weapon=nullptr, const const_attack_ptr &opp_weapon=nullptr) const
The unit's resistance against a given damage type.
Definition: unit.cpp:1818
utils::string_map_res get_base_resistances() const
Gets resistances without any abilities applied.
Definition: unit.hpp:1072
int max_attacks() const
The maximum number of attacks this unit may perform per turn, usually 1.
Definition: unit.hpp:992
int resistance_value(unit_ability_list resistance_list, const std::string &damage_name) const
For the provided list of resistance abilities, determine the damage resistance based on which are act...
Definition: unit.cpp:1793
const_attack_itors attacks() const
Const overload of attacks.
Definition: unit.hpp:947
int attacks_left() const
Gets the remaining number of attacks this unit can perform this turn.
Definition: unit.hpp:1008
void set_attacks(int left)
Sets the number of attacks this unit has left this turn.
Definition: unit.hpp:1029
color_t xp_color() const
Color for this unit's XP.
Definition: unit.cpp:1252
unit_animation_component & anim_comp() const
Definition: unit.hpp:1641
const std::string & effect_image_mods() const
Gets any IPF image mods applied by effects.
Definition: unit.hpp:1691
std::vector< std::string > overlays_abilities() const
Get the [overlay] ability overlay images.
Definition: unit.hpp:1710
color_t hp_color() const
Color for this unit's current hitpoints.
Definition: unit.cpp:1208
std::vector< std::string > halo_abilities() const
Get the [halo] abilities halo image(s).
Definition: unit.hpp:1661
std::string TC_image_mods() const
Constructs a recolor (RC) IPF string for this unit's team color.
Definition: unit.cpp:2764
static color_t hp_color_max()
Definition: unit.cpp:1218
const std::string & flag_rgb() const
Get the source color palette to use when recoloring the unit's image.
Definition: unit.cpp:1182
std::string image_ellipse() const
Get the unit's ellipse image.
Definition: unit.hpp:1670
std::string image_mods() const
Gets an IPF string containing all IPF image mods.
Definition: unit.cpp:2769
std::string default_anim_image() const
The default image to use for animation frames with no defined image.
Definition: unit.cpp:2593
std::string image_halo() const
Get the unit's halo image.
Definition: unit.hpp:1653
const std::vector< std::string > & overlays() const
Get the unit's overlay images.
Definition: unit.hpp:1704
std::string absolute_image() const
The name of the file to game_display (used in menus).
Definition: unit.cpp:2588
void set_image_ellipse(const std::string &ellipse)
Set the unit's ellipse image.
Definition: unit.hpp:1676
std::vector< std::string > halo_or_icon_abilities(const std::string &image_type) const
Definition: abilities.cpp:638
void set_image_halo(const std::string &halo)
Set the unit's halo image.
Definition: unit.cpp:2830
std::size_t advancements_count() const
Definition: unit.hpp:1591
std::size_t traits_count() const
Definition: unit.hpp:1581
void add_modification(const std::string &type, const config &modification, bool no_add=false)
Add a new modification to the unit.
Definition: unit.cpp:2471
static const std::set< std::string > builtin_effects
Definition: unit.hpp:1614
std::string describe_builtin_effect(const std::string &type, const config &effect)
Construct a string describing a built-in effect.
Definition: unit.cpp:1996
const config & get_modifications() const
Set the raw modifications.
Definition: unit.hpp:1559
config & get_modifications()
Get the raw modifications.
Definition: unit.hpp:1553
void apply_modifications()
Re-apply all saved modifications.
Definition: unit.cpp:2598
void expire_modifications(const std::string &duration)
Clears those modifications whose duration has expired.
Definition: unit.cpp:1318
std::size_t objects_count() const
Definition: unit.hpp:1586
void apply_builtin_effect(const std::string &type, const config &effect)
Apply a builtin effect to the unit.
Definition: unit.cpp:2061
std::size_t modification_count(const std::string &type, const std::string &id) const
Count modifications of a particular type.
Definition: unit.cpp:1970
bool emits_zoc() const
Tests whether the unit has a zone-of-control, considering incapacitated.
Definition: unit.hpp:1412
bool get_emit_zoc() const
Gets the raw zone-of-control flag, disregarding incapacitated.
Definition: unit.hpp:1418
int jamming() const
Gets the unit's jamming points.
Definition: unit.hpp:1480
bool has_goto() const
Gets whether this unit has a multi-turn destination set.
Definition: unit.hpp:1456
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1431
bool has_moved() const
Checks if this unit has moved.
Definition: unit.hpp:1386
int movement_cost(const t_translation::terrain_code &terrain) const
Get the unit's movement cost on a particular terrain.
Definition: unit.hpp:1514
const movetype & movement_type() const
Get the unit's movement type.
Definition: unit.hpp:1504
void set_movement(int moves, bool unit_action=false)
Set this unit's remaining movement to moves.
Definition: unit.cpp:1292
const map_location & get_interrupted_move() const
Get the target location of the unit's interrupted move.
Definition: unit.hpp:1492
int vision_cost(const t_translation::terrain_code &terrain) const
Get the unit's vision cost on a particular terrain.
Definition: unit.hpp:1524
void set_resting(bool rest)
Sets this unit's resting status.
Definition: unit.hpp:1406
void set_facing(map_location::direction dir) const
The this unit's facing.
Definition: unit.cpp:1719
void set_total_movement(int value)
Definition: unit.hpp:1345
void set_emit_zoc(bool val)
Sets the raw zone-of-control flag.
Definition: unit.hpp:1424
void set_goto(const map_location &new_goto)
Sets this unit's long term destination.
Definition: unit.hpp:1468
int movement_left() const
Gets how far a unit can move, considering the incapacitated flag.
Definition: unit.hpp:1356
int movement_left(bool base_value) const
Gets how far a unit can move.
Definition: unit.hpp:1368
int total_movement() const
The maximum moves this unit has.
Definition: unit.hpp:1340
int jamming_cost(const t_translation::terrain_code &terrain) const
Get the unit's jamming cost on a particular terrain.
Definition: unit.hpp:1534
void set_location(const map_location &loc)
Sets this unit's map location.
Definition: unit.hpp:1441
void remove_movement_ai()
Sets the unit to have no moves left for this turn.
Definition: unit.cpp:2799
bool move_interrupted() const
Check whether the unit's move has been interrupted.
Definition: unit.hpp:1486
int vision() const
Gets the unit's vision points.
Definition: unit.hpp:1474
const map_location & get_goto() const
The map location to which this unit is moving over multiple turns, if any.
Definition: unit.hpp:1462
map_location::direction facing() const
The current direction this unit is facing within its hex.
Definition: unit.hpp:1447
bool resting() const
Checks whether this unit is 'resting'.
Definition: unit.hpp:1400
void set_interrupted_move(const map_location &interrupted_move)
Set the target location of the unit's interrupted move.
Definition: unit.hpp:1498
bool is_flying() const
Check if the unit is a flying unit.
Definition: unit.hpp:1540
std::vector< std::string > get_advancements_list() const
Definition: unit.hpp:1153
std::vector< std::string > get_objects_list() const
Definition: unit.hpp:1148
std::vector< std::string > get_modifications_list(const std::string &mod_type) const
Gets a list of the modification this unit currently has.
Definition: unit.cpp:972
int upkeep() const
Gets the amount of gold this unit costs a side per turn.
Definition: unit.cpp:1728
std::vector< std::string > trait_nonhidden_ids() const
Gets the ids of the traits corresponding to those returned by trait_names() and trait_descriptions().
Definition: unit.hpp:1127
utils::optional< std::size_t > has_ability_distant() const
Gets if this unit own ability with [affect_adjacent] subtags.
Definition: unit.hpp:1321
void add_trait_description(const config &trait, const t_string &description)
Register a trait's name and its description for the UI's use.
Definition: unit.cpp:2574
bool is_healthy() const
Gets whether this unit is healthy - ie, always rest heals.
Definition: unit.hpp:1308
bool is_fearless() const
Gets whether this unit is fearless - ie, unaffected by time of day.
Definition: unit.hpp:1302
utils::optional< std::size_t > affect_distant(const std::string &tag_name) const
Gets if this unit own ability of tag_name type with [affect_adjacent] subtags.
Definition: unit.hpp:1314
utils::variant< upkeep_full, upkeep_loyal, int > upkeep_t
Definition: unit.hpp:1190
const std::vector< t_string > & trait_descriptions() const
Gets the descriptions of the currently registered traits.
Definition: unit.hpp:1116
std::vector< std::string > get_traits_list() const
Gets a list of the traits this unit currently has, including hidden traits.
Definition: unit.hpp:1143
const std::vector< t_string > & trait_names() const
Gets the names of the currently registered traits.
Definition: unit.hpp:1106
void generate_traits(bool must_have_only=false)
Applies mandatory traits (e.g.
Definition: unit.cpp:839
upkeep_t upkeep_raw() const
Gets the raw variant controlling the upkeep value.
Definition: unit.hpp:1285
void set_loyal(bool loyal)
Definition: unit.cpp:1743
bool loyal() const
Gets whether this unit is loyal - ie, it costs no upkeep.
Definition: unit.cpp:1738
void set_upkeep(upkeep_t v)
Sets the upkeep value to a specific value value.
Definition: unit.hpp:1291
utils::optional< std::size_t > has_ability_distant_image() const
Gets if this unit own ability with [affect_adjacent] subtags in same time what halo_image or overlay_...
Definition: unit.hpp:1327
void set(CURSOR_TYPE type)
Use the default parameter to reset cursors.
Definition: cursor.cpp:178
Definition: halo.cpp:39
constexpr auto filter
Definition: ranges.hpp:38
std::map< std::string, t_string, res_compare > string_map_res
std::map< std::string, t_string > string_map
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
std::shared_ptr< attack_type > attack_ptr
Definition: ptr.hpp:33
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:61
Encapsulates the map of the game.
Definition: location.hpp:45
bool valid() const
Definition: location.hpp:110
direction
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:47
std::size_t value
Definition: id.hpp:25
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
Visitor helper struct to fetch the upkeep type flag if applicable, or the the value otherwise.
Definition: unit.hpp:1227
Data typedef for unit_ability_list.
Definition: unit.hpp:37
map_location student_loc
Used by the formula in the ability.
Definition: unit.hpp:51
unit_ability(const config *ability_cfg, map_location student_loc, map_location teacher_loc)
Definition: unit.hpp:38
const config * ability_cfg
The contents of the ability tag, never nullptr.
Definition: unit.hpp:58
map_location teacher_loc
The location of the teacher, that is the unit who owns the ability tags (different from student becau...
Definition: unit.hpp:56
Object which temporarily resets a unit's movement.
Definition: unit.hpp:2202
static map_location::direction n
static map_location::direction s
std::string get_checksum(const unit &u, backwards_compatibility::unit_checksum_version version=backwards_compatibility::unit_checksum_version::current)
Gets a checksum for a unit.
Definition: unit.cpp:2870
MacOS doesn't support std::visit when targing MacOS < 10.14 (currently we target 10....
#define b