PotentialTargets.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * PotentialTargets.cpp, 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. #include "StdInc.h"
  11. #include "PotentialTargets.h"
  12. #include "../../lib/CStack.h"//todo: remove
  13. #include "../../lib/mapObjects/CGTownInstance.h"
  14. PotentialTargets::PotentialTargets(
  15. const battle::Unit * attacker,
  16. DamageCache & damageCache,
  17. std::shared_ptr<HypotheticBattle> state)
  18. {
  19. auto attackerInfo = state->battleGetUnitByID(attacker->unitId());
  20. auto reachability = state->getReachability(attackerInfo);
  21. auto avHexes = state->battleGetAvailableHexes(reachability, attackerInfo, false);
  22. //FIXME: this should part of battleGetAvailableHexes
  23. bool forceTarget = false;
  24. const battle::Unit * forcedTarget = nullptr;
  25. BattleHex forcedHex;
  26. if(attackerInfo->hasBonusOfType(BonusType::ATTACKS_NEAREST_CREATURE))
  27. {
  28. forceTarget = true;
  29. auto nearest = state->getNearestStack(attackerInfo);
  30. if(nearest.first != nullptr)
  31. {
  32. forcedTarget = nearest.first;
  33. forcedHex = nearest.second;
  34. }
  35. }
  36. auto aliveUnits = state->battleGetUnitsIf([=](const battle::Unit * unit)
  37. {
  38. return unit->isValidTarget() && unit->unitId() != attackerInfo->unitId();
  39. });
  40. for(auto defender : aliveUnits)
  41. {
  42. if(!forceTarget && !state->battleMatchOwner(attackerInfo, defender))
  43. continue;
  44. auto GenerateAttackInfo = [&](bool shooting, BattleHex hex) -> AttackPossibility
  45. {
  46. int distance = hex.isValid() ? reachability.distances[hex.toInt()] : 0;
  47. auto bai = BattleAttackInfo(attackerInfo, defender, distance, shooting);
  48. return AttackPossibility::evaluate(bai, hex, damageCache, state);
  49. };
  50. if(forceTarget)
  51. {
  52. if(forcedTarget && defender->unitId() == forcedTarget->unitId())
  53. possibleAttacks.push_back(GenerateAttackInfo(false, forcedHex));
  54. else
  55. unreachableEnemies.push_back(defender);
  56. }
  57. else if(state->battleCanShoot(attackerInfo, defender->getPosition()))
  58. {
  59. possibleAttacks.push_back(GenerateAttackInfo(true, BattleHex::INVALID));
  60. }
  61. else
  62. {
  63. for(BattleHex hex : avHexes)
  64. {
  65. if(!CStack::isMeleeAttackPossible(attackerInfo, defender, hex))
  66. continue;
  67. auto bai = GenerateAttackInfo(false, hex);
  68. if(!bai.affectedUnits.empty())
  69. possibleAttacks.push_back(bai);
  70. }
  71. if(!vstd::contains_if(possibleAttacks, [=](const AttackPossibility & pa) { return pa.attack.defender->unitId() == defender->unitId(); }))
  72. unreachableEnemies.push_back(defender);
  73. }
  74. }
  75. boost::sort(possibleAttacks, [](const AttackPossibility & lhs, const AttackPossibility & rhs) -> bool
  76. {
  77. return lhs.damageDiff() > rhs.damageDiff();
  78. });
  79. }
  80. int64_t PotentialTargets::bestActionValue() const
  81. {
  82. if(possibleAttacks.empty())
  83. return 0;
  84. return bestAction().attackValue();
  85. }
  86. const AttackPossibility & PotentialTargets::bestAction() const
  87. {
  88. if(possibleAttacks.empty())
  89. throw std::runtime_error("No best action, since we don't have any actions");
  90. return possibleAttacks.front();
  91. }