CUnitState.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * CUnitState.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 "BattleUnitTurnReason.h"
  12. #include "Unit.h"
  13. #include "../bonuses/BonusCache.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class JsonSerializeFormat;
  16. class UnitChanges;
  17. namespace vstd
  18. {
  19. class RNG;
  20. }
  21. namespace battle
  22. {
  23. class CUnitState;
  24. class DLL_LINKAGE CAmmo
  25. {
  26. public:
  27. explicit CAmmo(const battle::Unit * Owner, CSelector totalSelector);
  28. CAmmo & operator=(const CAmmo & other);
  29. CAmmo & operator=(CAmmo && other) = delete;
  30. int32_t available() const;
  31. bool canUse(int32_t amount = 1) const;
  32. virtual bool isLimited() const;
  33. virtual void reset();
  34. virtual int32_t total() const;
  35. virtual void use(int32_t amount = 1);
  36. virtual void serializeJson(JsonSerializeFormat & handler);
  37. protected:
  38. int32_t used;
  39. const battle::Unit * owner;
  40. BonusValueCache totalProxy;
  41. };
  42. class DLL_LINKAGE CShots : public CAmmo
  43. {
  44. public:
  45. explicit CShots(const battle::Unit * Owner);
  46. bool isLimited() const override;
  47. int32_t total() const override;
  48. void setEnv(const IUnitEnvironment * env_);
  49. private:
  50. const IUnitEnvironment * env;
  51. BonusValueCache shooter;
  52. };
  53. class DLL_LINKAGE CCasts : public CAmmo
  54. {
  55. public:
  56. explicit CCasts(const battle::Unit * Owner);
  57. };
  58. class DLL_LINKAGE CRetaliations : public CAmmo
  59. {
  60. public:
  61. explicit CRetaliations(const battle::Unit * Owner);
  62. bool isLimited() const override;
  63. int32_t total() const override;
  64. void reset() override;
  65. void serializeJson(JsonSerializeFormat & handler) override;
  66. private:
  67. mutable int32_t totalCache;
  68. BonusValueCache noRetaliation;
  69. BonusValueCache unlimited;
  70. };
  71. class DLL_LINKAGE CHealth
  72. {
  73. public:
  74. explicit CHealth(const battle::Unit * Owner);
  75. CHealth(const CHealth & other) = default;
  76. CHealth & operator=(const CHealth & other);
  77. void init();
  78. void reset();
  79. void damage(int64_t & amount);
  80. HealInfo heal(int64_t & amount, EHealLevel level, EHealPower power);
  81. int32_t getCount() const;
  82. int32_t getFirstHPleft() const;
  83. int32_t getResurrected() const;
  84. /// returns total remaining health
  85. int64_t available() const;
  86. /// returns total initial health
  87. int64_t total() const;
  88. void takeResurrected();
  89. void serializeJson(JsonSerializeFormat & handler);
  90. private:
  91. void addResurrected(int32_t amount);
  92. void setFromTotal(const int64_t totalHealth);
  93. const battle::Unit * owner;
  94. int32_t firstHPleft;
  95. int32_t fullUnits;
  96. int32_t resurrected;
  97. };
  98. class DLL_LINKAGE CUnitState : public Unit
  99. {
  100. public:
  101. bool cloned;
  102. bool defending;
  103. bool defendingAnim;
  104. bool drainedMana;
  105. bool fear;
  106. bool hadMorale;
  107. bool castSpellThisTurn;
  108. bool ghost;
  109. bool ghostPending;
  110. bool movedThisRound;
  111. bool summoned;
  112. bool waiting;
  113. bool waitedThisTurn; //"waited()" that stays true for full turn after wait - needed as UI button hackfix
  114. CCasts casts;
  115. CRetaliations counterAttacks;
  116. CHealth health;
  117. CShots shots;
  118. ///id of alive clone of this stack clone if any
  119. si32 cloneID;
  120. ///position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
  121. BattleHex position;
  122. CUnitState();
  123. CUnitState(const CUnitState & other) = delete;
  124. CUnitState(CUnitState && other) = delete;
  125. CUnitState & operator= (const CUnitState & other);
  126. CUnitState & operator= (CUnitState && other) = delete;
  127. bool doubleWide() const override;
  128. int32_t creatureIndex() const override;
  129. CreatureID creatureId() const override;
  130. int32_t creatureLevel() const override;
  131. int32_t creatureCost() const override;
  132. int32_t creatureIconIndex() const override;
  133. int32_t getCasterUnitId() const override;
  134. int32_t getSpellSchoolLevel(const spells::Spell * spell, SpellSchool * outSelectedSchool = nullptr) const override;
  135. int32_t getEffectLevel(const spells::Spell * spell) const override;
  136. int64_t getSpellBonus(const spells::Spell * spell, int64_t base, const Unit * affectedStack) const override;
  137. int64_t getSpecificSpellBonus(const spells::Spell * spell, int64_t base) const override;
  138. int32_t getEffectPower(const spells::Spell * spell) const override;
  139. int32_t getEnchantPower(const spells::Spell * spell) const override;
  140. int64_t getEffectValue(const spells::Spell * spell) const override;
  141. int64_t getEffectRange(const spells::Spell * spell) const override;
  142. PlayerColor getCasterOwner() const override;
  143. const CGHeroInstance * getHeroCaster() const override;
  144. void getCasterName(MetaString & text) const override;
  145. void getCastDescription(const spells::Spell * spell, const battle::Units & attacked, MetaString & text) const override;
  146. int32_t manaLimit() const override;
  147. bool ableToRetaliate() const override;
  148. bool alive() const override;
  149. bool isGhost() const override;
  150. bool isFrozen() const override;
  151. bool isValidTarget(bool allowDead = false) const override;
  152. bool isHypnotized() const override;
  153. bool isInvincible() const override;
  154. bool isClone() const override;
  155. bool hasClone() const override;
  156. bool canCast() const override;
  157. bool isCaster() const override;
  158. bool canShootBlocked() const override;
  159. bool canShoot() const override;
  160. bool isShooter() const override;
  161. int32_t getKilled() const override;
  162. int32_t getCount() const override;
  163. int32_t getFirstHPleft() const override;
  164. int64_t getAvailableHealth() const override;
  165. int64_t getTotalHealth() const override;
  166. uint32_t getMaxHealth() const override;
  167. BattleHex getPosition() const override;
  168. void setPosition(const BattleHex & hex) override;
  169. int32_t getInitiative(int turn = 0) const override;
  170. uint8_t getRangedFullDamageDistance() const;
  171. uint8_t getShootingRangeDistance() const;
  172. ui32 getMovementRange(int turn) const override;
  173. ui32 getMovementRange() const override;
  174. bool canMove(int turn = 0) const override;
  175. bool defended(int turn = 0) const override;
  176. bool moved(int turn = 0) const override;
  177. bool willMove(int turn = 0) const override;
  178. bool waited(int turn = 0) const override;
  179. std::shared_ptr<Unit> acquire() const override;
  180. std::shared_ptr<CUnitState> acquireState() const override;
  181. BattlePhases::Type battleQueuePhase(int turn) const override;
  182. int getTotalAttacks(bool ranged) const override;
  183. int getMinDamage(bool ranged) const override;
  184. int getMaxDamage(bool ranged) const override;
  185. int getAttack(bool ranged) const override;
  186. int getDefense(bool ranged) const override;
  187. void save(JsonNode & data) override;
  188. void load(const JsonNode & data) override;
  189. void damage(int64_t & amount) override;
  190. HealInfo heal(int64_t & amount, EHealLevel level, EHealPower power) override;
  191. void localInit(const IUnitEnvironment * env_);
  192. void serializeJson(JsonSerializeFormat & handler);
  193. FactionID getFactionID() const override;
  194. void afterAttack(bool ranged, bool counter);
  195. void afterNewRound();
  196. void afterGetsTurn(BattleUnitTurnReason reason);
  197. void makeGhost();
  198. void onRemoved();
  199. private:
  200. const IUnitEnvironment * env;
  201. BonusCachePerTurn immobilizedPerTurn;
  202. BonusCachePerTurn stackSpeedPerTurn;
  203. UnitBonusValuesProxy bonusCache;
  204. void reset();
  205. };
  206. class DLL_LINKAGE CUnitStateDetached final : public CUnitState
  207. {
  208. public:
  209. explicit CUnitStateDetached(const IUnitInfo * unit_, const IBonusBearer * bonus_);
  210. CUnitStateDetached & operator= (const CUnitState & other);
  211. TConstBonusListPtr getAllBonuses(const CSelector & selector, const std::string & cachingStr = "") const override;
  212. int32_t getTreeVersion() const override;
  213. uint32_t unitId() const override;
  214. BattleSide unitSide() const override;
  215. const CCreature * unitType() const override;
  216. PlayerColor unitOwner() const override;
  217. SlotID unitSlot() const override;
  218. int32_t unitBaseAmount() const override;
  219. void spendMana(ServerCallback * server, const int spellCost) const override;
  220. private:
  221. const IUnitInfo * unit;
  222. const IBonusBearer * bonus;
  223. };
  224. }
  225. VCMI_LIB_NAMESPACE_END