2018-06-20 00:14:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
using static PKHeX.Core.Legal;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
public static class EvolutionChain
|
|
|
|
|
{
|
2018-06-23 00:59:02 +00:00
|
|
|
|
private static readonly List<EvoCriteria> NONE = new List<EvoCriteria>(0);
|
|
|
|
|
internal static IReadOnlyList<EvoCriteria>[] GetEvolutionChainsAllGens(PKM pkm, IEncounterable Encounter)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-21 04:38:56 +00:00
|
|
|
|
var CompleteEvoChain = GetEvolutionChain(pkm, Encounter, pkm.Species, pkm.CurrentLevel);
|
2018-06-23 03:16:31 +00:00
|
|
|
|
if (Encounter is EncounterInvalid || pkm.IsEgg || CompleteEvoChain.Count == 0)
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return GetChainSingle(pkm, CompleteEvoChain);
|
2018-06-20 00:50:10 +00:00
|
|
|
|
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return GetChainAll(pkm, Encounter, CompleteEvoChain);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 00:59:02 +00:00
|
|
|
|
private static List<EvoCriteria>[] GetChainBase(int maxgen)
|
2018-06-21 04:38:56 +00:00
|
|
|
|
{
|
2018-06-23 00:59:02 +00:00
|
|
|
|
var GensEvoChains = new List<EvoCriteria>[maxgen + 1];
|
2018-06-20 00:14:22 +00:00
|
|
|
|
for (int i = 0; i <= maxgen; i++)
|
2018-06-20 00:50:10 +00:00
|
|
|
|
GensEvoChains[i] = NONE; // default no-evolutions
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return GensEvoChains;
|
|
|
|
|
}
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
2018-06-23 00:59:02 +00:00
|
|
|
|
private static List<EvoCriteria>[] GetChainSingle(PKM pkm, List<EvoCriteria> CompleteEvoChain)
|
2018-06-21 04:38:56 +00:00
|
|
|
|
{
|
|
|
|
|
var chain = GetChainBase(pkm.Format);
|
|
|
|
|
chain[pkm.Format] = CompleteEvoChain;
|
|
|
|
|
return chain;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-21 04:38:56 +00:00
|
|
|
|
|
2018-06-23 00:59:02 +00:00
|
|
|
|
private static List<EvoCriteria>[] GetChainAll(PKM pkm, IEncounterable Encounter, IEnumerable<EvoCriteria> CompleteEvoChain)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-21 04:38:56 +00:00
|
|
|
|
int maxgen = pkm is PK1 && !pkm.Gen1_NotTradeback ? 2 : pkm.Format;
|
|
|
|
|
var GensEvoChains = GetChainBase(maxgen);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
2018-06-21 01:25:23 +00:00
|
|
|
|
var queue = new Queue<EvoCriteria>(CompleteEvoChain);
|
|
|
|
|
var mostEvolved = queue.Dequeue();
|
|
|
|
|
|
2018-06-21 04:38:56 +00:00
|
|
|
|
int lvl = pkm.CurrentLevel;
|
|
|
|
|
int maxLevel = lvl;
|
|
|
|
|
int pkGen = pkm.GenNumber;
|
|
|
|
|
|
2018-06-21 01:25:23 +00:00
|
|
|
|
// Iterate generations backwards
|
|
|
|
|
// Maximum level of an earlier generation (GenX) will never be greater than a later generation (GenX+Y).
|
2018-06-21 04:38:56 +00:00
|
|
|
|
int mingen = (pkm is PK2 || pkm.VC2) && !pkm.Gen2_NotTradeback ? 1 : pkGen;
|
2018-06-23 03:16:31 +00:00
|
|
|
|
bool noxfrDecremented = true;
|
2018-06-21 04:38:56 +00:00
|
|
|
|
for (int g = GensEvoChains.Length - 1; g >= mingen; g--)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkGen == 1 && pkm.Gen1_NotTradeback && g == 2)
|
|
|
|
|
continue;
|
|
|
|
|
if (pkGen <= 2 && 3 <= g && g <= 6)
|
|
|
|
|
continue;
|
2018-06-20 00:50:10 +00:00
|
|
|
|
|
2018-06-21 01:25:23 +00:00
|
|
|
|
if (g <= 4 && 2 < pkm.Format && g < pkm.Format && !pkm.HasOriginalMetLocation && lvl > pkm.Met_Level)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
|
|
|
|
// Met location was lost at this point but it also means the pokemon existed in generations 1 to 4 with maximum level equals to met level
|
|
|
|
|
lvl = pkm.Met_Level;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 01:25:23 +00:00
|
|
|
|
int maxspeciesgen = g == 2 && pkm.VC1 ? MaxSpeciesID_1 : GetMaxSpeciesOrigin(g);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
|
|
|
|
// Remove future gen evolutions after a few special considerations:
|
|
|
|
|
// If the pokemon origin is illegal (e.g. Gen3 Infernape) the list will be emptied -- species lineage did not exist at any evolution stage.
|
2018-06-21 01:25:23 +00:00
|
|
|
|
while (mostEvolved.Species > maxspeciesgen)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-21 04:43:54 +00:00
|
|
|
|
if (queue.Count == 0)
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return GensEvoChains;
|
2018-06-21 01:25:23 +00:00
|
|
|
|
if (mostEvolved.RequiresLvlUp)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-23 03:16:31 +00:00
|
|
|
|
// This is a gen 3 pokemon in a gen 4 phase evolution that requires level up and then transfered to gen 5+
|
2018-06-20 00:14:22 +00:00
|
|
|
|
// We can deduce that it existed in gen 4 until met level,
|
|
|
|
|
// but if current level is met level we can also deduce it existed in gen 3 until maximum met level -1
|
2018-06-23 03:16:31 +00:00
|
|
|
|
if (g == 3 && pkm.Format > 4 && lvl == maxLevel)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
lvl--;
|
|
|
|
|
|
|
|
|
|
// The same condition for gen2 evolution of gen 1 pokemon, level of the pokemon in gen 1 games would be CurrentLevel -1 one level below gen 2 level
|
2018-06-21 02:42:45 +00:00
|
|
|
|
else if (g == 1 && pkm.Format == 2 && lvl == maxLevel)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
lvl--;
|
|
|
|
|
}
|
2018-06-21 01:25:23 +00:00
|
|
|
|
mostEvolved = queue.Dequeue();
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Alolan form evolutions, remove from gens 1-6 chains
|
2018-06-21 01:25:23 +00:00
|
|
|
|
if (EvolveToAlolanForms.Contains(mostEvolved.Species))
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-21 01:25:23 +00:00
|
|
|
|
if (g < 7 && pkm.Format >= 7 && mostEvolved.Form > 0)
|
|
|
|
|
{
|
2018-06-21 04:43:54 +00:00
|
|
|
|
if (queue.Count == 0)
|
2018-06-21 01:25:23 +00:00
|
|
|
|
break;
|
|
|
|
|
mostEvolved = queue.Dequeue();
|
|
|
|
|
}
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 01:25:23 +00:00
|
|
|
|
GensEvoChains[g] = GetEvolutionChain(pkm, Encounter, mostEvolved.Species, lvl);
|
2018-06-23 00:59:02 +00:00
|
|
|
|
if (GensEvoChains[g].Count == 0)
|
2018-06-20 00:50:10 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
2018-06-23 03:16:31 +00:00
|
|
|
|
if (g > 2 && !pkm.HasOriginalMetLocation && g >= pkGen && noxfrDecremented)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-23 03:16:31 +00:00
|
|
|
|
bool isTransferred = HasMetLocationUpdatedTransfer(pkGen, g);
|
2018-06-23 02:40:17 +00:00
|
|
|
|
if (!isTransferred)
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-06-23 03:16:31 +00:00
|
|
|
|
noxfrDecremented = false;
|
|
|
|
|
|
2018-06-23 02:40:17 +00:00
|
|
|
|
// Remove previous evolutions below transfer level
|
|
|
|
|
// For example a gen3 Charizard in format 7 with current level 36 and met level 36, thus could never be Charmander / Charmeleon in Gen5+.
|
|
|
|
|
// chain level for charmander is 35, is below met level.
|
|
|
|
|
int minlvl = GetMinLevelGeneration(pkm, g);
|
|
|
|
|
GensEvoChains[g].RemoveAll(e => e.Level < minlvl);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-20 00:50:10 +00:00
|
|
|
|
else if (g == 2 && pkm.TradebackStatus == TradebackType.Gen1_NotTradeback)
|
|
|
|
|
{
|
|
|
|
|
GensEvoChains[2] = NONE;
|
|
|
|
|
}
|
2018-06-23 02:40:17 +00:00
|
|
|
|
else if (g == 1)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-23 02:40:17 +00:00
|
|
|
|
int lastIndex = GensEvoChains[1].Count - 1;
|
|
|
|
|
if (GensEvoChains[1][lastIndex].Species <= MaxSpeciesID_1)
|
|
|
|
|
return GensEvoChains;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
// Remove generation 2 pre-evolutions
|
2018-06-23 02:40:17 +00:00
|
|
|
|
GensEvoChains[1].RemoveAt(lastIndex);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
if (!pkm.VC1)
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return GensEvoChains;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
|
|
|
|
// Remove generation 2 pre-evolutions from gen 7 and future generations
|
2018-06-21 04:38:56 +00:00
|
|
|
|
for (int fgen = 7; fgen < GensEvoChains.Length; fgen++)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
|
|
|
|
var chain = GensEvoChains[fgen];
|
2018-06-23 00:59:02 +00:00
|
|
|
|
int g1Index = -1;
|
|
|
|
|
for (int i = 0; i < chain.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (chain[i].Species > MaxSpeciesID_1)
|
|
|
|
|
continue;
|
|
|
|
|
g1Index = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-06-20 00:14:22 +00:00
|
|
|
|
if (g1Index < 0) // no g2 species present
|
|
|
|
|
continue;
|
2018-06-23 00:59:02 +00:00
|
|
|
|
if (g1Index + 1 == chain.Count) // already pruned or no g2prevo
|
2018-06-20 00:14:22 +00:00
|
|
|
|
continue;
|
2018-06-23 02:40:17 +00:00
|
|
|
|
|
|
|
|
|
int count = chain.Count;
|
|
|
|
|
for (int i = g1Index + 1; i < count; i++)
|
|
|
|
|
GensEvoChains[fgen].RemoveAt(i);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return GensEvoChains;
|
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
|
|
|
|
internal static int GetEvoChainSpeciesIndex(IReadOnlyList<EvoCriteria> chain, int species)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < chain.Count; i++)
|
|
|
|
|
if (chain[i].Species == species)
|
|
|
|
|
return i;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
private static List<EvoCriteria> GetEvolutionChain(PKM pkm, IEncounterable Encounter, int maxspec, int maxlevel)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-23 00:59:02 +00:00
|
|
|
|
var vs = GetValidPreEvolutions(pkm);
|
|
|
|
|
if (Encounter.Species == maxspec)
|
|
|
|
|
return vs;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
|
|
|
|
// Evolution chain is in reverse order (devolution)
|
2018-06-23 00:59:02 +00:00
|
|
|
|
// Find the index of the minimum species to determine the end of the chain
|
|
|
|
|
int minindex = GetEvoChainSpeciesIndex(vs, Encounter.Species);
|
|
|
|
|
bool last = minindex == vs.Count - 1;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
2018-06-23 00:59:02 +00:00
|
|
|
|
// If we remove a pre-evolution, update the chain if appropriate.
|
|
|
|
|
if (!last)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-23 00:59:02 +00:00
|
|
|
|
// Remove chain species after the encounter
|
|
|
|
|
int count = vs.Count;
|
|
|
|
|
for (int i = minindex + 1; i < count; i++)
|
|
|
|
|
vs.RemoveAt(i);
|
|
|
|
|
|
|
|
|
|
if (vs.Count == 0)
|
|
|
|
|
return vs; // no species left in chain
|
|
|
|
|
CheckLastEncounterRemoval(Encounter, vs);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
|
|
|
|
// maxspec is used to remove future gen evolutions, to gather evolution chain of a pokemon in previous generations
|
|
|
|
|
int skip = Math.Max(0, GetEvoChainSpeciesIndex(vs, maxspec));
|
|
|
|
|
for (int i = 0; i < skip; i++)
|
|
|
|
|
vs.RemoveAt(0);
|
|
|
|
|
|
2018-06-20 00:14:22 +00:00
|
|
|
|
// Maxlevel is also used for previous generations, it removes evolutions imposible before the transfer level
|
|
|
|
|
// For example a fire red charizard whose current level in XY is 50 but met level is 20, it couldnt be a Charizard in gen 3 and 4 games
|
2018-06-23 00:59:02 +00:00
|
|
|
|
vs.RemoveAll(e => e.MinLevel > maxlevel);
|
|
|
|
|
|
|
|
|
|
// Reduce the evolution chain levels to max level to limit any later analysis/results.
|
2018-06-20 00:14:22 +00:00
|
|
|
|
foreach (var d in vs)
|
|
|
|
|
d.Level = Math.Min(d.Level, maxlevel);
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2018-06-20 00:14:22 +00:00
|
|
|
|
return vs;
|
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
|
|
|
|
private static void CheckLastEncounterRemoval(IEncounterable Encounter, IReadOnlyList<EvoCriteria> vs)
|
|
|
|
|
{
|
|
|
|
|
// Last entry from vs is removed, turn next entry into the encountered Pokémon
|
|
|
|
|
var last = vs[vs.Count - 1];
|
|
|
|
|
last.MinLevel = Encounter.LevelMin;
|
|
|
|
|
last.RequiresLvlUp = false;
|
|
|
|
|
|
|
|
|
|
var first = vs[0];
|
|
|
|
|
if (first.RequiresLvlUp)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (first.MinLevel == 2)
|
|
|
|
|
{
|
|
|
|
|
// Example: Raichu in Gen 2 or later
|
|
|
|
|
// Because Pichu requires a level up, the minimum level of the resulting Raichu must be be >2
|
|
|
|
|
// But after removing Pichu (because the origin species is Pikachu), the Raichu minimum level should be 1.
|
|
|
|
|
first.MinLevel = 1;
|
|
|
|
|
first.RequiresLvlUp = false;
|
|
|
|
|
}
|
|
|
|
|
else // ingame trade / stone can evolve immediately
|
|
|
|
|
{
|
|
|
|
|
first.MinLevel = last.MinLevel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static List<EvoCriteria> GetValidPreEvolutions(PKM pkm, int maxspeciesorigin = -1, int lvl = -1, bool skipChecks = false)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (lvl < 0)
|
|
|
|
|
lvl = pkm.CurrentLevel;
|
|
|
|
|
if (pkm.IsEgg && !skipChecks)
|
|
|
|
|
return new List<EvoCriteria>
|
|
|
|
|
{
|
|
|
|
|
new EvoCriteria { Species = pkm.Species, Level = lvl, MinLevel = lvl },
|
|
|
|
|
};
|
|
|
|
|
if (pkm.Species == 292 && lvl >= 20 && (!pkm.HasOriginalMetLocation || pkm.Met_Level + 1 <= lvl))
|
|
|
|
|
return new List<EvoCriteria>
|
|
|
|
|
{
|
|
|
|
|
new EvoCriteria { Species = 292, Level = lvl, MinLevel = 20 },
|
|
|
|
|
new EvoCriteria { Species = 290, Level = pkm.GenNumber < 5 ? lvl : lvl-1, MinLevel = 1 }
|
|
|
|
|
// Shedinja spawns after evolving, which is after level up moves were prompted. Not for future generations.
|
|
|
|
|
};
|
|
|
|
|
if (maxspeciesorigin == -1 && pkm.InhabitedGeneration(2) && pkm.GenNumber == 1)
|
|
|
|
|
maxspeciesorigin = MaxSpeciesID_2;
|
|
|
|
|
|
|
|
|
|
int tree = maxspeciesorigin == MaxSpeciesID_2 ? 2 : pkm.Format;
|
|
|
|
|
var et = EvolutionTree.GetEvolutionTree(tree);
|
|
|
|
|
return et.GetValidPreEvolutions(pkm, maxLevel: lvl, maxSpeciesOrigin: maxspeciesorigin, skipChecks: skipChecks);
|
|
|
|
|
}
|
|
|
|
|
private static int GetMinLevelGeneration(PKM pkm, int generation)
|
|
|
|
|
{
|
|
|
|
|
if (!pkm.InhabitedGeneration(generation))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (pkm.Format <= 2)
|
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
|
|
if (!pkm.HasOriginalMetLocation && generation != pkm.GenNumber)
|
|
|
|
|
return pkm.Met_Level;
|
|
|
|
|
|
|
|
|
|
if (pkm.GenNumber <= 3)
|
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an enumerable list of species IDs from the possible lineage of the <see cref="pkm"/> based on its current format.
|
|
|
|
|
/// </summary>
|
2018-06-20 00:50:10 +00:00
|
|
|
|
/// <param name="pkm">Pokémon to fetch the lineage for.</param>
|
2018-06-20 00:14:22 +00:00
|
|
|
|
internal static IEnumerable<int> GetLineage(PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
if (pkm.IsEgg)
|
|
|
|
|
return new[] { pkm.Species };
|
|
|
|
|
|
|
|
|
|
var table = EvolutionTree.GetEvolutionTree(pkm.Format);
|
|
|
|
|
var lineage = table.GetValidPreEvolutions(pkm, maxLevel: pkm.CurrentLevel);
|
|
|
|
|
return lineage.Select(evolution => evolution.Species);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|