CBattleInfoCallback.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "CCallbackBase.h"
  13. #include "ReachabilityInfo.h"
  14. #include "BattleAttackInfo.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class CGHeroInstance;
  17. class CStack;
  18. class ISpellCaster;
  19. class CSpell;
  20. struct CObstacleInstance;
  21. class IBonusBearer;
  22. class CRandomGenerator;
  23. namespace spells
  24. {
  25. class Caster;
  26. class Spell;
  27. }
  28. struct DLL_LINKAGE AttackableTiles
  29. {
  30. std::set<BattleHex> hostileCreaturePositions;
  31. std::set<BattleHex> friendlyCreaturePositions; //for Dragon Breath
  32. template <typename Handler> void serialize(Handler &h, const int version)
  33. {
  34. h & hostileCreaturePositions;
  35. h & friendlyCreaturePositions;
  36. }
  37. };
  38. enum class PossiblePlayerBattleAction // actions performed at l-click
  39. {
  40. INVALID = -1,
  41. CREATURE_INFO,
  42. HERO_INFO,
  43. MOVE_TACTICS,
  44. CHOOSE_TACTICS_STACK,
  45. MOVE_STACK,
  46. ATTACK,
  47. WALK_AND_ATTACK,
  48. ATTACK_AND_RETURN,
  49. SHOOT,
  50. CATAPULT,
  51. HEAL,
  52. NO_LOCATION, // massive spells that affect every possible target, automatic casts
  53. ANY_LOCATION,
  54. OBSTACLE,
  55. TELEPORT,
  56. SACRIFICE,
  57. RANDOM_GENIE_SPELL, // random spell on a friendly creature
  58. FREE_LOCATION, // used with Force Field and Fire Wall - all tiles affected by spell must be free
  59. AIMED_SPELL_CREATURE, // spell targeted at creature
  60. };
  61. struct DLL_LINKAGE BattleClientInterfaceData
  62. {
  63. si32 creatureSpellToCast;
  64. ui8 tacticsMode;
  65. };
  66. class DLL_LINKAGE CBattleInfoCallback : public virtual CBattleInfoEssentials
  67. {
  68. public:
  69. enum ERandomSpell
  70. {
  71. RANDOM_GENIE, RANDOM_AIMED
  72. };
  73. boost::optional<int> battleIsFinished() const override; //return none if battle is ongoing; otherwise the victorious side (0/1) or 2 if it is a draw
  74. std::vector<std::shared_ptr<const CObstacleInstance>> battleGetAllObstaclesOnPos(BattleHex tile, bool onlyBlocking = true) const override;
  75. std::vector<std::shared_ptr<const CObstacleInstance>> getAllAffectedObstaclesByStack(const battle::Unit * unit) const override;
  76. const CStack * battleGetStackByPos(BattleHex pos, bool onlyAlive = true) const;
  77. const battle::Unit * battleGetUnitByPos(BattleHex pos, bool onlyAlive = true) const override;
  78. ///returns all alive units excluding turrets
  79. battle::Units battleAliveUnits() const;
  80. ///returns all alive units from particular side excluding turrets
  81. battle::Units battleAliveUnits(ui8 side) const;
  82. void battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn = 0, int8_t lastMoved = -1) const;
  83. ///returns reachable hexes (valid movement destinations), DOES contain stack current position
  84. std::vector<BattleHex> battleGetAvailableHexes(const battle::Unit * unit, bool addOccupiable, std::vector<BattleHex> * attackable) const;
  85. ///returns reachable hexes (valid movement destinations), DOES contain stack current position (lite version)
  86. std::vector<BattleHex> battleGetAvailableHexes(const battle::Unit * unit) const;
  87. std::vector<BattleHex> battleGetAvailableHexes(const ReachabilityInfo & cache, const battle::Unit * unit) const;
  88. int battleGetSurrenderCost(const PlayerColor & Player) const; //returns cost of surrendering battle, -1 if surrendering is not possible
  89. ReachabilityInfo::TDistances battleGetDistances(const battle::Unit * unit, BattleHex assumedPosition) const;
  90. std::set<BattleHex> battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID) const;
  91. bool isEnemyUnitWithinSpecifiedRange(BattleHex attackerPosition, const battle::Unit * defenderUnit, unsigned int range) const;
  92. bool battleCanAttack(const CStack * stack, const CStack * target, BattleHex dest) const; //determines if stack with given ID can attack target at the selected destination
  93. bool battleCanShoot(const battle::Unit * attacker, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination
  94. bool battleCanShoot(const battle::Unit * attacker) const; //determines if stack with given ID shoot in principle
  95. bool battleIsUnitBlocked(const battle::Unit * unit) const; //returns true if there is neighboring enemy stack
  96. std::set<const battle::Unit *> battleAdjacentUnits(const battle::Unit * unit) const;
  97. DamageEstimation calculateDmgRange(const BattleAttackInfo & info) const;
  98. /// estimates damage dealt by attacker to defender;
  99. /// only non-random bonuses are considered in estimation
  100. /// returns pair <min dmg, max dmg>
  101. DamageEstimation battleEstimateDamage(const BattleAttackInfo & bai, DamageEstimation * retaliationDmg = nullptr) const;
  102. DamageEstimation battleEstimateDamage(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPosition, DamageEstimation * retaliationDmg = nullptr) const;
  103. DamageEstimation battleEstimateDamage(const battle::Unit * attacker, const battle::Unit * defender, int movementDistance, DamageEstimation * retaliationDmg = nullptr) const;
  104. bool battleHasDistancePenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const;
  105. bool battleHasWallPenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const;
  106. bool battleHasShootingPenalty(const battle::Unit * shooter, BattleHex destHex) const;
  107. BattleHex wallPartToBattleHex(EWallPart part) const;
  108. EWallPart battleHexToWallPart(BattleHex hex) const; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  109. bool isWallPartPotentiallyAttackable(EWallPart wallPart) const; // returns true if the wall part is potentially attackable (independent of wall state), false if not
  110. bool isWallPartAttackable(EWallPart wallPart) const; // returns true if the wall part is actually attackable, false if not
  111. std::vector<BattleHex> getAttackableBattleHexes() const;
  112. si8 battleMinSpellLevel(ui8 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
  113. si8 battleMaxSpellLevel(ui8 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
  114. int32_t battleGetSpellCost(const spells::Spell * sp, const CGHeroInstance * caster) const; //returns cost of given spell
  115. ESpellCastProblem::ESpellCastProblem battleCanCastSpell(const spells::Caster * caster, spells::Mode mode) const; //returns true if there are no general issues preventing from casting a spell
  116. SpellID battleGetRandomStackSpell(CRandomGenerator & rand, const CStack * stack, ERandomSpell mode) const;
  117. SpellID getRandomBeneficialSpell(CRandomGenerator & rand, const CStack * subject) const;
  118. SpellID getRandomCastedSpell(CRandomGenerator & rand, const CStack * caster) const; //called at the beginning of turn for Faerie Dragon
  119. si8 battleCanTeleportTo(const battle::Unit * stack, BattleHex destHex, int telportLevel) const; //checks if teleportation of given stack to given position can take place
  120. std::vector<PossiblePlayerBattleAction> getClientActionsForStack(const CStack * stack, const BattleClientInterfaceData & data);
  121. PossiblePlayerBattleAction getCasterAction(const CSpell * spell, const spells::Caster * caster, spells::Mode mode) const;
  122. //convenience methods using the ones above
  123. bool isInTacticRange(BattleHex dest) const;
  124. 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)
  125. AttackableTiles getPotentiallyAttackableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const; //TODO: apply rotation to two-hex attacker
  126. AttackableTiles getPotentiallyShootableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
  127. std::vector<const battle::Unit *> getAttackedBattleUnits(const battle::Unit* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
  128. std::set<const CStack*> getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
  129. bool isToReverse(const battle::Unit * attacker, const battle::Unit * defender) const; //determines if attacker standing at attackerHex should reverse in order to attack defender
  130. ReachabilityInfo getReachability(const battle::Unit * unit) const;
  131. ReachabilityInfo getReachability(const ReachabilityInfo::Parameters & params) const;
  132. AccessibilityInfo getAccesibility() const;
  133. AccessibilityInfo getAccesibility(const battle::Unit * stack) const; //Hexes ocupied by stack will be marked as accessible.
  134. AccessibilityInfo getAccesibility(const std::vector<BattleHex> & accessibleHexes) const; //given hexes will be marked as accessible
  135. std::pair<const battle::Unit *, BattleHex> getNearestStack(const battle::Unit * closest) const;
  136. BattleHex getAvaliableHex(const CreatureID & creID, ui8 side, int initialPos = -1) const; //find place for adding new stack
  137. protected:
  138. ReachabilityInfo getFlyingReachability(const ReachabilityInfo::Parameters & params) const;
  139. ReachabilityInfo makeBFS(const AccessibilityInfo & accessibility, const ReachabilityInfo::Parameters & params) const;
  140. bool isInObstacle(BattleHex hex, const std::set<BattleHex> & obstacles, const ReachabilityInfo::Parameters & params) const;
  141. std::set<BattleHex> getStoppers(BattlePerspective::BattlePerspective whichSidePerspective) const; //get hexes with stopping obstacles (quicksands)
  142. };
  143. VCMI_LIB_NAMESPACE_END