PKHeX/PKHeX.Core/Legality/Verifiers/PIDVerifier.cs

159 lines
6.1 KiB
C#
Raw Normal View History

using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.Core
{
2018-07-02 02:17:37 +00:00
/// <summary>
/// Verifies the <see cref="PKM.EncryptionConstant"/>.
/// </summary>
public sealed class PIDVerifier : Verifier
{
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;
if (EncounterMatch.Species == (int)Species.Wurmple)
VerifyECPIDWurmple(data);
if (pkm.PID == 0)
data.AddLine(Get(LPIDZero, Severity.Fishy));
if (pkm.Nature >= 25) // out of range
data.AddLine(GetInvalid(LPIDNatureMismatch));
VerifyShiny(data);
}
private void VerifyShiny(LegalityAnalysis data)
{
var pkm = data.pkm;
var Info = data.Info;
switch (data.EncounterMatch)
{
case EncounterStatic s:
if (!s.Shiny.IsValid(pkm))
data.AddLine(GetInvalid(LEncStaticPIDShiny, CheckIdentifier.Shiny));
if (s.Generation != 5)
break;
// Generation 5 has a correlation for wild captures.
// Certain static encounter types are just generated straightforwardly.
if (s.Location == 75) // Entree Forest
break;
// Not wild / forced ability
if (s.Gift || s.Ability == 4)
break;
// Forced PID or generated without an encounter
if (s is EncounterStatic5N || (s is EncounterStatic5 s5 && s5.Roaming) || s.Shiny == Shiny.Never)
break;
VerifyG5PID_IDCorrelation(data);
break;
case EncounterSlot w:
Offload EncounterSlot loading logic to reduce complexity (#2980) * Rework gen1 slot loading Slot templates are precomputed from ROM data and just loaded straight in, with tight coupling to the encounter area (grouped by slot types). * Revise fuzzy met check for underleveled wild evos Example: Level 23 poliwhirl in RBY as a level 50 poliwhirl, will assume the chain is 25-50 for poliwhirl (as poliwag evolves at 25). Instead of revising the origin chain, just ignore the evo min level in the comparison. Previous commit fixed it for gen1. * Rework gen2-4 slot loading Gen4 not finished, Type Encounter data and some edge encounters not recognizing yet... * Add feebas slots for old/good encounters * Begin moving properties Great news! Gen5-7 need to be de-dumbed like Gen1-4. Then I can remove the bang (!) on the Area accessor and ensure that it's never null! * Split off XD pokespot slot encounter table type * Set area in constructor * Deduplicate g3 roaming encounters * Deduplicate xd encounter locations (rebattle) Only difference is met location; no need to create 500 extra encounter objects. A simple contains check is ok (rarely in gen3 format). * Make all slots have a readonly reference to their parent area * Minor clean * Remove "Safari" slot type flag Can be determined via other means (generation-location), allows us to reduce the size of SlotType member to a byte Output of slot binaries didn't preserve the Safari flag anyway. * Update SlotType.cs * Handle type encounters correctly * Merge safari area into regular xy area * Merge dexnav accessor logic * fix some logic so that tests pass again rearrange g5 dw init to be done outside of static constructor (initializer instead) PIDGenerator: friend safari slots now generate with required flawless IV count * Add cianwood tentacool gift encounter * Remove unnecessary abstractions Fake area just returned a slot; since Slots have a non-null reference to the area, we can just return the slot and use the API to grab a list of possible slots for the chain. Increase restrictiveness of location/type get-set operations * Minor tweaks, pass parameters DexNav observed state isn't necessary to use, only need to see if it's possible to dexnav. Now that we have metadata for slots, we can. * Remove unused legality tables
2020-08-30 17:23:22 +00:00
if (pkm.IsShiny && w.Area.Type == SlotType.HiddenGrotto)
data.AddLine(GetInvalid(LG5PIDShinyGrotto, CheckIdentifier.Shiny));
Offload EncounterSlot loading logic to reduce complexity (#2980) * Rework gen1 slot loading Slot templates are precomputed from ROM data and just loaded straight in, with tight coupling to the encounter area (grouped by slot types). * Revise fuzzy met check for underleveled wild evos Example: Level 23 poliwhirl in RBY as a level 50 poliwhirl, will assume the chain is 25-50 for poliwhirl (as poliwag evolves at 25). Instead of revising the origin chain, just ignore the evo min level in the comparison. Previous commit fixed it for gen1. * Rework gen2-4 slot loading Gen4 not finished, Type Encounter data and some edge encounters not recognizing yet... * Add feebas slots for old/good encounters * Begin moving properties Great news! Gen5-7 need to be de-dumbed like Gen1-4. Then I can remove the bang (!) on the Area accessor and ensure that it's never null! * Split off XD pokespot slot encounter table type * Set area in constructor * Deduplicate g3 roaming encounters * Deduplicate xd encounter locations (rebattle) Only difference is met location; no need to create 500 extra encounter objects. A simple contains check is ok (rarely in gen3 format). * Make all slots have a readonly reference to their parent area * Minor clean * Remove "Safari" slot type flag Can be determined via other means (generation-location), allows us to reduce the size of SlotType member to a byte Output of slot binaries didn't preserve the Safari flag anyway. * Update SlotType.cs * Handle type encounters correctly * Merge safari area into regular xy area * Merge dexnav accessor logic * fix some logic so that tests pass again rearrange g5 dw init to be done outside of static constructor (initializer instead) PIDGenerator: friend safari slots now generate with required flawless IV count * Add cianwood tentacool gift encounter * Remove unnecessary abstractions Fake area just returned a slot; since Slots have a non-null reference to the area, we can just return the slot and use the API to grab a list of possible slots for the chain. Increase restrictiveness of location/type get-set operations * Minor tweaks, pass parameters DexNav observed state isn't necessary to use, only need to see if it's possible to dexnav. Now that we have metadata for slots, we can. * Remove unused legality tables
2020-08-30 17:23:22 +00:00
if (Info.Generation == 5 && w.Area.Type != SlotType.HiddenGrotto)
VerifyG5PID_IDCorrelation(data);
break;
case PCD d: // fixed PID
if (d.Gift.PK.PID != 1 && pkm.EncryptionConstant != d.Gift.PK.PID)
data.AddLine(GetInvalid(LEncGiftPIDMismatch, CheckIdentifier.Shiny));
break;
2019-05-11 20:10:51 +00:00
case WC7 wc7 when wc7.IsAshGreninjaWC7(pkm) && pkm.IsShiny:
data.AddLine(GetInvalid(LEncGiftShinyMismatch, CheckIdentifier.Shiny));
break;
}
}
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)
data.AddLine(GetInvalid(LPIDTypeMismatch));
}
private void VerifyECPIDWurmple(LegalityAnalysis data)
{
var pkm = data.pkm;
if (pkm.Species == (int)Species.Wurmple)
{
// Indicate what it will evolve into
uint evoVal = WurmpleUtil.GetWurmpleEvoVal(pkm.EncryptionConstant);
var evolvesTo = evoVal == 0 ? (int)Species.Beautifly : (int)Species.Dustox;
var spec = LegalityAnalysis.SpeciesStrings[evolvesTo];
var msg = string.Format(L_XWurmpleEvo_0, spec);
data.AddLine(GetValid(msg, CheckIdentifier.EC));
}
else if (!WurmpleUtil.IsWurmpleEvoValid(pkm))
{
data.AddLine(GetInvalid(LPIDEncryptWurmple, CheckIdentifier.EC));
}
}
private void VerifyEC(LegalityAnalysis data)
{
var pkm = data.pkm;
var Info = data.Info;
if (pkm.EncryptionConstant == 0)
{
if (Info.EncounterMatch is WC8 wc8 && wc8.PID == 0 && wc8.EncryptionConstant == 0)
return; // HOME Gifts
data.AddLine(Get(LPIDEncryptZero, Severity.Fishy, CheckIdentifier.EC));
}
// Gen3-5 => Gen6 have PID==EC with an edge case exception.
if (3 <= Info.Generation && Info.Generation <= 5)
{
VerifyTransferEC(data);
return;
}
// Gen1-2, Gen6+ should have PID != EC
if (pkm.PID == pkm.EncryptionConstant)
{
data.AddLine(GetInvalid(LPIDEqualsEC)); // better to flag than 1:2^32 odds since RNG is not feasible to yield match
return;
}
// Check for Gen3-5 => Gen6 edge case being incorrectly applied here.
if ((pkm.PID ^ 0x80000000) == pkm.EncryptionConstant)
{
int xor = pkm.TSV ^ pkm.PSV;
if (xor >> 3 == 1) // 8 <= x <= 15
data.AddLine(Get(LTransferPIDECXor, Severity.Fishy, CheckIdentifier.EC));
}
}
private void VerifyTransferEC(LegalityAnalysis data)
{
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.
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);
if (valid)
return;
var msg = xorPID ? LTransferPIDECBitFlip : LTransferPIDECEquals;
data.AddLine(GetInvalid(msg, CheckIdentifier.EC));
}
}
}