mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-27 22:40:22 +00:00
9166d0eb64
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
92 lines
2.7 KiB
C#
92 lines
2.7 KiB
C#
using System;
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
/// <summary>
|
|
/// Top-level current move verifier.
|
|
/// </summary>
|
|
internal static class LearnVerifier
|
|
{
|
|
private static readonly MoveResult Duplicate = new(LearnMethod.Duplicate);
|
|
private static readonly MoveResult EmptyInvalid = new(LearnMethod.EmptyInvalid);
|
|
|
|
public static void Verify(Span<MoveResult> result, PKM pk, IEncounterTemplate enc, EvolutionHistory history)
|
|
{
|
|
// Clear any existing parse results.
|
|
result.Clear();
|
|
|
|
// Load moves.
|
|
Span<int> current = stackalloc int[4];
|
|
pk.GetMoves(current);
|
|
|
|
// Verify the moves.
|
|
VerifyMoves(result, current, pk, enc, history);
|
|
|
|
// Finalize the checks.
|
|
Finalize(result, current);
|
|
}
|
|
|
|
private static void VerifyMoves(Span<MoveResult> result, ReadOnlySpan<int> current, PKM pk, IEncounterTemplate enc, EvolutionHistory history)
|
|
{
|
|
if (pk.IsEgg)
|
|
LearnVerifierEgg.Verify(result, current, enc, pk);
|
|
else
|
|
LearnVerifierHistory.Verify(result, current, enc, pk, history);
|
|
}
|
|
|
|
private static void Finalize(Span<MoveResult> result, ReadOnlySpan<int> current)
|
|
{
|
|
// Flag duplicate move indexes.
|
|
VerifyNoEmptyDuplicates(result, current);
|
|
|
|
// Can't have empty first move.
|
|
if (current[0] == 0)
|
|
result[0] = EmptyInvalid;
|
|
}
|
|
|
|
private static void VerifyNoEmptyDuplicates(Span<MoveResult> result, ReadOnlySpan<int> current)
|
|
{
|
|
bool emptySlot = false;
|
|
for (int i = 0; i < result.Length; i++)
|
|
{
|
|
var move = current[i];
|
|
if (move == 0)
|
|
{
|
|
emptySlot = true;
|
|
continue;
|
|
}
|
|
|
|
// If an empty slot was noted for a prior move, flag the empty slots.
|
|
if (emptySlot)
|
|
{
|
|
FlagEmptySlotsBeforeIndex(result, current, i);
|
|
emptySlot = false;
|
|
continue;
|
|
}
|
|
|
|
// Check for same move in next move slots
|
|
FlagDuplicateMovesAfterIndex(result, current, i, move);
|
|
}
|
|
}
|
|
|
|
private static void FlagDuplicateMovesAfterIndex(Span<MoveResult> result, ReadOnlySpan<int> current, int index, int move)
|
|
{
|
|
for (int i = result.Length - 1; i > index; i--)
|
|
{
|
|
if (current[i] != move)
|
|
continue;
|
|
result[index] = Duplicate;
|
|
return;
|
|
}
|
|
}
|
|
|
|
private static void FlagEmptySlotsBeforeIndex(Span<MoveResult> result, ReadOnlySpan<int> current, int index)
|
|
{
|
|
for (int i = index - 1; i >= 0; i--)
|
|
{
|
|
if (current[i] != 0)
|
|
return;
|
|
result[i] = EmptyInvalid;
|
|
}
|
|
}
|
|
}
|