CStack.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "JsonNode.h"
  12. #include "HeroBonus.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. struct BattleStackAttacked;
  18. class BattleInfo;
  19. //Represents STACK_BATTLE nodes
  20. class DLL_LINKAGE CStack : public CBonusSystemNode, public battle::CUnitState, public battle::IUnitEnvironment
  21. {
  22. public:
  23. const CStackInstance * base; //garrison slot from which stack originates (nullptr for war machines, summoned cres, etc)
  24. ui32 ID; //unique ID of stack
  25. const CCreature * type;
  26. ETerrainType::EETerrainType nativeTerrain; //tmp variable to save native terrain value on battle init
  27. ui32 baseAmount;
  28. PlayerColor owner; //owner - player color (255 for neutrals)
  29. SlotID slot; //slot - position in garrison (may be 255 for neutrals/called creatures)
  30. ui8 side;
  31. BattleHex initialPosition; //position on battlefield; -2 - keep, -3 - lower tower, -4 - upper tower
  32. CStack(const CStackInstance * base, PlayerColor O, int I, ui8 Side, SlotID S);
  33. CStack(const CStackBasicDescriptor * stack, PlayerColor O, int I, ui8 Side, SlotID S = SlotID(255));
  34. CStack();
  35. ~CStack();
  36. const CCreature * getCreature() const; //deprecated
  37. std::string nodeName() const override;
  38. void localInit(BattleInfo * battleInfo);
  39. std::string getName() const; //plural or singular
  40. bool canBeHealed() const; //for first aid tent - only harmed stacks that are not war machines
  41. bool isOnNativeTerrain() const;
  42. bool isOnTerrain(int terrain) const;
  43. ui32 level() const;
  44. si32 magicResistance() const override; //include aura of resistance
  45. std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
  46. const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
  47. static std::vector<BattleHex> meleeAttackHexes(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  48. static bool isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  49. BattleHex::EDir destShiftDir() const;
  50. void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand) const; //requires bsa.damageAmout filled
  51. static void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand, std::shared_ptr<battle::CUnitState> customState); //requires bsa.damageAmout filled
  52. const CCreature * unitType() const override;
  53. int32_t unitBaseAmount() const override;
  54. uint32_t unitId() const override;
  55. ui8 unitSide() const override;
  56. PlayerColor unitOwner() const override;
  57. SlotID unitSlot() const override;
  58. std::string getDescription() const override;
  59. bool unitHasAmmoCart(const battle::Unit * unit) const override;
  60. PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
  61. void spendMana(ServerCallback * server, const int spellCost) const override;
  62. PlayerColor getOwner() const override
  63. {
  64. return this->owner;
  65. }
  66. template <typename Handler> void serialize(Handler & h, const int version)
  67. {
  68. //this assumes that stack objects is newly created
  69. //stackState is not serialized here
  70. assert(isIndependentNode());
  71. h & static_cast<CBonusSystemNode&>(*this);
  72. h & type;
  73. h & ID;
  74. h & baseAmount;
  75. h & owner;
  76. h & slot;
  77. h & side;
  78. h & initialPosition;
  79. const CArmedInstance * army = (base ? base->armyObj : nullptr);
  80. SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID());
  81. if(h.saving)
  82. {
  83. h & army;
  84. h & extSlot;
  85. }
  86. else
  87. {
  88. h & army;
  89. h & extSlot;
  90. if(extSlot == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  91. {
  92. auto hero = dynamic_cast<const CGHeroInstance *>(army);
  93. assert(hero);
  94. base = hero->commander;
  95. }
  96. else if(slot == SlotID::SUMMONED_SLOT_PLACEHOLDER || slot == SlotID::ARROW_TOWERS_SLOT || slot == SlotID::WAR_MACHINES_SLOT)
  97. {
  98. //no external slot possible, so no base stack
  99. base = nullptr;
  100. }
  101. else if(!army || extSlot == SlotID() || !army->hasStackAtSlot(extSlot))
  102. {
  103. base = nullptr;
  104. logGlobal->warn("%s doesn't have a base stack!", type->nameSing);
  105. }
  106. else
  107. {
  108. base = &army->getStack(extSlot);
  109. }
  110. }
  111. }
  112. private:
  113. const BattleInfo * battle; //do not serialize
  114. };