PKHeX/PKHeX.Core/Legality/Verifiers/LevelVerifier.cs
Kurt 1e86fdcea8
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-29 17:55:27 -08:00

108 lines
4.4 KiB
C#

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 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)
{
data.AddLine(GetInvalid(string.Format(LEggFMetLevel_0, elvl)));
return;
}
var reqEXP = enc is EncounterStatic {Version: GameVersion.C}
? 125 // Gen2 Dizzy Punch gifts always have 125 EXP, even if it's more than the Lv5 exp required.
: Experience.GetEXP(elvl, pkm.PersonalInfo.EXPGrowth);
if (reqEXP != pkm.EXP)
data.AddLine(GetInvalid(LEggEXP));
return;
}
int lvl = pkm.CurrentLevel;
if (lvl < pkm.Met_Level)
data.AddLine(GetInvalid(LLevelMetBelow));
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));
}
// 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
if (pkm.Format <= 2 && enc is not EncounterTrade && enc.Species == pkm.Species && GBRestrictions.Trade_Evolution1.Contains(enc.Species))
VerifyG1TradeEvo(data);
}
private void VerifyG1TradeEvo(LegalityAnalysis data)
{
// 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.
if (ParseSettings.ActiveTrainer.Generation >= 3 || ParseSettings.AllowGBCartEra)
return;
var pkm = data.pkm;
var mustevolve = pkm.TradebackStatus == TradebackType.WasTradeback || (pkm.Format == 1 && !ParseSettings.IsFromActiveTrainer(pkm)) || GBRestrictions.IsTradedKadabraG1(pkm);
if (!mustevolve)
return;
// Pokemon have been traded but it is not evolved, trade evolutions are sequential dex numbers
var evolved = ParseSettings.SpeciesStrings[pkm.Species + 1];
var unevolved = ParseSettings.SpeciesStrings[pkm.Species];
data.AddLine(GetInvalid(string.Format(LEvoTradeReqOutsider, unevolved, evolved)));
}
}
}