2018-03-11 02:03:09 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
public static class HiddenPower
|
|
|
|
|
{
|
2018-07-13 01:16:24 +00:00
|
|
|
|
public static int GetType(IReadOnlyList<int> IVs, int format)
|
|
|
|
|
{
|
|
|
|
|
if (format <= 2)
|
|
|
|
|
return GetTypeGB(IVs);
|
|
|
|
|
return GetType(IVs);
|
|
|
|
|
}
|
2018-03-11 02:03:09 +00:00
|
|
|
|
public static int GetType(IReadOnlyList<int> IVs)
|
|
|
|
|
{
|
|
|
|
|
int hp = 0;
|
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
|
hp |= (IVs[i] & 1) << i;
|
|
|
|
|
hp *= 0xF;
|
|
|
|
|
hp /= 0x3F;
|
|
|
|
|
return hp;
|
|
|
|
|
}
|
2018-07-13 01:16:24 +00:00
|
|
|
|
public static int GetTypeGB(IReadOnlyList<int> IVs)
|
|
|
|
|
{
|
|
|
|
|
var IV_ATK = IVs[1];
|
|
|
|
|
var IV_DEF = IVs[2];
|
|
|
|
|
return ((IV_ATK & 3) << 2) | (IV_DEF & 3);
|
|
|
|
|
}
|
2018-07-14 03:30:57 +00:00
|
|
|
|
|
|
|
|
|
public static bool SetIVsForType(int hpVal, int[] IVs, int format)
|
|
|
|
|
{
|
|
|
|
|
if (format <= 2)
|
|
|
|
|
{
|
|
|
|
|
IVs[1] = (IVs[1] & ~3) | (hpVal >> 2);
|
|
|
|
|
IVs[2] = (IVs[2] & ~3) | (hpVal & 3);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return SetIVsForType(hpVal, IVs);
|
|
|
|
|
}
|
2018-03-11 02:03:09 +00:00
|
|
|
|
public static bool SetIVsForType(int hpVal, int[] IVs)
|
|
|
|
|
{
|
|
|
|
|
if (IVs.All(z => z == 31))
|
|
|
|
|
{
|
|
|
|
|
PKX.SetHPIVs(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.
|
|
|
|
|
int[] best = GetSuggestedHiddenPowerIVs(hpVal, IVs);
|
|
|
|
|
if (best == null)
|
|
|
|
|
return false; // can't force hidden power?
|
|
|
|
|
|
|
|
|
|
// set IVs back to array
|
|
|
|
|
for (int i = 0; i < IVs.Length; i++)
|
|
|
|
|
IVs[i] = best[i];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private static int[] GetSuggestedHiddenPowerIVs(int hpVal, int[] IVs)
|
|
|
|
|
{
|
|
|
|
|
var flawless = IVs.Select((v, i) => v == 31 ? i : -1).Where(v => v != -1).ToArray();
|
|
|
|
|
var permutations = GetPermutations(flawless, flawless.Length);
|
|
|
|
|
int flawedCount = 0;
|
|
|
|
|
int[] best = null;
|
|
|
|
|
foreach (var permute in permutations)
|
|
|
|
|
{
|
|
|
|
|
var ivs = (int[])IVs.Clone();
|
|
|
|
|
foreach (var item in permute)
|
|
|
|
|
{
|
|
|
|
|
ivs[item] ^= 1;
|
|
|
|
|
if (hpVal != GetType(ivs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
int ct = ivs.Count(z => z == 31);
|
|
|
|
|
if (ct <= flawedCount)
|
|
|
|
|
break; // any further flaws are always worse
|
|
|
|
|
|
|
|
|
|
flawedCount = ct;
|
|
|
|
|
best = ivs;
|
|
|
|
|
break; // any further flaws are always worse
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return best;
|
|
|
|
|
}
|
2018-06-09 23:04:06 +00:00
|
|
|
|
private static IEnumerable<IEnumerable<T>> GetPermutations<T>(ICollection<T> list, int length)
|
2018-03-11 02:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
// https://stackoverflow.com/a/10630026
|
|
|
|
|
if (length == 1)
|
|
|
|
|
return list.Select(t => new[] { t });
|
|
|
|
|
|
|
|
|
|
return GetPermutations(list, length - 1)
|
2018-06-09 23:04:06 +00:00
|
|
|
|
.SelectMany(list.Except, (t1, t2) => t1.Concat(new[] { t2 }));
|
2018-03-11 02:03:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|