using System;
using System.Runtime.CompilerServices;
namespace PKHeX.Core
{
///
/// Logic for calculating a Hidden Power Type based on IVs and generation-format.
///
public static class HiddenPower
{
///
/// Gets the current Hidden Power Type of the input for the requested format generation.
///
/// Current IVs
/// Hidden Power Type of the
/// Generation format
public static int GetType(ReadOnlySpan IVs, int format)
{
if (format <= 2)
return GetTypeGB(IVs);
return GetType(IVs);
}
///
/// Gets the current Hidden Power Type of the input for Generations 3+
///
/// Current IVs
/// Hidden Power Type of the
public static int GetType(ReadOnlySpan IVs)
{
int hp = 0;
for (int i = 0; i < 6; i++)
hp |= (IVs[i] & 1) << i;
hp *= 0xF;
hp /= 0x3F;
return hp;
}
///
/// Gets the current Hidden Power Type of the input for Generations 1 & 2
///
/// Current IVs
/// Hidden Power Type of the
public static int GetTypeGB(ReadOnlySpan IVs)
{
var atk = IVs[1];
var def = IVs[2];
return ((atk & 3) << 2) | (def & 3);
}
///
/// Modifies the provided to have the requested for Generations 1 & 2
///
/// Hidden Power Type
/// Current 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);
return true;
}
///
/// Modifies the provided to have the requested .
///
/// Hidden Power Type
/// Current IVs (6 total)
/// Generation format
/// True if the Hidden Power of the is obtained, with or without modifications
public static bool SetIVsForType(int hiddenPowerType, Span IVs, int format)
{
if (format <= 2)
return SetTypeGB(hiddenPowerType, IVs);
return SetIVsForType(hiddenPowerType, IVs);
}
///
/// Sets the to the requested for Generation 3+ game formats.
///
/// Hidden Power Type
/// Current IVs (6 total)
/// True if the Hidden Power of the is obtained, with or without modifications
public static bool SetIVsForType(int hpVal, Span IVs)
{
int flawlessCount = IVs.Count(31);
if (flawlessCount == 0)
return false;
if (flawlessCount == IVs.Length)
{
SetIVs(hpVal, IVs); // Get IVs
return true;
}
int current = GetType(IVs);
if (current == hpVal)
return true; // no mods necessary
// 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)
return false; // can't force hidden power?
// set IVs back to array
result.CopyTo(IVs);
return true;
}
// Non-recursive https://en.wikipedia.org/wiki/Heap%27s_algorithm
private static bool GetSuggestedHiddenPowerIVs(int hpVal, ReadOnlySpan original, Span ivs, Span best)
{
const int max = 31;
// 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];
int mutated = c.Length + 1; // result tracking
for (int i = 1; i < c.Length;)
{
if (c[i] >= i) // Reset the state and simulate popping the stack by incrementing the pointer.
{
c[i++] = 0;
continue;
}
var x = (i & 1) == 1 ? c[i] : 0;
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
}
c[i]++;
i = 1;
}
return mutated <= c.Length; // did we actually find a suitable result?
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Swap(ref T a, ref T b) => (a, b) = (b, a);
/// Calculate the Hidden Power Type of the entered IVs.
/// Hidden Power Type
/// Individual Values (H/A/B/S/C/D)
/// Generation specific format
public static void SetIVs(int type, Span ivs, int format = PKX.Generation)
{
if (format <= 2)
{
ivs[1] = (ivs[1] & ~3) | (type >> 2);
ivs[2] = (ivs[2] & ~3) | (type & 3);
}
else
{
var bits = DefaultLowBits[type];
for (int i = 0; i < 6; i++)
ivs[i] = (ivs[i] & 0x1E) + ((bits >> i) & 1);
}
}
///
/// Hidden Power IV values (even or odd) to achieve a specified Hidden Power Type
///
///
/// There are other IV combinations to achieve the same Hidden Power Type.
/// These are just precomputed for fast modification.
/// Individual Values (H/A/B/S/C/D)
///
public static readonly byte[] DefaultLowBits =
{
0b000011, // Fighting
0b001000, // Flying
0b001011, // Poison
0b001111, // Ground
0b010011, // Rock
0b011001, // Bug
0b011101, // Ghost
0b011111, // Steel
0b100101, // Fire
0b101001, // Water
0b101101, // Grass
0b101111, // Electric
0b110101, // Psychic
0b111001, // Ice
0b111101, // Dragon
0b111111, // Dark
};
}
}