diff --git a/PKHeX.Core/Editing/HiddenPower.cs b/PKHeX.Core/Editing/HiddenPower.cs index 14cb9a703..a7d2ec862 100644 --- a/PKHeX.Core/Editing/HiddenPower.cs +++ b/PKHeX.Core/Editing/HiddenPower.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; namespace PKHeX.Core; @@ -67,8 +66,8 @@ public static int GetTypeGB(ReadOnlySpan IVs) /// True if the Hidden Power of the is obtained, with or without modifications public static bool SetTypeGB(int hiddenPowerType, Span IVs) { - IVs[1] = (IVs[1] & ~3) | (hiddenPowerType >> 2); - IVs[2] = (IVs[2] & ~3) | (hiddenPowerType & 3); + IVs[1] = (IVs[1] & 0b1100) | (hiddenPowerType >> 2); + IVs[2] = (IVs[2] & 0b1100) | (hiddenPowerType & 3); return true; } @@ -109,76 +108,56 @@ public static bool SetIVsForType(int hpVal, Span IVs) } // Required HP type doesn't match IVs. Make currently-flawless IVs flawed. - Span scratch = stackalloc int[IVs.Length]; - Span result = stackalloc int[IVs.Length]; - var success = GetSuggestedHiddenPowerIVs(hpVal, IVs, scratch, result); - if (!success) + var bits = GetSuggestedHiddenPowerIVs(hpVal, IVs); + if (bits == NoResult) return false; // can't force hidden power? // set IVs back to array - result.CopyTo(IVs); + ForceLowBits(IVs, bits); return true; } - // Non-recursive https://en.wikipedia.org/wiki/Heap%27s_algorithm - private static bool GetSuggestedHiddenPowerIVs(int hpVal, ReadOnlySpan original, Span ivs, Span best) + private const byte NoResult = byte.MaxValue; + + private static byte GetSuggestedHiddenPowerIVs(int hpVal, ReadOnlySpan IVs) { - const int max = 31; + // Iterate through all bit combinations that yield our Hidden Power Type. + // There's at most 5 we need to check, so brute force is fine. + // Prefer the least amount of IVs changed (31 -> 30). - // Get a list of indexes that can be mutated - Span indexes = stackalloc int[original.Length]; - int flaw = 0; - for (int i = 0; i < original.Length; i++) - { - if (original[i] == max) - indexes[flaw++] = i; - } - indexes = indexes[..flaw]; - Span c = stackalloc int[indexes.Length]; + // Get the starting index from our 64 possible bit states. + int index = SixBitType.IndexOf((byte)hpVal); + if (index == -1) + return NoResult; - int mutated = c.Length + 1; // result tracking - for (int i = 1; i < c.Length;) + var bestIndex = NoResult; + var bestIndexFlaws = 6; + do { - ref int ci = ref c[i]; - if (i <= ci) // Reset the state and simulate popping the stack by incrementing the pointer. - { - ci = 0; - ++i; + var flaws = GetFlawedBitCount(IVs, index); + if (flaws >= bestIndexFlaws) continue; - } - - var x = (i & 1) * ci; // if lowest bit set, ci : 0 (branch-less) - Swap(ref indexes[i], ref indexes[x]); - - // Inlined continuance check - original.CopyTo(ivs); - var q = Math.Min(indexes.Length, mutated); - for (var j = 0; j < q; j++) - { - ivs[indexes[j]] ^= 1; - if (hpVal != GetType(ivs)) - continue; - - var ct = j + 1; - if (ct >= mutated) - break; // any further flaws are always worse - - mutated = ct; - ivs.CopyTo(best); - if (j == 0) // nothing will be better than only 1 flaw - return true; - break; // any further flaws are always worse - } - - ci++; - i = 1; - } - - return mutated <= c.Length; // did we actually find a suitable result? + bestIndex = (byte)index; + bestIndexFlaws = flaws; + } while (++index < SixBitType.Length && SixBitType[index] == hpVal); + return bestIndex; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void Swap(ref T a, ref T b) => (a, b) = (b, a); + private static int GetFlawedBitCount(ReadOnlySpan ivs, int bitValue) + { + const int max = 31; + int flaws = 0; + for (int i = 0; i < ivs.Length; i++) + { + var iv = ivs[i]; + if ((iv & 1) == (bitValue & (1 << i))) + continue; // ok + if (iv != max) + return NoResult; + flaws++; + } + return flaws; + } /// Calculate the Hidden Power Type of the entered IVs. /// Hidden Power Type @@ -188,17 +167,21 @@ public static void SetIVs(int type, Span ivs, EntityContext context = PKX.C { if (context.Generation() <= 2) { - ivs[1] = (ivs[1] & ~3) | (type >> 2); - ivs[2] = (ivs[2] & ~3) | (type & 3); + ivs[1] = (ivs[1] & 0b1100) | (type >> 2); + ivs[2] = (ivs[2] & 0b1100) | (type & 3); } else { - var bits = DefaultLowBits[type]; - for (int i = 0; i < 6; i++) - ivs[i] = (ivs[i] & 0x1E) + ((bits >> i) & 1); + ForceLowBits(ivs, DefaultLowBits[type]); } } + private static void ForceLowBits(Span ivs, byte bits) + { + for (int i = 0; i < ivs.Length; i++) + ivs[i] = (ivs[i] & 0b11110) | ((bits >> i) & 1); + } + /// /// Hidden Power IV values (even or odd) to achieve a specified Hidden Power Type ///