BattleExchangeVariant.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. value = 0;
  23. isRetalitated = false;
  24. }
  25. };
  26. class BattleExchangeVariant
  27. {
  28. public:
  29. BattleExchangeVariant()
  30. :dpsScore(0), attackerValue()
  31. {
  32. }
  33. int64_t trackAttack(const AttackPossibility & ap, HypotheticBattle * state);
  34. int64_t trackAttack(
  35. std::shared_ptr<StackWithBonuses> attacker,
  36. std::shared_ptr<StackWithBonuses> defender,
  37. bool shooting,
  38. bool isOurAttack,
  39. std::shared_ptr<CBattleInfoCallback> cb,
  40. bool evaluateOnly = false);
  41. int64_t getScore() const { return dpsScore; }
  42. void adjustPositions(
  43. std::vector<const battle::Unit *> attackers,
  44. const AttackPossibility & ap,
  45. std::map<BattleHex, battle::Units> & reachabilityMap);
  46. private:
  47. int64_t dpsScore;
  48. std::map<uint32_t, AttackerValue> attackerValue;
  49. int64_t calculateDpsReduce(
  50. const battle::Unit * attacker,
  51. const battle::Unit * defender,
  52. uint64_t damageDealt,
  53. std::shared_ptr<CBattleInfoCallback> cb) const;
  54. };
  55. struct EvaluationResult
  56. {
  57. static const int64_t INEFFECTIVE_SCORE = -1000000;
  58. AttackPossibility bestAttack;
  59. bool wait;
  60. int64_t score;
  61. bool defend;
  62. EvaluationResult(AttackPossibility & ap)
  63. :wait(false), score(0), bestAttack(ap), defend(false)
  64. {
  65. }
  66. };
  67. class BattleExchangeEvaluator
  68. {
  69. private:
  70. std::shared_ptr<CBattleInfoCallback> cb;
  71. std::shared_ptr<Environment> env;
  72. std::map<BattleHex, std::vector<const battle::Unit *>> reachabilityMap;
  73. std::vector<battle::Units> turnOrder;
  74. public:
  75. BattleExchangeEvaluator(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env)
  76. :cb(cb), reachabilityMap(), env(env), turnOrder()
  77. {
  78. }
  79. EvaluationResult findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
  80. int64_t calculateExchange(const AttackPossibility & ap);
  81. void updateReachabilityMap(HypotheticBattle & hb);
  82. std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap);
  83. bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
  84. };