BattleExchangeVariant.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(): dpsScore(0) {}
  51. int64_t trackAttack(const AttackPossibility & ap, HypotheticBattle & state);
  52. int64_t trackAttack(
  53. std::shared_ptr<StackWithBonuses> attacker,
  54. std::shared_ptr<StackWithBonuses> defender,
  55. bool shooting,
  56. bool isOurAttack,
  57. const CBattleInfoCallback & cb,
  58. bool evaluateOnly = false);
  59. int64_t getScore() const { return dpsScore; }
  60. void adjustPositions(
  61. std::vector<const battle::Unit *> attackers,
  62. const AttackPossibility & ap,
  63. std::map<BattleHex, battle::Units> & reachabilityMap);
  64. private:
  65. int64_t dpsScore;
  66. std::map<uint32_t, AttackerValue> attackerValue;
  67. };
  68. class BattleExchangeEvaluator
  69. {
  70. private:
  71. std::shared_ptr<CBattleInfoCallback> cb;
  72. std::shared_ptr<Environment> env;
  73. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  74. std::vector<battle::Units> turnOrder;
  75. public:
  76. BattleExchangeEvaluator(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env): cb(cb), env(env) {}
  77. EvaluationResult findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
  78. int64_t calculateExchange(const AttackPossibility & ap, PotentialTargets & targets, HypotheticBattle & hb);
  79. void updateReachabilityMap(HypotheticBattle & hb);
  80. std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, HypotheticBattle & hb);
  81. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  82. MoveTarget findMoveTowardsUnreachable(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
  83. std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit);
  84. };