AttackPossibility.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * AttackPossibility.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 "AttackPossibility.h"
  12. #include "../../lib/CStack.h" // TODO: remove
  13. // Eventually only IBattleInfoCallback and battle::Unit should be used,
  14. // CUnitState should be private and CStack should be removed completely
  15. AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
  16. : from(from), dest(dest), attack(attack)
  17. {
  18. }
  19. int64_t AttackPossibility::damageDiff() const
  20. {
  21. return damageDealt - damageReceived - collateralDamage + shootersBlockedDmg;
  22. }
  23. int64_t AttackPossibility::attackValue() const
  24. {
  25. return damageDiff();
  26. }
  27. int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
  28. {
  29. int64_t res = 0;
  30. if(attackInfo.shooting)
  31. return 0;
  32. auto attacker = attackInfo.attacker;
  33. auto hexes = attacker->getSurroundingHexes(hex);
  34. for(BattleHex tile : hexes)
  35. {
  36. auto st = state->battleGetUnitByPos(tile, true);
  37. if(!st || !state->battleMatchOwner(st, attacker))
  38. continue;
  39. if(!state->battleCanShoot(st))
  40. continue;
  41. BattleAttackInfo rangeAttackInfo(st, attacker, true);
  42. rangeAttackInfo.defenderPos = hex;
  43. BattleAttackInfo meleeAttackInfo(st, attacker, false);
  44. meleeAttackInfo.defenderPos = hex;
  45. auto rangeDmg = getCbc()->battleEstimateDamage(rangeAttackInfo);
  46. auto meleeDmg = getCbc()->battleEstimateDamage(meleeAttackInfo);
  47. int64_t gain = (rangeDmg.first + rangeDmg.second - meleeDmg.first - meleeDmg.second) / 2 + 1;
  48. res += gain;
  49. }
  50. return res;
  51. }
  52. AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
  53. {
  54. auto attacker = attackInfo.attacker;
  55. auto defender = attackInfo.defender;
  56. const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
  57. static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);
  58. const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
  59. const bool mindControlled = [&](const battle::Unit *attacker) -> bool
  60. {
  61. auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker));
  62. if (actualSide && actualSide.get() != attacker->unitSide())
  63. return true;
  64. return false;
  65. } (attacker);
  66. AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
  67. std::vector<BattleHex> defenderHex;
  68. if(attackInfo.shooting)
  69. defenderHex = defender->getHexes();
  70. else
  71. defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
  72. for(BattleHex defHex : defenderHex)
  73. {
  74. if(defHex == hex) {
  75. // should be impossible but check anyway
  76. continue;
  77. }
  78. AttackPossibility ap(hex, defHex, attackInfo);
  79. ap.attackerState = attacker->acquireState();
  80. ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
  81. const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
  82. if (!attackInfo.shooting)
  83. ap.attackerState->setPosition(hex);
  84. std::vector<const battle::Unit*> units;
  85. if (attackInfo.shooting)
  86. units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
  87. else
  88. units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
  89. // ensure the defender is also affected
  90. bool addDefender = true;
  91. for(auto unit : units) {
  92. if (unit->unitId() == defender->unitId()) {
  93. addDefender = false;
  94. break;
  95. }
  96. }
  97. if(addDefender) {
  98. units.push_back(defender);
  99. }
  100. for(auto u : units) {
  101. if(!ap.attackerState->alive()) {
  102. break;
  103. }
  104. assert(u->unitId() != attacker->unitId());
  105. auto defenderState = u->acquireState();
  106. ap.affectedUnits.push_back(defenderState);
  107. for(int i = 0; i < totalAttacks; i++) {
  108. si64 damageDealt, damageReceived;
  109. TDmgRange retaliation(0, 0);
  110. auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
  111. vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
  112. vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
  113. vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
  114. vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
  115. damageDealt = (attackDmg.first + attackDmg.second) / 2;
  116. ap.attackerState->afterAttack(attackInfo.shooting, false);
  117. //FIXME: use ranged retaliation
  118. damageReceived = 0;
  119. if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
  120. {
  121. damageReceived = (retaliation.first + retaliation.second) / 2;
  122. defenderState->afterAttack(attackInfo.shooting, true);
  123. }
  124. bool isEnemy = state->battleMatchOwner(attacker, u) && !mindControlled;
  125. if(isEnemy)
  126. ap.damageDealt += damageDealt;
  127. else // friendly fire
  128. ap.collateralDamage += damageDealt;
  129. if(u->unitId() == defender->unitId() ||
  130. (!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
  131. {
  132. //FIXME: handle RANGED_RETALIATION ?
  133. ap.damageReceived += damageReceived;
  134. }
  135. ap.attackerState->damage(damageReceived);
  136. defenderState->damage(damageDealt);
  137. if (!ap.attackerState->alive() || !defenderState->alive())
  138. break;
  139. }
  140. }
  141. if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue()) {
  142. bestAp = ap;
  143. }
  144. }
  145. // check how much damage we gain from blocking enemy shooters on this hex
  146. bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
  147. logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %lld %lld %lld %lld",
  148. attackInfo.attacker->unitType()->identifier,
  149. attackInfo.defender->unitType()->identifier,
  150. (int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
  151. bestAp.damageDealt, bestAp.damageReceived, bestAp.collateralDamage, bestAp.shootersBlockedDmg);
  152. //TODO other damage related to attack (eg. fire shield and other abilities)
  153. return bestAp;
  154. }