2019-05-11 21:25:58 +00:00
|
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2018-07-02 02:17:37 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies the <see cref="PKM.EncryptionConstant"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class PIDVerifier : Verifier
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
|
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.PID;
|
|
|
|
|
|
|
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
if (pkm.Format >= 6)
|
|
|
|
|
VerifyEC(data);
|
|
|
|
|
|
|
|
|
|
var EncounterMatch = data.EncounterMatch;
|
2019-12-09 01:39:19 +00:00
|
|
|
|
if (EncounterMatch.Species == (int)Species.Wurmple)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
VerifyECPIDWurmple(data);
|
|
|
|
|
|
|
|
|
|
if (pkm.PID == 0)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(Get(LPIDZero, Severity.Fishy));
|
2018-12-26 06:59:52 +00:00
|
|
|
|
if (pkm.Nature >= 25) // out of range
|
|
|
|
|
data.AddLine(GetInvalid(LPIDNatureMismatch));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
|
|
|
|
VerifyShiny(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyShiny(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
|
|
|
|
|
switch (data.EncounterMatch)
|
|
|
|
|
{
|
|
|
|
|
case EncounterStatic s:
|
|
|
|
|
if (!s.Shiny.IsValid(pkm))
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LEncStaticPIDShiny, CheckIdentifier.Shiny));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2020-10-24 18:06:05 +00:00
|
|
|
|
if (s is EncounterStatic8U u && u.Shiny == Shiny.Random)
|
|
|
|
|
{
|
|
|
|
|
// Underground Raids are originally anti-shiny on encounter.
|
|
|
|
|
// When selecting a prize at the end, the game rolls and force-shiny is applied to be XOR=1.
|
|
|
|
|
var xor = pkm.ShinyXor;
|
|
|
|
|
if (xor <= 15 && xor != 1)
|
|
|
|
|
data.AddLine(GetInvalid(LEncStaticPIDShiny, CheckIdentifier.Shiny));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (s.Generation != 5)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
break;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
|
|
|
|
|
// Generation 5 has a correlation for wild captures.
|
|
|
|
|
// Certain static encounter types are just generated straightforwardly.
|
2018-06-24 05:00:01 +00:00
|
|
|
|
if (s.Location == 75) // Entree Forest
|
|
|
|
|
break;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
|
|
|
|
|
// Not wild / forced ability
|
|
|
|
|
if (s.Gift || s.Ability == 4)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
break;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
|
|
|
|
|
// Forced PID or generated without an encounter
|
2020-10-31 18:43:17 +00:00
|
|
|
|
// Crustle has 0x80 for its StartWildBattle flag; dunno what it does, but sometimes it doesn't align with the expected PID xor.
|
|
|
|
|
if (s is EncounterStatic5 s5 && (s5.Roaming || s5.Shiny != Shiny.Random || s5.Species == (int)Species.Crustle))
|
2018-06-24 05:00:01 +00:00
|
|
|
|
break;
|
|
|
|
|
VerifyG5PID_IDCorrelation(data);
|
|
|
|
|
break;
|
|
|
|
|
|
2020-11-04 02:56:58 +00:00
|
|
|
|
case EncounterSlot5 w:
|
|
|
|
|
if (w.Area.Type == SlotType.HiddenGrotto && pkm.IsShiny)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LG5PIDShinyGrotto, CheckIdentifier.Shiny));
|
2020-11-04 02:56:58 +00:00
|
|
|
|
if (w.Area.Type != SlotType.HiddenGrotto)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
VerifyG5PID_IDCorrelation(data);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PCD d: // fixed PID
|
|
|
|
|
if (d.Gift.PK.PID != 1 && pkm.EncryptionConstant != d.Gift.PK.PID)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LEncGiftPIDMismatch, CheckIdentifier.Shiny));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
break;
|
2019-09-10 07:21:51 +00:00
|
|
|
|
|
2019-05-11 20:10:51 +00:00
|
|
|
|
case WC7 wc7 when wc7.IsAshGreninjaWC7(pkm) && pkm.IsShiny:
|
|
|
|
|
data.AddLine(GetInvalid(LEncGiftShinyMismatch, CheckIdentifier.Shiny));
|
2019-05-11 20:08:22 +00:00
|
|
|
|
break;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyG5PID_IDCorrelation(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var pid = pkm.EncryptionConstant;
|
|
|
|
|
var result = (pid & 1) ^ (pid >> 31) ^ (pkm.TID & 1) ^ (pkm.SID & 1);
|
|
|
|
|
if (result != 0)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LPIDTypeMismatch));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-06-24 05:00:01 +00:00
|
|
|
|
private void VerifyECPIDWurmple(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
|
2019-12-09 01:39:19 +00:00
|
|
|
|
if (pkm.Species == (int)Species.Wurmple)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
2019-05-11 21:25:58 +00:00
|
|
|
|
// Indicate what it will evolve into
|
|
|
|
|
uint evoVal = WurmpleUtil.GetWurmpleEvoVal(pkm.EncryptionConstant);
|
2020-08-21 23:35:49 +00:00
|
|
|
|
var evolvesTo = evoVal == 0 ? (int)Species.Beautifly : (int)Species.Dustox;
|
|
|
|
|
var spec = LegalityAnalysis.SpeciesStrings[evolvesTo];
|
2018-09-01 21:11:12 +00:00
|
|
|
|
var msg = string.Format(L_XWurmpleEvo_0, spec);
|
2018-06-24 05:00:01 +00:00
|
|
|
|
data.AddLine(GetValid(msg, CheckIdentifier.EC));
|
|
|
|
|
}
|
2019-05-11 21:25:58 +00:00
|
|
|
|
else if (!WurmpleUtil.IsWurmpleEvoValid(pkm))
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LPIDEncryptWurmple, CheckIdentifier.EC));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2020-09-09 19:47:24 +00:00
|
|
|
|
private static void VerifyEC(LegalityAnalysis data)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var Info = data.Info;
|
|
|
|
|
|
|
|
|
|
if (pkm.EncryptionConstant == 0)
|
2020-08-21 23:35:49 +00:00
|
|
|
|
{
|
|
|
|
|
if (Info.EncounterMatch is WC8 wc8 && wc8.PID == 0 && wc8.EncryptionConstant == 0)
|
|
|
|
|
return; // HOME Gifts
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(Get(LPIDEncryptZero, Severity.Fishy, CheckIdentifier.EC));
|
2020-08-21 23:35:49 +00:00
|
|
|
|
}
|
2019-09-10 07:21:51 +00:00
|
|
|
|
|
2020-08-21 23:35:49 +00:00
|
|
|
|
// Gen3-5 => Gen6 have PID==EC with an edge case exception.
|
2018-06-24 05:00:01 +00:00
|
|
|
|
if (3 <= Info.Generation && Info.Generation <= 5)
|
2018-07-27 02:34:27 +00:00
|
|
|
|
{
|
2018-06-24 05:00:01 +00:00
|
|
|
|
VerifyTransferEC(data);
|
2020-08-21 23:35:49 +00:00
|
|
|
|
return;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
}
|
2020-08-21 23:35:49 +00:00
|
|
|
|
|
|
|
|
|
// Gen1-2, Gen6+ should have PID != EC
|
|
|
|
|
if (pkm.PID == pkm.EncryptionConstant)
|
|
|
|
|
{
|
2020-09-09 19:47:24 +00:00
|
|
|
|
data.AddLine(GetInvalid(LPIDEqualsEC, CheckIdentifier.EC)); // better to flag than 1:2^32 odds since RNG is not feasible to yield match
|
2020-08-21 23:35:49 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for Gen3-5 => Gen6 edge case being incorrectly applied here.
|
|
|
|
|
if ((pkm.PID ^ 0x80000000) == pkm.EncryptionConstant)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
|
|
|
|
int xor = pkm.TSV ^ pkm.PSV;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
if (xor >> 3 == 1) // 8 <= x <= 15
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(Get(LTransferPIDECXor, Severity.Fishy, CheckIdentifier.EC));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2020-09-09 19:47:24 +00:00
|
|
|
|
private static void VerifyTransferEC(LegalityAnalysis data)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
// When transferred to Generation 6, the Encryption Constant is copied from the PID.
|
|
|
|
|
// The PID is then checked to see if it becomes shiny with the new Shiny rules (>>4 instead of >>3)
|
|
|
|
|
// If the PID is nonshiny->shiny, the top bit is flipped.
|
|
|
|
|
|
|
|
|
|
// Check to see if the PID and EC are properly configured.
|
2020-03-12 03:09:09 +00:00
|
|
|
|
var ec = pkm.EncryptionConstant; // should be original PID
|
|
|
|
|
bool xorPID = ((pkm.TID ^ pkm.SID ^ (int)(ec & 0xFFFF) ^ (int)(ec >> 16)) & ~0x7) == 8;
|
|
|
|
|
bool valid = pkm.PID == (xorPID ? (ec ^ 0x80000000) : ec);
|
2018-06-24 05:00:01 +00:00
|
|
|
|
if (valid)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-09-01 21:11:12 +00:00
|
|
|
|
var msg = xorPID ? LTransferPIDECBitFlip : LTransferPIDECEquals;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
data.AddLine(GetInvalid(msg, CheckIdentifier.EC));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|