PKHeX/PKHeX.Core/Legality/MoveList.cs
Kurt 9166d0eb64
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 16:15:27 -07:00

121 lines
4.2 KiB
C#

using System;
using static PKHeX.Core.Legal;
using static PKHeX.Core.GameVersion;
namespace PKHeX.Core;
/// <summary>
/// Logic for obtaining a list of moves.
/// </summary>
internal static class MoveList
{
internal static int[] GetBaseEggMoves(PKM pk, int species, int form, GameVersion gameSource, int lvl)
{
if (gameSource == Any)
gameSource = (GameVersion)pk.Version;
switch (gameSource)
{
case GSC or GS:
// If checking back-transfer specimen (GSC->RBY), remove moves that must be deleted prior to transfer
static int[] getRBYCompatibleMoves(int format, int[] moves) => format == 1 ? Array.FindAll(moves, m => m <= MaxMoveID_1) : moves;
if (pk.InhabitedGeneration(2))
return getRBYCompatibleMoves(pk.Format, LevelUpGS[species].GetMoves(lvl));
break;
case C:
if (pk.InhabitedGeneration(2))
return getRBYCompatibleMoves(pk.Format, LevelUpC[species].GetMoves(lvl));
break;
case R or S or RS:
if (pk.InhabitedGeneration(3))
return LevelUpRS[species].GetMoves(lvl);
break;
case E:
if (pk.InhabitedGeneration(3))
return LevelUpE[species].GetMoves(lvl);
break;
case FR or LG or FRLG:
// The only difference in FR/LG is Deoxys, which doesn't breed.
if (pk.InhabitedGeneration(3))
return LevelUpFR[species].GetMoves(lvl);
break;
case D or P or DP:
if (pk.InhabitedGeneration(4))
return LevelUpDP[species].GetMoves(lvl);
break;
case Pt:
if (pk.InhabitedGeneration(4))
return LevelUpPt[species].GetMoves(lvl);
break;
case HG or SS or HGSS:
if (pk.InhabitedGeneration(4))
return LevelUpHGSS[species].GetMoves(lvl);
break;
case B or W or BW:
if (pk.InhabitedGeneration(5))
return LevelUpBW[species].GetMoves(lvl);
break;
case B2 or W2 or B2W2:
if (pk.InhabitedGeneration(5))
return LevelUpB2W2[species].GetMoves(lvl);
break;
case X or Y or XY:
if (pk.InhabitedGeneration(6))
return LevelUpXY[species].GetMoves(lvl);
break;
case AS or OR or ORAS:
if (pk.InhabitedGeneration(6))
return LevelUpAO[species].GetMoves(lvl);
break;
case SN or MN or SM:
if (species > MaxSpeciesID_7)
break;
if (pk.InhabitedGeneration(7))
{
int index = PersonalTable.SM.GetFormIndex(species, form);
return LevelUpSM[index].GetMoves(lvl);
}
break;
case US or UM or USUM:
if (pk.InhabitedGeneration(7))
{
int index = PersonalTable.USUM.GetFormIndex(species, form);
return LevelUpUSUM[index].GetMoves(lvl);
}
break;
case SW or SH or SWSH:
if (pk.InhabitedGeneration(8))
{
int index = PersonalTable.SWSH.GetFormIndex(species, form);
return LevelUpSWSH[index].GetMoves(lvl);
}
break;
case PLA:
if (pk.InhabitedGeneration(8))
{
int index = PersonalTable.LA.GetFormIndex(species, form);
return LevelUpLA[index].GetMoves(lvl);
}
break;
case BD or SP or BDSP:
if (pk.InhabitedGeneration(8))
{
int index = PersonalTable.BDSP.GetFormIndex(species, form);
return LevelUpBDSP[index].GetMoves(lvl);
}
break;
}
return Array.Empty<int>();
}
}