BattleExchangeVariant.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * BattleExchangeVariant.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 "../../lib/AI_Base.h"
  12. #include "../../lib/battle/ReachabilityInfo.h"
  13. #include "PotentialTargets.h"
  14. #include "StackWithBonuses.h"
  15. struct BattleScore
  16. {
  17. float ourDamageReduce;
  18. float enemyDamageReduce;
  19. BattleScore(float enemyDamageReduce, float ourDamageReduce)
  20. :enemyDamageReduce(enemyDamageReduce), ourDamageReduce(ourDamageReduce)
  21. {
  22. }
  23. BattleScore() : BattleScore(0, 0) {}
  24. float value()
  25. {
  26. return enemyDamageReduce - ourDamageReduce;
  27. }
  28. BattleScore operator+(BattleScore & other)
  29. {
  30. BattleScore result = *this;
  31. result.ourDamageReduce += other.ourDamageReduce;
  32. result.enemyDamageReduce += other.enemyDamageReduce;
  33. return result;
  34. }
  35. };
  36. struct AttackerValue
  37. {
  38. float value;
  39. bool isRetaliated;
  40. BattleHex position;
  41. AttackerValue();
  42. };
  43. struct MoveTarget
  44. {
  45. float score;
  46. BattleHexArray positions;
  47. std::optional<AttackPossibility> cachedAttack;
  48. uint8_t turnsToReach;
  49. MoveTarget();
  50. };
  51. struct EvaluationResult
  52. {
  53. static const int64_t INEFFECTIVE_SCORE = -100000000;
  54. AttackPossibility bestAttack;
  55. MoveTarget bestMove;
  56. bool wait;
  57. float score;
  58. bool defend;
  59. EvaluationResult(const AttackPossibility & ap)
  60. :wait(false), score(INEFFECTIVE_SCORE), bestAttack(ap), defend(false)
  61. {
  62. }
  63. };
  64. /// <summary>
  65. /// The class represents evaluation of attack value
  66. /// of exchanges between all stacks which can access particular hex
  67. /// starting from initial attack represented by AttackPossibility and further according turn order.
  68. /// Negative score value means we get more demage than deal
  69. /// </summary>
  70. class BattleExchangeVariant
  71. {
  72. public:
  73. BattleExchangeVariant()
  74. : dpsScore() {}
  75. float trackAttack(
  76. const AttackPossibility & ap,
  77. std::shared_ptr<HypotheticBattle> hb,
  78. DamageCache & damageCache);
  79. float trackAttack(
  80. std::shared_ptr<StackWithBonuses> attacker,
  81. std::shared_ptr<StackWithBonuses> defender,
  82. bool shooting,
  83. bool isOurAttack,
  84. DamageCache & damageCache,
  85. std::shared_ptr<HypotheticBattle> hb,
  86. bool evaluateOnly = false);
  87. const BattleScore & getScore() const { return dpsScore; }
  88. private:
  89. BattleScore dpsScore;
  90. std::map<uint32_t, AttackerValue> attackerValue;
  91. };
  92. struct ReachabilityData
  93. {
  94. std::map<int, battle::Units> units;
  95. // shooters which are within mellee attack and mellee units
  96. battle::Units melleeAccessible;
  97. // far shooters
  98. battle::Units shooters;
  99. std::set<uint32_t> enemyUnitsReachingAttacker;
  100. };
  101. class ReachabilityMapCache
  102. {
  103. struct PerTurnData{
  104. std::bitset<GameConstants::BFIELD_SIZE> isValid;
  105. std::array<battle::Units, GameConstants::BFIELD_SIZE> hexes;
  106. };
  107. std::map<uint32_t, ReachabilityInfo> unitReachabilityMap; // unit ID -> reachability
  108. std::map<uint32_t, PerTurnData> hexReachabilityPerTurn;
  109. //const ReachabilityInfo & update();
  110. battle::Units computeOneTurnReachableUnits(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env, const std::vector<battle::Units> & turnOrder, uint8_t turn, BattleHex hex);
  111. public:
  112. const battle::Units & getOneTurnReachableUnits(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env, const std::vector<battle::Units> & turnOrder, uint8_t turn, BattleHex hex);
  113. void update(const std::vector<battle::Units> & turnOrder, std::shared_ptr<HypotheticBattle> hb);
  114. };
  115. class BattleExchangeEvaluator
  116. {
  117. private:
  118. std::shared_ptr<CBattleInfoCallback> cb;
  119. std::shared_ptr<Environment> env;
  120. mutable ReachabilityMapCache reachabilityMap;
  121. std::vector<battle::Units> turnOrder;
  122. float negativeEffectMultiplier;
  123. int simulationTurnsCount;
  124. float scoreValue(const BattleScore & score) const;
  125. BattleScore calculateExchange(
  126. const AttackPossibility & ap,
  127. uint8_t turn,
  128. PotentialTargets & targets,
  129. DamageCache & damageCache,
  130. std::shared_ptr<HypotheticBattle> hb,
  131. battle::Units additionalUnits = {}) const;
  132. bool canBeHitThisTurn(const AttackPossibility & ap);
  133. public:
  134. BattleExchangeEvaluator(
  135. std::shared_ptr<CBattleInfoCallback> cb,
  136. std::shared_ptr<Environment> env,
  137. float strengthRatio,
  138. int simulationTurnsCount): cb(cb), env(env), simulationTurnsCount(simulationTurnsCount){
  139. negativeEffectMultiplier = strengthRatio >= 1 ? 1 : strengthRatio * strengthRatio;
  140. }
  141. EvaluationResult findBestTarget(
  142. const battle::Unit * activeStack,
  143. PotentialTargets & targets,
  144. DamageCache & damageCache,
  145. std::shared_ptr<HypotheticBattle> hb,
  146. bool siegeDefense = false);
  147. float evaluateExchange(
  148. const AttackPossibility & ap,
  149. uint8_t turn,
  150. PotentialTargets & targets,
  151. DamageCache & damageCache,
  152. std::shared_ptr<HypotheticBattle> hb) const;
  153. const battle::Units & getOneTurnReachableUnits(uint8_t turn, BattleHex hex) const;
  154. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  155. ReachabilityData getExchangeUnits(
  156. const AttackPossibility & ap,
  157. uint8_t turn,
  158. PotentialTargets & targets,
  159. std::shared_ptr<HypotheticBattle> hb,
  160. battle::Units additionalUnits = {}) const;
  161. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  162. MoveTarget findMoveTowardsUnreachable(
  163. const battle::Unit * activeStack,
  164. PotentialTargets & targets,
  165. DamageCache & damageCache,
  166. std::shared_ptr<HypotheticBattle> hb);
  167. battle::Units getAdjacentUnits(const battle::Unit * unit) const;
  168. float getPositiveEffectMultiplier() const { return 1; }
  169. float getNegativeEffectMultiplier() const { return negativeEffectMultiplier; }
  170. };