Unit.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Unit.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <vcmi/Creature.h>
  12. #include <vcmi/spells/Caster.h>
  13. #include "../bonuses/Bonus.h"
  14. #include "../bonuses/IBonusBearer.h"
  15. #include "IUnitInfo.h"
  16. #include "BattleHexArray.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. enum class EMetaText : uint8_t;
  19. class MetaString;
  20. class JsonNode;
  21. class JsonSerializeFormat;
  22. namespace battle
  23. {
  24. namespace BattlePhases
  25. {
  26. enum Type
  27. {
  28. SIEGE, // turrets/catapult,
  29. NORMAL, // normal (unmoved) creatures, other war machines,
  30. WAIT_MORALE, // waited creatures that had morale,
  31. WAIT, // rest of waited creatures
  32. NUMBER_OF_PHASES // number of phases.
  33. };
  34. }
  35. // Healed HP (also drained life) and resurrected units info
  36. struct HealInfo
  37. {
  38. HealInfo() = default;
  39. HealInfo(int64_t healedHP, int32_t resurrected)
  40. : healedHealthPoints(healedHP), resurrectedCount(resurrected)
  41. { }
  42. int64_t healedHealthPoints = 0;
  43. int32_t resurrectedCount = 0;
  44. HealInfo & operator+=(const HealInfo & other)
  45. {
  46. healedHealthPoints += other.healedHealthPoints;
  47. resurrectedCount += other.resurrectedCount;
  48. return *this;
  49. }
  50. };
  51. class CUnitState;
  52. class DLL_LINKAGE Unit : public IUnitInfo, public spells::Caster, public virtual IBonusBearer, public ACreature
  53. {
  54. static BattleHexArray::ArrayOfBattleHexArrays precomputeUnitHexes(BattleSide side, bool twoHex);
  55. public:
  56. virtual ~Unit();
  57. virtual bool doubleWide() const = 0;
  58. virtual int32_t creatureIndex() const = 0;
  59. virtual CreatureID creatureId() const = 0;
  60. virtual int32_t creatureLevel() const = 0;
  61. virtual int32_t creatureCost() const = 0;
  62. virtual int32_t creatureIconIndex() const = 0;
  63. virtual bool ableToRetaliate() const = 0;
  64. virtual bool alive() const = 0;
  65. virtual bool isGhost() const = 0;
  66. virtual bool isFrozen() const = 0;
  67. bool isDead() const;
  68. bool isTurret() const;
  69. virtual bool isValidTarget(bool allowDead = false) const = 0; //non-turret non-ghost stacks (can be attacked or be object of magic effect)
  70. virtual bool isHypnotized() const = 0;
  71. virtual bool isInvincible() const = 0;
  72. virtual bool isClone() const = 0;
  73. virtual bool hasClone() const = 0;
  74. virtual bool canCast() const = 0;
  75. virtual bool isCaster() const = 0;
  76. virtual bool canShootBlocked() const = 0;
  77. virtual bool canShoot() const = 0;
  78. virtual bool isShooter() const = 0;
  79. /// returns initial size of this unit
  80. virtual int32_t getCount() const = 0;
  81. /// returns remaining health of first unit
  82. virtual int32_t getFirstHPleft() const = 0;
  83. /// returns total amount of killed in this unit
  84. virtual int32_t getKilled() const = 0;
  85. /// returns total health that unit still has
  86. virtual int64_t getAvailableHealth() const = 0;
  87. /// returns total health that unit had initially
  88. virtual int64_t getTotalHealth() const = 0;
  89. virtual int getTotalAttacks(bool ranged) const = 0;
  90. virtual BattleHex getPosition() const = 0;
  91. virtual void setPosition(const BattleHex & hex) = 0;
  92. virtual bool canMove(int turn = 0) const = 0; //if stack can move
  93. virtual bool defended(int turn = 0) const = 0;
  94. virtual bool moved(int turn = 0) const = 0; //if stack was already moved this turn
  95. virtual bool willMove(int turn = 0) const = 0; //if stack has remaining move this turn
  96. virtual bool waited(int turn = 0) const = 0;
  97. virtual std::shared_ptr<Unit> acquire() const = 0;
  98. virtual std::shared_ptr<CUnitState> acquireState() const = 0;
  99. virtual BattlePhases::Type battleQueuePhase(int turn) const = 0;
  100. virtual std::string getDescription() const;
  101. const BattleHexArray & getSurroundingHexes(const BattleHex & assumedPosition = BattleHex::INVALID) const; // get six or 8 surrounding hexes depending on creature size
  102. /// Returns list of hexes from which attacker can attack this unit
  103. BattleHexArray getAttackableHexes(const Unit * attacker) const;
  104. static const BattleHexArray & getSurroundingHexes(const BattleHex & position, bool twoHex, BattleSide side);
  105. bool coversPos(const BattleHex & position) const; //checks also if unit is double-wide
  106. const BattleHexArray & getHexes() const; //up to two occupied hexes, starting from front
  107. const BattleHexArray & getHexes(const BattleHex & assumedPos) const; //up to two occupied hexes, starting from front
  108. static const BattleHexArray & getHexes(const BattleHex & assumedPos, bool twoHex, BattleSide side);
  109. BattleHex occupiedHex() const; //returns number of occupied hex (not the position) if stack is double wide; otherwise -1
  110. BattleHex occupiedHex(const BattleHex & assumedPos) const; //returns number of occupied hex (not the position) if stack is double wide and would stand on assumedPos; otherwise -1
  111. static BattleHex occupiedHex(const BattleHex & assumedPos, bool twoHex, BattleSide side);
  112. ///MetaStrings
  113. void addText(MetaString & text, EMetaText type, int32_t serial, const boost::logic::tribool & plural = boost::logic::indeterminate) const;
  114. void addNameReplacement(MetaString & text, const boost::logic::tribool & plural = boost::logic::indeterminate) const;
  115. std::string formatGeneralMessage(const int32_t baseTextId) const;
  116. int getRawSurrenderCost() const;
  117. //IConstBonusProvider
  118. const IBonusBearer* getBonusBearer() const override;
  119. //NOTE: save could possibly be const, but this requires heavy changes to Json serialization,
  120. //also this method should be called only after modifying object
  121. virtual void save(JsonNode & data) = 0;
  122. virtual void load(const JsonNode & data) = 0;
  123. virtual void damage(int64_t & amount) = 0;
  124. virtual HealInfo heal(int64_t & amount, EHealLevel level, EHealPower power) = 0;
  125. };
  126. class DLL_LINKAGE UnitInfo
  127. {
  128. public:
  129. uint32_t id = 0;
  130. TQuantity count = 0;
  131. CreatureID type;
  132. BattleSide side = BattleSide::NONE;
  133. BattleHex position;
  134. bool summoned = false;
  135. void serializeJson(JsonSerializeFormat & handler);
  136. void save(JsonNode & data);
  137. void load(uint32_t id_, const JsonNode & data);
  138. };
  139. }
  140. VCMI_LIB_NAMESPACE_END