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;
|
2022-02-05 01:35:15 +00:00
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
|
|
|
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies the stat details of data that has not yet left <see cref="GameVersion.PLA"/>.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class LegendsArceusVerifier : Verifier
|
|
|
|
{
|
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.RelearnMove;
|
|
|
|
|
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (data.Entity is not PA8 pa)
|
2022-02-05 01:35:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (pa.IsNoble)
|
|
|
|
data.AddLine(GetInvalid(LStatNobleInvalid));
|
2022-11-25 01:42:17 +00:00
|
|
|
if (pa.IsAlpha != data.EncounterMatch is IAlphaReadOnly { IsAlpha: true })
|
2022-02-05 01:35:15 +00:00
|
|
|
data.AddLine(GetInvalid(LStatAlphaInvalid));
|
|
|
|
|
|
|
|
CheckScalars(data, pa);
|
2022-02-05 21:48:34 +00:00
|
|
|
CheckGanbaru(data, pa);
|
2022-05-31 04:43:52 +00:00
|
|
|
|
|
|
|
CheckLearnset(data, pa);
|
|
|
|
CheckMastery(data, pa);
|
2022-02-05 21:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void CheckGanbaru(LegalityAnalysis data, PA8 pa)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
var gv = pa.GetGV(i);
|
|
|
|
var max = pa.GetMaxGanbaru(i);
|
|
|
|
if (gv <= max)
|
|
|
|
continue;
|
|
|
|
|
2022-06-04 20:21:18 +00:00
|
|
|
data.AddLine(GetInvalid(LGanbaruStatTooHigh, CheckIdentifier.GVs));
|
2022-02-05 21:48:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckScalars(LegalityAnalysis data, PA8 pa)
|
|
|
|
{
|
|
|
|
// Static encounters hard-match the Height & Weight; only slots are unchecked for Alpha Height/Weight.
|
|
|
|
if (pa.IsAlpha && data.EncounterMatch is EncounterSlot8a)
|
|
|
|
{
|
|
|
|
if (pa.HeightScalar != 255)
|
|
|
|
data.AddLine(GetInvalid(LStatIncorrectHeightValue));
|
|
|
|
if (pa.WeightScalar != 255)
|
|
|
|
data.AddLine(GetInvalid(LStatIncorrectWeightValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
// No way to mutate the display height scalar value. Must match!
|
2022-11-25 01:42:17 +00:00
|
|
|
if (pa.HeightScalar != pa.Scale)
|
2022-02-05 01:35:15 +00:00
|
|
|
data.AddLine(GetInvalid(LStatIncorrectHeightCopy, CheckIdentifier.Encounter));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void CheckLearnset(LegalityAnalysis data, PA8 pa)
|
|
|
|
{
|
|
|
|
var moveCount = GetMoveCount(pa);
|
|
|
|
if (moveCount == 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the bare minimum moveset.
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> expect = stackalloc ushort[4];
|
2022-05-31 04:43:52 +00:00
|
|
|
var minMoveCount = LoadBareMinimumMoveset(data.EncounterMatch, data.Info.EvoChainsAllGens, pa, expect);
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
|
|
// Flag move slots that are empty.
|
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 = data.Info.Moves;
|
2022-02-05 01:35:15 +00:00
|
|
|
for (int i = moveCount; i < minMoveCount; i++)
|
|
|
|
{
|
|
|
|
// Expected move should never be empty, but just future-proof against any revisions.
|
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
|
|
|
moves[i] = MoveResult.Unobtainable(expect[i]);
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the expected minimum count of moves, and modifies the input <see cref="moves"/> with the bare minimum move IDs.
|
|
|
|
/// </summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
private static int LoadBareMinimumMoveset(ISpeciesForm enc, EvolutionHistory h, PA8 pa, Span<ushort> moves)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
// Get any encounter moves
|
|
|
|
var pt = PersonalTable.LA;
|
|
|
|
var index = pt.GetFormIndex(enc.Species, enc.Form);
|
2022-05-10 04:24:10 +00:00
|
|
|
var learn = Legal.LevelUpLA;
|
|
|
|
var moveset = learn[index];
|
|
|
|
if (enc is IMasteryInitialMoveShop8 ms)
|
|
|
|
ms.LoadInitialMoveset(pa, moves, moveset, pa.Met_Level);
|
|
|
|
else
|
|
|
|
moveset.SetEncounterMoves(pa.Met_Level, moves);
|
2022-08-27 06:43:36 +00:00
|
|
|
var count = moves.IndexOf((ushort)0);
|
2022-02-05 01:35:15 +00:00
|
|
|
if ((uint)count >= 4)
|
|
|
|
return 4;
|
|
|
|
|
2022-02-06 00:47:19 +00:00
|
|
|
var purchasedCount = pa.GetPurchasedCount();
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> purchased = stackalloc ushort[purchasedCount];
|
2022-02-06 00:47:19 +00:00
|
|
|
LoadPurchasedMoves(pa, purchased);
|
|
|
|
|
2022-05-31 04:43:52 +00:00
|
|
|
// If it can be leveled up in other games, level it up in other games.
|
2022-08-04 01:17:46 +00:00
|
|
|
if (h.HasVisitedSWSH || h.HasVisitedBDSP)
|
2022-05-31 04:43:52 +00:00
|
|
|
return count;
|
|
|
|
|
2022-02-05 01:35:15 +00:00
|
|
|
// Level up to current level
|
2022-05-10 04:24:10 +00:00
|
|
|
var level = pa.CurrentLevel;
|
|
|
|
moveset.SetLevelUpMoves(pa.Met_Level, level, moves, purchased, count);
|
2022-08-27 06:43:36 +00:00
|
|
|
count = moves.IndexOf((ushort)0);
|
2022-02-05 01:35:15 +00:00
|
|
|
if ((uint)count >= 4)
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
// Evolve and try
|
2022-05-31 04:43:52 +00:00
|
|
|
var evos = h.Gen8a;
|
2022-05-08 01:29:36 +00:00
|
|
|
for (int i = 0; i < evos.Length - 1; i++)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-04-24 04:33:17 +00:00
|
|
|
var evo = evos[i];
|
2022-05-10 04:24:10 +00:00
|
|
|
var x = pt.GetFormIndex(evo.Species, evo.Form);
|
|
|
|
var m = learn[x];
|
|
|
|
m.SetEvolutionMoves(moves, purchased, count);
|
2022-08-27 06:43:36 +00:00
|
|
|
count = moves.IndexOf((ushort)0);
|
2022-02-05 01:35:15 +00:00
|
|
|
if ((uint)count >= 4)
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any tutored moves we don't know about??
|
2022-05-10 04:24:10 +00:00
|
|
|
var currentIndex = pt.GetFormIndex(evos[0].Species, evos[0].Form);
|
|
|
|
var currentLearn = learn[currentIndex];
|
|
|
|
return AddMasteredMissing(pa, moves, count, moveset, currentLearn, level);
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static void LoadPurchasedMoves(IMoveShop8 pa, Span<ushort> result)
|
2022-02-06 00:47:19 +00:00
|
|
|
{
|
|
|
|
int ctr = 0;
|
2023-01-22 04:02:33 +00:00
|
|
|
var purchased = pa.Permit.RecordPermitIndexes;
|
2022-02-06 00:47:19 +00:00
|
|
|
for (int i = 0; i < purchased.Length; i++)
|
|
|
|
{
|
|
|
|
if (pa.GetPurchasedRecordFlag(i))
|
|
|
|
result[ctr++] = purchased[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static int AddMasteredMissing(PA8 pa, Span<ushort> current, int ctr, Learnset baseLearn, Learnset currentLearn, int level)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var purchased = pa.Permit.RecordPermitIndexes;
|
|
|
|
for (int i = 0; i < purchased.Length; i++)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
// Buying the move tutor grants access, but does not learn the move.
|
|
|
|
// Mastering requires the move to be present in the movepool.
|
|
|
|
if (!pa.GetMasteredRecordFlag(i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Purchased moves can be swapped with existing moves; we're only interested in special granted moves.
|
|
|
|
if (pa.GetPurchasedRecordFlag(i))
|
|
|
|
continue;
|
|
|
|
|
2022-05-10 04:24:10 +00:00
|
|
|
// Check if we can swap it into the moveset after it evolves.
|
2023-01-22 04:02:33 +00:00
|
|
|
var move = purchased[i];
|
2022-05-10 04:24:10 +00:00
|
|
|
var baseLevel = baseLearn.GetMoveLevel(move);
|
|
|
|
var mustKnow = baseLevel is not -1 && baseLevel <= pa.Met_Level;
|
|
|
|
if (!mustKnow && currentLearn.GetMoveLevel(move) != level)
|
|
|
|
continue;
|
|
|
|
|
2022-02-05 01:35:15 +00:00
|
|
|
if (current.IndexOf(move) == -1)
|
|
|
|
current[ctr++] = move;
|
|
|
|
if (ctr == 4)
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
return ctr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int GetMoveCount(PKM pa)
|
|
|
|
{
|
|
|
|
var count = 0;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (pa.GetMove(i) is not 0)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CheckMastery(LegalityAnalysis data, PA8 pa)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var permit = pa.Permit;
|
2022-02-05 01:35:15 +00:00
|
|
|
var alphaMove = pa.AlphaMove;
|
|
|
|
if (alphaMove is not 0)
|
2023-01-22 04:02:33 +00:00
|
|
|
VerifyAlphaMove(data, pa, alphaMove, permit);
|
2022-02-05 01:35:15 +00:00
|
|
|
else
|
|
|
|
VerifyAlphaMoveZero(data);
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
for (int i = 0; i < permit.RecordCountUsed; i++)
|
|
|
|
VerifyTutorMoveIndex(data, pa, i, permit);
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private void VerifyTutorMoveIndex(LegalityAnalysis data, PA8 pa, int i, IPermitRecord permit)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
bool isPurchased = pa.GetPurchasedRecordFlag(i);
|
|
|
|
if (isPurchased)
|
|
|
|
{
|
|
|
|
// Check if the move can be purchased.
|
2023-01-22 04:02:33 +00:00
|
|
|
if (permit.IsRecordPermitted(i))
|
2022-02-05 01:35:15 +00:00
|
|
|
return; // If it has been legally purchased, then any mastery state is legal.
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
data.AddLine(GetInvalid(string.Format(LMoveShopPurchaseInvalid_0, ParseSettings.MoveStrings[permit.RecordPermitIndexes[i]])));
|
2022-02-05 01:35:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isMastered = pa.GetMasteredRecordFlag(i);
|
|
|
|
if (!isMastered)
|
|
|
|
return; // All good.
|
|
|
|
|
|
|
|
// Check if the move can be purchased; using a Mastery Seed checks the permission.
|
2023-01-22 04:02:33 +00:00
|
|
|
var moves = permit.RecordPermitIndexes;
|
|
|
|
var move = moves[i];
|
|
|
|
if (pa.AlphaMove == move)
|
2022-05-08 19:33:30 +00:00
|
|
|
return; // Previously checked.
|
2023-01-22 04:02:33 +00:00
|
|
|
if (data.EncounterMatch is (IMoveset m and IMasteryInitialMoveShop8) && m.Moves.Contains(move))
|
2022-02-05 01:35:15 +00:00
|
|
|
return; // Previously checked.
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!permit.IsRecordPermitted(i))
|
|
|
|
data.AddLine(GetInvalid(string.Format(LMoveShopMasterInvalid_0, ParseSettings.MoveStrings[move])));
|
2022-02-05 01:35:15 +00:00
|
|
|
else if (!CanLearnMoveByLevelUp(data, pa, i, moves))
|
2023-01-22 04:02:33 +00:00
|
|
|
data.AddLine(GetInvalid(string.Format(LMoveShopMasterNotLearned_0, ParseSettings.MoveStrings[move])));
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:01:47 +00:00
|
|
|
private static bool CanLearnMoveByLevelUp(LegalityAnalysis data, PA8 pa, int i, ReadOnlySpan<ushort> moves)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
// Check if the move can be learned in the learnset...
|
|
|
|
// Changing forms do not have separate tutor permissions, so we don't need to bother with form changes.
|
|
|
|
// Level up movepools can grant moves for mastery at lower levels for earlier evolutions... find the minimum.
|
|
|
|
int level = 101;
|
2022-05-31 04:43:52 +00:00
|
|
|
foreach (var evo in data.Info.EvoChainsAllGens.Gen8a)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
var pt = PersonalTable.LA;
|
2022-04-24 04:33:17 +00:00
|
|
|
var index = pt.GetFormIndex(evo.Species, evo.Form);
|
2022-02-05 01:35:15 +00:00
|
|
|
var moveset = Legal.LevelUpLA[index];
|
|
|
|
var lvl = moveset.GetLevelLearnMove(moves[i]);
|
|
|
|
if (lvl == -1)
|
|
|
|
continue; // cannot learn via level up
|
|
|
|
level = Math.Min(lvl, level);
|
|
|
|
}
|
|
|
|
return pa.CurrentLevel >= level;
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private void VerifyAlphaMove(LegalityAnalysis data, PA8 pa, ushort alphaMove, IPermitRecord permit)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-02-05 18:23:00 +00:00
|
|
|
if (!pa.IsAlpha || data.EncounterMatch is EncounterSlot8a { Type: SlotType.Landmark })
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
data.AddLine(GetInvalid(LMoveShopAlphaMoveShouldBeZero));
|
|
|
|
return;
|
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!CanMasterMoveFromMoveShop(alphaMove, permit))
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
data.AddLine(GetInvalid(LMoveShopAlphaMoveShouldBeOther));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An Alpha Move must be marked as mastered.
|
2023-01-22 04:02:33 +00:00
|
|
|
var masteredIndex = permit.RecordPermitIndexes.IndexOf(alphaMove);
|
2022-02-05 01:35:15 +00:00
|
|
|
// Index is already >= 0, implicitly via the above call not returning false.
|
|
|
|
if (!pa.GetMasteredRecordFlag(masteredIndex))
|
|
|
|
data.AddLine(GetInvalid(LMoveShopAlphaMoveShouldBeMastered));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyAlphaMoveZero(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var enc = data.Info.EncounterMatch;
|
2022-02-05 04:13:36 +00:00
|
|
|
if (enc is not IAlpha { IsAlpha: true })
|
2022-02-05 01:35:15 +00:00
|
|
|
return; // okay
|
|
|
|
|
2022-02-05 18:23:00 +00:00
|
|
|
if (enc is EncounterSlot8a { Type: SlotType.Landmark })
|
|
|
|
return; // okay
|
|
|
|
|
2022-02-05 01:35:15 +00:00
|
|
|
var pi = PersonalTable.LA.GetFormEntry(enc.Species, enc.Form);
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!pi.HasMoveShop) // must have had a tutor flag
|
2022-02-05 01:35:15 +00:00
|
|
|
data.AddLine(GetInvalid(LMoveShopAlphaMoveShouldBeOther));
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static bool CanMasterMoveFromMoveShop(ushort move, IPermitRecord permit)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var moves = permit.RecordPermitIndexes;
|
2022-02-05 01:35:15 +00:00
|
|
|
var index = moves.IndexOf(move);
|
|
|
|
if (index == -1)
|
|
|
|
return false; // not in the list
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!permit.IsRecordPermitted(index))
|
2022-02-05 01:35:15 +00:00
|
|
|
return false; // not a possible move
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|