using System;
namespace PKHeX.Core;
///
/// Level Up Learn Movepool Information
///
public sealed class Learnset
{
///
/// Moves that can be learned.
///
private readonly ushort[] Moves;
///
/// Levels at which a move at a given index can be learned.
///
private readonly byte[] Levels;
private const byte MagicEvolutionMoveLevel = 0;
public Learnset(ushort[] moves, byte[] levels)
{
Moves = moves;
Levels = levels;
}
public ReadOnlySpan GetAllMoves() => Moves;
public ReadOnlySpan GetMoveRange(int maxLevel, int minLevel = 0)
{
if (minLevel <= 1 && maxLevel >= 100)
return Moves;
if (minLevel > maxLevel)
return default;
int start = FindGrq(minLevel);
if (start < 0)
return default;
int end = FindLastLeq(maxLevel);
if (end < 0)
return default;
var length = end - start + 1;
return Moves.AsSpan(start, length);
}
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;
}
/// Returns the moves a Pokémon would have if it were encountered at the specified level.
/// In Generation 1, it is not possible to learn any moves lower than these encounter moves.
/// The level the Pokémon was encountered at.
/// Move array to write to
/// Starting index to begin overwriting at
/// Array of Move IDs
public void SetEncounterMoves(int level, Span 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 moves, ref int ctr, ushort move)
{
if (!moves.Contains(move))
moves[(ctr++) & 3] = move;
}
private static void RectifyOrderShift(Span 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 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;
}
}
/// Adds the learned moves by level up to the specified level.
public void SetLevelUpMoves(int startLevel, int endLevel, Span 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);
}
/// Adds the moves that are gained upon evolving.
/// Move array to write to
/// Starting index to begin overwriting at
public void SetEvolutionMoves(Span 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);
}
/// Adds the learned moves by level up to the specified level.
public void SetLevelUpMoves(int startLevel, int endLevel, Span moves, ReadOnlySpan 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);
}
/// Adds the moves that are gained upon evolving.
/// Move array to write to
/// Ignored moves
/// Starting index to begin overwriting at
public void SetEvolutionMoves(Span moves, ReadOnlySpan 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);
}
///
/// Checks if the specified move is learned by level up.
///
/// Move ID
public bool GetIsLearn(ushort move) => Moves.AsSpan().Contains(move);
/// Returns the level that a Pokémon can learn the specified move.
/// Move ID
/// Level the move is learned at. If the result is below 0, the move cannot be learned by leveling up.
public int GetLevelLearnMove(ushort move)
{
var index = Array.IndexOf(Moves, move);
if (index == -1)
return -1;
return Levels[index];
}
public ReadOnlySpan 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.Empty;
int start = 0;
if (count > 4)
{
start = count - 4;
count = 4;
}
return Moves.AsSpan(start, count);
}
}