Stackalloc marking set & raid IV gen

This commit is contained in:
Kurt 2022-01-08 22:48:33 -08:00
parent 9cde291595
commit 659fc9978b
7 changed files with 75 additions and 50 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.Linq;
namespace PKHeX.Core
{
@ -24,8 +23,12 @@ namespace PKHeX.Core
if (pk.Format <= 3)
return; // no markings (gen3 only has 4; can't mark stats intelligently
var markings = ivs.Select(MarkingMethod(pk)).ToArray(); // future: stackalloc
pk.Markings = PKX.ReorderSpeedLast(markings);
Span<int> markings = stackalloc int[ivs.Length];
var method = MarkingMethod(pk);
for (int i = 0; i < markings.Length; i++)
markings[i] = method(ivs[i], i);
PKX.ReorderSpeedLast(markings);
pk.SetMarkings(markings);
}
/// <summary>

View file

@ -1,4 +1,4 @@
using System.Linq;
using System;
using System.Runtime.CompilerServices;
namespace PKHeX.Core
@ -13,7 +13,9 @@ namespace PKHeX.Core
var pi = PersonalTable.SWSH.GetFormEntry(raid.Species, raid.Form);
var ratio = pi.Gender;
var abil = RemapAbilityToParam(raid.Ability);
var IVs = raid.IVs.Count == 0 ? GetBlankIVTemplate() : PKX.ReorderSpeedLast((int[])((int[])raid.IVs).Clone());
Span<int> IVs = stackalloc int[6];
LoadIVs(raid, IVs);
return Verify(pk8, seed, IVs, raid.FlawlessIVCount, abil, ratio);
}
@ -25,10 +27,25 @@ namespace PKHeX.Core
var pi = PersonalTable.SWSH.GetFormEntry(raid.Species, raid.Form);
var ratio = pi.Gender;
var abil = RemapAbilityToParam(raid.Ability);
var IVs = raid.IVs.Count == 0 ? GetBlankIVTemplate() : PKX.ReorderSpeedLast((int[])((int[])raid.IVs).Clone());
Span<int> IVs = stackalloc int[6];
LoadIVs(raid, IVs);
ApplyDetailsTo(pk8, seed, IVs, raid.FlawlessIVCount, abil, ratio);
}
private static void LoadIVs<T>(T raid, Span<int> IVs) where T : EncounterStatic8Nest<T>
{
if (raid.IVs.Count == 0)
{
IVs.Fill(-1);
}
else
{
((int[])raid.IVs).CopyTo(IVs);
PKX.ReorderSpeedLast(IVs);
}
}
private static int RemapAbilityToParam(AbilityPermission a) => a switch
{
AbilityPermission.Any12H => 254,
@ -36,9 +53,7 @@ namespace PKHeX.Core
_ => a.GetSingleValue(),
};
private static int[] GetBlankIVTemplate() => new[] {-1, -1, -1, -1, -1, -1};
private static bool Verify(PKM pk, ulong seed, int[] ivs, int iv_count, int ability_param, int gender_ratio, sbyte nature_param = -1, Shiny shiny = Shiny.Random)
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();
@ -67,7 +82,7 @@ namespace PKHeX.Core
const int UNSET = -1;
const int MAX = 31;
for (int i = ivs.Count(z => z == MAX); i < iv_count; i++)
for (int i = ivs.Count(MAX); i < iv_count; i++)
{
int index = (int)rng.NextInt(6);
while (ivs[index] != UNSET)
@ -188,7 +203,7 @@ namespace PKHeX.Core
}
}
private static bool ApplyDetailsTo(PKM pk, ulong seed, int[] ivs, int iv_count, int ability_param, int gender_ratio, sbyte nature_param = -1, Shiny shiny = Shiny.Random)
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();
@ -223,7 +238,7 @@ namespace PKHeX.Core
const int UNSET = -1;
const int MAX = 31;
for (int i = ivs.Count(z => z == MAX); i < iv_count; i++)
for (int i = ivs.Count(MAX); i < iv_count; i++)
{
int index = (int)rng.NextInt(6);
while (ivs[index] != UNSET)

View file

@ -489,15 +489,17 @@ namespace PKHeX.Core
marks[i] = ((val >> (i * 2)) & 3) % 3;
return marks;
}
set
{
if (value.Length > 8)
return;
int v = 0;
for (int i = 0; i < value.Length; i++)
v |= (value[i] % 3) << (i * 2);
MarkValue = v;
}
set => SetMarkings(value);
}
public override void SetMarkings(ReadOnlySpan<int> value)
{
if (value.Length > 8)
return;
int v = 0;
for (int i = 0; i < value.Length; i++)
v |= (value[i] % 3) << (i * 2);
MarkValue = v;
}
public bool GetRibbon(int index) => FlagUtil.GetFlag(Data, GetRibbonByte(index), index & 7);

View file

@ -316,15 +316,17 @@ namespace PKHeX.Core
marks[i] = ((val >> (i * 2)) & 3) % 3;
return marks;
}
set
{
if (value.Length > 8)
return;
int v = 0;
for (int i = 0; i < value.Length; i++)
v |= (value[i] % 3) << (i * 2);
MarkValue = v;
}
set => SetMarkings(value);
}
public override void SetMarkings(ReadOnlySpan<int> value)
{
if (value.Length > 8)
return;
int v = 0;
for (int i = 0; i < value.Length; i++)
v |= (value[i] % 3) << (i * 2);
MarkValue = v;
}
protected override bool TradeOT(ITrainerInfo tr)

View file

@ -437,15 +437,17 @@ namespace PKHeX.Core
marks[i] = ((val >> (i*2)) & 3) % 3;
return marks;
}
set
{
if (value.Length > 8)
return;
int v = 0;
for (int i = 0; i < value.Length; i++)
v |= (value[i] % 3) << (i*2);
MarkValue = v;
}
set => SetMarkings(value);
}
public override void SetMarkings(ReadOnlySpan<int> value)
{
if (value.Length > 8)
return;
int v = 0;
for (int i = 0; i < value.Length; i++)
v |= (value[i] % 3) << (i * 2);
MarkValue = v;
}
public void FixMemories()

View file

@ -458,15 +458,17 @@ namespace PKHeX.Core
mark[i] = (MarkValue >> i) & 1;
return mark;
}
set
{
if (value.Length > 8)
return;
byte b = 0;
for (int i = 0; i < value.Length; i++)
b |= (byte)(Math.Min(value[i], 1) << i);
MarkValue = b;
}
set => SetMarkings(value);
}
public virtual void SetMarkings(ReadOnlySpan<int> value)
{
if (value.Length > 8)
return;
byte b = 0;
for (int i = 0; i < value.Length; i++)
b |= (byte)(Math.Min(value[i], 1) << i);
MarkValue = b;
}
private int HPBitValPower => ((IV_HP & 2) >> 1) | ((IV_ATK & 2) >> 0) | ((IV_DEF & 2) << 1) | ((IV_SPE & 2) << 2) | ((IV_SPA & 2) << 3) | ((IV_SPD & 2) << 4);

View file

@ -290,13 +290,12 @@ namespace PKHeX.Core
/// </summary>
/// <param name="value">Input array to reorder</param>
/// <returns>Same array, reordered.</returns>
public static int[] ReorderSpeedLast(int[] value)
public static void ReorderSpeedLast(Span<int> value)
{
var spe = value[3];
value[3] = value[4];
value[4] = value[5];
value[5] = spe;
return value;
}
}
}