PKHeX/PKHeX.Core/Editing/PKM/LegalMoveInfo.cs
Kurt d47bb1d297
Update .NET Runtime to .NET 8.0 (#4082)
With the new version of Visual Studio bringing C# 12, we can revise our logic for better readability as well as use new methods/APIs introduced in the .NET 8.0 BCL.
2023-12-03 20:13:20 -08:00

40 lines
1.5 KiB
C#

using System;
using System.Buffers;
namespace PKHeX.Core;
/// <summary>
/// Caches move learn data via <see cref="ReloadMoves"/>
/// </summary>
public sealed class LegalMoveInfo
{
// Use a bool array instead of a HashSet; we have a limited range of moves.
// This implementation is faster (no hashcode or bucket search) with lower memory overhead (1 byte per move ID).
private readonly bool[] AllowedMoves = new bool[(int)Move.MAX_COUNT + 1];
/// <summary>
/// Checks if the requested <see cref="move"/> is legally able to be learned.
/// </summary>
/// <param name="move">Move to check if it can be learned</param>
/// <returns>True if it can learn the move</returns>
public bool CanLearn(ushort move) => AllowedMoves[move];
/// <summary>
/// Reloads the legality sources to permit the provided legal info.
/// </summary>
/// <param name="la">Details of analysis, moves to allow</param>
public bool ReloadMoves(LegalityAnalysis la)
{
var rent = ArrayPool<bool>.Shared.Rent(AllowedMoves.Length);
var span = rent.AsSpan(0, AllowedMoves.Length);
LearnPossible.Get(la.Entity, la.EncounterOriginal, la.Info.EvoChainsAllGens, span);
// check prior move-pool to not needlessly refresh the data set
bool diff = !span.SequenceEqual(AllowedMoves);
if (diff) // keep
span.CopyTo(AllowedMoves);
span.Clear();
ArrayPool<bool>.Shared.Return(rent);
return diff;
}
}