Fixes and improvements for LearnableAttacks,
All checks were successful
continuous-integration/drone/push Build is passing

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

View File

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

View File

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