Make HeldItem OptionalBorrowedPtr.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-12-12 13:40:30 +01:00
parent a01e22ad89
commit 4367d1f5cf
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 13 additions and 12 deletions

View File

@ -10,10 +10,11 @@ export uint8_t CreatureLib_Creature_Construct(Creature*& out, const BattleLibrar
const char* nickname, bool secretTalent, uint8_t talent, const char* nickname, bool secretTalent, uint8_t talent,
LearnedAttack* attacks[], size_t attacksNum, bool allowedExperienceGain) { LearnedAttack* attacks[], size_t attacksNum, bool allowedExperienceGain) {
Try(auto attacksVec = std::vector<LearnedAttack*>(attacks, attacks + (attacksNum * sizeof(LearnedAttack*))); Try(auto attacksVec = std::vector<LearnedAttack*>(attacks, attacks + (attacksNum * sizeof(LearnedAttack*)));
out = new Creature(library, ArbUt::BorrowedPtr<const CreatureLib::Library::CreatureSpecies>(species), variant, out =
level, experience, uid, gender, coloring, new Creature(library, ArbUt::BorrowedPtr<const CreatureLib::Library::CreatureSpecies>(species), variant,
ArbUt::BorrowedPtr<const CreatureLib::Library::Item>(heldItem), std::string(nickname), level, experience, uid, gender, coloring,
CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec, allowedExperienceGain);) ArbUt::OptionalBorrowedPtr<const CreatureLib::Library::Item>(heldItem), std::string(nickname),
CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec, allowedExperienceGain);)
}; };
export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; } export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; }
@ -42,7 +43,7 @@ export bool CreatureLib_Creature_HasHeldItem(const Creature* p, const char* name
return p->HasHeldItem(ArbUt::StringView(name)); return p->HasHeldItem(ArbUt::StringView(name));
} }
export bool CreatureLib_Creature_HasHeldItemWithHash(const Creature* p, uint32_t hash) { return p->HasHeldItem(hash); } export bool CreatureLib_Creature_HasHeldItemWithHash(const Creature* p, uint32_t hash) { return p->HasHeldItem(hash); }
BORROWED_GET_FUNC(Creature, GetHeldItem, const CreatureLib::Library::Item*); OPTIONAL_GET_FUNC(Creature, GetHeldItem, const CreatureLib::Library::Item*);
export uint8_t CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) { export uint8_t CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) {
Try(p->SetHeldItem(ArbUt::StringView(name));) Try(p->SetHeldItem(ArbUt::StringView(name));)
} }

View File

@ -46,7 +46,7 @@ Creature* CreateCreature::Create() {
if (gender == static_cast<Library::Gender>(-1)) { if (gender == static_cast<Library::Gender>(-1)) {
gender = species->GetRandomGender(rand); gender = species->GetRandomGender(rand);
} }
ArbUt::BorrowedPtr<const Library::Item> heldItem; ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem;
if (!this->_heldItem.IsEmpty()) { if (!this->_heldItem.IsEmpty()) {
auto val = _library->GetItemLibrary()->TryGet(this->_heldItem.GetHash()); auto val = _library->GetItemLibrary()->TryGet(this->_heldItem.GetHash());
if (!val.has_value()) { if (!val.has_value()) {

View File

@ -10,7 +10,7 @@ namespace CreatureLib::Battling {
const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species, const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species,
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, level_int_t level, const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, level_int_t level,
uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring,
const ArbUt::BorrowedPtr<const Library::Item> heldItem, const std::string& nickname, const ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem, const std::string& nickname,
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks, const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
bool allowedExperienceGain) bool allowedExperienceGain)
: _library(library), _species(species), _variant(variant), _level(level), _experience(experience), : _library(library), _species(species), _variant(variant), _level(level), _experience(experience),

View File

@ -32,7 +32,7 @@ namespace CreatureLib::Battling {
uint32_t _uniqueIdentifier; uint32_t _uniqueIdentifier;
Library::Gender _gender; Library::Gender _gender;
uint8_t _coloring; uint8_t _coloring;
ArbUt::BorrowedPtr<const Library::Item> _heldItem; ArbUt::OptionalBorrowedPtr<const Library::Item> _heldItem;
uint32_t _currentHealth = -1; uint32_t _currentHealth = -1;
Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost; Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost;
@ -67,7 +67,7 @@ namespace CreatureLib::Battling {
const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species, const ArbUt::BorrowedPtr<const Library::CreatureSpecies>& species,
const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, level_int_t level, const ArbUt::BorrowedPtr<const Library::SpeciesVariant>& variant, level_int_t level,
uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring, uint32_t experience, uint32_t uid, Library::Gender gender, uint8_t coloring,
const ArbUt::BorrowedPtr<const Library::Item> heldItem, const std::string& nickname, const ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem, const std::string& nickname,
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks, const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
bool allowedExperienceGain = true); bool allowedExperienceGain = true);
@ -92,12 +92,12 @@ namespace CreatureLib::Battling {
inline Library::Gender GetGender() const noexcept { return _gender; } inline Library::Gender GetGender() const noexcept { return _gender; }
inline uint8_t GetColoring() const noexcept { return _coloring; } inline uint8_t GetColoring() const noexcept { return _coloring; }
inline bool HasHeldItem(const ArbUt::BasicStringView& name) const noexcept { inline bool HasHeldItem(const ArbUt::BasicStringView& name) const noexcept {
return _heldItem != nullptr && _heldItem->GetName() == name; return _heldItem.HasValue() && _heldItem.GetValue()->GetName() == name;
} }
inline bool HasHeldItem(uint32_t nameHash) const noexcept { inline bool HasHeldItem(uint32_t nameHash) const noexcept {
return _heldItem != nullptr && _heldItem->GetName() == nameHash; return _heldItem.HasValue() && _heldItem.GetValue()->GetName() == nameHash;
} }
inline const ArbUt::BorrowedPtr<const Library::Item>& GetHeldItem() const noexcept { return _heldItem; } inline const ArbUt::OptionalBorrowedPtr<const Library::Item>& GetHeldItem() const noexcept { return _heldItem; }
void SetHeldItem(const ArbUt::BasicStringView& itemName); void SetHeldItem(const ArbUt::BasicStringView& itemName);
void SetHeldItem(uint32_t itemNameHash); void SetHeldItem(uint32_t itemNameHash);
inline void SetHeldItem(const ArbUt::BorrowedPtr<const Library::Item>& item) noexcept { _heldItem = item; }; inline void SetHeldItem(const ArbUt::BorrowedPtr<const Library::Item>& item) noexcept { _heldItem = item; };