2022-08-22 00:34:32 +00:00
|
|
|
using System;
|
2021-06-04 00:50:48 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logic for Generating and Verifying Gen8 Raid Templates against PKM data.
|
|
|
|
/// </summary>
|
|
|
|
public static class RaidRNG
|
2020-01-23 08:11:07 +00:00
|
|
|
{
|
2023-08-15 03:01:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Verify a Raid Seed against a PKM.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pk">Entity to verify against</param>
|
|
|
|
/// <param name="seed">Seed that generated the entity</param>
|
|
|
|
/// <param name="ivs">Buffer of IVs (potentially with already specified values)</param>
|
2023-08-30 05:22:23 +00:00
|
|
|
/// <param name="param">Parameters to generate with</param>
|
2023-08-15 03:01:38 +00:00
|
|
|
/// <param name="forceNoShiny">Force the entity to be non-shiny via special handling</param>
|
|
|
|
/// <returns>True if the seed matches the entity</returns>
|
2023-08-30 05:22:23 +00:00
|
|
|
public static bool Verify(PKM pk, ulong seed, Span<int> ivs, in GenerateParam8 param, bool forceNoShiny = false)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var rng = new Xoroshiro128Plus(seed);
|
|
|
|
var ec = (uint)rng.NextInt();
|
|
|
|
if (ec != pk.EncryptionConstant)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint pid;
|
|
|
|
bool isShiny;
|
2020-01-23 08:11:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var trID = (uint)rng.NextInt();
|
|
|
|
pid = (uint)rng.NextInt();
|
2023-08-15 03:01:38 +00:00
|
|
|
var xor = GetShinyXor(pid, trID);
|
|
|
|
isShiny = xor < 16;
|
|
|
|
if (isShiny && forceNoShiny)
|
|
|
|
{
|
|
|
|
ForceShinyState(false, ref pid, trID);
|
|
|
|
isShiny = false;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-15 03:01:38 +00:00
|
|
|
ForceShinyState(isShiny, ref pid, pk.ID32);
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.PID != pid)
|
|
|
|
return false;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
const int UNSET = -1;
|
|
|
|
const int MAX = 31;
|
2023-08-30 05:22:23 +00:00
|
|
|
if (param.IVs.IsSpecified)
|
|
|
|
param.IVs.CopyToSpeedLast(ivs);
|
|
|
|
else
|
|
|
|
ivs.Fill(UNSET);
|
|
|
|
|
|
|
|
for (int i = ivs.Count(MAX); i < param.FlawlessIVs; i++)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
int index = (int)rng.NextInt(6);
|
|
|
|
while (ivs[index] != UNSET)
|
|
|
|
index = (int)rng.NextInt(6);
|
|
|
|
ivs[index] = MAX;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
if (ivs[i] == UNSET)
|
|
|
|
ivs[i] = (int)rng.NextInt(32);
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.IV_HP != ivs[0])
|
|
|
|
return false;
|
|
|
|
if (pk.IV_ATK != ivs[1])
|
|
|
|
return false;
|
|
|
|
if (pk.IV_DEF != ivs[2])
|
|
|
|
return false;
|
|
|
|
if (pk.IV_SPA != ivs[3])
|
|
|
|
return false;
|
|
|
|
if (pk.IV_SPD != ivs[4])
|
|
|
|
return false;
|
|
|
|
if (pk.IV_SPE != ivs[5])
|
|
|
|
return false;
|
|
|
|
|
2023-08-30 05:22:23 +00:00
|
|
|
int abil = param.Ability switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-30 05:22:23 +00:00
|
|
|
AbilityPermission.Any12H => (int)rng.NextInt(3),
|
|
|
|
AbilityPermission.Any12 => (int)rng.NextInt(2),
|
|
|
|
_ => param.Ability.GetSingleValue(),
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
|
|
|
abil <<= 1; // 1/2/4
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var current = pk.AbilityNumber;
|
|
|
|
if (abil == 4)
|
|
|
|
{
|
2023-08-15 03:01:38 +00:00
|
|
|
if (current != 4 && pk is PK8)
|
2020-01-23 08:11:07 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
// else, for things that were made Hidden Ability, defer to Ability Checks (Ability Patch)
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-30 05:22:23 +00:00
|
|
|
switch (param.GenderRatio)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-15 03:01:38 +00:00
|
|
|
case PersonalInfo.RatioMagicGenderless:
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != 2)
|
2020-10-10 19:59:31 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
break;
|
2023-08-15 03:01:38 +00:00
|
|
|
case PersonalInfo.RatioMagicFemale:
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Gender != 1)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case PersonalInfo.RatioMagicMale:
|
|
|
|
if (pk.Gender != 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
2023-08-30 05:22:23 +00:00
|
|
|
var gender = (int)rng.NextInt(253) + 1 < param.GenderRatio ? 1 : 0;
|
2023-08-15 03:01:38 +00:00
|
|
|
if (pk.Gender != gender && pk.Gender != 2) // allow Nincada(0/1)->Shedinja(2)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-30 05:22:23 +00:00
|
|
|
int nature = param.Nature != Nature.Random ? (int)param.Nature
|
|
|
|
: param.Species == (int)Species.Toxtricity
|
2022-11-26 03:55:44 +00:00
|
|
|
? ToxtricityUtil.GetRandomNature(ref rng, pk.Form)
|
|
|
|
: (byte)rng.NextInt(25);
|
|
|
|
if (pk.Nature != nature)
|
|
|
|
return false;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is IScaledSize s)
|
|
|
|
{
|
|
|
|
var height = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
|
|
|
var weight = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
2023-06-04 01:19:16 +00:00
|
|
|
if (height == 0 && weight == 0 && pk is IHomeTrack { HasTracker: true})
|
|
|
|
{
|
|
|
|
// HOME rerolls height/weight if both are 0
|
|
|
|
// This behavior started in 3.0.0, so only flag if the context is 9 or above.
|
|
|
|
if (pk.Context is not (EntityContext.Gen8 or EntityContext.Gen8a or EntityContext.Gen8b))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (s.HeightScalar != height)
|
|
|
|
return false;
|
|
|
|
if (s.WeightScalar != weight)
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-08-15 03:01:38 +00:00
|
|
|
private static void ForceShinyState(bool isShiny, ref uint pid, uint tid)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (isShiny)
|
2021-06-04 00:50:48 +00:00
|
|
|
{
|
2023-08-15 03:01:38 +00:00
|
|
|
if (!GetIsShiny(tid, pid))
|
|
|
|
pid = GetShinyPID((ushort)(tid & 0xFFFFu), (ushort)(tid >> 16), pid, 0);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-15 03:01:38 +00:00
|
|
|
if (GetIsShiny(tid, pid))
|
2022-06-18 18:04:24 +00:00
|
|
|
pid ^= 0x1000_0000;
|
2021-06-04 00:50:48 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 03:01:38 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Apply the details to the PKM
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pk">Entity to verify against</param>
|
|
|
|
/// <param name="seed">Seed that generated the entity</param>
|
|
|
|
/// <param name="ivs">Buffer of IVs (potentially with already specified values)</param>
|
2023-08-30 05:22:23 +00:00
|
|
|
/// <param name="param">Parameters to generate with</param>
|
|
|
|
/// <param name="criteria">Criteria to generate with</param>
|
2023-08-15 03:01:38 +00:00
|
|
|
/// <returns>True if the seed matches the entity</returns>
|
2023-08-30 05:22:23 +00:00
|
|
|
public static bool TryApply(PK8 pk, ulong seed, Span<int> ivs, in GenerateParam8 param, EncounterCriteria criteria)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var rng = new Xoroshiro128Plus(seed);
|
|
|
|
pk.EncryptionConstant = (uint)rng.NextInt();
|
2021-06-04 00:50:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
uint pid;
|
|
|
|
bool isShiny;
|
2020-01-23 08:11:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var trID = (uint)rng.NextInt();
|
|
|
|
pid = (uint)rng.NextInt();
|
2023-08-15 03:01:38 +00:00
|
|
|
var xor = GetShinyXor(pid, trID);
|
|
|
|
isShiny = xor < 16;
|
2023-08-30 05:22:23 +00:00
|
|
|
if (isShiny && param.Shiny == Shiny.Never)
|
2023-08-15 03:01:38 +00:00
|
|
|
{
|
|
|
|
ForceShinyState(false, ref pid, trID);
|
|
|
|
isShiny = false;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (isShiny)
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!GetIsShiny(pk.ID32, pid))
|
|
|
|
pid = GetShinyPID(pk.TID16, pk.SID16, pid, 0);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
if (GetIsShiny(pk.ID32, pid))
|
2022-06-18 18:04:24 +00:00
|
|
|
pid ^= 0x1000_0000;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.PID = pid;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
const int UNSET = -1;
|
|
|
|
const int MAX = 31;
|
2023-08-30 05:22:23 +00:00
|
|
|
if (param.IVs.IsSpecified)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-30 05:22:23 +00:00
|
|
|
param.IVs.CopyToSpeedLast(ivs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ivs.Fill(UNSET);
|
|
|
|
for (int i = ivs.Count(MAX); i < param.FlawlessIVs; i++)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
do { index = (int)rng.NextInt(6); }
|
|
|
|
while (ivs[index] != UNSET);
|
|
|
|
ivs[index] = MAX;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
if (ivs[i] == UNSET)
|
2023-08-31 02:08:00 +00:00
|
|
|
ivs[i] = (int)rng.NextInt(MAX + 1);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-31 02:08:00 +00:00
|
|
|
if (!param.IVs.IsSpecified && !criteria.IsIVsCompatibleSpeedLast(ivs, 8))
|
2023-08-30 05:22:23 +00:00
|
|
|
return false;
|
|
|
|
|
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];
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-30 05:22:23 +00:00
|
|
|
int abil = param.Ability switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-08-30 05:22:23 +00:00
|
|
|
AbilityPermission.Any12H => (int)rng.NextInt(3),
|
|
|
|
AbilityPermission.Any12 => (int)rng.NextInt(2),
|
|
|
|
_ => param.Ability.GetSingleValue(),
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
|
|
|
pk.RefreshAbility(abil);
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-30 05:22:23 +00:00
|
|
|
var gender = param.GenderRatio switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
PersonalInfo.RatioMagicGenderless => 2,
|
|
|
|
PersonalInfo.RatioMagicFemale => 1,
|
|
|
|
PersonalInfo.RatioMagicMale => 0,
|
2023-08-30 05:22:23 +00:00
|
|
|
_ => (int) rng.NextInt(253) + 1 < param.GenderRatio ? 1 : 0,
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
2023-08-30 05:22:23 +00:00
|
|
|
if (criteria.Gender != FixedGenderUtil.GenderRandom && gender != criteria.Gender)
|
|
|
|
return false;
|
|
|
|
pk.Gender = gender;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-30 05:22:23 +00:00
|
|
|
int nature = param.Nature != Nature.Random ? (byte)param.Nature
|
|
|
|
: param.Species == (int)Species.Toxtricity
|
2022-11-26 03:55:44 +00:00
|
|
|
? ToxtricityUtil.GetRandomNature(ref rng, pk.Form)
|
|
|
|
: (byte)rng.NextInt(25);
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-17 07:07:54 +00:00
|
|
|
pk.Nature = pk.StatNature = nature;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2023-08-15 03:01:38 +00:00
|
|
|
var height = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
|
|
|
var weight = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
|
|
|
pk.HeightScalar = (byte)height;
|
|
|
|
pk.WeightScalar = (byte)weight;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static uint GetShinyPID(ushort tid, ushort sid, uint pid, uint type)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
return (type ^ tid ^ sid ^ (pid & 0xFFFF)) << 16 | (pid & 0xFFFF);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static bool GetIsShiny(uint id32, uint pid) => GetShinyXor(pid, id32) < 16;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2022-03-06 21:03:39 +00:00
|
|
|
}
|