CUnitState.h 7.7 KB

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