using System;
namespace PKHeX.Core;
///
/// Evolution Reversal logic
///
public static class EvolutionReversal
{
///
/// Reverses from current state to see what evolutions the may have existed as.
///
/// Evolution Method lineage reversal object
/// Species to devolve from
/// Form to devolve from
/// Entity reference to sanity check evolutions with
/// Minimum level the entity may exist as
/// Maximum the entity may exist as
/// Maximum species ID that may exist
/// Option to bypass some evolution criteria
/// Species ID that should be the last node, if at all.
/// Reversed evolution lineage, with the lowest index being the current species-form.
public static EvoCriteria[] Reverse(this IEvolutionLookup lineage, ushort species, byte form, PKM pk, byte levelMin, byte levelMax, int maxSpeciesID, bool skipChecks, int stopSpecies)
{
// Sometimes we have to sanitize the inputs.
switch (species)
{
case (int)Species.Silvally:
form = 0;
break;
}
// Store our results -- trim at the end when we place it on the heap.
const int maxEvolutions = 3;
Span evos = stackalloc EvoCriteria[maxEvolutions];
var lvl = levelMax; // highest level, any level-up method will decrease.
evos[0] = new EvoCriteria { Species = species, Form = form, LevelMax = lvl }; // current species-form
// There aren't any circular evolution paths, and all lineages have at most 3 evolutions total.
// There aren't any convergent evolution paths, so only yield the first connection.
int ctr = 1; // count in the 'evos' span
while (species != stopSpecies)
{
var key = EvolutionTree.GetLookupKey(species, form);
var node = lineage[key];
bool oneValid = false;
for (int i = 0; i < 2; i++)
{
ref var link = ref i == 0 ? ref node.First : ref node.Second;
if (link.IsEmpty)
break;
if (link.IsEvolutionBanned(pk) && !skipChecks)
continue;
var evo = link.Method;
if (!evo.Valid(pk, lvl, skipChecks))
continue;
if (evo.RequiresLevelUp && levelMin >= lvl)
break; // impossible evolution
UpdateMinValues(evos[..ctr], evo, levelMin);
species = link.Species;
form = link.Form;
evos[ctr++] = evo.GetEvoCriteria(species, form, lvl);
if (evo.RequiresLevelUp)
lvl--;
oneValid = true;
break;
}
if (!oneValid)
break;
}
// Remove future gen pre-evolutions; no Munchlax from a Gen3 Snorlax, no Pichu from a Gen1-only Raichu, etc
ref var last = ref evos[ctr - 1];
if (last.Species > maxSpeciesID)
{
for (int i = 0; i < ctr; i++)
{
if (evos[i].Species > maxSpeciesID)
continue;
ctr--;
break;
}
}
// Last species is the wild/hatched species, the minimum level is because it has not evolved from previous species
var result = evos[..ctr];
last = ref result[^1];
last = last with { LevelMin = levelMin, LevelUpRequired = 0 };
// Rectify minimum levels
RectifyMinimumLevels(result);
return result.ToArray();
}
private static void RectifyMinimumLevels(Span result)
{
for (int i = result.Length - 2; i >= 0; i--)
{
ref var evo = ref result[i];
var prev = result[i + 1];
var min = (byte)Math.Max(prev.LevelMin + evo.LevelUpRequired, evo.LevelMin);
evo = evo with { LevelMin = min };
}
}
private static void UpdateMinValues(Span evos, EvolutionMethod evo, byte minLevel)
{
ref var last = ref evos[^1];
if (!evo.RequiresLevelUp)
{
// Evolutions like elemental stones, trade, etc
last = last with { LevelMin = minLevel };
return;
}
if (evo.Level == 0)
{
// Friendship based Level Up Evolutions, Pichu -> Pikachu, Eevee -> Umbreon, etc
last = last with { LevelMin = (byte)(minLevel + 1) };
// Raichu from Pikachu would have a minimum level of 1; accounting for Pichu (level up required) results in a minimum level of 2
if (evos.Length > 1)
{
ref var first = ref evos[0];
if (!first.RequiresLvlUp)
first = first with { LevelMin = (byte)(minLevel + 1) };
}
}
else // level up evolutions
{
last = last with { LevelMin = evo.Level };
if (evos.Length > 1)
{
ref var first = ref evos[0];
if (first.RequiresLvlUp)
{
// Pokemon like Crobat, its minimum level is Golbat minimum level + 1
if (first.LevelMin <= evo.Level)
first = first with { LevelMin = (byte)(evo.Level + 1) };
}
else
{
// Pokemon like Nidoqueen who evolve with an evolution stone, minimum level is prior evolution minimum level
if (first.LevelMin < evo.Level)
first = first with { LevelMin = evo.Level };
}
}
}
last = last with { LevelUpRequired = evo.RequiresLevelUp ? (byte)1 : (byte)0 };
}
}