BattleAI.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. std::map<const CStack *, int> stackCount;
  51. };
  52. struct AttackPossibility
  53. {
  54. const CStack *enemy; //redundant (to attack.defender) but looks nice
  55. BattleHex tile; //tile from which we attack
  56. BattleAttackInfo attack;
  57. int damageDealt;
  58. int damageReceived; //usually by counter-attack
  59. //int tacticImpact;
  60. int damageDiff() const;
  61. int attackValue() const;
  62. static AttackPossibility evaluate(const BattleAttackInfo &AttackInfo, const HypotheticChangesToBattleState &state, BattleHex hex);
  63. };
  64. template<typename Key, typename Val, typename Val2>
  65. const Val getValOr(const std::map<Key, Val> &Map, const Key &key, const Val2 defaultValue)
  66. {
  67. //returning references here won't work: defaultValue must be converted into Val, creating temporary
  68. auto i = Map.find(key);
  69. if(i != Map.end())
  70. return i->second;
  71. else
  72. return defaultValue;
  73. }
  74. struct PotentialTargets
  75. {
  76. std::vector<AttackPossibility> possibleAttacks;
  77. std::vector<const CStack *> unreachableEnemies;
  78. //std::function<AttackPossibility(bool,BattleHex)> GenerateAttackInfo; //args: shooting, destHex
  79. PotentialTargets(){};
  80. PotentialTargets(const CStack *attacker, const HypotheticChangesToBattleState &state = HypotheticChangesToBattleState());
  81. AttackPossibility bestAction() const;
  82. int bestActionValue() const;
  83. };
  84. struct TacticInfo
  85. {
  86. double ourPotential;
  87. double enemyPotential;
  88. double ourHealth;
  89. double enemyhealth;
  90. std::map<const CStack*, PotentialTargets> targets;
  91. TacticInfo(const HypotheticChangesToBattleState &state = HypotheticChangesToBattleState());
  92. double totalValue() const;
  93. int expectedLength() const;
  94. };
  95. class CBattleAI : public CBattleGameInterface
  96. {
  97. int side;
  98. shared_ptr<CBattleCallback> cb;
  99. //Previous setting of cb
  100. bool wasWaitingForRealize, wasUnlockingGs;
  101. unique_ptr<TacticInfo> tacticInfo;
  102. void print(const std::string &text) const;
  103. public:
  104. Priorities priorities;
  105. CBattleAI(void);
  106. ~CBattleAI(void);
  107. void init(shared_ptr<CBattleCallback> CB) OVERRIDE;
  108. void actionFinished(const BattleAction &action) OVERRIDE;//occurs AFTER every action taken by any stack or by the hero
  109. void actionStarted(const BattleAction &action) OVERRIDE;//occurs BEFORE every action taken by any stack or by the hero
  110. BattleAction activeStack(const CStack * stack) OVERRIDE; //called when it's turn of that stack
  111. void setPriorities(const Priorities &priorities) OVERRIDE;
  112. void battleAttack(const BattleAttack *ba) OVERRIDE; //called when stack is performing attack
  113. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) OVERRIDE; //called when stack receives damage (after battleAttack())
  114. void battleEnd(const BattleResult *br) OVERRIDE;
  115. //void battleResultsApplied() OVERRIDE; //called when all effects of last battle are applied
  116. void battleNewRoundFirst(int round) OVERRIDE; //called at the beginning of each turn before changes are applied;
  117. 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
  118. void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance) OVERRIDE;
  119. void battleSpellCast(const BattleSpellCast *sc) OVERRIDE;
  120. void battleStacksEffectsSet(const SetStackEffect & sse) OVERRIDE;//called when a specific effect is set to stacks
  121. //void battleTriggerEffect(const BattleTriggerEffect & bte) OVERRIDE;
  122. 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
  123. 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
  124. void battleNewStackAppeared(const CStack * stack) OVERRIDE; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  125. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) OVERRIDE; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  126. void battleCatapultAttacked(const CatapultAttack & ca) OVERRIDE; //called when catapult makes an attack
  127. void battleStacksRemoved(const BattleStacksRemoved & bsr) OVERRIDE; //called when certain stack is completely removed from battlefield
  128. BattleAction goTowards(const CStack * stack, BattleHex hex );
  129. BattleAction useCatapult(const CStack * stack);
  130. boost::optional<BattleAction> considerFleeingOrSurrendering();
  131. void attemptCastingSpell();
  132. std::vector<BattleHex> getTargetsToConsider(const CSpell *spell) const;
  133. };