Extract EffortValues class, add $rand + $suggest

Renames IV/EV verifier field objects for Legality Checks due to class name clashing.
This commit is contained in:
Kurt 2022-05-06 15:47:54 -07:00
parent be9c947dd6
commit 59ceec3c65
8 changed files with 123 additions and 57 deletions

View file

@ -38,6 +38,7 @@ namespace PKHeX.Core
new TypeSuggestion<PKM>(nameof(PKM.Move4_PP), p => p.SetSuggestedMovePP(3)),
new ComplexSuggestion(nameof(PKM.Moves), (_, _, info) => BatchModifications.SetMoves(info.Entity, info.Legality.GetMoveSet())),
new ComplexSuggestion(nameof(PKM.EVs), (_, _, info) => BatchModifications.SetEVs(info.Entity)),
new ComplexSuggestion(nameof(PKM.RelearnMoves), (_, value, info) => BatchModifications.SetSuggestedRelearnData(info, value)),
new ComplexSuggestion(PROP_RIBBONS, (_, value, info) => BatchModifications.SetSuggestedRibbons(info, value)),
new ComplexSuggestion(nameof(PKM.Met_Location), (_, _, info) => BatchModifications.SetSuggestedMetData(info)),
@ -66,6 +67,7 @@ namespace PKHeX.Core
new ComplexSet(nameof(PKM.EncryptionConstant), value => value == CONST_RAND, (pk, _) => pk.EncryptionConstant = Util.Rand32()),
new ComplexSet(nameof(PKM.PID), value => value == CONST_RAND, (pk, _) => pk.PID = Util.Rand32()),
new ComplexSet(nameof(PKM.Gender), value => value == CONST_RAND, (pk, _) => pk.SetPIDGender(pk.Gender)),
new ComplexSet(nameof(PKM.EVs), value => value == CONST_RAND, (pk, _) => SetRandomEVs(pk)),
// Shiny
new ComplexSet(nameof(PKM.PID),
@ -76,6 +78,13 @@ namespace PKHeX.Core
new ComplexSet(nameof(PKM.IsNicknamed), value => string.Equals(value, "false", StringComparison.OrdinalIgnoreCase), (pk, _) => pk.SetDefaultNickname()),
};
private static void SetRandomEVs(PKM pk)
{
Span<int> evs = stackalloc int[6];
EffortValues.SetRandom(evs, pk.Format);
pk.SetEVs(evs);
}
private static Shiny GetRequestedShinyState(string text)
{
if (text.EndsWith("0"))

View file

@ -90,13 +90,21 @@ namespace PKHeX.Core
/// </summary>
/// <param name="pk">Pokémon to modify.</param>
/// <param name="moves">Moves to apply.</param>
public static ModifyResult SetMoves(PKM pk, int[] moves)
public static ModifyResult SetMoves(PKM pk, Span<int> moves)
{
pk.SetMoves(moves);
pk.HealPP();
return ModifyResult.Modified;
}
public static ModifyResult SetEVs(PKM pk)
{
Span<int> evs = stackalloc int[6];
EffortValues.SetMax(evs, pk);
pk.SetEVs(evs);
return ModifyResult.Modified;
}
/// <summary>
/// Sets the contests stats as requested.
/// </summary>

View file

@ -272,8 +272,8 @@ namespace PKHeX.Core
LanguageIndex.Verify(this);
Trainer.Verify(this);
TrainerID.Verify(this);
IndividualValues.Verify(this);
EffortValues.Verify(this);
IVs.Verify(this);
EVs.Verify(this);
Level.Verify(this);
Ribbon.Verify(this);
AbilityValues.Verify(this);

View file

@ -7,8 +7,8 @@
{
public static readonly LanguageVerifier LanguageIndex = new();
public static readonly NicknameVerifier Nickname = new();
public static readonly EffortValueVerifier EffortValues = new();
public static readonly IndividualValueVerifier IndividualValues = new();
public static readonly EffortValueVerifier EVs = new();
public static readonly IndividualValueVerifier IVs = new();
public static readonly BallVerifier BallIndex = new();
public static readonly FormVerifier FormValues = new();
public static readonly ConsoleRegionVerifier ConsoleRegion = new();

View file

@ -424,6 +424,18 @@ namespace PKHeX.Core
value[5] = EV_SPD;
}
public void SetEVs(ReadOnlySpan<int> value)
{
if (value.Length != 6)
return;
EV_HP = value[0];
EV_ATK = value[1];
EV_DEF = value[2];
EV_SPE = value[3];
EV_SPA = value[4];
EV_SPD = value[5];
}
public int[] Stats
{
get => new[] { Stat_HPCurrent, Stat_ATK, Stat_DEF, Stat_SPE, Stat_SPA, Stat_SPD };

View file

@ -0,0 +1,76 @@
using System;
using System.Linq;
namespace PKHeX.Core;
public static class EffortValues
{
/// <summary>
/// Gets randomized EVs for a given generation format
/// </summary>
/// <param name="generation">Generation specific formatting option</param>
/// <returns>Array containing randomized EVs (H/A/B/S/C/D)</returns>
public static int[] GetRandom(int generation = PKX.Generation)
{
var evs = new int[6];
SetRandom(evs, generation);
return evs;
}
public static void SetRandom(Span<int> evs, int generation)
{
var rnd = Util.Rand;
if (generation > 2)
SetRandom252(evs, rnd);
else
SetRandom12(evs, rnd);
}
private static void SetRandom252(Span<int> evs, Random rnd)
{
do
{
int max = 510;
for (int i = 0; i < evs.Length - 1; i++)
max -= evs[i] = (byte)Math.Min(rnd.Next(Math.Min(300, max)), 252);
evs[5] = max;
} while (evs[5] > 252);
Util.Shuffle(evs, 0, evs.Length, rnd);
}
private static void SetRandom12(Span<int> evs, Random rnd)
{
for (int i = 0; i < evs.Length; i++)
evs[i] = rnd.Next(ushort.MaxValue + 1);
}
public static void SetMax(Span<int> evs, PKM entity)
{
if (entity.Format < 3)
SetMax12(evs);
else
SetMax252(evs, entity);
}
private static void SetMax252(Span<int> evs, PKM entity)
{
var stats = entity.PersonalInfo.Stats;
var ordered = stats
.Select((z, i) => (Stat: z, Index: i))
.OrderByDescending(z => z.Stat)
.ToArray();
evs[ordered[0].Index] = 252;
evs[ordered[1].Index] = 252;
evs[ordered[2].Index] = 6;
}
private static void SetMax12(Span<int> evs)
{
for (int i = 0; i < evs.Length; i++)
evs[i] = ushort.MaxValue;
}
public static void Clear(Span<int> values) => values.Clear();
}

View file

@ -32,36 +32,6 @@ namespace PKHeX.Core
/// <returns>A <see cref="bool"/> indicating whether or not the length is valid for a <see cref="PKM"/>.</returns>
public static bool IsPKM(long len) => Sizes.Contains((int)len);
/// <summary>
/// Gets randomized EVs for a given generation format
/// </summary>
/// <param name="generation">Generation specific formatting option</param>
/// <returns>Array containing randomized EVs (H/A/B/S/C/D)</returns>
public static int[] GetRandomEVs(int generation = Generation)
{
var rnd = Util.Rand;
if (generation > 2)
{
var evs = new int[6];
do
{
int max = 510;
for (int i = 0; i < evs.Length - 1; i++)
max -= evs[i] = (byte)Math.Min(rnd.Next(Math.Min(300, max)), 252);
evs[5] = max;
} while (evs[5] > 252);
Util.Shuffle(evs.AsSpan());
return evs;
}
else
{
var evs = new int[6];
for (int i = 0; i < evs.Length; i++)
evs[i] = rnd.Next(ushort.MaxValue + 1);
return evs;
}
}
/// <summary>
/// Translates a Gender string to Gender integer.
/// </summary>

View file

@ -253,30 +253,21 @@ namespace PKHeX.WinForms.Controls
private void UpdateRandomEVs(object sender, EventArgs e)
{
var evs = ModifierKeys switch
Span<int> values = stackalloc int[6];
switch (ModifierKeys)
{
Keys.Control => SetMaxEVs(Entity),
Keys.Alt => new int[6],
_ => PKX.GetRandomEVs(Entity.Format),
};
LoadEVs(evs);
UpdateEVs(sender, EventArgs.Empty);
static int[] SetMaxEVs(PKM entity)
{
if (entity.Format < 3)
return Enumerable.Repeat((int) ushort.MaxValue, 6).ToArray();
var stats = entity.PersonalInfo.Stats;
var ordered = stats.Select((z, i) => new {Stat = z, Index = i}).OrderByDescending(z => z.Stat).ToArray();
var result = new int[6];
result[ordered[0].Index] = 252;
result[ordered[1].Index] = 252;
result[ordered[2].Index] = 6;
return result;
case Keys.Control:
EffortValues.SetMax(values, Entity);
break;
case Keys.Alt:
EffortValues.Clear(values);
break;
default:
EffortValues.SetRandom(values, Entity.Format);
break;
}
LoadEVs(values);
UpdateEVs(sender, EventArgs.Empty);
}
private void UpdateHackedStats(object sender, EventArgs e)