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->(); }
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, GetExperience, uint32_t);
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 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(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) {
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,
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,
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,
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,
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) {
Try(p->SetVariant(Arbutils::CaseInsensitiveConstString(name), variant);)

View File

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

View File

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

View File

@ -5,26 +5,32 @@ using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, const ConstString& name, const SpeciesVariant* defaultVariant,
float genderRatio, const ConstString& growthRate, uint8_t captureRate)
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variants({{"default"_cnc, defaultVariant}}) {
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate), _variants(1) {
AssertNotNull(defaultVariant)
SetVariant("default"_cnc, defaultVariant);
}
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);
}
bool CreatureSpecies::TryGetVariant(uint32_t hash, const SpeciesVariant*& out) const {
return _variants.TryGet(hash, out);
bool CreatureSpecies::TryGetVariant(uint32_t hash, borrowed_ptr<const SpeciesVariant>& out) const {
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); }
const SpeciesVariant* CreatureSpecies::GetVariant(uint32_t key) const { return _variants.Get(key); }
borrowed_ptr<const SpeciesVariant> CreatureSpecies::GetVariant(const ConstString& name) const {
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); }
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 {

View File

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