1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/*
* DamageCalculator.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 "DamageCalculator.h"
#include "CBattleInfoCallback.h"
#include "Unit.h"
#include "../bonuses/Bonus.h"
#include "../mapObjects/CGTownInstance.h"
#include "../spells/CSpellHandler.h"
#include "../IGameSettings.h"
#include "../VCMI_Lib.h"
VCMI_LIB_NAMESPACE_BEGIN
DamageRange DamageCalculator::getBaseDamageSingle() const
{
int64_t minDmg = 0.0;
int64_t maxDmg = 0.0;
minDmg = info.attacker->getMinDamage(info.shooting);
maxDmg = info.attacker->getMaxDamage(info.shooting);
if(minDmg > maxDmg)
{
const auto & creatureName = info.attacker->creatureId().toEntity(VLC)->getNamePluralTranslated();
logGlobal->error("Creature %s: min damage %lld exceeds max damage %lld.", creatureName, minDmg, maxDmg);
logGlobal->error("This may lead to unexpected results, please report it to the mod's creator.");
// to avoid an RNG crash and make bless and curse spells work as expected
std::swap(minDmg, maxDmg);
}
if(info.attacker->creatureIndex() == CreatureID::ARROW_TOWERS)
{
const auto * town = callback.battleGetDefendedTown();
assert(town);
switch(info.attacker->getPosition().toInt())
{
case BattleHex::CASTLE_CENTRAL_TOWER:
return town->getKeepDamageRange();
case BattleHex::CASTLE_BOTTOM_TOWER:
case BattleHex::CASTLE_UPPER_TOWER:
return town->getTowerDamageRange();
default:
assert(0);
}
}
const std::string cachingStrSiedgeWeapon = "type_SIEGE_WEAPON";
static const auto selectorSiedgeWeapon = Selector::type()(BonusType::SIEGE_WEAPON);
if(info.attacker->hasBonus(selectorSiedgeWeapon, cachingStrSiedgeWeapon) && info.attacker->creatureIndex() != CreatureID::ARROW_TOWERS)
{
auto retrieveHeroPrimSkill = [&](PrimarySkill skill) -> int
{
std::shared_ptr<const Bonus> b = info.attacker->getBonus(Selector::sourceTypeSel(BonusSource::HERO_BASE_SKILL).And(Selector::typeSubtype(BonusType::PRIMARY_SKILL, BonusSubtypeID(skill))));
return b ? b->val : 0;
};
//minDmg and maxDmg are multiplied by hero attack + 1
minDmg *= retrieveHeroPrimSkill(PrimarySkill::ATTACK) + 1;
maxDmg *= retrieveHeroPrimSkill(PrimarySkill::ATTACK) + 1;
}
return { minDmg, maxDmg };
}
DamageRange DamageCalculator::getBaseDamageBlessCurse() const
{
const std::string cachingStrForcedMinDamage = "type_ALWAYS_MINIMUM_DAMAGE";
static const auto selectorForcedMinDamage = Selector::type()(BonusType::ALWAYS_MINIMUM_DAMAGE);
const std::string cachingStrForcedMaxDamage = "type_ALWAYS_MAXIMUM_DAMAGE";
static const auto selectorForcedMaxDamage = Selector::type()(BonusType::ALWAYS_MAXIMUM_DAMAGE);
TConstBonusListPtr curseEffects = info.attacker->getBonuses(selectorForcedMinDamage, cachingStrForcedMinDamage);
TConstBonusListPtr blessEffects = info.attacker->getBonuses(selectorForcedMaxDamage, cachingStrForcedMaxDamage);
int curseBlessAdditiveModifier = blessEffects->totalValue() - curseEffects->totalValue();
DamageRange baseDamage = getBaseDamageSingle();
DamageRange modifiedDamage = {
std::max(static_cast<int64_t>(1), baseDamage.min + curseBlessAdditiveModifier),
std::max(static_cast<int64_t>(1), baseDamage.max + curseBlessAdditiveModifier)
};
if(curseEffects->size() && blessEffects->size() )
{
logGlobal->warn("Stack has both curse and bless! Effects will negate each other!");
return modifiedDamage;
}
if(curseEffects->size())
{
return {
modifiedDamage.min,
modifiedDamage.min
};
}
if(blessEffects->size())
{
return {
modifiedDamage.max,
modifiedDamage.max
};
}
return modifiedDamage;
}
DamageRange DamageCalculator::getBaseDamageStack() const
{
auto stackSize = info.attacker->getCount();
auto baseDamage = getBaseDamageBlessCurse();
return {
baseDamage.min * stackSize,
baseDamage.max * stackSize
};
}
int DamageCalculator::getActorAttackBase() const
{
return info.attacker->getAttack(info.shooting);
}
int DamageCalculator::getActorAttackEffective() const
{
return getActorAttackBase() + getActorAttackSlayer() + getActorAttackIgnored();
}
int DamageCalculator::getActorAttackIgnored() const
{
int multAttackReductionPercent = battleBonusValue(info.defender, Selector::type()(BonusType::ENEMY_ATTACK_REDUCTION));
if(multAttackReductionPercent > 0)
{
//using ints so 1.5 for 5 attack is rounded down as in HotA / h3assist etc. (keep in mind h3assist 1.2 shows wrong value for 15 attack points and unupg. nix)
int reduction = vstd::divideAndRound( getActorAttackBase() * multAttackReductionPercent, 100);
return -std::min(reduction, getActorAttackBase());
}
return 0;
}
int DamageCalculator::getActorAttackSlayer() const
{
const std::string cachingStrSlayer = "type_SLAYER";
static const auto selectorSlayer = Selector::type()(BonusType::SLAYER);
if (!info.defender->hasBonusOfType(BonusType::KING))
return 0;
auto slayerEffects = info.attacker->getBonuses(selectorSlayer, cachingStrSlayer);
auto slayerAffected = info.defender->unitType()->valOfBonuses(BonusType::KING);
if(std::shared_ptr<const Bonus> slayerEffect = slayerEffects->getFirst(Selector::all))
{
const auto spLevel = slayerEffect->val;
bool isAffected = spLevel >= slayerAffected;
if(isAffected)
{
SpellID spell(SpellID::SLAYER);
int attackBonus = spell.toSpell()->getLevelPower(spLevel);
if(info.attacker->hasBonusOfType(BonusType::SPECIAL_PECULIAR_ENCHANT, BonusSubtypeID(spell)))
{
ui8 attackerTier = info.attacker->unitType()->getLevel();
ui8 specialtyBonus = std::max(5 - attackerTier, 0);
attackBonus += specialtyBonus;
}
return attackBonus;
}
}
return 0;
}
int DamageCalculator::getTargetDefenseBase() const
{
return info.defender->getDefense(info.shooting);
}
int DamageCalculator::getTargetDefenseEffective() const
{
return getTargetDefenseBase() + getTargetDefenseIgnored();
}
int DamageCalculator::getTargetDefenseIgnored() const
{
double multDefenceReduction = battleBonusValue(info.attacker, Selector::type()(BonusType::ENEMY_DEFENCE_REDUCTION)) / 100.0;
if(multDefenceReduction > 0)
{
int reduction = std::floor(multDefenceReduction * getTargetDefenseBase()) + 1;
return -std::min(reduction,getTargetDefenseBase());
}
return 0;
}
double DamageCalculator::getAttackSkillFactor() const
{
int attackAdvantage = getActorAttackEffective() - getTargetDefenseEffective();
if(attackAdvantage > 0)
{
// FIXME: use cb to acquire these settings
const double attackMultiplier = VLC->engineSettings()->getDouble(EGameSettings::COMBAT_ATTACK_POINT_DAMAGE_FACTOR);
const double attackMultiplierCap = VLC->engineSettings()->getDouble(EGameSettings::COMBAT_ATTACK_POINT_DAMAGE_FACTOR_CAP);
const double attackFactor = std::min(attackMultiplier * attackAdvantage, attackMultiplierCap);
return attackFactor;
}
return 0.f;
}
double DamageCalculator::getAttackBlessFactor() const
{
const std::string cachingStrDamage = "type_GENERAL_DAMAGE_PREMY";
static const auto selectorDamage = Selector::type()(BonusType::GENERAL_DAMAGE_PREMY);
return info.attacker->valOfBonuses(selectorDamage, cachingStrDamage) / 100.0;
}
double DamageCalculator::getAttackOffenseArcheryFactor() const
{
if(info.shooting)
{
const std::string cachingStrArchery = "type_PERCENTAGE_DAMAGE_BOOSTs_1";
static const auto selectorArchery = Selector::typeSubtype(BonusType::PERCENTAGE_DAMAGE_BOOST, BonusCustomSubtype::damageTypeRanged);
return info.attacker->valOfBonuses(selectorArchery, cachingStrArchery) / 100.0;
}
const std::string cachingStrOffence = "type_PERCENTAGE_DAMAGE_BOOSTs_0";
static const auto selectorOffence = Selector::typeSubtype(BonusType::PERCENTAGE_DAMAGE_BOOST, BonusCustomSubtype::damageTypeMelee);
return info.attacker->valOfBonuses(selectorOffence, cachingStrOffence) / 100.0;
}
double DamageCalculator::getAttackLuckFactor() const
{
if(info.luckyStrike)
return 1.0;
return 0.0;
}
double DamageCalculator::getAttackDeathBlowFactor() const
{
if(info.deathBlow)
return 1.0;
return 0.0;
}
double DamageCalculator::getAttackDoubleDamageFactor() const
{
if(info.doubleDamage) {
const auto cachingStr = "type_BONUS_DAMAGE_PERCENTAGEs_" + std::to_string(info.attacker->creatureIndex());
const auto selector = Selector::typeSubtype(BonusType::BONUS_DAMAGE_PERCENTAGE, BonusSubtypeID(info.attacker->creatureId()));
return info.attacker->valOfBonuses(selector, cachingStr) / 100.0;
}
return 0.0;
}
double DamageCalculator::getAttackJoustingFactor() const
{
//applying jousting bonus
if(info.chargeDistance > 0 && info.attacker->hasBonusOfType(BonusType::JOUSTING) && !info.defender->hasBonusOfType(BonusType::CHARGE_IMMUNITY))
return info.chargeDistance * (info.attacker->valOfBonuses(BonusType::JOUSTING))/100.0;
return 0.0;
}
double DamageCalculator::getAttackHateFactor() const
{
//assume that unit have only few HATE features and cache them all
auto allHateEffects = info.attacker->getBonusesOfType(BonusType::HATE);
return allHateEffects->valOfBonuses(Selector::subtype()(BonusSubtypeID(info.defender->creatureId()))) / 100.0;
}
double DamageCalculator::getAttackRevengeFactor() const
{
if(info.attacker->hasBonusOfType(BonusType::REVENGE)) //HotA Haspid ability
{
int totalStackCount = info.attacker->unitBaseAmount();
int currentStackHealth = info.attacker->getAvailableHealth();
int creatureHealth = info.attacker->getMaxHealth();
return sqrt(static_cast<double>((totalStackCount + 1) * creatureHealth) / (currentStackHealth + creatureHealth) - 1);
}
return 0.0;
}
double DamageCalculator::getDefenseSkillFactor() const
{
int defenseAdvantage = getTargetDefenseEffective() - getActorAttackEffective();
//bonus from attack/defense skills
if(defenseAdvantage > 0) //decreasing dmg
{
// FIXME: use cb to acquire these settings
const double defenseMultiplier = VLC->engineSettings()->getDouble(EGameSettings::COMBAT_DEFENSE_POINT_DAMAGE_FACTOR);
const double defenseMultiplierCap = VLC->engineSettings()->getDouble(EGameSettings::COMBAT_DEFENSE_POINT_DAMAGE_FACTOR_CAP);
const double dec = std::min(defenseMultiplier * defenseAdvantage, defenseMultiplierCap);
return dec;
}
return 0.0;
}
double DamageCalculator::getDefenseArmorerFactor() const
{
const std::string cachingStrArmorer = "type_GENERAL_DAMAGE_REDUCTIONs_N1_NsrcSPELL_EFFECT";
static const auto selectorArmorer = Selector::typeSubtype(BonusType::GENERAL_DAMAGE_REDUCTION, BonusCustomSubtype::damageTypeAll).And(Selector::sourceTypeSel(BonusSource::SPELL_EFFECT).Not());
return info.defender->valOfBonuses(selectorArmorer, cachingStrArmorer) / 100.0;
}
double DamageCalculator::getDefenseMagicShieldFactor() const
{
const std::string cachingStrMeleeReduction = "type_GENERAL_DAMAGE_REDUCTIONs_0";
static const auto selectorMeleeReduction = Selector::typeSubtype(BonusType::GENERAL_DAMAGE_REDUCTION, BonusCustomSubtype::damageTypeMelee);
const std::string cachingStrRangedReduction = "type_GENERAL_DAMAGE_REDUCTIONs_1";
static const auto selectorRangedReduction = Selector::typeSubtype(BonusType::GENERAL_DAMAGE_REDUCTION, BonusCustomSubtype::damageTypeRanged);
//handling spell effects - shield and air shield
if(info.shooting)
return info.defender->valOfBonuses(selectorRangedReduction, cachingStrRangedReduction) / 100.0;
else
return info.defender->valOfBonuses(selectorMeleeReduction, cachingStrMeleeReduction) / 100.0;
}
double DamageCalculator::getDefenseRangePenaltiesFactor() const
{
if(info.shooting)
{
BattleHex attackerPos = info.attackerPos.isValid() ? info.attackerPos : info.attacker->getPosition();
BattleHex defenderPos = info.defenderPos.isValid() ? info.defenderPos : info.defender->getPosition();
const std::string cachingStrAdvAirShield = "isAdvancedAirShield";
auto isAdvancedAirShield = [](const Bonus* bonus)
{
return bonus->source == BonusSource::SPELL_EFFECT
&& bonus->sid == BonusSourceID(SpellID(SpellID::AIR_SHIELD))
&& bonus->val >= MasteryLevel::ADVANCED;
};
const bool distPenalty = callback.battleHasDistancePenalty(info.attacker, attackerPos, defenderPos);
if(distPenalty || info.defender->hasBonus(isAdvancedAirShield, cachingStrAdvAirShield))
return 0.5;
}
else
{
const std::string cachingStrNoMeleePenalty = "type_NO_MELEE_PENALTY";
static const auto selectorNoMeleePenalty = Selector::type()(BonusType::NO_MELEE_PENALTY);
if(info.attacker->isShooter() && !info.attacker->hasBonus(selectorNoMeleePenalty, cachingStrNoMeleePenalty))
return 0.5;
}
return 0.0;
}
double DamageCalculator::getDefenseObstacleFactor() const
{
if(info.shooting)
{
BattleHex attackerPos = info.attackerPos.isValid() ? info.attackerPos : info.attacker->getPosition();
BattleHex defenderPos = info.defenderPos.isValid() ? info.defenderPos : info.defender->getPosition();
const bool obstaclePenalty = callback.battleHasWallPenalty(info.attacker, attackerPos, defenderPos);
if(obstaclePenalty)
return 0.5;
}
return 0.0;
}
double DamageCalculator::getDefenseUnluckyFactor() const
{
if(info.unluckyStrike)
return 0.5;
return 0.0;
}
double DamageCalculator::getDefenseBlindParalysisFactor() const
{
double multAttackReduction = battleBonusValue(info.attacker, Selector::type()(BonusType::GENERAL_ATTACK_REDUCTION)) / 100.0;
return multAttackReduction;
}
double DamageCalculator::getDefenseForgetfulnessFactor() const
{
if(info.shooting)
{
//todo: set actual percentage in spell bonus configuration instead of just level; requires non trivial backward compatibility handling
//get list first, total value of 0 also counts
TConstBonusListPtr forgetfulList = info.attacker->getBonusesOfType(BonusType::FORGETFULL);
if(!forgetfulList->empty())
{
int forgetful = forgetfulList->valOfBonuses(Selector::all);
//none of basic level
if(forgetful == 0 || forgetful == 1)
return 0.5;
else
logGlobal->warn("Attempt to calculate shooting damage with adv+ FORGETFULL effect");
}
}
return 0.0;
}
double DamageCalculator::getDefensePetrificationFactor() const
{
// Creatures that are petrified by a Basilisk's Petrifying attack or a Medusa's Stone gaze take 50% damage (R8 = 0.50) from ranged and melee attacks. Taking damage also deactivates the effect.
const std::string cachingStrAllReduction = "type_GENERAL_DAMAGE_REDUCTIONs_N1_srcSPELL_EFFECT";
static const auto selectorAllReduction = Selector::typeSubtype(BonusType::GENERAL_DAMAGE_REDUCTION, BonusCustomSubtype::damageTypeAll).And(Selector::sourceTypeSel(BonusSource::SPELL_EFFECT));
return info.defender->valOfBonuses(selectorAllReduction, cachingStrAllReduction) / 100.0;
}
double DamageCalculator::getDefenseMagicFactor() const
{
// Magic Elementals deal half damage (R8 = 0.50) against Magic Elementals and Black Dragons. This is not affected by the Orb of Vulnerability, Anti-Magic, or Magic Resistance.
if(info.attacker->creatureIndex() == CreatureID::MAGIC_ELEMENTAL)
{
const std::string cachingStrMagicImmunity = "type_LEVEL_SPELL_IMMUNITY";
static const auto selectorMagicImmunity = Selector::type()(BonusType::LEVEL_SPELL_IMMUNITY);
if(info.defender->valOfBonuses(selectorMagicImmunity, cachingStrMagicImmunity) >= 5)
return 0.5;
}
return 0.0;
}
double DamageCalculator::getDefenseMindFactor() const
{
// Psychic Elementals deal half damage (R8 = 0.50) against creatures that are immune to Mind spells, such as Giants and Undead. This is not affected by the Orb of Vulnerability.
if(info.attacker->creatureIndex() == CreatureID::PSYCHIC_ELEMENTAL)
{
const std::string cachingStrMindImmunity = "type_MIND_IMMUNITY";
static const auto selectorMindImmunity = Selector::type()(BonusType::MIND_IMMUNITY);
if(info.defender->hasBonus(selectorMindImmunity, cachingStrMindImmunity))
return 0.5;
}
return 0.0;
}
std::vector<double> DamageCalculator::getAttackFactors() const
{
return {
getAttackSkillFactor(),
getAttackOffenseArcheryFactor(),
getAttackBlessFactor(),
getAttackLuckFactor(),
getAttackJoustingFactor(),
getAttackDeathBlowFactor(),
getAttackDoubleDamageFactor(),
getAttackHateFactor(),
getAttackRevengeFactor()
};
}
std::vector<double> DamageCalculator::getDefenseFactors() const
{
return {
getDefenseSkillFactor(),
getDefenseArmorerFactor(),
getDefenseMagicShieldFactor(),
getDefenseRangePenaltiesFactor(),
getDefenseObstacleFactor(),
getDefenseBlindParalysisFactor(),
getDefenseUnluckyFactor(),
getDefenseForgetfulnessFactor(),
getDefensePetrificationFactor(),
getDefenseMagicFactor(),
getDefenseMindFactor()
};
}
DamageRange DamageCalculator::getCasualties(const DamageRange & damageDealt) const
{
return {
getCasualties(damageDealt.min),
getCasualties(damageDealt.max),
};
}
int64_t DamageCalculator::getCasualties(int64_t damageDealt) const
{
if (damageDealt < info.defender->getFirstHPleft())
return 0;
int64_t damageLeft = damageDealt - info.defender->getFirstHPleft();
int64_t killsLeft = damageLeft / info.defender->getMaxHealth();
return std::min<int32_t>(1 + killsLeft, info.defender->getCount());
}
int DamageCalculator::battleBonusValue(const IBonusBearer * bearer, const CSelector & selector) const
{
auto noLimit = Selector::effectRange()(BonusLimitEffect::NO_LIMIT);
auto limitMatches = info.shooting
? Selector::effectRange()(BonusLimitEffect::ONLY_DISTANCE_FIGHT)
: Selector::effectRange()(BonusLimitEffect::ONLY_MELEE_FIGHT);
//any regular bonuses or just ones for melee/ranged
return bearer->getBonuses(selector, noLimit.Or(limitMatches))->totalValue();
};
DamageEstimation DamageCalculator::calculateDmgRange() const
{
DamageRange damageBase = getBaseDamageStack();
auto attackFactors = getAttackFactors();
auto defenseFactors = getDefenseFactors();
double attackFactorTotal = 1.0;
double defenseFactorTotal = 1.0;
for (auto & factor : attackFactors)
{
assert(factor >= 0.0);
attackFactorTotal += factor;
}
for (auto & factor : defenseFactors)
{
assert(factor >= 0.0);
defenseFactorTotal *= (1 - std::min(1.0, factor));
}
double resultingFactor = attackFactorTotal * defenseFactorTotal;
DamageRange damageDealt {
std::max<int64_t>( 1.0, std::floor(damageBase.min * resultingFactor)),
std::max<int64_t>( 1.0, std::floor(damageBase.max * resultingFactor))
};
DamageRange killsDealt = getCasualties(damageDealt);
return DamageEstimation{damageDealt, killsDealt};
}
VCMI_LIB_NAMESPACE_END
|