Files
CreatureLib/src/Library/CreatureData/LearnableAttacks.hpp
Deukhoofd f3d22f7ba9
All checks were successful
continuous-integration/drone/push Build is passing
Support getting distinct level attacks from LearnableAttacks.
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
2020-08-11 17:25:04 +02:00

53 lines
1.7 KiB
C++

#ifndef CREATURELIB_LEARNABLEATTACKS_HPP
#define CREATURELIB_LEARNABLEATTACKS_HPP
#include <Arbutils/Assert.hpp>
#include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/Random.hpp>
#include <unordered_map>
#include "../Attacks/AttackData.hpp"
namespace CreatureLib::Library {
class LearnableAttacks {
protected:
ArbUt::Dictionary<uint8_t, ArbUt::List<const AttackData*>> _learnedByLevel;
std::unordered_set<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);
}
}
}
virtual ~LearnableAttacks() = default;
void AddLevelAttack(uint8_t level, const AttackData* attack);
inline const ArbUt::List<const AttackData*>& GetAttacksForLevel(uint8_t level) const {
return _learnedByLevel.Get(level);
}
inline const std::unordered_set<const AttackData*>& GetDistinctLevelAttacks() const noexcept {
return _distinctLevelAttacks;
}
virtual const AttackData* GetRandomAttack(ArbUt::Random& rand) const {
if (_distinctLevelAttacks.empty()) {
return nullptr;
}
auto val = rand.Get(_distinctLevelAttacks.size());
auto it = _distinctLevelAttacks.begin();
std::advance(it, val);
return *it;
}
};
}
#endif // CREATURELIB_LEARNABLEATTACKS_HPP