PkmnLib/src/Library/Species/SpeciesLibrary.cpp

25 lines
937 B
C++
Raw Normal View History

2019-12-29 14:29:52 +00:00
#include "SpeciesLibrary.hpp"
namespace PkmnLib::Library {
2020-12-12 13:25:27 +00:00
std::optional<ArbUt::BorrowedPtr<const PokemonSpecies>>
SpeciesLibrary::FindPreEvolution(const ArbUt::BorrowedPtr<const PokemonSpecies>& species) const noexcept {
auto tryGet = _preEvolutionCache.TryGet(species);
if (tryGet.has_value()) {
return tryGet->get();
}
2020-12-12 13:25:27 +00:00
for (const auto& s : _values) {
const auto* pkmn = dynamic_cast<const PokemonSpecies* nullable>(s.second.get());
2021-07-09 12:36:14 +00:00
if (pkmn == nullptr) {
continue;
}
2020-12-12 13:25:27 +00:00
for (const auto& evo : pkmn->GetEvolutions()) {
if (evo->GetNewSpecies() == species) {
2020-12-12 13:25:27 +00:00
auto* non_const = const_cast<SpeciesLibrary*>(this);
non_const->_preEvolutionCache.Insert(species, pkmn);
return pkmn;
}
}
}
2020-12-12 13:25:27 +00:00
return {};
}
}