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
|
|
|
|
|
{
|
2020-12-22 01:17:56 +00:00
|
|
|
|
private static readonly List<EvoCriteria> NONE = new(0);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
internal static IReadOnlyList<EvoCriteria>[] GetEvolutionChainsAllGens(PKM pkm, IEncounterTemplate enc)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2021-08-01 05:38:04 +00:00
|
|
|
|
var chain = GetEvolutionChain(pkm, enc, pkm.Species, pkm.CurrentLevel);
|
|
|
|
|
if (enc is EncounterInvalid || pkm.IsEgg || chain.Count == 0)
|
|
|
|
|
return GetChainSingle(pkm, chain);
|
2018-06-20 00:50:10 +00:00
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
return GetChainAll(pkm, enc, chain);
|
2018-06-21 04:38:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
private static List<EvoCriteria>[] GetChainSingle(PKM pkm, List<EvoCriteria> fullChain)
|
2018-06-21 04:38:56 +00:00
|
|
|
|
{
|
2018-08-30 04:58:44 +00:00
|
|
|
|
var chain = GetChainBase(Math.Max(2, pkm.Format));
|
2021-08-01 05:38:04 +00:00
|
|
|
|
chain[pkm.Format] = fullChain;
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return chain;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-21 04:38:56 +00:00
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
private static List<EvoCriteria>[] GetChainAll(PKM pkm, IEncounterTemplate enc, IReadOnlyList<EvoCriteria> fullChain)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2021-08-20 22:59:54 +00:00
|
|
|
|
int maxgen = ParseSettings.AllowGen1Tradeback && pkm is PK1 ? 2 : pkm.Format;
|
2018-06-21 04:38:56 +00:00
|
|
|
|
var GensEvoChains = GetChainBase(maxgen);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
var queue = new Queue<EvoCriteria>(fullChain);
|
2018-06-21 01:25:23 +00:00
|
|
|
|
var mostEvolved = queue.Dequeue();
|
|
|
|
|
|
2018-06-21 04:38:56 +00:00
|
|
|
|
int lvl = pkm.CurrentLevel;
|
|
|
|
|
int maxLevel = lvl;
|
2020-06-28 03:18:29 +00:00
|
|
|
|
int pkGen = enc.Generation;
|
2018-06-21 04:38:56 +00:00
|
|
|
|
|
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).
|
2021-08-20 22:59:54 +00:00
|
|
|
|
int mingen = pkGen >= 3 ? pkGen : GBRestrictions.GetTradebackStatusInitial(pkm) == PotentialGBOrigin.Gen2Only ? 2 : 1;
|
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
|
|
|
|
{
|
2020-09-06 17:53:13 +00:00
|
|
|
|
if (pkGen <= 2 && g == 6)
|
|
|
|
|
g = 2;
|
2020-05-31 19:12:48 +00:00
|
|
|
|
|
|
|
|
|
if (g <= 4 && pkm.Format > 2 && pkm.Format > g && !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-07-19 23:42:45 +00:00
|
|
|
|
{
|
|
|
|
|
if (g <= 2 && pkm.VC1)
|
|
|
|
|
GensEvoChains[pkm.Format] = NONE; // invalidate here since we haven't reached the regular invalidation
|
2018-06-21 04:38:56 +00:00
|
|
|
|
return GensEvoChains;
|
2018-07-19 23:42:45 +00:00
|
|
|
|
}
|
2018-06-21 01:25:23 +00:00
|
|
|
|
if (mostEvolved.RequiresLvlUp)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2019-02-24 21:57:10 +00:00
|
|
|
|
// This is a Gen3 pokemon in a Gen4 phase evolution that requires level up and then transferred to Gen5+
|
|
|
|
|
// We can deduce that it existed in Gen4 until met level,
|
|
|
|
|
// but if current level is met level we can also deduce it existed in Gen3 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--;
|
|
|
|
|
|
2019-02-24 21:57:10 +00:00
|
|
|
|
// The same condition for Gen2 evolution of Gen1 pokemon, level of the pokemon in Gen1 games would be CurrentLevel -1 one level below Gen2 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
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 03:18:29 +00:00
|
|
|
|
GensEvoChains[g] = GetEvolutionChain(pkm, enc, 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 06:18:52 +00:00
|
|
|
|
noxfrDecremented = g > (pkGen != 3 ? 4 : 5);
|
2018-06-23 03:16:31 +00:00
|
|
|
|
|
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+.
|
2019-02-24 21:57:10 +00:00
|
|
|
|
// chain level for Charmander is 35, is below met level.
|
2018-06-23 02:40:17 +00:00
|
|
|
|
int minlvl = GetMinLevelGeneration(pkm, g);
|
|
|
|
|
GensEvoChains[g].RemoveAll(e => e.Level < minlvl);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-23 02:40:17 +00:00
|
|
|
|
else if (g == 1)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2018-06-23 05:48:10 +00:00
|
|
|
|
// Remove Gen2 post-evolutions (Scizor, Blissey...)
|
|
|
|
|
if (GensEvoChains[1][0].Species > MaxSpeciesID_1)
|
|
|
|
|
GensEvoChains[1].RemoveAt(0);
|
|
|
|
|
|
|
|
|
|
// Remove Gen2 pre-evolutions (Pichu, Cleffa...)
|
2018-06-23 02:40:17 +00:00
|
|
|
|
int lastIndex = GensEvoChains[1].Count - 1;
|
2018-06-23 05:48:10 +00:00
|
|
|
|
if (lastIndex >= 0 && GensEvoChains[1][lastIndex].Species > MaxSpeciesID_1)
|
|
|
|
|
GensEvoChains[1].RemoveAt(lastIndex);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
|
2018-06-23 05:48:10 +00:00
|
|
|
|
// Remove Gen7 pre-evolutions and chain break scenarios
|
|
|
|
|
if (pkm.VC1)
|
|
|
|
|
TrimVC1Transfer(pkm, GensEvoChains);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return GensEvoChains;
|
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2021-06-03 19:04:19 +00:00
|
|
|
|
private static bool HasMetLocationUpdatedTransfer(int originalGeneration, int currentGeneration) => originalGeneration switch
|
2020-11-02 22:46:26 +00:00
|
|
|
|
{
|
2021-06-03 19:04:19 +00:00
|
|
|
|
< 3 => currentGeneration >= 3,
|
|
|
|
|
<= 4 => currentGeneration != originalGeneration,
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => false,
|
2021-06-03 19:04:19 +00:00
|
|
|
|
};
|
2020-11-02 22:46:26 +00:00
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
private static void TrimVC1Transfer(PKM pkm, IList<List<EvoCriteria>> allChains)
|
2018-06-23 05:48:10 +00:00
|
|
|
|
{
|
2021-08-01 05:38:04 +00:00
|
|
|
|
if (allChains[7].All(z => z.Species > MaxSpeciesID_1))
|
|
|
|
|
allChains[pkm.Format] = NONE; // needed a Gen1 species present; invalidate the chain.
|
2018-06-23 05:48:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
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++)
|
2018-08-03 03:11:42 +00:00
|
|
|
|
{
|
2018-06-23 00:59:02 +00:00
|
|
|
|
if (chain[i].Species == species)
|
|
|
|
|
return i;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2021-08-01 05:38:04 +00:00
|
|
|
|
private static List<EvoCriteria> GetEvolutionChain(PKM pkm, IEncounterTemplate enc, int mostEvolvedSpecies, int maxlevel)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2021-08-01 05:38:04 +00:00
|
|
|
|
var chain = GetValidPreEvolutions(pkm, minLevel: enc.LevelMin);
|
|
|
|
|
if (enc.Species == mostEvolvedSpecies)
|
2018-06-23 06:18:52 +00:00
|
|
|
|
{
|
2020-06-28 03:18:29 +00:00
|
|
|
|
if (chain.Count != 1)
|
2018-06-28 00:41:53 +00:00
|
|
|
|
{
|
2021-08-01 05:38:04 +00:00
|
|
|
|
chain.RemoveAll(z => z.Species != enc.Species);
|
|
|
|
|
chain[0].MinLevel = enc.LevelMin;
|
2018-06-28 00:41:53 +00:00
|
|
|
|
}
|
2020-06-28 03:18:29 +00:00
|
|
|
|
return chain;
|
2018-06-23 06:18:52 +00:00
|
|
|
|
}
|
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
|
2021-08-01 05:38:04 +00:00
|
|
|
|
int minIndex = GetEvoChainSpeciesIndex(chain, enc.Species);
|
|
|
|
|
bool last = minIndex < 0 || minIndex == chain.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
|
2020-06-28 03:18:29 +00:00
|
|
|
|
int count = chain.Count;
|
2021-08-01 05:38:04 +00:00
|
|
|
|
for (int i = minIndex + 1; i < count; i++)
|
2020-06-28 03:18:29 +00:00
|
|
|
|
chain.RemoveAt(i);
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2020-06-28 03:18:29 +00:00
|
|
|
|
if (chain.Count == 0)
|
|
|
|
|
return chain; // no species left in chain
|
2021-08-01 05:38:04 +00:00
|
|
|
|
CheckLastEncounterRemoval(enc, chain);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2019-02-24 21:57:10 +00:00
|
|
|
|
// maxspec is used to remove future geneneration evolutions, to gather evolution chain of a pokemon in previous generations
|
2021-08-01 05:38:04 +00:00
|
|
|
|
int skip = Math.Max(0, GetEvoChainSpeciesIndex(chain, mostEvolvedSpecies));
|
2018-06-23 00:59:02 +00:00
|
|
|
|
for (int i = 0; i < skip; i++)
|
2020-06-28 03:18:29 +00:00
|
|
|
|
chain.RemoveAt(0);
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2019-02-24 21:57:10 +00:00
|
|
|
|
// Gen3->4 and Gen4->5 transfer sets the Met Level property to the Pokémon's current level.
|
|
|
|
|
// Removes evolutions impossible before the transfer level.
|
|
|
|
|
// For example a FireRed Charizard with a current level (in XY) is 50 but Met Level is 20; it couldn't be a Charizard in Gen3 and Gen4 games
|
2020-06-28 03:18:29 +00:00
|
|
|
|
chain.RemoveAll(e => e.MinLevel > maxlevel);
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
|
|
|
|
// Reduce the evolution chain levels to max level to limit any later analysis/results.
|
2020-06-28 03:18:29 +00:00
|
|
|
|
foreach (var d in chain)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
d.Level = Math.Min(d.Level, maxlevel);
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2020-06-28 03:18:29 +00:00
|
|
|
|
return chain;
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-06-23 00:59:02 +00:00
|
|
|
|
|
2021-07-14 00:33:23 +00:00
|
|
|
|
private static void CheckLastEncounterRemoval(IEncounterTemplate enc, IReadOnlyList<EvoCriteria> chain)
|
2018-06-23 00:59:02 +00:00
|
|
|
|
{
|
2020-06-28 03:18:29 +00:00
|
|
|
|
// Last entry from chain is removed, turn next entry into the encountered Pokémon
|
2021-05-14 22:30:55 +00:00
|
|
|
|
var last = chain[^1];
|
2020-06-28 03:18:29 +00:00
|
|
|
|
last.MinLevel = enc.LevelMin;
|
2018-06-23 00:59:02 +00:00
|
|
|
|
last.RequiresLvlUp = false;
|
|
|
|
|
|
2020-06-28 03:18:29 +00:00
|
|
|
|
var first = chain[0];
|
2018-06-23 00:59:02 +00:00
|
|
|
|
if (first.RequiresLvlUp)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (first.MinLevel == 2)
|
|
|
|
|
{
|
2019-02-24 21:57:10 +00:00
|
|
|
|
// Example: Raichu in Gen2 or later
|
2018-06-23 00:59:02 +00:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
2019-02-24 21:57:10 +00:00
|
|
|
|
else // in-game trade or evolution stone can evolve immediately
|
2018-06-23 00:59:02 +00:00
|
|
|
|
{
|
|
|
|
|
first.MinLevel = last.MinLevel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 03:18:29 +00:00
|
|
|
|
internal static List<EvoCriteria> GetValidPreEvolutions(PKM pkm, int maxspeciesorigin = -1, int maxLevel = -1, int minLevel = 1, bool skipChecks = false)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
{
|
2020-06-28 03:18:29 +00:00
|
|
|
|
if (maxLevel < 0)
|
|
|
|
|
maxLevel = pkm.CurrentLevel;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2020-12-11 04:42:30 +00:00
|
|
|
|
if (maxspeciesorigin == -1 && pkm.InhabitedGeneration(2) && pkm.Format <= 2 && pkm.Generation == 1)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
maxspeciesorigin = MaxSpeciesID_2;
|
|
|
|
|
|
2020-01-04 19:14:25 +00:00
|
|
|
|
int tree = Math.Max(2, pkm.Format);
|
2018-11-14 03:04:58 +00:00
|
|
|
|
var et = EvolutionTree.GetEvolutionTree(pkm, tree);
|
2020-06-28 03:18:29 +00:00
|
|
|
|
return et.GetValidPreEvolutions(pkm, maxLevel: maxLevel, maxSpeciesOrigin: maxspeciesorigin, skipChecks: skipChecks, minLevel: minLevel);
|
2018-06-20 00:14:22 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-06-20 00:14:22 +00:00
|
|
|
|
private static int GetMinLevelGeneration(PKM pkm, int generation)
|
|
|
|
|
{
|
|
|
|
|
if (!pkm.InhabitedGeneration(generation))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (pkm.Format <= 2)
|
|
|
|
|
return 2;
|
|
|
|
|
|
2020-12-11 04:42:30 +00:00
|
|
|
|
var origin = pkm.Generation;
|
2019-02-22 04:41:04 +00:00
|
|
|
|
if (!pkm.HasOriginalMetLocation && generation != origin)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
return pkm.Met_Level;
|
|
|
|
|
|
2019-09-10 07:21:51 +00:00
|
|
|
|
// gen 3 and prior can't obtain anything at level 1
|
2019-02-22 04:41:04 +00:00
|
|
|
|
if (origin <= 3)
|
2018-06-20 00:14:22 +00:00
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|