2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies the Friendship, Affection, and other miscellaneous stats that can be present for OT/HT data.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class HistoryVerifier : Verifier
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.Memory;
|
|
|
|
|
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
bool neverOT = !GetCanOTHandle(data.Info.EncounterMatch, data.Entity, data.Info.Generation);
|
|
|
|
VerifyHandlerState(data, neverOT);
|
|
|
|
VerifyTradeState(data);
|
|
|
|
VerifyOTMisc(data, neverOT);
|
|
|
|
VerifyHTMisc(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyTradeState(LegalityAnalysis data)
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var pk = data.Entity;
|
|
|
|
|
|
|
|
if (data.Entity is IGeoTrack t)
|
|
|
|
VerifyGeoLocationData(data, t, data.Entity);
|
|
|
|
|
|
|
|
if (pk.VC && pk is PK7 {Geo1_Country: 0}) // VC transfers set Geo1 Country
|
|
|
|
data.AddLine(GetInvalid(LGeoMemoryMissing));
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pk.IsUntraded)
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Can't have HT details even as a Link Trade egg, except in some games.
|
|
|
|
if (pk.IsEgg && !EggStateLegality.IsValidHTEgg(pk))
|
|
|
|
data.AddLine(GetInvalid(LMemoryArgBadHT));
|
|
|
|
return;
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.CurrentHandler != 0) // Badly edited; PKHeX doesn't trip this.
|
|
|
|
data.AddLine(GetInvalid(LMemoryHTFlagInvalid));
|
|
|
|
else if (pk.HT_Friendship != 0)
|
|
|
|
data.AddLine(GetInvalid(LMemoryStatFriendshipHT0));
|
|
|
|
else if (pk is IAffection {HT_Affection: not 0})
|
|
|
|
data.AddLine(GetInvalid(LMemoryStatAffectionHT0));
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Don't check trade evolutions if Untraded. The Evolution Chain already checks for trade evolutions.
|
|
|
|
}
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="PKM.CurrentHandler"/> state is set correctly.
|
|
|
|
/// </summary>
|
|
|
|
private void VerifyHandlerState(LegalityAnalysis data, bool neverOT)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var Info = data.Info;
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// HT Flag
|
|
|
|
if (ParseSettings.CheckActiveHandler)
|
|
|
|
{
|
|
|
|
var tr = ParseSettings.ActiveTrainer;
|
|
|
|
var withOT = tr.IsFromTrainer(pk);
|
|
|
|
var flag = pk.CurrentHandler;
|
|
|
|
var expect = withOT ? 0 : 1;
|
|
|
|
if (flag != expect)
|
2019-12-22 00:01:51 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
data.AddLine(GetInvalid(LTransferCurrentHandlerInvalid));
|
2019-12-08 02:12:31 +00:00
|
|
|
return;
|
2019-12-22 00:01:51 +00:00
|
|
|
}
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (flag == 1)
|
2022-06-05 20:18:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.HT_Name != tr.OT)
|
|
|
|
data.AddLine(GetInvalid(LTransferHTMismatchName));
|
|
|
|
if (pk is IHandlerLanguage h && h.HT_Language != tr.Language)
|
|
|
|
data.AddLine(GetInvalid(LTransferHTMismatchLanguage));
|
2022-06-05 20:18:31 +00:00
|
|
|
}
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pk.IsUntraded && IsUntradeableEncounter(Info.EncounterMatch)) // Starter, untradeable
|
|
|
|
data.AddLine(GetInvalid(LTransferCurrentHandlerInvalid));
|
|
|
|
if ((Info.Generation != pk.Format || neverOT) && pk.CurrentHandler != 1)
|
|
|
|
data.AddLine(GetInvalid(LTransferHTFlagRequired));
|
|
|
|
}
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool IsUntradeableEncounter(IEncounterTemplate enc) => enc switch
|
|
|
|
{
|
|
|
|
EncounterStatic7b { Location: 28 } => true, // LGP/E Starter
|
2022-11-25 01:42:17 +00:00
|
|
|
EncounterStatic9 { Species: 998 or 999, Level: 68 } => true, // SV Ride legend
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks the non-Memory data for the <see cref="PKM.OT_Name"/> details.
|
|
|
|
/// </summary>
|
|
|
|
private void VerifyOTMisc(LegalityAnalysis data, bool neverOT)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var Info = data.Info;
|
2022-05-31 04:43:52 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
VerifyOTAffection(data, neverOT, Info.Generation, pk);
|
|
|
|
VerifyOTFriendship(data, neverOT, Info.Generation, pk);
|
|
|
|
}
|
2022-05-31 04:43:52 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void VerifyOTFriendship(LegalityAnalysis data, bool neverOT, int origin, PKM pk)
|
|
|
|
{
|
|
|
|
if (origin < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (origin <= 2)
|
|
|
|
{
|
|
|
|
VerifyOTFriendshipVC12(data, pk);
|
|
|
|
return;
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (neverOT)
|
2022-05-31 04:43:52 +00:00
|
|
|
{
|
|
|
|
// Verify the original friendship value since it cannot change from the value it was assigned in the original generation.
|
|
|
|
// If none match, then it is not a valid OT friendship.
|
2022-06-18 18:04:24 +00:00
|
|
|
var fs = pk.OT_Friendship;
|
|
|
|
var enc = data.Info.EncounterMatch;
|
2022-11-25 01:42:17 +00:00
|
|
|
if (GetBaseFriendship(enc) != fs)
|
2022-06-18 18:04:24 +00:00
|
|
|
data.AddLine(GetInvalid(LMemoryStatFriendshipOTBaseEvent));
|
2022-05-31 04:43:52 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void VerifyOTFriendshipVC12(LegalityAnalysis data, PKM pk)
|
|
|
|
{
|
|
|
|
// Verify the original friendship value since it cannot change from the value it was assigned in the original generation.
|
|
|
|
// Since some evolutions have different base friendship values, check all possible evolutions for a match.
|
|
|
|
// If none match, then it is not a valid OT friendship.
|
|
|
|
// VC transfers use SM personal info
|
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 any = IsMatchFriendship(data.Info.EvoChainsAllGens.Gen7, pk.OT_Friendship);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!any)
|
|
|
|
data.AddLine(GetInvalid(LMemoryStatFriendshipOTBaseEvent));
|
|
|
|
}
|
2022-05-31 04:43:52 +00:00
|
|
|
|
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
|
|
|
private static bool IsMatchFriendship(EvoCriteria[] evos, int fs)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
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 pt = PersonalTable.USUM;
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var z in evos)
|
2022-05-31 04:43:52 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pt.IsPresentInGame(z.Species, z.Form))
|
|
|
|
continue;
|
|
|
|
var entry = pt.GetFormEntry(z.Species, z.Form);
|
|
|
|
if (entry.BaseFriendship == fs)
|
|
|
|
return true;
|
2022-05-31 04:43:52 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-05-31 04:43:52 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void VerifyOTAffection(LegalityAnalysis data, bool neverOT, int origin, PKM pk)
|
|
|
|
{
|
|
|
|
if (pk is not IAffection a)
|
|
|
|
return;
|
2020-08-02 18:06:30 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (origin < 6)
|
|
|
|
{
|
|
|
|
// Can gain affection in Gen6 via the Contest glitch applying affection to OT rather than HT.
|
|
|
|
// VC encounters cannot obtain OT affection since they can't visit Gen6.
|
|
|
|
if ((origin <= 2 && a.OT_Affection != 0) || IsInvalidContestAffection(a))
|
|
|
|
data.AddLine(GetInvalid(LMemoryStatAffectionOT0));
|
|
|
|
}
|
|
|
|
else if (neverOT)
|
|
|
|
{
|
|
|
|
if (origin == 6)
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.IsUntraded && pk.XY)
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (a.OT_Affection != 0)
|
|
|
|
data.AddLine(GetInvalid(LMemoryStatAffectionOT0));
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (IsInvalidContestAffection(a))
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
data.AddLine(GetInvalid(LMemoryStatAffectionOT0));
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (a.OT_Affection != 0)
|
|
|
|
data.AddLine(GetInvalid(LMemoryStatAffectionOT0));
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks the non-Memory data for the <see cref="PKM.HT_Name"/> details.
|
|
|
|
/// </summary>
|
|
|
|
private void VerifyHTMisc(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var htGender = pk.HT_Gender;
|
|
|
|
if (htGender > 1 || (pk.IsUntraded && htGender != 0))
|
|
|
|
data.AddLine(GetInvalid(string.Format(LMemoryHTGender, htGender)));
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is IHandlerLanguage h)
|
|
|
|
VerifyHTLanguage(data, h, pk);
|
|
|
|
}
|
2019-12-08 02:12:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void VerifyHTLanguage(LegalityAnalysis data, IHandlerLanguage h, PKM pk)
|
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
var enc = data.EncounterOriginal;
|
|
|
|
if (enc is EncounterStatic9 { GiftWithLanguage: true })
|
|
|
|
{
|
|
|
|
if (h.HT_Language == 0)
|
|
|
|
data.AddLine(GetInvalid(LMemoryHTLanguage));
|
|
|
|
else if (pk.IsUntraded && h.HT_Language != pk.Language)
|
|
|
|
data.AddLine(GetInvalid(LMemoryHTLanguage));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (h.HT_Language == 0)
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(pk.HT_Name))
|
|
|
|
data.AddLine(GetInvalid(LMemoryHTLanguage));
|
|
|
|
return;
|
2021-01-10 05:25:28 +00:00
|
|
|
}
|
2021-01-02 01:08:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(pk.HT_Name))
|
|
|
|
data.AddLine(GetInvalid(LMemoryHTLanguage));
|
|
|
|
else if (h.HT_Language > (int)LanguageID.ChineseT)
|
|
|
|
data.AddLine(GetInvalid(LMemoryHTLanguage));
|
|
|
|
}
|
2022-02-25 04:17:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void VerifyGeoLocationData(LegalityAnalysis data, IGeoTrack t, PKM pk)
|
|
|
|
{
|
|
|
|
var valid = t.GetValidity();
|
|
|
|
if (valid == GeoValid.CountryAfterPreviousEmpty)
|
|
|
|
data.AddLine(GetInvalid(LGeoBadOrder));
|
|
|
|
else if (valid == GeoValid.RegionWithoutCountry)
|
|
|
|
data.AddLine(GetInvalid(LGeoNoRegion));
|
|
|
|
if (t.Geo1_Country != 0 && pk.IsUntraded) // traded
|
|
|
|
data.AddLine(GetInvalid(LGeoNoCountryHT));
|
|
|
|
}
|
2022-02-25 04:17:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// ORAS contests mistakenly apply 20 affection to the OT instead of the current handler's value
|
|
|
|
private static bool IsInvalidContestAffection(IAffection pk) => pk.OT_Affection != 255 && pk.OT_Affection % 20 != 0;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool GetCanOTHandle(IEncounterTemplate enc, PKM pk, int generation)
|
|
|
|
{
|
|
|
|
// Handlers introduced in Generation 6. OT Handling was always the case for Generation 3-5 data.
|
|
|
|
if (generation < 6)
|
|
|
|
return generation >= 3;
|
|
|
|
|
|
|
|
return enc switch
|
2019-12-08 02:12:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
EncounterTrade => false,
|
|
|
|
EncounterSlot8GO => false,
|
|
|
|
WC6 wc6 when wc6.OT_Name.Length > 0 => false,
|
|
|
|
WC7 wc7 when wc7.OT_Name.Length > 0 && wc7.TID != 18075 => false, // Ash Pikachu QR Gift doesn't set Current Handler
|
|
|
|
WC8 wc8 when wc8.GetHasOT(pk.Language) => false,
|
|
|
|
WB8 wb8 when wb8.GetHasOT(pk.Language) => false,
|
|
|
|
WA8 wa8 when wa8.GetHasOT(pk.Language) => false,
|
|
|
|
WC8 {IsHOMEGift: true} => false,
|
|
|
|
_ => true,
|
2021-01-02 01:08:49 +00:00
|
|
|
};
|
2019-12-08 02:12:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
private static int GetBaseFriendship(IEncounterTemplate enc) => enc switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
IFixedOTFriendship f => f.OT_Friendship,
|
2022-11-25 01:42:17 +00:00
|
|
|
_ => GetBaseFriendship(enc.Context, enc.Species, enc.Form),
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
private static int GetBaseFriendship(EntityContext context, ushort species, byte form) => context switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
EntityContext.Gen6 => PersonalTable.AO[species].BaseFriendship,
|
|
|
|
EntityContext.Gen7 => PersonalTable.USUM[species].BaseFriendship,
|
|
|
|
EntityContext.Gen8 => PersonalTable.SWSH.GetFormEntry(species, form).BaseFriendship,
|
|
|
|
EntityContext.Gen8a => PersonalTable.LA.GetFormEntry(species, form).BaseFriendship,
|
|
|
|
EntityContext.Gen8b => PersonalTable.BDSP.GetFormEntry(species, form).BaseFriendship,
|
|
|
|
EntityContext.Gen9 => PersonalTable.SV.GetFormEntry(species, form).BaseFriendship,
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(context)),
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
2020-05-10 03:47:32 +00:00
|
|
|
}
|