CUnitState.h 7.8 KB

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