CStack.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ui32 level() const;
  40. si32 magicResistance() const override; //include aura of resistance
  41. std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
  42. const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
  43. static bool isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
  44. BattleHex::EDir destShiftDir() const;
  45. void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand) const; //requires bsa.damageAmout filled
  46. static void prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand, std::shared_ptr<battle::CUnitState> customState); //requires bsa.damageAmout filled
  47. const CCreature * unitType() const override;
  48. int32_t unitBaseAmount() const override;
  49. uint32_t unitId() const override;
  50. ui8 unitSide() const override;
  51. PlayerColor unitOwner() const override;
  52. SlotID unitSlot() const override;
  53. std::string getDescription() const override;
  54. bool unitHasAmmoCart(const battle::Unit * unit) const override;
  55. PlayerColor unitEffectiveOwner(const battle::Unit * unit) const override;
  56. void spendMana(const spells::PacketSender * server, const int spellCost) const override;
  57. template <typename Handler> void serialize(Handler & h, const int version)
  58. {
  59. //this assumes that stack objects is newly created
  60. //stackState is not serialized here
  61. assert(isIndependentNode());
  62. h & static_cast<CBonusSystemNode&>(*this);
  63. h & type;
  64. h & ID;
  65. h & baseAmount;
  66. h & owner;
  67. h & slot;
  68. h & side;
  69. h & initialPosition;
  70. const CArmedInstance * army = (base ? base->armyObj : nullptr);
  71. SlotID extSlot = (base ? base->armyObj->findStack(base) : SlotID());
  72. if(h.saving)
  73. {
  74. h & army;
  75. h & extSlot;
  76. }
  77. else
  78. {
  79. h & army;
  80. h & extSlot;
  81. if(extSlot == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  82. {
  83. auto hero = dynamic_cast<const CGHeroInstance *>(army);
  84. assert(hero);
  85. base = hero->commander;
  86. }
  87. else if(slot == SlotID::SUMMONED_SLOT_PLACEHOLDER || slot == SlotID::ARROW_TOWERS_SLOT || slot == SlotID::WAR_MACHINES_SLOT)
  88. {
  89. //no external slot possible, so no base stack
  90. base = nullptr;
  91. }
  92. else if(!army || extSlot == SlotID() || !army->hasStackAtSlot(extSlot))
  93. {
  94. base = nullptr;
  95. logGlobal->warn("%s doesn't have a base stack!", type->nameSing);
  96. }
  97. else
  98. {
  99. base = &army->getStack(extSlot);
  100. }
  101. }
  102. }
  103. private:
  104. const BattleInfo * battle; //do not serialize
  105. };