PKHeX/PKHeX.Core/Legality/Verifiers/LevelVerifier.cs

142 lines
5.2 KiB
C#
Raw Normal View History

using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.Core;
/// <summary>
/// Verifies the <see cref="PKM.CurrentLevel"/>.
/// </summary>
public sealed class LevelVerifier : Verifier
{
protected override CheckIdentifier Identifier => CheckIdentifier.Level;
public override void Verify(LegalityAnalysis data)
{
var pk = data.Entity;
var enc = data.EncounterOriginal;
if (enc is MysteryGift gift)
{
2023-03-26 00:48:49 +00:00
if (!IsMetLevelMatchEncounter(gift, pk))
{
2023-03-26 00:48:49 +00:00
data.AddLine(GetInvalid(LLevelMetGift));
return;
}
if (gift.Level > pk.CurrentLevel)
{
data.AddLine(GetInvalid(LLevelMetGiftFail));
return;
}
}
if (pk.IsEgg)
{
int elvl = enc.LevelMin;
if (elvl != pk.CurrentLevel)
{
data.AddLine(GetInvalid(string.Format(LEggFMetLevel_0, elvl)));
return;
}
var reqEXP = enc is EncounterStatic2 { DizzyPunchEgg: true }
? 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;
}
int lvl = pk.CurrentLevel;
if (lvl >= 100)
{
var expect = Experience.GetEXP(100, pk.PersonalInfo.EXPGrowth);
if (pk.EXP != expect)
data.AddLine(GetInvalid(LLevelEXPTooHigh));
}
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));
}
2023-03-26 00:48:49 +00:00
private static bool IsMetLevelMatchEncounter(MysteryGift gift, PKM pk)
{
if (gift.Level == pk.Met_Level)
return true;
if (!pk.HasOriginalMetLocation)
return true;
return gift switch
{
WC3 wc3 when wc3.Met_Level == pk.Met_Level || wc3.IsEgg => true,
WC7 wc7 when wc7.MetLevel == pk.Met_Level => true,
PGT { IsManaphyEgg: true } when pk.Met_Level == 0 => true,
_ => false,
};
}
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));
}
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)));
}
}
/// <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.
if (enc.Species != species)
return false;
if (!GBRestrictions.IsTradeEvolution1(enc.Species))
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.AllowGBStadium2)
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;
// 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)
return MoveInfo.IsAnyFromGeneration(1, moves);
// Gen1 stuff can only be un-evolved if it was never traded from the OT.
if (MoveInfo.IsAnyFromGeneration(2, moves))
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
}
}