CBattleInfoCallback.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * CBattleInfoCallback.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 <vcmi/spells/Magic.h>
  12. #include "ReachabilityInfo.h"
  13. #include "BattleAttackInfo.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class CGHeroInstance;
  16. class CStack;
  17. class ISpellCaster;
  18. class SpellCastEnvironment;
  19. class CSpell;
  20. struct CObstacleInstance;
  21. class IBonusBearer;
  22. class PossiblePlayerBattleAction;
  23. namespace vstd
  24. {
  25. class RNG;
  26. }
  27. namespace spells
  28. {
  29. class Caster;
  30. class Spell;
  31. }
  32. struct DLL_LINKAGE AttackableTiles
  33. {
  34. std::set<BattleHex> hostileCreaturePositions;
  35. std::set<BattleHex> friendlyCreaturePositions; //for Dragon Breath
  36. template <typename Handler> void serialize(Handler &h)
  37. {
  38. h & hostileCreaturePositions;
  39. h & friendlyCreaturePositions;
  40. }
  41. };
  42. struct DLL_LINKAGE BattleClientInterfaceData
  43. {
  44. std::vector<SpellID> creatureSpellsToCast;
  45. ui8 tacticsMode;
  46. };
  47. class DLL_LINKAGE CBattleInfoCallback : public virtual CBattleInfoEssentials
  48. {
  49. public:
  50. std::optional<BattleSide> battleIsFinished() const override; //return none if battle is ongoing; otherwise the victorious side (0/1) or 2 if it is a draw
  51. std::vector<std::shared_ptr<const CObstacleInstance>> battleGetAllObstaclesOnPos(BattleHex tile, bool onlyBlocking = true) const override;
  52. std::vector<std::shared_ptr<const CObstacleInstance>> getAllAffectedObstaclesByStack(const battle::Unit * unit, const std::set<BattleHex> & passed) const override;
  53. //Handle obstacle damage here, requires SpellCastEnvironment
  54. bool handleObstacleTriggersForUnit(SpellCastEnvironment & spellEnv, const battle::Unit & unit, const std::set<BattleHex> & passed = {}) const;
  55. const CStack * battleGetStackByPos(BattleHex pos, bool onlyAlive = true) const;
  56. const battle::Unit * battleGetUnitByPos(BattleHex pos, bool onlyAlive = true) const override;
  57. ///returns all alive units excluding turrets
  58. battle::Units battleAliveUnits() const;
  59. ///returns all alive units from particular side excluding turrets
  60. battle::Units battleAliveUnits(BattleSide side) const;
  61. void battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn = 0, BattleSide lastMoved = BattleSide::NONE) const;
  62. ///returns reachable hexes (valid movement destinations), DOES contain stack current position
  63. std::vector<BattleHex> battleGetAvailableHexes(const battle::Unit * unit, bool obtainMovementRange, bool addOccupiable, std::vector<BattleHex> * attackable) const;
  64. ///returns reachable hexes (valid movement destinations), DOES contain stack current position (lite version)
  65. std::vector<BattleHex> battleGetAvailableHexes(const battle::Unit * unit, bool obtainMovementRange) const;
  66. std::vector<BattleHex> battleGetAvailableHexes(const ReachabilityInfo & cache, const battle::Unit * unit, bool obtainMovementRange) const;
  67. int battleGetSurrenderCost(const PlayerColor & Player) const; //returns cost of surrendering battle, -1 if surrendering is not possible
  68. ReachabilityInfo::TDistances battleGetDistances(const battle::Unit * unit, BattleHex assumedPosition) const;
  69. std::set<BattleHex> battleGetAttackedHexes(const battle::Unit * attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID) const;
  70. bool isEnemyUnitWithinSpecifiedRange(BattleHex attackerPosition, const battle::Unit * defenderUnit, unsigned int range) const;
  71. std::pair< std::vector<BattleHex>, int > getPath(BattleHex start, BattleHex dest, const battle::Unit * stack) const;
  72. bool battleCanAttack(const battle::Unit * stack, const battle::Unit * target, BattleHex dest) const; //determines if stack with given ID can attack target at the selected destination
  73. bool battleCanShoot(const battle::Unit * attacker, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination
  74. bool battleCanShoot(const battle::Unit * attacker) const; //determines if stack with given ID shoot in principle
  75. bool battleIsUnitBlocked(const battle::Unit * unit) const; //returns true if there is neighboring enemy stack
  76. std::set<const battle::Unit *> battleAdjacentUnits(const battle::Unit * unit) const;
  77. DamageEstimation calculateDmgRange(const BattleAttackInfo & info) const;
  78. /// estimates damage dealt by attacker to defender;
  79. /// only non-random bonuses are considered in estimation
  80. /// returns pair <min dmg, max dmg>
  81. DamageEstimation battleEstimateDamage(const BattleAttackInfo & bai, DamageEstimation * retaliationDmg = nullptr) const;
  82. DamageEstimation battleEstimateDamage(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPosition, DamageEstimation * retaliationDmg = nullptr) const;
  83. DamageEstimation battleEstimateDamage(const battle::Unit * attacker, const battle::Unit * defender, int getMovementRange, DamageEstimation * retaliationDmg = nullptr) const;
  84. bool battleIsInsideWalls(BattleHex from) const;
  85. bool battleHasPenaltyOnLine(BattleHex from, BattleHex dest, bool checkWall, bool checkMoat) const;
  86. bool battleHasDistancePenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const;
  87. bool battleHasWallPenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const;
  88. bool battleHasShootingPenalty(const battle::Unit * shooter, BattleHex destHex) const;
  89. BattleHex wallPartToBattleHex(EWallPart part) const;
  90. EWallPart battleHexToWallPart(BattleHex hex) const; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  91. bool isWallPartPotentiallyAttackable(EWallPart wallPart) const; // returns true if the wall part is potentially attackable (independent of wall state), false if not
  92. bool isWallPartAttackable(EWallPart wallPart) const; // returns true if the wall part is actually attackable, false if not
  93. std::vector<BattleHex> getAttackableBattleHexes() const;
  94. si8 battleMinSpellLevel(BattleSide side) const; //calculates maximum spell level possible to be cast on battlefield - takes into account artifacts of both heroes; if no effects are set, 0 is returned
  95. si8 battleMaxSpellLevel(BattleSide side) const; //calculates minimum spell level possible to be cast on battlefield - takes into account artifacts of both heroes; if no effects are set, 0 is returned
  96. int32_t battleGetSpellCost(const spells::Spell * sp, const CGHeroInstance * caster) const; //returns cost of given spell
  97. ESpellCastProblem battleCanCastSpell(const spells::Caster * caster, spells::Mode mode) const; //returns true if there are no general issues preventing from casting a spell
  98. SpellID getRandomBeneficialSpell(vstd::RNG & rand, const battle::Unit * caster, const battle::Unit * target) const;
  99. SpellID getRandomCastedSpell(vstd::RNG & rand, const CStack * caster) const; //called at the beginning of turn for Faerie Dragon
  100. std::vector<PossiblePlayerBattleAction> getClientActionsForStack(const CStack * stack, const BattleClientInterfaceData & data);
  101. PossiblePlayerBattleAction getCasterAction(const CSpell * spell, const spells::Caster * caster, spells::Mode mode) const;
  102. //convenience methods using the ones above
  103. bool isInTacticRange(BattleHex dest) const;
  104. si8 battleGetTacticDist() const; //returns tactic distance for calling player or 0 if this player is not in tactic phase (for ALL_KNOWING actual distance for tactic side)
  105. AttackableTiles getPotentiallyAttackableHexes(
  106. const battle::Unit* attacker,
  107. const battle::Unit* defender,
  108. BattleHex destinationTile,
  109. BattleHex attackerPos,
  110. BattleHex defenderPos) const; //TODO: apply rotation to two-hex attacker
  111. AttackableTiles getPotentiallyAttackableHexes(
  112. const battle::Unit * attacker,
  113. BattleHex destinationTile,
  114. BattleHex attackerPos) const;
  115. AttackableTiles getPotentiallyShootableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
  116. std::vector<const battle::Unit *> getAttackedBattleUnits(
  117. const battle::Unit* attacker,
  118. const battle::Unit * defender,
  119. BattleHex destinationTile,
  120. bool rangedAttack,
  121. BattleHex attackerPos = BattleHex::INVALID,
  122. BattleHex defenderPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
  123. std::set<const CStack*> getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
  124. bool isToReverse(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerHex = BattleHex::INVALID, BattleHex defenderHex = BattleHex::INVALID) const; //determines if attacker standing at attackerHex should reverse in order to attack defender
  125. ReachabilityInfo getReachability(const battle::Unit * unit) const;
  126. ReachabilityInfo getReachability(const ReachabilityInfo::Parameters & params) const;
  127. AccessibilityInfo getAccessibility() const;
  128. AccessibilityInfo getAccessibility(const battle::Unit * stack) const; //Hexes occupied by stack will be marked as accessible.
  129. AccessibilityInfo getAccessibility(const std::vector<BattleHex> & accessibleHexes) const; //given hexes will be marked as accessible
  130. std::pair<const battle::Unit *, BattleHex> getNearestStack(const battle::Unit * closest) const;
  131. BattleHex getAvailableHex(const CreatureID & creID, BattleSide side, int initialPos = -1) const; //find place for adding new stack
  132. protected:
  133. ReachabilityInfo getFlyingReachability(const ReachabilityInfo::Parameters & params) const;
  134. ReachabilityInfo makeBFS(const AccessibilityInfo & accessibility, const ReachabilityInfo::Parameters & params) const;
  135. bool isInObstacle(BattleHex hex, const std::set<BattleHex> & obstacles, const ReachabilityInfo::Parameters & params) const;
  136. std::set<BattleHex> getStoppers(BattleSide whichSidePerspective) const; //get hexes with stopping obstacles (quicksands)
  137. };
  138. VCMI_LIB_NAMESPACE_END