2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2021-05-16 04:41:04 +00:00
|
|
|
using System.Collections.Generic;
|
2021-06-03 19:04:19 +00:00
|
|
|
using static PKHeX.Core.Move;
|
|
|
|
using static PKHeX.Core.Species;
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Restriction logic for evolutions that are a little more complex than <see cref="EvolutionMethod"/> can simply check.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Currently only checks "is able to know a move required to level up".
|
|
|
|
/// </remarks>
|
|
|
|
internal static class EvolutionRestrictions
|
2020-11-02 21:11:51 +00:00
|
|
|
{
|
2021-06-03 19:04:19 +00:00
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// List of species that evolve from a previous species having a move while leveling up
|
2021-06-03 19:04:19 +00:00
|
|
|
/// </summary>
|
2023-03-05 04:00:27 +00:00
|
|
|
private static readonly Dictionary<ushort, MoveEvolution> SpeciesEvolutionWithMove = new()
|
2020-11-02 21:11:51 +00:00
|
|
|
{
|
2023-03-05 04:00:27 +00:00
|
|
|
{(int)Eevee, new()}, // FairyMoves
|
2022-06-18 18:04:24 +00:00
|
|
|
{(int)MimeJr, new(01, (int)Mimic)},
|
|
|
|
{(int)Bonsly, new(02, (int)Mimic)},
|
|
|
|
{(int)Aipom, new(03, (int)Mimic)},
|
|
|
|
{(int)Lickitung, new(04, (int)Rollout)},
|
|
|
|
{(int)Tangela, new(05, (int)AncientPower)},
|
|
|
|
{(int)Yanma, new(06, (int)AncientPower)},
|
|
|
|
{(int)Piloswine, new(07, (int)AncientPower)},
|
|
|
|
{(int)Steenee, new(08, (int)Stomp)},
|
|
|
|
{(int)Clobbopus, new(09, (int)Taunt)},
|
|
|
|
{(int)Stantler, new(10, (int)PsyshieldBash)},
|
|
|
|
{(int)Qwilfish, new(11, (int)BarbBarrage)},
|
2022-11-25 01:42:17 +00:00
|
|
|
{(int)Primeape, new(12, (int)RageFist)},
|
|
|
|
{(int)Girafarig, new(13, (int)TwinBeam)},
|
|
|
|
{(int)Dunsparce, new(14, (int)HyperDrill)},
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
|
|
|
|
2023-03-05 04:00:27 +00:00
|
|
|
private readonly record struct MoveEvolution(byte ReferenceIndex, ushort Move);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-03-26 00:55:55 +00:00
|
|
|
private static ReadOnlySpan<ushort> FairyMoves => new ushort[]
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
(int)SweetKiss,
|
|
|
|
(int)Charm,
|
|
|
|
(int)Moonlight,
|
|
|
|
(int)DisarmingVoice,
|
|
|
|
(int)DrainingKiss,
|
|
|
|
(int)CraftyShield,
|
|
|
|
(int)FlowerShield,
|
|
|
|
(int)MistyTerrain,
|
|
|
|
(int)PlayRough,
|
|
|
|
(int)FairyWind,
|
|
|
|
(int)Moonblast,
|
|
|
|
(int)FairyLock,
|
|
|
|
(int)AromaticMist,
|
|
|
|
(int)Geomancy,
|
|
|
|
(int)DazzlingGleam,
|
|
|
|
(int)BabyDollEyes,
|
|
|
|
(int)LightofRuin,
|
|
|
|
(int)TwinkleTackleP,
|
|
|
|
(int)TwinkleTackleS,
|
|
|
|
(int)FloralHealing,
|
|
|
|
(int)GuardianofAlola,
|
|
|
|
(int)FleurCannon,
|
|
|
|
(int)NaturesMadness,
|
|
|
|
(int)LetsSnuggleForever,
|
|
|
|
(int)SparklySwirl,
|
|
|
|
(int)MaxStarfall,
|
|
|
|
(int)Decorate,
|
|
|
|
(int)SpiritBreak,
|
|
|
|
(int)StrangeSteam,
|
|
|
|
(int)MistyExplosion,
|
|
|
|
(int)SpringtideStorm,
|
|
|
|
};
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <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>
|
2023-01-22 04:02:33 +00:00
|
|
|
private static ReadOnlySpan<byte> MinLevelEvolutionWithMove => new byte[]
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
00, 00, 00, 00, 00, 29, 09, 02, 02, 02, // Sylveon (Eevee with Fairy Move)
|
|
|
|
00, 00, 00, 00, 18, 15, 15, 02, 32, 00, // Mr. Mime (Mime Jr with Mimic)
|
|
|
|
00, 00, 00, 00, 17, 17, 15, 02, 16, 16, // Sudowoodo (Bonsly with Mimic)
|
|
|
|
00, 00, 00, 00, 32, 32, 32, 02, 32, 00, // Ambipom (Aipom with Double Hit)
|
|
|
|
00, 00, 02, 00, 02, 33, 33, 02, 06, 00, // Lickilicky (Lickitung with Rollout)
|
|
|
|
00, 00, 00, 00, 02, 36, 38, 02, 24, 00, // Tangrowth (Tangela with Ancient Power)
|
|
|
|
00, 00, 00, 00, 02, 33, 33, 02, 33, 00, // Yanmega (Yanma with Ancient Power)
|
|
|
|
00, 00, 00, 00, 02, 02, 02, 02, 02, 00, // Mamoswine (Piloswine with Ancient Power)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 02, 28, 28, // Tsareena (Steenee with Stomp)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 00, 35, 00, // Grapploct (Clobbopus with Taunt)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, // Wyrdeer (Stantler with AGILE Psyshield Bash)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 00, 00, 00, // Overqwil (Qwilfish-1 with STRONG Barb Barrage)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 00, 00, 35, // Annihilape (Primeape with Rage Fist)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 00, 00, 32, // Farigiraf (Girafarig with Twin Beam)
|
|
|
|
00, 00, 00, 00, 00, 00, 00, 00, 00, 32, // Dudunsparce (Dunsparce with Hyper Drill)
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private const int MinLevelEvoWidth = 10;
|
|
|
|
|
|
|
|
private static ReadOnlySpan<byte> MinLevelEvolutionWithMove_8LA => new byte[]
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
00, // Sylveon (Eevee with Fairy Move)
|
|
|
|
25, // Mr. Mime (Mime Jr with Mimic)
|
|
|
|
29, // Sudowoodo (Bonsly with Mimic)
|
|
|
|
25, // Ambipom (Aipom with Double Hit)
|
|
|
|
34, // Lickilicky (Lickitung with Rollout)
|
|
|
|
34, // Tangrowth (Tangela with Ancient Power)
|
|
|
|
34, // Yanmega (Yanma with Ancient Power)
|
|
|
|
34, // Mamoswine (Piloswine with Ancient Power)
|
|
|
|
99, // Tsareena (Steenee with Stomp)
|
|
|
|
99, // Grapploct (Clobbopus with Taunt)
|
|
|
|
31, // Wyrdeer (Stantler with AGILE Psyshield Bash)
|
2022-11-25 01:42:17 +00:00
|
|
|
25, // Overqwil (Qwilfish-1 with STRONG Barb Barrage)
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static ReadOnlySpan<byte> CanEggHatchWithEvolveMove => new byte[]
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, // Sylveon (Eevee with Fairy Move)
|
|
|
|
0, 0, 0, 0, 1, 1, 1, 1, 1, 0, // Mr. Mime (Mime Jr with Mimic)
|
|
|
|
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, // Sudowoodo (Bonsly with Mimic)
|
|
|
|
0, 0, 0, 0, 1, 1, 1, 1, 1, 0, // Ambipom (Aipom with Double Hit)
|
|
|
|
0, 0, 1, 0, 1, 1, 1, 1, 1, 0, // Lickilicky (Lickitung with Rollout)
|
|
|
|
0, 0, 0, 0, 1, 1, 1, 1, 1, 0, // Tangrowth (Tangela with Ancient Power)
|
|
|
|
0, 0, 0, 0, 1, 1, 1, 1, 1, 0, // Yanmega (Yanma with Ancient Power)
|
|
|
|
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, // Mamoswine (Piloswine with Ancient Power)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Tsareena (Steenee with Stomp)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, // Grapploct (Clobbopus with Taunt)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Wyrdeer (Stantler with AGILE Psyshield Bash)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Overqwil (Qwilfish-1 with STRONG Barb Barrage)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Annihilape (Primeape with Rage Fist)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // Farigiraf (Girafarig with Twin Beam)
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // Dudunsparce (Dunsparce with Hyper Drill)
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
2021-05-16 18:12:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="pk"/> 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 pk, LegalInfo info)
|
|
|
|
{
|
|
|
|
// Known-move evolutions were introduced in Gen4.
|
|
|
|
if (pk.Format < 4) // doesn't exist yet!
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// OK if un-evolved from original encounter
|
2022-08-27 06:43:36 +00:00
|
|
|
ushort species = pk.Species;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (info.EncounterMatch.Species == species)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// 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 (pk.Species != (int)Sylveon)
|
2021-05-16 04:41:04 +00:00
|
|
|
return true;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2021-05-16 04:41:04 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check if the move was already known when it was originally encountered.
|
|
|
|
var gen = info.Generation;
|
|
|
|
var index = entry.ReferenceIndex;
|
|
|
|
if (enc is EncounterEgg)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var slice = CanEggHatchWithEvolveMove.Slice(index * MinLevelEvoWidth, MinLevelEvoWidth);
|
|
|
|
if (slice[gen] != 0) // true
|
2021-05-16 04:41:04 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var result = move == 0 ? IsMoveInherited(pk, info, FairyMoves) : IsMoveInherited(pk, info, move);
|
2021-05-16 04:41:04 +00:00
|
|
|
if (result)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (enc is IMoveset s)
|
2021-05-16 04:41:04 +00:00
|
|
|
{
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
var moves = s.Moves;
|
2022-08-22 00:34:32 +00:00
|
|
|
var result = move == 0 ? moves.ContainsAny(FairyMoves) : moves.Contains(move);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (result)
|
|
|
|
return true;
|
2020-11-02 21:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Current level must be at least the minimum post-evolution level.
|
|
|
|
var lvl = GetMinLevelKnowRequiredMove(pk, gen, index, info.EvoChainsAllGens);
|
|
|
|
return pk.CurrentLevel >= lvl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetMinLevelKnowRequiredMove(PKM pk, int gen, int index, EvolutionHistory evos)
|
|
|
|
{
|
|
|
|
var lvl = GetLevelLearnMove(pk, gen, index);
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// 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
|
2022-08-21 08:39:16 +00:00
|
|
|
// Sylveon: always one level after met level, for gen 4 and 5 Eevee in gen 6 games minimum for evolution is one level after transfer to generation 5
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.HasOriginalMetLocation || (pk.Format == 4 && gen == 3) || pk.VC || pk.Species == (int)Sylveon)
|
|
|
|
lvl = Math.Max(pk.Met_Level + 1, lvl);
|
2022-06-27 05:32:51 +00:00
|
|
|
|
2022-08-04 01:17:46 +00:00
|
|
|
if (evos.HasVisitedPLA) // No Level Up required, and different levels than mainline SW/SH.
|
2022-06-27 05:32:51 +00:00
|
|
|
{
|
|
|
|
var la = MinLevelEvolutionWithMove_8LA[index];
|
|
|
|
if (la <= lvl)
|
|
|
|
return la;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return lvl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetLevelLearnMove(PKM pk, int gen, int index)
|
|
|
|
{
|
|
|
|
// Get the minimum level in any generation when the pokemon could learn the evolve move
|
2023-01-22 04:02:33 +00:00
|
|
|
var levels = MinLevelEvolutionWithMove.Slice(index * MinLevelEvoWidth, MinLevelEvoWidth);
|
2022-06-18 18:04:24 +00:00
|
|
|
var lvl = 101;
|
|
|
|
var end = pk.Format;
|
|
|
|
for (int g = gen; g <= end; g++)
|
2021-05-16 04:41:04 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var l = levels[g];
|
|
|
|
if (l == 0)
|
|
|
|
continue;
|
|
|
|
if (l == 2)
|
|
|
|
return 2; // true minimum
|
|
|
|
if (l < lvl)
|
|
|
|
lvl = l; // best minimum
|
|
|
|
}
|
|
|
|
return lvl;
|
|
|
|
}
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static bool IsMoveInherited(PKM pk, LegalInfo info, ushort move)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// In 3DS games, the inherited move must be in the relearn moves.
|
2022-06-26 06:08:28 +00:00
|
|
|
if (info.Generation >= 6 && !pk.IsOriginalMovesetDeleted())
|
|
|
|
return pk.HasRelearnMove(move);
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// In Pre-3DS games, the move is inherited if it has the move and it can be hatched with the move.
|
|
|
|
if (pk.HasMove(move))
|
|
|
|
return true;
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return DidLearnAndForget(info);
|
|
|
|
}
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static bool IsMoveInherited(PKM pk, LegalInfo info, ReadOnlySpan<ushort> moves)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// In 3DS games, the inherited move must be in the relearn moves.
|
|
|
|
if (info.Generation >= 6)
|
2022-06-26 06:08:28 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> relearn = stackalloc ushort[4];
|
2022-06-26 06:08:28 +00:00
|
|
|
pk.GetRelearnMoves(relearn);
|
|
|
|
return relearn.IndexOfAny(moves) != -1;
|
|
|
|
}
|
2020-11-02 21:11:51 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// In Pre-3DS games, the move is inherited if it has the move and it can be hatched with the move.
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> pkMoves = stackalloc ushort[4];
|
2022-06-26 06:08:28 +00:00
|
|
|
pk.GetMoves(pkMoves);
|
|
|
|
var index = pkMoves.IndexOfAny(moves);
|
|
|
|
if (index != -1)
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
2021-05-16 04:41:04 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
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.
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
// If any move is not an egg source, then a slot could have been forgotten.
|
|
|
|
foreach (var move in info.Moves)
|
|
|
|
{
|
|
|
|
if (!move.Info.Method.IsEggSource())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-11-02 21:11:51 +00:00
|
|
|
}
|
|
|
|
}
|