CStack.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. class DLL_LINKAGE CStack : public CBonusSystemNode, public CStackBasicDescriptor, public ISpellCaster
  16. {
  17. public:
  18. const CStackInstance *base; //garrison slot from which stack originates (nullptr for war machines, summoned cres, etc)
  19. ui32 ID; //unique ID of stack
  20. ui32 baseAmount;
  21. ui32 firstHPleft; //HP of first creature in stack
  22. PlayerColor owner; //owner - player colour (255 for neutrals)
  23. SlotID slot; //slot - position in garrison (may be 255 for neutrals/called creatures)
  24. bool attackerOwned; //if true, this stack is owned by attakcer (this one from left hand side of battle)
  25. BattleHex position; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
  26. ///how many times this stack has been counterattacked this round
  27. ui8 counterAttacksPerformed;
  28. ///cached total count of counterattacks; should be cleared each round;do not serialize
  29. mutable ui8 counterAttacksTotalCache;
  30. si16 shots; //how many shots left
  31. ui8 casts; //how many casts left
  32. TQuantity resurrected; // these units will be taken back after battle is over
  33. ///id of alive clone of this stack clone if any
  34. si32 cloneID;
  35. std::set<EBattleStackState::EBattleStackState> state;
  36. //overrides
  37. const CCreature* getCreature() const {return type;}
  38. CStack(const CStackInstance *base, PlayerColor O, int I, bool AO, SlotID S); //c-tor
  39. CStack(const CStackBasicDescriptor *stack, PlayerColor O, int I, bool AO, SlotID S = SlotID(255)); //c-tor
  40. CStack(); //c-tor
  41. ~CStack();
  42. std::string nodeName() const override;
  43. void init(); //set initial (invalid) values
  44. void postInit(); //used to finish initialization when inheriting creature parameters is working
  45. std::string getName() const; //plural or singular
  46. bool willMove(int turn = 0) const; //if stack has remaining move this turn
  47. bool ableToRetaliate() const; //if stack can retaliate after attacked
  48. ///how many times this stack can counterattack in one round
  49. ui8 counterAttacksTotal() const;
  50. ///how many times this stack can counterattack in one round more
  51. si8 counterAttacksRemaining() const;
  52. bool moved(int turn = 0) const; //if stack was already moved this turn
  53. bool waited(int turn = 0) const;
  54. bool canMove(int turn = 0) const; //if stack can move
  55. bool canBeHealed() const; //for first aid tent - only harmed stacks that are not war machines
  56. ///returns actual heal value based on internal state
  57. ui32 calculateHealedHealthPoints(ui32 toHeal, const bool resurrect) const;
  58. ui32 level() const;
  59. si32 magicResistance() const override; //include aura of resistance
  60. std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
  61. const CGHeroInstance *getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
  62. ui32 totalHealth() const; // total health for all creatures in stack;
  63. static bool isMeleeAttackPossible(const CStack * attacker, const CStack * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  64. bool doubleWide() const;
  65. BattleHex occupiedHex() const; //returns number of occupied hex (not the position) if stack is double wide; otherwise -1
  66. 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
  67. std::vector<BattleHex> getHexes() const; //up to two occupied hexes, starting from front
  68. std::vector<BattleHex> getHexes(BattleHex assumedPos) const; //up to two occupied hexes, starting from front
  69. static std::vector<BattleHex> getHexes(BattleHex assumedPos, bool twoHex, bool AttackerOwned); //up to two occupied hexes, starting from front
  70. bool coversPos(BattleHex position) const; //checks also if unit is double-wide
  71. std::vector<BattleHex> getSurroundingHexes(BattleHex attackerPos = BattleHex::INVALID) const; // get six or 8 surrounding hexes depending on creature size
  72. std::pair<int,int> countKilledByAttack(int damageReceived) const; //returns pair<killed count, new left HP>
  73. void prepareAttacked(BattleStackAttacked &bsa, CRandomGenerator & rand, boost::optional<int> customCount = boost::none) const; //requires bsa.damageAmout filled
  74. ///ISpellCaster
  75. ui8 getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool = nullptr) const override;
  76. ui32 getSpellBonus(const CSpell * spell, ui32 base, const CStack * affectedStack) const override;
  77. ///default spell school level for effect calculation
  78. int getEffectLevel(const CSpell * spell) const override;
  79. ///default spell-power for damage/heal calculation
  80. int getEffectPower(const CSpell * spell) const override;
  81. ///default spell-power for timed effects duration
  82. int getEnchantPower(const CSpell * spell) const override;
  83. ///damage/heal override(ignores spell configuration, effect level and effect power)
  84. int getEffectValue(const CSpell * spell) const override;
  85. const PlayerColor getOwner() const override;
  86. void getCasterName(MetaString & text) const override;
  87. void getCastDescription(const CSpell * spell, const std::vector<const CStack *> & attacked, MetaString & text) const override;
  88. ///stack will be ghost in next battle state update
  89. void makeGhost();
  90. template <typename Handler> void serialize(Handler &h, const int version)
  91. {
  92. assert(isIndependentNode());
  93. h & static_cast<CBonusSystemNode&>(*this);
  94. h & static_cast<CStackBasicDescriptor&>(*this);
  95. h & ID & baseAmount & firstHPleft & owner & slot & attackerOwned & position & state & counterAttacksPerformed
  96. & shots & casts & count & resurrected;
  97. const CArmedInstance *army = (base ? base->armyObj : nullptr);
  98. SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID());
  99. if(h.saving)
  100. {
  101. h & army & extSlot;
  102. }
  103. else
  104. {
  105. h & army & extSlot;
  106. if(extSlot == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  107. {
  108. auto hero = dynamic_cast<const CGHeroInstance *>(army);
  109. assert (hero);
  110. base = hero->commander;
  111. }
  112. else if(slot == SlotID::SUMMONED_SLOT_PLACEHOLDER || slot == SlotID::ARROW_TOWERS_SLOT || slot == SlotID::WAR_MACHINES_SLOT)
  113. {
  114. //no external slot possible, so no base stack
  115. base = nullptr;
  116. }
  117. else if(!army || extSlot == SlotID() || !army->hasStackAtSlot(extSlot))
  118. {
  119. base = nullptr;
  120. logGlobal->warnStream() << type->nameSing << " doesn't have a base stack!";
  121. }
  122. else
  123. {
  124. base = &army->getStack(extSlot);
  125. }
  126. }
  127. }
  128. bool alive() const;
  129. bool isDead() const;
  130. bool isGhost() const; //determines if stack was removed
  131. bool isValidTarget(bool allowDead = false) const; //non-turret non-ghost stacks (can be attacked or be object of magic effect)
  132. bool isTurret() const;
  133. };