Support for finding the previous evolution step of a Pokemon.

This commit is contained in:
Deukhoofd 2020-08-11 21:46:22 +02:00
parent 9274b675e9
commit 5e917b7047
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 33 additions and 0 deletions

View File

@ -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();
}

View File

@ -1 +1,20 @@
#include "SpeciesLibrary.hpp"
namespace PkmnLib::Library {
ArbUt::BorrowedPtr<const PokemonSpecies>
SpeciesLibrary::FindPreEvolution(const ArbUt::BorrowedPtr<const PokemonSpecies>& 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<SpeciesLibrary*>(this);
non_const->_preEvolutionCache[species] = pkmn;
return pkmn;
}
}
}
return nullptr;
}
}

View File

@ -6,6 +6,9 @@
namespace PkmnLib::Library {
class SpeciesLibrary : public CreatureLib::Library::SpeciesLibrary {
ArbUt::Dictionary<ArbUt::BorrowedPtr<const PokemonSpecies>, ArbUt::BorrowedPtr<const PokemonSpecies>>
_preEvolutionCache;
public:
inline bool TryGet(const ArbUt::BasicStringView& name,
ArbUt::BorrowedPtr<const PokemonSpecies>& 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<const PokemonSpecies>
FindPreEvolution(const ArbUt::BorrowedPtr<const PokemonSpecies>& species) const noexcept;
};
}