Pass HitData as reference to damage calculation library, to prevent multiple lookup.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-25 16:51:10 +02:00
parent 864a9d933d
commit 8eb22ad68d
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 36 additions and 27 deletions

View File

@ -7,19 +7,23 @@ export const DamageLibrary* CreatureLib_DamageLibrary_Construct() { return new D
export void CreatureLib_DamageLibrary_Destruct(const DamageLibrary* p) { delete p; }
export uint8_t CreatureLib_DamageLibrary_GetDamage(uint32_t& out, const DamageLibrary* p, ExecutingAttack* attack,
Creature* target, uint8_t hitIndex) {
Try(out = p->GetDamage(attack, target, hitIndex);)
Creature* target, uint8_t hitIndex,
ExecutingAttack::HitData* hitData) {
Try(out = p->GetDamage(attack, target, hitIndex, *hitData);)
}
export uint8_t CreatureLib_DamageLibrary_GetBasePower(uint8_t& out, const DamageLibrary* p, ExecutingAttack* attack,
Creature* target, uint8_t hitIndex) {
Try(out = p->GetBasePower(attack, target, hitIndex);)
Creature* target, uint8_t hitIndex,
ExecutingAttack::HitData* hitData) {
Try(out = p->GetBasePower(attack, target, hitIndex, *hitData);)
}
export float CreatureLib_DamageLibrary_GetStatModifier(float& out, const DamageLibrary* p, ExecutingAttack* attack,
Creature* target, uint8_t hitIndex) {
Try(out = p->GetStatModifier(attack, target, hitIndex);)
Creature* target, uint8_t hitIndex,
ExecutingAttack::HitData* hitData) {
Try(out = p->GetStatModifier(attack, target, hitIndex, *hitData);)
}
export float CreatureLib_DamageLibrary_GetDamageModifier(float& out, const DamageLibrary* p, ExecutingAttack* attack,
Creature* target, uint8_t hitIndex) {
Try(out = p->GetDamageModifier(attack, target, hitIndex);)
Creature* target, uint8_t hitIndex,
ExecutingAttack::HitData* hitData) {
Try(out = p->GetDamageModifier(attack, target, hitIndex, *hitData);)
}

View File

@ -147,8 +147,8 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
HOOK(ChangeEffectiveness, attack, attack, target, hitIndex, &effectiveness)
hit.SetEffectiveness(effectiveness);
hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target, hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex));
hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex, hit));
hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex, hit));
if (attackData->GetCategory() == Library::AttackCategory::Status) {
if (attackData->HasSecondaryEffect()) {

View File

@ -3,21 +3,22 @@
#include "../ScriptHandling/ScriptMacros.hpp"
using namespace CreatureLib::Battling;
uint32_t DamageLibrary::GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const {
uint32_t DamageLibrary::GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack)
AssertNotNull(target)
auto levelMod = static_cast<float>(2 * attack->GetUser()->GetLevel()) / 5 + 2;
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
auto bp = hit.GetBasePower();
auto statMod = GetStatModifier(attack, target, hitIndex);
auto bp = hitData.GetBasePower();
auto statMod = GetStatModifier(attack, target, hitIndex, hitData);
HOOK(ModifyStatModifier, attack, attack, target, hitIndex, &statMod);
uint32_t damage = static_cast<uint32_t>((((levelMod * static_cast<float>(bp) * statMod) / 50) + 2) *
GetDamageModifier(attack, target, hitIndex));
GetDamageModifier(attack, target, hitIndex, hitData));
HOOK(OverrideDamage, attack, attack, target, hitIndex, &damage);
return damage;
}
uint8_t DamageLibrary::GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const {
uint8_t DamageLibrary::GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack)
AssertNotNull(target)
auto bp = attack->GetAttack()->GetAttack()->GetBasePower();
@ -25,13 +26,13 @@ uint8_t DamageLibrary::GetBasePower(ExecutingAttack* attack, Creature* target, u
return bp;
}
float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const {
float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack)
AssertNotNull(target)
auto user = attack->GetUser();
AssertNotNull(user)
HOOK(ChangeDamageStatsUser, attack, attack, target, hitIndex, &user);
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
Library::Statistic offensiveStat;
Library::Statistic defensiveStat;
if (attack->GetAttack()->GetAttack()->GetCategory() == Library::AttackCategory::Physical) {
@ -42,9 +43,9 @@ float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target,
defensiveStat = Library::Statistic::MagicalDefense;
}
auto bypassDefensive = hit.IsCritical() && target->GetStatBoost(defensiveStat) > 0;
auto bypassDefensive = hitData.IsCritical() && target->GetStatBoost(defensiveStat) > 0;
HOOK(BypassDefensiveStat, attack, attack, target, hitIndex, &bypassDefensive);
auto bypassOffensive = hit.IsCritical() && user->GetStatBoost(offensiveStat) < 0;
auto bypassOffensive = hitData.IsCritical() && user->GetStatBoost(offensiveStat) < 0;
HOOK(BypassOffensiveStat, attack, attack, target, hitIndex, &bypassOffensive);
float offensiveValue;
@ -64,12 +65,12 @@ float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target,
return offensiveValue / defensiveValue;
}
float DamageLibrary::GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const {
float DamageLibrary::GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const {
AssertNotNull(attack)
AssertNotNull(target)
float mod = 1;
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
mod *= hit.GetEffectiveness();
mod *= hitData.GetEffectiveness();
HOOK(ModifyDamageModifier, attack, attack, target, hitIndex, &mod);
return mod;
}

View File

@ -8,11 +8,15 @@ namespace CreatureLib::Battling {
class DamageLibrary {
public:
virtual ~DamageLibrary() = default;
virtual uint32_t GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual uint32_t GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const;
virtual uint8_t GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual float GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual float GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual uint8_t GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const;
virtual float GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const;
virtual float GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex,
const ExecutingAttack::HitData& hitData) const;
};
}