From 46a4e0c6c912ca22dbe2bdfc87085fbf0022396d Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 23 Aug 2026 09:28:03 -0500 Subject: [PATCH] update LCRNG reversal lattice consts https: //github.com/kwsch/PKHeX/issues/4844#issuecomment-5355902735 Co-Authored-By: StarfBerry <65578604+StarfBerry@users.noreply.github.com> --- .../Legality/RNG/Algorithms/LCRNGReversal.cs | 20 +++++++++---------- .../RNG/Algorithms/LCRNGReversalSkip.cs | 15 +++++++------- .../Legality/RNG/Algorithms/MRNGReversal.cs | 4 ++-- PKHeX.Core/Legality/RNG/Algorithms/XDRNG.cs | 10 +++++----- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversal.cs b/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversal.cs index cbe106d11..758828eac 100644 --- a/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversal.cs +++ b/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversal.cs @@ -13,14 +13,14 @@ public static class LCRNGReversal // https://github.com/StarfBerry/PokeRNG/blob/main/Recovery/LCG_Recovery.py private const uint RMult = LCRNG.rMult; // reverse multiplier constant private const uint RLag0 = 0x7ED7; // 32471 - private const uint RLag1 = 0x71A4; // -68321 mod 32471 - private const uint RLower = 0x79C8BF4A; // ((-0x50F40B53C37 + 0xffff_ffff) >> 16) + (32471 << 16) - private const uint RUpper = 0x79C8A5F4; // (-0x50E5A0B3C37 >> 16) + (32471 << 16) + private const uint RLag1 = 0xD33; // 68321 mod 32471 + private const uint RLower = 0x50F5A0B; // (0x50E5A0BBB0E + 0xffff_ffff) >> 16 + private const uint RUpper = 0x50F40B4; // (0x50F40B4D464 >> 16) - private const uint Lag0 = 0x6134; // -26579 mod 51463 + private const uint Lag0 = 0x67D3; // 26579 private const uint Lag1 = 0xC907; // 51463 - private const uint Lower = 0x64833CB0; // ((-0xC34F11DB + 0x7fff_ffff) >> 16) + (51463 << 15) - private const uint Upper = 0x6483CBBC; // (0x4BBCEE25 >> 16) + (51463 << 15) + private const uint Lower = 0x3443; // (-0x4BBCEE25 + 0x7fff_ffff) >> 16 + private const uint Upper = 0xC34E; // (0xC34E02CF >> 16) /// /// Finds all seeds that can generate the IVs. @@ -49,7 +49,7 @@ public static int GetSeedsIVs(Span result, uint hp, uint atk, uint def, ui /// Count of results added to public static int GetSeeds(Span result, uint first, uint second) { - uint tmp = ((first - (second * RMult)) >> 16) * RLag0; + uint tmp = ((second * RMult - first) >> 16) * RLag0; uint lo = (tmp + RLower) >> 16; uint up = (tmp + RUpper) >> 16; if (lo != up) // true in around 10% of cases @@ -76,10 +76,10 @@ public static int GetSeeds(Span result, uint first, uint second) /// Count of results added to public static int GetSeedsIVs(Span result, uint first, uint second) { - ulong tmp = (ulong)(((LCRNG.Mult * first) - second >> 16) & 0xFFFF) * Lag1; - var lo = (uint)(((tmp + Lower) >> 15) * Lag0); + var tmp = ((second - LCRNG.Mult * first) >> 16) * Lag1; + var lo = ((tmp + Lower) >> 15) * Lag0; var mi = lo + Lag0; - var up = (uint)(((tmp + Upper) >> 15) * Lag0); + var up = ((tmp + Upper) >> 15) * Lag0; int ctr = 0; // around 2.70 iterations in average diff --git a/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversalSkip.cs b/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversalSkip.cs index 331790e38..45bf55ea3 100644 --- a/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversalSkip.cs +++ b/PKHeX.Core/Legality/RNG/Algorithms/LCRNGReversalSkip.cs @@ -13,11 +13,12 @@ public static class LCRNGReversalSkip // https://github.com/StarfBerry/PokeRNG/blob/main/Recovery/LCG_Recovery.py private const uint RMult2 = 0xDC6C95D9; // reverse multiplier constant private const uint RLag0 = 0x6C31; // 27697 - private const uint RLag1PID = 0x5D20; // -59251 mod 27697 + private const uint RLag1PID = 0xF11; // 59251 mod 27697 private const uint RLag1IVs = 0x2E90; // -43474 mod 27697 - private const uint RLowerPID = 0x4B8D621D; // ((-0x20A49DE2F046 + 0xffff_ffff) >> 16) + (27697 << 16) - private const uint RLowerIVs = 0x4B8CE21D; // ((-0x20A49DE2F046 + 0x7fff_ffff) >> 16) + (27697 << 16) - private const uint RUpper = 0x4B8D08D7; // (-0x20A3F728F046 >> 16) + (27697 << 16) + private const uint RLowerPID = 0x20A4F729; // (0x20A3F7295C77 + 0xffff_ffff) >> 16 + private const uint RUpperPID = 0x20A49DE2; // (0x20A49DE2B5BD >> 16) + private const uint RLowerIVs = 0x1574621D; // ((-0x20A49DE2B5BD + 0x7fff_ffff) >> 16) + (27697 << 15) + private const uint RUpperIVs = 0x157488D6; // (-0x20A3F7295C77 >> 16) + (27697 << 15) /// /// Finds all seeds that can generate the IVs, with a vblank skip between the two IV rand() calls. @@ -46,9 +47,9 @@ public static int GetSeedsIVs(Span result, uint hp, uint atk, uint def, ui /// Count of results added to public static int GetSeeds(Span result, uint first, uint third) { - uint tmp = ((first - (third * RMult2)) >> 16) * RLag0; + uint tmp = (((third * RMult2) - first) >> 16) * RLag0; uint lo = (tmp + RLowerPID) >> 16; - uint up = (tmp + RUpper) >> 16; + uint up = (tmp + RUpperPID) >> 16; if (lo != up) // true in around 35% of cases return 0; @@ -75,7 +76,7 @@ public static int GetSeedsIVs(Span result, uint first, uint third) { uint tmp = ((first - (third * RMult2)) >> 16) * RLag0; uint lo = (tmp + RLowerIVs) >> 15; - uint up = (tmp + RUpper) >> 15; + uint up = (tmp + RUpperIVs) >> 15; int ctr = 0; AddSeeds(result, (lo * RLag1IVs) % RLag0, first, third, ref ctr); diff --git a/PKHeX.Core/Legality/RNG/Algorithms/MRNGReversal.cs b/PKHeX.Core/Legality/RNG/Algorithms/MRNGReversal.cs index 0e205ef88..cb3ef1aab 100644 --- a/PKHeX.Core/Legality/RNG/Algorithms/MRNGReversal.cs +++ b/PKHeX.Core/Legality/RNG/Algorithms/MRNGReversal.cs @@ -11,8 +11,8 @@ public static class MRNGReversal private const uint RMult2 = 0xDC6C95D9; // reverse multiplier constant private const uint RLag0 = 0x6C31; // 27697 private const uint RLag1IVs = 0x2E90; // -43474 mod 27697 - private const uint RLower = 0x670A0357; // ((-0x5277CA86A92 + 0x7fff_ffff) >> 16) + (27697 << 16) - private const uint RUpper = 0x670A2A11; // (-0x526D5EE6A92 >> 16) + (27697 << 16) + private const uint RLower = 0x30F18357; // ((-0x5277CA86A92 + 0x7fff_ffff) >> 16) + (27697 << 15) + private const uint RUpper = 0x30F1AA11; // (-0x526D5EE6A92 >> 16) + (27697 << 15) /// /// Finds all Ranch RNG states that can generate the IVs. diff --git a/PKHeX.Core/Legality/RNG/Algorithms/XDRNG.cs b/PKHeX.Core/Legality/RNG/Algorithms/XDRNG.cs index 61853033a..335db2018 100644 --- a/PKHeX.Core/Legality/RNG/Algorithms/XDRNG.cs +++ b/PKHeX.Core/Legality/RNG/Algorithms/XDRNG.cs @@ -243,13 +243,13 @@ public static int GetSeeds(Span result, uint hp, uint atk, uint def, uint // https://github.com/StarfBerry/PokeRNG/blob/1e9b9ddf2494837c7d6704c7b8a3831f644bdea9/Recovery/LCG_Recovery.py#L229 private const uint Lag0 = 0xE8D1; // 59601 private const uint Lag1 = 0x5F47; // -35210 mod 59601 - private const uint RLower = 0x55FF8537; // ((-0x92D27AC8F311 + 0xffff_ffff) >> 16) + (59601 << 16) - private const uint RUpper = 0x55FFBC6D; // (-0x92D14392F311 >> 16) + (59601 << 16) + private const uint RLower = 0x55FF8537; // ((-0x92D27AC8A4AC + 0xffff_ffff) >> 16) + (59601 << 16) + private const uint RUpper = 0x55FFBC6C; // (-0x92D14393DBE2 >> 16) + (59601 << 16) private const uint Lag0IVs = 0x44C5; // 17605 private const uint Lag1IVs = 0xE8D1; // 59601 - private const uint RLowerIVs = 0x1E694392; // (0x1E68C392F311 + 0x7fff_ffff) >> 16 - private const uint RUpperIVs = 0x1E69FAC8; // (0x1E69FAC8F311 >> 16) + private const uint RLowerIVs = 0x1E694393; // (0x1E68C393DBE2 + 0x7fff_ffff) >> 16 + private const uint RUpperIVs = 0x1E69FAC8; // (0x1E69FAC8A4AC >> 16) /// /// Finds all the origin seeds for two 16 bit rand() calls @@ -299,7 +299,7 @@ public static int GetSeeds(Span result, uint first, uint second) /// Count of results added to public static int GetSeedsIVs(Span result, uint first, uint second) { - ulong tmp = ((((rMult * second) - first) >> 16) & 0xFFFF) * Lag1IVs; + ulong tmp = (((rMult * second) - first) >> 16) * Lag1IVs; var lo = (uint)((tmp + RLowerIVs) >> 15) * Lag0IVs; var mi = lo + Lag0IVs;