AttackPossibility.cpp 7.3 KB

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