2022-09-04 19:03:37 +00:00
|
|
|
using System;
|
2018-04-29 05:56:10 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains a collection of methods that mutate the input Pokémon object, usually to obtain a <see cref="PIDType"/> correlation.
|
|
|
|
/// </summary>
|
|
|
|
public static class PIDGenerator
|
2017-07-16 01:36:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SetValuesFromSeedLCRNG(PKM pk, PIDType type, uint seed)
|
2017-07-16 01:36:55 +00:00
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var A = LCRNG.Next(seed);
|
|
|
|
var B = LCRNG.Next(A);
|
2022-06-18 18:04:24 +00:00
|
|
|
var skipBetweenPID = type is PIDType.Method_3 or PIDType.Method_3_Unown;
|
|
|
|
if (skipBetweenPID) // VBlank skip between PID rand() [RARE]
|
2022-09-04 19:03:37 +00:00
|
|
|
B = LCRNG.Next(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
var swappedPIDHalves = type is >= PIDType.Method_1_Unown and <= PIDType.Method_4_Unown;
|
2023-12-04 04:13:20 +00:00
|
|
|
if (swappedPIDHalves) // switched order of PID halves, "BA**"
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.PID = (A & 0xFFFF0000) | (B >> 16);
|
|
|
|
else
|
|
|
|
pk.PID = (B & 0xFFFF0000) | (A >> 16);
|
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var C = LCRNG.Next(B);
|
2022-06-18 18:04:24 +00:00
|
|
|
var skipIV1Frame = type is PIDType.Method_2 or PIDType.Method_2_Unown;
|
|
|
|
if (skipIV1Frame) // VBlank skip after PID
|
2022-09-04 19:03:37 +00:00
|
|
|
C = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
var skipIV2Frame = type is PIDType.Method_4 or PIDType.Method_4_Unown;
|
|
|
|
if (skipIV2Frame) // VBlank skip between IVs
|
2022-09-04 19:03:37 +00:00
|
|
|
D = LCRNG.Next(D);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
Span<int> IVs = stackalloc int[6];
|
|
|
|
MethodFinder.GetIVsInt32(IVs, C >> 16, D >> 16);
|
|
|
|
if (type == PIDType.Method_1_Roamer)
|
2017-07-16 01:36:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Only store lowest 8 bits of IV data; zero out the other bits.
|
|
|
|
IVs[1] &= 7;
|
|
|
|
for (int i = 2; i < 6; i++)
|
|
|
|
IVs[i] = 0;
|
2017-07-16 01:36:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.SetIVs(IVs);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SetValuesFromSeedBACD(PKM pk, PIDType type, uint seed)
|
|
|
|
{
|
|
|
|
bool shiny = type is PIDType.BACD_R_S or PIDType.BACD_U_S;
|
2022-09-04 19:03:37 +00:00
|
|
|
uint X = shiny ? LCRNG.Next(seed) : seed;
|
|
|
|
var A = LCRNG.Next(X);
|
|
|
|
var B = LCRNG.Next(A);
|
|
|
|
var C = LCRNG.Next(B);
|
|
|
|
var D = LCRNG.Next(C);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
if (shiny)
|
2017-07-16 01:36:55 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
uint PID = (X & 0xFFFF0000) | ((X >> 16) ^ pk.TID16 ^ pk.SID16);
|
2022-06-18 18:04:24 +00:00
|
|
|
PID &= 0xFFFFFFF8;
|
|
|
|
PID |= (B >> 16) & 0x7; // lowest 3 bits
|
2017-07-16 01:36:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.PID = PID;
|
2017-07-16 01:36:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (type is PIDType.BACD_R_AX or PIDType.BACD_U_AX)
|
2017-07-16 01:36:55 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
uint low = B >> 16;
|
2023-01-22 04:02:33 +00:00
|
|
|
pk.PID = ((A & 0xFFFF0000) ^ ((low ^ pk.TID16 ^ pk.SID16) << 16)) | low;
|
2017-07-16 01:36:55 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2022-02-09 17:26:10 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.PID = (A & 0xFFFF0000) | (B >> 16);
|
2022-02-09 17:26:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
Span<int> IVs = stackalloc int[6];
|
|
|
|
MethodFinder.GetIVsInt32(IVs, C >> 16, D >> 16);
|
|
|
|
pk.SetIVs(IVs);
|
2017-07-16 01:36:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
bool antishiny = type is PIDType.BACD_R_A or PIDType.BACD_U_A;
|
|
|
|
while (antishiny && pk.IsShiny)
|
|
|
|
pk.PID = unchecked(pk.PID + 1);
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SetValuesFromSeedXDRNG(PKM pk, uint seed)
|
|
|
|
{
|
2023-10-26 08:07:20 +00:00
|
|
|
var species = pk.Species;
|
|
|
|
switch (species)
|
2018-04-29 16:31:13 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case (int)Species.Umbreon or (int)Species.Eevee: // Colo Umbreon, XD Eevee
|
2023-10-26 08:07:20 +00:00
|
|
|
pk.TID16 = (ushort)XDRNG.Next16(ref seed);
|
|
|
|
pk.SID16 = (ushort)XDRNG.Next16(ref seed);
|
|
|
|
seed = XDRNG.Next2(seed); // fake PID
|
2022-06-18 18:04:24 +00:00
|
|
|
break;
|
|
|
|
case (int)Species.Espeon: // Colo Espeon
|
2023-10-26 08:07:20 +00:00
|
|
|
var tid = pk.TID16 = (ushort)XDRNG.Next16(ref seed);
|
|
|
|
var sid = pk.SID16 = (ushort)XDRNG.Next16(ref seed);
|
|
|
|
LockFinder.SkipValidColoStarter(ref seed, tid, sid);
|
|
|
|
seed = XDRNG.Next2(seed); // fake PID
|
2022-06-18 18:04:24 +00:00
|
|
|
break;
|
2017-07-16 01:36:55 +00:00
|
|
|
}
|
2022-09-04 19:03:37 +00:00
|
|
|
var A = XDRNG.Next(seed); // IV1
|
|
|
|
var B = XDRNG.Next(A); // IV2
|
2023-10-26 08:07:20 +00:00
|
|
|
var C = XDRNG.Next(B); // Ability
|
|
|
|
|
|
|
|
if (species is (int)Species.Umbreon or (int)Species.Espeon)
|
|
|
|
{
|
|
|
|
// Reuse existing logic.
|
|
|
|
pk.PID = LockFinder.GenerateStarterPID(ref C, pk.TID16, pk.SID16);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Generate PID.
|
|
|
|
var D = XDRNG.Next(C); // PID
|
|
|
|
var E = XDRNG.Next(D); // PID
|
|
|
|
pk.PID = (D & 0xFFFF0000) | (E >> 16);
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
Span<int> IVs = stackalloc int[6];
|
|
|
|
MethodFinder.GetIVsInt32(IVs, A >> 16, B >> 16);
|
|
|
|
pk.SetIVs(IVs);
|
|
|
|
}
|
2017-09-17 21:43:28 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SetValuesFromSeedXDRNG_EReader(PKM pk, uint seed)
|
|
|
|
{
|
2022-09-04 19:03:37 +00:00
|
|
|
var D = XDRNG.Prev3(seed); // PID
|
|
|
|
var E = XDRNG.Next(D); // PID
|
2018-05-08 04:16:54 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.PID = (D & 0xFFFF0000) | (E >> 16);
|
|
|
|
}
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SetValuesFromSeedChannel(PKM pk, uint seed)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var O = XDRNG.Next(seed); // SID16
|
2022-09-04 19:03:37 +00:00
|
|
|
var A = XDRNG.Next(O); // PID
|
|
|
|
var B = XDRNG.Next(A); // PID
|
|
|
|
var C = XDRNG.Next(B); // Held Item
|
|
|
|
var D = XDRNG.Next(C); // Version
|
|
|
|
var E = XDRNG.Next(D); // OT Gender
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
const ushort TID16 = 40122;
|
|
|
|
pk.ID32 = (O & 0xFFFF0000) | TID16;
|
|
|
|
var SID16 = O >> 16;
|
2022-06-18 18:04:24 +00:00
|
|
|
var pid1 = A >> 16;
|
|
|
|
var pid2 = B >> 16;
|
|
|
|
var pid = (pid1 << 16) | pid2;
|
2023-01-22 04:02:33 +00:00
|
|
|
if ((pid2 > 7 ? 0 : 1) != (pid1 ^ SID16 ^ TID16))
|
2022-06-18 18:04:24 +00:00
|
|
|
pid ^= 0x80000000;
|
|
|
|
pk.PID = pid;
|
2024-02-23 03:20:54 +00:00
|
|
|
pk.HeldItem = (ushort)(C >> 31) + 169; // 0-Ganlon, 1-Salac
|
|
|
|
pk.Version = GameVersion.S + (byte)(D >> 31); // 0-Sapphire, 1-Ruby
|
|
|
|
pk.OriginalTrainerGender = (byte)(E >> 31);
|
2022-06-18 18:04:24 +00:00
|
|
|
Span<int> ivs = stackalloc int[6];
|
2022-09-04 19:03:37 +00:00
|
|
|
XDRNG.GetSequentialIVsInt32(E, ivs);
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.SetIVs(ivs);
|
|
|
|
}
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SetValuesFromSeed(PKM pk, PIDType type, uint seed)
|
|
|
|
{
|
|
|
|
var method = GetGeneratorMethod(type);
|
|
|
|
method(pk, seed);
|
|
|
|
}
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static Action<PKM, uint> GetGeneratorMethod(PIDType t)
|
|
|
|
{
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case PIDType.Channel:
|
|
|
|
return SetValuesFromSeedChannel;
|
|
|
|
case PIDType.CXD:
|
|
|
|
return SetValuesFromSeedXDRNG;
|
|
|
|
|
|
|
|
case PIDType.Method_1 or PIDType.Method_2 or PIDType.Method_3 or PIDType.Method_4:
|
|
|
|
case PIDType.Method_1_Unown or PIDType.Method_2_Unown or PIDType.Method_3_Unown or PIDType.Method_4_Unown:
|
|
|
|
case PIDType.Method_1_Roamer:
|
|
|
|
return (pk, seed) => SetValuesFromSeedLCRNG(pk, t, seed);
|
|
|
|
|
|
|
|
case PIDType.BACD_R:
|
|
|
|
case PIDType.BACD_R_A:
|
|
|
|
case PIDType.BACD_R_S:
|
|
|
|
case PIDType.BACD_R_AX:
|
|
|
|
return (pk, seed) => SetValuesFromSeedBACD(pk, t, seed & 0xFFFF);
|
|
|
|
case PIDType.BACD_U:
|
|
|
|
case PIDType.BACD_U_A:
|
|
|
|
case PIDType.BACD_U_S:
|
|
|
|
case PIDType.BACD_U_AX:
|
|
|
|
return (pk, seed) => SetValuesFromSeedBACD(pk, t, seed);
|
|
|
|
|
|
|
|
case PIDType.PokeSpot:
|
2023-08-17 07:07:54 +00:00
|
|
|
return SetRandomPokeSpotPID;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
case PIDType.G5MGShiny:
|
|
|
|
return SetValuesFromSeedMG5Shiny;
|
|
|
|
|
|
|
|
case PIDType.Pokewalker:
|
2023-10-08 06:14:34 +00:00
|
|
|
return (pk, seed) =>
|
|
|
|
{
|
|
|
|
var pid = pk.PID = PokewalkerRNG.GetPID(pk.TID16, pk.SID16, seed % 24, pk.Gender, pk.PersonalInfo.Gender);
|
|
|
|
pk.RefreshAbility((int)(pid & 1));
|
|
|
|
};
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// others: unimplemented
|
|
|
|
case PIDType.CuteCharm:
|
|
|
|
break;
|
|
|
|
case PIDType.ChainShiny:
|
|
|
|
return SetRandomChainShinyPID;
|
|
|
|
case PIDType.G4MGAntiShiny:
|
|
|
|
break;
|
2018-04-30 01:26:36 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return (_, _) => { };
|
|
|
|
}
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SetRandomChainShinyPID(PKM pk, uint seed)
|
|
|
|
{
|
2023-10-12 02:28:51 +00:00
|
|
|
pk.PID = ClassicEraRNG.GetChainShinyPID(ref seed, pk.ID32);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
Span<int> IVs = stackalloc int[6];
|
2023-10-12 02:28:51 +00:00
|
|
|
var rand1 = LCRNG.Next16(ref seed);
|
|
|
|
var rand2 = LCRNG.Next16(ref seed);
|
|
|
|
MethodFinder.GetIVsInt32(IVs, rand1, rand2);
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.SetIVs(IVs);
|
|
|
|
}
|
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
private static void SetRandomPokeSpotPID(PKM pk, uint seed)
|
|
|
|
{
|
|
|
|
var D = XDRNG.Next(seed); // PID
|
|
|
|
var E = XDRNG.Next(D); // PID
|
|
|
|
pk.PID = (D & 0xFFFF0000) | (E >> 16);
|
|
|
|
}
|
|
|
|
|
2024-02-23 03:20:54 +00:00
|
|
|
public static void SetRandomPokeSpotPID(PKM pk, Nature nature, byte gender, int ability, int slot)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var rnd = Util.Rand;
|
|
|
|
while (true)
|
2017-09-17 21:43:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var seed = rnd.Rand32();
|
2023-08-17 07:07:54 +00:00
|
|
|
if (!MethodFinder.IsPokeSpotActivation(slot, seed, out var newSeed))
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
SetRandomPokeSpotPID(pk, newSeed);
|
|
|
|
pk.SetRandomIVs();
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
if (!IsValidCriteria4(pk, nature, ability, gender))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return;
|
2017-09-17 21:43:28 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-04-29 05:56:10 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
public static uint GetMG5ShinyPID(uint gval, uint av, ushort TID16, ushort SID16)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
uint PID = ((gval ^ TID16 ^ SID16) << 16) | gval;
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((PID & 0x10000) != av << 16)
|
|
|
|
PID ^= 0x10000;
|
|
|
|
return PID;
|
|
|
|
}
|
2018-04-29 18:33:35 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SetValuesFromSeedMG5Shiny(PKM pk, uint seed)
|
|
|
|
{
|
|
|
|
var gv = seed >> 24;
|
|
|
|
var av = seed & 1; // arbitrary choice
|
2023-01-22 04:02:33 +00:00
|
|
|
pk.PID = GetMG5ShinyPID(gv, av, pk.TID16, pk.SID16);
|
2022-06-18 18:04:24 +00:00
|
|
|
SetRandomIVs(pk);
|
|
|
|
}
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2024-02-23 03:20:54 +00:00
|
|
|
public static uint SetRandomWildPID4(PKM pk, Nature nature, int ability, byte gender, PIDType type)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
pk.RefreshAbility(ability);
|
|
|
|
pk.Gender = gender;
|
|
|
|
var method = GetGeneratorMethod(type);
|
|
|
|
|
|
|
|
var rnd = Util.Rand;
|
|
|
|
while (true)
|
2018-04-30 01:26:36 +00:00
|
|
|
{
|
2024-02-23 03:20:54 +00:00
|
|
|
var seed = rnd.Rand32();
|
|
|
|
method(pk, seed);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!IsValidCriteria4(pk, nature, ability, gender))
|
|
|
|
continue;
|
2024-02-23 03:20:54 +00:00
|
|
|
return seed;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2024-02-23 03:20:54 +00:00
|
|
|
public static bool IsValidCriteria4(PKM pk, Nature nature, int ability, byte gender)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (pk.GetSaneGender() != gender)
|
|
|
|
return false;
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Nature != nature)
|
|
|
|
return false;
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2023-08-12 23:01:16 +00:00
|
|
|
if ((pk.EncryptionConstant & 1) != ability)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-30 01:26:36 +00:00
|
|
|
|
2024-02-23 03:20:54 +00:00
|
|
|
public static void SetRandomWildPID5(PKM pk, Nature nature, int ability, byte gender, PIDType specific = PIDType.None)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var tidbit = (pk.TID16 ^ pk.SID16) & 1;
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.RefreshAbility(ability);
|
|
|
|
pk.Gender = gender;
|
|
|
|
pk.Nature = nature;
|
2018-04-29 05:56:10 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (ability == 2)
|
|
|
|
ability = 0;
|
2018-04-29 05:56:10 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var rnd = Util.Rand;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
uint seed = rnd.Rand32();
|
|
|
|
if (specific == PIDType.G5MGShiny)
|
2018-04-29 05:56:10 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
SetValuesFromSeedMG5Shiny(pk, seed);
|
|
|
|
seed = pk.PID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var bitxor = (seed >> 31) ^ (seed & 1);
|
|
|
|
if (bitxor != tidbit)
|
|
|
|
seed ^= 1;
|
2018-04-29 05:56:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (((seed >> 16) & 1) != ability)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pk.PID = seed;
|
|
|
|
if (pk.GetSaneGender() != gender)
|
|
|
|
continue;
|
|
|
|
return;
|
2018-04-29 05:56:10 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-04-29 05:56:10 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SetRandomIVs(PKM pk)
|
|
|
|
{
|
|
|
|
pk.SetRandomIVs();
|
2017-07-16 01:36:55 +00:00
|
|
|
}
|
|
|
|
}
|