Pass ExecutingAttack values by reference instead of pointer.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-25 16:04:17 +02:00
parent 734d056449
commit 0e874346bb
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 25 additions and 27 deletions

View File

@ -13,7 +13,7 @@ export void CreatureLib_ExecutingAttack_Destruct(ExecutingAttack* p) { delete p;
export uint8_t CreatureLib_ExecutingAttack_GetAttackDataForTarget(ExecutingAttack::TargetData*& out, ExecutingAttack* p,
Creature* target) {
Try(out = p->GetAttackDataForTarget(target);)
Try(out = &p->GetAttackDataForTarget(target);)
}
export bool CreatureLib_ExecutingAttack_IsCreatureTarget(ExecutingAttack* p, Creature* target) {
@ -25,7 +25,7 @@ export LearnedAttack* CreatureLib_ExecutingAttack_GetAttack(ExecutingAttack* p)
export uint8_t CreatureLib_TargetData_GetHit(ExecutingAttack::HitData*& out, ExecutingAttack::TargetData* p,
uint8_t hit) {
Try(out = p->GetHit(hit);)
Try(out = &p->GetHit(hit);)
}
export uint8_t CreatureLib_TargetData_GetNumberOfHits(ExecutingAttack::TargetData* p) { return p->GetNumberOfHits(); }
export bool CreatureLib_TargetData_IsHit(ExecutingAttack::TargetData* p) { return p->IsHit(); }

View File

@ -91,7 +91,7 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
HOOK(OnBeforeAttack, attack, attack);
for (auto& kv : attack->GetTargets()) {
HandleAttackForTarget(attack, kv.first, &kv.second);
HandleAttackForTarget(attack, kv.first, kv.second);
}
// TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up.
@ -99,7 +99,7 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target,
ExecutingAttack::TargetData* targetData) {
ExecutingAttack::TargetData& targetData) {
auto user = attack->GetUser();
AssertNotNull(user)
AssertNotNull(target)
@ -121,12 +121,12 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
return;
}
if (!targetData->IsHit()) {
if (!targetData.IsHit()) {
HOOK(OnAttackMiss, targetSource, attack, target);
return;
}
auto numHits = targetData->GetNumberOfHits();
auto numHits = targetData.GetNumberOfHits();
if (numHits == 0)
return;
auto attackData = attack->GetAttack()->GetAttack();
@ -140,17 +140,15 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
if (target->IsFainted()) {
break;
}
auto hit = targetData->GetHit(hitIndex);
AssertNotNull(hit)
auto hitType = hit->GetType();
auto& hit = targetData.GetHit(hitIndex);
auto hitType = hit.GetType();
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, &hitType);
auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes());
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.SetEffectiveness(effectiveness);
hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target, hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex));
hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex));
if (attackData->GetCategory() == Library::AttackCategory::Status) {
if (attackData->HasSecondaryEffect()) {
@ -167,10 +165,10 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
}
}
} else {
auto damage = hit->GetDamage();
auto damage = hit.GetDamage();
if (damage > target->GetCurrentHealth()) {
damage = target->GetCurrentHealth();
hit->SetDamage(damage);
hit.SetDamage(damage);
}
if (damage > 0) {
target->Damage(damage, DamageSource::AttackDamage);

View File

@ -15,7 +15,7 @@ namespace CreatureLib::Battling {
static void ExecuteAttackChoice(AttackTurnChoice* choice);
static void HandleAttackForTarget(ExecutingAttack* attack, Creature* target,
ExecutingAttack::TargetData* targetData);
ExecutingAttack::TargetData& targetData);
static void ExecuteSwitchChoice(SwitchTurnChoice* choice);
static void ExecuteFleeChoice(FleeTurnChoice* choice);

View File

@ -7,8 +7,8 @@ uint32_t DamageLibrary::GetDamage(ExecutingAttack* attack, Creature* target, uin
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 hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
auto bp = hit.GetBasePower();
auto statMod = GetStatModifier(attack, target, hitIndex);
HOOK(ModifyStatModifier, attack, attack, target, hitIndex, &statMod);
uint32_t damage = static_cast<uint32_t>((((levelMod * static_cast<float>(bp) * statMod) / 50) + 2) *
@ -31,7 +31,7 @@ float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target,
auto user = attack->GetUser();
AssertNotNull(user)
HOOK(ChangeDamageStatsUser, attack, attack, target, hitIndex, &user);
auto hit = attack->GetAttackDataForTarget(target)->GetHit(hitIndex);
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
Library::Statistic offensiveStat;
Library::Statistic defensiveStat;
if (attack->GetAttack()->GetAttack()->GetCategory() == Library::AttackCategory::Physical) {
@ -42,9 +42,9 @@ float DamageLibrary::GetStatModifier(ExecutingAttack* attack, Creature* target,
defensiveStat = Library::Statistic::MagicalDefense;
}
auto bypassDefensive = hit->IsCritical() && target->GetStatBoost(defensiveStat) > 0;
auto bypassDefensive = hit.IsCritical() && target->GetStatBoost(defensiveStat) > 0;
HOOK(BypassDefensiveStat, attack, attack, target, hitIndex, &bypassDefensive);
auto bypassOffensive = hit->IsCritical() && user->GetStatBoost(offensiveStat) < 0;
auto bypassOffensive = hit.IsCritical() && user->GetStatBoost(offensiveStat) < 0;
HOOK(BypassOffensiveStat, attack, attack, target, hitIndex, &bypassOffensive);
float offensiveValue;
@ -68,9 +68,8 @@ float DamageLibrary::GetDamageModifier(ExecutingAttack* attack, Creature* target
AssertNotNull(attack)
AssertNotNull(target)
float mod = 1;
auto hit = attack->GetAttackDataForTarget(target)->GetHit(hitIndex);
AssertNotNull(hit)
mod *= hit->GetEffectiveness();
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
mod *= hit.GetEffectiveness();
HOOK(ModifyDamageModifier, attack, attack, target, hitIndex, &mod);
return mod;
}

View File

@ -44,8 +44,9 @@ namespace CreatureLib::Battling {
public:
explicit TargetData(uint8_t numberOfHits) : _hits(numberOfHits) { _hits.Resize(numberOfHits, HitData()); }
TargetData() = default;
TargetData& operator=(const TargetData&) = delete;
HitData* GetHit(uint8_t index) { return &_hits[index]; }
HitData& GetHit(uint8_t index) { return _hits[index]; }
uint8_t GetNumberOfHits() const noexcept { return _hits.Count(); }
@ -71,7 +72,7 @@ namespace CreatureLib::Battling {
virtual ~ExecutingAttack() noexcept { delete _script; };
TargetData* GetAttackDataForTarget(Creature* creature) { return &_targets[creature]; }
TargetData& GetAttackDataForTarget(Creature* creature) { return _targets[creature]; }
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.Has(creature); }