Clean up know-move-evolution checks

Looks like Mr. Rime case wasn't being handled, so I rewrote it. Better performance, less complexity. No need to double-reference the moves.

Cache a single Valid evolution result; every parse can reuse that object.
This commit is contained in:
Kurt 2021-05-15 21:41:04 -07:00
parent 4e12e54028
commit 0e2f070510
5 changed files with 130 additions and 180 deletions

View file

@ -1,4 +1,5 @@
using System.Linq;
using static PKHeX.Core.EvolutionRestrictions;
using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.Core
@ -13,14 +14,21 @@ namespace PKHeX.Core
/// </summary>
/// <param name="pkm">Source data to verify</param>
/// <param name="info">Source supporting information to verify with</param>
/// <returns></returns>
public static CheckResult VerifyEvolution(PKM pkm, LegalInfo info)
{
return IsValidEvolution(pkm, info)
? new CheckResult(CheckIdentifier.Evolution)
: new CheckResult(Severity.Invalid, LEvoInvalid, CheckIdentifier.Evolution);
// Check if basic evolution methods are satisfiable with this encounter.
if (!IsValidEvolution(pkm, info))
return new CheckResult(Severity.Invalid, LEvoInvalid, CheckIdentifier.Evolution);
// Check if complex evolution methods are satisfiable with this encounter.
if (!IsValidEvolutionWithMove(pkm, info))
return new CheckResult(Severity.Invalid, string.Format(LMoveEvoFCombination_0, ParseSettings.SpeciesStrings[pkm.Species]), CheckIdentifier.Evolution);
return VALID;
}
private static readonly CheckResult VALID = new(CheckIdentifier.Evolution);
/// <summary>
/// Checks if the Evolution from the source <see cref="IEncounterable"/> is valid.
/// </summary>
@ -29,23 +37,22 @@ namespace PKHeX.Core
/// <returns>Evolution is valid or not</returns>
private static bool IsValidEvolution(PKM pkm, LegalInfo info)
{
if (info.EvoChainsAllGens[pkm.Format].Count == 0)
var chains = info.EvoChainsAllGens;
if (chains[pkm.Format].Count == 0)
return false; // Can't exist as current species
// OK if un-evolved from original encounter
int species = pkm.Species;
if (info.EncounterMatch.Species == species)
return true;
// Bigender->Fixed (non-Genderless) destination species, accounting for PID-Gender relationship
if (species == (int)Species.Vespiquen && info.Generation < 6 && (pkm.PID & 0xFF) >= 0x1F) // Combee->Vespiquen Invalid Evolution
return false;
if (info.Generation > 0 && info.EvoChainsAllGens[info.Generation].All(z => z.Species != info.EncounterMatch.Species))
if (chains[info.Generation].All(z => z.Species != info.EncounterMatch.Species))
return false; // Can't exist as origin species
// If current species evolved with a move evolution and encounter species is not current species check if the evolution by move is valid
// Only the evolution by move is checked, if there is another evolution before the evolution by move is covered in IsEvolutionValid
if (EvolutionRestrictions.SpeciesEvolutionWithMove.Contains(species))
return EvolutionRestrictions.IsEvolutionValidWithMove(pkm, info);
return true;
}
}

View file

@ -306,12 +306,6 @@ namespace PKHeX.Core
FlagIncompatibleTransferHMs45(res, learnInfo.Source.CurrentMoves, gen, HMLearned, KnowDefogWhirlpool);
break;
}
// Pokemon that evolved by leveling up while learning a specific move
// This pokemon could only have 3 moves from preevolutions that are not the move used to evolved
// including special and eggs moves before relearn generations
if (EvolutionRestrictions.SpeciesEvolutionWithMove.Contains(pkm.Species))
ParseEvolutionLevelupMove(pkm, res, learnInfo.Source.CurrentMoves, info);
}
private static void ParseMovesByGeneration(PKM pkm, IList<CheckMoveResult> res, int gen, LegalInfo info, LearnInfo learnInfo)
@ -648,52 +642,6 @@ namespace PKHeX.Core
}
}
private static void ParseEvolutionLevelupMove(PKM pkm, IList<CheckMoveResult> res, IReadOnlyList<int> currentMoves, LegalInfo info)
{
// Ignore if there is an invalid move or an empty move, this validation is only for 4 non-empty moves that are all valid, but invalid as a 4 combination
// Ignore Mr. Mime and Sudowodoo from generations 1 to 3, they cant be evolved from Bonsly or Munchlax
// Ignore if encounter species is the evolution species, the pokemon was not evolved by the player
if (info.EncounterMatch.Species == pkm.Species)
return;
if (!res.All(r => r?.Valid ?? false) || currentMoves.Any(m => m == 0) || (EvolutionRestrictions.BabyEvolutionWithMove.Contains(pkm.Species) && info.Generation <= 3))
return;
var ValidMoves = MoveList.GetValidPostEvolutionMoves(pkm, pkm.Species, info.EvoChainsAllGens, GameVersion.Any);
// Add the evolution moves to valid moves in case some of these moves could not be learned after evolving
switch (pkm.Species)
{
case (int)Species.MrMime: // Mr. Mime (Mime Jr with Mimic)
case (int)Species.Sudowoodo: // Sudowoodo (Bonsly with Mimic)
ValidMoves.Add((int)Move.Mimic);
break;
case (int)Species.Ambipom: // Ambipom (Aipom with Double Hit)
ValidMoves.Add((int)Move.DoubleHit);
break;
case (int)Species.Lickilicky: // Lickilicky (Lickitung with Rollout)
ValidMoves.Add((int)Move.Rollout);
break;
case (int)Species.Tangrowth: // Tangrowth (Tangela with Ancient Power)
case (int)Species.Yanmega: // Yanmega (Yanma with Ancient Power)
case (int)Species.Mamoswine: // Mamoswine (Piloswine with Ancient Power)
ValidMoves.Add((int)Move.AncientPower);
break;
case (int)Species.Sylveon: // Sylveon (Eevee with Fairy Move)
// Add every fairy moves without checking if Eevee learn it or not; pokemon moves are determined legal before this function
ValidMoves.AddRange(EvolutionRestrictions.FairyMoves);
break;
case (int)Species.Tsareena: // Tsareena (Steenee with Stomp)
ValidMoves.Add((int)Move.Stomp);
break;
}
if (currentMoves.Any(m => ValidMoves.Contains(m)))
return;
for (int m = 0; m < 4; m++)
res[m] = new CheckMoveResult(res[m], Invalid, string.Format(LMoveEvoFCombination_0, SpeciesStrings[pkm.Species]), CurrentMove);
}
private static void GetHMCompatibility(PKM pkm, IReadOnlyList<CheckResult> res, int gen, IReadOnlyList<int> moves, out bool[] HMLearned, out bool KnowDefogWhirlpool)
{
HMLearned = new bool[4];

View file

@ -175,35 +175,6 @@ namespace PKHeX.Core
return GetValidRelearn(pkm, species, form, Breeding.GetCanInheritMoves(species), version);
}
internal static List<int> GetValidPostEvolutionMoves(PKM pkm, int species, IReadOnlyList<EvoCriteria>[] evoChains, GameVersion Version)
{
// Return moves that the pokemon could learn after evolving
var moves = new List<int>();
for (int i = 1; i < evoChains.Length; i++)
{
if (evoChains[i].Count != 0)
moves.AddRange(GetValidPostEvolutionMoves(pkm, species, evoChains[i], i, Version));
}
if (pkm.Generation >= 6)
moves.AddRange(pkm.RelearnMoves.Where(m => m != 0));
return moves.Distinct().ToList();
}
private static List<int> GetValidPostEvolutionMoves(PKM pkm, int species, IReadOnlyList<EvoCriteria> evoChain, int generation, GameVersion Version)
{
var evomoves = new List<int>();
var index = EvolutionChain.GetEvoChainSpeciesIndex(evoChain, species);
for (int i = 0; i <= index; i++)
{
var evo = evoChain[i];
var moves = GetMoves(pkm, evo.Species, 1, 1, evo.Level, evo.Form, Version: Version, types: MoveSourceType.ExternalSources, RemoveTransferHM: false, generation: generation);
// Moves from Species or any species after in the evolution phase
evomoves.AddRange(moves);
}
return evomoves;
}
internal static IEnumerable<int> GetExclusivePreEvolutionMoves(PKM pkm, int Species, IReadOnlyList<EvoCriteria> evoChain, int generation, GameVersion Version)
{
var preevomoves = new List<int>();

View file

@ -1,45 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
public static class EvolutionRestrictions
internal static class EvolutionRestrictions
{
public static bool IsMoveRequiredToEvolve(PKM pkm, int gen) => IsMoveRequiredToEvolve(pkm.Species, pkm.Format, gen);
public static bool IsMoveRequiredToEvolve(int species, int format, int gen)
// List of species that evolve from a previous species having a move while leveling up
private static readonly Dictionary<int, MoveEvolution> SpeciesEvolutionWithMove = new()
{
if (!SpeciesEvolutionWithMove.Contains(species))
return false;
if (format <= 3)
return false;
if (BabyEvolutionWithMove.Contains(species))
return gen > 3;
return true;
{(int)Species.Eevee, new(0, 0)}, // FairyMoves
{(int)Species.MimeJr, new(1, (int)Move.Mimic)},
{(int)Species.Bonsly, new(2, (int)Move.Mimic)},
{(int)Species.Aipom, new(3, (int)Move.Mimic)},
{(int)Species.Lickitung, new(4, (int)Move.Rollout)},
{(int)Species.Tangela, new(5, (int)Move.AncientPower)},
{(int)Species.Yanma, new(6, (int)Move.AncientPower)},
{(int)Species.Piloswine, new(7, (int)Move.AncientPower)},
{(int)Species.Steenee, new(8, (int)Move.Stomp)},
{(int)Species.Clobbopus, new(9, (int)Move.Taunt)},
};
private readonly struct MoveEvolution
{
public readonly int ReferenceIndex;
public readonly int Move;
public MoveEvolution(int referenceIndex, int move)
{
ReferenceIndex = referenceIndex;
Move = move;
}
}
internal static readonly int[] BabyEvolutionWithMove =
{
(int)Species.MrMime, // (Mime Jr with Mimic)
(int)Species.Sudowoodo, // (Bonsly with Mimic)
};
// List of species that evolve from a previous species having a move while leveling up
internal static readonly int[] SpeciesEvolutionWithMove =
{
(int)Species.MrMime, // Mr. Mime (Mime Jr with Mimic)
(int)Species.Sudowoodo, // Sudowoodo (Bonsly with Mimic)
(int)Species.Ambipom, // Ambipom (Aipom with Double Hit)
(int)Species.Lickilicky, // Lickilicky (Lickitung with Rollout)
(int)Species.Tangrowth, // Tangrowth (Tangela with Ancient Power)
(int)Species.Yanmega, // Yanmega (Yamma with Ancient Power)
(int)Species.Mamoswine, // Mamoswine (Piloswine with Ancient Power)
(int)Species.Sylveon, // Sylveon (Eevee with Fairy Move)
(int)Species.Tsareena, // Tsareena (Steenee with Stomp)
(int)Species.Grapploct // (Clobbopus with Taunt)
};
internal static readonly int[] FairyMoves =
private static readonly int[] FairyMoves =
{
(int)Move.SweetKiss,
(int)Move.Charm,
@ -73,25 +67,13 @@ namespace PKHeX.Core
(int)Move.MistyExplosion,
};
// Moves that trigger the evolution by move
private static readonly int[][] MoveEvolutionWithMove =
{
new [] { (int)Move.Mimic }, // Mr. Mime (Mime Jr with Mimic)
new [] { (int)Move.Mimic }, // Sudowoodo (Bonsly with Mimic)
new [] { (int)Move.DoubleHit }, // Ambipom (Aipom with Double Hit)
new [] { (int)Move.Rollout }, // Lickilicky (Lickitung with Rollout)
new [] { (int)Move.AncientPower }, // Tangrowth (Tangela with Ancient Power)
new [] { (int)Move.AncientPower }, // Yanmega (Yamma with Ancient Power)
new [] { (int)Move.AncientPower }, // Mamoswine (Piloswine with Ancient Power)
FairyMoves, // Sylveon (Eevee with Fairy Move)
new [] { (int)Move.Stomp }, // Tsareena (Steenee with Stomp)
new [] { (int)Move.Taunt }, // Grapploct (Clobbopus with Taunt)
};
// Min level for any species for every generation to learn the move for evolution by move
// 0 means it cant be learned in that generation
/// <summary>
/// Minimum current level for a given species to have learned the evolve-move and be successfully evolved.
/// </summary>
/// <remarks>Having a value of 0 means the move can't be learned.</remarks>
private static readonly int[][] MinLevelEvolutionWithMove =
{
new [] { 00, 00, 00, 00, 00, 29, 09, 02, 02 }, // Sylveon (Eevee with Fairy Move)
new [] { 00, 00, 00, 00, 18, 15, 15, 02, 02 }, // Mr. Mime (Mime Jr with Mimic)
new [] { 00, 00, 00, 00, 17, 17, 15, 02, 02 }, // Sudowoodo (Bonsly with Mimic)
new [] { 00, 00, 00, 00, 32, 32, 32, 02, 02 }, // Ambipom (Aipom with Double Hit)
@ -99,14 +81,13 @@ namespace PKHeX.Core
new [] { 00, 00, 00, 00, 02, 36, 38, 02, 02 }, // Tangrowth (Tangela with Ancient Power)
new [] { 00, 00, 00, 00, 02, 33, 33, 02, 02 }, // Yanmega (Yanma with Ancient Power)
new [] { 00, 00, 00, 00, 02, 02, 02, 02, 02 }, // Mamoswine (Piloswine with Ancient Power)
new [] { 00, 00, 00, 00, 00, 29, 09, 02, 02 }, // Sylveon (Eevee with Fairy Move)
new [] { 00, 00, 00, 00, 00, 00, 00, 02, 28 }, // Tsareena (Steenee with Stomp)
new [] { 00, 00, 00, 00, 00, 00, 00, 00, 35 }, // Grapploct (Clobbopus with Taunt)
};
// True -> the pokemon could hatch from an egg with the move for evolution as an egg move
private static readonly bool[][] EggMoveEvolutionWithMove =
private static readonly bool[][] CanEggHatchWithEvolveMove =
{
new [] { false, false, true, true, true, true, true, true, true }, // Sylveon (Eevee with Fairy Move)
new [] { false, false, false, false, true, true, true, true, true }, // Mr. Mime (Mime Jr with Mimic)
new [] { false, false, false, false, true, true, true, true, true }, // Sudowoodo (Bonsly with Mimic)
new [] { false, false, false, false, true, true, true, true, true }, // Ambipom (Aipom with Double Hit)
@ -114,38 +95,72 @@ namespace PKHeX.Core
new [] { false, false, false, false, true, true, true, true, true }, // Tangrowth (Tangela with Ancient Power)
new [] { false, false, false, false, true, true, true, true, true }, // Yanmega (Yanma with Ancient Power)
new [] { false, false, true, true, true, true, true, true, true }, // Mamoswine (Piloswine with Ancient Power)
new [] { false, false, true, true, true, true, true, true, true }, // Sylveon (Eevee with Fairy Move)
new [] { false, false, false, false, false, false, false, false, false }, // Tsareena (Steenee with Stomp)
new [] { false, false, false, false, false, false, false, false, true }, // Grapploct (Clobbopus with Taunt)
};
public static bool IsEvolutionValidWithMove(PKM pkm, LegalInfo info)
/// <summary>
/// Checks if the <see cref="pkm"/> is correctly evolved, assuming it had a known move requirement evolution in its evolution chain.
/// </summary>
/// <returns>True if unnecessary to check or the evolution was valid.</returns>
public static bool IsValidEvolutionWithMove(PKM pkm, LegalInfo info)
{
// Exclude species that do not evolve leveling with a move
// Exclude gen 1-3 formats
// Exclude Mr. Mime and Snorlax for gen 1-3 games
var gen = info.Generation;
if (!IsMoveRequiredToEvolve(pkm, gen))
// Known-move evolutions were introduced in Gen4.
if (pkm.Format < 4) // doesn't exist yet!
return true;
var lvl = GetLevelLearnMove(pkm, gen, info.EncounterMatch, info.Moves);
// Exclude evolution paths that did not require a move w/level-up evolution
var enc = info.EncounterOriginal;
if (!SpeciesEvolutionWithMove.TryGetValue(enc.Species, out var entry))
return true;
var move = entry.Move;
if (move == 0)
{
// Other evolutions are fine.
if (pkm.Species != (int) Species.Sylveon)
return true;
}
// Check if the move was already known when it was originally encountered.
var gen = info.Generation;
var index = entry.ReferenceIndex;
if (enc is EncounterEgg)
{
if (CanEggHatchWithEvolveMove[index][gen])
{
var result = move == 0 ? IsMoveInherited(pkm, info, FairyMoves) : IsMoveInherited(pkm, info, move);
if (result)
return true;
}
}
else if (enc is IMoveset s)
{
var result = move == 0 ? s.Moves.Any(FairyMoves.Contains) : s.Moves.Contains(move);
if (result)
return true;
}
// Current level must be at least the minimum post-evolution level.
var lvl = GetMinLevelKnowRequiredMove(pkm, gen, index);
return pkm.CurrentLevel >= lvl;
}
private static int GetMinLevelKnowRequiredMove(PKM pkm, int gen, int index)
{
var lvl = GetLevelLearnMove(pkm, gen, index);
// If has original met location the minimum evolution level is one level after met level
// Gen 3 pokemon in gen 4 games: minimum level is one level after transfer to generation 4
// VC pokemon: minimum level is one level after transfer to generation 7
// Sylveon: always one level after met level, for gen 4 and 5 eevees in gen 6 games minimum for evolution is one level after transfer to generation 5
if (pkm.HasOriginalMetLocation || (pkm.Format == 4 && gen == 3) || pkm.VC || pkm.Species == (int)Species.Sylveon)
if (pkm.HasOriginalMetLocation || (pkm.Format == 4 && gen == 3) || pkm.VC || pkm.Species == (int) Species.Sylveon)
lvl = Math.Max(pkm.Met_Level + 1, lvl);
// Current level must be at least one the minimum learn level
// the level-up event that triggers the learning of the move also triggers evolution with no further level-up required
return pkm.CurrentLevel >= lvl;
return lvl;
}
public static int GetLevelLearnMove(PKM pkm, int gen, IEncounterable enc, CheckMoveResult[] res)
private static int GetLevelLearnMove(PKM pkm, int gen, int index)
{
var index = Array.IndexOf(SpeciesEvolutionWithMove, pkm.Species);
// Get the minimum level in any generation when the pokemon could learn the evolve move
var levels = MinLevelEvolutionWithMove[index];
var lvl = 101;
@ -154,37 +169,41 @@ namespace PKHeX.Core
if (pkm.InhabitedGeneration(g) && levels[g] > 0)
lvl = Math.Min(lvl, levels[g]);
}
// Check also if the current encounter include the evolve move as an special move
// That means the pokemon have the move from the encounter level
var moves = MoveEvolutionWithMove[index];
if (enc is IMoveset s && s.Moves.Any(m => moves.Contains(m)))
lvl = Math.Min(lvl, enc.LevelMin);
// If the encounter is a player hatched egg, check if the move could be an egg move or inherited level up move
var allowegg = EggMoveEvolutionWithMove[index][gen];
if (enc is EncounterEgg && allowegg)
{
if (IsMoveInherited(pkm, gen, res, moves))
lvl = Math.Min(lvl, gen <= 3 ? 6 : 2);
}
return lvl;
}
private static bool IsMoveInherited(PKM pkm, int gen, CheckMoveResult[] res, int[] moves)
private static bool IsMoveInherited(PKM pkm, LegalInfo info, int move)
{
// In 3DS games, the inherited move must be in the relearn moves.
if (gen >= 6)
if (info.Generation >= 6)
return pkm.RelearnMoves.Contains(move);
// In Pre-3DS games, the move is inherited if it has the move and it can be hatched with the move.
if (pkm.HasMove(move))
return true;
return DidLearnAndForget(info);
}
private static bool IsMoveInherited(PKM pkm, LegalInfo info, int[] moves)
{
// In 3DS games, the inherited move must be in the relearn moves.
if (info.Generation >= 6)
return pkm.RelearnMoves.Any(moves.Contains);
// In Pre-3DS games, the move is inherited if it has the move and it can be hatched with the move.
if (pkm.Moves.Any(moves.Contains))
return true;
// If the pokemon does not have the move, it still could be an egg move that was forgotten.
return DidLearnAndForget(info);
}
private static bool DidLearnAndForget(LegalInfo info)
{
// If the pokemon does not currently have the move, it could have been an egg move that was forgotten.
// This requires the pokemon to not have 4 other moves identified as egg moves or inherited level up moves.
return 4 > res.Count(m => m.Source == MoveSource.EggMove || m.Source == MoveSource.InheritLevelUp);
var fromEggCount = info.Moves.Count(m => m.IsEggSource);
return fromEggCount < 4;
}
}
}

View file

@ -41,6 +41,11 @@
/// </summary>
public bool IsRelearn => Source == MoveSource.Relearn || (Source == MoveSource.EggMove && Generation >= 6);
/// <summary>
/// Indicates if the source of the move was validated as originating from an egg.
/// </summary>
public bool IsEggSource => Source is MoveSource.EggMove or MoveSource.InheritLevelUp;
/// <summary>
/// Checks if the Move should be present in a Relearn move pool (assuming Gen6+ origins).
/// </summary>