CStack.h 4.3 KB

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