BattleExchangeVariant.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 isRetalitated;
  40. BattleHex position;
  41. AttackerValue();
  42. };
  43. struct MoveTarget
  44. {
  45. float score;
  46. float scorePerTurn;
  47. std::vector<BattleHex> positions;
  48. std::optional<AttackPossibility> cachedAttack;
  49. uint8_t turnsToRich;
  50. MoveTarget();
  51. };
  52. struct EvaluationResult
  53. {
  54. static const int64_t INEFFECTIVE_SCORE = -10000;
  55. AttackPossibility bestAttack;
  56. MoveTarget bestMove;
  57. bool wait;
  58. float score;
  59. bool defend;
  60. EvaluationResult(const AttackPossibility & ap)
  61. :wait(false), score(INEFFECTIVE_SCORE), bestAttack(ap), defend(false)
  62. {
  63. }
  64. };
  65. /// <summary>
  66. /// The class represents evaluation of attack value
  67. /// of exchanges between all stacks which can access particular hex
  68. /// starting from initial attack represented by AttackPossibility and further according turn order.
  69. /// Negative score value means we get more demage than deal
  70. /// </summary>
  71. class BattleExchangeVariant
  72. {
  73. public:
  74. BattleExchangeVariant()
  75. : dpsScore() {}
  76. float trackAttack(
  77. const AttackPossibility & ap,
  78. std::shared_ptr<HypotheticBattle> hb,
  79. DamageCache & damageCache);
  80. float trackAttack(
  81. std::shared_ptr<StackWithBonuses> attacker,
  82. std::shared_ptr<StackWithBonuses> defender,
  83. bool shooting,
  84. bool isOurAttack,
  85. DamageCache & damageCache,
  86. std::shared_ptr<HypotheticBattle> hb,
  87. bool evaluateOnly = false);
  88. const BattleScore & getScore() const { return dpsScore; }
  89. private:
  90. BattleScore dpsScore;
  91. std::map<uint32_t, AttackerValue> attackerValue;
  92. };
  93. struct ReachabilityData
  94. {
  95. std::vector<const battle::Unit *> units;
  96. // shooters which are within mellee attack and mellee units
  97. std::vector<const battle::Unit *> melleeAccessible;
  98. // far shooters
  99. std::vector<const battle::Unit *> shooters;
  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. float scoreValue(const BattleScore & score) const;
  111. BattleScore calculateExchange(
  112. const AttackPossibility & ap,
  113. uint8_t turn,
  114. PotentialTargets & targets,
  115. DamageCache & damageCache,
  116. std::shared_ptr<HypotheticBattle> hb);
  117. bool canBeHitThisTurn(const AttackPossibility & ap);
  118. public:
  119. BattleExchangeEvaluator(
  120. std::shared_ptr<CBattleInfoCallback> cb,
  121. std::shared_ptr<Environment> env,
  122. float strengthRatio): cb(cb), env(env) {
  123. negativeEffectMultiplier = strengthRatio >= 1 ? 1 : strengthRatio;
  124. }
  125. EvaluationResult findBestTarget(
  126. const battle::Unit * activeStack,
  127. PotentialTargets & targets,
  128. DamageCache & damageCache,
  129. std::shared_ptr<HypotheticBattle> hb);
  130. float evaluateExchange(
  131. const AttackPossibility & ap,
  132. uint8_t turn,
  133. PotentialTargets & targets,
  134. DamageCache & damageCache,
  135. std::shared_ptr<HypotheticBattle> hb);
  136. std::vector<const battle::Unit *> getOneTurnReachableUnits(uint8_t turn, BattleHex hex);
  137. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  138. ReachabilityData getExchangeUnits(
  139. const AttackPossibility & ap,
  140. uint8_t turn,
  141. PotentialTargets & targets,
  142. std::shared_ptr<HypotheticBattle> hb);
  143. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  144. MoveTarget findMoveTowardsUnreachable(
  145. const battle::Unit * activeStack,
  146. PotentialTargets & targets,
  147. DamageCache & damageCache,
  148. std::shared_ptr<HypotheticBattle> hb);
  149. std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit) const;
  150. float getPositiveEffectMultiplier() const { return 1; }
  151. float getNegativeEffectMultiplier() const { return negativeEffectMultiplier; }
  152. };