Make SpeciesVariants smart pointers.
continuous-integration/drone/push Build was killed Details

This commit is contained in:
Deukhoofd 2020-05-26 14:35:18 +02:00
parent 36208da2fb
commit 8418c814b4
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 50 additions and 37 deletions

View File

@ -25,7 +25,7 @@ export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; }
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); } export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); }
SIMPLE_GET_FUNC_SMART_PTR(Creature, GetSpecies, const CreatureLib::Library::CreatureSpecies*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetSpecies, const CreatureLib::Library::CreatureSpecies*);
SIMPLE_GET_FUNC(Creature, GetVariant, const CreatureLib::Library::SpeciesVariant*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetVariant, const CreatureLib::Library::SpeciesVariant*);
SIMPLE_GET_FUNC(Creature, GetLevel, uint8_t); SIMPLE_GET_FUNC(Creature, GetLevel, uint8_t);
SIMPLE_GET_FUNC(Creature, GetExperience, uint32_t); SIMPLE_GET_FUNC(Creature, GetExperience, uint32_t);
SIMPLE_GET_FUNC(Creature, GetGender, CreatureLib::Library::Gender); SIMPLE_GET_FUNC(Creature, GetGender, CreatureLib::Library::Gender);
@ -84,7 +84,7 @@ export bool CreatureLib_Creature_HasVolatileScript(Creature* p, const char* scri
export size_t CreatureLib_Creature_GetAttacksCount(Creature* p) { return p->GetAttacks().Count(); } export size_t CreatureLib_Creature_GetAttacksCount(Creature* p) { return p->GetAttacks().Count(); }
export LearnedAttack* const* CreatureLib_Creature_GetAttacks(Creature* p) { return p->GetAttacks().RawData(); } export LearnedAttack* const* CreatureLib_Creature_GetAttacks(Creature* p) { return p->GetAttacks().RawData(); }
SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*);
SIMPLE_GET_FUNC(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*);
export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) { export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) {
return p->SetDisplaySpecies(borrowed_ptr<const CreatureLib::Library::CreatureSpecies>(species)); return p->SetDisplaySpecies(borrowed_ptr<const CreatureLib::Library::CreatureSpecies>(species));
} }

View File

@ -30,20 +30,26 @@ export bool CreatureLib_CreatureSpecies_HasVariantWithHash(const CreatureSpecies
} }
export bool CreatureLib_CreatureSpecies_TryGetVariant(const CreatureSpecies* p, const char* name, export bool CreatureLib_CreatureSpecies_TryGetVariant(const CreatureSpecies* p, const char* name,
const SpeciesVariant*& out) { const SpeciesVariant*& out) {
return p->TryGetVariant(Arbutils::CaseInsensitiveConstString::GetHash(name), out); borrowed_ptr<const SpeciesVariant> o;
auto res = p->TryGetVariant(Arbutils::CaseInsensitiveConstString::GetHash(name), o);
out = o.GetRaw();
return res;
} }
export bool CreatureLib_CreatureSpecies_TryGetVariantWithHash(const CreatureSpecies* p, uint32_t hash, export bool CreatureLib_CreatureSpecies_TryGetVariantWithHash(const CreatureSpecies* p, uint32_t hash,
const SpeciesVariant*& out) { const SpeciesVariant*& out) {
return p->TryGetVariant(hash, out); borrowed_ptr<const SpeciesVariant> o;
auto res = p->TryGetVariant(hash, o);
out = o.GetRaw();
return res;
} }
export uint8_t CreatureLib_CreatureSpecies_GetVariant(const SpeciesVariant*& out, const CreatureSpecies* p, export uint8_t CreatureLib_CreatureSpecies_GetVariant(const SpeciesVariant*& out, const CreatureSpecies* p,
const char* name) { const char* name) {
Try(out = p->GetVariant(Arbutils::CaseInsensitiveConstString::GetHash(name));) Try(out = p->GetVariant(Arbutils::CaseInsensitiveConstString::GetHash(name)).GetRaw();)
} }
export uint8_t CreatureLib_CreatureSpecies_GetVariantWithHash(const SpeciesVariant* out, const CreatureSpecies* p, export uint8_t CreatureLib_CreatureSpecies_GetVariantWithHash(const SpeciesVariant* out, const CreatureSpecies* p,
uint32_t hash) { uint32_t hash) {
Try(out = p->GetVariant(hash);) Try(out = p->GetVariant(hash).GetRaw();)
} }
export uint8_t CreatureLib_CreatureSpecies_SetVariant(CreatureSpecies* p, const char* name, SpeciesVariant* variant) { export uint8_t CreatureLib_CreatureSpecies_SetVariant(CreatureSpecies* p, const char* name, SpeciesVariant* variant) {
Try(p->SetVariant(Arbutils::CaseInsensitiveConstString(name), variant);) Try(p->SetVariant(Arbutils::CaseInsensitiveConstString(name), variant);)

View File

@ -7,10 +7,11 @@
using namespace CreatureLib; using namespace CreatureLib;
Battling::Creature::Creature(const BattleLibrary* library, const borrowed_ptr<const Library::CreatureSpecies>& species, Battling::Creature::Creature(const BattleLibrary* library, const borrowed_ptr<const Library::CreatureSpecies>& species,
const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, const borrowed_ptr<const Library::SpeciesVariant>& variant, uint8_t level,
Library::Gender gender, uint8_t coloring, const borrowed_ptr<const Library::Item> heldItem, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring,
std::string nickname, const Library::TalentIndex& talent, const borrowed_ptr<const Library::Item> heldItem, std::string nickname,
const List<LearnedAttack*>& attacks, bool allowedExperienceGain) const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks,
bool allowedExperienceGain)
: _library(library), _species(species), _variant(variant), _level(level), _experience(experience), : _library(library), _species(species), _variant(variant), _level(level), _experience(experience),
_uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)),
_talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks), _talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks),
@ -191,7 +192,7 @@ borrowed_ptr<const Library::CreatureSpecies> Battling::Creature::GetDisplaySpeci
species = _species; species = _species;
return species; return species;
} }
const Library::SpeciesVariant* Battling::Creature::GetDisplayVariant() const noexcept { borrowed_ptr<const Library::SpeciesVariant> Battling::Creature::GetDisplayVariant() const noexcept {
auto variant = _displayVariant; auto variant = _displayVariant;
if (variant == nullptr) if (variant == nullptr)
variant = _variant; variant = _variant;

View File

@ -25,10 +25,10 @@ namespace CreatureLib::Battling {
const BattleLibrary* _library; const BattleLibrary* _library;
borrowed_ptr<const Library::CreatureSpecies> _species; borrowed_ptr<const Library::CreatureSpecies> _species;
const Library::SpeciesVariant* _variant; borrowed_ptr<const Library::SpeciesVariant> _variant;
borrowed_ptr<const Library::CreatureSpecies> _displaySpecies = nullptr; borrowed_ptr<const Library::CreatureSpecies> _displaySpecies = nullptr;
const Library::SpeciesVariant* _displayVariant = nullptr; borrowed_ptr<const Library::SpeciesVariant> _displayVariant = nullptr;
uint8_t _level; uint8_t _level;
uint32_t _experience; uint32_t _experience;
@ -65,9 +65,10 @@ namespace CreatureLib::Battling {
public: public:
Creature(const BattleLibrary* library, const borrowed_ptr<const Library::CreatureSpecies>& species, Creature(const BattleLibrary* library, const borrowed_ptr<const Library::CreatureSpecies>& species,
const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, const borrowed_ptr<const Library::SpeciesVariant>& variant, uint8_t level, uint32_t experience,
Library::Gender gender, uint8_t coloring, const borrowed_ptr<const Library::Item> heldItem, uint32_t uid, Library::Gender gender, uint8_t coloring,
std::string nickname, const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks, const borrowed_ptr<const Library::Item> heldItem, std::string nickname,
const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks,
bool allowedExperienceGain = true); bool allowedExperienceGain = true);
virtual ~Creature() { virtual ~Creature() {
@ -84,7 +85,7 @@ namespace CreatureLib::Battling {
} }
inline const borrowed_ptr<const Library::CreatureSpecies>& GetSpecies() const noexcept { return _species; } inline const borrowed_ptr<const Library::CreatureSpecies>& GetSpecies() const noexcept { return _species; }
inline const Library::SpeciesVariant* GetVariant() const noexcept { return _variant; } inline const borrowed_ptr<const Library::SpeciesVariant>& GetVariant() const noexcept { return _variant; }
inline uint8_t GetLevel() const noexcept { return _level; } inline uint8_t GetLevel() const noexcept { return _level; }
inline uint32_t GetExperience() const noexcept { return _experience; } inline uint32_t GetExperience() const noexcept { return _experience; }
inline Library::Gender GetGender() const noexcept { return _gender; } inline Library::Gender GetGender() const noexcept { return _gender; }
@ -137,7 +138,7 @@ namespace CreatureLib::Battling {
const List<LearnedAttack*>& GetAttacks() noexcept { return _attacks; } const List<LearnedAttack*>& GetAttacks() noexcept { return _attacks; }
borrowed_ptr<const Library::CreatureSpecies> GetDisplaySpecies() const noexcept; borrowed_ptr<const Library::CreatureSpecies> GetDisplaySpecies() const noexcept;
const Library::SpeciesVariant* GetDisplayVariant() const noexcept; borrowed_ptr<const Library::SpeciesVariant> GetDisplayVariant() const noexcept;
void SetDisplaySpecies(const borrowed_ptr<const Library::CreatureSpecies>& species) noexcept { void SetDisplaySpecies(const borrowed_ptr<const Library::CreatureSpecies>& species) noexcept {
_displaySpecies = species; _displaySpecies = species;

View File

@ -5,26 +5,32 @@ using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant, CreatureSpecies::CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ConstString& growthRate, uint8_t captureRate) float genderRatio, const ConstString& growthRate, uint8_t captureRate)
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate), : _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate), _variants(1) {
_variants({{"default"_cnc, defaultVariant}}) {
AssertNotNull(defaultVariant) AssertNotNull(defaultVariant)
SetVariant("default"_cnc, defaultVariant);
} }
bool CreatureSpecies::HasVariant(const ConstString& name) const { return _variants.Has(name); } bool CreatureSpecies::HasVariant(const ConstString& name) const { return _variants.Has(name); }
bool CreatureSpecies::TryGetVariant(const ConstString& name, const SpeciesVariant*& out) const { bool CreatureSpecies::TryGetVariant(const ConstString& name, borrowed_ptr<const SpeciesVariant>& out) const {
return TryGetVariant(name.GetHash(), out); return TryGetVariant(name.GetHash(), out);
} }
bool CreatureSpecies::TryGetVariant(uint32_t hash, const SpeciesVariant*& out) const { bool CreatureSpecies::TryGetVariant(uint32_t hash, borrowed_ptr<const SpeciesVariant>& out) const {
return _variants.TryGet(hash, out); auto find = _variants.GetStdMap().find(hash);
if (find == _variants.end())
return false;
out = std::get<1>(*find);
return true;
} }
const SpeciesVariant* CreatureSpecies::GetVariant(const ConstString& name) const { return _variants.Get(name); } borrowed_ptr<const SpeciesVariant> CreatureSpecies::GetVariant(const ConstString& name) const {
const SpeciesVariant* CreatureSpecies::GetVariant(uint32_t key) const { return _variants.Get(key); } return _variants.Get(name);
}
borrowed_ptr<const SpeciesVariant> CreatureSpecies::GetVariant(uint32_t key) const { return _variants.Get(key); }
bool CreatureSpecies::HasVariant(uint32_t hash) const { return _variants.Has(hash); } bool CreatureSpecies::HasVariant(uint32_t hash) const { return _variants.Has(hash); }
void CreatureSpecies::SetVariant(const ConstString& name, const SpeciesVariant* variant) { void CreatureSpecies::SetVariant(const ConstString& name, const SpeciesVariant* variant) {
_variants.Insert(name, variant); _variants.GetStdMap().insert({name, std::unique_ptr<const SpeciesVariant>(variant)});
} }
Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const { Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {

View File

@ -3,6 +3,8 @@
#include <Arbutils/Collections/Dictionary.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/ConstString.hpp> #include <Arbutils/ConstString.hpp>
#include <Arbutils/Memory/borrowed_ptr.hpp>
#include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "../Gender.hpp" #include "../Gender.hpp"
@ -21,17 +23,13 @@ namespace CreatureLib::Library {
float _genderRate; float _genderRate;
const ConstString _growthRate; const ConstString _growthRate;
uint8_t _captureRate; uint8_t _captureRate;
Arbutils::Collections::Dictionary<uint32_t, const SpeciesVariant*> _variants; Arbutils::Collections::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variants;
public: public:
CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant, float genderRatio, CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant, float genderRatio,
const ConstString& growthRate, uint8_t captureRate); const ConstString& growthRate, uint8_t captureRate);
virtual ~CreatureSpecies() { virtual ~CreatureSpecies() { _variants.Clear(); }
for (auto v : _variants)
delete v.second;
_variants.Clear();
}
inline uint16_t GetId() const { return _id; } inline uint16_t GetId() const { return _id; }
inline float GetGenderRate() const { return _genderRate; } inline float GetGenderRate() const { return _genderRate; }
@ -40,16 +38,17 @@ namespace CreatureLib::Library {
[[nodiscard]] bool HasVariant(const ConstString& key) const; [[nodiscard]] bool HasVariant(const ConstString& key) const;
[[nodiscard]] bool HasVariant(uint32_t hash) const; [[nodiscard]] bool HasVariant(uint32_t hash) const;
[[nodiscard]] bool TryGetVariant(const ConstString& name, const SpeciesVariant*& out) const; [[nodiscard]] bool TryGetVariant(const ConstString& name, borrowed_ptr<const SpeciesVariant>& out) const;
[[nodiscard]] bool TryGetVariant(uint32_t hash, const SpeciesVariant*& out) const; [[nodiscard]] bool TryGetVariant(uint32_t hash, borrowed_ptr<const SpeciesVariant>& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const ConstString& key) const; [[nodiscard]] borrowed_ptr<const SpeciesVariant> GetVariant(const ConstString& key) const;
[[nodiscard]] const SpeciesVariant* GetVariant(uint32_t key) const; [[nodiscard]] borrowed_ptr<const SpeciesVariant> GetVariant(uint32_t key) const;
[[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const; [[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const;
[[nodiscard]] const ConstString& GetName() const { return _name; } [[nodiscard]] const ConstString& GetName() const { return _name; }
void SetVariant(const ConstString& name, const SpeciesVariant* variant); void SetVariant(const ConstString& name, const SpeciesVariant* variant);
const Arbutils::Collections::Dictionary<uint32_t, const SpeciesVariant*>& GetVariantsIterator() const { const Arbutils::Collections::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>>&
GetVariantsIterator() const {
return _variants; return _variants;
} }
}; };