CStack.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * CStack.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 "battle/BattleHex.h"
  12. #include "CCreatureHandler.h"
  13. #include "mapObjects/CGHeroInstance.h" // for commander serialization
  14. struct BattleStackAttacked;
  15. struct BattleInfo;
  16. class CStack;
  17. class CHealthInfo;
  18. template <typename Quantity>
  19. class DLL_LINKAGE CStackResource
  20. {
  21. public:
  22. CStackResource(const CStack * Owner):
  23. owner(Owner)
  24. {
  25. reset();
  26. }
  27. virtual void reset()
  28. {
  29. used = 0;
  30. };
  31. protected:
  32. const CStack * owner;
  33. Quantity used;
  34. };
  35. class DLL_LINKAGE CAmmo : public CStackResource<int32_t>
  36. {
  37. public:
  38. CAmmo(const CStack * Owner, CSelector totalSelector);
  39. int32_t available() const;
  40. bool canUse(int32_t amount = 1) const;
  41. virtual void reset() override;
  42. virtual int32_t total() const;
  43. virtual void use(int32_t amount = 1);
  44. template <typename Handler> void serialize(Handler & h, const int version)
  45. {
  46. if(!h.saving)
  47. reset();
  48. h & used;
  49. }
  50. protected:
  51. CBonusProxy totalProxy;
  52. };
  53. class DLL_LINKAGE CShots : public CAmmo
  54. {
  55. public:
  56. CShots(const CStack * Owner);
  57. void use(int32_t amount = 1) override;
  58. };
  59. class DLL_LINKAGE CCasts : public CAmmo
  60. {
  61. public:
  62. CCasts(const CStack * Owner);
  63. };
  64. class DLL_LINKAGE CRetaliations : public CAmmo
  65. {
  66. public:
  67. CRetaliations(const CStack * Owner);
  68. int32_t total() const override;
  69. void reset() override;
  70. private:
  71. mutable int32_t totalCache;
  72. };
  73. class DLL_LINKAGE IUnitHealthInfo
  74. {
  75. public:
  76. virtual int32_t unitMaxHealth() const = 0;
  77. virtual int32_t unitBaseAmount() const = 0;
  78. };
  79. class DLL_LINKAGE CHealth
  80. {
  81. public:
  82. CHealth(const IUnitHealthInfo * Owner);
  83. CHealth(const CHealth & other);
  84. void init();
  85. void reset();
  86. void damage(int32_t & amount);
  87. void heal(int32_t & amount, EHealLevel level, EHealPower power);
  88. int32_t getCount() const;
  89. int32_t getFirstHPleft() const;
  90. int32_t getResurrected() const;
  91. int64_t available() const;
  92. int64_t total() const;
  93. void toInfo(CHealthInfo & info) const;
  94. void fromInfo(const CHealthInfo & info);
  95. void takeResurrected();
  96. template <typename Handler> void serialize(Handler & h, const int version)
  97. {
  98. if(!h.saving)
  99. reset();
  100. h & firstHPleft;
  101. h & fullUnits;
  102. h & resurrected;
  103. }
  104. private:
  105. void addResurrected(int32_t amount);
  106. void setFromTotal(const int64_t totalHealth);
  107. const IUnitHealthInfo * owner;
  108. int32_t firstHPleft;
  109. int32_t fullUnits;
  110. int32_t resurrected;
  111. };
  112. class DLL_LINKAGE CStack : public CBonusSystemNode, public ISpellCaster, public IUnitHealthInfo
  113. {
  114. public:
  115. const CStackInstance * base; //garrison slot from which stack originates (nullptr for war machines, summoned cres, etc)
  116. ui32 ID; //unique ID of stack
  117. ui32 baseAmount;
  118. const CCreature * type;
  119. PlayerColor owner; //owner - player color (255 for neutrals)
  120. SlotID slot; //slot - position in garrison (may be 255 for neutrals/called creatures)
  121. ui8 side;
  122. BattleHex position; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
  123. CRetaliations counterAttacks;
  124. CShots shots;
  125. CCasts casts;
  126. CHealth health;
  127. ///id of alive clone of this stack clone if any
  128. si32 cloneID;
  129. std::set<EBattleStackState::EBattleStackState> state;
  130. CStack(const CStackInstance * base, PlayerColor O, int I, ui8 Side, SlotID S);
  131. CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S = SlotID(255));
  132. CStack();
  133. ~CStack();
  134. int32_t getKilled() const;
  135. int32_t getCount() const;
  136. int32_t getFirstHPleft() const;
  137. const CCreature * getCreature() const;
  138. std::string nodeName() const override;
  139. void init(); //set initial (invalid) values
  140. void localInit(BattleInfo * battleInfo);
  141. std::string getName() const; //plural or singular
  142. bool willMove(int turn = 0) const; //if stack has remaining move this turn
  143. bool ableToRetaliate() const; //if stack can retaliate after attacked
  144. bool moved(int turn = 0) const; //if stack was already moved this turn
  145. bool waited(int turn = 0) const;
  146. bool canCast() const;
  147. bool isCaster() const;
  148. bool canMove(int turn = 0) const; //if stack can move
  149. bool canShoot() const;
  150. bool isShooter() const;
  151. bool canBeHealed() const; //for first aid tent - only harmed stacks that are not war machines
  152. ui32 level() const;
  153. si32 magicResistance() const override; //include aura of resistance
  154. std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
  155. const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
  156. static bool isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  157. bool doubleWide() const;
  158. BattleHex occupiedHex() const; //returns number of occupied hex (not the position) if stack is double wide; otherwise -1
  159. BattleHex occupiedHex(BattleHex assumedPos) const; //returns number of occupied hex (not the position) if stack is double wide and would stand on assumedPos; otherwise -1
  160. std::vector<BattleHex> getHexes() const; //up to two occupied hexes, starting from front
  161. std::vector<BattleHex> getHexes(BattleHex assumedPos) const; //up to two occupied hexes, starting from front
  162. static std::vector<BattleHex> getHexes(BattleHex assumedPos, bool twoHex, ui8 side); //up to two occupied hexes, starting from front
  163. bool coversPos(BattleHex position) const; //checks also if unit is double-wide
  164. std::vector<BattleHex> getSurroundingHexes(BattleHex attackerPos = BattleHex::INVALID) const; // get six or 8 surrounding hexes depending on creature size
  165. BattleHex::EDir destShiftDir() const;
  166. CHealth healthAfterAttacked(int32_t & damage) const;
  167. CHealth healthAfterAttacked(int32_t & damage, const CHealth & customHealth) const;
  168. CHealth healthAfterHealed(int32_t & toHeal, EHealLevel level, EHealPower power) const;
  169. void prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand) const; //requires bsa.damageAmout filled
  170. void prepareAttacked(BattleStackAttacked & bsa, CRandomGenerator & rand, const CHealth & customHealth) const; //requires bsa.damageAmout filled
  171. ///ISpellCaster
  172. ui8 getSpellSchoolLevel(const CSpell * spell, int * outSelectedSchool = nullptr) const override;
  173. ui32 getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const override;
  174. ///default spell school level for effect calculation
  175. int getEffectLevel(const CSpell * spell) const override;
  176. ///default spell-power for damage/heal calculation
  177. int getEffectPower(const CSpell * spell) const override;
  178. ///default spell-power for timed effects duration
  179. int getEnchantPower(const CSpell * spell) const override;
  180. ///damage/heal override(ignores spell configuration, effect level and effect power)
  181. int getEffectValue(const CSpell * spell) const override;
  182. const PlayerColor getOwner() const override;
  183. void getCasterName(MetaString & text) const override;
  184. void getCastDescription(const CSpell * spell, const std::vector<const CStack *> & attacked, MetaString & text) const override;
  185. ///IUnitHealthInfo
  186. int32_t unitMaxHealth() const override;
  187. int32_t unitBaseAmount() const override;
  188. ///MetaStrings
  189. void addText(MetaString & text, ui8 type, int32_t serial, const boost::logic::tribool & plural = boost::logic::indeterminate) const;
  190. void addNameReplacement(MetaString & text, const boost::logic::tribool & plural = boost::logic::indeterminate) const;
  191. std::string formatGeneralMessage(const int32_t baseTextId) const;
  192. ///Non const API for NetPacks
  193. ///stack will be ghost in next battle state update
  194. void makeGhost();
  195. void setHealth(const CHealthInfo & value);
  196. void setHealth(const CHealth & value);
  197. template <typename Handler> void serialize(Handler & h, const int version)
  198. {
  199. assert(isIndependentNode());
  200. h & static_cast<CBonusSystemNode&>(*this);
  201. h & type;
  202. h & ID;
  203. h & baseAmount;
  204. h & owner;
  205. h & slot;
  206. h & side;
  207. h & position;
  208. h & state;
  209. h & shots;
  210. h & casts;
  211. h & counterAttacks;
  212. h & health;
  213. const CArmedInstance * army = (base ? base->armyObj : nullptr);
  214. SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID());
  215. if(h.saving)
  216. {
  217. h & army;
  218. h & extSlot;
  219. }
  220. else
  221. {
  222. h & army;
  223. h & extSlot;
  224. if(extSlot == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  225. {
  226. auto hero = dynamic_cast<const CGHeroInstance *>(army);
  227. assert(hero);
  228. base = hero->commander;
  229. }
  230. else if(slot == SlotID::SUMMONED_SLOT_PLACEHOLDER || slot == SlotID::ARROW_TOWERS_SLOT || slot == SlotID::WAR_MACHINES_SLOT)
  231. {
  232. //no external slot possible, so no base stack
  233. base = nullptr;
  234. }
  235. else if(!army || extSlot == SlotID() || !army->hasStackAtSlot(extSlot))
  236. {
  237. base = nullptr;
  238. logGlobal->warn("%s doesn't have a base stack!", type->nameSing);
  239. }
  240. else
  241. {
  242. base = &army->getStack(extSlot);
  243. }
  244. }
  245. }
  246. bool alive() const;
  247. bool isClone() const;
  248. bool isDead() const;
  249. bool isGhost() const; //determines if stack was removed
  250. bool isValidTarget(bool allowDead = false) const; //non-turret non-ghost stacks (can be attacked or be object of magic effect)
  251. bool isTurret() const;
  252. friend class CShots; //for BattleInfo access
  253. private:
  254. const BattleInfo * battle; //do not serialize
  255. };