2016-10-28 16:16:46 +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"
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
AttackPossibility::AttackPossibility(BattleHex tile_, const BattleAttackInfo & attack_)
|
|
|
|
: tile(tile_),
|
|
|
|
attack(attack_)
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
int64_t AttackPossibility::damageDiff() const
|
|
|
|
{
|
|
|
|
//TODO: use target priority from HypotheticBattle
|
|
|
|
const auto dealtDmgValue = damageDealt;
|
|
|
|
const auto receivedDmgValue = damageReceived;
|
|
|
|
|
|
|
|
int64_t diff = 0;
|
|
|
|
|
|
|
|
//friendly fire or not
|
|
|
|
if(attack.attacker->unitSide() == attack.defender->unitSide())
|
|
|
|
diff = -dealtDmgValue - receivedDmgValue;
|
|
|
|
else
|
|
|
|
diff = dealtDmgValue - receivedDmgValue;
|
|
|
|
|
|
|
|
//mind control
|
|
|
|
auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attack.attacker));
|
|
|
|
if(actualSide && actualSide.get() != attack.attacker->unitSide())
|
|
|
|
diff = -diff;
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t AttackPossibility::attackValue() const
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
|
|
|
return damageDiff() + tacticImpact;
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex)
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
|
|
|
static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);
|
|
|
|
|
|
|
|
const bool counterAttacksBlocked = attackInfo.attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
|
|
|
|
|
|
|
AttackPossibility ap(hex, attackInfo);
|
2016-10-28 16:16:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
ap.attackerState = attackInfo.attacker->acquireState();
|
2016-10-28 16:16:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
|
2016-10-28 16:16:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(!attackInfo.shooting)
|
|
|
|
ap.attackerState->setPosition(hex);
|
|
|
|
|
|
|
|
auto defenderState = attackInfo.defender->acquireState();
|
|
|
|
ap.affectedUnits.push_back(defenderState);
|
|
|
|
|
|
|
|
for(int i = 0; i < totalAttacks; i++)
|
2016-10-28 16:16:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
TDmgRange retaliation(0,0);
|
|
|
|
auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
|
|
|
|
|
|
|
|
vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
|
|
|
|
vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
|
|
|
|
|
|
|
|
vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
|
|
|
|
vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
|
|
|
|
|
|
|
|
ap.damageDealt += (attackDmg.first + attackDmg.second) / 2;
|
2016-10-28 16:16:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
ap.attackerState->afterAttack(attackInfo.shooting, false);
|
2016-10-28 16:16:46 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
//FIXME: use ranged retaliation
|
|
|
|
if(!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
|
|
|
|
{
|
|
|
|
ap.damageReceived += (retaliation.first + retaliation.second) / 2;
|
|
|
|
defenderState->afterAttack(attackInfo.shooting, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ap.attackerState->damage(ap.damageReceived);
|
|
|
|
defenderState->damage(ap.damageDealt);
|
|
|
|
|
|
|
|
if(!ap.attackerState->alive() || !defenderState->alive())
|
2016-10-28 16:16:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO other damage related to attack (eg. fire shield and other abilities)
|
|
|
|
|
|
|
|
return ap;
|
|
|
|
}
|