From 4d0108331a3e38b1ecee7d427b06c5feb1682b8a Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 24 Dec 2020 16:36:50 -0800 Subject: [PATCH] Move LegalityAnalysis derived movesets out of class LegalityAnalysis just does the analysis; no need to do unnecessary suggestion logic inside the object. --- .../Editing/Applicators/MoveSetApplicator.cs | 2 +- PKHeX.Core/Legality/LegalityAnalysis.cs | 62 --------------- PKHeX.Core/Legality/MoveListSuggest.cs | 79 ++++++++++++++++++- .../Controls/PKM Editor/PKMEditor.cs | 2 +- 4 files changed, 78 insertions(+), 67 deletions(-) diff --git a/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs b/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs index d046f2c36..f9a58267d 100644 --- a/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs +++ b/PKHeX.Core/Editing/Applicators/MoveSetApplicator.cs @@ -38,7 +38,7 @@ public static int[] GetMoveSet(this LegalityAnalysis la, bool random = false) { int[] m = la.GetSuggestedCurrentMoves(random ? MoveSourceType.All : MoveSourceType.None); - var learn = la.AllSuggestedMovesAndRelearn(); + var learn = la.GetSuggestedMovesAndRelearn(); if (!m.All(z => learn.Contains(z))) m = m.Intersect(learn).ToArray(); diff --git a/PKHeX.Core/Legality/LegalityAnalysis.cs b/PKHeX.Core/Legality/LegalityAnalysis.cs index b41dfbfc6..4c598af9b 100644 --- a/PKHeX.Core/Legality/LegalityAnalysis.cs +++ b/PKHeX.Core/Legality/LegalityAnalysis.cs @@ -65,29 +65,6 @@ public sealed class LegalityAnalysis /// Single line string public string Report(bool verbose = false) => verbose ? GetVerboseLegalityReport() : GetLegalityReport(); - private IEnumerable AllSuggestedMoves - { - get - { - if (!Parsed) - return new int[4]; - return _allSuggestedMoves ??= GetSuggestedCurrentMoves(); - } - } - - private IEnumerable AllSuggestedRelearnMoves - { - get - { - if (!Parsed) - return new int[4]; - return _allSuggestedRelearnMoves ??= MoveList.GetValidRelearn(pkm, Info.EncounterMatch.Species, Info.EncounterMatch.Form, (GameVersion)pkm.Version).ToArray(); - } - } - - private int[]? _allSuggestedMoves, _allSuggestedRelearnMoves; - public int[] AllSuggestedMovesAndRelearn() => AllSuggestedMoves.Concat(AllSuggestedRelearnMoves).ToArray(); - private string EncounterName { get @@ -447,44 +424,5 @@ private string GetVerboseLegalityReport() return GetLegalityReport() + string.Join(Environment.NewLine, lines); } - - /// - /// Gets the current array of four moves that might be legal. - /// - public IReadOnlyList GetSuggestedRelearnMovesFromEncounter() - { - var parsed = VerifyRelearnMoves.GetSuggestedRelearn(pkm, Info.EncounterOriginal, Info.Relearn); - if (parsed.Count == 0) // Always true for Origins < 6 and encounters without relearn permitted. - return new int[4]; - - if (!EncounterMatch.EggEncounter) - return parsed; - - List window = new(parsed.Where(z => z != 0)); - window.AddRange(pkm.Moves.Where((_, i) => Info.Moves[i].ShouldBeInRelearnMoves())); - window = window.Distinct().ToList(); - int[] moves = new int[4]; - int start = Math.Max(0, window.Count - 4); - int count = Math.Min(4, window.Count); - window.CopyTo(start, moves, 0, count); - return moves; - } - - /// - /// Gets four moves which can be learned depending on the input arguments. - /// - /// Allowed move sources for populating the result array - public int[] GetSuggestedCurrentMoves(MoveSourceType types = MoveSourceType.All) - { - if (!Parsed) - return new int[4]; - if (pkm.IsEgg && pkm.Format >= 6) - return pkm.RelearnMoves; - - if (pkm.IsEgg) - types = types.ClearNonEggSources(); - - return MoveListSuggest.GetSuggestedMoves(pkm, Info.EvoChainsAllGens, types, EncounterOriginal); - } } } diff --git a/PKHeX.Core/Legality/MoveListSuggest.cs b/PKHeX.Core/Legality/MoveListSuggest.cs index 0f3cd2262..bfa0abdbc 100644 --- a/PKHeX.Core/Legality/MoveListSuggest.cs +++ b/PKHeX.Core/Legality/MoveListSuggest.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace PKHeX.Core { - internal static class MoveListSuggest + public static class MoveListSuggest { - internal static int[] GetSuggestedMoves(PKM pkm, IReadOnlyList[] evoChains, MoveSourceType types, IEncounterable enc) + private static int[] GetSuggestedMoves(PKM pkm, IReadOnlyList[] evoChains, MoveSourceType types, IEncounterable enc) { if (pkm.IsEgg && pkm.Format <= 5) // pre relearn return MoveList.GetBaseEggMoves(pkm, pkm.Species, 0, (GameVersion)pkm.Version, pkm.CurrentLevel); @@ -59,5 +60,77 @@ private static IEnumerable GetValidMoves(PKM pkm, GameVersion version, IRea return r.Distinct(); } + + private static IEnumerable AllSuggestedMoves(this LegalityAnalysis analysis) + { + if (!analysis.Parsed) + return new int[4]; + return analysis.GetSuggestedCurrentMoves(); + } + + private static IEnumerable AllSuggestedRelearnMoves(this LegalityAnalysis analysis) + { + if (!analysis.Parsed) + return new int[4]; + var pkm = analysis.pkm; + var enc = analysis.EncounterMatch; + return MoveList.GetValidRelearn(pkm, enc.Species, enc.Form, (GameVersion)pkm.Version).ToArray(); + } + + public static int[] GetSuggestedMovesAndRelearn(this LegalityAnalysis analysis) + { + if (!analysis.Parsed) + return new int[4]; + return analysis.AllSuggestedMoves().Concat(analysis.AllSuggestedRelearnMoves()).ToArray(); + } + + /// + /// Gets four moves which can be learned depending on the input arguments. + /// + /// Parse information to generate a moveset for. + /// Allowed move sources for populating the result array + public static int[] GetSuggestedCurrentMoves(this LegalityAnalysis analysis, MoveSourceType types = MoveSourceType.All) + { + if (!analysis.Parsed) + return new int[4]; + var pkm = analysis.pkm; + if (pkm.IsEgg && pkm.Format >= 6) + return pkm.RelearnMoves; + + if (pkm.IsEgg) + types = types.ClearNonEggSources(); + + var info = analysis.Info; + return GetSuggestedMoves(pkm, info.EvoChainsAllGens, types, info.EncounterOriginal); + } + + /// + /// Gets the current array of four moves that might be legal. + /// + public static IReadOnlyList GetSuggestedRelearnMovesFromEncounter(this LegalityAnalysis analysis) + { + var info = analysis.Info; + if (info.Generation < 6) + return new int[4]; + + var pkm = analysis.pkm; + var enc = info.EncounterOriginal; + var parsed = VerifyRelearnMoves.GetSuggestedRelearn(pkm, enc, info.Relearn); + if (parsed.Count == 0) // Always true for Origins < 6 and encounters without relearn permitted. + return new int[4]; + + // Invalid encounters won't be recognized as an EncounterEgg; check if it *should* be a bred egg. + if (!enc.EggEncounter) + return parsed; + + List window = new(parsed.Where(z => z != 0)); + window.AddRange(pkm.Moves.Where((_, i) => info.Moves[i].ShouldBeInRelearnMoves())); + window = window.Distinct().ToList(); + int[] moves = new int[4]; + int start = Math.Max(0, window.Count - 4); + int count = Math.Min(4, window.Count); + window.CopyTo(start, moves, 0, count); + return moves; + } } } diff --git a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs index 86b423d5c..ebed159f5 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs @@ -336,7 +336,7 @@ public void UpdateLegality(LegalityAnalysis? la = null, bool skipMoveRepop = fal return; // Resort moves FieldsLoaded = false; - LegalMoveSource.ReloadMoves(Legality.AllSuggestedMovesAndRelearn()); + LegalMoveSource.ReloadMoves(Legality.GetSuggestedMovesAndRelearn()); FieldsLoaded = true; LegalityChanged?.Invoke(Legality.Valid, EventArgs.Empty); }