BattleExchangeVariant.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. std::vector<BattleHex> positions;
  26. MoveTarget();
  27. };
  28. struct EvaluationResult
  29. {
  30. static const int64_t INEFFECTIVE_SCORE = -1000000;
  31. AttackPossibility bestAttack;
  32. MoveTarget bestMove;
  33. bool wait;
  34. int64_t score;
  35. bool defend;
  36. EvaluationResult(const AttackPossibility & ap)
  37. :wait(false), score(INEFFECTIVE_SCORE), bestAttack(ap), defend(false)
  38. {
  39. }
  40. };
  41. /// <summary>
  42. /// The class represents evaluation of attack value
  43. /// of exchanges between all stacks which can access particular hex
  44. /// starting from initial attack represented by AttackPossibility and further according turn order.
  45. /// Negative score value means we get more demage than deal
  46. /// </summary>
  47. class BattleExchangeVariant
  48. {
  49. public:
  50. BattleExchangeVariant()
  51. :dpsScore(0), attackerValue()
  52. {
  53. }
  54. int64_t trackAttack(const AttackPossibility & ap, HypotheticBattle & state);
  55. int64_t trackAttack(
  56. std::shared_ptr<StackWithBonuses> attacker,
  57. std::shared_ptr<StackWithBonuses> defender,
  58. bool shooting,
  59. bool isOurAttack,
  60. const CBattleInfoCallback & cb,
  61. bool evaluateOnly = false);
  62. int64_t getScore() const { return dpsScore; }
  63. void adjustPositions(
  64. std::vector<const battle::Unit *> attackers,
  65. const AttackPossibility & ap,
  66. std::map<BattleHex, battle::Units> & reachabilityMap);
  67. private:
  68. int64_t dpsScore;
  69. std::map<uint32_t, AttackerValue> attackerValue;
  70. };
  71. class BattleExchangeEvaluator
  72. {
  73. private:
  74. std::shared_ptr<CBattleInfoCallback> cb;
  75. std::shared_ptr<Environment> env;
  76. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  77. std::vector<battle::Units> turnOrder;
  78. public:
  79. BattleExchangeEvaluator(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env)
  80. :cb(cb), reachabilityMap(), env(env), turnOrder()
  81. {
  82. }
  83. EvaluationResult findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
  84. int64_t calculateExchange(const AttackPossibility & ap, PotentialTargets & targets, HypotheticBattle & hb);
  85. void updateReachabilityMap(HypotheticBattle & hb);
  86. std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, HypotheticBattle & hb);
  87. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  88. MoveTarget findMoveTowardsUnreachable(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
  89. std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit);
  90. };