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
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool Verify<T>(this T raid, PK8 pk8, ulong seed) where T: EncounterStatic8Nest<T>
|
2020-01-23 08:11:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var pi = PersonalTable.SWSH.GetFormEntry(raid.Species, raid.Form);
|
|
|
|
var ratio = pi.Gender;
|
|
|
|
var abil = RemapAbilityToParam(raid.Ability);
|
2022-01-09 06:48:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
Span<int> IVs = stackalloc int[6];
|
|
|
|
LoadIVs(raid, IVs);
|
|
|
|
return Verify(pk8, seed, IVs, raid.FlawlessIVCount, abil, ratio);
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void ApplyDetailsTo<T>(this T raid, PK8 pk8, ulong seed) where T : EncounterStatic8Nest<T>
|
|
|
|
{
|
|
|
|
// Ensure the species-form is set correctly (nature)
|
|
|
|
pk8.Species = raid.Species;
|
|
|
|
pk8.Form = raid.Form;
|
|
|
|
var pi = PersonalTable.SWSH.GetFormEntry(raid.Species, raid.Form);
|
|
|
|
var ratio = pi.Gender;
|
|
|
|
var abil = RemapAbilityToParam(raid.Ability);
|
|
|
|
|
|
|
|
Span<int> IVs = stackalloc int[6];
|
|
|
|
LoadIVs(raid, IVs);
|
|
|
|
ApplyDetailsTo(pk8, seed, IVs, raid.FlawlessIVCount, abil, ratio);
|
|
|
|
}
|
|
|
|
|
2022-08-22 00:34:32 +00:00
|
|
|
private static void LoadIVs<T>(T raid, Span<int> span) where T : EncounterStatic8Nest<T>
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-22 00:34:32 +00:00
|
|
|
// Template stores with speed in middle (standard), convert for generator purpose.
|
|
|
|
var ivs = raid.IVs;
|
|
|
|
if (ivs.IsSpecified)
|
|
|
|
ivs.CopyToSpeedLast(span);
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2022-08-22 00:34:32 +00:00
|
|
|
span.Fill(-1);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2022-01-09 06:48:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static int RemapAbilityToParam(AbilityPermission a) => a switch
|
|
|
|
{
|
|
|
|
AbilityPermission.Any12H => 254,
|
|
|
|
AbilityPermission.Any12 => 255,
|
|
|
|
_ => a.GetSingleValue(),
|
|
|
|
};
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool Verify(PKM pk, ulong seed, Span<int> ivs, int iv_count, int ability_param, int gender_ratio, sbyte nature_param = -1, Shiny shiny = Shiny.Random)
|
|
|
|
{
|
|
|
|
var rng = new Xoroshiro128Plus(seed);
|
|
|
|
var ec = (uint)rng.NextInt();
|
|
|
|
if (ec != pk.EncryptionConstant)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint pid;
|
|
|
|
bool isShiny;
|
|
|
|
if (shiny == Shiny.Random) // let's decide if it's shiny or not!
|
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();
|
|
|
|
isShiny = GetShinyXor(pid, trID) < 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no need to calculate a fake trainer
|
|
|
|
pid = (uint)rng.NextInt();
|
|
|
|
isShiny = shiny == Shiny.Always;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
ForceShinyState(pk, isShiny, ref pid);
|
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;
|
|
|
|
for (int i = ivs.Count(MAX); i < iv_count; i++)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
int abil = ability_param switch
|
|
|
|
{
|
|
|
|
254 => (int)rng.NextInt(3),
|
|
|
|
255 => (int)rng.NextInt(2),
|
|
|
|
_ => ability_param,
|
|
|
|
};
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (current != 4)
|
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
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
switch (gender_ratio)
|
|
|
|
{
|
|
|
|
case PersonalInfo.RatioMagicGenderless when pk.Gender != 2:
|
|
|
|
if (pk.Gender != 2)
|
2020-10-10 19:59:31 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
break;
|
|
|
|
case PersonalInfo.RatioMagicFemale when pk.Gender != 1:
|
|
|
|
if (pk.Gender != 1)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case PersonalInfo.RatioMagicMale:
|
|
|
|
if (pk.Gender != 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
var gender = (int)rng.NextInt(252) + 1 < gender_ratio ? 1 : 0;
|
|
|
|
if (pk.Gender != gender)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-11-26 03:55:44 +00:00
|
|
|
int nature = nature_param != -1 ? nature_param
|
|
|
|
: pk.Species == (int)Species.Toxtricity
|
|
|
|
? 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);
|
|
|
|
if (s.HeightScalar != height)
|
|
|
|
return false;
|
|
|
|
var weight = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
|
|
|
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)]
|
|
|
|
public static void ForceShinyState(PKM pk, bool isShiny, ref uint pid)
|
|
|
|
{
|
|
|
|
if (isShiny)
|
2021-06-04 00:50:48 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!GetIsShiny(pk.TID, pk.SID, pid))
|
|
|
|
pid = GetShinyPID(pk.TID, pk.SID, pid, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetIsShiny(pk.TID, pk.SID, pid))
|
|
|
|
pid ^= 0x1000_0000;
|
2021-06-04 00:50:48 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool ApplyDetailsTo(PKM pk, ulong seed, Span<int> ivs, int iv_count, int ability_param, int gender_ratio, sbyte nature_param = -1, Shiny shiny = Shiny.Random)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
if (shiny == Shiny.Random) // let's decide if it's shiny or not!
|
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();
|
|
|
|
isShiny = GetShinyXor(pid, trID) < 16;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no need to calculate a fake trainer
|
|
|
|
pid = (uint)rng.NextInt();
|
|
|
|
isShiny = shiny == Shiny.Always;
|
|
|
|
}
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (isShiny)
|
|
|
|
{
|
|
|
|
if (!GetIsShiny(pk.TID, pk.SID, pid))
|
|
|
|
pid = GetShinyPID(pk.TID, pk.SID, pid, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetIsShiny(pk.TID, pk.SID, pid))
|
|
|
|
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;
|
|
|
|
for (int i = ivs.Count(MAX); i < iv_count; i++)
|
|
|
|
{
|
|
|
|
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
|
|
|
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
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int abil = ability_param switch
|
|
|
|
{
|
|
|
|
254 => (int)rng.NextInt(3),
|
|
|
|
255 => (int)rng.NextInt(2),
|
|
|
|
_ => ability_param,
|
|
|
|
};
|
|
|
|
pk.RefreshAbility(abil);
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Gender = gender_ratio switch
|
|
|
|
{
|
|
|
|
PersonalInfo.RatioMagicGenderless => 2,
|
|
|
|
PersonalInfo.RatioMagicFemale => 1,
|
|
|
|
PersonalInfo.RatioMagicMale => 0,
|
|
|
|
_ => (int) rng.NextInt(252) + 1 < gender_ratio ? 1 : 0,
|
|
|
|
};
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-11-26 03:55:44 +00:00
|
|
|
int nature = nature_param != -1 ? nature_param
|
|
|
|
: pk.Species == (int)Species.Toxtricity
|
|
|
|
? ToxtricityUtil.GetRandomNature(ref rng, pk.Form)
|
|
|
|
: (byte)rng.NextInt(25);
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.StatNature = pk.Nature = nature;
|
2020-01-23 08:11:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is IScaledSize s)
|
2020-01-23 08:11:07 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var height = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
|
|
|
var weight = (int)rng.NextInt(0x81) + (int)rng.NextInt(0x80);
|
|
|
|
s.HeightScalar = (byte)height;
|
|
|
|
s.WeightScalar = (byte)weight;
|
2020-01-23 08:11:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static uint GetShinyPID(int tid, int sid, uint pid, int type)
|
|
|
|
{
|
|
|
|
return (uint) (((tid ^ sid ^ (pid & 0xFFFF) ^ type) << 16) | (pid & 0xFFFF));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool GetIsShiny(int tid, int sid, uint pid)
|
|
|
|
{
|
|
|
|
return GetShinyXor(pid, (uint) ((sid << 16) | tid)) < 16;
|
2020-01-23 08:11:07 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private static uint GetShinyXor(uint pid, uint oid)
|
|
|
|
{
|
|
|
|
var xor = pid ^ oid;
|
|
|
|
return (xor ^ (xor >> 16)) & 0xFFFF;
|
|
|
|
}
|
2022-03-06 21:03:39 +00:00
|
|
|
}
|