Make the template IV/contest stat array readonly

See prior commits
no longer need to clone subarrays, they're shared immutable
This commit is contained in:
Kurt 2020-01-18 19:11:29 -08:00
parent 643c0c1cc7
commit 63ea57436f
6 changed files with 34 additions and 37 deletions

View file

@ -30,10 +30,10 @@ namespace PKHeX.Core
public bool Gift { get; set; }
public int Ball { get; set; } = 4; // Only checked when is Gift
public GameVersion Version { get; set; } = GameVersion.Any;
public int[] IVs { get; set; } = Array.Empty<int>();
public IReadOnlyList<int> IVs { get; set; } = Array.Empty<int>();
public int FlawlessIVCount { get; set; }
public int[] Contest { set => this.SetContestStats(value); }
internal IReadOnlyList<int> Contest { set => this.SetContestStats(value); }
public int CNT_Cool { get; set; }
public int CNT_Beauty { get; set; }
public int CNT_Cute { get; set; }
@ -50,18 +50,7 @@ namespace PKHeX.Core
public bool Roaming { get; set; }
public bool EggEncounter => EggLocation > 0;
private void CloneArrays()
{
// dereference original arrays with new copies
IVs = IVs.Length == 0 ? IVs : (int[])IVs.Clone();
}
internal virtual EncounterStatic Clone()
{
var result = (EncounterStatic)MemberwiseClone();
result.CloneArrays();
return result;
}
internal EncounterStatic Clone() => (EncounterStatic)MemberwiseClone();
private const string _name = "Static Encounter";
public string Name => _name;
@ -211,7 +200,7 @@ namespace PKHeX.Core
protected void SetIVs(PKM pk)
{
if (IVs.Length != 0)
if (IVs.Count != 0)
pk.SetRandomIVs(IVs, FlawlessIVCount);
else if (FlawlessIVCount > 0)
pk.SetRandomIVs(flawless: FlawlessIVCount);
@ -296,7 +285,7 @@ namespace PKHeX.Core
private bool IsMatchIVs(PKM pkm)
{
if (IVs.Length == 0)
if (IVs.Count == 0)
return true; // nothing to check, IVs are random
if (Generation <= 2 && pkm.Format > 2)
return true; // IVs are regenerated on VC transfer upward

View file

@ -25,7 +25,7 @@ namespace PKHeX.Core
public int TID { get; set; }
public int SID { get; set; }
public GameVersion Version { get; set; } = GameVersion.Any;
public int[] IVs { get; set; } = Array.Empty<int>();
public IReadOnlyList<int> IVs { get; set; } = Array.Empty<int>();
public int Form { get; set; }
public virtual Shiny Shiny { get; set; } = Shiny.Never;
public int Gender { get; set; } = -1;
@ -36,7 +36,7 @@ namespace PKHeX.Core
public int Ball { get; set; } = 4;
public int CurrentLevel { get; set; } = -1;
public int[] Contest { set => this.SetContestStats(value); }
internal IReadOnlyList<int> Contest { set => this.SetContestStats(value); }
public int CNT_Cool { get; set; }
public int CNT_Beauty { get; set; }
public int CNT_Cute { get; set; }
@ -59,12 +59,12 @@ namespace PKHeX.Core
public bool Fateful { get; set; }
public bool IsNicknamed { get; set; } = true;
public string[] Nicknames { get; internal set; } = Array.Empty<string>();
public string[] TrainerNames { get; internal set; } = Array.Empty<string>();
public string GetNickname(int language) => (uint)language < Nicknames.Length ? Nicknames[language] : string.Empty;
public string GetOT(int language) => (uint)language < TrainerNames.Length ? TrainerNames[language] : string.Empty;
public bool HasNickname => Nicknames.Length != 0;
public bool HasTrainerName => TrainerNames.Length != 0;
public IReadOnlyList<string> Nicknames { get; internal set; } = Array.Empty<string>();
public IReadOnlyList<string> TrainerNames { get; internal set; } = Array.Empty<string>();
public string GetNickname(int language) => (uint)language < Nicknames.Count ? Nicknames[language] : string.Empty;
public string GetOT(int language) => (uint)language < TrainerNames.Count ? TrainerNames[language] : string.Empty;
public bool HasNickname => Nicknames.Count != 0;
public bool HasTrainerName => TrainerNames.Count != 0;
public static readonly int[] DefaultMetLocation =
{
@ -166,7 +166,7 @@ namespace PKHeX.Core
protected void SetIVs(PKM pk)
{
if (IVs.Length != 0)
if (IVs.Count != 0)
pk.SetRandomIVs(IVs, 0);
else
pk.SetRandomIVs(flawless: 3);
@ -228,7 +228,7 @@ namespace PKHeX.Core
public virtual bool IsMatch(PKM pkm, int lvl)
{
if (IVs.Length != 0)
if (IVs.Count != 0)
{
if (!Legal.GetIsFixedIVSequenceValidSkipRand(IVs, pkm))
return false;
@ -337,7 +337,7 @@ namespace PKHeX.Core
{
if (Gender >= 0 && Gender != pkm.Gender)
return false;
if (IVs.Length != 0 && !Legal.GetIsFixedIVSequenceValidNoRand(IVs, pkm))
if (IVs.Count != 0 && !Legal.GetIsFixedIVSequenceValidNoRand(IVs, pkm))
return false;
}
if (pkm.Met_Location != 0 && pkm.Format == 2 && pkm.Met_Location != 126)
@ -391,8 +391,13 @@ namespace PKHeX.Core
const int start = (int)LanguageID.English;
const int end = (int)LanguageID.Spanish;
var index = Array.FindIndex(TrainerNames, start, end - start + 1, w => w == OT);
return index >= 0;
for (int i = start; i <= end; i++)
{
if (TrainerNames[i] == OT)
return true;
}
return false;
}
}

View file

@ -70,7 +70,7 @@ namespace PKHeX.Core
else if (z is EncounterStaticShadow s)
{
bool valid = false;
if (s.IVs.Length == 0) // not ereader
if (s.IVs.Count == 0) // not ereader
{
valid = LockFinder.IsAllShadowLockValid(s, info.PIDIV, pkm);
}

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using static PKHeX.Core.LegalityCheckStrings;
using static PKHeX.Core.LanguageID;
@ -272,7 +273,7 @@ namespace PKHeX.Core
private static int DetectTradeLanguage(string OT, EncounterTrade t, int currentLanguageID)
{
var names = t.TrainerNames;
for (int lang = 1; lang < names.Length; lang++)
for (int lang = 1; lang < names.Count; lang++)
{
if (names[lang] != OT)
continue;
@ -345,13 +346,13 @@ namespace PKHeX.Core
data.AddLine(result);
}
private static CheckResult CheckTradeOTOnly(LegalityAnalysis data, string[] validOT)
private static CheckResult CheckTradeOTOnly(LegalityAnalysis data, IReadOnlyList<string> validOT)
{
var pkm = data.pkm;
if (pkm.IsNicknamed && pkm.Format < 8)
return GetInvalid(LEncTradeChangedNickname, CheckIdentifier.Nickname);
int lang = pkm.Language;
if (validOT.Length <= lang)
if (validOT.Count <= lang)
return GetInvalid(LEncTradeIndexBad, CheckIdentifier.Trainer);
if (validOT[lang] != pkm.OT_Name)
return GetInvalid(LEncTradeChangedOT, CheckIdentifier.Trainer);

View file

@ -1036,7 +1036,7 @@ namespace PKHeX.Core
/// <param name="template">IV template to generate from</param>
/// <param name="flawless">Count of flawless IVs to set. If none provided, a count will be detected.</param>
/// <returns>Randomized IVs if desired.</returns>
public int[] SetRandomIVs(int[] template, int? flawless = null)
public int[] SetRandomIVs(IReadOnlyList<int> template, int? flawless = null)
{
int count = flawless ?? GetFlawlessIVCount();
int[] ivs = new int[6];

View file

@ -1,4 +1,6 @@
namespace PKHeX.Core
using System.Collections.Generic;
namespace PKHeX.Core
{
public interface IContestStats
{
@ -12,9 +14,9 @@
public static partial class Extensions
{
public static void SetContestStats(this IContestStats dest, int[] stats)
public static void SetContestStats(this IContestStats dest, IReadOnlyList<int> stats)
{
if (stats.Length != 6)
if (stats.Count != 6)
return;
dest.CNT_Cool = stats[0];