BattleInfo.h 5.9 KB

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