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);
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);

View File

@ -36,7 +36,8 @@ namespace CreatureLib::Battling {
};
private:
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets;
const ArbUt::BorrowedPtr<Creature>* _targets;
uint8_t _targetCount;
uint8_t _numberHits;
std::unique_ptr<HitData[]> _hits;
ArbUt::BorrowedPtr<Creature> _user;
@ -47,13 +48,10 @@ namespace CreatureLib::Battling {
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits,
ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<LearnedAttack>& attack,
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) {
AssertNotNull(user)
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.
_script.swap(const_cast<std::unique_ptr<Script>&>(script));
}
@ -63,7 +61,7 @@ namespace CreatureLib::Battling {
virtual ~ExecutingAttack() noexcept = default;
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) {
return _hits[i * _numberHits + hit];
}
@ -72,7 +70,7 @@ namespace CreatureLib::Battling {
}
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) {
return &_hits[i * _numberHits * sizeof(HitData)];
}
@ -80,13 +78,22 @@ namespace CreatureLib::Battling {
throw CreatureException("Invalid target requested.");
}
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.IndexOf(creature) != (size_t)-1; }
const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() noexcept { return _targets; }
uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
bool IsCreatureTarget(Creature* creature) noexcept {
for (uint8_t i = 0; i < _targetCount; i++) {
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; }
protected: