diff --git a/CInterface/Library/NatureLibrary.cpp b/CInterface/Library/NatureLibrary.cpp new file mode 100644 index 0000000..fc3f669 --- /dev/null +++ b/CInterface/Library/NatureLibrary.cpp @@ -0,0 +1,23 @@ +#include "../../src/Library/Natures/NatureLibrary.hpp" +#include "../Core.hpp" +using namespace PkmnLib::Library; + +export NatureLibrary* PkmnLib_NatureLibrary_Construct(size_t initialCapacity) { + return new NatureLibrary(initialCapacity); +} +export void PkmnLib_NatureLibrary_Destruct(const NatureLibrary* p) { delete p; } + +export uint8_t PkmnLib_NatureLibrary_LoadNature(NatureLibrary* p, const char* name, const Nature* nature) { + Try(p->LoadNature(Arbutils::CaseInsensitiveConstString(name), nature);) +} +export uint8_t PkmnLib_NatureLibrary_GetNatureByName(NatureLibrary* p, const char* name, const Nature*& out) { + Try(out = p->GetNatureByName(Arbutils::CaseInsensitiveConstString(name))); +} + +export const char* PkmnLib_NatureLibrary_GetRandomNatureName(NatureLibrary* p, Arbutils::Random* rand) { + return p->GetRandomNatureName(rand).c_str(); +} + +export uint8_t PkmnLib_NatureLibrary_GetNatureName(NatureLibrary* p, const Nature* nature, const char*& out) { + Try(out = p->GetNatureName(nature).c_str()); +} diff --git a/src/Library/Natures/NatureLibrary.hpp b/src/Library/Natures/NatureLibrary.hpp index 0668871..1a55cc9 100644 --- a/src/Library/Natures/NatureLibrary.hpp +++ b/src/Library/Natures/NatureLibrary.hpp @@ -15,11 +15,11 @@ namespace PkmnLib::Library { Arbutils::Collections::Dictionary _items; public: - explicit NatureLibrary(uint8_t size = 32) + explicit NatureLibrary(size_t size = 32) noexcept : _items(Arbutils::Collections::Dictionary(size)) {} - ~NatureLibrary(){ - for (auto& kv : _items){ + ~NatureLibrary() { + for (auto& kv : _items) { delete kv.second; } } @@ -38,10 +38,16 @@ namespace PkmnLib::Library { auto& map = _items.GetStdMap(); return std::next(std::begin(map), i)->first; } + inline const Arbutils::CaseInsensitiveConstString& + GetRandomNatureName(Arbutils::Random* rand) const { + auto i = rand->Get(_items.Count()); + auto& map = _items.GetStdMap(); + return std::next(std::begin(map), i)->first; + } - inline Arbutils::CaseInsensitiveConstString GetNatureName(const Nature* nature){ - for (const auto& v: _items){ - if (v.second == nature){ + inline Arbutils::CaseInsensitiveConstString GetNatureName(const Nature* nature) { + for (const auto& v : _items) { + if (v.second == nature) { return v.first; } }