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

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

View File

@ -47,7 +47,7 @@ OPTIONAL_GET_FUNC(Creature, GetHeldItem, const CreatureLib::Library::Item*);
export_func u8 CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) {
Try(p->SetHeldItem(ArbUt::StringView(name));)
}
export_func u8 CreatureLib_Creature_SetHeldItemWithHash(Creature* p, u32 hash) { Try(p->SetHeldItem(hash);) }
export_func u8 CreatureLib_Creature_SetHeldItemWithHash(Creature* p, u32 hash) { Try(p->SetHeldItemByHash(hash);) }
export_func void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const CreatureLib::Library::Item* item) {
return p->SetHeldItem(ArbUt::BorrowedPtr<const CreatureLib::Library::Item>(item));
}

View File

@ -2,22 +2,18 @@
#define BASELIBRARY(simpleName, fullname, returnType) \
export_func u8 simpleName##_Insert(fullname* p, const char* name, returnType* t) { \
Try(p->Insert(ArbUt::StringView::CalculateHash(name), t);) \
} \
\
export_func u8 simpleName##_InsertWithHash(fullname* p, u32 hashedKey, returnType* t) { \
Try(p->Insert(hashedKey, t);) \
Try(p->Insert(ArbUt::StringView(name), t);) \
} \
\
export_func u8 simpleName##_Delete(fullname* p, const char* name) { \
Try(p->Delete(ArbUt::StringView::CalculateHash(name));) \
Try(p->DeleteByHash(ArbUt::StringView::CalculateHash(name));) \
} \
\
export_func u8 simpleName##_DeleteWithHash(fullname* p, u32 hashedKey) { Try(p->Delete(hashedKey);) } \
export_func u8 simpleName##_DeleteWithHash(fullname* p, u32 hashedKey) { Try(p->DeleteByHash(hashedKey);) } \
\
export_func bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \
ArbUt::BorrowedPtr<const returnType> o; \
auto v = p->TryGet(ArbUt::StringView::CalculateHash(name)); \
auto v = p->TryGetByHash(ArbUt::StringView::CalculateHash(name)); \
if (!v.has_value()) { \
out = nullptr; \
return false; \
@ -29,7 +25,7 @@
\
export_func bool simpleName##_TryGetWithHash(fullname* p, u32 hashedKey, const returnType*& out) { \
ArbUt::BorrowedPtr<const returnType> o; \
auto v = p->TryGet(hashedKey); \
auto v = p->TryGetByHash(hashedKey); \
if (!v.has_value()) { \
out = nullptr; \
return false; \
@ -40,11 +36,11 @@
} \
\
export_func u8 simpleName##_Get(fullname* p, const char* name, const returnType*& out) { \
Try(out = p->Get(ArbUt::StringView::CalculateHash(name)).GetRaw();) \
Try(out = p->GetByHash(ArbUt::StringView::CalculateHash(name)).GetRaw();) \
} \
\
export_func u8 simpleName##_GetWithHash(fullname* p, u32 hashedKey, const returnType*& out) { \
Try(out = p->Get(hashedKey).GetRaw();) \
Try(out = p->GetByHash(hashedKey).GetRaw();) \
} \
\
export_func size_t simpleName##_GetCount(fullname* p) { return p->GetCount(); } \

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];

View File

@ -12,8 +12,7 @@ using namespace CreatureLib::Battling;
TEST_CASE("Turn ordering: Attack before pass") {
auto lib = TestLibrary::Get();
auto learnedAttack =
LearnedAttack(lib->GetAttackLibrary()->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto learnedAttack = LearnedAttack(lib->GetAttackLibrary()->Get("standard"_cnc), AttackLearnMethod::Unknown);
auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create();
auto choice1 = std::make_shared<PassTurnChoice>(creature);
@ -32,8 +31,8 @@ TEST_CASE("Turn ordering: Attack before pass") {
TEST_CASE("Turn ordering: High priority goes before no priority") {
auto lib = TestLibrary::Get();
const auto& l = lib->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->GetByHash("standard"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->GetByHash("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create();
auto choice1 = std::make_shared<AttackTurnChoice>(creature, a1, CreatureIndex(0, 0));
@ -55,8 +54,8 @@ TEST_CASE("Turn ordering: High priority goes before no priority") {
TEST_CASE("Turn ordering: Higher priority goes before high priority") {
auto lib = TestLibrary::Get();
const auto& l = lib->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->GetByHash("highPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->GetByHash("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create();
auto choice1 = std::make_shared<AttackTurnChoice>(creature, a1, CreatureIndex(0, 0));
auto choice2 = std::make_shared<AttackTurnChoice>(creature, a2, CreatureIndex(0, 0));
@ -76,8 +75,8 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority") {
TEST_CASE("Turn ordering: High priority goes before low priority") {
auto lib = TestLibrary::Get();
const auto& l = lib->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->GetByHash("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->GetByHash("higherPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create();
auto choice1 = std::make_shared<AttackTurnChoice>(creature, a1, CreatureIndex(0, 0));
@ -99,8 +98,8 @@ TEST_CASE("Turn ordering: High priority goes before low priority") {
TEST_CASE("Turn ordering: No priority goes before low priority") {
auto lib = TestLibrary::Get();
const auto& l = lib->GetAttackLibrary();
auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->Get("standard"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a1 = new LearnedAttack(l->GetByHash("lowPriority"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto a2 = new LearnedAttack(l->GetByHash("standard"_cnc.GetHash()), AttackLearnMethod::Unknown);
auto creature = CreateCreature(lib, "testSpecies1"_cnc, 1).Create();
auto choice1 = std::make_shared<AttackTurnChoice>(creature, a1, CreatureIndex(0, 0));
auto choice2 = std::make_shared<AttackTurnChoice>(creature, a2, CreatureIndex(0, 0));

View File

@ -7,7 +7,7 @@ using namespace CreatureLib::Library;
TEST_CASE("Iterate over library") {
auto& lib = TestLibrary::Get()->GetSpeciesLibrary();
auto i = 0;
for (auto b : *lib) {
for (auto& b : *lib) {
if (i == 0) {
CHECK(b.second->GetName() == "testSpecies1"_cnc);
}

View File

@ -22,7 +22,7 @@ BattleLibrary* TestLibrary::Get() {
SpeciesLibrary* TestLibrary::BuildSpeciesLibrary(const TalentLibrary* talentLibrary) {
auto l = new SpeciesLibrary();
l->Insert("testSpecies1"_cnc.GetHash(),
l->Insert("testSpecies1"_cnc,
new CreatureSpecies(
0, "testSpecies1"_cnc,
new SpeciesVariant("default"_cnc, 1, 1, 10, {0, 1}, StatisticSet<u16>(10, 10, 10, 10, 10, 10),
@ -34,17 +34,14 @@ SpeciesLibrary* TestLibrary::BuildSpeciesLibrary(const TalentLibrary* talentLibr
AttackLibrary* TestLibrary::BuildAttackLibrary() {
auto l = new AttackLibrary();
l->Insert("standard"_cnc.GetHash(), new AttackData("standard"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 0, new SecondaryEffect(), {}));
l->Insert("highPriority"_cnc.GetHash(),
new AttackData("highPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 1, new SecondaryEffect(), {}));
l->Insert("higherPriority"_cnc.GetHash(),
new AttackData("higherPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 2, new SecondaryEffect(), {}));
l->Insert("lowPriority"_cnc.GetHash(),
new AttackData("lowPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, -1, new SecondaryEffect(), {}));
l->Insert("standard"_cnc, new AttackData("standard"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 0, new SecondaryEffect(), {}));
l->Insert("highPriority"_cnc, new AttackData("highPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 1, new SecondaryEffect(), {}));
l->Insert("higherPriority"_cnc, new AttackData("higherPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, 2, new SecondaryEffect(), {}));
l->Insert("lowPriority"_cnc, new AttackData("lowPriority"_cnc, 0, AttackCategory::Physical, 20, 100, 30,
AttackTarget::AdjacentOpponent, -1, new SecondaryEffect(), {}));
return l;
}