From 57e8595bdfa43cbfdc1d61ea0d8e82b8e173e5dd Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 4 Jun 2020 21:58:28 +0200 Subject: [PATCH] Make ExecutingAttack creature list a raw C array, as this is a hot spot and could use some optimization. --- src/Battling/Flow/TurnHandler.cpp | 42 ++++++++++++------------- src/Battling/Flow/TurnHandler.hpp | 2 +- src/Battling/Models/ExecutingAttack.hpp | 31 +++++++++++------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index c4b0d76..c4b16c9 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -89,28 +89,28 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr choic } HOOK_LOCAL(OnBeforeAttack, attack, &attack); - for (auto& t : attack.GetTargets()) { - HandleAttackForTarget(&attack, t.GetRaw()); + for (uint8_t i = 0; i < attack.GetTargetCount(); i++) { + HandleAttackForTarget(&attack, attack.GetTargets()[0]); } } -void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target) { +void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr& target) { auto user = attack->GetUser(); AssertNotNull(user) AssertNotNull(target) - ScriptSource* targetSource = target; - ScriptSource* userSource = attack; + auto targetSource = target; + auto userSource = attack; bool fail = false; - HOOK(FailIncomingAttack, targetSource, attack, target, &fail); + HOOK(FailIncomingAttack, targetSource, attack, target.GetRaw(), &fail); if (fail) { // TODO: Fail handling. return; } bool invulnerable = false; - HOOK(IsInvulnerable, targetSource, attack, target, &invulnerable); + HOOK(IsInvulnerable, targetSource, attack, target.GetRaw(), &invulnerable); if (invulnerable) { // TODO: We should probably do something when a target is invulnerable. return; @@ -118,7 +118,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe auto numberOfHits = attack->GetNumberOfHits(); if (numberOfHits == 0) { - HOOK(OnAttackMiss, targetSource, attack, target); + HOOK(OnAttackMiss, targetSource, attack, target.GetRaw()); return; } @@ -126,7 +126,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe auto library = user->GetBattle()->GetLibrary(); AssertNotNull(library) auto& dmgLibrary = library->GetDamageLibrary(); - auto hitIterator = attack->GetTargetIteratorBegin(target); + auto hitIterator = attack->GetTargetIteratorBegin(target.GetRaw()); for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) { if (user->IsFainted()) { break; @@ -136,14 +136,14 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe } auto& hit = hitIterator[hitIndex]; uint8_t hitType = hit.GetType(); - HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, &hitType); + HOOK(ChangeAttackType, targetSource, attack, target.GetRaw(), hitIndex, &hitType); hit.SetType(hitType); auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()); - HOOK(ChangeEffectiveness, attack, attack, target, hitIndex, &effectiveness) + HOOK(ChangeEffectiveness, attack, attack, target.GetRaw(), hitIndex, &effectiveness) hit.SetEffectiveness(effectiveness); - hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target, hitIndex)); - hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex, hit)); - hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex, hit)); + hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target.GetRaw(), hitIndex)); + hit.SetBasePower(dmgLibrary->GetBasePower(attack, target.GetRaw(), hitIndex, hit)); + hit.SetDamage(dmgLibrary->GetDamage(attack, target.GetRaw(), hitIndex, hit)); if (attackData->GetCategory() == Library::AttackCategory::Status) { if (attackData->HasSecondaryEffect()) { @@ -153,10 +153,10 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe hasSecondaryEffect = true; } else { hasSecondaryEffect = - user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target); + user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw()); } if (hasSecondaryEffect) { - HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex); + HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex); } } } else { @@ -170,18 +170,18 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe if (attackData->HasSecondaryEffect()) { bool preventSecondary = false; - HOOK(PreventSecondaryEffects, targetSource, attack, target, hitIndex, &preventSecondary); + HOOK(PreventSecondaryEffects, targetSource, attack, target.GetRaw(), hitIndex, &preventSecondary); if (!preventSecondary) { auto& effect = attackData->GetSecondaryEffect(); bool hasSecondaryEffect; if (effect->GetChance() == -1) { hasSecondaryEffect = true; } else { - hasSecondaryEffect = - user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target); + hasSecondaryEffect = user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), + attack, target.GetRaw()); } if (hasSecondaryEffect) { - HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex); + HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex); } } } @@ -190,7 +190,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe } if (!user->IsFainted()) { - HOOK(OnAfterHits, userSource, attack, target); + HOOK(OnAfterHits, userSource, attack, target.GetRaw()); } } diff --git a/src/Battling/Flow/TurnHandler.hpp b/src/Battling/Flow/TurnHandler.hpp index 589d2a2..26175b6 100644 --- a/src/Battling/Flow/TurnHandler.hpp +++ b/src/Battling/Flow/TurnHandler.hpp @@ -14,7 +14,7 @@ namespace CreatureLib::Battling { static void ExecuteChoice(ArbUt::BorrowedPtr choice); static void ExecuteAttackChoice(ArbUt::BorrowedPtr choice); - static void HandleAttackForTarget(ExecutingAttack* attack, Creature* target); + static void HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr& target); static void ExecuteSwitchChoice(ArbUt::BorrowedPtr choice); static void ExecuteFleeChoice(ArbUt::BorrowedPtr choice); diff --git a/src/Battling/Models/ExecutingAttack.hpp b/src/Battling/Models/ExecutingAttack.hpp index a8b8f1f..e72e820 100644 --- a/src/Battling/Models/ExecutingAttack.hpp +++ b/src/Battling/Models/ExecutingAttack.hpp @@ -36,7 +36,8 @@ namespace CreatureLib::Battling { }; private: - ArbUt::List> _targets; + const ArbUt::BorrowedPtr* _targets; + uint8_t _targetCount; uint8_t _numberHits; std::unique_ptr _hits; ArbUt::BorrowedPtr _user; @@ -47,13 +48,10 @@ namespace CreatureLib::Battling { ExecutingAttack(const ArbUt::List>& targets, uint8_t numberHits, ArbUt::BorrowedPtr user, const ArbUt::BorrowedPtr& attack, const std::unique_ptr