using System;
using System.Collections.Generic;
namespace PKHeX.Core
{
///
/// Level Up Learn Movepool Information
///
public sealed class Learnset
{
///
/// Moves that can be learned.
///
private readonly int[] Moves;
///
/// Levels at which a move at a given index can be learned.
///
private readonly int[] Levels;
public Learnset(int[] moves, int[] levels)
{
Moves = moves;
Levels = levels;
}
///
/// Returns the moves a Pokémon can learn between the specified level range.
///
/// Maximum level
/// Minimum level
/// Array of Move IDs
public int[] GetMoves(int maxLevel, int minLevel = 0)
{
if (minLevel <= 1 && maxLevel >= 100)
return Moves;
if (minLevel > maxLevel)
return Array.Empty();
int start = Array.FindIndex(Levels, z => z >= minLevel);
if (start < 0)
return Array.Empty();
int end = Array.FindLastIndex(Levels, z => z <= maxLevel);
if (end < 0)
return Array.Empty();
int[] result = new int[end - start + 1];
Array.Copy(Moves, start, result, 0, result.Length);
return result;
}
///
/// Adds the moves a Pokémon can learn between the specified level range.
///
/// Movepool
/// Maximum level
/// Minimum level
/// Array of Move IDs
public List AddMoves(List moves, int maxLevel, int minLevel = 0)
{
if (minLevel <= 1 && maxLevel >= 100)
{
moves.AddRange(Moves);
return moves;
}
if (minLevel > maxLevel)
return moves;
int start = Array.FindIndex(Levels, z => z >= minLevel);
if (start < 0)
return moves;
int end = Array.FindLastIndex(Levels, z => z <= maxLevel);
if (end < 0)
return moves;
for (int i = start; i < end + 1; i++)
moves.Add(Moves[i]);
return moves;
}
///
/// Gets the moves a Pokémon can learn between the specified level range as a list.
///
/// Maximum level
/// Minimum level
/// Array of Move IDs
public List GetMoveList(int maxLevel, int minLevel = 0)
{
var list = new List();
return AddMoves(list, maxLevel, minLevel);
}
/// 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.
/// Array of Move IDs
public int[] GetEncounterMoves(int level)
{
const int count = 4;
var moves = new int[count];
return GetEncounterMoves(level, moves);
}
/// 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 int[] GetEncounterMoves(int level, int[] moves, int ctr = 0)
{
for (int i = 0; i < Moves.Length; i++)
{
if (Levels[i] > level)
break;
int move = Moves[i];
bool alreadyHasMove = false;
foreach (int m in moves) if (m == move) { alreadyHasMove = true; break; }
if (alreadyHasMove)
continue;
moves[ctr++] = move;
ctr &= 3;
}
return moves;
}
public IList GetUniqueMovesLearned(IEnumerable seed, int maxLevel, int minLevel = 0)
{
int start = Array.FindIndex(Levels, z => z >= minLevel);
int end = Array.FindLastIndex(Levels, z => z <= maxLevel);
var list = new List(seed);
for (int i = start; i <= end; i++)
{
if (!list.Contains(Moves[i]))
list.Add(Moves[i]);
}
return list;
}
/// Returns the index of the lowest level move if the Pokémon were encountered at the specified level.
/// Helps determine the minimum level an encounter can be at.
/// The level the Pokémon was encountered at.
/// Array of Move IDs
public int GetMinMoveLevel(int level)
{
if (Levels.Length == 0)
return 1;
int end = Array.FindLastIndex(Levels, z => z <= level);
return Math.Max(end - 4, 1);
}
private Dictionary? Learn;
private Dictionary GetDictionary()
{
var dict = new Dictionary();
for (int i = 0; i < Moves.Length; i++)
{
if (!dict.ContainsKey(Moves[i]))
dict.Add(Moves[i], Levels[i]);
}
return dict;
}
/// 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(int move)
{
return (Learn ??= GetDictionary()).TryGetValue(move, out var level) ? level : -1;
}
/// Returns the level that a Pokémon can learn the specified move.
/// Move ID
/// Minimum level to start looking at.
/// Level the move is learned at. If the result is below 0, the move cannot be learned by leveling up.
public int GetLevelLearnMove(int move, int min)
{
for (int i = 0; i < Moves.Length; i++)
{
if (move != Moves[i])
continue;
var lv = Levels[i];
if (lv >= min)
return lv;
}
return -1;
}
}
}