2022-07-01 00:23:27 +00:00
|
|
|
using System;
|
2017-08-29 15:32:22 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2021-01-28 00:52:04 +00:00
|
|
|
using static PKHeX.Core.PIDType;
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class containing logic to obtain a PKM's PIDIV method.
|
|
|
|
/// </summary>
|
|
|
|
public static class MethodFinder
|
2017-04-29 23:22:32 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Analyzes a <see cref="PKM"/> to find a matching PIDIV method.
|
2017-04-29 23:22:32 +00:00
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="pk">Input <see cref="PKM"/>.</param>
|
|
|
|
/// <returns><see cref="PIDIV"/> object containing seed and method info.</returns>
|
|
|
|
public static PIDIV Analyze(PKM pk)
|
|
|
|
{
|
|
|
|
if (pk.Format < 3)
|
|
|
|
return AnalyzeGB(pk);
|
|
|
|
var pid = pk.EncryptionConstant;
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var top = pid & 0xFFFF0000;
|
|
|
|
var bot = pid << 16;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
Span<uint> temp = stackalloc uint[6];
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
temp[i] = (uint)pk.GetIV(i);
|
|
|
|
ReadOnlySpan<uint> IVs = temp;
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
// Between XDRNG and LCRNG, the LCRNG will have the most results.
|
|
|
|
// Reuse our temp buffer across all methods.
|
|
|
|
const int maxResults = LCRNG.MaxCountSeedsIV;
|
|
|
|
Span<uint> seeds = stackalloc uint[maxResults];
|
|
|
|
|
|
|
|
if (GetLCRNGMatch(seeds, top, bot, IVs, out PIDIV pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
2022-09-04 19:03:37 +00:00
|
|
|
if (pk.Species == (int)Species.Unown && GetLCRNGUnownMatch(seeds, top, bot, IVs, out pidiv)) // frlg only
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetColoStarterMatch(seeds, pk, top, bot, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetXDRNGMatch(seeds, pk, top, bot, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
|
|
|
|
|
|
|
// Special cases
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetLCRNGRoamerMatch(seeds, top, bot, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetChannelMatch(seeds, top, bot, IVs, out pidiv, pk))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetMG4Match(seeds, pid, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetBACDMatch(seeds, pk, pid, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetModifiedPIDMatch(seeds, pk, pid, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return pidiv;
|
|
|
|
|
|
|
|
return PIDIV.None; // no match
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetModifiedPIDMatch(Span<uint> seeds, PKM pk, uint pid, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (pk.IsShiny)
|
2017-08-29 15:32:22 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
if (GetChainShinyMatch(seeds, pk, pid, IVs, out pidiv))
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
if (GetModified8BitMatch(pk, pid, out pidiv))
|
|
|
|
return true;
|
2017-08-29 15:32:22 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2017-08-29 15:32:22 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pid <= 0xFF && GetCuteCharmMatch(pk, pid, out pidiv))
|
|
|
|
return true;
|
2017-08-29 15:32:22 +00:00
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetPokewalkerMatch(pk, pid, out pidiv);
|
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool GetModified8BitMatch(PKM pk, uint pid, out PIDIV pidiv)
|
|
|
|
{
|
|
|
|
return pk.Gen4
|
|
|
|
? (pid <= 0xFF && GetCuteCharmMatch(pk, pid, out pidiv)) || GetG5MGShinyMatch(pk, pid, out pidiv)
|
|
|
|
: GetG5MGShinyMatch(pk, pid, out pidiv) || (pid <= 0xFF && GetCuteCharmMatch(pk, pid, out pidiv));
|
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetLCRNGMatch(Span<uint> seeds, uint top, uint bot, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var count = LCRNGReversal.GetSeeds(seeds, bot, top);
|
|
|
|
var reg = seeds[..count];
|
2023-04-23 00:51:32 +00:00
|
|
|
var iv1 = GetIVChunk(IVs[..3]);
|
|
|
|
var iv2 = GetIVChunk(IVs[3..]);
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in reg)
|
|
|
|
{
|
|
|
|
// A and B are already used by PID
|
2022-09-04 19:03:37 +00:00
|
|
|
var B = LCRNG.Next2(seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Method 1/2/4 can use 3 different RNG frames
|
2022-09-04 19:03:37 +00:00
|
|
|
var C = LCRNG.Next(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivC = C >> 16 & 0x7FFF;
|
|
|
|
if (iv1 == ivC)
|
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivD = D >> 16 & 0x7FFF;
|
|
|
|
if (iv2 == ivD) // ABCD
|
2017-04-29 23:22:32 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pidiv = new PIDIV(Method_1, seed);
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-02 06:41:19 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = LCRNG.Next(D);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivE = E >> 16 & 0x7FFF;
|
|
|
|
if (iv2 == ivE) // ABCE
|
|
|
|
{
|
|
|
|
pidiv = new PIDIV(Method_4, seed);
|
|
|
|
return true;
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2019-06-20 02:15:06 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2019-06-20 02:15:06 +00:00
|
|
|
var ivD = D >> 16 & 0x7FFF;
|
|
|
|
if (iv1 != ivD)
|
|
|
|
continue;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = LCRNG.Next(D);
|
2019-06-20 02:15:06 +00:00
|
|
|
var ivE = E >> 16 & 0x7FFF;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (iv2 == ivE) // ABDE
|
|
|
|
{
|
|
|
|
pidiv = new PIDIV(Method_2, seed);
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-20 02:15:06 +00:00
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-09-04 19:03:37 +00:00
|
|
|
count = LCRNGReversalSkip.GetSeeds(seeds, bot, top);
|
|
|
|
reg = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in reg)
|
|
|
|
{
|
|
|
|
// A and B are already used by PID
|
2022-09-04 19:03:37 +00:00
|
|
|
var C = LCRNG.Next3(seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Method 3
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivD = D >> 16 & 0x7FFF;
|
|
|
|
if (iv1 != ivD)
|
|
|
|
continue;
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = LCRNG.Next(D);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivE = E >> 16 & 0x7FFF;
|
|
|
|
if (iv2 != ivE)
|
|
|
|
continue;
|
|
|
|
pidiv = new PIDIV(Method_3, seed);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetLCRNGUnownMatch(Span<uint> seeds, uint top, uint bot, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// this is an exact copy of LCRNG 1,2,4 matching, except the PID has its halves switched (BACD, BADE, BACE)
|
2022-09-04 19:03:37 +00:00
|
|
|
var count = LCRNGReversal.GetSeeds(seeds, top, bot); // reversed!
|
|
|
|
var reg = seeds[..count];
|
2023-04-23 00:51:32 +00:00
|
|
|
var iv1 = GetIVChunk(IVs[..3]);
|
|
|
|
var iv2 = GetIVChunk(IVs[3..]);
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in reg)
|
|
|
|
{
|
|
|
|
// A and B are already used by PID
|
2022-09-04 19:03:37 +00:00
|
|
|
var B = LCRNG.Next2(seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Method 1/2/4 can use 3 different RNG frames
|
2022-09-04 19:03:37 +00:00
|
|
|
var C = LCRNG.Next(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivC = C >> 16 & 0x7FFF;
|
|
|
|
if (iv1 == ivC)
|
2017-05-01 15:37:23 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivD = D >> 16 & 0x7FFF;
|
|
|
|
if (iv2 == ivD) // BACD
|
2017-05-01 15:37:23 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pidiv = new PIDIV(Method_1_Unown, seed);
|
|
|
|
return true;
|
2017-05-01 15:37:23 +00:00
|
|
|
}
|
2017-05-02 06:41:19 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = LCRNG.Next(D);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivE = E >> 16 & 0x7FFF;
|
|
|
|
if (iv2 == ivE) // BACE
|
|
|
|
{
|
|
|
|
pidiv = new PIDIV(Method_4_Unown, seed);
|
|
|
|
return true;
|
2017-05-01 15:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2019-06-20 02:15:06 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2019-06-20 02:15:06 +00:00
|
|
|
var ivD = D >> 16 & 0x7FFF;
|
|
|
|
if (iv1 != ivD)
|
|
|
|
continue;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = LCRNG.Next(D);
|
2019-06-20 02:15:06 +00:00
|
|
|
var ivE = E >> 16 & 0x7FFF;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (iv2 == ivE) // BADE
|
|
|
|
{
|
|
|
|
pidiv = new PIDIV(Method_2_Unown, seed);
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-20 02:15:06 +00:00
|
|
|
}
|
2017-05-01 15:37:23 +00:00
|
|
|
}
|
2022-09-04 19:03:37 +00:00
|
|
|
count = LCRNGReversalSkip.GetSeeds(seeds, top, bot); // reversed!
|
|
|
|
reg = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in reg)
|
|
|
|
{
|
|
|
|
// A and B are already used by PID
|
2022-09-04 19:03:37 +00:00
|
|
|
var C = LCRNG.Next3(seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Method 3
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivD = D >> 16 & 0x7FFF;
|
|
|
|
if (iv1 != ivD)
|
|
|
|
continue;
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = LCRNG.Next(D);
|
2022-06-18 18:04:24 +00:00
|
|
|
var ivE = E >> 16 & 0x7FFF;
|
|
|
|
if (iv2 != ivE)
|
|
|
|
continue;
|
|
|
|
pidiv = new PIDIV(Method_3_Unown, seed);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetLCRNGRoamerMatch(Span<uint> seeds, uint top, uint bot, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-06-04 01:19:16 +00:00
|
|
|
if (IVs is not [_, <= 7, 0, 0, 0, 0])
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
2022-09-04 19:03:37 +00:00
|
|
|
|
2023-04-23 00:51:32 +00:00
|
|
|
var iv1 = GetIVChunk(IVs[..3]);
|
2022-09-04 19:03:37 +00:00
|
|
|
var count = LCRNGReversal.GetSeeds(seeds, bot, top);
|
|
|
|
var reg = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in reg)
|
2017-05-02 06:41:19 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Only the first 8 bits are kept
|
2022-09-04 19:03:37 +00:00
|
|
|
var ivC = LCRNG.Next3(seed) >> 16 & 0x00FF;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (iv1 != ivC)
|
|
|
|
continue;
|
2017-05-02 06:41:19 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pidiv = new PIDIV(Method_1_Roamer, seed);
|
|
|
|
return true;
|
2017-05-02 06:41:19 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetXDRNGMatch(Span<uint> seeds, PKM pk, uint top, uint bot, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var count = XDRNG.GetSeeds(seeds, top, bot);
|
|
|
|
var xdc = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in xdc)
|
2017-04-29 23:22:32 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var B = XDRNG.Prev(seed);
|
|
|
|
var A = XDRNG.Prev(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
var hi = A >> 16;
|
|
|
|
var lo = B >> 16;
|
|
|
|
if (IVsMatch(hi, lo, IVs))
|
2017-04-29 23:22:32 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
pidiv = new PIDIV(CXD, XDRNG.Prev(A));
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
// Check for anti-shiny against player TSV
|
2023-01-22 04:02:33 +00:00
|
|
|
var tsv = (uint)(pk.TID16 ^ pk.SID16) >> 3;
|
2022-06-18 18:04:24 +00:00
|
|
|
var psv = (top ^ bot) >> 3;
|
2023-12-04 04:13:20 +00:00
|
|
|
if (psv == tsv) // Already shiny, wouldn't be made anti-shiny
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var p2 = seed;
|
|
|
|
var p1 = B;
|
|
|
|
psv = ((p2 ^ p1) >> 19);
|
2023-12-04 04:13:20 +00:00
|
|
|
if (psv != tsv) // The prior PID must be shiny!
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
B = XDRNG.Prev(A);
|
|
|
|
A = XDRNG.Prev(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
hi = A >> 16;
|
|
|
|
lo = B >> 16;
|
2020-07-26 16:49:33 +00:00
|
|
|
if (IVsMatch(hi, lo, IVs))
|
2017-12-04 21:18:24 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
pidiv = new PIDIV(CXDAnti, XDRNG.Prev(A));
|
2020-07-26 16:49:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
p2 = XDRNG.Prev(p1);
|
|
|
|
p1 = XDRNG.Prev(p2);
|
2022-06-18 18:04:24 +00:00
|
|
|
psv = (p2 ^ p1) >> 19;
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
while (psv == tsv);
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetChannelMatch(Span<uint> seeds, uint top, uint bot, ReadOnlySpan<uint> IVs, out PIDIV pidiv, PKM pk)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var ver = pk.Version;
|
|
|
|
if (ver is not ((int)GameVersion.R or (int)GameVersion.S))
|
|
|
|
return GetNonMatch(out pidiv);
|
2017-06-30 02:32:29 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var undo = (top >> 16) ^ 0x8000;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((undo > 7 ? 0 : 1) != ((bot >> 16) ^ pk.SID16 ^ 40122))
|
2022-09-04 19:03:37 +00:00
|
|
|
top = (undo << 16);
|
|
|
|
|
|
|
|
var count = XDRNG.GetSeeds(seeds, top, bot);
|
|
|
|
var channel = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in channel)
|
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var C = XDRNG.Next3(seed); // held item
|
2022-06-18 18:04:24 +00:00
|
|
|
// no checks, held item can be swapped
|
2017-06-30 02:32:29 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = XDRNG.Next(C); // Version
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((D >> 31) + 1 != ver) // (0-Sapphire, 1-Ruby)
|
|
|
|
continue;
|
2017-06-30 02:32:29 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var E = XDRNG.Next(D); // OT Gender
|
2022-06-18 18:04:24 +00:00
|
|
|
if (E >> 31 != pk.OT_Gender)
|
|
|
|
continue;
|
2017-06-30 02:32:29 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
if (!XDRNG.GetSequentialIVsUInt32(E, IVs))
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
if (seed >> 16 != pk.SID16)
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2017-06-30 02:32:29 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
pidiv = new PIDIV(Channel, XDRNG.Prev(seed));
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetMG4Match(Span<uint> seeds, uint pid, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-12-02 06:33:08 +00:00
|
|
|
var currentPSV = getPSV(pid);
|
|
|
|
pid = ARNG.Prev(pid);
|
|
|
|
var originalPSV = getPSV(pid);
|
|
|
|
// ARNG shiny value must be different from the original shiny
|
|
|
|
// if we have a multi-rerolled PID, each re-roll must be from the same shiny value
|
|
|
|
if (originalPSV == currentPSV)
|
|
|
|
return GetNonMatch(out pidiv);
|
2022-09-04 19:03:37 +00:00
|
|
|
|
2023-12-02 06:33:08 +00:00
|
|
|
// ARNG can happen at most 3 times (checked all 2^32 seeds)
|
|
|
|
for (int i = 0; i < 3; i++)
|
2017-04-29 23:22:32 +00:00
|
|
|
{
|
2023-12-02 06:33:08 +00:00
|
|
|
var count = LCRNGReversal.GetSeeds(seeds, pid << 16, pid & 0xFFFF0000);
|
|
|
|
var mg4 = seeds[..count];
|
|
|
|
foreach (var seed in mg4)
|
|
|
|
{
|
|
|
|
var C = LCRNG.Next3(seed);
|
|
|
|
var D = LCRNG.Next(C);
|
|
|
|
if (!IVsMatch(C >> 16, D >> 16, IVs))
|
|
|
|
continue;
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2023-12-02 06:33:08 +00:00
|
|
|
pidiv = new PIDIV(G4MGAntiShiny, seed);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue checking for multi-rerolls
|
|
|
|
pid = ARNG.Prev(pid);
|
|
|
|
var prevPSV = getPSV(pid);
|
|
|
|
if (prevPSV != originalPSV)
|
|
|
|
break;
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
2023-12-02 06:33:08 +00:00
|
|
|
|
|
|
|
static uint getPSV(uint u32) => ((u32 >> 16) ^ (u32 & 0xFFFF)) >> 3;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool GetG5MGShinyMatch(PKM pk, uint pid, out PIDIV pidiv)
|
|
|
|
{
|
|
|
|
var low = pid & 0xFFFF;
|
|
|
|
// generation 5 shiny PIDs
|
|
|
|
if (low <= 0xFF)
|
2017-04-29 23:22:32 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var av = (pid >> 16) & 1;
|
2023-01-22 04:02:33 +00:00
|
|
|
var genPID = PIDGenerator.GetMG5ShinyPID(low, av, pk.TID16, pk.SID16);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (genPID == pid)
|
2017-04-30 03:04:54 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pidiv = PIDIV.G5MGShiny;
|
|
|
|
return true;
|
2017-04-30 03:04:54 +00:00
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2023-08-12 23:01:16 +00:00
|
|
|
internal static bool GetCuteCharmMatch(PKM pk, uint pid, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (pid > 0xFF)
|
|
|
|
return GetNonMatch(out pidiv);
|
2017-08-29 15:32:22 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
(var species, int genderValue) = GetCuteCharmGenderSpecies(pk, pid, pk.Species);
|
2023-01-22 04:02:33 +00:00
|
|
|
static byte getRatio(ushort species)
|
2022-09-04 19:03:37 +00:00
|
|
|
{
|
|
|
|
return species <= Legal.MaxSpeciesID_4
|
|
|
|
? PersonalTable.HGSS[species].Gender
|
|
|
|
: PKX.Personal[species].Gender;
|
|
|
|
}
|
2022-08-18 06:48:37 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
switch (genderValue)
|
|
|
|
{
|
|
|
|
case 2: break; // can't cute charm a genderless pk
|
|
|
|
case 0: // male
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
var gr = getRatio(species);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (gr >= PersonalInfo.RatioMagicFemale) // no modification for PID
|
|
|
|
break;
|
|
|
|
var rate = 25*((gr / 25) + 1); // buffered
|
|
|
|
var nature = pid % 25;
|
|
|
|
if (nature + rate != pid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pidiv = PIDIV.CuteCharm;
|
|
|
|
return true;
|
|
|
|
case 1: // female
|
|
|
|
if (pid >= 25)
|
|
|
|
break; // nope, this isn't a valid nature
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
if (getRatio(species) >= PersonalInfo.RatioMagicFemale) // no modification for PID
|
2022-06-18 18:04:24 +00:00
|
|
|
break;
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pidiv = PIDIV.CuteCharm;
|
|
|
|
return true;
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetChainShinyMatch(Span<uint> seeds, PKM pk, uint pid, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// 13 shiny bits
|
|
|
|
// PIDH & 7
|
|
|
|
// PIDL & 7
|
|
|
|
// IVs
|
2023-04-23 00:51:32 +00:00
|
|
|
var bot = GetIVChunk(IVs[..3]) << 16;
|
|
|
|
var top = GetIVChunk(IVs[3..]) << 16;
|
2022-09-04 19:03:37 +00:00
|
|
|
|
|
|
|
var count = LCRNGReversal.GetSeedsIVs(seeds, bot, top);
|
|
|
|
var reg = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in reg)
|
|
|
|
{
|
|
|
|
// check the individual bits
|
|
|
|
var s = seed;
|
|
|
|
int i = 15;
|
|
|
|
do
|
2017-04-30 03:04:54 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var bit = s >> 16 & 1;
|
|
|
|
if (bit != (pid >> i & 1))
|
|
|
|
break;
|
2022-09-04 19:03:37 +00:00
|
|
|
s = LCRNG.Prev(s);
|
2017-04-30 03:04:54 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
while (--i != 2);
|
|
|
|
if (i != 2) // bit failed
|
|
|
|
continue;
|
|
|
|
// Shiny Bits of PID validated
|
|
|
|
var upper = s;
|
|
|
|
if ((upper >> 16 & 7) != (pid >> 16 & 7))
|
|
|
|
continue;
|
2022-09-04 19:03:37 +00:00
|
|
|
var lower = LCRNG.Prev(upper);
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((lower >> 16 & 7) != (pid & 7))
|
|
|
|
continue;
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
var upid = (((pid & 0xFFFF) ^ pk.TID16 ^ pk.SID16) & 0xFFF8) | ((upper >> 16) & 0x7);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (upid != pid >> 16)
|
|
|
|
continue;
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
s = LCRNG.Prev2(lower); // unroll one final time to get the origin seed
|
2022-06-18 18:04:24 +00:00
|
|
|
pidiv = new PIDIV(ChainShiny, s);
|
|
|
|
return true;
|
2017-04-30 03:04:54 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetBACDMatch(Span<uint> seeds, PKM pk, uint pid, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-04-23 00:51:32 +00:00
|
|
|
var bot = GetIVChunk(IVs[..3]) << 16;
|
|
|
|
var top = GetIVChunk(IVs[3..]) << 16;
|
2021-09-06 07:35:40 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var count = LCRNGReversal.GetSeedsIVs(seeds, bot, top);
|
|
|
|
var reg = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
PIDType type = BACD_U;
|
|
|
|
foreach (var seed in reg)
|
2017-04-30 06:18:36 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var B = seed;
|
2022-09-04 19:03:37 +00:00
|
|
|
var A = LCRNG.Prev(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
var low = B >> 16;
|
2017-04-30 06:18:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var PID = (A & 0xFFFF0000) | low;
|
|
|
|
if (PID != pid)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
uint idxor = (uint)(pk.TID16 ^ pk.SID16);
|
2022-06-18 18:04:24 +00:00
|
|
|
bool isShiny = (idxor ^ PID >> 16 ^ (PID & 0xFFFF)) < 8;
|
|
|
|
if (!isShiny)
|
2017-04-30 06:18:36 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pk.IsShiny) // check for nyx antishiny
|
2017-05-01 05:11:51 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!IsBACD_U_AX(idxor, pid, low, A, ref type))
|
|
|
|
continue;
|
2017-05-01 05:11:51 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else // check for force shiny pk
|
2017-06-03 07:12:05 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!IsBACD_U_S(idxor, pid, low, ref A, ref type))
|
2017-06-03 07:12:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-30 06:18:36 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (!IsBACD_U_AX(idxor, pid, low, A, ref type))
|
2017-04-30 06:18:36 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((PID + 8 & 0xFFFFFFF8) != pid)
|
2017-04-30 06:18:36 +00:00
|
|
|
continue;
|
2022-06-18 18:04:24 +00:00
|
|
|
type = BACD_U_A;
|
2017-04-30 06:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-04 19:03:37 +00:00
|
|
|
var s = LCRNG.Prev(A);
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check for prior Restricted seed
|
|
|
|
var sn = s;
|
2022-09-04 19:03:37 +00:00
|
|
|
for (int i = 0; i < 3; i++, sn = LCRNG.Prev(sn))
|
2019-05-16 00:14:48 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((sn & 0xFFFF0000) != 0)
|
|
|
|
continue;
|
|
|
|
// shift from unrestricted enum val to restricted enum val
|
|
|
|
pidiv = new PIDIV(--type, sn);
|
|
|
|
return true;
|
2019-05-16 00:14:48 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
// no restricted seed found, thus unrestricted
|
|
|
|
pidiv = new PIDIV(type, s);
|
2017-08-29 15:32:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2023-10-08 06:14:34 +00:00
|
|
|
private static bool GetPokewalkerMatch(PKM pk, uint pid, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// check surface compatibility
|
2023-04-23 00:51:32 +00:00
|
|
|
// Bits 8-24 must all be zero or all be one.
|
|
|
|
const uint midMask = 0x00FFFF00;
|
2023-10-08 06:14:34 +00:00
|
|
|
var mid = pid & midMask;
|
2023-04-23 00:51:32 +00:00
|
|
|
if (mid is not (0 or midMask))
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
2023-04-23 00:51:32 +00:00
|
|
|
|
|
|
|
// Quirky Nature is not possible with the algorithm.
|
2023-10-08 06:14:34 +00:00
|
|
|
var nature = pid % 25;
|
2023-04-23 00:51:32 +00:00
|
|
|
if (nature == 24)
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
2019-05-16 00:14:48 +00:00
|
|
|
|
2023-04-23 00:51:32 +00:00
|
|
|
// No Pokewalker Pokémon evolves into a different gender-ratio species.
|
2023-09-02 23:46:23 +00:00
|
|
|
// Besides Azurill, and Froslass
|
2022-06-18 18:04:24 +00:00
|
|
|
var gender = pk.Gender;
|
2023-09-02 23:46:23 +00:00
|
|
|
var gr = pk.PersonalInfo.Gender;
|
|
|
|
if (pk.Species == (int)Species.Froslass)
|
|
|
|
gr = 0x7F; // Snorunt
|
2023-10-08 06:14:34 +00:00
|
|
|
var expect = PokewalkerRNG.GetPID(pk.TID16, pk.SID16, nature, gender, gr);
|
|
|
|
if (expect != pid)
|
2017-11-02 04:12:44 +00:00
|
|
|
{
|
2023-10-08 06:14:34 +00:00
|
|
|
if (!(gender == 0 && IsAzurillEdgeCaseM(pk, nature, pid)))
|
2017-11-02 04:12:44 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
pidiv = PIDIV.Pokewalker;
|
|
|
|
return true;
|
|
|
|
}
|
2017-11-02 04:12:44 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static bool IsAzurillEdgeCaseM(PKM pk, uint nature, uint oldpid)
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
// check for Azurill evolution edge case... 75% F-M is now 50% F-M; was this a Female->Male bend?
|
2022-08-27 06:43:36 +00:00
|
|
|
ushort species = pk.Species;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (species is not ((int)Species.Marill or (int)Species.Azumarill))
|
|
|
|
return false;
|
2017-11-02 04:12:44 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
const byte AzurillGenderRatio = 0xBF;
|
2023-08-12 23:01:16 +00:00
|
|
|
var gender = EntityGender.GetFromPIDAndRatio(pk.EncryptionConstant, AzurillGenderRatio);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (gender != 1)
|
|
|
|
return false;
|
|
|
|
|
2023-10-08 06:14:34 +00:00
|
|
|
var pid = PokewalkerRNG.GetPID(pk.TID16, pk.SID16, nature, 1, AzurillGenderRatio);
|
2022-06-18 18:04:24 +00:00
|
|
|
return pid == oldpid;
|
|
|
|
}
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
private static bool GetColoStarterMatch(Span<uint> seeds, PKM pk, uint top, uint bot, ReadOnlySpan<uint> IVs, out PIDIV pidiv)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-07-01 00:23:27 +00:00
|
|
|
bool starter = pk.Version == (int)GameVersion.CXD && pk.Species switch
|
|
|
|
{
|
|
|
|
(int)Species.Espeon when pk.Met_Level >= 25 => true,
|
|
|
|
(int)Species.Umbreon when pk.Met_Level >= 26 => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if (!starter)
|
2017-11-02 04:12:44 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
2017-06-25 00:20:15 +00:00
|
|
|
|
2023-04-23 00:51:32 +00:00
|
|
|
var iv1 = GetIVChunk(IVs[..3]);
|
|
|
|
var iv2 = GetIVChunk(IVs[3..]);
|
2022-09-04 19:03:37 +00:00
|
|
|
|
|
|
|
var count = XDRNG.GetSeeds(seeds, top, bot);
|
|
|
|
var xdc = seeds[..count];
|
2022-06-18 18:04:24 +00:00
|
|
|
foreach (var seed in xdc)
|
2017-08-29 15:32:22 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
uint origin = seed;
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!LockFinder.IsColoStarterValid(pk.Species, ref origin, pk.TID16, pk.SID16, pk.PID, iv1, iv2))
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
pidiv = new PIDIV(CXD_ColoStarter, origin);
|
|
|
|
return true;
|
2017-06-25 00:20:15 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetNonMatch(out pidiv);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns false and no <see cref="PIDIV"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pidiv">Null</param>
|
|
|
|
/// <returns>False</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static bool GetNonMatch(out PIDIV pidiv)
|
|
|
|
{
|
|
|
|
pidiv = PIDIV.None;
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-12 07:27:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the PID is a <see cref="PIDType.BACD_U_S"></see> match.
|
|
|
|
/// </summary>
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <param name="idxor"><see cref="PKM.TID16"/> ^ <see cref="PKM.SID16"/></param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="pid">Full actual PID</param>
|
|
|
|
/// <param name="low">Low portion of PID (B)</param>
|
|
|
|
/// <param name="A">First RNG call</param>
|
|
|
|
/// <param name="type">PID Type is updated if successful</param>
|
|
|
|
/// <returns>True/False if the PID matches</returns>
|
|
|
|
/// <remarks>First RNG call is unrolled once if the PID is valid with this correlation</remarks>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static bool IsBACD_U_S(uint idxor, uint pid, uint low, ref uint A, ref PIDType type)
|
|
|
|
{
|
|
|
|
// 0-Origin
|
|
|
|
// 1-PIDH
|
|
|
|
// 2-PIDL (ends up unused)
|
|
|
|
// 3-FORCEBITS
|
2023-01-22 04:02:33 +00:00
|
|
|
// PID = PIDH << 16 | (SID16 ^ TID16 ^ PIDH)
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var X = LCRNG.Prev(A); // unroll once as there's 3 calls instead of 2
|
2022-06-18 18:04:24 +00:00
|
|
|
uint PID = (X & 0xFFFF0000) | (idxor ^ X >> 16);
|
|
|
|
PID &= 0xFFFFFFF8;
|
|
|
|
PID |= low & 0x7; // lowest 3 bits
|
|
|
|
|
|
|
|
if (PID != pid)
|
|
|
|
return false;
|
|
|
|
A = X; // keep the unrolled seed
|
|
|
|
type = BACD_U_S;
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-12 07:27:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the PID is a <see cref="PIDType.BACD_U_AX"></see> match.
|
|
|
|
/// </summary>
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <param name="idxor"><see cref="PKM.TID16"/> ^ <see cref="PKM.SID16"/></param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="pid">Full actual PID</param>
|
|
|
|
/// <param name="low">Low portion of PID (B)</param>
|
|
|
|
/// <param name="A">First RNG call</param>
|
|
|
|
/// <param name="type">PID Type is updated if successful</param>
|
|
|
|
/// <returns>True/False if the PID matches</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static bool IsBACD_U_AX(uint idxor, uint pid, uint low, uint A, ref PIDType type)
|
|
|
|
{
|
|
|
|
if ((pid & 0xFFFF) != low)
|
|
|
|
return false;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// 0-Origin
|
|
|
|
// 1-ushort rnd, do until >8
|
|
|
|
// 2-PIDL
|
2017-09-12 07:27:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
uint rnd = A >> 16;
|
|
|
|
if (rnd < 8)
|
|
|
|
return false;
|
|
|
|
uint PID = ((rnd ^ idxor ^ low) << 16) | low;
|
|
|
|
if (PID != pid)
|
|
|
|
return false;
|
|
|
|
type = BACD_U_AX;
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-12 07:27:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static PIDIV AnalyzeGB(PKM _)
|
|
|
|
{
|
|
|
|
// not implemented; correlation between IVs and RNG hasn't been converted to code.
|
|
|
|
return PIDIV.None;
|
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Generates IVs from 2 RNG calls using 15 bits of each to generate 6 IVs (5bits each).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="r1">First rand frame</param>
|
|
|
|
/// <param name="r2">Second rand frame</param>
|
|
|
|
/// <param name="IVs">IVs that should be the result</param>
|
|
|
|
/// <returns>IVs match random number IVs</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static bool IVsMatch(uint r1, uint r2, ReadOnlySpan<uint> IVs)
|
|
|
|
{
|
|
|
|
if (IVs[0] != (r1 & 31))
|
|
|
|
return false;
|
|
|
|
if (IVs[1] != (r1 >> 5 & 31))
|
|
|
|
return false;
|
|
|
|
if (IVs[2] != (r1 >> 10 & 31))
|
|
|
|
return false;
|
|
|
|
if (IVs[3] != (r2 & 31))
|
|
|
|
return false;
|
|
|
|
if (IVs[4] != (r2 >> 5 & 31))
|
|
|
|
return false;
|
|
|
|
if (IVs[5] != (r2 >> 10 & 31))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Generates IVs from 2 RNG calls using 15 bits of each to generate 6 IVs (5bits each).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="result">Result storage</param>
|
|
|
|
/// <param name="r1">First rand frame</param>
|
|
|
|
/// <param name="r2">Second rand frame</param>
|
|
|
|
/// <returns>Array of 6 IVs</returns>
|
|
|
|
internal static void GetIVsInt32(Span<int> result, uint r1, uint r2)
|
|
|
|
{
|
|
|
|
result[5] = (int)r2 >> 10 & 31;
|
|
|
|
result[4] = (int)r2 >> 5 & 31;
|
|
|
|
result[3] = (int)r2 & 31;
|
|
|
|
result[2] = (int)r1 >> 10 & 31;
|
|
|
|
result[1] = (int)r1 >> 5 & 31;
|
|
|
|
result[0] = (int)r1 & 31;
|
|
|
|
}
|
2019-01-06 09:26:18 +00:00
|
|
|
|
2023-04-23 00:51:32 +00:00
|
|
|
private static uint GetIVChunk(ReadOnlySpan<uint> arr)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-04-23 00:51:32 +00:00
|
|
|
uint result = 0;
|
|
|
|
for (int i = 0; i < arr.Length; i++)
|
|
|
|
result |= arr[i] << (5*i);
|
|
|
|
return result;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2023-01-22 04:02:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsPokeSpotActivation(int slot, uint seed, out uint s)
|
|
|
|
{
|
|
|
|
s = seed;
|
|
|
|
var esv = (seed >> 16) % 100;
|
|
|
|
if (!IsPokeSpotSlotValid(slot, esv))
|
2017-04-30 23:53:54 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// todo
|
2017-04-30 23:53:54 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
// check for valid activation
|
2022-09-04 19:03:37 +00:00
|
|
|
s = XDRNG.Prev(seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((s >> 16) % 3 != 0)
|
2018-04-30 01:26:36 +00:00
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
if ((s >> 16) % 100 < 10) // can't fail a Munchlax/Bonsly encounter check
|
2018-04-30 01:26:36 +00:00
|
|
|
{
|
2018-06-06 02:08:31 +00:00
|
|
|
// todo
|
2018-04-30 01:26:36 +00:00
|
|
|
}
|
2022-09-04 19:03:37 +00:00
|
|
|
s = XDRNG.Prev(s);
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((s >> 16) % 3 != 0) // can't activate even if generous
|
2018-04-30 01:26:36 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// todo
|
2018-04-30 01:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool IsPokeSpotSlotValid(int slot, uint esv) => slot switch
|
|
|
|
{
|
|
|
|
0 => esv < 50 , // [0,50)
|
|
|
|
1 => esv - 50 < 35, // [50,85)
|
|
|
|
2 => esv >= 85, // [85,100)
|
|
|
|
_ => false,
|
|
|
|
};
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2023-08-12 23:01:16 +00:00
|
|
|
internal static bool IsCuteCharm4Valid(ISpeciesForm enc, PKM pk)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-12 23:01:16 +00:00
|
|
|
if (pk.Gender is not (0 or 1))
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2023-08-12 23:01:16 +00:00
|
|
|
if (pk.Species is not ((int)Species.Marill or (int)Species.Azumarill))
|
|
|
|
return true;
|
|
|
|
if (!IsCuteCharmAzurillMale(pk.PID)) // recognized as not Azurill
|
|
|
|
return true;
|
|
|
|
return enc.Species == (int)Species.Azurill; // encounter must be male Azurill
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private static bool IsCuteCharmAzurillMale(uint pid) => pid is >= 0xC8 and <= 0xE0;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// There are some edge cases when the gender ratio changes across evolutions.
|
|
|
|
/// </summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
private static (ushort Species, int Gender) GetCuteCharmGenderSpecies(PKM pk, uint pid, ushort currentSpecies) => currentSpecies switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// Nincada evo chain travels from M/F -> Genderless Shedinja
|
|
|
|
(int)Species.Shedinja => ((int)Species.Nincada, EntityGender.GetFromPID((int)Species.Nincada, pid)),
|
|
|
|
|
|
|
|
// These evolved species cannot be encountered with cute charm.
|
|
|
|
// 100% fixed gender does not modify PID; override this with the encounter species for correct calculation.
|
2023-12-04 04:13:20 +00:00
|
|
|
// We can assume the re-mapped species' [gender ratio] is what was encountered.
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Species.Wormadam => ((int)Species.Burmy, 1),
|
|
|
|
(int)Species.Mothim => ((int)Species.Burmy, 0),
|
|
|
|
(int)Species.Vespiquen => ((int)Species.Combee, 1),
|
|
|
|
(int)Species.Gallade => ((int)Species.Kirlia, 0),
|
|
|
|
(int)Species.Froslass => ((int)Species.Snorunt, 1),
|
|
|
|
// Azurill & Marill/Azumarill collision
|
|
|
|
// Changed gender ratio (25% M -> 50% M) needs special treatment.
|
2023-12-04 04:13:20 +00:00
|
|
|
// Double-check the encounter species with IsCuteCharm4Valid afterward.
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Species.Marill or (int)Species.Azumarill when IsCuteCharmAzurillMale(pid) => ((int)Species.Azurill, 0),
|
|
|
|
|
|
|
|
// Future evolutions
|
|
|
|
(int)Species.Sylveon => ((int)Species.Eevee, pk.Gender),
|
2022-09-04 19:03:37 +00:00
|
|
|
(int)Species.MrRime => ((int)Species.MimeJr, pk.Gender),
|
|
|
|
(int)Species.Kleavor => ((int)Species.Scyther, pk.Gender),
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
_ => (currentSpecies, pk.Gender),
|
|
|
|
};
|
2022-09-04 19:03:37 +00:00
|
|
|
|
|
|
|
public static PIDIV GetPokeSpotSeedFirst(PKM pk, byte slot)
|
|
|
|
{
|
|
|
|
// Activate (rand % 3)
|
|
|
|
// Munchlax / Bonsly (10%/30%)
|
|
|
|
// Encounter Slot Value (ESV) = 50%/35%/15% rarity (0-49, 50-84, 85-99)
|
|
|
|
|
|
|
|
Span<uint> seeds = stackalloc uint[XDRNG.MaxCountSeedsPID];
|
|
|
|
int count = XDRNG.GetSeeds(seeds, pk.EncryptionConstant);
|
|
|
|
var reg = seeds[..count];
|
|
|
|
foreach (var seed in reg)
|
|
|
|
{
|
|
|
|
// check for valid encounter slot info
|
|
|
|
if (IsPokeSpotActivation(slot, seed, out uint s))
|
|
|
|
return new PIDIV(PokeSpot, s);
|
|
|
|
}
|
|
|
|
return default;
|
|
|
|
}
|
2017-04-29 23:22:32 +00:00
|
|
|
}
|