CStack.h 4.8 KB

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