CStack.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "bonuses/Bonus.h"
  12. #include "bonuses/CBonusSystemNode.h"
  13. #include "CCreatureHandler.h" //todo: remove
  14. #include "battle/BattleHex.h"
  15. #include "mapObjects/CGHeroInstance.h" // for commander serialization
  16. #include "battle/CUnitState.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. struct BattleStackAttacked;
  19. class BattleInfo;
  20. //Represents STACK_BATTLE nodes
  21. class DLL_LINKAGE CStack final : public CBonusSystemNode, public battle::CUnitState, public battle::IUnitEnvironment
  22. {
  23. private:
  24. ui32 ID = -1; //unique ID of stack
  25. CreatureID typeID;
  26. TerrainId nativeTerrain; //tmp variable to save native terrain value on battle init
  27. ui32 baseAmount = -1;
  28. PlayerColor owner; //owner - player color (255 for neutrals)
  29. BattleSide side = BattleSide::NONE;
  30. SlotID slot; //slot - position in garrison (may be 255 for neutrals/called creatures)
  31. bool doubleWideCached = false;
  32. void postDeserialize(const CArmedInstance * army, const SlotID & extSlot);
  33. public:
  34. const CStackInstance * base = nullptr; //garrison slot from which stack originates (nullptr for war machines, summoned cres, etc)
  35. BattleHex initialPosition; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
  36. CStack(const CStackInstance * base, const PlayerColor & O, int I, BattleSide Side, const SlotID & S);
  37. CStack(const CStackBasicDescriptor * stack, const PlayerColor & O, int I, BattleSide Side, const SlotID & S = SlotID(255));
  38. CStack();
  39. ~CStack();
  40. std::string nodeName() const override;
  41. void localInit(BattleInfo * battleInfo);
  42. std::string getName() const; //plural or singular
  43. bool canBeHealed() const; //for first aid tent - only harmed stacks that are not war machines
  44. bool isOnNativeTerrain() const;
  45. bool isOnTerrain(TerrainId terrain) const;
  46. ui32 level() const;
  47. si32 magicResistance() const override; //include aura of resistance
  48. std::vector<SpellID> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
  49. const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
  50. static BattleHexArray meleeAttackHexes(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  51. static bool isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  52. BattleHex::EDir destShiftDir() const;
  53. void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand) const; //requires bsa.damageAmount filled
  54. static void prepareAttacked(BattleStackAttacked & bsa,
  55. vstd::RNG & rand,
  56. const std::shared_ptr<battle::CUnitState> & customState); //requires bsa.damageAmount filled
  57. const CCreature * unitType() const override;
  58. int32_t unitBaseAmount() const override;
  59. uint32_t unitId() const override;
  60. BattleSide unitSide() const override;
  61. PlayerColor unitOwner() const override;
  62. SlotID unitSlot() const override;
  63. bool doubleWide() const override { return doubleWideCached;};
  64. std::string getDescription() const override;
  65. bool unitHasAmmoCart(const battle::Unit * unit) const override;
  66. PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
  67. void spendMana(ServerCallback * server, const int spellCost) const override;
  68. const IBonusBearer* getBonusBearer() const override;
  69. PlayerColor getOwner() const override
  70. {
  71. return this->owner;
  72. }
  73. template <typename Handler> void serialize(Handler & h)
  74. {
  75. //this assumes that stack objects is newly created
  76. //stackState is not serialized here
  77. assert(isIndependentNode());
  78. h & static_cast<CBonusSystemNode&>(*this);
  79. h & typeID;
  80. h & ID;
  81. h & baseAmount;
  82. h & owner;
  83. h & slot;
  84. h & side;
  85. h & initialPosition;
  86. const CArmedInstance * army = (base ? base->armyObj : nullptr);
  87. SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID());
  88. if(h.saving)
  89. {
  90. h & army;
  91. h & extSlot;
  92. }
  93. else
  94. {
  95. h & army;
  96. h & extSlot;
  97. postDeserialize(army, extSlot);
  98. }
  99. }
  100. private:
  101. const BattleInfo * battle; //do not serialize
  102. };
  103. VCMI_LIB_NAMESPACE_END