using System; namespace PKHeX.Core; /// /// Logic for working with Effort Values /// public static class EffortValues { /// Maximum value for a single stat in Generation 1/2 formats. public const ushort Max12 = ushort.MaxValue; /// Maximum value for a single stat in Generation 6+ formats. public const byte Max252 = 252; /// Maximum value for a single stat in Generation 3-5 formats. public const byte Max255 = 255; /// Maximum value for the sum of all stats in Generation 3+ formats. public const ushort Max510 = 510; /// Since EVs are effective in multiples of 4, the leftover EVs (2) have no impact regardless of stat gained. public const ushort MaxEffective = 508; /// The leftover EVs if two stats are . public const byte LeftoverDual252 = 6; /// Vitamin Max for consideration in Gen3 & Gen4. public const byte MaxVitamins34 = 100; /// Single vitamin in Gen1/2 adds 2560 EVs to a stat. public const ushort VitaminBoost12 = 2560; /// Maximum EVs from vitamins in Gen1/2. public const ushort MaxVitamins12 = 2560 * 10; /// Maximum value for a single stat in Pokémon Champions. public const byte ChampionsMaxStat = 32; // 252/8 /// Maximum value for the sum of all stats in Pokémon Champions. public const byte ChampionsMaxTotal = 66; /// Maximum value for the sum of all stats when transferred into Pokémon Champions from HOME. public const byte ChampionsMaxTotalTransfer = 64; // 508/8 /// /// Gets randomized EVs for a given generation format /// /// Array to store the resulting EVs /// Generation specific formatting option public static void SetRandom(Span evs, byte generation) { var rnd = Util.Rand; if (generation > 2) SetRandom252(evs, rnd); else SetRandom12(evs, rnd); } private static void SetRandom252(Span evs, Random rnd) { // Set random EVs (max 252 per stat) until we run out of EVs. // The last stat index receives the remaining EVs const int maxTotal = Max510; const int maxStat = Max252; const int maxStatPlusBias = 300; // weight more towards the high end of received EVs while (true) // loop until we get a valid set of 6 stats { int remain = maxTotal; for (int i = 0; i < evs.Length - 1; i++) { var max = Math.Min(maxStatPlusBias, remain); var amount = rnd.Next(0, max + 1); if (amount > maxStat) amount = maxStat; remain -= (evs[i] = (byte)amount); } if (remain > maxStat) continue; // try again! must have had really low rand rolls. evs[5] = remain; break; // done! } rnd.Shuffle(evs); } private static void SetRandom12(Span evs, Random rnd) { // In generation 1/2, EVs can be 0-65535. for (int i = 0; i < evs.Length; i++) evs[i] = rnd.Next(Max12 + 1); } /// /// Sets the Effort Values to a reasonable max value. /// /// Array to store the resulting EVs /// Entity that will eventually receive the EVs public static void SetMax(Span evs, PKM entity) { if (entity.Format < 3) SetMax12(evs); else SetMax252(evs, entity); } private static void SetMax252(Span evs, PKM entity) { // Get the 3 highest base stat indexes from the entity PersonalInfo var pi = entity.PersonalInfo; Span<(int Index, int Stat)> tuples = stackalloc (int, int)[6]; pi.GetSortedStatIndexes(tuples); evs[tuples[0].Index] = Max252; evs[tuples[1].Index] = Max252; evs[tuples[2].Index] = LeftoverDual252; } private static void SetMax12(Span evs) { for (int i = 0; i < evs.Length; i++) evs[i] = Max12; } /// /// Sets all the EVs to zero. /// /// Array to store the resulting EVs public static void Clear(Span evs) => evs.Clear(); /// /// Evaluates the total EVs and returns a grade based on the maximum allowed. /// /// Total EVs public static EffortValueGrade GetGrade(int sum) => sum switch { 0 => EffortValueGrade.None, <= 128 => EffortValueGrade.Quarter, <= 256 => EffortValueGrade.Half, < MaxEffective => EffortValueGrade.NearFull, MaxEffective => EffortValueGrade.MaxEffective, Max510 - 1 => EffortValueGrade.MaxNearCap, Max510 => EffortValueGrade.MaxLegal, _ => EffortValueGrade.Illegal, }; /// /// Converts Pokémon Champions EVs (0-32) to mainline EVs (0-252) by multiplying by 8 and applying the appropriate clamps. /// /// Champion's EVs (0-32) /// Mainline EVs (0-252) public static void ConvertFromChampions(ReadOnlySpan champion, Span mainline) { int total = 0; for (int i = 0; i < champion.Length; i++) { int ev = ConvertFromChampions(champion[i]); mainline[i] = ev; total += ev; // Ensure we don't exceed the natural clamp. if (total <= Max510) continue; mainline[i] -= total - Max510; break; } } /// /// Converts mainline EVs (0-252) to Pokémon Champions EVs (0-32) by dividing by 8. /// /// Mainline EVs (0-252) /// Champion's EVs (0-32) public static void ConvertToChampions(ReadOnlySpan mainline, Span champion) { for (int i = 0; i < champion.Length; i++) champion[i] = ConvertToChampions(mainline[i]); } /// /// Converts an EV from Pokémon Champions (0-32) to mainline (0-252) by multiplying by 8 and applying the appropriate clamps. /// public static int ConvertFromChampions(int ev) => Math.Clamp((ev * 8) - 4, 0, Max252); /// /// Converts an EV from mainline (0-252) to Pokémon Champions (0-32) by dividing by 8. /// public static int ConvertToChampions(int ev) => (ev + 4) / 8; /// /// Checks if the EVs are within the Pokémon Champions limits (0-32 per stat, 66 total) and also accounts for the transfer limit (64 total). /// /// Array of EVs to check. /// True if the EVs are within the Pokémon Champions "fully-entered" limits, false otherwise. public static bool IsChampions(ReadOnlySpan evs) { if (evs.ContainsAnyExceptInRange(0, ChampionsMaxStat)) return false; var sum = 0; foreach (var bp in evs) sum += bp; // Sets allow for an extra +2 stat values compared to mainline. Transferred sets can also be imported, albeit much less commonly seen. return sum is <= ChampionsMaxTotal and >= ChampionsMaxTotalTransfer; } } /// /// Assessment of the total EVs (Gen3+), compared to the maximum allowed. /// public enum EffortValueGrade { /// No EVs None, /// 1-128 EVs Quarter, /// 129-256 EVs Half, /// 257-508 EVs NearFull, /// 508 EVs MaxEffective, /// 509 EVs MaxNearCap, /// 510 EVs MaxLegal, /// 511+ EVs Illegal, }