BattleInfo.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * BattleInfo.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 "../int3.h"
  12. #include "../bonuses/Bonus.h"
  13. #include "../bonuses/CBonusSystemNode.h"
  14. #include "CBattleInfoCallback.h"
  15. #include "IBattleState.h"
  16. #include "SiegeInfo.h"
  17. #include "SideInBattle.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. class CStack;
  20. class CStackInstance;
  21. class CStackBasicDescriptor;
  22. class BattleField;
  23. struct BattleLayout;
  24. class DLL_LINKAGE BattleInfo : public CBonusSystemNode, public CBattleInfoCallback, public IBattleState
  25. {
  26. BattleSideArray<SideInBattle> sides; //sides[0] - attacker, sides[1] - defender
  27. std::unique_ptr<BattleLayout> layout;
  28. public:
  29. BattleID battleID = BattleID(0);
  30. si32 round;
  31. si32 activeStack;
  32. const CGTownInstance * town; //used during town siege, nullptr if this is not a siege (note that fortless town IS also a siege)
  33. int3 tile; //for background and bonuses
  34. bool replayAllowed;
  35. std::vector<CStack*> stacks;
  36. std::vector<std::shared_ptr<CObstacleInstance> > obstacles;
  37. SiegeInfo si;
  38. BattleField battlefieldType; //like !!BA:B
  39. TerrainId terrainType; //used for some stack nativity checks (not the bonus limiters though that have their own copy)
  40. BattleSide tacticsSide; //which side is requested to play tactics phase
  41. ui8 tacticDistance; //how many hexes we can go forward (1 = only hexes adjacent to margin line)
  42. template <typename Handler> void serialize(Handler &h)
  43. {
  44. h & battleID;
  45. h & sides;
  46. h & round;
  47. h & activeStack;
  48. h & town;
  49. h & tile;
  50. h & stacks;
  51. h & obstacles;
  52. h & si;
  53. h & battlefieldType;
  54. h & terrainType;
  55. h & tacticsSide;
  56. h & tacticDistance;
  57. h & static_cast<CBonusSystemNode&>(*this);
  58. h & replayAllowed;
  59. }
  60. //////////////////////////////////////////////////////////////////////////
  61. BattleInfo(const BattleLayout & layout);
  62. BattleInfo();
  63. virtual ~BattleInfo();
  64. const IBattleInfo * getBattle() const override;
  65. std::optional<PlayerColor> getPlayerID() const override;
  66. //////////////////////////////////////////////////////////////////////////
  67. // IBattleInfo
  68. BattleID getBattleID() const override;
  69. int32_t getActiveStackID() const override;
  70. TStacks getStacksIf(const TStackFilter & predicate) const override;
  71. battle::Units getUnitsIf(const battle::UnitFilter & predicate) const override;
  72. BattleField getBattlefieldType() const override;
  73. TerrainId getTerrainType() const override;
  74. ObstacleCList getAllObstacles() const override;
  75. PlayerColor getSidePlayer(BattleSide side) const override;
  76. const CArmedInstance * getSideArmy(BattleSide side) const override;
  77. const CGHeroInstance * getSideHero(BattleSide side) const override;
  78. ui8 getTacticDist() const override;
  79. BattleSide getTacticsSide() const override;
  80. const CGTownInstance * getDefendedTown() const override;
  81. EWallState getWallState(EWallPart partOfWall) const override;
  82. EGateState getGateState() const override;
  83. uint32_t getCastSpells(BattleSide side) const override;
  84. int32_t getEnchanterCounter(BattleSide side) const override;
  85. const IBonusBearer * getBonusBearer() const override;
  86. uint32_t nextUnitId() const override;
  87. int64_t getActualDamage(const DamageRange & damage, int32_t attackerCount, vstd::RNG & rng) const override;
  88. int3 getLocation() const override;
  89. BattleLayout getLayout() const override;
  90. std::vector<SpellID> getUsedSpells(BattleSide side) const override;
  91. //////////////////////////////////////////////////////////////////////////
  92. // IBattleState
  93. void nextRound() override;
  94. void nextTurn(uint32_t unitId) override;
  95. void addUnit(uint32_t id, const JsonNode & data) override;
  96. void moveUnit(uint32_t id, const BattleHex & destination) override;
  97. void setUnitState(uint32_t id, const JsonNode & data, int64_t healthDelta) override;
  98. void removeUnit(uint32_t id) override;
  99. void updateUnit(uint32_t id, const JsonNode & data) override;
  100. void addUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  101. void updateUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  102. void removeUnitBonus(uint32_t id, const std::vector<Bonus> & bonus) override;
  103. void setWallState(EWallPart partOfWall, EWallState state) override;
  104. void addObstacle(const ObstacleChanges & changes) override;
  105. void updateObstacle(const ObstacleChanges& changes) override;
  106. void removeObstacle(uint32_t id) override;
  107. static void addOrUpdateUnitBonus(CStack * sta, const Bonus & value, bool forceAdd);
  108. //////////////////////////////////////////////////////////////////////////
  109. CStack * getStack(int stackID, bool onlyAlive = true);
  110. using CBattleInfoEssentials::battleGetArmyObject;
  111. CArmedInstance * battleGetArmyObject(BattleSide side) const;
  112. using CBattleInfoEssentials::battleGetFightingHero;
  113. CGHeroInstance * battleGetFightingHero(BattleSide side) const;
  114. CStack * generateNewStack(uint32_t id, const CStackInstance & base, BattleSide side, const SlotID & slot, const BattleHex & position);
  115. CStack * generateNewStack(uint32_t id, const CStackBasicDescriptor & base, BattleSide side, const SlotID & slot, const BattleHex & position);
  116. const SideInBattle & getSide(BattleSide side) const;
  117. SideInBattle & getSide(BattleSide side);
  118. const CGHeroInstance * getHero(const PlayerColor & player) const; //returns fighting hero that belongs to given player
  119. void localInit();
  120. static BattleInfo * setupBattle(const int3 & tile, TerrainId, const BattleField & battlefieldType, BattleSideArray<const CArmedInstance *> armies, BattleSideArray<const CGHeroInstance *> heroes, const BattleLayout & layout, const CGTownInstance * town);
  121. BattleSide whatSide(const PlayerColor & player) const;
  122. protected:
  123. #if SCRIPTING_ENABLED
  124. scripting::Pool * getContextPool() const override;
  125. #endif
  126. };
  127. class DLL_LINKAGE CMP_stack
  128. {
  129. int phase; //rules of which phase will be used
  130. int turn;
  131. BattleSide side;
  132. public:
  133. bool operator()(const battle::Unit * a, const battle::Unit * b) const;
  134. CMP_stack(int Phase = 1, int Turn = 0, BattleSide Side = BattleSide::ATTACKER);
  135. };
  136. VCMI_LIB_NAMESPACE_END