AttackPossibility.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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::calculateDpsReduce(
  28. const battle::Unit * attacker,
  29. const battle::Unit * defender,
  30. uint64_t damageDealt,
  31. std::shared_ptr<CBattleInfoCallback> cb)
  32. {
  33. vstd::amin(damageDealt, defender->getAvailableHealth());
  34. auto enemyDamageBeforeAttack = cb->battleEstimateDamage(BattleAttackInfo(defender, attacker, defender->canShoot()));
  35. auto enemiesKilled = damageDealt / defender->MaxHealth() + (damageDealt % defender->MaxHealth() >= defender->getFirstHPleft() ? 1 : 0);
  36. auto enemyDps = (enemyDamageBeforeAttack.first + enemyDamageBeforeAttack.second) / 2;
  37. auto dpsPerEnemy = enemyDps / (double)defender->getCount();
  38. return (int64_t)(dpsPerEnemy * (enemiesKilled + damageDealt / (double)defender->MaxHealth()) / 2);
  39. }
  40. int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
  41. {
  42. int64_t res = 0;
  43. if(attackInfo.shooting)
  44. return 0;
  45. auto attacker = attackInfo.attacker;
  46. auto hexes = attacker->getSurroundingHexes(hex);
  47. for(BattleHex tile : hexes)
  48. {
  49. auto st = state->battleGetUnitByPos(tile, true);
  50. if(!st || !state->battleMatchOwner(st, attacker))
  51. continue;
  52. if(!state->battleCanShoot(st))
  53. continue;
  54. BattleAttackInfo rangeAttackInfo(st, attacker, true);
  55. rangeAttackInfo.defenderPos = hex;
  56. BattleAttackInfo meleeAttackInfo(st, attacker, false);
  57. meleeAttackInfo.defenderPos = hex;
  58. auto rangeDmg = getCbc()->battleEstimateDamage(rangeAttackInfo);
  59. auto meleeDmg = getCbc()->battleEstimateDamage(meleeAttackInfo);
  60. int64_t gain = (rangeDmg.first + rangeDmg.second - meleeDmg.first - meleeDmg.second) / 2 + 1;
  61. res += gain;
  62. }
  63. return res;
  64. }
  65. AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
  66. {
  67. auto attacker = attackInfo.attacker;
  68. auto defender = attackInfo.defender;
  69. const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
  70. static const auto selectorBlocksRetaliation = Selector::type()(Bonus::BLOCKS_RETALIATION);
  71. const auto attackerSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker));
  72. const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
  73. AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
  74. std::vector<BattleHex> defenderHex;
  75. if(attackInfo.shooting)
  76. defenderHex = defender->getHexes();
  77. else
  78. defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
  79. for(BattleHex defHex : defenderHex)
  80. {
  81. if(defHex == hex) // should be impossible but check anyway
  82. continue;
  83. AttackPossibility ap(hex, defHex, attackInfo);
  84. ap.attackerState = attacker->acquireState();
  85. ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
  86. const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
  87. if (!attackInfo.shooting)
  88. ap.attackerState->setPosition(hex);
  89. std::vector<const battle::Unit*> units;
  90. if (attackInfo.shooting)
  91. units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
  92. else
  93. units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
  94. // ensure the defender is also affected
  95. bool addDefender = true;
  96. for(auto unit : units)
  97. {
  98. if (unit->unitId() == defender->unitId())
  99. {
  100. addDefender = false;
  101. break;
  102. }
  103. }
  104. if(addDefender)
  105. units.push_back(defender);
  106. for(auto u : units)
  107. {
  108. if(!ap.attackerState->alive())
  109. break;
  110. auto defenderState = u->acquireState();
  111. ap.affectedUnits.push_back(defenderState);
  112. for(int i = 0; i < totalAttacks; i++)
  113. {
  114. int64_t damageDealt, damageReceived, enemyDpsReduce, ourDpsReduce;
  115. TDmgRange retaliation(0, 0);
  116. auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
  117. TDmgRange enemyDamageBeforeAttack = getCbc()->battleEstimateDamage(BattleAttackInfo(u, attacker, u->canShoot()));
  118. vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
  119. vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
  120. vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
  121. vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
  122. damageDealt = (attackDmg.first + attackDmg.second) / 2;
  123. enemyDpsReduce = calculateDpsReduce(attacker, defender, damageDealt, getCbc());
  124. ap.attackerState->afterAttack(attackInfo.shooting, false);
  125. //FIXME: use ranged retaliation
  126. damageReceived = 0;
  127. ourDpsReduce = 0;
  128. if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
  129. {
  130. damageReceived = (retaliation.first + retaliation.second) / 2;
  131. ourDpsReduce = calculateDpsReduce(defender, attacker, damageReceived, getCbc());
  132. defenderState->afterAttack(attackInfo.shooting, true);
  133. }
  134. bool isEnemy = state->battleMatchOwner(attacker, u);
  135. // this includes enemy units as well as attacker units under enemy's mind control
  136. if(isEnemy)
  137. ap.damageDealt += enemyDpsReduce;
  138. // damaging attacker's units (even those under enemy's mind control) is considered friendly fire
  139. if(attackerSide == u->unitSide())
  140. ap.collateralDamage += enemyDpsReduce;
  141. if(u->unitId() == defender->unitId() ||
  142. (!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
  143. {
  144. //FIXME: handle RANGED_RETALIATION ?
  145. ap.damageReceived += ourDpsReduce;
  146. }
  147. ap.attackerState->damage(damageReceived);
  148. defenderState->damage(damageDealt);
  149. if (!ap.attackerState->alive() || !defenderState->alive())
  150. break;
  151. }
  152. }
  153. if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue())
  154. bestAp = ap;
  155. }
  156. // check how much damage we gain from blocking enemy shooters on this hex
  157. bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
  158. logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %lld %lld %lld %lld",
  159. attackInfo.attacker->unitType()->identifier,
  160. attackInfo.defender->unitType()->identifier,
  161. (int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
  162. bestAp.damageDealt, bestAp.damageReceived, bestAp.collateralDamage, bestAp.shootersBlockedDmg);
  163. //TODO other damage related to attack (eg. fire shield and other abilities)
  164. return bestAp;
  165. }