Span-ify MoveSetApplicator

This commit is contained in:
Kurt 2023-03-21 18:19:55 -07:00
parent 46de8c4b06
commit 4615e3577a
15 changed files with 210 additions and 129 deletions

View file

@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PKHeX.Core;
@ -9,99 +7,121 @@ namespace PKHeX.Core;
/// </summary>
public static class MoveSetApplicator
{
public static void SetMoveset(this PKM pk, bool random = false)
{
Span<ushort> moves = stackalloc ushort[4];
pk.GetMoveSet(moves, random);
pk.SetMoves(moves);
}
public static void SetRelearnMoves(this PKM pk, LegalityAnalysis la)
{
Span<ushort> moves = stackalloc ushort[4];
la.GetSuggestedRelearnMoves(moves);
pk.SetRelearnMoves(moves);
}
/// <summary>
/// Gets a moveset for the provided <see cref="PKM"/> data.
/// </summary>
/// <param name="pk">PKM to generate for</param>
/// <param name="moves">Result storage</param>
/// <param name="random">Full movepool &amp; shuffling</param>
/// <returns>4 moves</returns>
public static ushort[] GetMoveSet(this PKM pk, bool random = false)
public static void GetMoveSet(this PKM pk, Span<ushort> moves, bool random = false)
{
var la = new LegalityAnalysis(pk);
var moves = la.GetMoveSet(random);
la.GetMoveSet(moves, random);
if (random)
return moves;
return;
var clone = pk.Clone();
clone.SetMoves(moves);
clone.SetMaximumPPCurrent(moves);
var newLa = new LegalityAnalysis(clone);
if (newLa.Valid)
return;
// ReSharper disable once TailRecursiveCall
return newLa.Valid ? moves : GetMoveSet(pk, true);
GetMoveSet(pk, moves, true);
}
/// <summary>
/// Gets a moveset for the provided <see cref="PKM"/> data.
/// </summary>
/// <param name="la">Precomputed optional</param>
/// <param name="moves">Result storage</param>
/// <param name="random">Full movepool &amp; shuffling</param>
/// <returns>4 moves</returns>
public static ushort[] GetMoveSet(this LegalityAnalysis la, bool random = false)
public static void GetMoveSet(this LegalityAnalysis la, Span<ushort> moves, bool random = false)
{
var m = la.GetSuggestedCurrentMoves(random ? MoveSourceType.All : MoveSourceType.Encounter);
la.GetSuggestedCurrentMoves(moves, random ? MoveSourceType.All : MoveSourceType.Encounter);
if (random && !la.Entity.IsEgg)
Util.Shuffle(m.AsSpan());
const int count = 4;
if (m.Length > count)
return m[^count..];
Array.Resize(ref m, count);
return m;
Util.Shuffle(moves);
}
/// <summary>
/// Fetches <see cref="PKM.RelearnMoves"/> based on the provided <see cref="LegalityAnalysis"/>.
/// </summary>
/// <param name="legal"><see cref="LegalityAnalysis"/> which contains parsed information pertaining to legality.</param>
/// <param name="moves">Result storage</param>
/// <param name="enc">Encounter the relearn moves should be suggested for. If not provided, will use the original encounter from the analysis. </param>
/// <returns><see cref="PKM.RelearnMoves"/> best suited for the current <see cref="PKM"/> data.</returns>
public static IReadOnlyList<ushort> GetSuggestedRelearnMoves(this LegalityAnalysis legal, IEncounterTemplate? enc = null)
public static void GetSuggestedRelearnMoves(this LegalityAnalysis legal, Span<ushort> moves, IEncounterTemplate? enc = null)
{
enc ??= legal.EncounterOriginal;
var m = legal.GetSuggestedRelearnMovesFromEncounter(enc);
if (m.Any(z => z != 0))
return m;
legal.GetSuggestedRelearnMovesFromEncounter(moves, enc);
if (moves[0] != 0)
return;
if (enc is MysteryGift or EncounterEgg)
return m;
return;
if (enc is EncounterSlot6AO {CanDexNav: true} dn)
{
var moves = legal.Info.Moves;
for (int i = 0; i < moves.Length; i++)
var chk = legal.Info.Moves;
for (int i = 0; i < chk.Length; i++)
{
if (!moves[i].ShouldBeInRelearnMoves())
if (!chk[i].ShouldBeInRelearnMoves())
continue;
var move = legal.Entity.GetMove(i);
if (dn.CanBeDexNavMove(move))
return new ushort[] { move, 0, 0, 0 };
if (!dn.CanBeDexNavMove(move))
continue;
moves.Clear();
moves[0] = move;
return;
}
}
if (enc is EncounterSlot8b { IsUnderground: true } ug)
{
var moves = legal.Info.Moves;
for (int i = 0; i < moves.Length; i++)
var chk = legal.Info.Moves;
for (int i = 0; i < chk.Length; i++)
{
if (!moves[i].ShouldBeInRelearnMoves())
if (!chk[i].ShouldBeInRelearnMoves())
continue;
var move = legal.Entity.GetMove(i);
if (ug.CanBeUndergroundMove(move))
return new ushort[] { move, 0, 0, 0 };
if (!ug.CanBeUndergroundMove(move))
continue;
moves.Clear();
moves[0] = move;
return;
}
if (ug.GetBaseEggMove(out var any))
return new ushort[] { any, 0, 0, 0 };
{
moves.Clear();
moves[0] = any;
return;
}
}
var encounter = EncounterSuggestion.GetSuggestedMetInfo(legal.Entity);
if (encounter is IRelearn {Relearn: {HasMoves:true} r})
return r.ToArray();
return m;
r.CopyTo(moves);
}
}

View file

@ -361,7 +361,7 @@ public static class BatchEditing
if (cmd.PropertyValue.StartsWith(CONST_SUGGEST, StringComparison.OrdinalIgnoreCase))
return SetSuggestedPKMProperty(cmd.PropertyName, info, cmd.PropertyValue);
if (cmd is { PropertyValue: CONST_RAND, PropertyName: nameof(PKM.Moves) })
return SetMoves(pk, info.Legality.GetMoveSet(true));
return SetSuggestedMoveset(info, true);
if (SetComplexProperty(pk, cmd))
return ModifyResult.Modified;

View file

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System;
namespace PKHeX.Core;
@ -14,5 +14,5 @@ public sealed class BatchInfo
internal LegalityAnalysis Legality => la ??= new LegalityAnalysis(Entity);
public bool Legal => Legality.Valid;
internal IReadOnlyList<ushort> SuggestedRelearn => Legality.GetSuggestedRelearnMoves();
internal void SuggestedRelearn(Span<ushort> moves) => Legality.GetSuggestedRelearnMoves(moves, Legality.EncounterOriginal);
}

View file

@ -41,7 +41,7 @@ public static class BatchMods
new TypeSuggestion<PKM>(nameof(PKM.Move3_PP), p => p.SetSuggestedMovePP(2)),
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.Moves), (_, _, info) => BatchModifications.SetSuggestedMoveset(info)),
new ComplexSuggestion(PROP_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)),

View file

@ -10,6 +10,13 @@ internal static class BatchModifications
private static bool IsAll(ReadOnlySpan<char> p) => p.EndsWith("All", StringComparison.OrdinalIgnoreCase);
private static bool IsNone(ReadOnlySpan<char> p) => p.EndsWith("None", StringComparison.OrdinalIgnoreCase);
public static ModifyResult SetSuggestedMoveset(BatchInfo info, bool random = false)
{
Span<ushort> moves = stackalloc ushort[4];
info.Legality.GetMoveSet(moves, random);
return SetMoves(info.Entity, moves);
}
public static ModifyResult SetSuggestedRelearnData(BatchInfo info, ReadOnlySpan<char> propValue)
{
var pk = info.Entity;
@ -28,7 +35,7 @@ internal static class BatchModifications
}
}
pk.SetRelearnMoves(info.SuggestedRelearn);
pk.SetRelearnMoves(info.Legality);
return ModifyResult.Modified;
}

View file

@ -249,7 +249,7 @@ public static class CommonEdits
var legal = new LegalityAnalysis(pk);
if (legal.Parsed && !MoveResult.AllValid(legal.Info.Relearn))
pk.SetRelearnMoves(legal.GetSuggestedRelearnMoves());
pk.SetRelearnMoves(legal);
pk.ResetPartyStats();
pk.RefreshChecksum();
}

View file

@ -71,8 +71,8 @@ public static class BoxManipDefaults
new BoxManipModifyComplex(ModifyHatchEggs, (pk, sav) => pk.ForceHatchPKM(sav), s => s.Generation >= 2 && s is not SAV8LA),
new BoxManipModify(ModifyMaxFriendship, pk => pk.MaximizeFriendship()),
new BoxManipModify(ModifyMaxLevel, pk => pk.MaximizeLevel()),
new BoxManipModify(ModifyResetMoves, pk => pk.SetMoves(pk.GetMoveSet()), s => s.Generation >= 3),
new BoxManipModify(ModifyRandomMoves, pk => pk.SetMoves(pk.GetMoveSet(true))),
new BoxManipModify(ModifyResetMoves, pk => pk.SetMoveset(), s => s.Generation >= 3),
new BoxManipModify(ModifyRandomMoves, pk => pk.SetMoveset(true)),
new BoxManipModify(ModifyHyperTrain,pk => pk.SetSuggestedHyperTrainingData(), s => s.Generation >= 7 && s is not SAV8LA),
new BoxManipModify(ModifyGanbaru,pk => ((IGanbaru)pk).SetSuggestedGanbaruValues(pk), s => s is SAV8LA),
new BoxManipModify(ModifyRemoveNicknames, pk => pk.SetDefaultNickname()),

View file

@ -66,8 +66,7 @@ public sealed class Learnset
break;
var move = Moves[i];
bool alreadyHasMove = moves.IndexOf(move) >= 0;
if (alreadyHasMove)
if (moves.Contains(move))
continue;
moves[ctr++] = move;
@ -92,9 +91,10 @@ public sealed class Learnset
for (int i = start; i <= index; i++)
{
var move = Moves[i];
if (moves.IndexOf(move) == -1)
moves[ctr++] = move;
if (moves.Contains(move))
continue;
moves[ctr++] = move;
if (ctr == 4)
return;
}

View file

@ -1,6 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
namespace PKHeX.Core;
@ -9,39 +8,39 @@ namespace PKHeX.Core;
/// </summary>
public static class MoveListSuggest
{
private static ushort[] GetSuggestedMoves(PKM pk, EvolutionHistory evoChains, MoveSourceType types, IEncounterTemplate enc)
private static void GetSuggestedMoves(PKM pk, EvolutionHistory evoChains, MoveSourceType types, IEncounterTemplate enc, Span<ushort> moves)
{
if (pk is { IsEgg: true, Format: <= 5 }) // pre relearn
{
var moves = new ushort[4];
MoveList.GetCurrentMoves(pk, pk.Species, 0, (GameVersion)pk.Version, pk.CurrentLevel, moves);
return moves;
return;
}
if (types != MoveSourceType.None)
return GetValidMoves(pk, enc, evoChains, types);
if (types is not (MoveSourceType.None or MoveSourceType.Encounter))
{
GetValidMoves(pk, enc, evoChains, moves, types);
return;
}
// try to give current moves
if (enc.Generation <= 2)
{
var lvl = pk.Format >= 7 ? pk.Met_Level : pk.CurrentLevel;
var ver = enc.Version;
var result = new ushort[4];
MoveLevelUp.GetEncounterMoves(result, enc.Species, 0, lvl, ver);
return result;
MoveLevelUp.GetEncounterMoves(moves, enc.Species, 0, lvl, ver);
return;
}
if (pk.Species == enc.Species)
{
var result = new ushort[4];
MoveLevelUp.GetEncounterMoves(result, pk.Species, pk.Form, pk.CurrentLevel, (GameVersion)pk.Version);
return result;
MoveLevelUp.GetEncounterMoves(moves, pk.Species, pk.Form, pk.CurrentLevel, (GameVersion)pk.Version);
return;
}
return GetValidMoves(pk, enc, evoChains, types);
GetValidMoves(pk, enc, evoChains, moves, types);
}
private static ushort[] GetValidMoves(PKM pk, IEncounterTemplate enc, EvolutionHistory evoChains, MoveSourceType types = MoveSourceType.ExternalSources)
private static void GetValidMoves(PKM pk, IEncounterTemplate enc, EvolutionHistory evoChains, Span<ushort> moves, MoveSourceType types = MoveSourceType.ExternalSources)
{
var length = pk.MaxMoveID + 1;
bool[] rent = ArrayPool<bool>.Shared.Rent(length);
@ -49,106 +48,135 @@ public static class MoveListSuggest
LearnPossible.Get(pk, enc, evoChains, span, types);
var count = span[1..].Count(true);
var result = new ushort[count];
int ctr = 0;
for (ushort i = 1; i < span.Length; i++)
{
if (rent[i])
result[ctr++] = i;
}
int remain = moves.Length;
if (count <= remain)
LoadAll(moves, span);
else
LoadSample(moves, span, count, remain);
span.Clear();
ArrayPool<bool>.Shared.Return(rent);
return result;
}
private static void LoadSample(Span<ushort> moves, ReadOnlySpan<bool> span, int count, int remain)
{
// Selection Sampling
int ctr = 0;
var rnd = Util.Rand;
for (ushort i = 1; i < span.Length; i++)
{
if (!span[i])
continue;
if (rnd.Next(count--) >= remain)
continue;
moves[ctr++] = i;
if (--remain == 0)
break;
}
}
private static void LoadAll(Span<ushort> moves, ReadOnlySpan<bool> span)
{
int ctr = 0;
for (ushort i = 1; i < span.Length; i++)
{
if (!span[i])
continue;
moves[ctr++] = i;
}
}
/// <summary>
/// Gets four moves which can be learned depending on the input arguments.
/// </summary>
/// <param name="analysis">Parse information to generate a moveset for.</param>
/// <param name="moves">Result storage</param>
/// <param name="types">Allowed move sources for populating the result array</param>
public static ushort[] GetSuggestedCurrentMoves(this LegalityAnalysis analysis, MoveSourceType types = MoveSourceType.All)
public static void GetSuggestedCurrentMoves(this LegalityAnalysis analysis, Span<ushort> moves, MoveSourceType types = MoveSourceType.All)
{
if (!analysis.Parsed)
return new ushort[4];
return;
var pk = analysis.Entity;
if (pk is { IsEgg: true, Format: >= 6 })
return pk.RelearnMoves;
{
pk.GetRelearnMoves(moves);
return;
}
if (pk.IsEgg)
types = types.ClearNonEggSources();
var info = analysis.Info;
return GetSuggestedMoves(pk, info.EvoChainsAllGens, types, info.EncounterOriginal);
GetSuggestedMoves(pk, info.EvoChainsAllGens, types, info.EncounterOriginal, moves);
}
/// <summary>
/// Gets the current <see cref="PKM.RelearnMoves"/> array of four moves that might be legal.
/// </summary>
/// <remarks>Use <see cref="GetSuggestedRelearnMovesFromEncounter"/> instead of calling directly; this method just puts default values in without considering the final moveset.</remarks>
public static IReadOnlyList<ushort> GetSuggestedRelearn(this IEncounterTemplate enc, PKM pk)
public static void GetSuggestedRelearn(this IEncounterTemplate enc, PKM pk, Span<ushort> moves)
{
if (LearnVerifierRelearn.ShouldNotHaveRelearnMoves(enc, pk))
return Empty;
return;
return GetSuggestedRelearnInternal(enc, pk);
GetSuggestedRelearnInternal(enc, pk, moves);
}
// Invalid encounters won't be recognized as an EncounterEgg; check if it *should* be a bred egg.
private static IReadOnlyList<ushort> GetSuggestedRelearnInternal(this IEncounterTemplate enc, PKM pk) => enc switch
private static void GetSuggestedRelearnInternal(this IEncounterTemplate enc, PKM pk, Span<ushort> moves)
{
IRelearn { Relearn: { HasMoves: true } r } => r.ToArray(),
EncounterEgg or EncounterInvalid {EggEncounter: true} => GetSuggestedRelearnEgg(enc, pk),
_ => Empty,
};
if (enc is IRelearn { Relearn: { HasMoves: true } r })
r.CopyTo(moves);
else if (enc is EncounterEgg or EncounterInvalid { EggEncounter: true })
GetSuggestedRelearnEgg(enc, pk, moves);
}
private static ushort[] GetSuggestedRelearnEgg(IEncounterTemplate enc, PKM pk)
private static void GetSuggestedRelearnEgg(IEncounterTemplate enc, PKM pk, Span<ushort> moves)
{
Span<ushort> current = stackalloc ushort[4];
pk.GetRelearnMoves(current);
Span<ushort> expected = stackalloc ushort[current.Length];
_ = MoveBreed.GetExpectedMoves(current, enc, expected);
return expected.ToArray();
expected.CopyTo(moves);
}
private static readonly IReadOnlyList<ushort> Empty = new ushort[4];
/// <summary>
/// Gets the current <see cref="PKM.RelearnMoves"/> array of four moves that might be legal.
/// </summary>
public static IReadOnlyList<ushort> GetSuggestedRelearnMovesFromEncounter(this LegalityAnalysis analysis, IEncounterTemplate? enc = null)
public static void GetSuggestedRelearnMovesFromEncounter(this LegalityAnalysis analysis, Span<ushort> moves, IEncounterTemplate? enc = null)
{
var info = analysis.Info;
enc ??= info.EncounterOriginal;
var pk = analysis.Entity;
if (LearnVerifierRelearn.ShouldNotHaveRelearnMoves(enc, pk))
return Empty;
return;
if (enc is EncounterEgg or EncounterInvalid {EggEncounter: true})
return enc.GetSuggestedRelearnEgg(info.Moves, pk);
return enc.GetSuggestedRelearnInternal(pk);
enc.GetSuggestedRelearnEgg(info.Moves, pk, moves);
else
enc.GetSuggestedRelearnInternal(pk, moves);
}
private static IReadOnlyList<ushort> GetSuggestedRelearnEgg(this IEncounterTemplate enc, ReadOnlySpan<MoveResult> parse, PKM pk)
private static void GetSuggestedRelearnEgg(this IEncounterTemplate enc, ReadOnlySpan<MoveResult> parse, PKM pk, Span<ushort> moves)
{
var result = enc.GetEggRelearnMoves(parse, pk);
enc.GetEggRelearnMoves(parse, pk, moves);
int generation = enc.Generation;
if (generation <= 5) // gen2 does not have splitbreed, <=5 do not have relearn moves and shouldn't even be here.
return result;
return;
// Split-breed species like Budew & Roselia may be legal for one, and not the other.
// If we're not a split-breed or are already legal, return.
var split = Breeding.GetSplitBreedGeneration(generation);
if (split?.Contains(enc.Species) != true)
return result;
return;
var tmp = pk.Clone();
tmp.SetRelearnMoves(result);
tmp.SetRelearnMoves(moves);
var la = new LegalityAnalysis(tmp);
var moves = la.Info.Moves;
if (MoveResult.AllValid(moves))
return result;
var chk = la.Info.Moves;
if (MoveResult.AllValid(chk))
return;
// Try again with the other split-breed species if possible.
var generator = EncounterGenerator.GetGenerator(enc.Version);
@ -157,16 +185,16 @@ public static class MoveListSuggest
var other = generator.GetPossible(pk, chain, enc.Version, EncounterTypeGroup.Egg);
foreach (var incense in other)
{
if (incense.Species != enc.Species)
return incense.GetEggRelearnMoves(parse, pk);
if (incense.Species == enc.Species)
continue;
incense.GetEggRelearnMoves(parse, pk, moves);
break;
}
return result;
}
private static ushort[] GetEggRelearnMoves(this IEncounterTemplate enc, ReadOnlySpan<MoveResult> parse, PKM pk)
private static ushort[] GetEggRelearnMoves(this IEncounterTemplate enc, ReadOnlySpan<MoveResult> parse, PKM pk, Span<ushort> moves)
{
// Extract a list of the moves that should end up in the relearn move list.
Span<ushort> moves = stackalloc ushort[parse.Length];
LoadRelearnFlagged(moves, parse, pk);
Span<ushort> expected = stackalloc ushort[moves.Length];

View file

@ -479,12 +479,12 @@ public abstract class PKM : ISpeciesForm, ITrainerID32, IGeneration, IShiny, ILa
RelearnMove4 = value.Move4;
}
public void SetRelearnMoves(IReadOnlyList<ushort> value)
public void SetRelearnMoves(ReadOnlySpan<ushort> value)
{
RelearnMove1 = value.Count > 0 ? value[0] : default;
RelearnMove2 = value.Count > 1 ? value[1] : default;
RelearnMove3 = value.Count > 2 ? value[2] : default;
RelearnMove4 = value.Count > 3 ? value[3] : default;
RelearnMove1 = value.Length > 0 ? value[0] : default;
RelearnMove2 = value.Length > 1 ? value[1] : default;
RelearnMove3 = value.Length > 2 ? value[2] : default;
RelearnMove4 = value.Length > 3 ? value[3] : default;
}
public int PIDAbility

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using static PKHeX.Core.GameVersion;
namespace PKHeX.Core.Searching;
@ -152,7 +153,7 @@ public sealed class SearchSettings
{
if (Generation > 0 && !SearchUtil.SatisfiesFilterGeneration(pk, Generation))
return false;
if (Moves.Count > 0 && !SearchUtil.SatisfiesFilterMoves(pk, Moves))
if (Moves.Count > 0 && !SearchUtil.SatisfiesFilterMoves(pk, CollectionsMarshal.AsSpan(Moves)))
return false;
if (HiddenPowerType > -1 && pk.HPType != HiddenPowerType)
return false;

View file

@ -61,7 +61,7 @@ public static class SearchUtil
_ => true,
};
public static bool SatisfiesFilterMoves(PKM pk, IReadOnlyList<ushort> requiredMoves)
public static bool SatisfiesFilterMoves(PKM pk, ReadOnlySpan<ushort> requiredMoves)
{
foreach (var m in requiredMoves)
{

View file

@ -1,3 +1,5 @@
using System;
namespace PKHeX.Core;
/// <summary>
@ -50,8 +52,9 @@ public sealed class LegalityRejuvenator : IEntityRejuvenator
enc = la.EncounterOriginal;
if (result is PA8 pa8)
{
var relearn = la.GetSuggestedRelearnMoves(enc);
if (relearn.Count != 0)
Span<ushort> relearn = stackalloc ushort[4];
la.GetSuggestedRelearnMoves(relearn, enc);
if (relearn[0] != 0)
pa8.SetRelearnMoves(relearn);
pa8.ClearMoveShopFlags();
@ -88,9 +91,9 @@ public sealed class LegalityRejuvenator : IEntityRejuvenator
// Try again with rectified locations.
la = new LegalityAnalysis(result);
enc = la.EncounterOriginal;
var relearn = la.GetSuggestedRelearnMoves(enc);
if (relearn.Count != 0)
Span<ushort> relearn = stackalloc ushort[4];
la.GetSuggestedRelearnMoves(relearn);
if (relearn[0] != 0)
result.SetRelearnMoves(relearn);
}
}

View file

@ -15,9 +15,9 @@ public static partial class Util
return list;
}
private static List<ComboItem> GetCBListFromCSV(IReadOnlyList<string> inputCSV, int index)
private static List<ComboItem> GetCBListFromCSV(ReadOnlySpan<string> inputCSV, int index)
{
var arr = new List<ComboItem>(inputCSV.Count);
var arr = new List<ComboItem>(inputCSV.Length);
foreach (var line in inputCSV)
{
var span = line.AsSpan();

View file

@ -5,6 +5,7 @@ using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using PKHeX.Core;
using PKHeX.Drawing;
using PKHeX.Drawing.PokeSprite;
@ -735,29 +736,30 @@ public sealed partial class PKMEditor : UserControl, IMainEditor
private bool SetSuggestedMoves(bool random = false, bool silent = false)
{
var m = Entity.GetMoveSet(random);
if (m.Length == 0 || m.All(z => z == 0))
Span<ushort> moves = stackalloc ushort[4];
Entity.GetMoveSet(moves, random);
if (moves[0] == 0)
{
if (!silent)
WinFormsUtil.Alert(MsgPKMSuggestionFormat);
return false;
}
Span<ushort> moves = stackalloc ushort[4];
Entity.GetMoves(moves);
if (moves.SequenceEqual(m))
Span<ushort> current = stackalloc ushort[4];
Entity.GetMoves(current);
var same = Entity.IsEgg ? current.SequenceEqual(moves) : IsAllElementsShared(current, moves);
if (same)
return false;
if (!silent)
{
var mv = GameInfo.Strings.Move;
var movestrings = m.Select(v => v >= mv.Count ? MsgProgramError : mv[v]);
var msg = string.Join(Environment.NewLine, movestrings);
var msg = GetMoveListPrint(moves, GameInfo.Strings.movelist);
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgPKMSuggestionMoves, msg))
return false;
}
Entity.SetMoves(m);
Entity.SetMoves(moves);
Entity.HealPP();
FieldsLoaded = false;
LoadMoves(Entity);
ClickPP(this, EventArgs.Empty);
@ -765,20 +767,29 @@ public sealed partial class PKMEditor : UserControl, IMainEditor
return true;
}
private static bool IsAllElementsShared(Span<ushort> seq1, Span<ushort> seq2)
{
foreach (var entry in seq2)
{
if (!seq1.Contains(entry))
return false;
}
return true;
}
private bool SetSuggestedRelearnMoves(bool silent = false)
{
if (Entity.Format < 6)
return false;
var m = Legality.GetSuggestedRelearnMoves();
if (m.Count != 4 || Entity.RelearnMoves.SequenceEqual(m))
Span<ushort> m = stackalloc ushort[4];
Legality.GetSuggestedRelearnMoves(m);
if (Entity.RelearnMove1 == m[0] && Entity.RelearnMove2 == m[1] && Entity.RelearnMove3 == m[2] && Entity.RelearnMove4 == m[3])
return false;
if (!silent)
{
var mv = GameInfo.Strings.Move;
var movestrings = m.Select(v => v >= mv.Count ? MsgProgramError : mv[v]);
var msg = string.Join(Environment.NewLine, movestrings);
var msg = GetMoveListPrint(m, GameInfo.Strings.movelist);
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgPKMSuggestionRelearn, msg))
return false;
}
@ -790,6 +801,17 @@ public sealed partial class PKMEditor : UserControl, IMainEditor
return true;
}
private static string GetMoveListPrint(Span<ushort> moves, ReadOnlySpan<string> names)
{
var sb = new StringBuilder();
foreach (var move in moves)
{
if (move != 0)
sb.AppendLine(names[move]);
}
return sb.ToString();
}
private bool SetSuggestedMetLocation(bool silent = false)
{
var encounter = EncounterSuggestion.GetSuggestedMetInfo(Entity);