CUnitState.h 8.2 KB

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