BattleExchangeVariant.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. battle::Units computeOneTurnReachableUnits(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env, const std::vector<battle::Units> & turnOrder, uint8_t turn, const BattleHex & hex);
  110. public:
  111. const battle::Units & getOneTurnReachableUnits(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env, const std::vector<battle::Units> & turnOrder, uint8_t turn, const BattleHex & hex);
  112. void update(const std::vector<battle::Units> & turnOrder, std::shared_ptr<HypotheticBattle> hb);
  113. };
  114. class BattleExchangeEvaluator
  115. {
  116. private:
  117. std::shared_ptr<CBattleInfoCallback> cb;
  118. std::shared_ptr<Environment> env;
  119. mutable ReachabilityMapCache reachabilityMap;
  120. std::vector<battle::Units> turnOrder;
  121. float negativeEffectMultiplier;
  122. int simulationTurnsCount;
  123. float scoreValue(const BattleScore & score) const;
  124. BattleScore calculateExchange(
  125. const AttackPossibility & ap,
  126. uint8_t turn,
  127. PotentialTargets & targets,
  128. DamageCache & damageCache,
  129. std::shared_ptr<HypotheticBattle> hb,
  130. const battle::Units & additionalUnits = {}) const;
  131. bool canBeHitThisTurn(const AttackPossibility & ap);
  132. public:
  133. BattleExchangeEvaluator(
  134. std::shared_ptr<CBattleInfoCallback> cb,
  135. std::shared_ptr<Environment> env,
  136. float strengthRatio,
  137. int simulationTurnsCount): cb(cb), env(env), simulationTurnsCount(simulationTurnsCount){
  138. negativeEffectMultiplier = strengthRatio >= 1 ? 1 : strengthRatio * strengthRatio;
  139. }
  140. EvaluationResult findBestTarget(
  141. const battle::Unit * activeStack,
  142. PotentialTargets & targets,
  143. DamageCache & damageCache,
  144. std::shared_ptr<HypotheticBattle> hb,
  145. bool siegeDefense = false);
  146. float evaluateExchange(
  147. const AttackPossibility & ap,
  148. uint8_t turn,
  149. PotentialTargets & targets,
  150. DamageCache & damageCache,
  151. std::shared_ptr<HypotheticBattle> hb) const;
  152. const battle::Units & getOneTurnReachableUnits(uint8_t turn, const BattleHex & hex) const;
  153. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  154. ReachabilityData getExchangeUnits(
  155. const AttackPossibility & ap,
  156. uint8_t turn,
  157. PotentialTargets & targets,
  158. std::shared_ptr<HypotheticBattle> hb,
  159. const battle::Units & additionalUnits = {}) const;
  160. bool checkPositionBlocksOurStacks(const HypotheticBattle & hb, const battle::Unit * unit, const BattleHex & position);
  161. MoveTarget findMoveTowardsUnreachable(
  162. const battle::Unit * activeStack,
  163. PotentialTargets & targets,
  164. DamageCache & damageCache,
  165. std::shared_ptr<HypotheticBattle> hb);
  166. battle::Units getAdjacentUnits(const battle::Unit * unit) const;
  167. float getPositiveEffectMultiplier() const { return 1; }
  168. float getNegativeEffectMultiplier() const { return negativeEffectMultiplier; }
  169. };