Make BaseLibraries use shared_ptr.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -11,13 +11,14 @@ bool CreatureLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::Execu
|
||||
}
|
||||
|
||||
static CreatureLib::Battling::LearnedAttack* _replacementAttack = nullptr;
|
||||
static CreatureLib::Library::AttackData* _replacementAttackData = nullptr;
|
||||
static std::shared_ptr<const CreatureLib::Library::AttackData> _replacementAttackData = nullptr;
|
||||
|
||||
static CreatureLib::Library::AttackData* GetReplacementAttackData() {
|
||||
static const std::shared_ptr<const CreatureLib::Library::AttackData>& GetReplacementAttackData() {
|
||||
if (_replacementAttackData == nullptr) {
|
||||
_replacementAttackData = new CreatureLib::Library::AttackData(
|
||||
"replacement"_cnc, 0, CreatureLib::Library::AttackCategory::Physical, 30, 255, 255,
|
||||
CreatureLib::Library::AttackTarget::Any, 0, new CreatureLib::Library::SecondaryEffect(), {});
|
||||
_replacementAttackData =
|
||||
std::shared_ptr<const CreatureLib::Library::AttackData>(new CreatureLib::Library::AttackData(
|
||||
"replacement"_cnc, 0, CreatureLib::Library::AttackCategory::Physical, 30, 255, 255,
|
||||
CreatureLib::Library::AttackTarget::Any, 0, new CreatureLib::Library::SecondaryEffect(), {}));
|
||||
}
|
||||
return _replacementAttackData;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ CreateCreature CreateCreature::WithAttack(const Arbutils::CaseInsensitiveConstSt
|
||||
|
||||
Creature* CreateCreature::Create() {
|
||||
auto rand = Arbutils::Random();
|
||||
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species);
|
||||
auto& species = this->_library->GetSpeciesLibrary()->Get(this->_species);
|
||||
auto variant = species->GetVariant(this->_variant);
|
||||
Library::TalentIndex talent;
|
||||
if (this->_talent.Empty()) {
|
||||
@@ -48,9 +48,9 @@ Creature* CreateCreature::Create() {
|
||||
if (gender == static_cast<Library::Gender>(-1)) {
|
||||
gender = species->GetRandomGender(rand);
|
||||
}
|
||||
const Library::Item* heldItem = nullptr;
|
||||
std::shared_ptr<const Library::Item> heldItem = nullptr;
|
||||
if (!this->_heldItem.Empty()) {
|
||||
if (!_library->GetItemLibrary()->TryGet(this->_heldItem, heldItem)) {
|
||||
if (!_library->GetItemLibrary()->TryGet(this->_heldItem.GetHash(), heldItem)) {
|
||||
throw CreatureException("Invalid held item.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace CreatureLib::Battling {
|
||||
uint8_t _coloring = 0;
|
||||
Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc;
|
||||
uint32_t _identifier = 0;
|
||||
List<std::tuple<const Library::AttackData*, AttackLearnMethod>> _attacks;
|
||||
List<std::tuple<std::shared_ptr<const Library::AttackData>, AttackLearnMethod>> _attacks;
|
||||
|
||||
public:
|
||||
CreateCreature(const BattleLibrary* library, const Arbutils::CaseInsensitiveConstString& species, uint8_t level)
|
||||
|
||||
@@ -6,11 +6,13 @@
|
||||
|
||||
using namespace CreatureLib;
|
||||
|
||||
Battling::Creature::Creature(const BattleLibrary* library, const Library::CreatureSpecies* species,
|
||||
Battling::Creature::Creature(const BattleLibrary* library,
|
||||
const std::shared_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 Library::Item* heldItem,
|
||||
std::string nickname, const Library::TalentIndex& talent,
|
||||
const List<LearnedAttack*>& attacks, bool allowedExperienceGain)
|
||||
Library::Gender gender, uint8_t coloring,
|
||||
const std::shared_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),
|
||||
@@ -185,7 +187,7 @@ void Battling::Creature::AddExperience(uint32_t amount) {
|
||||
_experience = exp;
|
||||
_level = level;
|
||||
}
|
||||
const Library::CreatureSpecies* Battling::Creature::GetDisplaySpecies() const noexcept {
|
||||
std::shared_ptr<const Library::CreatureSpecies> Battling::Creature::GetDisplaySpecies() const noexcept {
|
||||
auto species = _displaySpecies;
|
||||
if (species == nullptr)
|
||||
species = _species;
|
||||
@@ -198,14 +200,14 @@ const Library::SpeciesVariant* Battling::Creature::GetDisplayVariant() const noe
|
||||
return variant;
|
||||
}
|
||||
void Battling::Creature::SetHeldItem(const ConstString& itemName) {
|
||||
const Library::Item* item;
|
||||
if (!_library->GetItemLibrary()->TryGet(itemName, item)) {
|
||||
std::shared_ptr<const Library::Item> item;
|
||||
if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) {
|
||||
throw CreatureException("Item not found.");
|
||||
}
|
||||
_heldItem = item;
|
||||
}
|
||||
void Battling::Creature::SetHeldItem(uint32_t itemNameHash) {
|
||||
const Library::Item* item;
|
||||
std::shared_ptr<const Library::Item> item;
|
||||
if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) {
|
||||
throw CreatureException("Item not found.");
|
||||
}
|
||||
|
||||
@@ -23,10 +23,10 @@ namespace CreatureLib::Battling {
|
||||
protected:
|
||||
const BattleLibrary* _library;
|
||||
|
||||
const Library::CreatureSpecies* _species;
|
||||
std::shared_ptr<const Library::CreatureSpecies> _species;
|
||||
const Library::SpeciesVariant* _variant;
|
||||
|
||||
const Library::CreatureSpecies* _displaySpecies = nullptr;
|
||||
std::shared_ptr<const Library::CreatureSpecies> _displaySpecies = nullptr;
|
||||
const Library::SpeciesVariant* _displayVariant = nullptr;
|
||||
|
||||
uint8_t _level;
|
||||
@@ -34,7 +34,7 @@ namespace CreatureLib::Battling {
|
||||
uint32_t _uniqueIdentifier;
|
||||
Library::Gender _gender;
|
||||
uint8_t _coloring;
|
||||
const Library::Item* _heldItem;
|
||||
std::shared_ptr<const Library::Item> _heldItem;
|
||||
uint32_t _currentHealth;
|
||||
|
||||
Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost;
|
||||
@@ -63,10 +63,10 @@ namespace CreatureLib::Battling {
|
||||
void OnFaint();
|
||||
|
||||
public:
|
||||
Creature(const BattleLibrary* library, const Library::CreatureSpecies* species,
|
||||
Creature(const BattleLibrary* library, const std::shared_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 Library::Item* heldItem, std::string nickname,
|
||||
const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks,
|
||||
Library::Gender gender, uint8_t coloring, const std::shared_ptr<const Library::Item> heldItem,
|
||||
std::string nickname, const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks,
|
||||
bool allowedExperienceGain = true);
|
||||
|
||||
virtual ~Creature() {
|
||||
@@ -82,7 +82,7 @@ namespace CreatureLib::Battling {
|
||||
_currentHealth = GetBoostedStat(Library::Statistic::Health);
|
||||
}
|
||||
|
||||
inline const Library::CreatureSpecies* GetSpecies() const noexcept { return _species; }
|
||||
inline const std::shared_ptr<const Library::CreatureSpecies>& GetSpecies() const noexcept { return _species; }
|
||||
inline const Library::SpeciesVariant* GetVariant() const noexcept { return _variant; }
|
||||
inline uint8_t GetLevel() const noexcept { return _level; }
|
||||
inline uint32_t GetExperience() const noexcept { return _experience; }
|
||||
@@ -94,10 +94,10 @@ namespace CreatureLib::Battling {
|
||||
inline bool HasHeldItem(uint32_t nameHash) const noexcept {
|
||||
return _heldItem != nullptr && _heldItem->GetName() == nameHash;
|
||||
}
|
||||
inline const Library::Item* GetHeldItem() const noexcept { return _heldItem; }
|
||||
inline const std::shared_ptr<const Library::Item>& GetHeldItem() const noexcept { return _heldItem; }
|
||||
void SetHeldItem(const ConstString& itemName);
|
||||
void SetHeldItem(uint32_t itemNameHash);
|
||||
inline void SetHeldItem(const Library::Item* item) noexcept { _heldItem = item; };
|
||||
inline void SetHeldItem(const std::shared_ptr<const Library::Item>& item) noexcept { _heldItem = item; };
|
||||
|
||||
inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; }
|
||||
|
||||
@@ -135,10 +135,12 @@ namespace CreatureLib::Battling {
|
||||
|
||||
const List<LearnedAttack*>& GetAttacks() noexcept { return _attacks; }
|
||||
|
||||
const Library::CreatureSpecies* GetDisplaySpecies() const noexcept;
|
||||
std::shared_ptr<const Library::CreatureSpecies> GetDisplaySpecies() const noexcept;
|
||||
const Library::SpeciesVariant* GetDisplayVariant() const noexcept;
|
||||
|
||||
void SetDisplaySpecies(const Library::CreatureSpecies* species) noexcept { _displaySpecies = species; }
|
||||
void SetDisplaySpecies(const std::shared_ptr<const Library::CreatureSpecies>& species) noexcept {
|
||||
_displaySpecies = species;
|
||||
}
|
||||
void SetDisplayVariant(const Library::SpeciesVariant* variant) noexcept { _displayVariant = variant; };
|
||||
|
||||
inline bool AllowedExperienceGain() const noexcept { return _allowedExperienceGain; }
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
#include "LearnedAttack.hpp"
|
||||
#include <Arbutils/Assert.hpp>
|
||||
CreatureLib::Battling::LearnedAttack::LearnedAttack(const CreatureLib::Library::AttackData* attack, uint8_t maxUses,
|
||||
AttackLearnMethod learnMethod)
|
||||
CreatureLib::Battling::LearnedAttack::LearnedAttack(
|
||||
const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses,
|
||||
AttackLearnMethod learnMethod)
|
||||
: _attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) {
|
||||
AssertNotNull(_attack)
|
||||
}
|
||||
|
||||
CreatureLib::Battling::LearnedAttack::LearnedAttack(const CreatureLib::Library::AttackData* attack,
|
||||
AttackLearnMethod learnMethod)
|
||||
CreatureLib::Battling::LearnedAttack::LearnedAttack(
|
||||
const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, AttackLearnMethod learnMethod)
|
||||
: _attack(attack), _maxUses(attack->GetBaseUsages()), _remainingUses(_maxUses), _learnMethod(learnMethod) {
|
||||
AssertNotNull(_attack)
|
||||
}
|
||||
|
||||
const CreatureLib::Library::AttackData* CreatureLib::Battling::LearnedAttack::GetAttack() const noexcept {
|
||||
const std::shared_ptr<const CreatureLib::Library::AttackData>&
|
||||
CreatureLib::Battling::LearnedAttack::GetAttack() const noexcept {
|
||||
return _attack;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
#ifndef CREATURELIB_LEARNEDATTACK_HPP
|
||||
#define CREATURELIB_LEARNEDATTACK_HPP
|
||||
|
||||
#include <memory>
|
||||
#include "../../Library/Attacks/AttackData.hpp"
|
||||
#include "AttackLearnMethod.hpp"
|
||||
|
||||
namespace CreatureLib::Battling {
|
||||
class LearnedAttack {
|
||||
const Library::AttackData* _attack;
|
||||
std::shared_ptr<const Library::AttackData> _attack;
|
||||
uint8_t _maxUses;
|
||||
uint8_t _remainingUses;
|
||||
AttackLearnMethod _learnMethod;
|
||||
|
||||
public:
|
||||
LearnedAttack(const Library::AttackData* attack, uint8_t maxUses, AttackLearnMethod learnMethod);
|
||||
LearnedAttack(const Library::AttackData* attack, AttackLearnMethod learnMethod);
|
||||
LearnedAttack(const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses,
|
||||
AttackLearnMethod learnMethod);
|
||||
LearnedAttack(const std::shared_ptr<const CreatureLib::Library::AttackData>& attack,
|
||||
AttackLearnMethod learnMethod);
|
||||
|
||||
virtual ~LearnedAttack() = default;
|
||||
|
||||
const Library::AttackData* GetAttack() const noexcept;
|
||||
const std::shared_ptr<const Library::AttackData>& GetAttack() const noexcept;
|
||||
uint8_t GetMaxUses() const noexcept;
|
||||
uint8_t GetRemainingUses() const noexcept;
|
||||
AttackLearnMethod GetLearnMethod() const noexcept;
|
||||
|
||||
@@ -7,23 +7,19 @@
|
||||
#include <Arbutils/ConstString.hpp>
|
||||
#include <Arbutils/Random.hpp>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace CreatureLib::Library {
|
||||
template <class T> class BaseLibrary {
|
||||
Arbutils::Collections::Dictionary<uint32_t, const T*> _values;
|
||||
Arbutils::Collections::Dictionary<uint32_t, std::shared_ptr<const T>> _values;
|
||||
Arbutils::Collections::List<uint32_t> _listValues;
|
||||
size_t _index;
|
||||
|
||||
public:
|
||||
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity), _listValues(initialCapacity) {}
|
||||
|
||||
virtual ~BaseLibrary() {
|
||||
for (const auto& v : _values) {
|
||||
delete v.second;
|
||||
}
|
||||
_values.Clear();
|
||||
}
|
||||
virtual ~BaseLibrary() { _values.Clear(); }
|
||||
|
||||
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
|
||||
AssertNotNull(value)
|
||||
@@ -32,7 +28,7 @@ namespace CreatureLib::Library {
|
||||
}
|
||||
inline void Insert(uint32_t hashedKey, const T* value) {
|
||||
AssertNotNull(value)
|
||||
_values.Insert(hashedKey, value);
|
||||
_values.Insert(hashedKey, std::shared_ptr<const T>(value));
|
||||
_listValues.Append(hashedKey);
|
||||
}
|
||||
|
||||
@@ -47,31 +43,38 @@ namespace CreatureLib::Library {
|
||||
_listValues.Remove(k);
|
||||
}
|
||||
|
||||
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
|
||||
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const std::shared_ptr<const T>& out) const {
|
||||
return TryGet(name.GetHash(), out);
|
||||
}
|
||||
bool TryGet(uint32_t hashedKey, const T*& out) const { return _values.TryGet(hashedKey, out); }
|
||||
bool TryGet(uint32_t hashedKey, std::shared_ptr<const T>& out) const { return _values.TryGet(hashedKey, out); }
|
||||
|
||||
[[nodiscard]] inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>&
|
||||
Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return _values.Get(name.GetHash());
|
||||
}
|
||||
[[nodiscard]] inline const T* Get(uint32_t hashedKey) const { return _values.Get(hashedKey); }
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>& Get(uint32_t hashedKey) const {
|
||||
return _values.Get(hashedKey);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>&
|
||||
operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return Get(name);
|
||||
}
|
||||
[[nodiscard]] inline const T* operator[](uint32_t hashedKey) const { return Get(hashedKey); }
|
||||
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const T*>& GetIterator() const {
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>& operator[](uint32_t hashedKey) const {
|
||||
return Get(hashedKey);
|
||||
}
|
||||
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const std::shared_ptr<const T>>&
|
||||
GetIterator() const {
|
||||
return _values;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t GetCount() const { return _values.Count(); }
|
||||
|
||||
inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
|
||||
inline const std::shared_ptr<const T>& GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
|
||||
auto i = rand.Get(_listValues.Count());
|
||||
return _values[_listValues[i]];
|
||||
}
|
||||
inline const T* GetRandomValue(Arbutils::Random* rand) const {
|
||||
inline const std::shared_ptr<const T>& GetRandomValue(Arbutils::Random* rand) const {
|
||||
auto i = rand->Get(_listValues.Count());
|
||||
return _values[_listValues[i]];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user