BattleExchangeVariant.h 4.7 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 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. void adjustPositions(
  90. std::vector<const battle::Unit *> attackers,
  91. const AttackPossibility & ap,
  92. std::map<BattleHex, battle::Units> & reachabilityMap);
  93. private:
  94. BattleScore dpsScore;
  95. std::map<uint32_t, AttackerValue> attackerValue;
  96. };
  97. struct ReachabilityData
  98. {
  99. std::vector<const battle::Unit *> units;
  100. // shooters which are within mellee attack and mellee units
  101. std::vector<const battle::Unit *> melleeAccessible;
  102. // far shooters
  103. std::vector<const battle::Unit *> shooters;
  104. };
  105. class BattleExchangeEvaluator
  106. {
  107. private:
  108. std::shared_ptr<CBattleInfoCallback> cb;
  109. std::shared_ptr<Environment> env;
  110. std::map<uint32_t, ReachabilityInfo> reachabilityCache;
  111. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  112. std::vector<battle::Units> turnOrder;
  113. float negativeEffectMultiplier;
  114. float scoreValue(const BattleScore & score) const;
  115. BattleScore calculateExchange(
  116. const AttackPossibility & ap,
  117. uint8_t turn,
  118. PotentialTargets & targets,
  119. DamageCache & damageCache,
  120. std::shared_ptr<HypotheticBattle> hb);
  121. bool canBeHitThisTurn(const AttackPossibility & ap);
  122. public:
  123. BattleExchangeEvaluator(
  124. std::shared_ptr<CBattleInfoCallback> cb,
  125. std::shared_ptr<Environment> env,
  126. float strengthRatio): cb(cb), env(env) {
  127. negativeEffectMultiplier = strengthRatio >= 1 ? 1 : strengthRatio;
  128. }
  129. EvaluationResult findBestTarget(
  130. const battle::Unit * activeStack,
  131. PotentialTargets & targets,
  132. DamageCache & damageCache,
  133. std::shared_ptr<HypotheticBattle> hb);
  134. float evaluateExchange(
  135. const AttackPossibility & ap,
  136. uint8_t turn,
  137. PotentialTargets & targets,
  138. DamageCache & damageCache,
  139. std::shared_ptr<HypotheticBattle> hb);
  140. std::vector<const battle::Unit *> getOneTurnReachableUnits(uint8_t turn, BattleHex hex);
  141. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  142. ReachabilityData getExchangeUnits(
  143. const AttackPossibility & ap,
  144. uint8_t turn,
  145. PotentialTargets & targets,
  146. std::shared_ptr<HypotheticBattle> hb);
  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. };