DamageCalculator.cpp 18 KB

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