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;
|
2021-10-08 06:56:01 +00:00
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies the <see cref="PKM.CurrentLevel"/>.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class LevelVerifier : Verifier
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.Level;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var enc = data.EncounterOriginal;
|
|
|
|
if (enc is MysteryGift gift)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (gift.Level != pk.Met_Level && pk.HasOriginalMetLocation)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
switch (gift)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case WC3 wc3 when wc3.Met_Level == pk.Met_Level || wc3.IsEgg:
|
|
|
|
break;
|
|
|
|
case WC7 wc7 when wc7.MetLevel == pk.Met_Level:
|
|
|
|
break;
|
|
|
|
case PGT {IsManaphyEgg: true} when pk.Met_Level == 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data.AddLine(GetInvalid(LLevelMetGift));
|
|
|
|
return;
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
if (gift.Level > pk.CurrentLevel)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
data.AddLine(GetInvalid(LLevelMetGiftFail));
|
2018-06-24 05:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.IsEgg)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
int elvl = enc.LevelMin;
|
|
|
|
if (elvl != pk.CurrentLevel)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
data.AddLine(GetInvalid(string.Format(LEggFMetLevel_0, elvl)));
|
2018-06-24 05:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var reqEXP = enc is EncounterStatic2Odd
|
|
|
|
? 125 // Gen2 Dizzy Punch gifts always have 125 EXP, even if it's more than the Lv5 exp required.
|
|
|
|
: Experience.GetEXP(elvl, pk.PersonalInfo.EXPGrowth);
|
|
|
|
if (reqEXP != pk.EXP)
|
|
|
|
data.AddLine(GetInvalid(LEggEXP));
|
|
|
|
return;
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int lvl = pk.CurrentLevel;
|
|
|
|
if (lvl >= 100)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var expect = Experience.GetEXP(100, pk.PersonalInfo.EXPGrowth);
|
|
|
|
if (pk.EXP != expect)
|
|
|
|
data.AddLine(GetInvalid(LLevelEXPTooHigh));
|
|
|
|
}
|
2021-10-08 06:56:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (lvl < pk.Met_Level)
|
|
|
|
data.AddLine(GetInvalid(LLevelMetBelow));
|
|
|
|
else if (!enc.IsWithinEncounterRange(pk) && lvl != 100 && pk.EXP == Experience.GetEXP(lvl, pk.PersonalInfo.EXPGrowth))
|
|
|
|
data.AddLine(Get(LLevelEXPThreshold, Severity.Fishy));
|
|
|
|
else
|
|
|
|
data.AddLine(GetValid(LLevelMetSane));
|
|
|
|
}
|
2021-10-08 06:56:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public void VerifyG1(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var enc = data.EncounterMatch;
|
|
|
|
if (pk.IsEgg)
|
|
|
|
{
|
|
|
|
const int elvl = 5;
|
|
|
|
if (elvl != pk.CurrentLevel)
|
|
|
|
data.AddLine(GetInvalid(string.Format(LEggFMetLevel_0, elvl)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pk.Met_Location != 0) // crystal
|
|
|
|
{
|
|
|
|
int lvl = pk.CurrentLevel;
|
|
|
|
if (lvl < pk.Met_Level)
|
|
|
|
data.AddLine(GetInvalid(LLevelMetBelow));
|
|
|
|
}
|
2020-11-01 23:27:54 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (IsTradeEvolutionRequired(data, enc))
|
|
|
|
{
|
|
|
|
// Pokemon has been traded illegally between games without evolving.
|
|
|
|
// Trade evolution species IDs for Gen1 are sequential dex numbers.
|
|
|
|
var species = enc.Species;
|
|
|
|
var evolved = ParseSettings.SpeciesStrings[species + 1];
|
|
|
|
var unevolved = ParseSettings.SpeciesStrings[species];
|
|
|
|
data.AddLine(GetInvalid(string.Format(LEvoTradeReqOutsider, unevolved, evolved)));
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if a Gen1 trade evolution must have occurred.
|
|
|
|
/// </summary>
|
|
|
|
private static bool IsTradeEvolutionRequired(LegalityAnalysis data, IEncounterTemplate enc)
|
|
|
|
{
|
|
|
|
// There is no way to prevent a Gen1 trade evolution, as held items (Everstone) did not exist.
|
|
|
|
// Machoke, Graveler, Haunter and Kadabra captured in the second phase evolution, excluding in-game trades, are already checked
|
|
|
|
var pk = data.Entity;
|
|
|
|
var species = pk.Species;
|
|
|
|
|
|
|
|
// This check is only applicable if it's a trade evolution that has not been evolved.
|
2022-08-26 17:07:24 +00:00
|
|
|
if (!GBRestrictions.Trade_Evolution1.Contains((byte)enc.Species) || enc.Species != species)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Context check is only applicable to gen1/2; transferring to Gen2 is a trade.
|
|
|
|
// Stadium 2 can transfer across game/generation boundaries without initiating a trade.
|
|
|
|
// Ignore this check if the environment's loaded trainer is not from Gen1/2 or is from GB Era.
|
|
|
|
if (ParseSettings.ActiveTrainer.Generation >= 3 || ParseSettings.AllowGBCartEra)
|
|
|
|
return false;
|
|
|
|
|
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-06-18 18:04:24 +00:00
|
|
|
// Gen2 stuff can be traded between Gen2 games holding an Everstone, assuming it hasn't been transferred to Gen1 for special moves.
|
|
|
|
if (enc.Generation == 2)
|
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
|
|
|
return Array.Exists(moves, z => z.Generation != 2);
|
2022-06-18 18:04:24 +00:00
|
|
|
// Gen1 stuff can only be un-evolved if it was never traded from the OT.
|
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 (Array.Exists(moves, z => z.Generation != 1))
|
2022-06-18 18:04:24 +00:00
|
|
|
return true; // traded to Gen2 for special moves
|
|
|
|
if (pk.Format != 1)
|
|
|
|
return true; // traded to Gen2 (current state)
|
|
|
|
return !ParseSettings.IsFromActiveTrainer(pk); // not with OT
|
|
|
|
}
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|