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