2022-06-27 03:29:15 +00:00
|
|
|
using System;
|
2022-03-06 08:43:49 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2018-03-11 02:03:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logic for calculating a Hidden Power Type based on IVs and generation-format.
|
|
|
|
/// </summary>
|
|
|
|
public static class HiddenPower
|
2018-03-11 02:03:09 +00:00
|
|
|
{
|
2019-02-24 21:57:10 +00:00
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Gets the current Hidden Power Type of the input <see cref="IVs"/> for the requested format generation.
|
2019-02-24 21:57:10 +00:00
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="IVs">Current IVs</param>
|
|
|
|
/// <returns>Hidden Power Type of the <see cref="IVs"/></returns>
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
/// <param name="context">Generation format</param>
|
|
|
|
public static int GetType(ReadOnlySpan<int> IVs, EntityContext context)
|
2018-03-11 02:03:09 +00:00
|
|
|
{
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
if (context.Generation() <= 2)
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetTypeGB(IVs);
|
|
|
|
return GetType(IVs);
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +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(ReadOnlySpan<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
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the current Hidden Power Type of the input <see cref="IVs"/> for Generations 1 & 2
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="IVs">Current IVs</param>
|
|
|
|
/// <returns>Hidden Power Type of the <see cref="IVs"/></returns>
|
|
|
|
public static int GetTypeGB(ReadOnlySpan<int> IVs)
|
|
|
|
{
|
|
|
|
var atk = IVs[1];
|
|
|
|
var def = IVs[2];
|
|
|
|
return ((atk & 3) << 2) | (def & 3);
|
|
|
|
}
|
2018-07-14 03:30:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Modifies the provided <see cref="IVs"/> to have the requested <see cref="hiddenPowerType"/> for Generations 1 & 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, Span<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>
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
/// <param name="context">Generation format</param>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <returns>True if the Hidden Power of the <see cref="IVs"/> is obtained, with or without modifications</returns>
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
public static bool SetIVsForType(int hiddenPowerType, Span<int> IVs, EntityContext context)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
if (context.Generation() <= 2)
|
2022-06-18 18:04:24 +00:00
|
|
|
return SetTypeGB(hiddenPowerType, IVs);
|
|
|
|
return SetIVsForType(hiddenPowerType, IVs);
|
|
|
|
}
|
2021-01-16 20:01:40 +00:00
|
|
|
|
2022-06-18 18:04:24 +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, Span<int> IVs)
|
|
|
|
{
|
|
|
|
int flawlessCount = IVs.Count(31);
|
|
|
|
if (flawlessCount == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (flawlessCount == IVs.Length)
|
2018-07-14 03:30:57 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
SetIVs(hpVal, IVs); // Get IVs
|
|
|
|
return true;
|
2018-07-14 03:30:57 +00:00
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int current = GetType(IVs);
|
|
|
|
if (current == hpVal)
|
|
|
|
return true; // no mods necessary
|
2022-03-06 08:43:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Required HP type doesn't match IVs. Make currently-flawless IVs flawed.
|
|
|
|
Span<int> scratch = stackalloc int[IVs.Length];
|
|
|
|
Span<int> result = stackalloc int[IVs.Length];
|
|
|
|
var success = GetSuggestedHiddenPowerIVs(hpVal, IVs, scratch, result);
|
|
|
|
if (!success)
|
|
|
|
return false; // can't force hidden power?
|
2018-03-11 02:03:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// set IVs back to array
|
|
|
|
result.CopyTo(IVs);
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-11 02:03:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Non-recursive https://en.wikipedia.org/wiki/Heap%27s_algorithm
|
|
|
|
private static bool GetSuggestedHiddenPowerIVs(int hpVal, ReadOnlySpan<int> original, Span<int> ivs, Span<int> best)
|
|
|
|
{
|
|
|
|
const int max = 31;
|
2018-03-11 02:03:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Get a list of indexes that can be mutated
|
|
|
|
Span<int> indexes = stackalloc int[original.Length];
|
|
|
|
int flaw = 0;
|
|
|
|
for (int i = 0; i < original.Length; i++)
|
|
|
|
{
|
|
|
|
if (original[i] == max)
|
|
|
|
indexes[flaw++] = i;
|
2018-03-11 02:03:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
indexes = indexes[..flaw];
|
|
|
|
Span<int> c = stackalloc int[indexes.Length];
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int mutated = c.Length + 1; // result tracking
|
|
|
|
for (int i = 1; i < c.Length;)
|
2018-03-11 02:03:09 +00:00
|
|
|
{
|
2022-06-27 03:29:15 +00:00
|
|
|
ref int ci = ref c[i];
|
|
|
|
if (i <= ci) // Reset the state and simulate popping the stack by incrementing the pointer.
|
2021-05-07 06:26:38 +00:00
|
|
|
{
|
2022-06-27 03:29:15 +00:00
|
|
|
ci = 0;
|
2022-06-29 17:49:39 +00:00
|
|
|
++i;
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2021-05-07 06:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 03:29:15 +00:00
|
|
|
var x = (i & 1) * ci; // if lowest bit set, ci : 0 (branch-less)
|
2022-06-18 18:04:24 +00:00
|
|
|
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++)
|
2018-03-11 02:03:09 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
ivs[indexes[j]] ^= 1;
|
|
|
|
if (hpVal != GetType(ivs))
|
2022-03-06 08:43:49 +00:00
|
|
|
continue;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
var ct = j + 1;
|
|
|
|
if (ct >= mutated)
|
2018-03-11 02:03:09 +00:00
|
|
|
break; // any further flaws are always worse
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
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
|
2022-03-06 08:43:49 +00:00
|
|
|
}
|
2018-03-11 02:03:09 +00:00
|
|
|
|
2022-06-27 03:29:15 +00:00
|
|
|
ci++;
|
2022-06-18 18:04:24 +00:00
|
|
|
i = 1;
|
2018-03-11 02:03:09 +00:00
|
|
|
}
|
2019-03-16 19:07:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return mutated <= c.Length; // did we actually find a suitable result?
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
private static void Swap<T>(ref T a, ref T b) => (a, b) = (b, a);
|
2022-03-06 08:43:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <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>
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
/// <param name="context">Generation specific format</param>
|
|
|
|
public static void SetIVs(int type, Span<int> ivs, EntityContext context = PKX.Context)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves
In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.
The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.
The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.
* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
|
|
|
if (context.Generation() <= 2)
|
2019-03-16 19:07:22 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
ivs[1] = (ivs[1] & ~3) | (type >> 2);
|
|
|
|
ivs[2] = (ivs[2] & ~3) | (type & 3);
|
2019-03-16 19:07:22 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2019-03-16 19:07:22 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var bits = DefaultLowBits[type];
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
ivs[i] = (ivs[i] & 0x1E) + ((bits >> i) & 1);
|
|
|
|
}
|
2018-03-11 02:03:09 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <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
|
|
|
|
};
|
2018-03-11 02:03:09 +00:00
|
|
|
}
|