Use hash as key for Species variants, added C interface for Species.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
f95c625971
commit
5f341ffbb7
|
@ -0,0 +1,53 @@
|
|||
#include "../../src/Library/CreatureData/CreatureSpecies.hpp"
|
||||
#define export extern "C"
|
||||
|
||||
export CreatureLib::Library::CreatureSpecies*
|
||||
CreatureLib_CreatureSpecies_Construct(uint16_t id, const char* name,
|
||||
CreatureLib::Library::SpeciesVariant* defaultVariant, float genderRatio,
|
||||
const char* growthRate, uint8_t captureRate) {
|
||||
return new CreatureLib::Library::CreatureSpecies(id, Arbutils::CaseInsensitiveConstString(name), defaultVariant,
|
||||
genderRatio, Arbutils::CaseInsensitiveConstString(growthRate),
|
||||
captureRate);
|
||||
}
|
||||
|
||||
export void CreatureLib_CreatureSpecies_Destruct(const CreatureLib::Library::CreatureSpecies* p) { delete p; }
|
||||
|
||||
#define SIMPLE_GET_FUNC(type, name, returnType) \
|
||||
export returnType CreatureLib_##type##_##name(const CreatureLib::Library::type* p) { return p->name(); }
|
||||
|
||||
SIMPLE_GET_FUNC(CreatureSpecies, GetId, uint16_t);
|
||||
SIMPLE_GET_FUNC(CreatureSpecies, GetGenderRate, float);
|
||||
SIMPLE_GET_FUNC(CreatureSpecies, GetCaptureRate, uint8_t);
|
||||
export const char* CreatureLib_CreatureSpecies_GetName(const CreatureLib::Library::CreatureSpecies* p) {
|
||||
return p->GetName().c_str();
|
||||
}
|
||||
export const char* CreatureLib_CreatureSpecies_GetGrowthRate(const CreatureLib::Library::CreatureSpecies* p) {
|
||||
return p->GetGrowthRate().c_str();
|
||||
}
|
||||
export bool CreatureLib_CreatureSpecies_HasVariant(const CreatureLib::Library::CreatureSpecies* p, const char* name) {
|
||||
return p->HasVariant(Arbutils::CaseInsensitiveConstString::GetHash(name));
|
||||
}
|
||||
export bool CreatureLib_CreatureSpecies_HasVariantWithHash(const CreatureLib::Library::CreatureSpecies* p,
|
||||
uint32_t hash) {
|
||||
return p->HasVariant(hash);
|
||||
}
|
||||
export bool CreatureLib_CreatureSpecies_TryGetVariant(const CreatureLib::Library::CreatureSpecies* p, const char* name,
|
||||
const CreatureLib::Library::SpeciesVariant* out) {
|
||||
return p->TryGetVariant(Arbutils::CaseInsensitiveConstString::GetHash(name), out);
|
||||
}
|
||||
export bool CreatureLib_CreatureSpecies_TryGetVariantWithHash(const CreatureLib::Library::CreatureSpecies* p,
|
||||
uint32_t hash,
|
||||
const CreatureLib::Library::SpeciesVariant* out) {
|
||||
return p->TryGetVariant(hash, out);
|
||||
}
|
||||
|
||||
export const CreatureLib::Library::SpeciesVariant*
|
||||
CreatureLib_CreatureSpecies_GetVariant(const CreatureLib::Library::CreatureSpecies* p, const char* name) {
|
||||
return p->GetVariant(Arbutils::CaseInsensitiveConstString::GetHash(name));
|
||||
}
|
||||
export const CreatureLib::Library::SpeciesVariant*
|
||||
CreatureLib_CreatureSpecies_GetVariantWithHash(const CreatureLib::Library::CreatureSpecies* p, uint32_t hash) {
|
||||
return p->GetVariant(hash);
|
||||
}
|
||||
|
||||
#undef SIMPLE_GET_FUNC
|
|
@ -10,7 +10,10 @@ CreatureSpecies::CreatureSpecies(uint16_t id, const ConstString& name, const Spe
|
|||
bool CreatureSpecies::HasVariant(const ConstString& name) const { return _variants.find(name) != _variants.end(); }
|
||||
|
||||
bool CreatureSpecies::TryGetVariant(const ConstString& name, const SpeciesVariant*& out) const {
|
||||
auto find = _variants.find(name);
|
||||
return TryGetVariant(name.GetHash(), out);
|
||||
}
|
||||
bool CreatureSpecies::TryGetVariant(uint32_t hash, const SpeciesVariant*& out) const {
|
||||
auto find = _variants.find(hash);
|
||||
if (find != _variants.end()) {
|
||||
out = find->second;
|
||||
return true;
|
||||
|
@ -18,10 +21,9 @@ bool CreatureSpecies::TryGetVariant(const ConstString& name, const SpeciesVarian
|
|||
return false;
|
||||
}
|
||||
|
||||
const SpeciesVariant* CreatureSpecies::GetVariant(const ConstString& name) const {
|
||||
auto key = name;
|
||||
return _variants.at(key);
|
||||
}
|
||||
const SpeciesVariant* CreatureSpecies::GetVariant(const ConstString& name) const { return _variants.at(name); }
|
||||
const SpeciesVariant* CreatureSpecies::GetVariant(uint32_t key) const { return _variants.at(key); }
|
||||
bool CreatureSpecies::HasVariant(uint32_t hash) const { return _variants.at(hash); }
|
||||
|
||||
void CreatureSpecies::SetVariant(const ConstString& name, const SpeciesVariant* variant) {
|
||||
auto find = _variants.find(name);
|
||||
|
@ -37,4 +39,4 @@ Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
|
|||
if (val >= this->_genderRate)
|
||||
return Gender ::Female;
|
||||
return Gender ::Male;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace CreatureLib::Library {
|
|||
float _genderRate;
|
||||
const ConstString _growthRate;
|
||||
uint8_t _captureRate;
|
||||
std::unordered_map<ConstString, const SpeciesVariant*> _variants;
|
||||
std::unordered_map<uint32_t, const SpeciesVariant*> _variants;
|
||||
|
||||
public:
|
||||
CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant, float genderRatio,
|
||||
|
@ -38,14 +38,17 @@ namespace CreatureLib::Library {
|
|||
inline uint8_t GetCaptureRate() const { return _captureRate; }
|
||||
|
||||
[[nodiscard]] bool HasVariant(const ConstString& key) const;
|
||||
[[nodiscard]] bool HasVariant(uint32_t hash) const;
|
||||
[[nodiscard]] bool TryGetVariant(const ConstString& name, const SpeciesVariant*& out) const;
|
||||
[[nodiscard]] bool TryGetVariant(uint32_t hash, const SpeciesVariant*& out) const;
|
||||
[[nodiscard]] const SpeciesVariant* GetVariant(const ConstString& key) const;
|
||||
[[nodiscard]] const SpeciesVariant* GetVariant(uint32_t key) const;
|
||||
[[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const;
|
||||
[[nodiscard]] const ConstString& GetName() const { return _name; }
|
||||
|
||||
void SetVariant(const ConstString& name, const SpeciesVariant* variant);
|
||||
|
||||
const std::unordered_map<ConstString, const SpeciesVariant*>& GetVariantsIterator() const { return _variants; }
|
||||
const std::unordered_map<uint32_t, const SpeciesVariant*>& GetVariantsIterator() const { return _variants; }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue