Fixes and improvements for LearnableAttacks,
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
parent
f3d22f7ba9
commit
7ac63839b8
|
@ -15,14 +15,17 @@ export void CreatureLib_LearnableAttacks_AddLevelAttack(LearnableAttacks* p, uin
|
|||
export const AttackData* const* CreatureLib_LearnableAttacks_GetAttacksForLevel(LearnableAttacks* p, uint8_t level) {
|
||||
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) {
|
||||
return p->GetAttacksForLevel(level).Count();
|
||||
}
|
||||
|
||||
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) {
|
||||
return p->GetDistinctLevelAttacks().cbegin().operator->();
|
||||
return p->GetDistinctLevelAttacks().RawData();
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue