From 5e917b70470b8d1212713fce4a1dea679c8a27f9 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 11 Aug 2020 21:46:22 +0200 Subject: [PATCH] Support for finding the previous evolution step of a Pokemon. --- CInterface/Library/SpeciesLibrary.cpp | 8 ++++++++ src/Library/Species/SpeciesLibrary.cpp | 19 +++++++++++++++++++ src/Library/Species/SpeciesLibrary.hpp | 6 ++++++ 3 files changed, 33 insertions(+) create mode 100644 CInterface/Library/SpeciesLibrary.cpp diff --git a/CInterface/Library/SpeciesLibrary.cpp b/CInterface/Library/SpeciesLibrary.cpp new file mode 100644 index 0000000..553a63f --- /dev/null +++ b/CInterface/Library/SpeciesLibrary.cpp @@ -0,0 +1,8 @@ +#include "../../src/Library/Species/SpeciesLibrary.hpp" +#include "../Core.hpp" +using namespace PkmnLib::Library; + +export const PokemonSpecies* PkmnLib_PokemonLibrary_FindPreEvolution(const SpeciesLibrary* p, + const PokemonSpecies* species) { + return p->FindPreEvolution(species).GetRaw(); +} \ No newline at end of file diff --git a/src/Library/Species/SpeciesLibrary.cpp b/src/Library/Species/SpeciesLibrary.cpp index aac3139..e5e49af 100644 --- a/src/Library/Species/SpeciesLibrary.cpp +++ b/src/Library/Species/SpeciesLibrary.cpp @@ -1 +1,20 @@ #include "SpeciesLibrary.hpp" +namespace PkmnLib::Library { + ArbUt::BorrowedPtr + SpeciesLibrary::FindPreEvolution(const ArbUt::BorrowedPtr& species) const noexcept { + if (_preEvolutionCache.Has(species)) { + return _preEvolutionCache[species]; + } + for (auto& s : _values) { + auto pkmn = (PokemonSpecies*)s.second.get(); + for (auto& evo : pkmn->GetEvolutions()) { + if (evo->GetNewSpecies() == species) { + auto non_const = const_cast(this); + non_const->_preEvolutionCache[species] = pkmn; + return pkmn; + } + } + } + return nullptr; + } +} diff --git a/src/Library/Species/SpeciesLibrary.hpp b/src/Library/Species/SpeciesLibrary.hpp index 675cffc..722d0d9 100644 --- a/src/Library/Species/SpeciesLibrary.hpp +++ b/src/Library/Species/SpeciesLibrary.hpp @@ -6,6 +6,9 @@ namespace PkmnLib::Library { class SpeciesLibrary : public CreatureLib::Library::SpeciesLibrary { + ArbUt::Dictionary, ArbUt::BorrowedPtr> + _preEvolutionCache; + public: inline bool TryGet(const ArbUt::BasicStringView& name, ArbUt::BorrowedPtr& outSpecies) const { @@ -26,6 +29,9 @@ namespace PkmnLib::Library { void Insert(const ArbUt::StringView& name, const PokemonSpecies* species) { CreatureLib::Library::SpeciesLibrary::Insert(name, species); } + + ArbUt::BorrowedPtr + FindPreEvolution(const ArbUt::BorrowedPtr& species) const noexcept; }; }