2022-08-03 23:11:49 +00:00
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains logic for the Generation 8b (BD/SP) roaming spawns.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Roaming encounters use the pokemon's 32-bit <see cref="PKM.EncryptionConstant"/> as RNG seed.
|
|
|
|
/// </remarks>
|
|
|
|
public static class Roaming8bRNG
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private const int UNSET = -1;
|
|
|
|
|
2022-08-03 23:11:49 +00:00
|
|
|
public static void ApplyDetails(PKM pk, EncounterCriteria criteria, Shiny shiny = Shiny.FixedValue, int flawless = -1, int maxAttempts = 70_000)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (shiny == Shiny.FixedValue)
|
2022-08-03 23:11:49 +00:00
|
|
|
shiny = criteria.Shiny is Shiny.Random or Shiny.Never ? Shiny.Never : criteria.Shiny;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (flawless == -1)
|
|
|
|
flawless = 0;
|
|
|
|
|
|
|
|
int ctr = 0;
|
|
|
|
var rnd = Util.Rand;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
var seed = rnd.Rand32();
|
|
|
|
if (seed == int.MaxValue)
|
|
|
|
continue; // Unity's Rand is [int.MinValue, int.MaxValue)
|
|
|
|
if (TryApplyFromSeed(pk, criteria, shiny, flawless, seed))
|
|
|
|
return;
|
|
|
|
} while (++ctr != maxAttempts);
|
|
|
|
TryApplyFromSeed(pk, EncounterCriteria.Unrestricted, shiny, flawless, rnd.Rand32());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool TryApplyFromSeed(PKM pk, EncounterCriteria criteria, Shiny shiny, int flawless, uint seed)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var xoro = new Xoroshiro128Plus8b(seed);
|
|
|
|
|
|
|
|
// Encryption Constant
|
|
|
|
pk.EncryptionConstant = seed;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// PID
|
|
|
|
var fakeTID = xoro.NextUInt(); // fakeTID
|
|
|
|
var pid = xoro.NextUInt();
|
|
|
|
pid = GetRevisedPID(fakeTID, pid, pk);
|
2023-01-22 04:02:33 +00:00
|
|
|
var xor = GetShinyXor(pk.ID32, pid);
|
2022-08-03 23:11:49 +00:00
|
|
|
var type = GetRareType(xor);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (shiny == Shiny.Never)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-08-03 23:11:49 +00:00
|
|
|
if (type != Shiny.Never)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (shiny != Shiny.Random)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-08-03 23:11:49 +00:00
|
|
|
if (type == Shiny.Never)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
|
2022-08-03 23:11:49 +00:00
|
|
|
if (shiny == Shiny.AlwaysSquare && type != Shiny.AlwaysSquare)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2022-08-03 23:11:49 +00:00
|
|
|
if (shiny == Shiny.AlwaysStar && type != Shiny.AlwaysStar)
|
2021-11-20 02:23:49 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
pk.PID = pid;
|
|
|
|
|
|
|
|
// Check IVs: Create flawless IVs at random indexes, then the random IVs for not flawless.
|
|
|
|
Span<int> ivs = stackalloc [] { UNSET, UNSET, UNSET, UNSET, UNSET, UNSET };
|
|
|
|
const int MAX = 31;
|
|
|
|
var determined = 0;
|
|
|
|
while (determined < flawless)
|
|
|
|
{
|
|
|
|
var idx = (int)xoro.NextUInt(6);
|
|
|
|
if (ivs[idx] != UNSET)
|
|
|
|
continue;
|
|
|
|
ivs[idx] = 31;
|
|
|
|
determined++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < ivs.Length; i++)
|
|
|
|
{
|
|
|
|
if (ivs[i] == UNSET)
|
|
|
|
ivs[i] = (int)xoro.NextUInt(MAX + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!criteria.IsIVsCompatible(ivs, 8))
|
|
|
|
return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.IV_HP = ivs[0];
|
|
|
|
pk.IV_ATK = ivs[1];
|
|
|
|
pk.IV_DEF = ivs[2];
|
|
|
|
pk.IV_SPA = ivs[3];
|
|
|
|
pk.IV_SPD = ivs[4];
|
|
|
|
pk.IV_SPE = ivs[5];
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Ability
|
|
|
|
pk.SetAbilityIndex((int)xoro.NextUInt(2));
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Remainder
|
|
|
|
var scale = (IScaledSize)pk;
|
|
|
|
scale.HeightScalar = (byte)((int)xoro.NextUInt(0x81) + (int)xoro.NextUInt(0x80));
|
|
|
|
scale.WeightScalar = (byte)((int)xoro.NextUInt(0x81) + (int)xoro.NextUInt(0x80));
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ValidateRoamingEncounter(PKM pk, Shiny shiny = Shiny.Random, int flawless = 0)
|
|
|
|
{
|
|
|
|
var seed = pk.EncryptionConstant;
|
|
|
|
if (seed == int.MaxValue)
|
|
|
|
return false; // Unity's Rand is [int.MinValue, int.MaxValue)
|
|
|
|
var xoro = new Xoroshiro128Plus8b(seed);
|
|
|
|
|
|
|
|
// Check PID
|
|
|
|
var fakeTID = xoro.NextUInt(); // fakeTID
|
|
|
|
var pid = xoro.NextUInt();
|
|
|
|
pid = GetRevisedPID(fakeTID, pid, pk);
|
|
|
|
if (pk.PID != pid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check IVs: Create flawless IVs at random indexes, then the random IVs for not flawless.
|
|
|
|
Span<int> ivs = stackalloc [] { UNSET, UNSET, UNSET, UNSET, UNSET, UNSET };
|
|
|
|
|
|
|
|
var determined = 0;
|
|
|
|
while (determined < flawless)
|
|
|
|
{
|
|
|
|
var idx = (int)xoro.NextUInt(6);
|
|
|
|
if (ivs[idx] != UNSET)
|
|
|
|
continue;
|
|
|
|
ivs[idx] = 31;
|
|
|
|
determined++;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
for (var i = 0; i < ivs.Length; i++)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (ivs[i] == UNSET)
|
|
|
|
ivs[i] = (int)xoro.NextUInt(31 + 1);
|
|
|
|
}
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (ivs[0] != pk.IV_HP ) return false;
|
|
|
|
if (ivs[1] != pk.IV_ATK) return false;
|
|
|
|
if (ivs[2] != pk.IV_DEF) return false;
|
|
|
|
if (ivs[3] != pk.IV_SPA) return false;
|
|
|
|
if (ivs[4] != pk.IV_SPD) return false;
|
|
|
|
if (ivs[5] != pk.IV_SPE) return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Don't check Hidden ability, as roaming encounters are 1/2 only.
|
|
|
|
if (pk.AbilityNumber != (1 << (int)xoro.NextUInt(2)))
|
|
|
|
return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetIsMatchEnd(pk, xoro) || GetIsMatchEndWithCuteCharm(pk, xoro) || GetIsMatchEndWithSynchronize(pk, xoro);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool GetIsMatchEnd(PKM pk, Xoroshiro128Plus8b xoro)
|
|
|
|
{
|
|
|
|
// Check that gender matches
|
|
|
|
var genderRatio = PersonalTable.BDSP.GetFormEntry(pk.Species, pk.Form).Gender;
|
|
|
|
if (genderRatio == PersonalInfo.RatioMagicGenderless)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != (int)Gender.Genderless)
|
2021-11-20 02:23:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (genderRatio == PersonalInfo.RatioMagicMale)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != (int)Gender.Male)
|
2021-11-20 02:23:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (genderRatio == PersonalInfo.RatioMagicFemale)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != (int)Gender.Female)
|
|
|
|
return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != (((int)xoro.NextUInt(253) + 1 < genderRatio) ? 1 : 0))
|
2021-11-20 02:23:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check that the nature matches
|
|
|
|
if (pk.Nature != (int)xoro.NextUInt(25))
|
|
|
|
return false;
|
2021-11-25 21:34:53 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetIsHeightWeightMatch(pk, xoro);
|
|
|
|
}
|
2021-11-25 21:34:53 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool GetIsMatchEndWithCuteCharm(PKM pk, Xoroshiro128Plus8b xoro)
|
|
|
|
{
|
|
|
|
// Check that gender matches
|
|
|
|
// Assume that the gender is a match due to cute charm.
|
2021-11-25 21:34:53 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check that the nature matches
|
|
|
|
if (pk.Nature != (int)xoro.NextUInt(25))
|
|
|
|
return false;
|
2021-11-25 16:32:36 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetIsHeightWeightMatch(pk, xoro);
|
|
|
|
}
|
2021-11-25 21:34:53 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool GetIsMatchEndWithSynchronize(PKM pk, Xoroshiro128Plus8b xoro)
|
|
|
|
{
|
|
|
|
// Check that gender matches
|
|
|
|
var genderRatio = PersonalTable.BDSP.GetFormEntry(pk.Species, pk.Form).Gender;
|
|
|
|
if (genderRatio == PersonalInfo.RatioMagicGenderless)
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != (int)Gender.Genderless)
|
|
|
|
return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (genderRatio == PersonalInfo.RatioMagicMale)
|
|
|
|
{
|
|
|
|
if (pk.Gender != (int)Gender.Male)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (genderRatio == PersonalInfo.RatioMagicFemale)
|
|
|
|
{
|
|
|
|
if (pk.Gender != (int)Gender.Female)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
2021-11-20 02:23:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != (((int)xoro.NextUInt(253) + 1 < genderRatio) ? 1 : 0))
|
|
|
|
return false;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Assume that the nature is a match due to synchronize.
|
|
|
|
|
|
|
|
return GetIsHeightWeightMatch(pk, xoro);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool GetIsHeightWeightMatch(PKM pk, Xoroshiro128Plus8b xoro)
|
|
|
|
{
|
|
|
|
// Check height/weight
|
|
|
|
if (pk is not IScaledSize s)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var height = xoro.NextUInt(0x81) + xoro.NextUInt(0x80);
|
|
|
|
var weight = xoro.NextUInt(0x81) + xoro.NextUInt(0x80);
|
|
|
|
return s.HeightScalar == height && s.WeightScalar == weight;
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static uint GetRevisedPID(uint fakeTID, uint pid, ITrainerID32 tr)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var xor = GetShinyXor(pid, fakeTID);
|
2023-01-22 04:02:33 +00:00
|
|
|
var newXor = GetShinyXor(pid, tr.ID32);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
var fakeRare = GetRareType(xor);
|
|
|
|
var newRare = GetRareType(newXor);
|
|
|
|
|
|
|
|
if (fakeRare == newRare)
|
|
|
|
return pid;
|
|
|
|
|
|
|
|
var isShiny = xor < 16;
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!isShiny)
|
|
|
|
return pid ^ 0x1000_0000;
|
|
|
|
var low = pid & 0xFFFF;
|
|
|
|
return (((xor == 0 ? 0u : 1u) ^ tr.TID16 ^ tr.SID16 ^ low) << 16) | low; // force same shiny star type
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Shiny GetRareType(uint xor) => xor switch
|
|
|
|
{
|
|
|
|
0 => Shiny.AlwaysSquare,
|
|
|
|
< 16 => Shiny.AlwaysStar,
|
|
|
|
_ => Shiny.Never,
|
|
|
|
};
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static uint GetShinyXor(uint pid, uint id32)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
var xor = pid ^ id32;
|
2022-06-18 18:04:24 +00:00
|
|
|
return (xor ^ (xor >> 16)) & 0xFFFF;
|
2021-11-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
}
|