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

This commit is contained in:
Deukhoofd 2020-06-04 21:58:28 +02:00
parent 657d646fda
commit 57e8595bdf
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
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); HOOK_LOCAL(OnBeforeAttack, attack, &attack);
for (auto& t : attack.GetTargets()) { for (uint8_t i = 0; i < attack.GetTargetCount(); i++) {
HandleAttackForTarget(&attack, t.GetRaw()); 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(); auto user = attack->GetUser();
AssertNotNull(user) AssertNotNull(user)
AssertNotNull(target) AssertNotNull(target)
ScriptSource* targetSource = target; auto targetSource = target;
ScriptSource* userSource = attack; auto userSource = attack;
bool fail = false; bool fail = false;
HOOK(FailIncomingAttack, targetSource, attack, target, &fail); HOOK(FailIncomingAttack, targetSource, attack, target.GetRaw(), &fail);
if (fail) { if (fail) {
// TODO: Fail handling. // TODO: Fail handling.
return; return;
} }
bool invulnerable = false; bool invulnerable = false;
HOOK(IsInvulnerable, targetSource, attack, target, &invulnerable); HOOK(IsInvulnerable, targetSource, attack, target.GetRaw(), &invulnerable);
if (invulnerable) { if (invulnerable) {
// TODO: We should probably do something when a target is invulnerable. // TODO: We should probably do something when a target is invulnerable.
return; return;
@ -118,7 +118,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
auto numberOfHits = attack->GetNumberOfHits(); auto numberOfHits = attack->GetNumberOfHits();
if (numberOfHits == 0) { if (numberOfHits == 0) {
HOOK(OnAttackMiss, targetSource, attack, target); HOOK(OnAttackMiss, targetSource, attack, target.GetRaw());
return; return;
} }
@ -126,7 +126,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
auto library = user->GetBattle()->GetLibrary(); auto library = user->GetBattle()->GetLibrary();
AssertNotNull(library) AssertNotNull(library)
auto& dmgLibrary = library->GetDamageLibrary(); auto& dmgLibrary = library->GetDamageLibrary();
auto hitIterator = attack->GetTargetIteratorBegin(target); auto hitIterator = attack->GetTargetIteratorBegin(target.GetRaw());
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) { for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
if (user->IsFainted()) { if (user->IsFainted()) {
break; break;
@ -136,14 +136,14 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
} }
auto& hit = hitIterator[hitIndex]; auto& hit = hitIterator[hitIndex];
uint8_t hitType = hit.GetType(); uint8_t hitType = hit.GetType();
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, &hitType); HOOK(ChangeAttackType, targetSource, attack, target.GetRaw(), hitIndex, &hitType);
hit.SetType(hitType); hit.SetType(hitType);
auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()); 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.SetEffectiveness(effectiveness);
hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target, hitIndex)); hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target.GetRaw(), hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex, hit)); hit.SetBasePower(dmgLibrary->GetBasePower(attack, target.GetRaw(), hitIndex, hit));
hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex, hit)); hit.SetDamage(dmgLibrary->GetDamage(attack, target.GetRaw(), hitIndex, hit));
if (attackData->GetCategory() == Library::AttackCategory::Status) { if (attackData->GetCategory() == Library::AttackCategory::Status) {
if (attackData->HasSecondaryEffect()) { if (attackData->HasSecondaryEffect()) {
@ -153,10 +153,10 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
hasSecondaryEffect = true; hasSecondaryEffect = true;
} else { } else {
hasSecondaryEffect = hasSecondaryEffect =
user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target); user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw());
} }
if (hasSecondaryEffect) { if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex); HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
} }
} }
} else { } else {
@ -170,18 +170,18 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe
if (attackData->HasSecondaryEffect()) { if (attackData->HasSecondaryEffect()) {
bool preventSecondary = false; bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSource, attack, target, hitIndex, &preventSecondary); HOOK(PreventSecondaryEffects, targetSource, attack, target.GetRaw(), hitIndex, &preventSecondary);
if (!preventSecondary) { if (!preventSecondary) {
auto& effect = attackData->GetSecondaryEffect(); auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect; bool hasSecondaryEffect;
if (effect->GetChance() == -1) { if (effect->GetChance() == -1) {
hasSecondaryEffect = true; hasSecondaryEffect = true;
} else { } else {
hasSecondaryEffect = hasSecondaryEffect = user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(),
user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target); attack, target.GetRaw());
} }
if (hasSecondaryEffect) { 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()) { 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 ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice);
static void ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> 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 ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice);
static void ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice); static void ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice);

View File

@ -36,7 +36,8 @@ namespace CreatureLib::Battling {
}; };
private: private:
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets; const ArbUt::BorrowedPtr<Creature>* _targets;
uint8_t _targetCount;
uint8_t _numberHits; uint8_t _numberHits;
std::unique_ptr<HitData[]> _hits; std::unique_ptr<HitData[]> _hits;
ArbUt::BorrowedPtr<Creature> _user; ArbUt::BorrowedPtr<Creature> _user;
@ -47,13 +48,10 @@ namespace CreatureLib::Battling {
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits,
ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<LearnedAttack>& attack, ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<LearnedAttack>& attack,
const std::unique_ptr<Script>& script) const std::unique_ptr<Script>& script)
: _targets(targets.Count()), _numberHits(numberHits), : _targets(targets.RawData()), _targetCount(targets.Count()), _numberHits(numberHits),
_hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) { _hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) {
AssertNotNull(user) AssertNotNull(user)
AssertNotNull(attack) AssertNotNull(attack)
for (auto target : targets) {
_targets.Append(target);
}
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr. // Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
_script.swap(const_cast<std::unique_ptr<Script>&>(script)); _script.swap(const_cast<std::unique_ptr<Script>&>(script));
} }
@ -63,7 +61,7 @@ namespace CreatureLib::Battling {
virtual ~ExecutingAttack() noexcept = default; virtual ~ExecutingAttack() noexcept = default;
HitData& GetHitData(Creature* creature, uint8_t hit) { HitData& GetHitData(Creature* creature, uint8_t hit) {
for (size_t i = 0; i < _targets.Count(); i++) { for (uint8_t i = 0; i < _targetCount; i++) {
if (_targets[i] == creature) { if (_targets[i] == creature) {
return _hits[i * _numberHits + hit]; return _hits[i * _numberHits + hit];
} }
@ -72,7 +70,7 @@ namespace CreatureLib::Battling {
} }
HitData* GetTargetIteratorBegin(Creature* creature) { HitData* GetTargetIteratorBegin(Creature* creature) {
for (size_t i = 0; i < _targets.Count(); i++) { for (uint8_t i = 0; i < _targetCount; i++) {
if (_targets[i] == creature) { if (_targets[i] == creature) {
return &_hits[i * _numberHits * sizeof(HitData)]; return &_hits[i * _numberHits * sizeof(HitData)];
} }
@ -80,13 +78,22 @@ namespace CreatureLib::Battling {
throw CreatureException("Invalid target requested."); throw CreatureException("Invalid target requested.");
} }
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.IndexOf(creature) != (size_t)-1; } bool IsCreatureTarget(Creature* creature) noexcept {
const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() noexcept { return _targets; } for (uint8_t i = 0; i < _targetCount; i++) {
uint8_t GetNumberOfHits() const noexcept { return _numberHits; } if (_targets[i] == creature) {
return true;
}
}
return false;
}
ArbUt::BorrowedPtr<Creature> GetUser() noexcept { return _user; } inline const uint8_t GetTargetCount() const noexcept { return _targetCount; }
inline const ArbUt::BorrowedPtr<Creature>* GetTargets() const noexcept { return _targets; }
inline uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; } inline const ArbUt::BorrowedPtr<Creature>& GetUser() noexcept { return _user; }
inline const ArbUt::BorrowedPtr<LearnedAttack>& GetAttack() noexcept { return _attack; }
size_t ScriptCount() const override { return _user->ScriptCount() + 1; } size_t ScriptCount() const override { return _user->ScriptCount() + 1; }
protected: protected: