CreatureLib/src/Battling/History/HistoryHolder.cpp

56 lines
2.2 KiB
C++

#include "HistoryHolder.hpp"
#include "HistoryElementKind.hpp"
#include "HistoryElements/AttackUseHistory.hpp"
namespace CreatureLib::Battling {
ArbUt::OptionalBorrowedPtr<const HistoryElement> HistoryHolder::GetLastUsedAttack(u32 maxDepth) const noexcept {
ArbUt::OptionalBorrowedPtr<const HistoryElement> 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<const HistoryElement>
HistoryHolder::GetLastUsedAttackOnTarget(Creature* non_null target, u32 maxDepth) const noexcept {
ArbUt::OptionalBorrowedPtr<const HistoryElement> 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<const AttackUseHistory>().GetValue()->GetAttack()->GetTargets().Contains(target)) {
return c;
}
}
c = c.GetValue()->GetPrevious();
}
return {};
}
ArbUt::OptionalBorrowedPtr<const HistoryElement>
HistoryHolder::GetLastDamageOnTarget(const ArbUt::BorrowedPtr<const Creature>& creature,
u32 maxDepth) const noexcept {
ArbUt::OptionalBorrowedPtr<const HistoryElement> 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<const DamageHistory>().GetValue()->GetTarget() == creature) {
return c;
}
c = c.GetValue()->GetPrevious();
}
return {};
}
}