Make ExecutingAttack creature list a raw C array, as this is a hot spot and could use some optimization.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-06-04 21:58:28 +02:00
parent 657d646fda
commit 57e8595bdf
3 changed files with 41 additions and 34 deletions

View File

@@ -89,28 +89,28 @@ void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> 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<Creature>& 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());
}
}

View File

@@ -14,7 +14,7 @@ namespace CreatureLib::Battling {
static void ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice);
static void ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choice);
static void HandleAttackForTarget(ExecutingAttack* attack, Creature* target);
static void HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target);
static void ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice);
static void ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice);