using System;
namespace PKHeX.Core;
public interface IHyperTrain
{
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; }
}
public static partial class Extensions
{
///
/// Toggles the Hyper Training flag for a given stat.
///
/// Hyper Trainable object
/// Battle Stat (H/A/B/S/C/D)
/// Final Hyper Training Flag value
public static bool HyperTrainInvert(this IHyperTrain t, int index) => index switch
{
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,
};
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;
///
/// Gets one of the values based on its index within the array.
///
/// Entity to check.
/// Index to get
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)),
};
///
/// Sets to valid values which may best enhance the stats.
///
///
/// History of evolutions present as
/// to use (if already known). Will fetch the current if not provided.
public static void SetSuggestedHyperTrainingData(this PKM pk, EvolutionHistory h, ReadOnlySpan IVs)
{
if (pk is not IHyperTrain t)
return;
if (!pk.IsHyperTrainingAvailable(h))
{
t.HyperTrainFlags = 0;
return;
}
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;
// sometimes unusual speed IVs are desirable for competitive reasons
// if nothing else was HT'd and the IV isn't too high, it was probably intentional
t.HT_SPE = IVs[3] != 31 && IVs[3] > 2 &&
(IVs[3] > 17 || t.HT_HP || t.HT_ATK || t.HT_DEF || t.HT_SPA || t.HT_SPD);
if (pk is ICombatPower pb)
pb.ResetCP();
}
///
public static void SetSuggestedHyperTrainingData(this PKM pk, ReadOnlySpan IVs) => pk.SetSuggestedHyperTrainingData(EvolutionHistory.Empty, IVs);
///
public static void SetSuggestedHyperTrainingData(this PKM pk)
{
Span ivs = stackalloc int[6];
pk.GetIVs(ivs);
pk.SetSuggestedHyperTrainingData(ivs);
}
///
/// Indicates if Hyper Training is available for toggling.
///
/// Entity to train
/// History of evolutions present as
/// True if available, otherwise false.
public static bool IsHyperTrainingAvailable(this IHyperTrain t, EvolutionHistory h) => t switch
{
// Check for game formats where training is unavailable:
PA8 pa8 => h.Gen7.Length > 0 || pa8.HasVisitedSWSH(h.Gen8) || pa8.HasVisitedBDSP(h.Gen8b),
_ => true,
};
///
/// Entity data
/// History of evolutions present as
public static bool IsHyperTrainingAvailable(this PKM pk, EvolutionHistory h)
{
if (pk is not IHyperTrain t)
return false;
if (!t.IsHyperTrainingAvailable(h))
return false;
// Gated behind level 100.
return pk.CurrentLevel == 100;
}
}