From aee0f04d9cc6a490fa3f54b894851d4bcbfb5624 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 28 Aug 2021 12:16:01 +0200 Subject: [PATCH] Further fixes for rounding errors in damage calculations. --- src/Battling/Library/DamageLibrary.cpp | 49 ++++++++++++++------------ tests/BattleTests/DamageTests.cpp | 4 +++ 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/Battling/Library/DamageLibrary.cpp b/src/Battling/Library/DamageLibrary.cpp index 8e194dc..12787d6 100644 --- a/src/Battling/Library/DamageLibrary.cpp +++ b/src/Battling/Library/DamageLibrary.cpp @@ -21,6 +21,30 @@ uint32_t PkmnLib::Battling::DamageLibrary::GetDamage(CreatureLib::Battling::Exec floatDamage = fl(floatDamage * statMod); floatDamage = fl(floatDamage / 50) + 2; floatDamage = fl(floatDamage * damageMod); + + if (attack->GetTargetCount() > 1) { + floatDamage = fl(floatDamage * 0.75f); + } + if (hitData.IsCritical()) { + float critModifier = 1.5; + PKMN_HOOK(OverrideCriticalModifier, attack, attack, target, hitIndex, &critModifier); + floatDamage = fl(floatDamage * critModifier); + } + + if (_hasRandomness) { + Ensure(attack->GetUser()->GetBattle().GetValue()); + float randPercentage = 85 + attack->GetUser()->GetBattle().GetValue()->GetRandom()->Get(0, 16); + floatDamage = fl(floatDamage * (randPercentage / 100.0)); + } + + if (attack->GetUser()->HasType(hitData.GetType())) { + float stabModifier = 1.5; + PKMN_HOOK(OverrideSTABModifier, attack, attack, target, hitIndex, &stabModifier); + floatDamage = fl(floatDamage * stabModifier); + } + + floatDamage = fl(floatDamage * hitData.GetEffectiveness()); + uint32_t damage = 0; if (floatDamage < 0) { damage = 0; @@ -77,29 +101,10 @@ float PkmnLib::Battling::DamageLibrary::GetStatModifier(CreatureLib::Battling::E return offensiveValue / defensiveValue; } -float PkmnLib::Battling::DamageLibrary::GetDamageModifier(CreatureLib::Battling::ExecutingAttack* attack, - CreatureLib::Battling::Creature* target, uint8_t hitIndex, - const HitData& hitData) const { +float PkmnLib::Battling::DamageLibrary::GetDamageModifier(CreatureLib::Battling::ExecutingAttack*, + CreatureLib::Battling::Creature*, uint8_t, + const HitData&) const { float mod = 1; - if (attack->GetTargetCount() > 1) - mod *= 0.75; - if (hitData.IsCritical()) { - float critModifier = 1.5; - PKMN_HOOK(OverrideCriticalModifier, attack, attack, target, hitIndex, &critModifier); - mod *= critModifier; - } - if (_hasRandomness) { - Ensure(attack->GetUser()->GetBattle().GetValue()); - float randPercentage = 85 + attack->GetUser()->GetBattle().GetValue()->GetRandom()->Get(0, 16); - mod *= randPercentage / 100.0; - } - if (attack->GetUser()->HasType(hitData.GetType())) { - float stabModifier = 1.5; - PKMN_HOOK(OverrideSTABModifier, attack, attack, target, hitIndex, &stabModifier); - mod *= stabModifier; - } - - mod *= hitData.GetEffectiveness(); // HOOK: Modify damage modifier. return mod; } diff --git a/tests/BattleTests/DamageTests.cpp b/tests/BattleTests/DamageTests.cpp index 9202b5e..4a143ad 100644 --- a/tests/BattleTests/DamageTests.cpp +++ b/tests/BattleTests/DamageTests.cpp @@ -37,6 +37,10 @@ TEST_CASE("Correct rounding for damage.") { auto damage = lib->GetDamageLibrary()->GetDamage(&executingAttack, mon2, 0, hit); REQUIRE_EQ(144, damage); + + hit.SetBasePower(110); + damage = lib->GetDamageLibrary()->GetDamage(&executingAttack, mon2, 0, hit); + REQUIRE_EQ(284, damage); } #endif \ No newline at end of file