From a570e90f672bd15ca1be92f76587971edd24e5b0 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 19 Nov 2021 13:08:34 +0100 Subject: [PATCH] Adds GetLastUsedAttackOnTarget helper to BattleHistory --- src/Battling/History/HistoryElementKind.hpp | 2 + .../History/HistoryElements/DamageHistory.hpp | 1 + .../HistoryElements/HistoryElement.hpp | 2 + src/Battling/History/HistoryHolder.cpp | 56 +++++++++++++++++++ src/Battling/History/HistoryHolder.hpp | 36 ++---------- 5 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 src/Battling/History/HistoryHolder.cpp diff --git a/src/Battling/History/HistoryElementKind.hpp b/src/Battling/History/HistoryElementKind.hpp index 3fcdd60..c086cdc 100644 --- a/src/Battling/History/HistoryElementKind.hpp +++ b/src/Battling/History/HistoryElementKind.hpp @@ -1,5 +1,7 @@ #ifndef CREATURELIB_HISTORYELEMENTKIND_HPP #define CREATURELIB_HISTORYELEMENTKIND_HPP +#include +#include ENUM(HistoryElementKind, uint8_t, AttackUse, Damage) #endif // CREATURELIB_HISTORYELEMENTKIND_HPP diff --git a/src/Battling/History/HistoryElements/DamageHistory.hpp b/src/Battling/History/HistoryElements/DamageHistory.hpp index 637e2aa..33a6a91 100644 --- a/src/Battling/History/HistoryElements/DamageHistory.hpp +++ b/src/Battling/History/HistoryElements/DamageHistory.hpp @@ -1,5 +1,6 @@ #ifndef CREATURELIB_DAMAGEHISTORY_HPP #define CREATURELIB_DAMAGEHISTORY_HPP +#include "../../Models/Creature.hpp" #include "../../Models/DamageSource.hpp" #include "HistoryElement.hpp" diff --git a/src/Battling/History/HistoryElements/HistoryElement.hpp b/src/Battling/History/HistoryElements/HistoryElement.hpp index d89ec5f..c6e07d4 100644 --- a/src/Battling/History/HistoryElements/HistoryElement.hpp +++ b/src/Battling/History/HistoryElements/HistoryElement.hpp @@ -1,6 +1,8 @@ #ifndef CREATURELIB_HISTORYELEMENT_HPP #define CREATURELIB_HISTORYELEMENT_HPP +#include +#include "../../../Defines.hpp" #include "../HistoryElementKind.hpp" namespace CreatureLib::Battling { diff --git a/src/Battling/History/HistoryHolder.cpp b/src/Battling/History/HistoryHolder.cpp new file mode 100644 index 0000000..0e3d850 --- /dev/null +++ b/src/Battling/History/HistoryHolder.cpp @@ -0,0 +1,56 @@ +#include "HistoryHolder.hpp" +#include "HistoryElementKind.hpp" +#include "HistoryElements/AttackUseHistory.hpp" + +namespace CreatureLib::Battling { + ArbUt::OptionalBorrowedPtr HistoryHolder::GetLastUsedAttack(u32 maxDepth) 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.GetValue()->GetPrevious(); + } + return {}; + } + + ArbUt::OptionalBorrowedPtr + HistoryHolder::GetLastUsedAttackOnTarget(Creature* target, u32 maxDepth) 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) { + if (c.ForceAs().GetValue()->GetAttack()->GetTargets().Contains(target)) { + return c; + } + } + c = c.GetValue()->GetPrevious(); + } + return {}; + } + + ArbUt::OptionalBorrowedPtr + HistoryHolder::GetLastDamageOnTarget(const ArbUt::BorrowedPtr& creature, + u32 maxDepth) 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; + } + c = c.GetValue()->GetPrevious(); + } + return {}; + } +} \ No newline at end of file diff --git a/src/Battling/History/HistoryHolder.hpp b/src/Battling/History/HistoryHolder.hpp index d0e5eb9..d3cfa55 100644 --- a/src/Battling/History/HistoryHolder.hpp +++ b/src/Battling/History/HistoryHolder.hpp @@ -3,8 +3,8 @@ #include #include "../../Library/Exceptions/CreatureException.hpp" -#include "HistoryElements/DamageHistory.hpp" #include "HistoryElements/HistoryElement.hpp" +#include "HistoryElements/DamageHistory.hpp" template concept HistoryElementType = std::is_base_of::value; @@ -30,37 +30,13 @@ namespace CreatureLib::Battling { [[nodiscard]] ArbUt::OptionalBorrowedPtr GetTopElement() const noexcept { return _top; } [[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.GetValue()->GetPrevious(); - } - return {}; - } + GetLastUsedAttack(u32 maxDepth = 0) const noexcept; [[nodiscard]] ArbUt::OptionalBorrowedPtr - 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; - } - c = c.GetValue()->GetPrevious(); - } - return {}; - } + GetLastUsedAttackOnTarget(Creature* target, u32 maxDepth = 0) const noexcept; + + [[nodiscard]] ArbUt::OptionalBorrowedPtr + GetLastDamageOnTarget(const ArbUt::BorrowedPtr& creature, u32 maxDepth = 0) const noexcept; }; }