Fixes and improvements for LearnableAttacks,
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-11 18:53:01 +02:00
parent f3d22f7ba9
commit 7ac63839b8
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 19 additions and 20 deletions

View File

@ -15,14 +15,17 @@ 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 p->GetAttacksForLevel(level).RawData();
} }
export bool CreatureLib_LearnableAttacks_HasAttacksForLevel(LearnableAttacks* p, uint8_t level) {
return p->HasAttacksForLevel(level);
}
export size_t CreatureLib_LearnableAttacks_GetAttacksForLevelCount(LearnableAttacks* p, uint8_t level) { export size_t CreatureLib_LearnableAttacks_GetAttacksForLevelCount(LearnableAttacks* p, uint8_t level) {
return p->GetAttacksForLevel(level).Count(); return p->GetAttacksForLevel(level).Count();
} }
export size_t CreatureLib_LearnableAttacks_GetDistinctLevelAttacksCount(LearnableAttacks* p) { export size_t CreatureLib_LearnableAttacks_GetDistinctLevelAttacksCount(LearnableAttacks* p) {
return p->GetDistinctLevelAttacks().size(); return p->GetDistinctLevelAttacks().Count();
} }
export const AttackData* const* CreatureLib_LearnableAttacks_GetDistinctLevelAttacks(LearnableAttacks* p) { export const AttackData* const* CreatureLib_LearnableAttacks_GetDistinctLevelAttacks(LearnableAttacks* p) {
return p->GetDistinctLevelAttacks().cbegin().operator->(); return p->GetDistinctLevelAttacks().RawData();
} }

View File

@ -3,12 +3,11 @@
using namespace CreatureLib::Library; using namespace CreatureLib::Library;
void LearnableAttacks::AddLevelAttack(uint8_t level, const AttackData* attack) { void LearnableAttacks::AddLevelAttack(uint8_t level, const AttackData* attack) {
ArbUt::List<const AttackData*> levelData; if (_learnedByLevel.Has(level)) {
if (_learnedByLevel.TryGet(level, levelData)) { _learnedByLevel[level].Append(attack);
levelData.Append(attack);
} else { } else {
levelData = {attack}; _learnedByLevel.Insert(level, {attack});
_learnedByLevel.Insert(level, levelData);
} }
_distinctLevelAttacks.insert(attack); if (!_distinctLevelAttacks.Contains(attack))
_distinctLevelAttacks.Append(attack);
} }

View File

@ -12,36 +12,33 @@ namespace CreatureLib::Library {
class LearnableAttacks { class LearnableAttacks {
protected: protected:
ArbUt::Dictionary<uint8_t, ArbUt::List<const AttackData*>> _learnedByLevel; ArbUt::Dictionary<uint8_t, ArbUt::List<const AttackData*>> _learnedByLevel;
std::unordered_set<const AttackData*> _distinctLevelAttacks; ArbUt::List<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<const AttackData*>>(levelAttackCapacity)) {}
for (auto kv : _learnedByLevel) {
for (auto attack : kv.second) {
AssertNotNull(attack)
_distinctLevelAttacks.insert(attack);
}
}
}
virtual ~LearnableAttacks() = default; virtual ~LearnableAttacks() = default;
void AddLevelAttack(uint8_t level, const AttackData* attack); void AddLevelAttack(uint8_t level, const AttackData* attack);
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<const AttackData*>& GetAttacksForLevel(uint8_t level) const {
if (!_learnedByLevel.Has(level)) {
THROW_CREATURE("No attacks found for level " << (uint32_t)level << ".");
}
return _learnedByLevel.Get(level); return _learnedByLevel.Get(level);
} }
inline const std::unordered_set<const AttackData*>& GetDistinctLevelAttacks() const noexcept { inline const ArbUt::List<const AttackData*>& GetDistinctLevelAttacks() const noexcept {
return _distinctLevelAttacks; return _distinctLevelAttacks;
} }
virtual const AttackData* GetRandomAttack(ArbUt::Random& rand) const { virtual const AttackData* GetRandomAttack(ArbUt::Random& rand) const {
if (_distinctLevelAttacks.empty()) { if (_distinctLevelAttacks.Count() == 0) {
return nullptr; return nullptr;
} }
auto val = rand.Get(_distinctLevelAttacks.size()); auto val = rand.Get(_distinctLevelAttacks.Count());
auto it = _distinctLevelAttacks.begin(); auto it = _distinctLevelAttacks.begin();
std::advance(it, val); std::advance(it, val);
return *it; return *it;