Adds optional optimization to history helper functions to limit the max amount of turns searched through.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-10-24 11:10:30 +02:00
parent 7354c835e6
commit 47c3429295
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 19 additions and 8 deletions

View File

@ -1,6 +1,7 @@
#ifndef CREATURELIB_HISTORYHOLDER_HPP #ifndef CREATURELIB_HISTORYHOLDER_HPP
#define CREATURELIB_HISTORYHOLDER_HPP #define CREATURELIB_HISTORYHOLDER_HPP
#include <utility>
#include "../../Library/Exceptions/CreatureException.hpp" #include "../../Library/Exceptions/CreatureException.hpp"
#include "HistoryElements/DamageHistory.hpp" #include "HistoryElements/DamageHistory.hpp"
#include "HistoryElements/HistoryElement.hpp" #include "HistoryElements/HistoryElement.hpp"
@ -11,9 +12,10 @@ concept HistoryElementType = std::is_base_of<CreatureLib::Battling::HistoryEleme
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class HistoryHolder { class HistoryHolder {
HistoryElement* _top = nullptr; HistoryElement* _top = nullptr;
std::function<u32()> _getTurnNumber;
public: public:
HistoryHolder() {} HistoryHolder(std::function<u32()> getTurnNumber) : _getTurnNumber(std::move(getTurnNumber)) {}
HistoryHolder(const HistoryHolder&) = delete; HistoryHolder(const HistoryHolder&) = delete;
HistoryHolder& operator=(const HistoryHolder&) = delete; HistoryHolder& operator=(const HistoryHolder&) = delete;
@ -27,21 +29,30 @@ namespace CreatureLib::Battling {
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement> GetTopElement() const noexcept { return _top; } [[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement> GetTopElement() const noexcept { return _top; }
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement> GetLastUsedAttack() const noexcept { [[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement>
const auto* c = _top; GetLastUsedAttack(u32 maxDepth = 0) const noexcept {
while (c != nullptr) { ArbUt::OptionalBorrowedPtr<const HistoryElement> c = _top;
if (c->GetKind() == HistoryElementKind::AttackUse) { auto turn = _getTurnNumber();
while (c.HasValue()) {
if (maxDepth != 0 && turn - c.GetValue()->GetTurnNumber() > maxDepth - 1) {
break;
}
if (c.GetValue()->GetKind() == HistoryElementKind::AttackUse) {
return c; return c;
} }
c = c->GetPrevious(); c = c.GetValue()->GetPrevious();
} }
return {}; return {};
} }
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement> [[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement>
GetLastDamageOnTarget(const ArbUt::BorrowedPtr<const Creature>& creature) const noexcept { GetLastDamageOnTarget(const ArbUt::BorrowedPtr<const Creature>& creature, u32 maxDepth = 0) const noexcept {
ArbUt::OptionalBorrowedPtr<const HistoryElement> c = _top; ArbUt::OptionalBorrowedPtr<const HistoryElement> c = _top;
auto turn = _getTurnNumber();
while (c.HasValue()) { while (c.HasValue()) {
if (maxDepth != 0 && turn - c.GetValue()->GetTurnNumber() > maxDepth - 1) {
break;
}
if (c.GetValue()->GetKind() == HistoryElementKind::Damage && if (c.GetValue()->GetKind() == HistoryElementKind::Damage &&
c.ForceAs<const DamageHistory>().GetValue()->GetTarget() == creature) { c.ForceAs<const DamageHistory>().GetValue()->GetTarget() == creature) {
return c; return c;

View File

@ -26,7 +26,7 @@ namespace CreatureLib::Battling {
bool _hasEnded = false; bool _hasEnded = false;
BattleResult _battleResult = BattleResult::Empty(); BattleResult _battleResult = BattleResult::Empty();
EventHook _eventHook; EventHook _eventHook;
ArbUt::UniquePtr<HistoryHolder> _historyHolder = new HistoryHolder(); ArbUt::UniquePtr<HistoryHolder> _historyHolder = new HistoryHolder([this]() { return GetCurrentTurn(); });
uint32_t _currentTurn = 0; uint32_t _currentTurn = 0;