PKHeX/PKHeX.Core/Legality/MoveList.cs

85 lines
3.1 KiB
C#
Raw Normal View History

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
using System;
using static PKHeX.Core.Legal;
2021-03-14 18:28:46 +00:00
using static PKHeX.Core.GameVersion;
namespace PKHeX.Core;
/// <summary>
/// Logic for obtaining a list of moves.
/// </summary>
internal static class MoveList
{
internal static void GetCurrentMoves(PKM pk, ushort species, byte form, GameVersion gameSource, int lvl, Span<ushort> moves) => _ = gameSource switch
{
GSC or GS => Get(moves, LevelUpGS, species, lvl, pk.Format),
C => Get(moves, LevelUpC, species, lvl, pk.Format),
R or S or RS => Get(moves, LevelUpRS, species, lvl),
E => Get(moves, LevelUpE, species, lvl),
FR or LG or FRLG => Get(moves, LevelUpFR, species, lvl),
D or P or DP => Get(moves, LevelUpDP, species, lvl),
Pt => Get(moves, LevelUpPt, species, lvl),
HG or SS or HGSS => Get(moves, LevelUpHGSS, species, lvl),
B or W or BW => Get(moves, LevelUpBW, species, lvl),
B2 or W2 or B2W2 => Get(moves, LevelUpB2W2, species, lvl),
X or Y or XY => Get(moves, LevelUpXY, species, lvl),
AS or OR or ORAS => Get(moves, LevelUpAO, species, lvl),
SN or MN or SM => Get(moves, LevelUpSM, PersonalTable.SM, species, form, lvl),
US or UM or USUM => Get(moves, LevelUpUSUM, PersonalTable.USUM, species, form, lvl),
SW or SH or SWSH => Get(moves, LevelUpSWSH, PersonalTable.SWSH, species, form, lvl),
BD or SP or BDSP => Get(moves, LevelUpBDSP, PersonalTable.BDSP, species, form, lvl),
PLA => Get(moves, LevelUpLA, PersonalTable.LA, species, form, lvl),
SL or VL or SV => Get(moves, LevelUpSV, PersonalTable.SV, species, form, lvl),
_ => moves,
};
private static Span<ushort> Get(Span<ushort> moves, Learnset[] source, ushort species, int lvl)
{
if (species >= source.Length)
return moves;
source[species].SetLevelUpMoves(1, lvl, moves);
return moves;
}
private static Span<ushort> Get<T>(Span<ushort> moves, Learnset[] source, T pt, ushort species, byte form, int lvl) where T : IPersonalTable
{
if (!pt.IsPresentInGame(species, form))
return moves;
int index = pt.GetFormIndex(species, form);
source[index].SetLevelUpMoves(1, lvl, moves);
return moves;
}
private static Span<ushort> Get(Span<ushort> moves, Learnset[] source, ushort species, int lvl, int format)
{
if (species > MaxSpeciesID_2)
return moves;
source[species].SetLevelUpMoves(1, lvl, moves);
if (format != 1)
return moves;
// If checking back-transfer specimen (GSC->RBY), remove moves that must be deleted prior to transfer
// Remove all values greater than MaxMoveID_1, and shift the remaining indexes down.
for (int i = 0; i < moves.Length; i++)
{
if (moves[i] <= MaxMoveID_1)
continue;
// Shift remaining indexes down, set last index to 0
for (int j = i; j < moves.Length - 1; j++)
moves[j] = moves[j + 1];
moves[^1] = 0;
}
return moves;
}
}