Rework several libraries to use new StringViewDictionary
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is failing

This commit is contained in:
2022-05-16 17:21:39 +02:00
parent dcf6b20e65
commit e6f38cfb26
11 changed files with 67 additions and 79 deletions

View File

@@ -23,14 +23,14 @@ CreateCreature CreateCreature::WithAttack(const ArbUt::StringView& attackName, A
THROW("You have already set the maximum amount of allowed moves.");
}
auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash());
auto attackData = _library->GetAttackLibrary()->Get(attackName);
_attacks.Append(std::tuple(attackData, learnMethod));
return *this;
}
Creature* CreateCreature::Create() {
auto rand = ArbUt::Random();
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.GetHash());
auto species = this->_library->GetSpeciesLibrary()->Get(this->_species);
auto variant = species->GetVariant(this->_variant);
Library::TalentIndex talent;
if (this->_talent.IsEmpty()) {
@@ -48,7 +48,7 @@ Creature* CreateCreature::Create() {
}
ArbUt::OptionalBorrowedPtr<const Library::Item> heldItem;
if (!this->_heldItem.IsEmpty()) {
auto val = _library->GetItemLibrary()->TryGet(this->_heldItem.GetHash());
auto val = _library->GetItemLibrary()->TryGet(this->_heldItem);
if (!val.has_value()) {
THROW("Invalid held item '", this->_heldItem.c_str(), "'.");
}

View File

@@ -337,24 +337,24 @@ namespace CreatureLib::Battling {
variant = _variant.GetRaw();
return variant;
}
void Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) {
void Creature::SetHeldItem(const ArbUt::StringView& itemName) {
if (itemName == ""_cnc) {
_heldItem = {};
_heldItemTriggerScript = {};
} else {
auto v = _library->GetItemLibrary()->TryGet(itemName.GetHash());
auto v = _library->GetItemLibrary()->TryGet(itemName);
if (!v.has_value()) {
THROW("Item not found '", itemName.c_str(), "'.");
}
_heldItem = v.value();
}
}
void Creature::SetHeldItem(u32 itemNameHash) {
void Creature::SetHeldItemByHash(u32 itemNameHash) {
if (itemNameHash == ArbUt::StringView::CalculateHash("")) {
_heldItem = {};
_heldItemTriggerScript = {};
} else {
auto v = _library->GetItemLibrary()->TryGet(itemNameHash);
auto v = _library->GetItemLibrary()->TryGetByHash(itemNameHash);
if (!v.has_value()) {
THROW("Item not found.");
}

View File

@@ -108,8 +108,8 @@ namespace CreatureLib::Battling {
return _heldItem.HasValue() && _heldItem.GetValue()->GetName() == nameHash;
}
inline const ArbUt::OptionalBorrowedPtr<const Library::Item>& GetHeldItem() const noexcept { return _heldItem; }
void SetHeldItem(const ArbUt::BasicStringView& itemName);
void SetHeldItem(u32 itemNameHash);
void SetHeldItem(const ArbUt::StringView& itemName);
void SetHeldItemByHash(u32 itemNameHash);
inline void SetHeldItem(const ArbUt::BorrowedPtr<const Library::Item>& item) noexcept { _heldItem = item; };
bool ConsumeHeldItem();

View File

@@ -1,8 +1,8 @@
#ifndef CREATURELIB_BASELIBRARY_HPP
#define CREATURELIB_BASELIBRARY_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/Collections/StringViewDictionary.hpp>
#include <Arbutils/Memory/Memory.hpp>
#include <Arbutils/Random.hpp>
#include <Arbutils/String/StringView.hpp>
@@ -10,7 +10,7 @@
namespace CreatureLib::Library {
template <class T> class BaseLibrary {
protected:
ArbUt::Dictionary<u32, std::unique_ptr<const T>> _values;
ArbUt::StringViewDictionary<std::unique_ptr<const T>> _values;
ArbUt::List<u32> _listValues;
public:
@@ -20,14 +20,9 @@ namespace CreatureLib::Library {
inline virtual void Insert(const ArbUt::StringView& key, const T* non_null value) {
EnsureNotNull(value)
_values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)});
_values.GetStdMap().insert({key, std::unique_ptr<const T>(value)});
_listValues.Append(key);
}
inline virtual void Insert(u32 hashedKey, const T* non_null value) {
EnsureNotNull(value)
_values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)});
_listValues.Append(hashedKey);
}
inline void Delete(const ArbUt::StringView& key) noexcept {
_values.erase(key.GetHash());
@@ -36,7 +31,7 @@ namespace CreatureLib::Library {
_listValues.Remove(k.value());
}
}
inline void Delete(u32 hashedKey) noexcept {
inline void DeleteByHash(u32 hashedKey) noexcept {
_values.Remove(hashedKey);
auto k = _listValues.IndexOf(hashedKey);
if (k.has_value()) {
@@ -44,40 +39,42 @@ namespace CreatureLib::Library {
}
}
std::optional<ArbUt::BorrowedPtr<const T>> TryGet(const ArbUt::BasicStringView& name) const noexcept {
return TryGet(name.GetHash());
std::optional<ArbUt::BorrowedPtr<const T>> TryGet(const ArbUt::StringView& name) const noexcept {
auto opt = _values.TryGet(name);
if (opt.has_value()) {
return std::make_optional<ArbUt::BorrowedPtr<const T>>(opt.value());
}
return {};
}
std::optional<ArbUt::BorrowedPtr<const T>> TryGet(u32 hashedKey) const noexcept {
auto find = _values.GetStdMap().find(hashedKey);
if (find == _values.GetStdMap().end())
return {};
return std::get<1>(*find);
std::optional<ArbUt::BorrowedPtr<const T>> TryGetByHash(u32 hashedKey) const noexcept {
auto opt = _values.TryGet(hashedKey);
if (opt.has_value()) {
return std::make_optional<ArbUt::BorrowedPtr<const T>>(opt.value());
}
return {};
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(const ArbUt::BasicStringView& name) const {
return _values.Get(name.GetHash());
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(const ArbUt::StringView& name) const {
return _values.Get(name);
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> GetByHash(u32 hashedKey) const {
return _values.GetFromHash(hashedKey);
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(u32 hashedKey) const { return _values.Get(hashedKey); }
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> operator[](const ArbUt::BasicStringView& name) const {
return Get(name);
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> operator[](u32 hashedKey) const { return Get(hashedKey); }
[[nodiscard]] inline const ArbUt::Dictionary<u32, const std::unique_ptr<const T>>&
[[nodiscard]] inline const ArbUt::StringViewDictionary<const std::unique_ptr<const T>>&
GetIterator() const noexcept {
return _values;
}
using const_iterator = typename std::unordered_map<u32, ArbUt::BorrowedPtr<const T>>::const_iterator;
using const_iterator = typename ArbUt::StringViewDictionary<std::unique_ptr<const T>>::const_iterator;
inline const_iterator begin() const {
return reinterpret_cast<const std::unordered_map<u32, ArbUt::BorrowedPtr<const T>>&>(_values.GetStdMap())
.begin();
}
inline const_iterator end() const {
return reinterpret_cast<const std::unordered_map<u32, ArbUt::BorrowedPtr<const T>>&>(_values.GetStdMap())
.end();
}
inline const_iterator begin() const { return _values.begin(); }
inline const_iterator end() const { return _values.end(); }
[[nodiscard]] size_t GetCount() const noexcept { return _values.Count(); }
ArbUt::BorrowedPtr<const T> GetAtIndex(size_t index) const {

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_SPECIESLIBRARY_HPP
#define CREATURELIB_SPECIESLIBRARY_HPP
#include <Arbutils/Collections/Dictionary.hpp>
#include "BaseLibrary.hpp"
#include "CreatureData/CreatureSpecies.hpp"
@@ -16,10 +17,6 @@ namespace CreatureLib::Library {
BaseLibrary::Insert(key, value);
_valuesById.Insert(value->GetId(), value);
}
void Insert(u32 hashedKey, const CreatureSpecies* non_null value) override {
BaseLibrary::Insert(hashedKey, value);
_valuesById.Insert(value->GetId(), value);
}
const ArbUt::BorrowedPtr<const CreatureSpecies>& GetById(u16 id) const { return _valuesById[id]; }
};

View File

@@ -3,6 +3,7 @@
#include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/Collections/StringViewDictionary.hpp>
#include <Arbutils/String/StringView.hpp>
#include <numeric>
#include "../Defines.hpp"
@@ -10,13 +11,14 @@
namespace CreatureLib::Library {
class TypeLibrary {
ArbUt::Dictionary<ArbUt::StringView, u8> _types;
ArbUt::StringViewDictionary<u8> _types;
ArbUt::List<ArbUt::List<float>> _effectiveness;
public:
TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::Dictionary<ArbUt::StringView, u8>(initialCapacity)) {}
TypeLibrary(size_t initialCapacity = 20) : _types(ArbUt::StringViewDictionary<u8>(initialCapacity)) {}
inline u8 GetTypeId(const ArbUt::StringView& key) const { return _types.Get(key); }
inline u8 GetTypeIdByHash(u32 keyHash) const { return _types.GetFromHash(keyHash); }
[[nodiscard]] inline float GetSingleEffectiveness(u8 attacking, u8 defensive) const {
try {
return _effectiveness[attacking][defensive];