CUnitState.h 7.3 KB

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