AttackPossibility.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 auto attackerSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker));
  59. const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
  60. AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
  61. std::vector<BattleHex> defenderHex;
  62. if(attackInfo.shooting)
  63. defenderHex = defender->getHexes();
  64. else
  65. defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
  66. for(BattleHex defHex : defenderHex)
  67. {
  68. if(defHex == hex) // should be impossible but check anyway
  69. continue;
  70. AttackPossibility ap(hex, defHex, attackInfo);
  71. ap.attackerState = attacker->acquireState();
  72. ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
  73. const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
  74. if (!attackInfo.shooting)
  75. ap.attackerState->setPosition(hex);
  76. std::vector<const battle::Unit*> units;
  77. if (attackInfo.shooting)
  78. units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
  79. else
  80. units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
  81. // ensure the defender is also affected
  82. bool addDefender = true;
  83. for(auto unit : units)
  84. {
  85. if (unit->unitId() == defender->unitId())
  86. {
  87. addDefender = false;
  88. break;
  89. }
  90. }
  91. if(addDefender)
  92. units.push_back(defender);
  93. for(auto u : units)
  94. {
  95. if(!ap.attackerState->alive())
  96. break;
  97. assert(u->unitId() != attacker->unitId());
  98. auto defenderState = u->acquireState();
  99. ap.affectedUnits.push_back(defenderState);
  100. for(int i = 0; i < totalAttacks; i++)
  101. {
  102. si64 damageDealt, damageReceived;
  103. TDmgRange retaliation(0, 0);
  104. auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
  105. vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
  106. vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
  107. vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
  108. vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
  109. damageDealt = (attackDmg.first + attackDmg.second) / 2;
  110. ap.attackerState->afterAttack(attackInfo.shooting, false);
  111. //FIXME: use ranged retaliation
  112. damageReceived = 0;
  113. if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
  114. {
  115. damageReceived = (retaliation.first + retaliation.second) / 2;
  116. defenderState->afterAttack(attackInfo.shooting, true);
  117. }
  118. bool isEnemy = state->battleMatchOwner(attacker, u);
  119. // this includes enemy units as well as attacker units under enemy's mind control
  120. if(isEnemy)
  121. ap.damageDealt += damageDealt;
  122. // damaging attacker's units (even those under enemy's mind control) is considered friendly fire
  123. if(attackerSide == u->unitSide())
  124. ap.collateralDamage += damageDealt;
  125. if(u->unitId() == defender->unitId() ||
  126. (!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
  127. {
  128. //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. // check how much damage we gain from blocking enemy shooters on this hex
  141. bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
  142. logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %lld %lld %lld %lld",
  143. attackInfo.attacker->unitType()->identifier,
  144. attackInfo.defender->unitType()->identifier,
  145. (int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
  146. bestAp.damageDealt, bestAp.damageReceived, bestAp.collateralDamage, bestAp.shootersBlockedDmg);
  147. //TODO other damage related to attack (eg. fire shield and other abilities)
  148. return bestAp;
  149. }