Add functionality to retrieve a variants list.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-08 18:56:00 +02:00
parent 4ffc8ba01f
commit 15100f3cd5
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 20 additions and 12 deletions

View File

@ -55,3 +55,7 @@ export uint8_t CreatureLib_CreatureSpecies_SetVariant(CreatureSpecies* p, const
export Gender CreatureLib_CreatureSpecies_GetRandomGender(CreatureSpecies* p, ArbUt::Random* random) {
return p->GetRandomGender(*random);
}
export const SpeciesVariant* const* CreatureLib_CreatureSpecies_GetVariants(CreatureSpecies* p) {
return reinterpret_cast<const SpeciesVariant* const*>(p->GetVariantsIterator().RawData());
}

View File

@ -5,21 +5,23 @@ using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate)
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate), _variants(1) {
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variantsLookup(1), _variantsList(1) {
AssertNotNull(defaultVariant)
SetVariant("default"_cnc, defaultVariant);
}
bool CreatureSpecies::TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
auto find = _variants.GetStdMap().find(hash);
if (find == _variants.end())
auto find = _variantsLookup.GetStdMap().find(hash);
if (find == _variantsLookup.end())
return false;
out = std::get<1>(*find);
return true;
}
void CreatureSpecies::SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant) {
_variants.GetStdMap().insert({name, std::unique_ptr<const SpeciesVariant>(variant)});
_variantsLookup.GetStdMap().insert({name, std::unique_ptr<const SpeciesVariant>(variant)});
_variantsList.Append(variant);
}
Gender CreatureSpecies::GetRandomGender(ArbUt::Random& rand) const noexcept {

View File

@ -21,13 +21,15 @@ namespace CreatureLib::Library {
float _genderRate;
const ArbUt::StringView _growthRate;
uint8_t _captureRate;
ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variants;
ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variantsLookup;
ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList;
public:
CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate);
virtual ~CreatureSpecies() noexcept { _variants.Clear(); }
virtual ~CreatureSpecies() noexcept { _variantsLookup.Clear(); }
inline uint16_t GetId() const noexcept { return _id; }
inline float GetGenderRate() const noexcept { return _genderRate; }
@ -35,9 +37,9 @@ namespace CreatureLib::Library {
inline uint8_t GetCaptureRate() const noexcept { return _captureRate; }
[[nodiscard]] inline bool HasVariant(const ArbUt::BasicStringView& key) const noexcept {
return _variants.Has(key);
return _variantsLookup.Has(key);
}
[[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variants.Has(hash); }
[[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variantsLookup.Has(hash); }
[[nodiscard]] inline bool TryGetVariant(const ArbUt::BasicStringView& name,
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
return TryGetVariant(name.GetHash(), out);
@ -45,18 +47,18 @@ namespace CreatureLib::Library {
[[nodiscard]] bool TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept;
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant>
GetVariant(const ArbUt::BasicStringView& key) const {
return _variants.Get(key);
return _variantsLookup.Get(key);
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(uint32_t key) const {
return _variants.Get(key);
return _variantsLookup.Get(key);
}
[[nodiscard]] Gender GetRandomGender(ArbUt::Random& rand) const noexcept;
[[nodiscard]] inline const ArbUt::StringView& GetName() const { return _name; }
void SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant);
inline const ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>>& GetVariantsIterator() const {
return _variants;
inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const {
return _variantsList;
}
};
}