CBattleInfoCallback.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "CCallbackBase.h"
  12. #include "ReachabilityInfo.h"
  13. #include "BattleAttackInfo.h"
  14. #include "../spells/Magic.h"
  15. class CGHeroInstance;
  16. class CStack;
  17. class ISpellCaster;
  18. class CSpell;
  19. struct CObstacleInstance;
  20. class IBonusBearer;
  21. class CRandomGenerator;
  22. struct DLL_LINKAGE AttackableTiles
  23. {
  24. std::set<BattleHex> hostileCreaturePositions;
  25. std::set<BattleHex> friendlyCreaturePositions; //for Dragon Breath
  26. template <typename Handler> void serialize(Handler &h, const int version)
  27. {
  28. h & hostileCreaturePositions;
  29. h & friendlyCreaturePositions;
  30. }
  31. };
  32. enum class PossiblePlayerBattleAction // actions performed at l-click
  33. {
  34. INVALID = -1, CREATURE_INFO,
  35. MOVE_TACTICS, CHOOSE_TACTICS_STACK,
  36. MOVE_STACK, ATTACK, WALK_AND_ATTACK, ATTACK_AND_RETURN, SHOOT, //OPEN_GATE, //we can open castle gate during siege
  37. NO_LOCATION, ANY_LOCATION, OBSTACLE, TELEPORT, SACRIFICE, RANDOM_GENIE_SPELL,
  38. FREE_LOCATION, //used with Force Field and Fire Wall - all tiles affected by spell must be free
  39. CATAPULT, HEAL, RISE_DEMONS,
  40. AIMED_SPELL_CREATURE
  41. };
  42. struct DLL_LINKAGE BattleClientInterfaceData
  43. {
  44. si32 creatureSpellToCast;
  45. ui8 tacticsMode;
  46. };
  47. class DLL_LINKAGE CBattleInfoCallback : public virtual CBattleInfoEssentials
  48. {
  49. public:
  50. enum ERandomSpell
  51. {
  52. RANDOM_GENIE, RANDOM_AIMED
  53. };
  54. boost::optional<int> battleIsFinished() const; //return none if battle is ongoing; otherwise the victorious side (0/1) or 2 if it is a draw
  55. std::vector<std::shared_ptr<const CObstacleInstance>> battleGetAllObstaclesOnPos(BattleHex tile, bool onlyBlocking = true) const; //blocking obstacles makes tile inaccessible, others cause special effects (like Land Mines, Moat, Quicksands)
  56. std::vector<std::shared_ptr<const CObstacleInstance>> getAllAffectedObstaclesByStack(const CStack * stack) const;
  57. const CStack * battleGetStackByPos(BattleHex pos, bool onlyAlive = true) const;
  58. const battle::Unit * battleGetUnitByPos(BattleHex pos, bool onlyAlive = true) const;
  59. ///returns all alive units excluding turrets
  60. battle::Units battleAliveUnits() const;
  61. ///returns all alive units from particular side excluding turrets
  62. battle::Units battleAliveUnits(ui8 side) const;
  63. void battleGetTurnOrder(std::vector<battle::Units> & out, const size_t maxUnits, const int maxTurns, const int turn = 0, int8_t lastMoved = -1) const;
  64. void battleGetStackCountOutsideHexes(bool *ac) const; // returns hexes which when in front of a stack cause us to move the amount box back
  65. ///returns reachable hexes (valid movement destinations), DOES contain stack current position
  66. std::vector<BattleHex> battleGetAvailableHexes(const battle::Unit * unit, bool addOccupiable, std::vector<BattleHex> * attackable) const;
  67. ///returns reachable hexes (valid movement destinations), DOES contain stack current position (lite version)
  68. std::vector<BattleHex> battleGetAvailableHexes(const battle::Unit * unit) const;
  69. std::vector<BattleHex> battleGetAvailableHexes(const ReachabilityInfo & cache, const battle::Unit * unit) const;
  70. int battleGetSurrenderCost(PlayerColor Player) const; //returns cost of surrendering battle, -1 if surrendering is not possible
  71. ReachabilityInfo::TDistances battleGetDistances(const battle::Unit * unit, BattleHex assumedPosition) const;
  72. std::set<BattleHex> battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID) const;
  73. bool battleCanAttack(const CStack * stack, const CStack * target, BattleHex dest) const; //determines if stack with given ID can attack target at the selected destination
  74. bool battleCanShoot(const battle::Unit * attacker, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination
  75. bool battleCanShoot(const battle::Unit * attacker) const; //determines if stack with given ID shoot in principle
  76. bool battleIsUnitBlocked(const battle::Unit * unit) const; //returns true if there is neighboring enemy stack
  77. std::set<const battle::Unit *> battleAdjacentUnits(const battle::Unit * unit) const;
  78. TDmgRange calculateDmgRange(const BattleAttackInfo & info) const; //charge - number of hexes travelled before attack (for champion's jousting); returns pair <min dmg, max dmg>
  79. TDmgRange battleEstimateDamage(const BattleAttackInfo & bai, TDmgRange * retaliationDmg = nullptr) const; //estimates damage dealt by attacker to defender; it may be not precise especially when stack has randomly working bonuses; returns pair <min dmg, max dmg>
  80. TDmgRange battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg = nullptr) const; //estimates damage dealt by attacker to defender; it may be not precise especially when stack has randomly working bonuses; returns pair <min dmg, max dmg>
  81. bool battleHasDistancePenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const;
  82. bool battleHasWallPenalty(const IBonusBearer * shooter, BattleHex shooterPosition, BattleHex destHex) const;
  83. bool battleHasShootingPenalty(const battle::Unit * shooter, BattleHex destHex) const;
  84. BattleHex wallPartToBattleHex(EWallPart::EWallPart part) const;
  85. EWallPart::EWallPart battleHexToWallPart(BattleHex hex) const; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  86. bool isWallPartPotentiallyAttackable(EWallPart::EWallPart wallPart) const; // returns true if the wall part is potentially attackable (independent of wall state), false if not
  87. std::vector<BattleHex> getAttackableBattleHexes() const;
  88. 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
  89. 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
  90. ui32 battleGetSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //returns cost of given spell
  91. ESpellCastProblem::ESpellCastProblem battleCanCastSpell(const spells::Caster * caster, spells::Mode mode) const; //returns true if there are no general issues preventing from casting a spell
  92. SpellID battleGetRandomStackSpell(CRandomGenerator & rand, const CStack * stack, ERandomSpell mode) const;
  93. SpellID getRandomBeneficialSpell(CRandomGenerator & rand, const CStack * subject) const;
  94. SpellID getRandomCastedSpell(CRandomGenerator & rand, const CStack * caster) const; //called at the beginning of turn for Faerie Dragon
  95. si8 battleCanTeleportTo(const battle::Unit * stack, BattleHex destHex, int telportLevel) const; //checks if teleportation of given stack to given position can take place
  96. std::vector<PossiblePlayerBattleAction> getClientActionsForStack(const CStack * stack, const BattleClientInterfaceData & data);
  97. PossiblePlayerBattleAction getCasterAction(const CSpell * spell, const spells::Caster * caster, spells::Mode mode) const;
  98. //convenience methods using the ones above
  99. bool isInTacticRange(BattleHex dest) const;
  100. 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)
  101. AttackableTiles getPotentiallyAttackableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const; //TODO: apply rotation to two-hex attacker
  102. AttackableTiles getPotentiallyShootableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
  103. 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
  104. std::set<const CStack*> getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
  105. bool isToReverse(BattleHex hexFrom, BattleHex hexTo, bool curDir /*if true, creature is in attacker's direction*/, bool toDoubleWide, bool toDir) const; //determines if creature should be reversed (it stands on hexFrom and should 'see' hexTo)
  106. bool isToReverseHlp(BattleHex hexFrom, BattleHex hexTo, bool curDir) const; //helper for isToReverse
  107. ReachabilityInfo getReachability(const battle::Unit * unit) const;
  108. ReachabilityInfo getReachability(const ReachabilityInfo::Parameters & params) const;
  109. AccessibilityInfo getAccesibility() const;
  110. AccessibilityInfo getAccesibility(const battle::Unit * stack) const; //Hexes ocupied by stack will be marked as accessible.
  111. AccessibilityInfo getAccesibility(const std::vector<BattleHex> & accessibleHexes) const; //given hexes will be marked as accessible
  112. std::pair<const battle::Unit *, BattleHex> getNearestStack(const battle::Unit * closest) const;
  113. BattleHex getAvaliableHex(CreatureID creID, ui8 side, int initialPos = -1) const; //find place for adding new stack
  114. protected:
  115. ReachabilityInfo getFlyingReachability(const ReachabilityInfo::Parameters & params) const;
  116. ReachabilityInfo makeBFS(const AccessibilityInfo & accessibility, const ReachabilityInfo::Parameters & params) const;
  117. std::set<BattleHex> getStoppers(BattlePerspective::BattlePerspective whichSidePerspective) const; //get hexes with stopping obstacles (quicksands)
  118. };