2020-05-09 12:51:00 +02:00
|
|
|
/*
|
|
|
|
* AttackPossibility.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "AttackPossibility.h"
|
2020-05-09 13:09:32 +02:00
|
|
|
#include "../../lib/CStack.h" // TODO: remove
|
|
|
|
// Eventually only IBattleInfoCallback and battle::Unit should be used,
|
|
|
|
// CUnitState should be private and CStack should be removed completely
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2023-03-23 17:49:33 +02:00
|
|
|
uint64_t averageDmg(const DamageRange & range)
|
2022-10-17 18:47:16 +02:00
|
|
|
{
|
2023-03-23 17:49:33 +02:00
|
|
|
return (range.min + range.max) / 2;
|
2022-10-17 18:47:16 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
void DamageCache::cacheDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
|
|
|
|
{
|
|
|
|
auto damage = averageDmg(hb->battleEstimateDamage(attacker, defender, 0).damage);
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
damageCache[attacker->unitId()][defender->unitId()] = static_cast<float>(damage) / attacker->getCount();
|
2023-08-08 17:37:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DamageCache::buildDamageCache(std::shared_ptr<HypotheticBattle> hb, int side)
|
|
|
|
{
|
|
|
|
auto stacks = hb->battleGetUnitsIf([=](const battle::Unit * u) -> bool
|
|
|
|
{
|
2023-08-27 19:44:03 +02:00
|
|
|
return u->isValidTarget();
|
2023-08-08 17:37:42 +02:00
|
|
|
});
|
|
|
|
|
2024-01-10 00:38:54 +02:00
|
|
|
std::vector<const battle::Unit *> ourUnits;
|
|
|
|
std::vector<const battle::Unit *> enemyUnits;
|
2023-08-08 17:37:42 +02:00
|
|
|
|
|
|
|
for(auto stack : stacks)
|
|
|
|
{
|
|
|
|
if(stack->unitSide() == side)
|
|
|
|
ourUnits.push_back(stack);
|
|
|
|
else
|
|
|
|
enemyUnits.push_back(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto ourUnit : ourUnits)
|
|
|
|
{
|
|
|
|
if(!ourUnit->alive())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(auto enemyUnit : enemyUnits)
|
|
|
|
{
|
|
|
|
if(enemyUnit->alive())
|
|
|
|
{
|
|
|
|
cacheDamage(ourUnit, enemyUnit, hb);
|
|
|
|
cacheDamage(enemyUnit, ourUnit, hb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t DamageCache::getDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
|
|
|
|
{
|
2024-02-13 23:42:05 +02:00
|
|
|
bool wasComputedBefore = damageCache[attacker->unitId()].count(defender->unitId());
|
2023-08-08 17:37:42 +02:00
|
|
|
|
2024-02-13 23:42:05 +02:00
|
|
|
if (!wasComputedBefore)
|
2023-08-08 17:37:42 +02:00
|
|
|
cacheDamage(attacker, defender, hb);
|
|
|
|
|
2024-02-13 23:42:05 +02:00
|
|
|
return damageCache[attacker->unitId()][defender->unitId()] * attacker->getCount();
|
2023-08-08 17:37:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t DamageCache::getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
|
|
|
|
{
|
|
|
|
if(parent)
|
|
|
|
{
|
|
|
|
auto attackerDamageMap = parent->damageCache.find(attacker->unitId());
|
|
|
|
|
|
|
|
if(attackerDamageMap != parent->damageCache.end())
|
|
|
|
{
|
|
|
|
auto targetDamage = attackerDamageMap->second.find(defender->unitId());
|
|
|
|
|
|
|
|
if(targetDamage != attackerDamageMap->second.end())
|
|
|
|
{
|
|
|
|
return static_cast<int64_t>(targetDamage->second * attacker->getCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getDamage(attacker, defender, hb);
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:51:00 +02:00
|
|
|
AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
|
|
|
|
: from(from), dest(dest), attack(attack)
|
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
this->attack.attackerPos = from;
|
|
|
|
this->attack.defenderPos = dest;
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float AttackPossibility::damageDiff() const
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
return defenderDamageReduce - attackerDamageReduce - collateralDamageReduce + shootersBlockedDmg;
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float AttackPossibility::damageDiff(float positiveEffectMultiplier, float negativeEffectMultiplier) const
|
2023-08-19 09:22:34 +02:00
|
|
|
{
|
|
|
|
return positiveEffectMultiplier * (defenderDamageReduce + shootersBlockedDmg)
|
|
|
|
- negativeEffectMultiplier * (attackerDamageReduce + collateralDamageReduce);
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float AttackPossibility::attackValue() const
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
|
|
|
return damageDiff();
|
|
|
|
}
|
|
|
|
|
2023-10-22 20:37:43 +02:00
|
|
|
float hpFunction(uint64_t unitHealthStart, uint64_t unitHealthEnd, uint64_t maxHealth)
|
|
|
|
{
|
|
|
|
float ratioStart = static_cast<float>(unitHealthStart) / maxHealth;
|
|
|
|
float ratioEnd = static_cast<float>(unitHealthEnd) / maxHealth;
|
|
|
|
float base = 0.666666f;
|
|
|
|
|
|
|
|
// reduce from max to 0 must be 1.
|
|
|
|
// 10 hp from end costs bit more than 10 hp from start because our goal is to kill unit, not just hurt it
|
|
|
|
// ********** 2 * base - ratioStart *********
|
|
|
|
// * *
|
|
|
|
// * height = ratioStart - ratioEnd *
|
|
|
|
// * *
|
|
|
|
// ******************** 2 * base - ratioEnd ******
|
|
|
|
// S = (a + b) * h / 2
|
|
|
|
return (base * (4 - ratioStart - ratioEnd)) * (ratioStart - ratioEnd) / 2 ;
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
/// <summary>
|
|
|
|
/// How enemy damage will be reduced by this attack
|
|
|
|
/// Half bounty for kill, half for making damage equal to enemy health
|
|
|
|
/// Bounty - the killed creature average damage calculated against attacker
|
|
|
|
/// </summary>
|
2023-08-26 12:06:41 +02:00
|
|
|
float AttackPossibility::calculateDamageReduce(
|
2022-04-11 08:12:41 +02:00
|
|
|
const battle::Unit * attacker,
|
|
|
|
const battle::Unit * defender,
|
|
|
|
uint64_t damageDealt,
|
2023-08-08 17:37:42 +02:00
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<CBattleInfoCallback> state)
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
const float HEALTH_BOUNTY = 0.5;
|
2023-10-22 20:37:43 +02:00
|
|
|
const float KILL_BOUNTY = 0.5;
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
2023-05-10 08:06:53 +02:00
|
|
|
auto attackerUnitForMeasurement = attacker;
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
if(!attackerUnitForMeasurement || attackerUnitForMeasurement->isTurret())
|
2023-05-10 08:06:53 +02:00
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
auto ourUnits = state->battleGetUnitsIf([&](const battle::Unit * u) -> bool
|
2023-05-10 08:06:53 +02:00
|
|
|
{
|
2023-08-13 12:56:04 +02:00
|
|
|
return u->unitSide() != defender->unitSide()
|
|
|
|
&& !u->isTurret()
|
|
|
|
&& u->creatureId() != CreatureID::CATAPULT
|
|
|
|
&& u->creatureId() != CreatureID::BALLISTA
|
|
|
|
&& u->creatureId() != CreatureID::FIRST_AID_TENT
|
|
|
|
&& u->getCount();
|
2023-05-10 08:06:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if(ourUnits.empty())
|
|
|
|
attackerUnitForMeasurement = defender;
|
|
|
|
else
|
|
|
|
attackerUnitForMeasurement = ourUnits.front();
|
|
|
|
}
|
|
|
|
|
2023-08-19 09:22:34 +02:00
|
|
|
auto maxHealth = defender->getMaxHealth();
|
|
|
|
auto availableHealth = defender->getFirstHPleft() + ((defender->getCount() - 1) * maxHealth);
|
|
|
|
|
|
|
|
vstd::amin(damageDealt, availableHealth);
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
auto enemyDamageBeforeAttack = damageCache.getOriginalDamage(defender, attackerUnitForMeasurement, state);
|
2023-08-19 09:22:34 +02:00
|
|
|
auto enemiesKilled = damageDealt / maxHealth + (damageDealt % maxHealth >= defender->getFirstHPleft() ? 1 : 0);
|
2023-08-08 17:37:42 +02:00
|
|
|
auto damagePerEnemy = enemyDamageBeforeAttack / (double)defender->getCount();
|
2023-10-22 20:37:43 +02:00
|
|
|
auto exceedingDamage = (damageDealt % maxHealth);
|
|
|
|
float hpValue = (damageDealt / maxHealth);
|
|
|
|
|
|
|
|
if(defender->getFirstHPleft() >= exceedingDamage)
|
|
|
|
{
|
|
|
|
hpValue += hpFunction(defender->getFirstHPleft(), defender->getFirstHPleft() - exceedingDamage, maxHealth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hpValue += hpFunction(defender->getFirstHPleft(), 0, maxHealth);
|
|
|
|
hpValue += hpFunction(maxHealth, maxHealth + defender->getFirstHPleft() - exceedingDamage, maxHealth);
|
|
|
|
}
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2023-10-22 20:37:43 +02:00
|
|
|
return damagePerEnemy * (enemiesKilled * KILL_BOUNTY + hpValue * HEALTH_BOUNTY);
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
int64_t AttackPossibility::evaluateBlockedShootersDmg(
|
|
|
|
const BattleAttackInfo & attackInfo,
|
|
|
|
BattleHex hex,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<CBattleInfoCallback> state)
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
|
|
|
int64_t res = 0;
|
|
|
|
|
|
|
|
if(attackInfo.shooting)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto attacker = attackInfo.attacker;
|
|
|
|
auto hexes = attacker->getSurroundingHexes(hex);
|
|
|
|
for(BattleHex tile : hexes)
|
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
auto st = state->battleGetUnitByPos(tile, true);
|
|
|
|
if(!st || !state->battleMatchOwner(st, attacker))
|
2020-05-09 12:51:00 +02:00
|
|
|
continue;
|
2023-08-08 17:37:42 +02:00
|
|
|
if(!state->battleCanShoot(st))
|
2020-05-09 12:51:00 +02:00
|
|
|
continue;
|
|
|
|
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
|
|
|
BattleAttackInfo rangeAttackInfo(st, attacker, 0, true);
|
2020-05-09 12:51:00 +02:00
|
|
|
rangeAttackInfo.defenderPos = hex;
|
|
|
|
|
2023-01-14 19:03:18 +02:00
|
|
|
BattleAttackInfo meleeAttackInfo(st, attacker, 0, false);
|
2020-05-09 12:51:00 +02:00
|
|
|
meleeAttackInfo.defenderPos = hex;
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
auto rangeDmg = state->battleEstimateDamage(rangeAttackInfo);
|
|
|
|
auto meleeDmg = state->battleEstimateDamage(meleeAttackInfo);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2023-03-24 17:17:17 +02:00
|
|
|
int64_t gain = averageDmg(rangeDmg.damage) - averageDmg(meleeDmg.damage) + 1;
|
2020-05-09 12:51:00 +02:00
|
|
|
res += gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
AttackPossibility AttackPossibility::evaluate(
|
|
|
|
const BattleAttackInfo & attackInfo,
|
|
|
|
BattleHex hex,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<CBattleInfoCallback> state)
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
|
|
|
auto attacker = attackInfo.attacker;
|
|
|
|
auto defender = attackInfo.defender;
|
|
|
|
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto selectorBlocksRetaliation = Selector::type()(BonusType::BLOCKS_RETALIATION);
|
2023-08-08 17:37:42 +02:00
|
|
|
const auto attackerSide = state->playerToSide(state->battleGetOwner(attacker));
|
2020-05-09 12:51:00 +02:00
|
|
|
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
|
|
|
|
|
|
|
AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
|
|
|
|
|
|
|
|
std::vector<BattleHex> defenderHex;
|
|
|
|
if(attackInfo.shooting)
|
|
|
|
defenderHex = defender->getHexes();
|
|
|
|
else
|
|
|
|
defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
|
|
|
|
|
|
|
|
for(BattleHex defHex : defenderHex)
|
|
|
|
{
|
2020-05-16 15:14:58 +02:00
|
|
|
if(defHex == hex) // should be impossible but check anyway
|
2020-05-09 12:51:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
AttackPossibility ap(hex, defHex, attackInfo);
|
|
|
|
ap.attackerState = attacker->acquireState();
|
|
|
|
ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
|
|
|
|
|
|
|
|
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
|
|
|
|
|
|
|
|
if (!attackInfo.shooting)
|
|
|
|
ap.attackerState->setPosition(hex);
|
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
std::vector<const battle::Unit *> defenderUnits;
|
|
|
|
std::vector<const battle::Unit *> retaliatedUnits = {attacker};
|
|
|
|
std::vector<const battle::Unit *> affectedUnits;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
if (attackInfo.shooting)
|
2024-07-28 13:41:21 +02:00
|
|
|
defenderUnits = state->getAttackedBattleUnits(attacker, defender, defHex, true, hex, defender->getPosition());
|
2020-05-09 12:51:00 +02:00
|
|
|
else
|
2020-05-16 15:14:58 +02:00
|
|
|
{
|
2024-07-28 13:41:21 +02:00
|
|
|
defenderUnits = state->getAttackedBattleUnits(attacker, defender, defHex, false, hex, defender->getPosition());
|
|
|
|
retaliatedUnits = state->getAttackedBattleUnits(defender, attacker, hex, false, defender->getPosition(), hex);
|
2024-07-22 19:39:23 +02:00
|
|
|
|
|
|
|
// attacker can not melle-attack itself but still can hit that place where it was before moving
|
|
|
|
vstd::erase_if(defenderUnits, [attacker](const battle::Unit * u) -> bool { return u->unitId() == attacker->unitId(); });
|
|
|
|
|
|
|
|
if(!vstd::contains_if(retaliatedUnits, [attacker](const battle::Unit * u) -> bool { return u->unitId() == attacker->unitId(); }))
|
2020-05-16 15:14:58 +02:00
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
retaliatedUnits.push_back(attacker);
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-16 15:14:58 +02:00
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
// ensure the defender is also affected
|
|
|
|
if(!vstd::contains_if(defenderUnits, [defender](const battle::Unit * u) -> bool { return u->unitId() == defender->unitId(); }))
|
|
|
|
{
|
|
|
|
defenderUnits.push_back(defender);
|
|
|
|
}
|
|
|
|
|
|
|
|
affectedUnits = defenderUnits;
|
|
|
|
vstd::concatenate(affectedUnits, retaliatedUnits);
|
|
|
|
|
|
|
|
logAi->trace("Attacked battle units count %d, %d->%d", affectedUnits.size(), hex.hex, defHex.hex);
|
|
|
|
|
|
|
|
std::map<uint32_t, std::shared_ptr<battle::CUnitState>> defenderStates;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
for(auto u : affectedUnits)
|
2020-05-16 15:14:58 +02:00
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
if(u->unitId() == attacker->unitId())
|
|
|
|
continue;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
auto defenderState = u->acquireState();
|
2024-07-22 19:39:23 +02:00
|
|
|
|
2020-05-09 12:51:00 +02:00
|
|
|
ap.affectedUnits.push_back(defenderState);
|
2024-07-22 19:39:23 +02:00
|
|
|
defenderStates[u->unitId()] = defenderState;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < totalAttacks; i++)
|
|
|
|
{
|
|
|
|
if(!ap.attackerState->alive() || !defenderStates[defender->unitId()]->alive())
|
|
|
|
break;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
for(auto u : defenderUnits)
|
2020-05-17 10:21:49 +02:00
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
auto defenderState = defenderStates.at(u->unitId());
|
|
|
|
|
2024-01-10 00:38:54 +02:00
|
|
|
int64_t damageDealt;
|
|
|
|
float defenderDamageReduce;
|
|
|
|
float attackerDamageReduce;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2023-03-24 17:17:17 +02:00
|
|
|
DamageEstimation retaliation;
|
2023-08-08 17:37:42 +02:00
|
|
|
auto attackDmg = state->battleEstimateDamage(ap.attack, &retaliation);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2023-03-24 17:17:17 +02:00
|
|
|
damageDealt = averageDmg(attackDmg.damage);
|
2024-08-03 15:07:50 +02:00
|
|
|
vstd::amin(damageDealt, defenderState->getAvailableHealth());
|
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
defenderDamageReduce = calculateDamageReduce(attacker, u, damageDealt, damageCache, state);
|
2020-05-09 12:51:00 +02:00
|
|
|
ap.attackerState->afterAttack(attackInfo.shooting, false);
|
|
|
|
|
|
|
|
//FIXME: use ranged retaliation
|
2022-10-17 18:47:16 +02:00
|
|
|
attackerDamageReduce = 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
if (!attackInfo.shooting && u->unitId() == defender->unitId() && defenderState->ableToRetaliate() && !counterAttacksBlocked)
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
2024-07-22 19:39:23 +02:00
|
|
|
for(auto retaliated : retaliatedUnits)
|
|
|
|
{
|
|
|
|
if(retaliated->unitId() == attacker->unitId())
|
|
|
|
{
|
2024-08-03 15:07:50 +02:00
|
|
|
int64_t damageReceived = averageDmg(retaliation.damage);
|
|
|
|
|
|
|
|
vstd::amin(damageReceived, ap.attackerState->getAvailableHealth());
|
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
attackerDamageReduce = calculateDamageReduce(defender, retaliated, damageReceived, damageCache, state);
|
|
|
|
ap.attackerState->damage(damageReceived);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-03 15:07:50 +02:00
|
|
|
auto retaliationCollateral = state->battleEstimateDamage(defender, retaliated, 0);
|
|
|
|
int64_t damageReceived = averageDmg(retaliationCollateral.damage);
|
|
|
|
|
|
|
|
vstd::amin(damageReceived, retaliated->getAvailableHealth());
|
|
|
|
|
|
|
|
if(defender->unitSide() == retaliated->unitSide())
|
2024-07-22 19:39:23 +02:00
|
|
|
defenderDamageReduce += calculateDamageReduce(defender, retaliated, damageReceived, damageCache, state);
|
|
|
|
else
|
|
|
|
ap.collateralDamageReduce += calculateDamageReduce(defender, retaliated, damageReceived, damageCache, state);
|
|
|
|
|
|
|
|
defenderStates.at(retaliated->unitId())->damage(damageReceived);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:51:00 +02:00
|
|
|
defenderState->afterAttack(attackInfo.shooting, true);
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
bool isEnemy = state->battleMatchOwner(attacker, u);
|
2020-05-23 12:38:24 +02:00
|
|
|
|
|
|
|
// this includes enemy units as well as attacker units under enemy's mind control
|
2020-05-09 12:51:00 +02:00
|
|
|
if(isEnemy)
|
2022-10-17 18:47:16 +02:00
|
|
|
ap.defenderDamageReduce += defenderDamageReduce;
|
2020-05-23 12:38:24 +02:00
|
|
|
|
|
|
|
// damaging attacker's units (even those under enemy's mind control) is considered friendly fire
|
|
|
|
if(attackerSide == u->unitSide())
|
2022-10-17 18:47:16 +02:00
|
|
|
ap.collateralDamageReduce += defenderDamageReduce;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
if(u->unitId() == defender->unitId()
|
|
|
|
|| (!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
|
|
|
//FIXME: handle RANGED_RETALIATION ?
|
2022-10-17 18:47:16 +02:00
|
|
|
ap.attackerDamageReduce += attackerDamageReduce;
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defenderState->damage(damageDealt);
|
2024-08-04 16:50:19 +02:00
|
|
|
|
|
|
|
if(u->unitId() == defender->unitId())
|
|
|
|
{
|
|
|
|
ap.defenderDead = !defenderState->alive();
|
|
|
|
}
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-22 19:39:23 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=2
|
|
|
|
logAi->trace("BattleAI AP: %s -> %s at %d from %d, affects %d units: d:%lld a:%lld c:%lld s:%lld",
|
|
|
|
attackInfo.attacker->unitType()->getJsonKey(),
|
|
|
|
attackInfo.defender->unitType()->getJsonKey(),
|
|
|
|
(int)ap.dest, (int)ap.from, (int)ap.affectedUnits.size(),
|
|
|
|
ap.defenderDamageReduce, ap.attackerDamageReduce, ap.collateralDamageReduce, ap.shootersBlockedDmg);
|
|
|
|
#endif
|
|
|
|
|
2020-05-17 10:21:49 +02:00
|
|
|
if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue())
|
2020-05-09 12:51:00 +02:00
|
|
|
bestAp = ap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check how much damage we gain from blocking enemy shooters on this hex
|
2023-08-08 17:37:42 +02:00
|
|
|
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, damageCache, state);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace("BattleAI best AP: %s -> %s at %d from %d, affects %d units: d:%lld a:%lld c:%lld s:%lld",
|
2023-01-02 18:00:51 +02:00
|
|
|
attackInfo.attacker->unitType()->getJsonKey(),
|
|
|
|
attackInfo.defender->unitType()->getJsonKey(),
|
2020-05-09 12:51:00 +02:00
|
|
|
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
|
2022-10-17 18:47:16 +02:00
|
|
|
bestAp.defenderDamageReduce, bestAp.attackerDamageReduce, bestAp.collateralDamageReduce, bestAp.shootersBlockedDmg);
|
2023-04-08 13:05:47 +02:00
|
|
|
#endif
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
//TODO other damage related to attack (eg. fire shield and other abilities)
|
|
|
|
return bestAp;
|
|
|
|
}
|