Implements GetRandomAttack method on LearnableAttacks.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-19 17:42:32 +02:00
parent c6ce5fe8a7
commit ad9cbf0670
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 13 additions and 44 deletions

View File

@ -1,8 +0,0 @@
#include "CreatureMoves.hpp"
#include <utility>
CreatureLib::Library::LevelMove::LevelMove(uint8_t level, std::string name) : _level(level), _name(std::move(name)) {}
uint8_t CreatureLib::Library::LevelMove::GetLevel() const { return this->_level; }
std::string CreatureLib::Library::LevelMove::GetName() const { return this->_name; }

View File

@ -1,33 +0,0 @@
#ifndef CREATURELIB_CREATUREMOVES_HPP
#define CREATURELIB_CREATUREMOVES_HPP
#include <cstdint>
#include <string>
#include <vector>
namespace CreatureLib::Library {
class LevelMove {
private:
uint8_t _level;
std::string _name;
public:
LevelMove(uint8_t level, std::string name);
[[nodiscard]] inline uint8_t GetLevel() const;
[[nodiscard]] inline std::string GetName() const;
};
class CreatureMoves {
private:
std::vector<LevelMove> _levelMoves;
std::vector<std::string> _eggMoves;
std::vector<std::string> _machineMoves;
std::vector<std::string> _tutorMoves;
std::vector<std::string> _variantDependentMoves;
public:
// TODO: Public API funcs
};
}
#endif // CREATURELIB_CREATUREMOVES_HPP

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/Random.hpp>
#include <unordered_map> #include <unordered_map>
#include "../Attacks/AttackData.hpp" #include "../Attacks/AttackData.hpp"
@ -12,19 +13,29 @@ using namespace Arbutils::Collections;
namespace CreatureLib::Library { namespace CreatureLib::Library {
class LearnableAttacks { class LearnableAttacks {
Dictionary<uint8_t, List<const AttackData*>> _learnedByLevel; Dictionary<uint8_t, List<const AttackData*>> _learnedByLevel;
std::unordered_set<const AttackData*> _distinctAttacks;
public: public:
LearnableAttacks(size_t levelAttackCapacity) explicit LearnableAttacks(size_t levelAttackCapacity)
: _learnedByLevel(Dictionary<uint8_t, List<const AttackData*>>(levelAttackCapacity)) { : _learnedByLevel(Dictionary<uint8_t, List<const AttackData*>>(levelAttackCapacity)) {
for (auto kv : _learnedByLevel) { for (auto kv : _learnedByLevel) {
for (auto attack : kv.second) for (auto attack : kv.second) {
AssertNotNull(attack) AssertNotNull(attack)
_distinctAttacks.insert(attack);
} }
} }
}
virtual ~LearnableAttacks() = default;
void AddLevelMove(uint8_t level, const AttackData* attack); void AddLevelMove(uint8_t level, const AttackData* attack);
const List<const AttackData*>& GetAttacksForLevel(uint8_t level) const; const List<const AttackData*>& GetAttacksForLevel(uint8_t level) const;
virtual const AttackData* GetRandomAttack(Arbutils::Random rand = Arbutils::Random()) {
auto val = rand.Get(_distinctAttacks.size());
return *std::next(_distinctAttacks.begin(), val);
}
}; };
} }

View File

@ -6,7 +6,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "../StatisticSet.hpp" #include "../StatisticSet.hpp"
#include "CreatureMoves.hpp"
#include "LearnableAttacks.hpp" #include "LearnableAttacks.hpp"
#include "TalentIndex.hpp" #include "TalentIndex.hpp"
using ConstString = Arbutils::CaseInsensitiveConstString; using ConstString = Arbutils::CaseInsensitiveConstString;