using System.Collections.Generic;
namespace PKHeX.Core;
///
/// Exposes abstractions for reverse and forward evolution lookups.
///
public interface IEvolutionNetwork
{
///
/// Provides interactions to look forward in an evolution tree.
///
IEvolutionForward Forward { get; }
///
/// Provides interactions to look backward in an evolution tree.
///
IEvolutionReverse Reverse { get; }
}
///
/// Base abstraction for
///
public abstract class EvolutionNetwork : IEvolutionNetwork
{
public IEvolutionForward Forward { get; }
public IEvolutionReverse Reverse { get; }
protected EvolutionNetwork(IEvolutionForward forward, IEvolutionReverse reverse)
{
Forward = forward;
Reverse = reverse;
}
///
/// Gets all species the - can evolve to & from, yielded in order of increasing evolution stage.
///
/// Species ID
/// Form ID
/// Enumerable of species IDs (with the Form IDs included, left shifted by 11).
public IEnumerable<(ushort Species, byte Form)> GetEvolutionsAndPreEvolutions(ushort species, byte form)
{
foreach (var s in Reverse.GetPreEvolutions(species, form))
yield return s;
yield return (species, form);
foreach (var s in Forward.GetEvolutions(species, form))
yield return s;
}
///
/// Checks if the requested - is can provide any common evolutions with the -.
///
public bool IsSpeciesDerivedFrom(ushort species, byte form, ushort otherSpecies, byte otherForm, bool ignoreForm = true)
{
var evos = GetEvolutionsAndPreEvolutions(species, form);
foreach (var (s, f) in evos)
{
if (s != otherSpecies)
continue;
if (ignoreForm)
return true;
return f == otherForm;
}
return false;
}
///
/// Gets the base (baby) species and form of the given - pair.
///
public (ushort Species, byte Form) GetBaseSpeciesForm(ushort species, byte form)
{
var chain = Reverse.GetPreEvolutions(species, form);
foreach (var evo in chain)
return evo;
return (species, form);
}
}