Support for adding flags to CreatureSpecies and CreatureVariant.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-10 17:46:09 +02:00
parent 1807e720dd
commit 2ca4f53b0c
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 52 additions and 14 deletions

View File

@ -4,10 +4,14 @@ using namespace CreatureLib::Library;
export uint8_t CreatureLib_CreatureSpecies_Construct(CreatureSpecies*& out, uint16_t id, const char* name, export uint8_t CreatureLib_CreatureSpecies_Construct(CreatureSpecies*& out, uint16_t id, const char* name,
SpeciesVariant* defaultVariant, float genderRatio, SpeciesVariant* defaultVariant, float genderRatio,
const char* growthRate, uint8_t captureRate) { const char* growthRate, uint8_t captureRate, const char* flags[],
size_t flagsCount) {
Try(out = new CreatureSpecies(id, ArbUt::StringView(name), defaultVariant, genderRatio, Try(std::unordered_set<uint32_t> conversedFlags(flagsCount);
ArbUt::StringView(growthRate), captureRate);) for (size_t i = 0; i < flagsCount; i++) { conversedFlags.insert(ArbUt::StringView::CalculateHash(flags[i])); }
out = new CreatureSpecies(id, ArbUt::StringView(name), defaultVariant, genderRatio,
ArbUt::StringView(growthRate), captureRate, conversedFlags);)
} }
export void CreatureLib_CreatureSpecies_Destruct(const CreatureSpecies* p) { delete p; } export void CreatureLib_CreatureSpecies_Destruct(const CreatureSpecies* p) { delete p; }
@ -62,3 +66,6 @@ export size_t CreatureLib_CreatureSpecies_GetVariantsCount(CreatureSpecies* p) {
export const SpeciesVariant* const* CreatureLib_CreatureSpecies_GetVariants(CreatureSpecies* p) { export const SpeciesVariant* const* CreatureLib_CreatureSpecies_GetVariants(CreatureSpecies* p) {
return reinterpret_cast<const SpeciesVariant* const*>(p->GetVariantsIterator().RawData()); return reinterpret_cast<const SpeciesVariant* const*>(p->GetVariantsIterator().RawData());
} }
export bool CreatureLib_CreatureSpecies_HasFlag(const CreatureSpecies* p, const char* key) {
return p->HasFlag(ArbUt::StringView::CalculateHash(key));
}

View File

@ -3,11 +3,18 @@
using namespace CreatureLib::Library; using namespace CreatureLib::Library;
export SpeciesVariant* CreatureLib_SpeciesVariant_Construct( export SpeciesVariant*
const char* name, float height, float weight, uint32_t baseExperience, uint8_t types[], size_t typeLength, CreatureLib_SpeciesVariant_Construct(const char* name, float height, float weight, uint32_t baseExperience,
uint16_t baseHealth, uint16_t baseAttack, uint16_t baseDefense, uint16_t baseMagicalAttack, uint8_t types[], size_t typeLength, uint16_t baseHealth, uint16_t baseAttack,
uint16_t baseMagicalDefense, uint16_t baseSpeed, const char* talents[], size_t talentsLength, uint16_t baseDefense, uint16_t baseMagicalAttack, uint16_t baseMagicalDefense,
const char* secretTalents[], size_t secretTalentsLength, const LearnableAttacks* attacks) { uint16_t baseSpeed, const char* talents[], size_t talentsLength,
const char* secretTalents[], size_t secretTalentsLength,
const LearnableAttacks* attacks, const char* flags[], size_t flagsCount) {
std::unordered_set<uint32_t> conversedFlags(flagsCount);
for (size_t i = 0; i < flagsCount; i++) {
conversedFlags.insert(ArbUt::StringView::CalculateHash(flags[i]));
}
auto talentsWrapped = ArbUt::List<ArbUt::StringView>(talentsLength); auto talentsWrapped = ArbUt::List<ArbUt::StringView>(talentsLength);
for (size_t i = 0; i < talentsLength; i++) { for (size_t i = 0; i < talentsLength; i++) {
@ -22,7 +29,7 @@ export SpeciesVariant* CreatureLib_SpeciesVariant_Construct(
ArbUt::StringView(name), height, weight, baseExperience, ArbUt::List<uint8_t>(types, types + typeLength), ArbUt::StringView(name), height, weight, baseExperience, ArbUt::List<uint8_t>(types, types + typeLength),
CreatureLib::Library::StatisticSet<uint16_t>(baseHealth, baseAttack, baseDefense, baseMagicalAttack, CreatureLib::Library::StatisticSet<uint16_t>(baseHealth, baseAttack, baseDefense, baseMagicalAttack,
baseMagicalDefense, baseSpeed), baseMagicalDefense, baseSpeed),
talentsWrapped, secretTalentsWrapped, attacks); talentsWrapped, secretTalentsWrapped, attacks, conversedFlags);
} }
export void CreatureLib_SpeciesVariant_Destruct(SpeciesVariant* p) { delete p; } export void CreatureLib_SpeciesVariant_Destruct(SpeciesVariant* p) { delete p; }
@ -47,3 +54,6 @@ export const LearnableAttacks* CreatureLib_SpeciesVariant_GetLearnableAttacks(Sp
export uint8_t CreatureLib_SpeciesVariant_GetRandomTalent(SpeciesVariant* p, ArbUt::Random* rand) { export uint8_t CreatureLib_SpeciesVariant_GetRandomTalent(SpeciesVariant* p, ArbUt::Random* rand) {
return p->GetRandomTalent(*rand).GetIndex(); return p->GetRandomTalent(*rand).GetIndex();
} }
export bool CreatureLib_SpeciesVariant_HasFlag(const SpeciesVariant* p, const char* key) {
return p->HasFlag(ArbUt::StringView::CalculateHash(key));
}

View File

@ -1,12 +1,14 @@
#include "CreatureSpecies.hpp" #include "CreatureSpecies.hpp"
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
#include <utility>
using namespace CreatureLib::Library; using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant, CreatureSpecies::CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate) float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate,
std::unordered_set<uint32_t> flags)
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate), : _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variantsLookup(1), _variantsList(1) { _variantsLookup(1), _variantsList(1), _flags(std::move(flags)) {
AssertNotNull(defaultVariant) AssertNotNull(defaultVariant)
SetVariant("default"_cnc, defaultVariant); SetVariant("default"_cnc, defaultVariant);
} }

View File

@ -24,10 +24,12 @@ namespace CreatureLib::Library {
ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variantsLookup; ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variantsLookup;
ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList; ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList;
std::unordered_set<uint32_t> _flags;
public: public:
CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant, CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate); float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate,
std::unordered_set<uint32_t> flags = {});
virtual ~CreatureSpecies() noexcept { _variantsLookup.Clear(); } virtual ~CreatureSpecies() noexcept { _variantsLookup.Clear(); }
@ -60,6 +62,13 @@ namespace CreatureLib::Library {
inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const { inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const {
return _variantsList; return _variantsList;
} }
inline bool HasFlag(const ArbUt::StringView& key) const noexcept {
return this->_flags.find(key) != this->_flags.end();
}
inline bool HasFlag(uint32_t keyHash) const noexcept {
return this->_flags.find(keyHash) != this->_flags.end();
}
}; };
} }

View File

@ -27,14 +27,17 @@ namespace CreatureLib::Library {
ArbUt::List<ArbUt::StringView> _talents; ArbUt::List<ArbUt::StringView> _talents;
ArbUt::List<ArbUt::StringView> _secretTalents; ArbUt::List<ArbUt::StringView> _secretTalents;
std::unique_ptr<const LearnableAttacks> _attacks; std::unique_ptr<const LearnableAttacks> _attacks;
std::unordered_set<uint32_t> _flags;
public: public:
SpeciesVariant(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience, SpeciesVariant(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience,
const ArbUt::List<uint8_t>& types, Library::StatisticSet<uint16_t> baseStats, const ArbUt::List<uint8_t>& types, Library::StatisticSet<uint16_t> baseStats,
const ArbUt::List<ArbUt::StringView>& talents, const ArbUt::List<ArbUt::StringView>& talents,
const ArbUt::List<ArbUt::StringView>& secretTalents, const LearnableAttacks* attacks) const ArbUt::List<ArbUt::StringView>& secretTalents, const LearnableAttacks* attacks,
std::unordered_set<uint32_t> flags = {})
: _name(name), _height(height), _weight(weight), _baseExperience(baseExperience), _types((types)), : _name(name), _height(height), _weight(weight), _baseExperience(baseExperience), _types((types)),
_baseStatistics(baseStats), _talents(talents), _secretTalents(secretTalents), _attacks(attacks){}; _baseStatistics(baseStats), _talents(talents), _secretTalents(secretTalents), _attacks(attacks),
_flags(flags){};
virtual ~SpeciesVariant() = default; virtual ~SpeciesVariant() = default;
inline const ArbUt::StringView& GetName() const { return _name; } inline const ArbUt::StringView& GetName() const { return _name; }
@ -73,6 +76,13 @@ namespace CreatureLib::Library {
} }
[[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& GetTalents() const { return _talents; } [[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& GetTalents() const { return _talents; }
[[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& GetSecretTalents() const { return _secretTalents; } [[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& GetSecretTalents() const { return _secretTalents; }
inline bool HasFlag(const ArbUt::StringView& key) const noexcept {
return this->_flags.find(key) != this->_flags.end();
}
inline bool HasFlag(uint32_t keyHash) const noexcept {
return this->_flags.find(keyHash) != this->_flags.end();
}
}; };
} }