2019-12-08 02:12:31 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.Memory;
|
|
|
|
|
|
|
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
|
{
|
2020-02-13 04:42:45 +00:00
|
|
|
|
bool neverOT = !GetCanOTHandle(data.Info.EncounterMatch, data.pkm, data.Info.Generation);
|
2019-12-08 02:12:31 +00:00
|
|
|
|
VerifyHandlerState(data, neverOT);
|
|
|
|
|
VerifyTradeState(data);
|
|
|
|
|
VerifyOTMisc(data, neverOT);
|
|
|
|
|
VerifyHTMisc(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyTradeState(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
|
|
|
|
|
if (data.pkm is IGeoTrack t)
|
|
|
|
|
VerifyGeoLocationData(data, t, data.pkm);
|
|
|
|
|
|
2020-12-22 06:33:48 +00:00
|
|
|
|
if (pkm.VC && pkm is PK7 {Geo1_Country: 0}) // VC transfers set Geo1 Country
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LGeoMemoryMissing));
|
|
|
|
|
|
|
|
|
|
if (!pkm.IsUntraded)
|
2019-12-22 00:01:51 +00:00
|
|
|
|
{
|
2021-11-22 03:29:07 +00:00
|
|
|
|
// Can't have HT details even as a Link Trade egg, except in some games.
|
|
|
|
|
if (pkm.IsEgg && !EggStateLegality.IsValidHTEgg(pkm))
|
2019-12-22 00:01:51 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryArgBadHT));
|
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
|
|
|
|
|
|
|
|
|
if (pkm.CurrentHandler != 0) // Badly edited; PKHeX doesn't trip this.
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryHTFlagInvalid));
|
|
|
|
|
else if (pkm.HT_Friendship != 0)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatFriendshipHT0));
|
2020-12-29 08:58:08 +00:00
|
|
|
|
else if (pkm is IAffection {HT_Affection: not 0})
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatAffectionHT0));
|
|
|
|
|
|
|
|
|
|
// Don't check trade evolutions if Untraded. The Evolution Chain already checks for trade evolutions.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="PKM.CurrentHandler"/> state is set correctly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void VerifyHandlerState(LegalityAnalysis data, bool neverOT)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var Info = data.Info;
|
|
|
|
|
|
|
|
|
|
// HT Flag
|
2022-06-05 20:18:31 +00:00
|
|
|
|
if (ParseSettings.CheckActiveHandler)
|
|
|
|
|
{
|
|
|
|
|
var tr = ParseSettings.ActiveTrainer;
|
|
|
|
|
var withOT = tr.IsFromTrainer(pkm);
|
|
|
|
|
var flag = pkm.CurrentHandler;
|
|
|
|
|
var expect = withOT ? 0 : 1;
|
|
|
|
|
if (flag != expect)
|
|
|
|
|
{
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LTransferCurrentHandlerInvalid));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag == 1)
|
|
|
|
|
{
|
|
|
|
|
if (pkm.HT_Name != tr.OT)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LTransferHTMismatchName));
|
|
|
|
|
if (pkm is IHandlerLanguage h && h.HT_Language != tr.Language)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LTransferHTMismatchLanguage));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 02:12:31 +00:00
|
|
|
|
if ((Info.Generation != pkm.Format || neverOT) && pkm.CurrentHandler != 1)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LTransferHTFlagRequired));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks the non-Memory data for the <see cref="PKM.OT_Name"/> details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void VerifyOTMisc(LegalityAnalysis data, bool neverOT)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var Info = data.Info;
|
|
|
|
|
|
|
|
|
|
VerifyOTAffection(data, neverOT, Info.Generation, pkm);
|
|
|
|
|
VerifyOTFriendship(data, neverOT, Info.Generation, pkm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyOTFriendship(LegalityAnalysis data, bool neverOT, int origin, PKM pkm)
|
|
|
|
|
{
|
2022-02-22 01:46:03 +00:00
|
|
|
|
if (origin < 0)
|
|
|
|
|
return;
|
2022-05-31 04:43:52 +00:00
|
|
|
|
|
2022-02-22 01:46:03 +00:00
|
|
|
|
if (origin <= 2)
|
2019-12-08 02:12:31 +00:00
|
|
|
|
{
|
2022-05-31 04:43:52 +00:00
|
|
|
|
VerifyOTFriendshipVC12(data, pkm);
|
|
|
|
|
return;
|
2019-12-09 23:50:03 +00:00
|
|
|
|
}
|
2022-05-31 04:43:52 +00:00
|
|
|
|
|
|
|
|
|
if (neverOT)
|
2019-12-09 23:50:03 +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.
|
|
|
|
|
var fs = pkm.OT_Friendship;
|
2020-12-22 17:09:49 +00:00
|
|
|
|
var enc = data.Info.EncounterMatch;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
if (GetBaseFriendship(enc, origin) != fs)
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatFriendshipOTBaseEvent));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 04:43:52 +00:00
|
|
|
|
private void VerifyOTFriendshipVC12(LegalityAnalysis data, PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
var any = IsMatchFriendship(data.Info.EvoChainsAllGens.Gen7, PersonalTable.USUM, pkm.OT_Friendship);
|
|
|
|
|
if (!any)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatFriendshipOTBaseEvent));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsMatchFriendship(EvoCriteria[] evos, PersonalTable pt, int fs)
|
|
|
|
|
{
|
|
|
|
|
foreach (var z in evos)
|
|
|
|
|
{
|
|
|
|
|
if (!pt.IsPresentInGame(z.Species, z.Form))
|
|
|
|
|
continue;
|
|
|
|
|
var entry = pt.GetFormEntry(z.Species, z.Form);
|
|
|
|
|
if (entry.BaseFriendship == fs)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 02:12:31 +00:00
|
|
|
|
private void VerifyOTAffection(LegalityAnalysis data, bool neverOT, int origin, PKM pkm)
|
|
|
|
|
{
|
2020-12-22 01:12:39 +00:00
|
|
|
|
if (pkm is not IAffection a)
|
2020-08-02 18:06:30 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-12-08 02:12:31 +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.
|
2020-08-02 18:06:30 +00:00
|
|
|
|
if ((origin <= 2 && a.OT_Affection != 0) || IsInvalidContestAffection(a))
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatAffectionOT0));
|
|
|
|
|
}
|
|
|
|
|
else if (neverOT)
|
|
|
|
|
{
|
|
|
|
|
if (origin == 6)
|
|
|
|
|
{
|
|
|
|
|
if (pkm.IsUntraded && pkm.XY)
|
|
|
|
|
{
|
2020-08-02 18:06:30 +00:00
|
|
|
|
if (a.OT_Affection != 0)
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatAffectionOT0));
|
|
|
|
|
}
|
2020-08-02 18:06:30 +00:00
|
|
|
|
else if (IsInvalidContestAffection(a))
|
2019-12-08 02:12:31 +00:00
|
|
|
|
{
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatAffectionOT0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-08-02 18:06:30 +00:00
|
|
|
|
if (a.OT_Affection != 0)
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryStatAffectionOT0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks the non-Memory data for the <see cref="PKM.HT_Name"/> details.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void VerifyHTMisc(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var htGender = pkm.HT_Gender;
|
|
|
|
|
if (htGender > 1 || (pkm.IsUntraded && htGender != 0))
|
|
|
|
|
data.AddLine(GetInvalid(string.Format(LegalityCheckStrings.LMemoryHTGender, htGender)));
|
|
|
|
|
|
|
|
|
|
if (pkm is IHandlerLanguage h)
|
|
|
|
|
VerifyHTLanguage(data, h, pkm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyHTLanguage(LegalityAnalysis data, IHandlerLanguage h, PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
if (h.HT_Language == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(pkm.HT_Name))
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryHTLanguage));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(pkm.HT_Name))
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryHTLanguage));
|
|
|
|
|
else if (h.HT_Language > (int)LanguageID.ChineseT)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LMemoryHTLanguage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyGeoLocationData(LegalityAnalysis data, IGeoTrack t, PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
var valid = t.GetValidity();
|
|
|
|
|
if (valid == GeoValid.CountryAfterPreviousEmpty)
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LGeoBadOrder));
|
2021-08-13 22:36:30 +00:00
|
|
|
|
else if (valid == GeoValid.RegionWithoutCountry)
|
2019-12-08 02:12:31 +00:00
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LGeoNoRegion));
|
|
|
|
|
if (t.Geo1_Country != 0 && pkm.IsUntraded) // traded
|
|
|
|
|
data.AddLine(GetInvalid(LegalityCheckStrings.LGeoNoCountryHT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ORAS contests mistakenly apply 20 affection to the OT instead of the current handler's value
|
2020-08-02 18:06:30 +00:00
|
|
|
|
private static bool IsInvalidContestAffection(IAffection pkm) => pkm.OT_Affection != 255 && pkm.OT_Affection % 20 != 0;
|
2019-12-08 02:12:31 +00:00
|
|
|
|
|
2021-07-14 00:33:23 +00:00
|
|
|
|
public static bool GetCanOTHandle(IEncounterTemplate enc, PKM pkm, int generation)
|
2019-12-08 02:12:31 +00:00
|
|
|
|
{
|
2021-01-10 05:25:28 +00:00
|
|
|
|
// 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
|
|
|
|
|
{
|
|
|
|
|
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(pkm.Language) => false,
|
2021-11-20 02:23:49 +00:00
|
|
|
|
WB8 wb8 when wb8.GetHasOT(pkm.Language) => false,
|
2022-02-05 01:35:15 +00:00
|
|
|
|
WA8 wa8 when wa8.GetHasOT(pkm.Language) => false,
|
2021-01-10 05:25:28 +00:00
|
|
|
|
WC8 {IsHOMEGift: true} => false,
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => true,
|
2021-01-10 05:25:28 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2021-01-02 01:08:49 +00:00
|
|
|
|
|
2021-11-20 02:23:49 +00:00
|
|
|
|
private static int GetBaseFriendship(IEncounterTemplate enc, int generation) => enc switch
|
|
|
|
|
{
|
|
|
|
|
IFixedOTFriendship f => f.OT_Friendship,
|
2022-02-25 04:17:58 +00:00
|
|
|
|
|
|
|
|
|
{ Version: GameVersion.BDSP or GameVersion.BD or GameVersion.SP }
|
|
|
|
|
=> PersonalTable.BDSP.GetFormEntry(enc.Species, enc.Form).BaseFriendship,
|
|
|
|
|
{ Version: GameVersion.PLA }
|
|
|
|
|
=> PersonalTable.LA .GetFormEntry(enc.Species, enc.Form).BaseFriendship,
|
|
|
|
|
|
2021-11-20 02:23:49 +00:00
|
|
|
|
_ => GetBaseFriendship(generation, enc.Species, enc.Form),
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-02 01:08:49 +00:00
|
|
|
|
private static int GetBaseFriendship(int generation, int species, int form) => generation switch
|
2019-12-08 02:12:31 +00:00
|
|
|
|
{
|
2021-01-02 01:08:49 +00:00
|
|
|
|
6 => PersonalTable.AO[species].BaseFriendship,
|
|
|
|
|
7 => PersonalTable.USUM[species].BaseFriendship,
|
|
|
|
|
8 => PersonalTable.SWSH.GetFormEntry(species, form).BaseFriendship,
|
2022-05-31 04:43:52 +00:00
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(generation)),
|
2021-01-02 01:08:49 +00:00
|
|
|
|
};
|
2019-12-08 02:12:31 +00:00
|
|
|
|
}
|
2020-05-10 03:47:32 +00:00
|
|
|
|
}
|