PKHeX/PKHeX.Core/Legality/Verifiers/MemoryVerifier.cs

391 lines
17 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
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.OT_Memory"/>, <see cref="PKM.HT_Memory"/>, and associated values.
/// </summary>
public sealed class MemoryVerifier : Verifier
{
protected override CheckIdentifier Identifier => CheckIdentifier.Memory;
public override void Verify(LegalityAnalysis data)
{
var hist = VerifyHistory(data);
2018-07-02 02:17:37 +00:00
VerifyOTMemory(data);
VerifyHTMemory(data);
data.AddLine(hist);
}
private CheckResult VerifyHistory(LegalityAnalysis data)
{
var pkm = data.pkm;
var Info = data.Info;
var EncounterMatch = data.EncounterMatch;
if (Info.Generation < 6)
{
if (pkm.Format < 6)
2018-07-02 02:17:37 +00:00
return GetValid(V128);
if (pkm.OT_Affection != 0 && Info.Generation <= 2 || IsInvalidContestAffection(pkm))
2018-07-02 02:17:37 +00:00
return GetInvalid(V129);
if (pkm.OT_Memory > 0 || pkm.OT_Feeling > 0 || pkm.OT_Intensity > 0 || pkm.OT_TextVar > 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V130);
}
if (pkm.Format >= 6 && Info.Generation != pkm.Format && pkm.CurrentHandler != 1)
2018-07-02 02:17:37 +00:00
return GetInvalid(V124);
if (pkm.HT_Gender > 1)
2018-07-02 02:17:37 +00:00
return GetInvalid(string.Format(V131, pkm.HT_Gender));
if (EncounterMatch is WC6 wc6 && wc6.OT_Name.Length > 0)
{
if (pkm.OT_Friendship != PersonalTable.AO[EncounterMatch.Species].BaseFriendship)
2018-07-02 02:17:37 +00:00
return GetInvalid(V132);
if (pkm.OT_Affection != 0 && (pkm.AO || !pkm.IsUntraded) && IsInvalidContestAffection(pkm))
2018-07-02 02:17:37 +00:00
return GetInvalid(V133);
if (pkm.CurrentHandler != 1)
2018-07-02 02:17:37 +00:00
return GetInvalid(V134);
}
else if (EncounterMatch is WC7 wc7 && wc7.OT_Name.Length > 0 && wc7.TID != 18075) // Ash Pikachu QR Gift doesn't set Current Handler
{
if (pkm.OT_Friendship != PersonalTable.USUM[EncounterMatch.Species].BaseFriendship)
2018-07-02 02:17:37 +00:00
return GetInvalid(V132);
if (pkm.OT_Affection != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V133);
if (pkm.CurrentHandler != 1)
2018-07-02 02:17:37 +00:00
return GetInvalid(V134);
}
else if (EncounterMatch is MysteryGift mg && mg.Format < 6 && pkm.Format >= 6)
{
if (pkm.OT_Affection != 0 && IsInvalidContestAffection(pkm))
2018-07-02 02:17:37 +00:00
return GetInvalid(V133);
if (pkm.CurrentHandler != 1)
2018-07-02 02:17:37 +00:00
return GetInvalid(V134);
}
// Check sequential order (no zero gaps)
if (pkm is IGeoTrack t)
{
var valid = t.GetValidity();
if (valid == GeoValid.CountryAfterPreviousEmpty)
2018-07-02 02:17:37 +00:00
return GetInvalid(V135);
if (valid == GeoValid.RegionWithoutCountry)
2018-07-02 02:17:37 +00:00
return GetInvalid(V136);
}
if (pkm.Format >= 7)
return VerifyHistory7(data);
// Determine if we should check for Handling Trainer Memories
// A Pokémon is untraded if...
2018-07-12 02:13:09 +00:00
bool untraded = GetIsUntradedByEncounterMemories(pkm, EncounterMatch, Info.Generation);
if (untraded) // Is not Traded
{
if (pkm.HT_Name.Length != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V146);
if (pkm is IGeoTrack g && g.Geo1_Country != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V147);
if (pkm.HT_Memory != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V148);
if (pkm.CurrentHandler != 0) // Badly edited; PKHeX doesn't trip this.
2018-07-02 02:17:37 +00:00
return GetInvalid(V139);
if (pkm.HT_Friendship != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V140);
if (pkm.HT_Affection != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V141);
if (pkm.XY && pkm is IContestStats s && s.HasContestStats())
2018-07-02 02:17:37 +00:00
return GetInvalid(V138);
if (VerifyHistoryUntradedHandler(pkm, out CheckResult chk1))
return chk1;
if (EncounterMatch.Species != pkm.Species && VerifyHistoryUntradedEvolution(pkm, Info.EvoChainsAllGens, out CheckResult chk2))
return chk2;
}
else // Is Traded
{
if (pkm.Format == 6 && pkm.HT_Memory == 0 && !pkm.IsEgg)
2018-07-02 02:17:37 +00:00
return GetInvalid(V150);
}
// Memory ChecksResult
if (pkm.IsEgg)
{
if (pkm.HT_Memory != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V149);
if (pkm.OT_Memory != 0)
2018-07-02 02:17:37 +00:00
return GetInvalid(V151);
}
else if (!(EncounterMatch is WC6))
{
if (pkm.OT_Memory == 0 ^ !pkm.Gen6)
2018-07-02 02:17:37 +00:00
return GetInvalid(V152);
if (Info.Generation < 6 && pkm.OT_Affection != 0 && IsInvalidContestAffection(pkm))
2018-07-02 02:17:37 +00:00
return GetInvalid(V129);
}
// Unimplemented: Ingame Trade Memories
2018-07-02 02:17:37 +00:00
return GetValid(V145);
}
private CheckResult VerifyHistory7(LegalityAnalysis data)
{
var pkm = data.pkm;
var EncounterMatch = data.EncounterMatch;
var Info = data.Info;
if (pkm.VC1 && pkm is IGeoTrack g)
{
var hasGeo = g.Geo1_Country != 0;
if (!hasGeo)
2018-07-02 02:17:37 +00:00
return GetInvalid(V137);
}
if ((2 >= Info.Generation || Info.Generation >= 7) && pkm is IContestStats s && s.HasContestStats())
2018-07-02 02:17:37 +00:00
return GetInvalid(V138);
if (!pkm.WasEvent && pkm.HT_Name.Length == 0) // Is not Traded
{
if (VerifyHistoryUntradedHandler(pkm, out CheckResult chk1))
return chk1;
if (EncounterMatch.Species != pkm.Species && VerifyHistoryUntradedEvolution(pkm, Info.EvoChainsAllGens, out CheckResult chk2))
return chk2;
}
2018-07-02 02:17:37 +00:00
return GetValid(V145);
}
2018-07-02 02:17:37 +00:00
private bool VerifyHistoryUntradedHandler(PKM pkm, out CheckResult result)
{
result = null;
if (pkm.CurrentHandler != 0) // Badly edited; PKHeX doesn't trip this.
2018-07-02 02:17:37 +00:00
result = GetInvalid(V139);
else if (pkm.HT_Friendship != 0)
2018-07-02 02:17:37 +00:00
result = GetInvalid(V140);
else if (pkm.HT_Affection != 0)
2018-07-02 02:17:37 +00:00
result = GetInvalid(V141);
else
return false;
return true;
}
2018-07-02 02:17:37 +00:00
private bool VerifyHistoryUntradedEvolution(PKM pkm, IReadOnlyList<EvoCriteria>[] chain, out CheckResult result)
{
result = null;
// Handling Trainer string is empty implying it has not been traded.
// If it must be trade evolved, flag it.
if (pkm.Species == 350) // Milotic
{
if (Legal.IsTradeEvolved(chain, pkm.Format))
return false;
if (pkm is IContestStats s && s.CNT_Beauty < 170) // Beauty Contest Stat Requirement
2018-07-02 02:17:37 +00:00
result = GetInvalid(V143);
else if (pkm.CurrentLevel == 1)
2018-07-02 02:17:37 +00:00
result = GetInvalid(V144);
else
return false;
return true;
}
if (!Legal.IsTradeEvolved(chain, pkm.Format))
return false;
2018-07-02 02:17:37 +00:00
result = GetInvalid(V142);
return true;
}
private CheckResult VerifyCommonMemory(PKM pkm, int handler)
{
Memories.GetMemoryVariables(pkm, out int m, out int t, out int i, out int f, out string tr, handler);
int matchingMoveMemory = Array.IndexOf(Memories.MoveSpecificMemories[0], m);
if (matchingMoveMemory != -1 && pkm.Species != 235 && !Legal.GetCanLearnMachineMove(pkm, Memories.MoveSpecificMemories[1][matchingMoveMemory], 6))
return GetInvalid(string.Format(V153, tr));
if (m == 6 && !Memories.LocationsWithPKCenter[0].Contains(t))
return GetInvalid(string.Format(V154, tr));
if (m == 21) // {0} saw {2} carrying {1} on its back. {4} that {3}.
if (!Legal.GetCanLearnMachineMove(new PK6 { Species = t, EXP = PKX.GetEXP(100, t) }, 19, 6))
return GetInvalid(string.Format(V153, tr));
if ((m == 16 || m == 48) && (t == 0 || !Legal.GetCanKnowMove(pkm, t, 6)))
return GetInvalid(string.Format(V153, tr));
if (m == 49 && (t == 0 || !Legal.GetCanRelearnMove(pkm, t, 6))) // {0} was able to remember {2} at {1}'s instruction. {4} that {3}.
return GetInvalid(string.Format(V153, tr));
if (!Memories.CanHaveIntensity(m, i))
return GetInvalid(string.Format(V254, tr, Memories.GetMinimumIntensity(m)));
if (m != 4 && !Memories.CanHaveFeeling(m, f))
return GetInvalid(string.Format(V255, tr));
return GetValid(string.Format(V155, tr));
}
private void VerifyOTMemoryIs(LegalityAnalysis data, int m, int i, int t, int f)
{
var pkm = data.pkm;
if (pkm.OT_Memory != m)
2018-07-02 02:17:37 +00:00
data.AddLine(GetInvalid(string.Format(V197, V205, m)));
if (pkm.OT_Intensity != i)
2018-07-02 02:17:37 +00:00
data.AddLine(GetInvalid(string.Format(V198, V205, i)));
if (pkm.OT_TextVar != t)
2018-07-02 02:17:37 +00:00
data.AddLine(GetInvalid(string.Format(V199, V205, t)));
if (pkm.OT_Feeling != f)
2018-07-02 02:17:37 +00:00
data.AddLine(GetInvalid(string.Format(V200, V205, f)));
}
2018-07-02 02:17:37 +00:00
private void VerifyOTMemory(LegalityAnalysis data)
{
var pkm = data.pkm;
if (pkm.Format < 6)
return;
var Info = data.Info;
if (Info.Generation < 6 || pkm.IsEgg)
{
VerifyOTMemoryIs(data, 0, 0, 0, 0); // empty
return;
}
switch (data.EncounterMatch)
{
case EncounterTrade _:
switch (Info.Generation)
{
case 6:
break; // Undocumented, uncommon, and insignificant -- don't bother.
case 7:
VerifyOTMemoryIs(data, 1, 3, 40, 5);
break;
}
return;
case WC6 g when !g.IsEgg:
VerifyOTMemoryIs(data, g.OT_Memory, g.OT_Intensity, g.OT_TextVar, g.OT_Feeling);
return;
case WC7 g when !g.IsEgg:
VerifyOTMemoryIs(data, g.OT_Memory, g.OT_Intensity, g.OT_TextVar, g.OT_Feeling);
return;
}
if (Info.Generation >= 7)
{
VerifyOTMemoryIs(data, 0, 0, 0, 0); // empty
return;
}
switch (pkm.OT_Memory)
{
case 2: // {0} hatched from an Egg and saw {1} for the first time at... {2}. {4} that {3}.
if (pkm.Egg_Location == 0)
data.AddLine(Severity.Invalid, string.Format(V160, V205), CheckIdentifier.Memory);
break;
case 4: // {0} became {1}s friend when it arrived via Link Trade at... {2}. {4} that {3}.
data.AddLine(Severity.Invalid, string.Format(V161, V205), CheckIdentifier.Memory);
return;
case 6: // {0} went to the Pokémon Center in {2} with {1} and had its tired body healed there. {4} that {3}.
int matchingOriginGame = Array.IndexOf(Memories.LocationsWithPKCenter[0], pkm.OT_TextVar);
if (matchingOriginGame != -1)
{
int gameID = Memories.LocationsWithPKCenter[1][matchingOriginGame];
if (pkm.XY && gameID != 0 || pkm.AO && gameID != 1)
data.AddLine(Severity.Invalid, string.Format(V162, V205), CheckIdentifier.Memory);
}
data.AddLine(VerifyCommonMemory(pkm, 0));
return;
case 14:
if (!Legal.GetCanBeCaptured(pkm.OT_TextVar, Info.Generation, (GameVersion)pkm.Version))
data.AddLine(Severity.Invalid, string.Format(V165, V205), CheckIdentifier.Memory);
else
data.AddLine(Severity.Valid, string.Format(V164, V205), CheckIdentifier.Memory);
return;
}
if (pkm.XY && Memories.Memory_NotXY.Contains(pkm.OT_Memory))
data.AddLine(Severity.Invalid, string.Format(V163, V205), CheckIdentifier.Memory);
if (pkm.AO && Memories.Memory_NotAO.Contains(pkm.OT_Memory))
data.AddLine(Severity.Invalid, string.Format(V163, V205), CheckIdentifier.Memory);
data.AddLine(VerifyCommonMemory(pkm, 0));
}
2018-07-02 02:17:37 +00:00
private void VerifyHTMemory(LegalityAnalysis data)
{
var pkm = data.pkm;
if (pkm.Format < 6)
return;
var Info = data.Info;
if (pkm.Format >= 7)
{
/*
* Bank Transfer adds in the Link Trade Memory.
* Trading 7<->7 between games (not Bank) clears this data.
*/
if (pkm.HT_Memory == 0)
{
if (pkm.HT_TextVar != 0 || pkm.HT_Intensity != 0 || pkm.HT_Feeling != 0)
data.AddLine(Severity.Invalid, V329, CheckIdentifier.Memory);
return;
}
// Transfer 6->7 & withdraw to same HT => keeps past gen memory
// Don't require link trade memory for these past gen cases
int gen = Info.Generation;
if (3 <= gen && gen < 7 && pkm.CurrentHandler == 1)
return;
if (pkm.HT_Memory != 4)
data.AddLine(Severity.Invalid, V156, CheckIdentifier.Memory);
if (pkm.HT_TextVar != 0)
data.AddLine(Severity.Invalid, V157, CheckIdentifier.Memory);
if (pkm.HT_Intensity != 1)
data.AddLine(Severity.Invalid, V158, CheckIdentifier.Memory);
if (pkm.HT_Feeling > 10)
data.AddLine(Severity.Invalid, V159, CheckIdentifier.Memory);
return;
}
switch (pkm.HT_Memory)
{
case 0:
if (string.IsNullOrEmpty(pkm.HT_Name))
return;
data.AddLine(Severity.Invalid, V150, CheckIdentifier.Memory); return;
case 1: // {0} met {1} at... {2}. {1} threw a Poké Ball at it, and they started to travel together. {4} that {3}.
data.AddLine(Severity.Invalid, string.Format(V202, V206), CheckIdentifier.Memory); return;
case 2: // {0} hatched from an Egg and saw {1} for the first time at... {2}. {4} that {3}.
data.AddLine(Severity.Invalid, string.Format(V160, V206), CheckIdentifier.Memory); return;
case 14:
if (Legal.GetCanBeCaptured(pkm.HT_TextVar, 6))
data.AddLine(Severity.Valid, string.Format(V164, V206), CheckIdentifier.Memory);
else
data.AddLine(Severity.Invalid, string.Format(V165, V206), CheckIdentifier.Memory);
return;
}
data.AddLine(VerifyCommonMemory(pkm, 1));
}
// ORAS contests mistakenly apply 20 affection to the OT instead of the current handler's value
private static bool IsInvalidContestAffection(PKM pkm) => pkm.OT_Affection != 255 && pkm.OT_Affection % 20 != 0;
2018-07-12 02:13:09 +00:00
private static bool GetIsUntradedByEncounterMemories(PKM pkm, IEncounterable EncounterMatch, int generation)
{
if (generation < 6)
return false;
if (EncounterMatch is EncounterLink link && !link.OT)
return false;
bool untraded = pkm.HT_Name.Length == 0 || (pkm is IGeoTrack g && g.Geo1_Country == 0);
2018-07-12 02:13:09 +00:00
if (!(EncounterMatch is MysteryGift gift))
return untraded;
untraded |= !pkm.WasEventEgg;
untraded &= gift.IsEgg;
return untraded;
}
}
}