2
0

BattleExchangeVariant.h 5.5 KB

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