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

128 lines
5.4 KiB
C#
Raw Normal View History

using System.Linq;
using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.Core
{
2018-07-02 02:17:37 +00:00
/// <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 pkm = data.pkm;
var enc = data.EncounterOriginal;
if (enc is MysteryGift gift)
{
if (gift.Level != pkm.Met_Level && pkm.HasOriginalMetLocation)
{
switch (gift)
{
case WC3 wc3 when wc3.Met_Level == pkm.Met_Level || wc3.IsEgg:
break;
case WC7 wc7 when wc7.MetLevel == pkm.Met_Level:
break;
case PGT {IsManaphyEgg: true} when pkm.Met_Level == 0:
break;
default:
data.AddLine(GetInvalid(LLevelMetGift));
return;
}
}
if (gift.Level > pkm.CurrentLevel)
{
data.AddLine(GetInvalid(LLevelMetGiftFail));
return;
}
}
if (pkm.IsEgg)
{
int elvl = enc.LevelMin;
if (elvl != pkm.CurrentLevel)
2018-07-29 01:09:29 +00:00
{
data.AddLine(GetInvalid(string.Format(LEggFMetLevel_0, elvl)));
2018-07-29 01:09:29 +00:00
return;
}
var reqEXP = enc is EncounterStatic {Version: GameVersion.C}
2018-07-29 01:09:29 +00:00
? 125 // Gen2 Dizzy Punch gifts always have 125 EXP, even if it's more than the Lv5 exp required.
2019-11-16 01:34:18 +00:00
: Experience.GetEXP(elvl, pkm.PersonalInfo.EXPGrowth);
2018-07-29 01:09:29 +00:00
if (reqEXP != pkm.EXP)
data.AddLine(GetInvalid(LEggEXP));
return;
}
int lvl = pkm.CurrentLevel;
if (lvl < pkm.Met_Level)
data.AddLine(GetInvalid(LLevelMetBelow));
Fracture the encounter matching checks to allow progressive validation (#3137) ## Issue We want to discard-but-remember any slots that aren't a perfect fit, on the off chance that a better one exists later in the search space. If there's no better match, then we gotta go with what we got. ## Example: Wurmple exists in area `X`, and also has a more rare slot for Silcoon, with the same level for both slots. * We have a Silcoon that we've leveled up a few times. Was our Silcoon originally a Wurmple, or was it caught as a Silcoon? * To be sure, we have to check the EC/PID if the Wurmple wouldn't evolve into Cascoon instead. * We don't want to wholly reject that Wurmple slot, as maybe the Met Level isn't within Silcoon's slot range. --- Existing implementation would store "deferred" matches in a list; we only need to keep 1 of these matches around (less allocation!). We also want to differentiate between a "good" deferral and a "bad" deferral; I don't think this is necessary but it's currently used by Mystery Gift matching (implemented for the Eeveelution mystery gifts which matter for evolution moves). The existing logic didn't use inheritance, and instead had static methods being reused across generations. Quite kludgy. Also, the existing logic was a pain to modify the master encounter yield methods, as one generation's quirks had to not impact all other generations that used the method. --- The new implementation splits out the encounter yielding methods to be separate for each generation / subset. Now, things don't have to check `WasLink` for Gen7 origin, because Pokémon Link wasn't a thing in Gen7. --- ## Future Maybe refactoring yielders into "GameCores" that expose yielding behaviors / properties, rather than the static logic. As more generations and side-gamegroups get added (thanks LGPE/GO/GameCube), all this switch stuff gets annoying to maintain instead of just overriding/inheritance. ## Conclusion This shouldn't impact any legality results negatively; if you notice any regressions, report them! This should reduce false flags where we didn't defer-discard an encounter when we should have (wild area mons being confused with raids).
2021-01-30 01:55:27 +00:00
else if (!enc.IsWithinEncounterRange(pkm) && lvl != 100 && pkm.EXP == Experience.GetEXP(lvl, pkm.PersonalInfo.EXPGrowth))
data.AddLine(Get(LLevelEXPThreshold, Severity.Fishy));
else
data.AddLine(GetValid(LLevelMetSane));
}
public void VerifyG1(LegalityAnalysis data)
{
var pkm = data.pkm;
var enc = data.EncounterMatch;
if (pkm.IsEgg)
{
const int elvl = 5;
if (elvl != pkm.CurrentLevel)
data.AddLine(GetInvalid(string.Format(LEggFMetLevel_0, elvl)));
return;
}
if (pkm.Met_Location != 0) // crystal
{
int lvl = pkm.CurrentLevel;
if (lvl < pkm.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 pkm = data.pkm;
var species = pkm.Species;
// This check is only applicable if it's a trade evolution that has not been evolved.
if (!GBRestrictions.Trade_Evolution1.Contains(enc.Species) || enc.Species != 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.AllowGBCartEra)
return false;
// 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 data.Info.Moves.All(z => z.Generation == 2);
// Gen1 stuff can only be un-evolved if it was never traded from the OT.
if (data.Info.Moves.Any(z => z.Generation != 1))
return true; // traded to Gen2 for special moves
if (pkm.Format != 1)
return true; // traded to Gen2 (current state)
return !ParseSettings.IsFromActiveTrainer(pkm); // not with OT
}
}
}