25 lines
		
	
	
		
			938 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			938 B
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "SpeciesLibrary.hpp"
 | |
| namespace PkmnLib::Library {
 | |
|     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();
 | |
|         }
 | |
|         for (const auto& s : _values) {
 | |
|             const auto* pkmn = dynamic_cast<const PokemonSpecies * nullable>(s.second.get());
 | |
|             if (pkmn == nullptr) {
 | |
|                 continue;
 | |
|             }
 | |
|             for (const auto& evo : pkmn->GetEvolutions()) {
 | |
|                 if (evo->GetNewSpecies() == species) {
 | |
|                     auto* non_const = const_cast<SpeciesLibrary*>(this);
 | |
|                     non_const->_preEvolutionCache.Insert(species, pkmn);
 | |
|                     return pkmn;
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         return {};
 | |
|     }
 | |
| }
 |