AttackPossibility.cpp 6.1 KB

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