PKHeX/PKHeX.Core/Editing/HiddenPower.cs

204 lines
7.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core
{
/// <summary>
/// Logic for calculating a Hidden Power Type based on IVs and generation-format.
/// </summary>
public static class HiddenPower
{
/// <summary>
/// Gets the current Hidden Power Type of the input <see cref="IVs"/> for the requested format generation.
/// </summary>
/// <param name="IVs">Current IVs</param>
/// <returns>Hidden Power Type of the <see cref="IVs"/></returns>
/// <param name="format">Generation format</param>
public static int GetType(IReadOnlyList<int> IVs, int format)
{
if (format <= 2)
return GetTypeGB(IVs);
return GetType(IVs);
}
2018-07-29 20:27:48 +00:00
/// <summary>
/// Gets the current Hidden Power Type of the input <see cref="IVs"/> for Generations 3+
/// </summary>
/// <param name="IVs">Current IVs</param>
/// <returns>Hidden Power Type of the <see cref="IVs"/></returns>
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-29 20:27:48 +00:00
/// <summary>
/// Gets the current Hidden Power Type of the input <see cref="IVs"/> for Generations 1 &amp; 2
/// </summary>
/// <param name="IVs">Current IVs</param>
/// <returns>Hidden Power Type of the <see cref="IVs"/></returns>
public static int GetTypeGB(IReadOnlyList<int> IVs)
{
var atk = IVs[1];
var def = IVs[2];
return ((atk & 3) << 2) | (def & 3);
}
2018-07-14 03:30:57 +00:00
/// <summary>
/// Modifies the provided <see cref="IVs"/> to have the requested <see cref="hiddenPowerType"/> for Generations 1 &amp; 2
/// </summary>
/// <param name="hiddenPowerType">Hidden Power Type</param>
/// <param name="IVs">Current IVs</param>
/// <returns>True if the Hidden Power of the <see cref="IVs"/> is obtained, with or without modifications</returns>
public static bool SetTypeGB(int hiddenPowerType, int[] IVs)
{
IVs[1] = (IVs[1] & ~3) | (hiddenPowerType >> 2);
IVs[2] = (IVs[2] & ~3) | (hiddenPowerType & 3);
return true;
}
/// <summary>
/// Modifies the provided <see cref="IVs"/> to have the requested <see cref="hiddenPowerType"/>.
/// </summary>
/// <param name="hiddenPowerType">Hidden Power Type</param>
/// <param name="IVs">Current IVs (6 total)</param>
/// <param name="format">Generation format</param>
/// <returns>True if the Hidden Power of the <see cref="IVs"/> is obtained, with or without modifications</returns>
public static bool SetIVsForType(int hiddenPowerType, int[] IVs, int format)
2018-07-14 03:30:57 +00:00
{
if (format <= 2)
return SetTypeGB(hiddenPowerType, IVs);
return SetIVsForType(hiddenPowerType, IVs);
2018-07-14 03:30:57 +00:00
}
2018-07-29 20:27:48 +00:00
/// <summary>
/// Sets the <see cref="IVs"/> to the requested <see cref="hpVal"/> for Generation 3+ game formats.
/// </summary>
/// <param name="hpVal">Hidden Power Type</param>
/// <param name="IVs">Current IVs (6 total)</param>
/// <returns>True if the Hidden Power of the <see cref="IVs"/> is obtained, with or without modifications</returns>
public static bool SetIVsForType(int hpVal, int[] IVs)
{
if (Array.TrueForAll(IVs, z => z == 31))
{
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.
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
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;
}
2018-07-29 20:27:48 +00:00
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
private static int[]? GetSuggestedHiddenPowerIVs(int hpVal, int[] IVs)
{
const int max = 31;
var flawless = new int[IVs.Length]; // future: stackalloc
int flawlessCount = 0;
for (int i = 0; i < IVs.Length; i++)
{
if (IVs[i] == max)
flawless[++flawlessCount] = i;
}
var permutations = GetPermutations(flawless, flawlessCount);
int flawedCount = 0; // result tracking
int[]? best = null; // result tracking
int[] ivs = (int[])IVs.Clone();
foreach (var permute in permutations)
{
foreach (var index in permute)
{
ivs[index] ^= 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 = (int[])ivs.Clone();
break; // any further flaws are always worse
}
// Restore IVs for another iteration
Buffer.BlockCopy(IVs, 0, ivs, 0, ivs.Length);
}
return best;
}
2018-07-29 20:27:48 +00:00
private static IEnumerable<IEnumerable<T>> GetPermutations<T>(ICollection<T> list, int length)
{
// https://stackoverflow.com/a/10630026
if ((uint)length <= 1)
return list.Select(t => new[] { t });
return GetPermutations(list, length - 1)
.SelectMany(list.Except, (t1, t2) => t1.Concat(new[] { t2 }));
}
/// <summary>Calculate the Hidden Power Type of the entered IVs.</summary>
/// <param name="type">Hidden Power Type</param>
/// <param name="ivs">Individual Values (H/A/B/S/C/D)</param>
/// <param name="format">Generation specific format</param>
/// <returns>Hidden Power Type</returns>
public static int[] SetIVs(int type, int[] ivs, int format = PKX.Generation)
{
if (format <= 2)
{
ivs[1] = (ivs[1] & ~3) | (type >> 2);
ivs[2] = (ivs[2] & ~3) | (type & 3);
return ivs;
}
var bits = DefaultLowBits[type];
for (int i = 0; i < 6; i++)
ivs[i] = (ivs[i] & 0x1E) + ((bits >> i) & 1);
return ivs;
}
/// <summary>
/// Hidden Power IV values (even or odd) to achieve a specified Hidden Power Type
/// </summary>
/// <remarks>
/// 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)
/// </remarks>
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
};
}
}