CStack.h 4.7 KB

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