diff --git a/src/Battling/History/HistoryHolder.hpp b/src/Battling/History/HistoryHolder.hpp index d1c69d1..d0e5eb9 100644 --- a/src/Battling/History/HistoryHolder.hpp +++ b/src/Battling/History/HistoryHolder.hpp @@ -1,6 +1,7 @@ #ifndef CREATURELIB_HISTORYHOLDER_HPP #define CREATURELIB_HISTORYHOLDER_HPP +#include #include "../../Library/Exceptions/CreatureException.hpp" #include "HistoryElements/DamageHistory.hpp" #include "HistoryElements/HistoryElement.hpp" @@ -11,9 +12,10 @@ concept HistoryElementType = std::is_base_of _getTurnNumber; public: - HistoryHolder() {} + HistoryHolder(std::function getTurnNumber) : _getTurnNumber(std::move(getTurnNumber)) {} HistoryHolder(const HistoryHolder&) = delete; HistoryHolder& operator=(const HistoryHolder&) = delete; @@ -27,21 +29,30 @@ namespace CreatureLib::Battling { [[nodiscard]] ArbUt::OptionalBorrowedPtr GetTopElement() const noexcept { return _top; } - [[nodiscard]] ArbUt::OptionalBorrowedPtr GetLastUsedAttack() const noexcept { - const auto* c = _top; - while (c != nullptr) { - if (c->GetKind() == HistoryElementKind::AttackUse) { + [[nodiscard]] ArbUt::OptionalBorrowedPtr + GetLastUsedAttack(u32 maxDepth = 0) const noexcept { + ArbUt::OptionalBorrowedPtr c = _top; + auto turn = _getTurnNumber(); + while (c.HasValue()) { + if (maxDepth != 0 && turn - c.GetValue()->GetTurnNumber() > maxDepth - 1) { + break; + } + if (c.GetValue()->GetKind() == HistoryElementKind::AttackUse) { return c; } - c = c->GetPrevious(); + c = c.GetValue()->GetPrevious(); } return {}; } [[nodiscard]] ArbUt::OptionalBorrowedPtr - GetLastDamageOnTarget(const ArbUt::BorrowedPtr& creature) const noexcept { + GetLastDamageOnTarget(const ArbUt::BorrowedPtr& creature, u32 maxDepth = 0) const noexcept { ArbUt::OptionalBorrowedPtr c = _top; + auto turn = _getTurnNumber(); while (c.HasValue()) { + if (maxDepth != 0 && turn - c.GetValue()->GetTurnNumber() > maxDepth - 1) { + break; + } if (c.GetValue()->GetKind() == HistoryElementKind::Damage && c.ForceAs().GetValue()->GetTarget() == creature) { return c; diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index a7608af..f325da5 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -26,7 +26,7 @@ namespace CreatureLib::Battling { bool _hasEnded = false; BattleResult _battleResult = BattleResult::Empty(); EventHook _eventHook; - ArbUt::UniquePtr _historyHolder = new HistoryHolder(); + ArbUt::UniquePtr _historyHolder = new HistoryHolder([this]() { return GetCurrentTurn(); }); uint32_t _currentTurn = 0;