BattleExchangeVariant.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. int64_t value;
  18. bool isRetalitated;
  19. BattleHex position;
  20. AttackerValue();
  21. };
  22. struct MoveTarget
  23. {
  24. int64_t score;
  25. int64_t 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 = -1000000;
  34. AttackPossibility bestAttack;
  35. MoveTarget bestMove;
  36. bool wait;
  37. int64_t 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. int64_t trackAttack(const AttackPossibility & ap, HypotheticBattle & state);
  56. int64_t trackAttack(
  57. std::shared_ptr<StackWithBonuses> attacker,
  58. std::shared_ptr<StackWithBonuses> defender,
  59. bool shooting,
  60. bool isOurAttack,
  61. DamageCache & damageCache,
  62. std::shared_ptr<HypotheticBattle> hb,
  63. bool evaluateOnly = false);
  64. int64_t getScore() const { return dpsScore; }
  65. void adjustPositions(
  66. std::vector<const battle::Unit *> attackers,
  67. const AttackPossibility & ap,
  68. std::map<BattleHex, battle::Units> & reachabilityMap);
  69. private:
  70. float positiveEffectMultiplier;
  71. float negativeEffectMultiplier;
  72. int64_t dpsScore;
  73. std::map<uint32_t, AttackerValue> attackerValue;
  74. };
  75. class BattleExchangeEvaluator
  76. {
  77. private:
  78. std::shared_ptr<CBattleInfoCallback> cb;
  79. std::shared_ptr<Environment> env;
  80. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  81. std::vector<battle::Units> turnOrder;
  82. float negativeEffectMultiplier;
  83. public:
  84. BattleExchangeEvaluator(
  85. std::shared_ptr<CBattleInfoCallback> cb,
  86. std::shared_ptr<Environment> env,
  87. float strengthRatio): cb(cb), env(env) {
  88. negativeEffectMultiplier = strengthRatio;
  89. }
  90. EvaluationResult findBestTarget(
  91. const battle::Unit * activeStack,
  92. PotentialTargets & targets,
  93. DamageCache & damageCache,
  94. std::shared_ptr<HypotheticBattle> hb);
  95. int64_t calculateExchange(
  96. const AttackPossibility & ap,
  97. PotentialTargets & targets,
  98. DamageCache & damageCache,
  99. std::shared_ptr<HypotheticBattle> hb);
  100. void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
  101. std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, std::shared_ptr<HypotheticBattle> hb);
  102. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  103. MoveTarget findMoveTowardsUnreachable(
  104. const battle::Unit * activeStack,
  105. PotentialTargets & targets,
  106. DamageCache & damageCache,
  107. std::shared_ptr<HypotheticBattle> hb);
  108. std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit);
  109. float getPositiveEffectMultiplier() { return 1; }
  110. float getNegativeEffectMultiplier() { return negativeEffectMultiplier; }
  111. };