From 5dcee4fb9aed09357e07a09d9936e7e0161ca4ed Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 24 Oct 2019 18:11:25 -0700 Subject: [PATCH] Set random moves if GetMoveSet yields invalid set Closes #2406 No clean way to check for these edge scenarios, so just double check the moveset and return random moves if we fail --- PKHeX.Core/Editing/CommonEdits.cs | 24 +++++++++++++++---- .../Controls/PKM Editor/PKMEditor.cs | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/PKHeX.Core/Editing/CommonEdits.cs b/PKHeX.Core/Editing/CommonEdits.cs index 0f7b04f4c..2abb8c35f 100644 --- a/PKHeX.Core/Editing/CommonEdits.cs +++ b/PKHeX.Core/Editing/CommonEdits.cs @@ -282,9 +282,15 @@ namespace PKHeX.Core /// Fetches based on the provided . /// /// Pokémon to modify. + /// best suited for the current data. + public static IReadOnlyList GetSuggestedRelearnMoves(this PKM pk) => new LegalityAnalysis(pk).GetSuggestedRelearnMoves(); + + /// + /// Fetches based on the provided . + /// /// which contains parsed information pertaining to legality. /// best suited for the current data. - public static IReadOnlyList GetSuggestedRelearnMoves(this PKM pk, LegalityAnalysis legal) + public static IReadOnlyList GetSuggestedRelearnMoves(this LegalityAnalysis legal) { var m = legal.GetSuggestedRelearn(); if (m.Any(z => z != 0)) @@ -361,7 +367,7 @@ namespace PKHeX.Core var legal = new LegalityAnalysis(pk); if (legal.Parsed && legal.Info.Relearn.Any(z => !z.Valid)) - pk.SetRelearnMoves(pk.GetSuggestedRelearnMoves(legal)); + pk.SetRelearnMoves(legal.GetSuggestedRelearnMoves()); pk.RefreshChecksum(); } @@ -606,7 +612,17 @@ namespace PKHeX.Core public static int[] GetMoveSet(this PKM pkm, bool random = false) { var la = new LegalityAnalysis(pkm); - return pkm.GetMoveSet(la, random); + var moves = pkm.GetMoveSet(la, random); + + if (random) + return moves; + + var clone = pkm.Clone(); + clone.SetMoves(moves); + var newLa = new LegalityAnalysis(pkm); + + // ReSharper disable once TailRecursiveCall + return newLa.Valid ? moves : GetMoveSet(pkm, true); } /// @@ -630,7 +646,7 @@ namespace PKHeX.Core const int count = 4; if (m.Length > count) - return m.Skip(m.Length - count).ToArray(); + return m.SliceEnd(m.Length - count); Array.Resize(ref m, count); return m; } diff --git a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs index 9c22c1dd8..98be563c4 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.cs @@ -671,7 +671,7 @@ namespace PKHeX.WinForms.Controls if (pkm.Format < 6) return false; - var m = pkm.GetSuggestedRelearnMoves(Legality); + var m = Legality.GetSuggestedRelearnMoves(); if (pkm.RelearnMoves.SequenceEqual(m)) return false;