BattleExchangeVariant.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. std::vector<BattleHex> positions;
  47. std::optional<AttackPossibility> cachedAttack;
  48. uint8_t turnsToRich;
  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, std::vector<const battle::Unit *>> units;
  95. // shooters which are within mellee attack and mellee units
  96. std::vector<const battle::Unit *> melleeAccessible;
  97. // far shooters
  98. std::vector<const battle::Unit *> shooters;
  99. std::set<uint32_t> enemyUnitsReachingAttacker;
  100. };
  101. class BattleExchangeEvaluator
  102. {
  103. private:
  104. std::shared_ptr<CBattleInfoCallback> cb;
  105. std::shared_ptr<Environment> env;
  106. std::map<uint32_t, ReachabilityInfo> reachabilityCache;
  107. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  108. std::vector<battle::Units> turnOrder;
  109. float negativeEffectMultiplier;
  110. int simulationTurnsCount;
  111. float scoreValue(const BattleScore & score) const;
  112. BattleScore calculateExchange(
  113. const AttackPossibility & ap,
  114. uint8_t turn,
  115. PotentialTargets & targets,
  116. DamageCache & damageCache,
  117. std::shared_ptr<HypotheticBattle> hb,
  118. std::vector<const battle::Unit *> additionalUnits = {}) const;
  119. bool canBeHitThisTurn(const AttackPossibility & ap);
  120. public:
  121. BattleExchangeEvaluator(
  122. std::shared_ptr<CBattleInfoCallback> cb,
  123. std::shared_ptr<Environment> env,
  124. float strengthRatio,
  125. int simulationTurnsCount): cb(cb), env(env), simulationTurnsCount(simulationTurnsCount){
  126. negativeEffectMultiplier = strengthRatio >= 1 ? 1 : strengthRatio * strengthRatio;
  127. }
  128. EvaluationResult findBestTarget(
  129. const battle::Unit * activeStack,
  130. PotentialTargets & targets,
  131. DamageCache & damageCache,
  132. std::shared_ptr<HypotheticBattle> hb);
  133. float evaluateExchange(
  134. const AttackPossibility & ap,
  135. uint8_t turn,
  136. PotentialTargets & targets,
  137. DamageCache & damageCache,
  138. std::shared_ptr<HypotheticBattle> hb) const;
  139. std::vector<const battle::Unit *> getOneTurnReachableUnits(uint8_t turn, BattleHex hex) const;
  140. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  141. ReachabilityData getExchangeUnits(
  142. const AttackPossibility & ap,
  143. uint8_t turn,
  144. PotentialTargets & targets,
  145. std::shared_ptr<HypotheticBattle> hb,
  146. std::vector<const battle::Unit *> additionalUnits = {}) const;
  147. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  148. MoveTarget findMoveTowardsUnreachable(
  149. const battle::Unit * activeStack,
  150. PotentialTargets & targets,
  151. DamageCache & damageCache,
  152. std::shared_ptr<HypotheticBattle> hb);
  153. std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit) const;
  154. float getPositiveEffectMultiplier() const { return 1; }
  155. float getNegativeEffectMultiplier() const { return negativeEffectMultiplier; }
  156. };