2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2018-06-06 04:31:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
2022-08-21 08:39:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Exposes flags to indicate if a stat index has been hyper trained.
|
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
public interface IHyperTrain
|
2018-06-06 04:31:42 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
byte HyperTrainFlags { get; set; }
|
|
|
|
bool HT_HP { get; set; }
|
|
|
|
bool HT_ATK { get; set; }
|
|
|
|
bool HT_DEF { get; set; }
|
|
|
|
bool HT_SPA { get; set; }
|
|
|
|
bool HT_SPD { get; set; }
|
|
|
|
bool HT_SPE { get; set; }
|
|
|
|
}
|
2018-06-06 04:31:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static partial class Extensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Toggles the Hyper Training flag for a given stat.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="t">Hyper Trainable object</param>
|
|
|
|
/// <param name="index">Battle Stat (H/A/B/S/C/D)</param>
|
|
|
|
/// <returns>Final Hyper Training Flag value</returns>
|
|
|
|
public static bool HyperTrainInvert(this IHyperTrain t, int index) => index switch
|
2018-06-06 04:31:42 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
0 => t.HT_HP ^= true,
|
|
|
|
1 => t.HT_ATK ^= true,
|
|
|
|
2 => t.HT_DEF ^= true,
|
|
|
|
3 => t.HT_SPE ^= true,
|
|
|
|
4 => t.HT_SPA ^= true,
|
|
|
|
5 => t.HT_SPD ^= true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2018-06-06 04:31:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool IsHyperTrainedAll(this IHyperTrain t) => t.HyperTrainFlags == 0x3F;
|
|
|
|
public static void HyperTrainClear(this IHyperTrain t) => t.HyperTrainFlags = 0;
|
|
|
|
public static bool IsHyperTrained(this IHyperTrain t) => t.HyperTrainFlags != 0;
|
2018-07-29 20:27:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets one of the <see cref="IHyperTrain"/> values based on its index within the array.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="t">Entity to check.</param>
|
|
|
|
/// <param name="index">Index to get</param>
|
|
|
|
public static bool IsHyperTrained(this IHyperTrain t, int index) => index switch
|
|
|
|
{
|
|
|
|
0 => t.HT_HP,
|
|
|
|
1 => t.HT_ATK,
|
|
|
|
2 => t.HT_DEF,
|
|
|
|
3 => t.HT_SPE,
|
|
|
|
4 => t.HT_SPA,
|
|
|
|
5 => t.HT_SPD,
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(index)),
|
|
|
|
};
|
2018-06-06 04:31:42 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Sets <see cref="IHyperTrain.HyperTrainFlags"/> to valid values which may best enhance the <see cref="PKM"/> stats.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pk"></param>
|
|
|
|
/// <param name="h">History of evolutions present as</param>
|
|
|
|
/// <param name="IVs"><see cref="PKM.IVs"/> to use (if already known). Will fetch the current <see cref="PKM.IVs"/> if not provided.</param>
|
|
|
|
public static void SetSuggestedHyperTrainingData(this PKM pk, EvolutionHistory h, ReadOnlySpan<int> IVs)
|
|
|
|
{
|
|
|
|
if (pk is not IHyperTrain t)
|
|
|
|
return;
|
2023-01-22 04:02:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pk.IsHyperTrainingAvailable(h))
|
|
|
|
t.HyperTrainFlags = 0;
|
2023-01-22 04:02:33 +00:00
|
|
|
else
|
|
|
|
t.SetSuggestedHyperTrainingData(pk, IVs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc cref="SetSuggestedHyperTrainingData(PKM,EvolutionHistory,ReadOnlySpan{int})"/>
|
|
|
|
public static void SetSuggestedHyperTrainingData(this PKM pk, EntityContext c, ReadOnlySpan<int> IVs)
|
|
|
|
{
|
|
|
|
if (pk is not IHyperTrain t)
|
2022-06-18 18:04:24 +00:00
|
|
|
return;
|
2018-06-06 04:31:42 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
if (!c.IsHyperTrainingAvailable(pk.CurrentLevel))
|
|
|
|
t.HyperTrainFlags = 0;
|
|
|
|
else
|
|
|
|
t.SetSuggestedHyperTrainingData(pk, IVs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void SetSuggestedHyperTrainingData(this IHyperTrain t, PKM pk, ReadOnlySpan<int> IVs)
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
t.HT_HP = IVs[0] != 31;
|
|
|
|
t.HT_ATK = IVs[1] != 31 && IVs[1] > 2;
|
|
|
|
t.HT_DEF = IVs[2] != 31;
|
|
|
|
t.HT_SPA = IVs[4] != 31;
|
|
|
|
t.HT_SPD = IVs[5] != 31;
|
2023-01-22 04:02:33 +00:00
|
|
|
t.HT_SPE = IsSpeedHyperTrainSuggested(t, IVs);
|
2020-08-15 04:49:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is ICombatPower pb)
|
|
|
|
pb.ResetCP();
|
|
|
|
}
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
private static bool IsSpeedHyperTrainSuggested(IHyperTrain t, ReadOnlySpan<int> IVs)
|
|
|
|
{
|
|
|
|
// sometimes unusual speed IVs are desirable for competitive reasons
|
|
|
|
var speed = IVs[3];
|
|
|
|
if (speed is 31 or <= 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// if IV isn't too high and nothing else was HT'd, it was probably intentional to stay low.
|
|
|
|
if (speed > 17)
|
|
|
|
return true;
|
|
|
|
return t.HT_HP || t.HT_ATK || t.HT_DEF || t.HT_SPA || t.HT_SPD;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <inheritdoc cref="SetSuggestedHyperTrainingData(PKM,EvolutionHistory,ReadOnlySpan{int})"/>
|
2023-01-22 04:02:33 +00:00
|
|
|
public static void SetSuggestedHyperTrainingData(this PKM pk, ReadOnlySpan<int> IVs)
|
|
|
|
{
|
|
|
|
pk.SetSuggestedHyperTrainingData(pk.Context, IVs);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/// <inheritdoc cref="SetSuggestedHyperTrainingData(PKM,EvolutionHistory,ReadOnlySpan{int})"/>
|
|
|
|
public static void SetSuggestedHyperTrainingData(this PKM pk)
|
|
|
|
{
|
|
|
|
Span<int> ivs = stackalloc int[6];
|
|
|
|
pk.GetIVs(ivs);
|
|
|
|
pk.SetSuggestedHyperTrainingData(ivs);
|
|
|
|
}
|
2022-06-12 16:26:45 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
2023-01-22 04:02:33 +00:00
|
|
|
/// Indicates if Hyper Training is available to be set.
|
2022-06-18 18:04:24 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="t">Entity to train</param>
|
|
|
|
/// <param name="h">History of evolutions present as</param>
|
|
|
|
/// <returns>True if available, otherwise false.</returns>
|
|
|
|
public static bool IsHyperTrainingAvailable(this IHyperTrain t, EvolutionHistory h) => t switch
|
|
|
|
{
|
|
|
|
// Check for game formats where training is unavailable:
|
2022-08-04 01:17:46 +00:00
|
|
|
PA8 => h.HasVisitedGen7 || h.HasVisitedSWSH || h.HasVisitedBDSP,
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => true,
|
|
|
|
};
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
/// <inheritdoc cref="IsHyperTrainingAvailable(IHyperTrain,EvolutionHistory)"/>
|
|
|
|
public static bool IsHyperTrainingAvailable(this EntityContext c, int currentLevel)
|
|
|
|
{
|
|
|
|
var min = GetHyperTrainMinLevel(c);
|
|
|
|
return currentLevel <= min;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc cref="GetHyperTrainMinLevel(IHyperTrain,EvolutionHistory)"/>
|
|
|
|
public static int GetHyperTrainMinLevel(this EntityContext c) => c switch
|
|
|
|
{
|
|
|
|
EntityContext.Gen7 or EntityContext.Gen8 or EntityContext.Gen8b => 100,
|
|
|
|
EntityContext.Gen9 => 50,
|
|
|
|
_ => 101,
|
|
|
|
};
|
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
/// <summary>
|
2023-01-22 04:02:33 +00:00
|
|
|
/// Gets the minimum level required for Hyper Training an IV.
|
2022-11-25 01:42:17 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="_">Entity to train</param>
|
|
|
|
/// <param name="h">History of evolutions present as</param>
|
|
|
|
/// <returns>True if available, otherwise false.</returns>
|
|
|
|
public static int GetHyperTrainMinLevel(this IHyperTrain _, EvolutionHistory h)
|
|
|
|
{
|
|
|
|
if (h.HasVisitedGen9)
|
|
|
|
return 50;
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <inheritdoc cref="IsHyperTrainingAvailable(IHyperTrain, EvolutionHistory)"/>
|
|
|
|
/// <param name="pk">Entity data</param>
|
|
|
|
/// <param name="h">History of evolutions present as</param>
|
|
|
|
public static bool IsHyperTrainingAvailable(this PKM pk, EvolutionHistory h)
|
|
|
|
{
|
|
|
|
if (pk is not IHyperTrain t)
|
|
|
|
return false;
|
|
|
|
if (!t.IsHyperTrainingAvailable(h))
|
|
|
|
return false;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
// Gated behind level.
|
|
|
|
var min = t.GetHyperTrainMinLevel(h);
|
|
|
|
return pk.CurrentLevel >= min;
|
2018-06-06 04:31:42 +00:00
|
|
|
}
|
|
|
|
}
|