BattleAI.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #include "../../lib/BattleHex.h"
  3. #include "../../lib/HeroBonus.h"
  4. #include "../../lib/CBattleCallback.h"
  5. class CSpell;
  6. class StackWithBonuses : public IBonusBearer
  7. {
  8. public:
  9. const CStack *stack;
  10. mutable std::vector<Bonus> bonusesToAdd;
  11. virtual const TBonusListPtr getAllBonuses(const CSelector &selector, const CSelector &limit, const CBonusSystemNode *root = NULL, const std::string &cachingStr = "") const OVERRIDE;
  12. };
  13. struct EnemyInfo
  14. {
  15. const CStack * s;
  16. int adi, adr;
  17. std::vector<BattleHex> attackFrom; //for melee fight
  18. EnemyInfo(const CStack * _s) : s(_s)
  19. {}
  20. void calcDmg(const CStack * ourStack);
  21. bool operator==(const EnemyInfo& ei) const
  22. {
  23. return s == ei.s;
  24. }
  25. };
  26. //FIXME: unused function
  27. /*
  28. static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const BattleHex &h2)
  29. {
  30. int shooters[2] = {0}; //count of shooters on hexes
  31. for(int i = 0; i < 2; i++)
  32. BOOST_FOREACH(BattleHex neighbour, (i ? h2 : h1).neighbouringTiles())
  33. if(const CStack *s = cbc->battleGetStackByPos(neighbour))
  34. if(s->getCreature()->isShooting())
  35. shooters[i]++;
  36. return shooters[0] < shooters[1];
  37. }
  38. */
  39. struct ThreatMap
  40. {
  41. std::array<std::vector<BattleAttackInfo>, GameConstants::BFIELD_SIZE> threatMap; // [hexNr] -> enemies able to strike
  42. const CStack *endangered;
  43. std::array<int, GameConstants::BFIELD_SIZE> sufferedDamage;
  44. ThreatMap(const CStack *Endangered);
  45. };
  46. struct HypotheticChangesToBattleState
  47. {
  48. std::map<const CStack *, const IBonusBearer *> bonusesOfStacks;
  49. std::map<const CStack *, int> counterAttacksLeft;
  50. };
  51. struct AttackPossibility
  52. {
  53. const CStack *enemy; //redundant (to attack.defender) but looks nice
  54. BattleHex tile; //tile from which we attack
  55. BattleAttackInfo attack;
  56. int damageDealt;
  57. int damageReceived; //usually by counter-attack
  58. int tacticImpact;
  59. int damageDiff() const;
  60. int attackValue() const;
  61. static AttackPossibility evaluate(const BattleAttackInfo &AttackInfo, const HypotheticChangesToBattleState &state, BattleHex hex);
  62. };
  63. template<typename Key, typename Val, typename Val2>
  64. const Val &getValOr(const std::map<Key, Val> &Map, const Key &key, const Val2 &defaultValue)
  65. {
  66. auto i = Map.find(key);
  67. if(i != Map.end())
  68. return i->second;
  69. else
  70. return defaultValue;
  71. }
  72. struct PotentialTargets
  73. {
  74. std::vector<AttackPossibility> possibleAttacks;
  75. std::vector<const CStack *> unreachableEnemies;
  76. //std::function<AttackPossibility(bool,BattleHex)> GenerateAttackInfo; //args: shooting, destHex
  77. PotentialTargets(){};
  78. PotentialTargets(const CStack *attacker, const HypotheticChangesToBattleState &state = HypotheticChangesToBattleState());
  79. AttackPossibility bestAction() const;
  80. int bestActionValue() const;
  81. };
  82. class CBattleAI : public CBattleGameInterface
  83. {
  84. int side;
  85. shared_ptr<CBattleCallback> cb;
  86. //Previous setting of cb
  87. bool wasWaitingForRealize, wasUnlockingGs;
  88. void print(const std::string &text) const;
  89. public:
  90. CBattleAI(void);
  91. ~CBattleAI(void);
  92. void init(shared_ptr<CBattleCallback> CB) OVERRIDE;
  93. void actionFinished(const BattleAction &action) OVERRIDE;//occurs AFTER every action taken by any stack or by the hero
  94. void actionStarted(const BattleAction &action) OVERRIDE;//occurs BEFORE every action taken by any stack or by the hero
  95. BattleAction activeStack(const CStack * stack) OVERRIDE; //called when it's turn of that stack
  96. void battleAttack(const BattleAttack *ba) OVERRIDE; //called when stack is performing attack
  97. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) OVERRIDE; //called when stack receives damage (after battleAttack())
  98. void battleEnd(const BattleResult *br) OVERRIDE;
  99. //void battleResultsApplied() OVERRIDE; //called when all effects of last battle are applied
  100. void battleNewRoundFirst(int round) OVERRIDE; //called at the beginning of each turn before changes are applied;
  101. void battleNewRound(int round) OVERRIDE; //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  102. void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance) OVERRIDE;
  103. void battleSpellCast(const BattleSpellCast *sc) OVERRIDE;
  104. void battleStacksEffectsSet(const SetStackEffect & sse) OVERRIDE;//called when a specific effect is set to stacks
  105. //void battleTriggerEffect(const BattleTriggerEffect & bte) OVERRIDE;
  106. void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) OVERRIDE; //called by engine when battle starts; side=0 - left, side=1 - right
  107. void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom) OVERRIDE; //called when stacks are healed / resurrected first element of pair - stack id, second - healed hp
  108. void battleNewStackAppeared(const CStack * stack) OVERRIDE; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  109. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) OVERRIDE; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  110. void battleCatapultAttacked(const CatapultAttack & ca) OVERRIDE; //called when catapult makes an attack
  111. void battleStacksRemoved(const BattleStacksRemoved & bsr) OVERRIDE; //called when certain stack is completely removed from battlefield
  112. BattleAction goTowards(const CStack * stack, BattleHex hex );
  113. BattleAction useCatapult(const CStack * stack);
  114. boost::optional<BattleAction> considerFleeingOrSurrendering();
  115. void attemptCastingSpell();
  116. std::vector<BattleHex> getTargetsToConsider(const CSpell *spell) const;
  117. };