2019-05-30 04:35:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Legal Move information for a single <see cref="PKM"/>, for indicating if a move is legal or not.
|
|
|
|
|
/// </summary>
|
2019-10-04 02:09:02 +00:00
|
|
|
|
public sealed class LegalMoveSource
|
2019-05-30 04:35:52 +00:00
|
|
|
|
{
|
2021-07-06 15:42:15 +00:00
|
|
|
|
public readonly bool[] IsMoveBoxOrdered = new bool[4];
|
2021-07-16 03:39:53 +00:00
|
|
|
|
|
|
|
|
|
/// <summary> Creates a shallow copy of the array reference for use in binding. </summary>
|
2019-05-30 04:35:52 +00:00
|
|
|
|
public IReadOnlyList<ComboItem> DataSource => (ComboItem[])MoveDataAllowed.Clone();
|
2021-07-16 03:39:53 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-20 20:42:25 +00:00
|
|
|
|
/// Checks if the requested <see cref="move"/> is legally able to be learned.
|
2021-07-16 03:39:53 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="move">Move to check if can be learned</param>
|
|
|
|
|
/// <returns>True if can learn the move</returns>
|
2019-05-30 04:35:52 +00:00
|
|
|
|
public bool CanLearn(int move) => AllowedMoves.Contains(move);
|
|
|
|
|
|
2020-12-22 01:17:56 +00:00
|
|
|
|
private readonly HashSet<int> AllowedMoves = new();
|
2019-05-30 04:35:52 +00:00
|
|
|
|
private ComboItem[] MoveDataAllowed = Array.Empty<ComboItem>();
|
|
|
|
|
|
2021-07-16 03:39:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reloads the legality sources to permit the provided legal <see cref="moves"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="moves">Legal moves to allow</param>
|
2019-05-30 04:35:52 +00:00
|
|
|
|
public void ReloadMoves(IReadOnlyList<int> moves)
|
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
// check prior move-pool to not needlessly refresh the data set
|
2019-05-30 04:35:52 +00:00
|
|
|
|
if (AllowedMoves.Count == moves.Count && AllowedMoves.SetEquals(moves))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
AllowedMoves.Clear();
|
|
|
|
|
foreach (var m in moves)
|
|
|
|
|
AllowedMoves.Add(m);
|
|
|
|
|
Array.Sort(MoveDataAllowed, Compare);
|
|
|
|
|
// MoveDataAllowed = MoveDataAllowed.OrderByDescending(m => AllowedMoves.Contains(m.Value)).ToArray();
|
|
|
|
|
|
2020-06-17 02:46:22 +00:00
|
|
|
|
// defer re-population until dropdown is opened; handled by dropdown event
|
2021-07-06 15:42:15 +00:00
|
|
|
|
Array.Clear(IsMoveBoxOrdered, 0, IsMoveBoxOrdered.Length);
|
2019-05-30 04:35:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int Compare(ComboItem i1, ComboItem i2)
|
|
|
|
|
{
|
|
|
|
|
// split into 2 groups: Allowed & Not, and sort each sublist
|
|
|
|
|
var c1 = AllowedMoves.Contains(i1.Value);
|
|
|
|
|
var c2 = AllowedMoves.Contains(i2.Value);
|
|
|
|
|
if (c1)
|
|
|
|
|
return c2 ? string.CompareOrdinal(i1.Text, i2.Text) : -1;
|
|
|
|
|
return c2 ? 1 : string.CompareOrdinal(i1.Text, i2.Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReloadMoves(IReadOnlyList<ComboItem> moves)
|
|
|
|
|
{
|
|
|
|
|
MoveDataAllowed = moves.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|