PKHeX/PKHeX.Core/Legality/Learnset/Learnset.cs
Kurt 6d0c2d9520 Add more overload methods
Learnset: slightly quicker check.
SCBlock: assists when you have Key separate from the encoded data.
2023-04-10 00:49:39 -07:00

261 lines
7.8 KiB
C#

using System;
namespace PKHeX.Core;
/// <summary>
/// Level Up Learn Movepool Information
/// </summary>
public sealed class Learnset
{
/// <summary>
/// Moves that can be learned.
/// </summary>
internal readonly ushort[] Moves;
/// <summary>
/// Levels at which a move at a given index can be learned.
/// </summary>
private readonly byte[] Levels;
private const byte MagicEvolutionMoveLevel = 0;
public Learnset(ushort[] moves, byte[] levels)
{
Moves = moves;
Levels = levels;
}
public (bool HasMoves, int Start, int End) GetMoveRange(int maxLevel, int minLevel = 0)
{
if (minLevel <= 1 && maxLevel >= 100)
return (true, 0, Moves.Length - 1);
if (minLevel > maxLevel)
return default;
int start = FindGrq(minLevel);
if (start < 0)
return default;
int end = FindLastLeq(maxLevel);
if (end < 0)
return default;
return (true, start, end);
}
private int FindGrq(int level, int start = 0)
{
var levels = Levels;
for (int i = start; i < levels.Length; i++)
{
if (levels[i] >= level)
return i;
}
return -1;
}
private int FindGr(int level, int start)
{
var levels = Levels;
for (int i = start; i < levels.Length; i++)
{
if (levels[i] >= level)
return i;
}
return -1;
}
private int FindLastLeq(int level, int end = 0)
{
var levels = Levels;
for (int i = levels.Length - 1; i >= end; i--)
{
if (levels[i] <= level)
return i;
}
return -1;
}
/// <summary>Returns the moves a Pokémon would have if it were encountered at the specified level.</summary>
/// <remarks>In Generation 1, it is not possible to learn any moves lower than these encounter moves.</remarks>
/// <param name="level">The level the Pokémon was encountered at.</param>
/// <param name="moves">Move array to write to</param>
/// <param name="ctr">Starting index to begin overwriting at</param>
/// <returns>Array of Move IDs</returns>
public void SetEncounterMoves(int level, Span<ushort> moves, int ctr = 0)
{
for (int i = 0; i < Moves.Length; i++)
{
if (Levels[i] > level)
break;
AddMoveShiftLater(moves, ref ctr, Moves[i]);
}
RectifyOrderShift(moves, ctr);
}
private static void AddMoveShiftLater(Span<ushort> moves, ref int ctr, ushort move)
{
if (!moves.Contains(move))
moves[(ctr++) & 3] = move;
}
private static void RectifyOrderShift(Span<ushort> moves, int ctr)
{
// Perform (n & 3) rotations as if we were inserting moves, but a minimal amount of times.
// This skips the rotation for when moves are inserted and then overwritten by later inserted moves.
if (ctr <= moves.Length)
return;
var rotation = ctr & 3;
if (rotation == 0)
return;
// rotate n times in-place
for (int i = 0; i < rotation; i++)
{
var move = moves[0];
for (int j = 0; j < 3; j++)
moves[j] = moves[j + 1];
moves[3] = move;
}
}
public void SetEncounterMovesBackwards(int level, Span<ushort> moves, int ctr = 0)
{
int index = FindLastLeq(level);
while (true)
{
if (index == -1)
return; // no moves to add?
// In the event we have multiple moves at the same level, insert them in regular descending order.
int start = index;
while (start != 0 && Levels[start] == Levels[start - 1])
start--;
for (int i = start; i <= index; i++)
{
var move = Moves[i];
if (moves.Contains(move))
continue;
moves[ctr++] = move;
if (ctr == 4)
return;
}
index = start - 1;
}
}
/// <summary>Adds the learned moves by level up to the specified level.</summary>
public void SetLevelUpMoves(int startLevel, int endLevel, Span<ushort> moves, int ctr = 0)
{
int startIndex = FindGrq(startLevel);
if (startIndex == -1)
return;
int endIndex = FindGr(endLevel, startIndex);
if (endIndex == -1)
endIndex = Levels.Length;
for (int i = startIndex; i < endIndex; i++)
AddMoveShiftLater(moves, ref ctr, Moves[i]);
RectifyOrderShift(moves, ctr);
}
/// <summary>Adds the moves that are gained upon evolving.</summary>
/// <param name="moves">Move array to write to</param>
/// <param name="ctr">Starting index to begin overwriting at</param>
public void SetEvolutionMoves(Span<ushort> moves, int ctr = 0)
{
// Evolution moves are always at the lowest indexes of the learnset.
for (int i = 0; i < Moves.Length; i++)
{
if (Levels[i] != MagicEvolutionMoveLevel)
break;
AddMoveShiftLater(moves, ref ctr, Moves[i]);
}
RectifyOrderShift(moves, ctr);
}
/// <summary>Adds the learned moves by level up to the specified level.</summary>
public void SetLevelUpMoves(int startLevel, int endLevel, Span<ushort> moves, ReadOnlySpan<ushort> ignore, int ctr = 0)
{
int startIndex = FindGrq(startLevel);
if (startIndex == -1)
return; // No more remain
int endIndex = FindGr(endLevel, startIndex);
if (endIndex == -1)
endIndex = Levels.Length;
for (int i = startIndex; i < endIndex; i++)
{
var move = Moves[i];
if (ignore.IndexOf(move) >= 0)
continue;
AddMoveShiftLater(moves, ref ctr, move);
}
RectifyOrderShift(moves, ctr);
}
/// <summary>Adds the moves that are gained upon evolving.</summary>
/// <param name="moves">Move array to write to</param>
/// <param name="ignore">Ignored moves</param>
/// <param name="ctr">Starting index to begin overwriting at</param>
public void SetEvolutionMoves(Span<ushort> moves, ReadOnlySpan<ushort> ignore, int ctr = 0)
{
for (int i = 0; i < Moves.Length; i++)
{
if (Levels[i] != MagicEvolutionMoveLevel)
break;
var move = Moves[i];
if (ignore.IndexOf(move) >= 0)
continue;
AddMoveShiftLater(moves, ref ctr, move);
}
RectifyOrderShift(moves, ctr);
}
/// <summary>
/// Checks if the specified move is learned by level up.
/// </summary>
/// <param name="move">Move ID</param>
public bool GetIsLearn(ushort move) => Moves.AsSpan().Contains(move);
/// <summary>Returns the level that a Pokémon can learn the specified move.</summary>
/// <param name="move">Move ID</param>
/// <returns>Level the move is learned at. If the result is below 0, the move cannot be learned by leveling up.</returns>
public int GetLevelLearnMove(ushort move)
{
var index = Array.IndexOf(Moves, move);
if (index == -1)
return -1;
return Levels[index];
}
public ReadOnlySpan<ushort> GetBaseEggMoves(int level)
{
// Count moves <= level
var count = 0;
foreach (ref var x in Levels.AsSpan())
{
if (x > level)
break;
count++;
}
// Return a slice containing the moves <= level.
if (count == 0)
return ReadOnlySpan<ushort>.Empty;
int start = 0;
if (count > 4)
{
start = count - 4;
count = 4;
}
return Moves.AsSpan(start, count);
}
}