BattleExchangeVariant.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 AttackerValue
  16. {
  17. float value;
  18. bool isRetalitated;
  19. BattleHex position;
  20. AttackerValue();
  21. };
  22. struct MoveTarget
  23. {
  24. float score;
  25. float scorePerTurn;
  26. std::vector<BattleHex> positions;
  27. std::optional<AttackPossibility> cachedAttack;
  28. uint8_t turnsToRich;
  29. MoveTarget();
  30. };
  31. struct EvaluationResult
  32. {
  33. static const int64_t INEFFECTIVE_SCORE = -10000;
  34. AttackPossibility bestAttack;
  35. MoveTarget bestMove;
  36. bool wait;
  37. float score;
  38. bool defend;
  39. EvaluationResult(const AttackPossibility & ap)
  40. :wait(false), score(INEFFECTIVE_SCORE), bestAttack(ap), defend(false)
  41. {
  42. }
  43. };
  44. /// <summary>
  45. /// The class represents evaluation of attack value
  46. /// of exchanges between all stacks which can access particular hex
  47. /// starting from initial attack represented by AttackPossibility and further according turn order.
  48. /// Negative score value means we get more demage than deal
  49. /// </summary>
  50. class BattleExchangeVariant
  51. {
  52. public:
  53. BattleExchangeVariant(float positiveEffectMultiplier, float negativeEffectMultiplier)
  54. : dpsScore(0), positiveEffectMultiplier(positiveEffectMultiplier), negativeEffectMultiplier(negativeEffectMultiplier) {}
  55. float trackAttack(
  56. const AttackPossibility & ap,
  57. std::shared_ptr<HypotheticBattle> hb,
  58. DamageCache & damageCache);
  59. float trackAttack(
  60. std::shared_ptr<StackWithBonuses> attacker,
  61. std::shared_ptr<StackWithBonuses> defender,
  62. bool shooting,
  63. bool isOurAttack,
  64. DamageCache & damageCache,
  65. std::shared_ptr<HypotheticBattle> hb,
  66. bool evaluateOnly = false);
  67. float getScore() const { return dpsScore; }
  68. void adjustPositions(
  69. std::vector<const battle::Unit *> attackers,
  70. const AttackPossibility & ap,
  71. std::map<BattleHex, battle::Units> & reachabilityMap);
  72. private:
  73. float positiveEffectMultiplier;
  74. float negativeEffectMultiplier;
  75. float dpsScore;
  76. std::map<uint32_t, AttackerValue> attackerValue;
  77. };
  78. class BattleExchangeEvaluator
  79. {
  80. private:
  81. std::shared_ptr<CBattleInfoCallback> cb;
  82. std::shared_ptr<Environment> env;
  83. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  84. std::vector<battle::Units> turnOrder;
  85. float negativeEffectMultiplier;
  86. public:
  87. BattleExchangeEvaluator(
  88. std::shared_ptr<CBattleInfoCallback> cb,
  89. std::shared_ptr<Environment> env,
  90. float strengthRatio): cb(cb), env(env) {
  91. negativeEffectMultiplier = strengthRatio;
  92. }
  93. EvaluationResult findBestTarget(
  94. const battle::Unit * activeStack,
  95. PotentialTargets & targets,
  96. DamageCache & damageCache,
  97. std::shared_ptr<HypotheticBattle> hb);
  98. float calculateExchange(
  99. const AttackPossibility & ap,
  100. PotentialTargets & targets,
  101. DamageCache & damageCache,
  102. std::shared_ptr<HypotheticBattle> hb);
  103. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  104. std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, std::shared_ptr<HypotheticBattle> hb);
  105. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  106. MoveTarget findMoveTowardsUnreachable(
  107. const battle::Unit * activeStack,
  108. PotentialTargets & targets,
  109. DamageCache & damageCache,
  110. std::shared_ptr<HypotheticBattle> hb);
  111. std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit);
  112. float getPositiveEffectMultiplier() { return 1; }
  113. float getNegativeEffectMultiplier() { return negativeEffectMultiplier; }
  114. };