CUnitState.h 8.3 KB

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