1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Formatting

This commit is contained in:
Victor Luchits 2020-05-09 13:51:00 +03:00
parent be10694b73
commit 59c39527c5
3 changed files with 191 additions and 187 deletions

View File

@ -1,188 +1,190 @@
/* /*
* AttackPossibility.cpp, part of VCMI engine * AttackPossibility.cpp, part of VCMI engine
* *
* Authors: listed in file AUTHORS in main folder * Authors: listed in file AUTHORS in main folder
* *
* License: GNU General Public License v2.0 or later * License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder * Full text of license available in license.txt file, in main folder
* *
*/ */
#include "StdInc.h" #include "StdInc.h"
#include "AttackPossibility.h" #include "AttackPossibility.h"
#include "../../lib/CStack.h"//todo: remove #include "../../lib/CStack.h"//todo: remove
AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack) AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
: from(from), dest(dest), attack(attack) : from(from), dest(dest), attack(attack)
{ {
} }
int64_t AttackPossibility::damageDiff() const int64_t AttackPossibility::damageDiff() const
{ {
return damageDealt - damageReceived - collateralDamage + shootersBlockedDmg; return damageDealt - damageReceived - collateralDamage + shootersBlockedDmg;
} }
int64_t AttackPossibility::attackValue() const int64_t AttackPossibility::attackValue() const
{ {
return damageDiff(); return damageDiff();
} }
int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state) int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
{ {
int64_t res = 0; int64_t res = 0;
if(attackInfo.shooting) if(attackInfo.shooting)
return 0; return 0;
auto attacker = attackInfo.attacker; auto attacker = attackInfo.attacker;
auto hexes = attacker->getSurroundingHexes(hex); auto hexes = attacker->getSurroundingHexes(hex);
for(BattleHex tile : hexes) for(BattleHex tile : hexes)
{ {
auto st = state->battleGetUnitByPos(tile, true); auto st = state->battleGetUnitByPos(tile, true);
if(!st || !state->battleMatchOwner(st, attacker)) if(!st || !state->battleMatchOwner(st, attacker))
continue; continue;
if(!state->battleCanShoot(st)) if(!state->battleCanShoot(st))
continue; continue;
BattleAttackInfo rangeAttackInfo(st, attacker, true); BattleAttackInfo rangeAttackInfo(st, attacker, true);
rangeAttackInfo.defenderPos = hex; rangeAttackInfo.defenderPos = hex;
BattleAttackInfo meleeAttackInfo(st, attacker, false); BattleAttackInfo meleeAttackInfo(st, attacker, false);
meleeAttackInfo.defenderPos = hex; meleeAttackInfo.defenderPos = hex;
auto rangeDmg = getCbc()->battleEstimateDamage(rangeAttackInfo); auto rangeDmg = getCbc()->battleEstimateDamage(rangeAttackInfo);
auto meleeDmg = getCbc()->battleEstimateDamage(meleeAttackInfo); auto meleeDmg = getCbc()->battleEstimateDamage(meleeAttackInfo);
int64_t gain = (rangeDmg.first + rangeDmg.second - meleeDmg.first - meleeDmg.second) / 2 + 1; int64_t gain = (rangeDmg.first + rangeDmg.second - meleeDmg.first - meleeDmg.second) / 2 + 1;
res += gain; res += gain;
} }
return res; return res;
} }
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state) AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
{ {
auto attacker = attackInfo.attacker; auto attacker = attackInfo.attacker;
auto defender = attackInfo.defender; auto defender = attackInfo.defender;
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION"; const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION); static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation); const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
const bool mindControlled = [&](const battle::Unit *attacker) -> bool const bool mindControlled = [&](const battle::Unit *attacker) -> bool
{ {
auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker)); auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker));
if (actualSide && actualSide.get() != attacker->unitSide()) if (actualSide && actualSide.get() != attacker->unitSide())
return true; return true;
return false; return false;
} (attacker); } (attacker);
AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo); AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
std::vector<BattleHex> defenderHex; std::vector<BattleHex> defenderHex;
if(attackInfo.shooting) { if(attackInfo.shooting)
defenderHex = defender->getHexes(); defenderHex = defender->getHexes();
} else { else
defenderHex = CStack::meleeAttackHexes(attacker, defender, hex); defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
}
for(BattleHex defHex : defenderHex)
for(BattleHex defHex : defenderHex) { {
if(defHex == hex) { if(defHex == hex) {
// should be impossible but check anyway // should be impossible but check anyway
continue; continue;
} }
AttackPossibility ap(hex, defHex, attackInfo); AttackPossibility ap(hex, defHex, attackInfo);
ap.attackerState = attacker->acquireState(); ap.attackerState = attacker->acquireState();
ap.shootersBlockedDmg = bestAp.shootersBlockedDmg; ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting); const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
if (!attackInfo.shooting) if (!attackInfo.shooting)
ap.attackerState->setPosition(hex); ap.attackerState->setPosition(hex);
std::vector<const battle::Unit*> units; std::vector<const battle::Unit*> units;
if (attackInfo.shooting) if (attackInfo.shooting)
units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID); units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
else else
units = state->getAttackedBattleUnits(attacker, defHex, false, hex); units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
// ensure the defender is also affected // ensure the defender is also affected
bool addDefender = true; bool addDefender = true;
for(auto unit : units) { for(auto unit : units) {
if (unit->unitId() == defender->unitId()) { if (unit->unitId() == defender->unitId()) {
addDefender = false; addDefender = false;
break; break;
} }
} }
if(addDefender) { if(addDefender) {
units.push_back(defender); units.push_back(defender);
} }
for(auto u : units) { for(auto u : units) {
if(!ap.attackerState->alive()) { if(!ap.attackerState->alive()) {
break; break;
} }
assert(u->unitId() != attacker->unitId()); assert(u->unitId() != attacker->unitId());
auto defenderState = u->acquireState(); auto defenderState = u->acquireState();
ap.affectedUnits.push_back(defenderState); ap.affectedUnits.push_back(defenderState);
for(int i = 0; i < totalAttacks; i++) { for(int i = 0; i < totalAttacks; i++) {
si64 damageDealt, damageReceived; si64 damageDealt, damageReceived;
TDmgRange retaliation(0, 0); TDmgRange retaliation(0, 0);
auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation); auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
vstd::amin(attackDmg.first, defenderState->getAvailableHealth()); vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
vstd::amin(attackDmg.second, defenderState->getAvailableHealth()); vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth()); vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth()); vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
damageDealt = (attackDmg.first + attackDmg.second) / 2; damageDealt = (attackDmg.first + attackDmg.second) / 2;
ap.attackerState->afterAttack(attackInfo.shooting, false); ap.attackerState->afterAttack(attackInfo.shooting, false);
//FIXME: use ranged retaliation //FIXME: use ranged retaliation
damageReceived = 0; damageReceived = 0;
if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked) if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
{ {
damageReceived = (retaliation.first + retaliation.second) / 2; damageReceived = (retaliation.first + retaliation.second) / 2;
defenderState->afterAttack(attackInfo.shooting, true); defenderState->afterAttack(attackInfo.shooting, true);
} }
bool isEnemy = state->battleMatchOwner(attacker, u) && !mindControlled; bool isEnemy = state->battleMatchOwner(attacker, u) && !mindControlled;
if(isEnemy) if(isEnemy)
ap.damageDealt += damageDealt; ap.damageDealt += damageDealt;
else // friendly fire else // friendly fire
ap.collateralDamage += damageDealt; ap.collateralDamage += damageDealt;
if(u->unitId() == defender->unitId() || if(u->unitId() == defender->unitId() ||
(!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex))) { //FIXME: handle RANGED_RETALIATION ? (!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
ap.damageReceived += damageReceived; {
} //FIXME: handle RANGED_RETALIATION ?
ap.damageReceived += damageReceived;
ap.attackerState->damage(damageReceived); }
defenderState->damage(damageDealt);
ap.attackerState->damage(damageReceived);
if (!ap.attackerState->alive() || !defenderState->alive()) defenderState->damage(damageDealt);
break;
} if (!ap.attackerState->alive() || !defenderState->alive())
} break;
}
if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue()) { }
bestAp = ap;
} if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue()) {
} bestAp = ap;
}
// check how much damage we gain from blocking enemy shooters on this hex }
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
// check how much damage we gain from blocking enemy shooters on this hex
logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %d %d %d %s", bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
VLC->creh->creatures.at(attackInfo.attacker->acquireState()->creatureId())->identifier.c_str(),
VLC->creh->creatures.at(attackInfo.defender->acquireState()->creatureId())->identifier.c_str(), logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %d %d %d %s",
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(), VLC->creh->creatures.at(attackInfo.attacker->acquireState()->creatureId())->identifier.c_str(),
(int)bestAp.damageDealt, (int)bestAp.damageReceived, (int)bestAp.collateralDamage, (int)bestAp.shootersBlockedDmg); VLC->creh->creatures.at(attackInfo.defender->acquireState()->creatureId())->identifier.c_str(),
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
//TODO other damage related to attack (eg. fire shield and other abilities) (int)bestAp.damageDealt, (int)bestAp.damageReceived, (int)bestAp.collateralDamage, (int)bestAp.shootersBlockedDmg);
return bestAp;
} //TODO other damage related to attack (eg. fire shield and other abilities)
return bestAp;
}

View File

@ -164,11 +164,12 @@ BattleAction CBattleAI::activeStack( const CStack * stack )
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id); return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
else if(bestAttack.attack.shooting) else if(bestAttack.attack.shooting)
return BattleAction::makeShotAttack(stack, bestAttack.attack.defender); return BattleAction::makeShotAttack(stack, bestAttack.attack.defender);
else { else
{
auto &target = bestAttack; auto &target = bestAttack;
logAi->debug("BattleAI: %s -> %s %d from, %d curpos %d dist %d speed %d: %d %d %d", logAi->debug("BattleAI: %s -> %s %d from, %d curpos %d dist %d speed %d: %d %d %d",
VLC->creh->creatures.at(target.attackerState->creatureId())->identifier.c_str(), VLC->creh->creatures.at(target.attackerState->creatureId())->identifier,
VLC->creh->creatures.at(target.affectedUnits[0]->creatureId())->identifier.c_str(), VLC->creh->creatures.at(target.affectedUnits[0]->creatureId())->identifier,
(int)target.affectedUnits.size(), (int)target.from, (int)bestAttack.attack.attacker->getPosition().hex, (int)target.affectedUnits.size(), (int)target.from, (int)bestAttack.attack.attacker->getPosition().hex,
(int)bestAttack.attack.chargedFields, (int)bestAttack.attack.attacker->Speed(0, true), (int)bestAttack.attack.chargedFields, (int)bestAttack.attack.attacker->Speed(0, true),
(int)target.damageDealt, (int)target.damageReceived, (int)target.attackValue() (int)target.damageDealt, (int)target.damageReceived, (int)target.attackValue()

View File

@ -69,7 +69,8 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
} }
else else
{ {
for(BattleHex hex : avHexes) { for(BattleHex hex : avHexes)
{
if(!CStack::isMeleeAttackPossible(attackerInfo, defender, hex)) if(!CStack::isMeleeAttackPossible(attackerInfo, defender, hex))
continue; continue;