2016-11-08 16:43:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-11-26 06:51:07 +00:00
|
|
|
|
using static PKHeX.Core.GameVersion;
|
2016-11-08 16:43:57 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generation specific Evolution Tree data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Used to determine if a <see cref="PKM.Species"/> can evolve from prior steps in its evolution branch.
|
|
|
|
|
/// </remarks>
|
2018-07-02 02:55:23 +00:00
|
|
|
|
public sealed class EvolutionTree
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
2017-07-29 18:54:52 +00:00
|
|
|
|
private static readonly EvolutionTree Evolves1;
|
|
|
|
|
private static readonly EvolutionTree Evolves2;
|
|
|
|
|
private static readonly EvolutionTree Evolves3;
|
|
|
|
|
private static readonly EvolutionTree Evolves4;
|
|
|
|
|
private static readonly EvolutionTree Evolves5;
|
|
|
|
|
private static readonly EvolutionTree Evolves6;
|
|
|
|
|
private static readonly EvolutionTree Evolves7;
|
2018-11-14 03:04:58 +00:00
|
|
|
|
private static readonly EvolutionTree Evolves7b;
|
2019-09-23 23:56:47 +00:00
|
|
|
|
private static readonly EvolutionTree Evolves8;
|
2017-07-29 18:54:52 +00:00
|
|
|
|
|
|
|
|
|
static EvolutionTree()
|
|
|
|
|
{
|
|
|
|
|
// Evolution tables need Personal Tables initialized beforehand, hence why the EvolutionTree data is initialized here.
|
2019-10-08 01:40:09 +00:00
|
|
|
|
static byte[] get(string resource) => Util.GetBinaryResource($"evos_{resource}.pkl");
|
|
|
|
|
static byte[][] unpack(string resource) => Data.UnpackMini(get(resource), resource);
|
2017-07-29 18:54:52 +00:00
|
|
|
|
|
2019-11-26 06:51:07 +00:00
|
|
|
|
Evolves1 = new EvolutionTree(new[] { get("rby") }, Gen1, PersonalTable.Y, Legal.MaxSpeciesID_1);
|
|
|
|
|
Evolves2 = new EvolutionTree(new[] { get("gsc") }, Gen2, PersonalTable.C, Legal.MaxSpeciesID_2);
|
|
|
|
|
Evolves3 = new EvolutionTree(new[] { get("g3") }, Gen3, PersonalTable.RS, Legal.MaxSpeciesID_3);
|
|
|
|
|
Evolves4 = new EvolutionTree(new[] { get("g4") }, Gen4, PersonalTable.DP, Legal.MaxSpeciesID_4);
|
|
|
|
|
Evolves5 = new EvolutionTree(new[] { get("g5") }, Gen5, PersonalTable.BW, Legal.MaxSpeciesID_5);
|
|
|
|
|
Evolves6 = new EvolutionTree(unpack("ao"), Gen6, PersonalTable.AO, Legal.MaxSpeciesID_6);
|
|
|
|
|
Evolves7 = new EvolutionTree(unpack("uu"), Gen7, PersonalTable.USUM, Legal.MaxSpeciesID_7_USUM);
|
|
|
|
|
Evolves7b = new EvolutionTree(unpack("gg"), Gen7, PersonalTable.GG, Legal.MaxSpeciesID_7b);
|
|
|
|
|
Evolves8 = new EvolutionTree(unpack("ss"), Gen8, PersonalTable.SWSH, Legal.MaxSpeciesID_8);
|
2018-07-02 02:55:23 +00:00
|
|
|
|
|
|
|
|
|
// There's always oddballs.
|
|
|
|
|
Evolves7.FixEvoTreeSM();
|
2019-11-23 05:04:34 +00:00
|
|
|
|
Evolves8.FixEvoTreeSS();
|
2017-07-29 18:54:52 +00:00
|
|
|
|
}
|
2018-07-02 02:55:23 +00:00
|
|
|
|
|
2017-07-29 18:54:52 +00:00
|
|
|
|
internal static EvolutionTree GetEvolutionTree(int generation)
|
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
return generation switch
|
2017-07-29 18:54:52 +00:00
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
1 => Evolves1,
|
|
|
|
|
2 => Evolves2,
|
|
|
|
|
3 => Evolves3,
|
|
|
|
|
4 => Evolves4,
|
|
|
|
|
5 => Evolves5,
|
|
|
|
|
6 => Evolves6,
|
|
|
|
|
7 => Evolves7,
|
|
|
|
|
_ => Evolves8
|
|
|
|
|
};
|
2017-07-29 18:54:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-14 03:04:58 +00:00
|
|
|
|
internal static EvolutionTree GetEvolutionTree(PKM pkm, int generation)
|
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
return generation switch
|
2018-11-14 03:04:58 +00:00
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
1 => Evolves1,
|
|
|
|
|
2 => Evolves2,
|
|
|
|
|
3 => Evolves3,
|
|
|
|
|
4 => Evolves4,
|
|
|
|
|
5 => Evolves5,
|
|
|
|
|
6 => Evolves6,
|
|
|
|
|
7 => (pkm.GG ? Evolves7b : Evolves7),
|
|
|
|
|
_ => Evolves8
|
|
|
|
|
};
|
2018-11-14 03:04:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 17:36:28 +00:00
|
|
|
|
private readonly IReadOnlyList<EvolutionMethod[]> Entries;
|
2016-11-17 16:36:45 +00:00
|
|
|
|
private readonly EvolutionLineage[] Lineage;
|
2016-11-15 02:25:08 +00:00
|
|
|
|
private readonly GameVersion Game;
|
2016-11-08 16:43:57 +00:00
|
|
|
|
private readonly PersonalTable Personal;
|
2017-01-29 17:26:40 +00:00
|
|
|
|
private readonly int MaxSpeciesTree;
|
2016-11-08 16:43:57 +00:00
|
|
|
|
|
2018-07-02 02:55:23 +00:00
|
|
|
|
private EvolutionTree(IReadOnlyList<byte[]> data, GameVersion game, PersonalTable personal, int maxSpeciesTree)
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
2016-11-15 02:25:08 +00:00
|
|
|
|
Game = game;
|
2016-11-08 16:43:57 +00:00
|
|
|
|
Personal = personal;
|
2017-01-29 17:26:40 +00:00
|
|
|
|
MaxSpeciesTree = maxSpeciesTree;
|
2018-07-02 02:55:23 +00:00
|
|
|
|
Entries = GetEntries(data);
|
|
|
|
|
Lineage = CreateTree();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-23 17:36:28 +00:00
|
|
|
|
private IReadOnlyList<EvolutionMethod[]> GetEntries(IReadOnlyList<byte[]> data)
|
2018-07-02 02:55:23 +00:00
|
|
|
|
{
|
2019-10-08 01:40:09 +00:00
|
|
|
|
return Game switch
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
2019-11-26 06:51:07 +00:00
|
|
|
|
Gen1 => EvolutionSet1.GetArray(data[0], MaxSpeciesTree),
|
|
|
|
|
Gen2 => EvolutionSet1.GetArray(data[0], MaxSpeciesTree),
|
|
|
|
|
Gen3 => EvolutionSet3.GetArray(data[0]),
|
|
|
|
|
Gen4 => EvolutionSet4.GetArray(data[0]),
|
|
|
|
|
Gen5 => EvolutionSet5.GetArray(data[0]),
|
|
|
|
|
Gen6 => EvolutionSet6.GetArray(data),
|
|
|
|
|
Gen7 => EvolutionSet7.GetArray(data),
|
|
|
|
|
Gen8 => EvolutionSet7.GetArray(data),
|
2019-10-08 01:40:09 +00:00
|
|
|
|
_ => throw new Exception()
|
|
|
|
|
};
|
2018-07-02 02:55:23 +00:00
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2018-07-02 02:55:23 +00:00
|
|
|
|
private EvolutionLineage[] CreateTree()
|
|
|
|
|
{
|
|
|
|
|
var lineage = new EvolutionLineage[Entries.Count];
|
2016-11-08 16:43:57 +00:00
|
|
|
|
for (int i = 0; i < Entries.Count; i++)
|
2018-07-02 02:55:23 +00:00
|
|
|
|
lineage[i] = new EvolutionLineage();
|
2019-11-26 06:51:07 +00:00
|
|
|
|
if (Game == Gen6)
|
2018-07-02 02:55:23 +00:00
|
|
|
|
Array.Resize(ref lineage, MaxSpeciesTree + 1);
|
2016-11-08 16:43:57 +00:00
|
|
|
|
|
2019-11-18 05:45:53 +00:00
|
|
|
|
if (Game.GetGeneration() <= 6)
|
|
|
|
|
GenerateEntriesSpeciesOnly(lineage);
|
|
|
|
|
else
|
|
|
|
|
GenerateEntriesSpeciesForm(lineage);
|
|
|
|
|
|
2018-07-02 02:55:23 +00:00
|
|
|
|
return lineage;
|
2016-11-15 02:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 06:51:07 +00:00
|
|
|
|
private void GenerateEntriesSpeciesOnly(IReadOnlyList<EvolutionLineage> lineage)
|
2019-11-18 05:45:53 +00:00
|
|
|
|
{
|
2019-11-26 06:51:07 +00:00
|
|
|
|
for (int species = 1; species < lineage.Count; species++)
|
2019-11-18 05:45:53 +00:00
|
|
|
|
CreateBranch(lineage, species, 0, species);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 06:51:07 +00:00
|
|
|
|
private void GenerateEntriesSpeciesForm(IReadOnlyList<EvolutionLineage> lineage)
|
2019-11-18 05:45:53 +00:00
|
|
|
|
{
|
|
|
|
|
for (int species = 1; species <= MaxSpeciesTree; species++)
|
|
|
|
|
{
|
|
|
|
|
var pi = Personal[species];
|
|
|
|
|
var fc = pi.FormeCount;
|
|
|
|
|
for (int form = 0; form < fc; form++)
|
|
|
|
|
{
|
|
|
|
|
var index = Personal.GetFormeIndex(species, form);
|
|
|
|
|
CreateBranch(lineage, species, form, index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateBranch(IReadOnlyList<EvolutionLineage> lineage, int species, int form, int index)
|
2016-11-15 02:25:08 +00:00
|
|
|
|
{
|
2019-11-18 05:45:53 +00:00
|
|
|
|
var evos = Entries[index];
|
2018-07-02 02:55:23 +00:00
|
|
|
|
// Iterate over all possible evolutions
|
2019-11-18 05:45:53 +00:00
|
|
|
|
foreach (var evo in evos)
|
|
|
|
|
CreateLeaf(lineage, evo, species, form, index);
|
2016-11-15 02:25:08 +00:00
|
|
|
|
}
|
2018-07-02 02:55:23 +00:00
|
|
|
|
|
2019-11-18 05:45:53 +00:00
|
|
|
|
private void CreateLeaf(IReadOnlyList<EvolutionLineage> lineage, EvolutionMethod evo, int species, int form, int index)
|
2018-07-02 02:55:23 +00:00
|
|
|
|
{
|
2019-11-18 05:45:53 +00:00
|
|
|
|
int evolveTo = GetIndex(evo);
|
|
|
|
|
if (evolveTo < 0)
|
2018-07-02 02:55:23 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-11-18 05:45:53 +00:00
|
|
|
|
var chainTo = lineage[evolveTo];
|
|
|
|
|
var current = lineage[index];
|
|
|
|
|
var sourceEvo = evo.Copy(species, form);
|
2018-07-02 02:55:23 +00:00
|
|
|
|
|
2019-11-18 05:45:53 +00:00
|
|
|
|
chainTo.Insert(sourceEvo);
|
2018-07-02 02:55:23 +00:00
|
|
|
|
// If current entries has a pre-evolution, propagate to evolution as well
|
2019-11-18 05:45:53 +00:00
|
|
|
|
if (current.Chain.Count != 0)
|
|
|
|
|
chainTo.Chain.Insert(0, current.Chain[0]);
|
2018-07-02 02:55:23 +00:00
|
|
|
|
|
2019-11-18 05:45:53 +00:00
|
|
|
|
if (evolveTo >= index)
|
2018-07-02 02:55:23 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If destination species evolves into something (ie a 'baby' Pokemon like Cleffa)
|
|
|
|
|
// Add it to the corresponding parent chains
|
2019-11-18 05:45:53 +00:00
|
|
|
|
foreach (var method in Entries[evolveTo])
|
2018-07-02 02:55:23 +00:00
|
|
|
|
{
|
|
|
|
|
int newIndex = GetIndex(method);
|
|
|
|
|
if (newIndex < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
lineage[newIndex].Insert(sourceEvo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void FixEvoTreeSM()
|
2016-11-15 02:25:08 +00:00
|
|
|
|
{
|
2019-11-23 05:04:34 +00:00
|
|
|
|
UnpackForms((int)Species.Wormadam, 2);
|
|
|
|
|
UnpackForms((int)Species.Gastrodon, 1);
|
|
|
|
|
UnpackForms((int)Species.Meowstic, 1);
|
|
|
|
|
UnpackForms((int)Species.Gourgeist, 3);
|
|
|
|
|
|
|
|
|
|
BanEvo((int)Species.Raichu, 1, EvolutionMethod.BanSM);
|
|
|
|
|
BanEvo((int)Species.Marowak, 0, EvolutionMethod.BanSM);
|
|
|
|
|
BanEvo((int)Species.Raichu, 0, EvolutionMethod.BanSM);
|
|
|
|
|
}
|
2017-02-02 06:19:37 +00:00
|
|
|
|
|
2019-11-23 05:04:34 +00:00
|
|
|
|
private void FixEvoTreeSS()
|
|
|
|
|
{
|
|
|
|
|
SpreadForms((int)Species.Silvally, 17);
|
|
|
|
|
}
|
2016-11-15 02:25:08 +00:00
|
|
|
|
|
2019-11-23 05:04:34 +00:00
|
|
|
|
private void BanEvo(int species, int type, IReadOnlyCollection<GameVersion> versionsBanned)
|
|
|
|
|
{
|
|
|
|
|
var entry = Personal.GetFormeIndex(species, 0);
|
|
|
|
|
var lin = Lineage[entry];
|
|
|
|
|
lin.Chain[type][0].Banlist = versionsBanned;
|
|
|
|
|
}
|
2017-06-11 00:36:33 +00:00
|
|
|
|
|
2019-11-23 05:04:34 +00:00
|
|
|
|
private void UnpackForms(int species, int formCount)
|
|
|
|
|
{
|
|
|
|
|
var baseChain = Lineage[species];
|
|
|
|
|
for (int i = 1; i <= formCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var entry = Personal.GetFormeIndex(species, i);
|
|
|
|
|
var lin = Lineage[entry];
|
|
|
|
|
lin.Chain.Add(new List<EvolutionMethod> { baseChain.Chain[0][i] });
|
|
|
|
|
}
|
|
|
|
|
baseChain.Chain[0].RemoveRange(1, formCount);
|
|
|
|
|
}
|
2017-06-11 00:36:33 +00:00
|
|
|
|
|
2019-11-23 05:04:34 +00:00
|
|
|
|
private void SpreadForms(int species, int formCount)
|
|
|
|
|
{
|
|
|
|
|
var baseChain = Lineage[species];
|
|
|
|
|
for (int i = 1; i <= formCount; i++)
|
2016-11-15 02:25:08 +00:00
|
|
|
|
{
|
2019-11-23 05:04:34 +00:00
|
|
|
|
var entry = Personal.GetFormeIndex(species, i);
|
|
|
|
|
var lin = Lineage[entry];
|
|
|
|
|
lin.Chain.Add(baseChain.Chain[0]);
|
2016-11-15 02:25:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-08 16:43:57 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private int GetIndex(PKM pkm)
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkm.Format < 7)
|
|
|
|
|
return pkm.Species;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return Personal.GetFormeIndex(pkm.Species, pkm.AltForm);
|
2016-11-08 16:43:57 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private int GetIndex(EvolutionMethod evo)
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
|
|
|
|
int evolvesToSpecies = evo.Species;
|
|
|
|
|
if (evolvesToSpecies == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (Personal == null)
|
|
|
|
|
return evolvesToSpecies;
|
|
|
|
|
|
|
|
|
|
int evolvesToForm = evo.Form;
|
|
|
|
|
if (evolvesToForm < 0)
|
|
|
|
|
evolvesToForm = 0;
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return Personal.GetFormeIndex(evolvesToSpecies, evolvesToForm);
|
2016-11-08 16:43:57 +00:00
|
|
|
|
}
|
2018-07-02 02:55:23 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of evolutions for the input <see cref="PKM"/> by checking each evolution in the chain.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pkm">Pokémon data to check with.</param>
|
|
|
|
|
/// <param name="maxLevel">Maximum level to permit before the chain breaks.</param>
|
|
|
|
|
/// <param name="maxSpeciesOrigin">Maximum species ID to permit within the chain.</param>
|
|
|
|
|
/// <param name="skipChecks">Ignores an evolution's criteria, causing the returned list to have all possible evolutions.</param>
|
|
|
|
|
/// <param name="minLevel">Minimum level to permit before the chain breaks.</param>
|
|
|
|
|
/// <returns></returns>
|
2018-06-19 02:10:21 +00:00
|
|
|
|
public List<EvoCriteria> GetValidPreEvolutions(PKM pkm, int maxLevel, int maxSpeciesOrigin = -1, bool skipChecks = false, int minLevel = 1)
|
2016-11-08 16:43:57 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int index = GetIndex(pkm);
|
2017-04-15 02:55:40 +00:00
|
|
|
|
if (maxSpeciesOrigin <= 0)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
maxSpeciesOrigin = Legal.GetMaxSpeciesOrigin(pkm);
|
2019-11-26 06:51:07 +00:00
|
|
|
|
return Lineage[index].GetExplicitLineage(pkm, maxLevel, skipChecks, maxSpeciesOrigin, minLevel);
|
2016-11-08 16:43:57 +00:00
|
|
|
|
}
|
2018-11-28 06:05:36 +00:00
|
|
|
|
|
2019-11-26 06:51:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all species the <see cref="species"/>-<see cref="form"/> can evolve to & from.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="species">Species ID</param>
|
|
|
|
|
/// <param name="form">Form ID</param>
|
|
|
|
|
/// <returns>Enumerable of species IDs.</returns>
|
2018-11-28 06:05:36 +00:00
|
|
|
|
public IEnumerable<int> GetEvolutionsAndPreEvolutions(int species, int form)
|
|
|
|
|
{
|
|
|
|
|
foreach (var s in GetPreEvolutions(species, form))
|
|
|
|
|
yield return s;
|
|
|
|
|
yield return species;
|
|
|
|
|
foreach (var s in GetEvolutions(species, form))
|
|
|
|
|
yield return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<int> GetPreEvolutions(int species, int form)
|
|
|
|
|
{
|
|
|
|
|
int index = Personal.GetFormeIndex(species, form);
|
|
|
|
|
var node = Lineage[index];
|
2019-03-24 01:59:45 +00:00
|
|
|
|
foreach (var methods in node.Chain)
|
2018-11-28 06:05:36 +00:00
|
|
|
|
{
|
2019-03-24 01:59:45 +00:00
|
|
|
|
foreach (var prevo in methods)
|
2018-11-28 06:05:36 +00:00
|
|
|
|
yield return prevo.Species;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<int> GetEvolutions(int species, int form)
|
|
|
|
|
{
|
|
|
|
|
int index = Personal.GetFormeIndex(species, form);
|
|
|
|
|
var node = Entries[index];
|
2019-03-23 17:36:28 +00:00
|
|
|
|
foreach (var z in node)
|
2018-11-28 06:05:36 +00:00
|
|
|
|
{
|
|
|
|
|
var s = z.Species;
|
|
|
|
|
if (s == 0)
|
|
|
|
|
continue;
|
|
|
|
|
yield return s;
|
|
|
|
|
foreach (var next in GetEvolutions(s, form))
|
|
|
|
|
yield return next;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-08 16:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|