Misc api improvements

extract common nature amp logic
generic ienumerable copyto which now returns count of elements copied
(maybe useful later). prior usages never tried to copy an enumerable
larger than the dest array (now length checked).
This commit is contained in:
Kurt 2018-02-28 21:50:50 -08:00
parent 24982fe8c5
commit e4210e2b3e
3 changed files with 27 additions and 15 deletions

View file

@ -720,9 +720,8 @@ namespace PKHeX.Core
Stats[3] = (ushort)(((HT_SPE ? 31 : IV_SPE) + 2 * p.SPE + EV_SPE / 4) * level / 100 + 5);
// Account for nature
int incr = Nature / 5 + 1;
int decr = Nature % 5 + 1;
if (incr == decr || incr >= Stats.Length) return Stats;
if (PKX.GetNatureModification(Nature, out int incr, out int decr))
return Stats;
Stats[incr] *= 11; Stats[incr] /= 10;
Stats[decr] *= 9; Stats[decr] /= 10;
return Stats;

View file

@ -353,6 +353,20 @@ namespace PKHeX.Core
return 1;
return 2;
}
/// <summary>
/// Gets the nature modification values and checks if they are equal.
/// </summary>
/// <param name="nature">Nature</param>
/// <param name="incr">Increased stat</param>
/// <param name="decr">Decreased stat</param>
/// <returns>True if nature modification values are equal</returns>
public static bool GetNatureModification(int nature, out int incr, out int decr)
{
incr = nature / 5 + 1;
decr = nature % 5 + 1;
return incr == decr;
}
/// <summary>
/// Positions for shuffling.
@ -838,11 +852,17 @@ namespace PKHeX.Core
/// <param name="list">Source list to copy from</param>
/// <param name="dest">Destination list/array</param>
/// <param name="start">Starting point to copy to</param>
public static void CopyTo(this IEnumerable<PKM> list, IList<PKM> dest, int start = 0)
/// <returns>Count of <see cref="T"/> copied.</returns>
public static int CopyTo<T>(this IEnumerable<T> list, IList<T> dest, int start = 0)
{
int ctr = 0;
int ctr = start;
foreach (var z in list)
dest[start + ctr++] = z;
{
if (dest.Count <= ctr)
break;
dest[ctr++] = z;
}
return ctr - start;
}
/// <summary>

View file

@ -259,12 +259,8 @@ namespace PKHeX.WinForms.Controls
foreach (var label in L_Stats.Skip(1))
label.ResetForeColor();
// Recolor the Stat Labels based on boosted stats.
int incr = nature / 5 + 1;
int decr = nature % 5 + 1;
// Set Colored StatLabels only if Nature isn't Neutral
if (incr == decr || incr >= L_Stats.Length)
if (PKX.GetNatureModification(nature, out int incr, out int decr))
return;
L_Stats[incr].ForeColor = Color.Red;
L_Stats[decr].ForeColor = Color.Blue;
@ -275,11 +271,8 @@ namespace PKHeX.WinForms.Controls
foreach (var label in L_Stats.Skip(1))
label.ResetForeColor();
int incr = nature / 5 + 1;
int decr = nature % 5 + 1;
// Set Colored StatLabels only if Nature isn't Neutral
if (incr == decr || incr >= L_Stats.Length)
if (PKX.GetNatureModification(nature, out int incr, out int decr))
return "-/-";
return $"+{L_Stats[incr].Text} / -{L_Stats[decr].Text}".Replace(":", "");
}