CUnitState.h 7.5 KB

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