Fixes for damage calculation: Adds missing + 2, and handles rounding correctly.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-08-28 12:04:18 +02:00
parent 7fe7c8becf
commit 037ff590f0
4 changed files with 71 additions and 6 deletions

View File

@@ -5,22 +5,28 @@
using HitData = const CreatureLib::Battling::ExecutingAttack::HitData;
inline constexpr float to_f(i32 a) { return static_cast<float>(a); }
inline constexpr float fl(float a) { return std::floor(a); }
uint32_t PkmnLib::Battling::DamageLibrary::GetDamage(CreatureLib::Battling::ExecutingAttack* attack,
CreatureLib::Battling::Creature* target, uint8_t hitIndex,
const HitData& hitData) const {
auto levelMod = static_cast<float>(2 * attack->GetUser()->GetLevel()) / 5;
auto levelMod = fl((2 * attack->GetUser()->GetLevel()) / 5.0f) + 2;
auto bp = hitData.GetBasePower();
auto statMod = GetStatModifier(attack, target, hitIndex, hitData);
auto damageMod = GetDamageModifier(attack, target, hitIndex, hitData);
// HOOK: Modify stat modifier
auto floatDamage = (((levelMod * static_cast<float>(bp) * statMod) / 50) + 2) * damageMod;
uint32_t damage;
auto floatDamage = fl(levelMod * bp);
floatDamage = fl(floatDamage * statMod);
floatDamage = fl(floatDamage / 50) + 2;
floatDamage = fl(floatDamage * damageMod);
uint32_t damage = 0;
if (floatDamage < 0) {
damage = 0;
} else if (floatDamage >= (float)UINT32_MAX) {
damage = UINT32_MAX;
} else { // TODO: C++ 20 - add [[likely]] attribute when supported by both gcc and Clang
} else {
damage = static_cast<uint32_t>(floatDamage);
}
// HOOK: Override damage
@@ -82,8 +88,8 @@ float PkmnLib::Battling::DamageLibrary::GetDamageModifier(CreatureLib::Battling:
PKMN_HOOK(OverrideCriticalModifier, attack, attack, target, hitIndex, &critModifier);
mod *= critModifier;
}
Ensure(attack->GetUser()->GetBattle().GetValue());
if (_hasRandomness) {
Ensure(attack->GetUser()->GetBattle().GetValue());
float randPercentage = 85 + attack->GetUser()->GetBattle().GetValue()->GetRandom()->Get(0, 16);
mod *= randPercentage / 100.0;
}