Rework LearnableAttacks to use ArbUt::BorrowedPtr
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-11 19:09:58 +02:00
parent 7ac63839b8
commit 2c4c0f2277
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 12 additions and 10 deletions

View File

@ -13,7 +13,7 @@ export void CreatureLib_LearnableAttacks_AddLevelAttack(LearnableAttacks* p, uin
} }
export const AttackData* const* CreatureLib_LearnableAttacks_GetAttacksForLevel(LearnableAttacks* p, uint8_t level) { export const AttackData* const* CreatureLib_LearnableAttacks_GetAttacksForLevel(LearnableAttacks* p, uint8_t level) {
return p->GetAttacksForLevel(level).RawData(); return reinterpret_cast<const AttackData* const*>(p->GetAttacksForLevel(level).RawData());
} }
export bool CreatureLib_LearnableAttacks_HasAttacksForLevel(LearnableAttacks* p, uint8_t level) { export bool CreatureLib_LearnableAttacks_HasAttacksForLevel(LearnableAttacks* p, uint8_t level) {
return p->HasAttacksForLevel(level); return p->HasAttacksForLevel(level);
@ -27,5 +27,5 @@ export size_t CreatureLib_LearnableAttacks_GetDistinctLevelAttacksCount(Learnabl
} }
export const AttackData* const* CreatureLib_LearnableAttacks_GetDistinctLevelAttacks(LearnableAttacks* p) { export const AttackData* const* CreatureLib_LearnableAttacks_GetDistinctLevelAttacks(LearnableAttacks* p) {
return p->GetDistinctLevelAttacks().RawData(); return reinterpret_cast<const AttackData* const*>(p->GetDistinctLevelAttacks().RawData());
} }

View File

@ -2,7 +2,7 @@
using namespace CreatureLib::Library; using namespace CreatureLib::Library;
void LearnableAttacks::AddLevelAttack(uint8_t level, const AttackData* attack) { void LearnableAttacks::AddLevelAttack(uint8_t level, ArbUt::BorrowedPtr<const AttackData> attack) {
if (_learnedByLevel.Has(level)) { if (_learnedByLevel.Has(level)) {
_learnedByLevel[level].Append(attack); _learnedByLevel[level].Append(attack);
} else { } else {

View File

@ -4,6 +4,7 @@
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
#include <Arbutils/Collections/Dictionary.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp> #include <Arbutils/Collections/List.hpp>
#include <Arbutils/Memory/BorrowedPtr.hpp>
#include <Arbutils/Random.hpp> #include <Arbutils/Random.hpp>
#include <unordered_map> #include <unordered_map>
#include "../Attacks/AttackData.hpp" #include "../Attacks/AttackData.hpp"
@ -11,30 +12,31 @@
namespace CreatureLib::Library { namespace CreatureLib::Library {
class LearnableAttacks { class LearnableAttacks {
protected: protected:
ArbUt::Dictionary<uint8_t, ArbUt::List<const AttackData*>> _learnedByLevel; ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>> _learnedByLevel;
ArbUt::List<const AttackData*> _distinctLevelAttacks; ArbUt::List<ArbUt::BorrowedPtr<const AttackData>> _distinctLevelAttacks;
public: public:
explicit LearnableAttacks(size_t levelAttackCapacity) explicit LearnableAttacks(size_t levelAttackCapacity)
: _learnedByLevel(ArbUt::Dictionary<uint8_t, ArbUt::List<const AttackData*>>(levelAttackCapacity)) {} : _learnedByLevel(
ArbUt::Dictionary<uint8_t, ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>>(levelAttackCapacity)) {}
virtual ~LearnableAttacks() = default; virtual ~LearnableAttacks() = default;
void AddLevelAttack(uint8_t level, const AttackData* attack); void AddLevelAttack(uint8_t level, ArbUt::BorrowedPtr<const AttackData> attack);
inline bool HasAttacksForLevel(uint8_t level) const noexcept { return _learnedByLevel.Has(level); } inline bool HasAttacksForLevel(uint8_t level) const noexcept { return _learnedByLevel.Has(level); }
inline const ArbUt::List<const AttackData*>& GetAttacksForLevel(uint8_t level) const { inline const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetAttacksForLevel(uint8_t level) const {
if (!_learnedByLevel.Has(level)) { if (!_learnedByLevel.Has(level)) {
THROW_CREATURE("No attacks found for level " << (uint32_t)level << "."); THROW_CREATURE("No attacks found for level " << (uint32_t)level << ".");
} }
return _learnedByLevel.Get(level); return _learnedByLevel.Get(level);
} }
inline const ArbUt::List<const AttackData*>& GetDistinctLevelAttacks() const noexcept { inline const ArbUt::List<ArbUt::BorrowedPtr<const AttackData>>& GetDistinctLevelAttacks() const noexcept {
return _distinctLevelAttacks; return _distinctLevelAttacks;
} }
virtual const AttackData* GetRandomAttack(ArbUt::Random& rand) const { virtual ArbUt::BorrowedPtr<const AttackData> GetRandomAttack(ArbUt::Random& rand) const {
if (_distinctLevelAttacks.Count() == 0) { if (_distinctLevelAttacks.Count() == 0) {
return nullptr; return nullptr;
} }