From 659fc9978b03e97644e288b21450448a4d78242a Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 8 Jan 2022 22:48:33 -0800 Subject: [PATCH] Stackalloc marking set & raid IV gen --- .../Editing/Applicators/MarkingApplicator.cs | 9 +++-- PKHeX.Core/Legality/RNG/RaidRNG.cs | 33 ++++++++++++++----- PKHeX.Core/PKM/G8PKM.cs | 20 ++++++----- PKHeX.Core/PKM/PB7.cs | 20 ++++++----- PKHeX.Core/PKM/PK7.cs | 20 ++++++----- PKHeX.Core/PKM/PKM.cs | 20 ++++++----- PKHeX.Core/PKM/Util/PKX.cs | 3 +- 7 files changed, 75 insertions(+), 50 deletions(-) diff --git a/PKHeX.Core/Editing/Applicators/MarkingApplicator.cs b/PKHeX.Core/Editing/Applicators/MarkingApplicator.cs index fff876a7c..5189f9f57 100644 --- a/PKHeX.Core/Editing/Applicators/MarkingApplicator.cs +++ b/PKHeX.Core/Editing/Applicators/MarkingApplicator.cs @@ -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 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); } /// diff --git a/PKHeX.Core/Legality/RNG/RaidRNG.cs b/PKHeX.Core/Legality/RNG/RaidRNG.cs index 144ca9926..9a5d3da8d 100644 --- a/PKHeX.Core/Legality/RNG/RaidRNG.cs +++ b/PKHeX.Core/Legality/RNG/RaidRNG.cs @@ -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 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 IVs = stackalloc int[6]; + LoadIVs(raid, IVs); ApplyDetailsTo(pk8, seed, IVs, raid.FlawlessIVCount, abil, ratio); } + private static void LoadIVs(T raid, Span IVs) where T : EncounterStatic8Nest + { + 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 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 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) diff --git a/PKHeX.Core/PKM/G8PKM.cs b/PKHeX.Core/PKM/G8PKM.cs index 7b1f49667..c1a050a08 100644 --- a/PKHeX.Core/PKM/G8PKM.cs +++ b/PKHeX.Core/PKM/G8PKM.cs @@ -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 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); diff --git a/PKHeX.Core/PKM/PB7.cs b/PKHeX.Core/PKM/PB7.cs index 88ca60d31..a8f89fd7b 100644 --- a/PKHeX.Core/PKM/PB7.cs +++ b/PKHeX.Core/PKM/PB7.cs @@ -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 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) diff --git a/PKHeX.Core/PKM/PK7.cs b/PKHeX.Core/PKM/PK7.cs index 0e057ea26..5a6ea37b3 100644 --- a/PKHeX.Core/PKM/PK7.cs +++ b/PKHeX.Core/PKM/PK7.cs @@ -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 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() diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index b261a986c..b7bbb4bbd 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -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 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); diff --git a/PKHeX.Core/PKM/Util/PKX.cs b/PKHeX.Core/PKM/Util/PKX.cs index 6b046398e..33a84f706 100644 --- a/PKHeX.Core/PKM/Util/PKX.cs +++ b/PKHeX.Core/PKM/Util/PKX.cs @@ -290,13 +290,12 @@ namespace PKHeX.Core /// /// Input array to reorder /// Same array, reordered. - public static int[] ReorderSpeedLast(int[] value) + public static void ReorderSpeedLast(Span value) { var spe = value[3]; value[3] = value[4]; value[4] = value[5]; value[5] = spe; - return value; } } }