CBattleCallback.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #pragma once
  2. #include "BattleHex.h"
  3. class CGameState;
  4. class CGTownInstance;
  5. class CGHeroInstance;
  6. class CStack;
  7. class CSpell;
  8. struct BattleInfo;
  9. struct CObstacleInstance;
  10. class IBonusBearer;
  11. struct InfoAboutHero;
  12. namespace boost
  13. {class shared_mutex;}
  14. namespace BattleSide
  15. {
  16. enum {ATTACKER = 0, DEFENDER = 1};
  17. }
  18. typedef std::vector<const CStack*> TStacks;
  19. class CBattleInfoEssentials;
  20. //Basic class for various callbacks (interfaces called by players to get info about game and so forth)
  21. class DLL_LINKAGE CCallbackBase
  22. {
  23. const BattleInfo *battle; //battle to which the player is engaged, NULL if none or not applicable
  24. const BattleInfo * getBattle() const
  25. {
  26. return battle;
  27. }
  28. protected:
  29. CGameState *gs;
  30. int player; // -1 gives access to all information, otherwise callback provides only information "visible" for player
  31. CCallbackBase(CGameState *GS, int Player)
  32. : battle(nullptr), gs(GS), player(Player)
  33. {}
  34. CCallbackBase()
  35. : battle(nullptr), gs(nullptr), player(-1)
  36. {}
  37. void setBattle(const BattleInfo *B);
  38. bool duringBattle() const;
  39. public:
  40. boost::shared_mutex &getGsMutex(); //just return a reference to mutex, does not lock nor anything
  41. int getPlayerID() const;
  42. friend class CBattleInfoEssentials;
  43. };
  44. struct DLL_LINKAGE AttackableTiles
  45. {
  46. std::set<BattleHex> hostileCreaturePositions;
  47. std::set<BattleHex> friendlyCreaturePositions; //for Dragon Breath
  48. template <typename Handler> void serialize(Handler &h, const int version)
  49. {
  50. h & hostileCreaturePositions & friendlyCreaturePositions;
  51. }
  52. };
  53. //Accessibility is property of hex in battle. It doesn't depend on stack, side's perspective and so on.
  54. namespace EAccessibility
  55. {
  56. enum EAccessibility
  57. {
  58. ACCESSIBLE,
  59. ALIVE_STACK,
  60. OBSTACLE,
  61. DESTRUCTIBLE_WALL,
  62. GATE, //sieges -> gate opens only for defender stacks
  63. UNAVAILABLE, //indestructible wall parts, special battlefields (like boat-to-boat)
  64. SIDE_COLUMN //used for first and last columns of hexes that are unavailable but wat machines can stand there
  65. };
  66. }
  67. typedef std::array<EAccessibility::EAccessibility, GameConstants::BFIELD_SIZE> TAccessibilityArray;
  68. struct DLL_LINKAGE AccessibilityInfo : TAccessibilityArray
  69. {
  70. bool occupiable(const CStack *stack, BattleHex tile) const;
  71. bool accessible(BattleHex tile, const CStack *stack) const; //checks for both tiles if stack is double wide
  72. bool accessible(BattleHex tile, bool doubleWide, bool attackerOwned) const; //checks for both tiles if stack is double wide
  73. };
  74. namespace BattlePerspective
  75. {
  76. enum BattlePerspective
  77. {
  78. INVALID = -2,
  79. ALL_KNOWING = -1,
  80. LEFT_SIDE,
  81. RIGHT_SIDE
  82. };
  83. }
  84. // Reachability info is result of BFS calculation. It's dependant on stack (it's owner, whether it's flying),
  85. // startPosition and perpective.
  86. struct DLL_LINKAGE ReachabilityInfo
  87. {
  88. typedef std::array<int, GameConstants::BFIELD_SIZE> TDistances;
  89. typedef std::array<BattleHex, GameConstants::BFIELD_SIZE> TPredecessors;
  90. enum { INFINITE_DIST = 1000000 };
  91. struct DLL_LINKAGE Parameters
  92. {
  93. const CStack *stack; //stack for which calculation is mage => not required (kept for debugging mostly), following variables are enough
  94. bool attackerOwned;
  95. bool doubleWide;
  96. bool flying;
  97. std::vector<BattleHex> knownAccessible; //hexes that will be treated as accessible, even if they're occupied by stack (by default - tiles occupied by stack we do reachability for, so it doesn't block itself)
  98. BattleHex startPosition; //assumed position of stack
  99. BattlePerspective::BattlePerspective perspective; //some obstacles (eg. quicksands) may be invisible for some side
  100. Parameters();
  101. Parameters(const CStack *Stack);
  102. };
  103. Parameters params;
  104. AccessibilityInfo accessibility;
  105. TDistances distances;
  106. TPredecessors predecessors;
  107. ReachabilityInfo()
  108. {
  109. distances.fill(INFINITE_DIST);
  110. predecessors.fill(BattleHex::INVALID);
  111. }
  112. bool isReachable(BattleHex hex) const
  113. {
  114. return distances[hex] < INFINITE_DIST;
  115. }
  116. };
  117. class DLL_LINKAGE CBattleInfoEssentials : public virtual CCallbackBase
  118. {
  119. protected:
  120. bool battleDoWeKnowAbout(ui8 side) const;
  121. public:
  122. enum EStackOwnership
  123. {
  124. ONLY_MINE, ONLY_ENEMY, MINE_AND_ENEMY
  125. };
  126. BattlePerspective::BattlePerspective battleGetMySide() const;
  127. ui8 battleTerrainType() const;
  128. int battleGetBattlefieldType() const; // 1. sand/shore 2. sand/mesas 3. dirt/birches 4. dirt/hills 5. dirt/pines 6. grass/hills 7. grass/pines 8. lava 9. magic plains 10. snow/mountains 11. snow/trees 12. subterranean 13. swamp/trees 14. fiery fields 15. rock lands 16. magic clouds 17. lucid pools 18. holy ground 19. clover field 20. evil fog 21. "favourable winds" text on magic plains background 22. cursed ground 23. rough 24. ship to ship 25. ship
  129. std::vector<shared_ptr<const CObstacleInstance> > battleGetAllObstacles(boost::optional<BattlePerspective::BattlePerspective> perspective = boost::none) const; //returns all obstacles on the battlefield
  130. TStacks battleGetAllStacks() const; //returns all stacks, alive or dead or undead or mechanical :)
  131. bool battleHasNativeStack(ui8 side) const;
  132. ui8 battleGetWallState(int partOfWall) const; //for determining state of a part of the wall; format: parameter [0] - keep, [1] - bottom tower, [2] - bottom wall, [3] - below gate, [4] - over gate, [5] - upper wall, [6] - uppert tower, [7] - gate; returned value: 1 - intact, 2 - damaged, 3 - destroyed; 0 - no battle
  133. int battleGetMoatDmg() const; //what dmg unit will suffer if ending turn in the moat
  134. const CGTownInstance * battleGetDefendedTown() const; //returns defended town if current battle is a siege, NULL instead
  135. const CStack *battleActiveStack() const;
  136. si8 battleTacticDist() const; //returns tactic distance in current tactics phase; 0 if not in tactics phase
  137. si8 battleGetTacticsSide() const; //returns which side is in tactics phase, undefined if none (?)
  138. bool battleCanFlee(int player) const;
  139. bool battleCanSurrender(int player) const;
  140. ui8 playerToSide(int player) const;
  141. ui8 battleGetSiegeLevel() const; //returns 0 when there is no siege, 1 if fort, 2 is citadel, 3 is castle
  142. bool battleHasHero(ui8 side) const;
  143. int battleCastSpells(ui8 side) const; //how many spells has given side casted
  144. const CGHeroInstance * battleGetFightingHero(ui8 side) const;
  145. //helpers
  146. TStacks battleAliveStacks() const;
  147. TStacks battleAliveStacks(ui8 side) const;
  148. const CStack * battleGetStackByID(int ID, bool onlyAlive = true) const; //returns stack info by given ID
  149. bool battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const;
  150. //ESpellCastProblem::ESpellCastProblem battleCanCastSpell(int player, ECastingMode::ECastingMode mode) const; //Checks if player is able to cast spells (at all) at the moment
  151. };
  152. struct DLL_LINKAGE BattleAttackInfo
  153. {
  154. const IBonusBearer *attackerBonuses, *defenderBonuses;
  155. const CStack *attacker, *defender;
  156. BattleHex attackerPosition, defenderPosition;
  157. int attackerCount;
  158. bool shooting;
  159. int chargedFields;
  160. bool luckyHit;
  161. bool deathBlow;
  162. bool ballistaDoubleDamage;
  163. BattleAttackInfo(const CStack *Attacker, const CStack *Defender, bool Shooting = false);
  164. BattleAttackInfo reverse() const;
  165. };
  166. class DLL_LINKAGE CBattleInfoCallback : public virtual CBattleInfoEssentials
  167. {
  168. public:
  169. enum ERandomSpell
  170. {
  171. RANDOM_GENIE, RANDOM_AIMED
  172. };
  173. //battle
  174. shared_ptr<const CObstacleInstance> battleGetObstacleOnPos(BattleHex tile, bool onlyBlocking = true) const; //blocking obstacles makes tile inaccessible, others cause special effects (like Land Mines, Moat, Quicksands)
  175. const CStack * battleGetStackByPos(BattleHex pos, bool onlyAlive = true) const; //returns stack info by given pos
  176. void battleGetStackQueue(std::vector<const CStack *> &out, const int howMany, const int turn = 0, int lastMoved = -1) const;
  177. void battleGetStackCountOutsideHexes(bool *ac) const; // returns hexes which when in front of a stack cause us to move the amount box back
  178. std::vector<BattleHex> battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<BattleHex> * attackable = NULL) const; //returns hexes reachable by creature with id ID (valid movement destinations), DOES contain stack current position
  179. int battleGetSurrenderCost(int Player) const; //returns cost of surrendering battle, -1 if surrendering is not possible
  180. ReachabilityInfo::TDistances battleGetDistances(const CStack * stack, BattleHex hex = BattleHex::INVALID, BattleHex * predecessors = NULL) const; //returns vector of distances to [dest hex number]
  181. std::set<BattleHex> battleGetAttackedHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID) const;
  182. bool battleCanShoot(const CStack * stack, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination
  183. bool battleIsStackBlocked(const CStack * stack) const; //returns true if there is neighboring enemy stack
  184. std::set<const CStack*> batteAdjacentCreatures (const CStack * stack) const;
  185. TDmgRange calculateDmgRange(const BattleAttackInfo &info) const; //charge - number of hexes travelled before attack (for champion's jousting); returns pair <min dmg, max dmg>
  186. TDmgRange calculateDmgRange(const CStack* attacker, const CStack* defender, TQuantity attackerCount, bool shooting, ui8 charge, bool lucky, bool deathBlow, bool ballistaDoubleDmg) const; //charge - number of hexes travelled before attack (for champion's jousting); returns pair <min dmg, max dmg>
  187. TDmgRange calculateDmgRange(const CStack* attacker, const CStack* defender, bool shooting, ui8 charge, bool lucky, bool deathBlow, bool ballistaDoubleDmg) const; //charge - number of hexes travelled before attack (for champion's jousting); returns pair <min dmg, max dmg>
  188. //hextowallpart //int battleGetWallUnderHex(BattleHex hex) const; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  189. std::pair<ui32, ui32> battleEstimateDamage(const BattleAttackInfo &bai, std::pair<ui32, ui32> * retaliationDmg = NULL) 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>
  190. std::pair<ui32, ui32> battleEstimateDamage(const CStack * attacker, const CStack * defender, std::pair<ui32, ui32> * retaliationDmg = NULL) 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>
  191. si8 battleHasDistancePenalty( const CStack * stack, BattleHex destHex ) const;
  192. si8 battleHasDistancePenalty(const IBonusBearer *bonusBearer, BattleHex shooterPosition, BattleHex destHex ) const;
  193. si8 battleHasWallPenalty(const CStack * stack, BattleHex destHex) const; //checks if given stack has wall penalty
  194. si8 battleHasWallPenalty(const IBonusBearer *bonusBearer, BattleHex shooterPosition, BattleHex destHex) const; //checks if given stack has wall penalty
  195. EWallParts::EWallParts battleHexToWallPart(BattleHex hex) const; //returns part of destructible wall / gate / keep under given hex or -1 if not found
  196. //*** MAGIC
  197. si8 battleMaxSpellLevel() 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
  198. ui32 battleGetSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //returns cost of given spell
  199. ESpellCastProblem::ESpellCastProblem battleCanCastSpell(int player, ECastingMode::ECastingMode mode) const; //returns true if there are no general issues preventing from casting a spell
  200. ESpellCastProblem::ESpellCastProblem battleCanCastThisSpell(int player, const CSpell * spell, ECastingMode::ECastingMode mode) const; //checks if given player can cast given spell
  201. ESpellCastProblem::ESpellCastProblem battleCanCastThisSpellHere(int player, const CSpell * spell, ECastingMode::ECastingMode mode, BattleHex dest) const; //checks if given player can cast given spell at given tile in given mode
  202. ESpellCastProblem::ESpellCastProblem battleCanCreatureCastThisSpell(const CSpell * spell, BattleHex destination) const; //determines if creature can cast a spell here
  203. std::vector<BattleHex> battleGetPossibleTargets(int player, const CSpell *spell) const;
  204. ui32 calculateSpellBonus(ui32 baseDamage, const CSpell * sp, const CGHeroInstance * caster, const CStack * affectedCreature) const;
  205. ui32 calculateSpellDmg(const CSpell * sp, const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const; //calculates damage inflicted by spell
  206. std::set<const CStack*> getAffectedCreatures(const CSpell * s, int skillLevel, ui8 attackerOwner, BattleHex destinationTile); //calculates stack affected by given spell
  207. si32 battleGetRandomStackSpell(const CStack * stack, ERandomSpell mode) const;
  208. TSpell getRandomBeneficialSpell(const CStack * subject) const;
  209. TSpell getRandomCastedSpell(const CStack * caster) const; //called at the beginning of turn for Faerie Dragon
  210. //checks for creature immunity / anything that prevent casting *at given hex* - doesn't take into acount general problems such as not having spellbook or mana points etc.
  211. ESpellCastProblem::ESpellCastProblem battleIsImmune(const CGHeroInstance * caster, const CSpell * spell, ECastingMode::ECastingMode mode, BattleHex dest) const;
  212. const CStack * getStackIf(boost::function<bool(const CStack*)> pred) const;
  213. si8 battleHasShootingPenalty(const CStack * stack, BattleHex destHex)
  214. {
  215. return battleHasDistancePenalty(stack, destHex) || battleHasWallPenalty(stack, destHex);
  216. }
  217. si8 battleCanTeleportTo(const CStack * stack, BattleHex destHex, int telportLevel) const; //checks if teleportation of given stack to given position can take place
  218. //convenience methods using the ones above
  219. bool isInTacticRange( BattleHex dest ) const;
  220. 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)
  221. AttackableTiles getPotentiallyAttackableHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
  222. std::set<const CStack*> getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
  223. ReachabilityInfo getReachability(const CStack *stack) const;
  224. ReachabilityInfo getReachability(const ReachabilityInfo::Parameters &params) const;
  225. AccessibilityInfo getAccesibility() const;
  226. AccessibilityInfo getAccesibility(const CStack *stack) const; //Hexes ocupied by stack will be marked as accessible.
  227. AccessibilityInfo getAccesibility(const std::vector<BattleHex> &accessibleHexes) const; //given hexes will be marked as accessible
  228. std::pair<const CStack *, BattleHex> getNearestStack(const CStack * closest, boost::logic::tribool attackerOwned) const;
  229. protected:
  230. ReachabilityInfo getFlyingReachability(const ReachabilityInfo::Parameters params) const;
  231. ReachabilityInfo makeBFS(const AccessibilityInfo &accessibility, const ReachabilityInfo::Parameters params) const;
  232. ReachabilityInfo makeBFS(const CStack *stack) const; //uses default parameters -> stack position and owner's perspective
  233. std::set<BattleHex> getStoppers(BattlePerspective::BattlePerspective whichSidePerspective) const; //get hexes with stopping obstacles (quicksands)
  234. };
  235. class DLL_LINKAGE CPlayerBattleCallback : public CBattleInfoCallback
  236. {
  237. public:
  238. bool battleCanFlee() const; //returns true if caller can flee from the battle
  239. TStacks battleGetStacks(EStackOwnership whose = MINE_AND_ENEMY, bool onlyAlive = true) const; //returns stacks on battlefield
  240. ESpellCastProblem::ESpellCastProblem battleCanCastThisSpell(const CSpell * spell) const; //determines if given spell can be casted (and returns problem description)
  241. ESpellCastProblem::ESpellCastProblem battleCanCastThisSpell(const CSpell * spell, BattleHex destination) const; //if hero can cast spell here
  242. ESpellCastProblem::ESpellCastProblem battleCanCreatureCastThisSpell(const CSpell * spell, BattleHex destination) const; //determines if creature can cast a spell here
  243. int battleGetSurrenderCost() const; //returns cost of surrendering battle, -1 if surrendering is not possible
  244. bool battleCanCastSpell(ESpellCastProblem::ESpellCastProblem *outProblem = nullptr) const; //returns true, if caller can cast a spell. If not, if pointer is given via arg, the reason will be written.
  245. const CGHeroInstance * battleGetMyHero() const;
  246. InfoAboutHero battleGetEnemyHero() const;
  247. };