Adds GetLastUsedAttackOnTarget helper to BattleHistory
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-11-19 13:08:34 +01:00
parent a89d79f8bc
commit a570e90f67
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 67 additions and 30 deletions

View File

@ -1,5 +1,7 @@
#ifndef CREATURELIB_HISTORYELEMENTKIND_HPP
#define CREATURELIB_HISTORYELEMENTKIND_HPP
#include <Arbutils/Enum.hpp>
#include <cstdint>
ENUM(HistoryElementKind, uint8_t, AttackUse, Damage)
#endif // CREATURELIB_HISTORYELEMENTKIND_HPP

View File

@ -1,5 +1,6 @@
#ifndef CREATURELIB_DAMAGEHISTORY_HPP
#define CREATURELIB_DAMAGEHISTORY_HPP
#include "../../Models/Creature.hpp"
#include "../../Models/DamageSource.hpp"
#include "HistoryElement.hpp"

View File

@ -1,6 +1,8 @@
#ifndef CREATURELIB_HISTORYELEMENT_HPP
#define CREATURELIB_HISTORYELEMENT_HPP
#include <Arbutils/Memory/Memory.hpp>
#include "../../../Defines.hpp"
#include "../HistoryElementKind.hpp"
namespace CreatureLib::Battling {

View File

@ -0,0 +1,56 @@
#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* 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 {};
}
}

View File

@ -3,8 +3,8 @@
#include <utility>
#include "../../Library/Exceptions/CreatureException.hpp"
#include "HistoryElements/DamageHistory.hpp"
#include "HistoryElements/HistoryElement.hpp"
#include "HistoryElements/DamageHistory.hpp"
template <class T>
concept HistoryElementType = std::is_base_of<CreatureLib::Battling::HistoryElement, T>::value;
@ -30,37 +30,13 @@ namespace CreatureLib::Battling {
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement> GetTopElement() const noexcept { return _top; }
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement>
GetLastUsedAttack(u32 maxDepth = 0) 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 {};
}
GetLastUsedAttack(u32 maxDepth = 0) const noexcept;
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement>
GetLastDamageOnTarget(const ArbUt::BorrowedPtr<const Creature>& creature, u32 maxDepth = 0) 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 {};
}
GetLastUsedAttackOnTarget(Creature* target, u32 maxDepth = 0) const noexcept;
[[nodiscard]] ArbUt::OptionalBorrowedPtr<const HistoryElement>
GetLastDamageOnTarget(const ArbUt::BorrowedPtr<const Creature>& creature, u32 maxDepth = 0) const noexcept;
};
}