Extract generation traversal list fetch to static class

This commit is contained in:
Kurt 2021-04-17 13:09:09 -07:00
parent 8e52d2fddb
commit 893f22a664
3 changed files with 56 additions and 49 deletions

View file

@ -163,7 +163,7 @@ namespace PKHeX.Core
if (vcBump)
pk.Version = (int)GameVersion.C;
var gens = VerifyCurrentMoves.GetGenMovesCheckOrder(pk, origin);
var gens = GenerationTraversal.GetVisitedGenerationOrder(pk, origin);
var canlearn = gens.SelectMany(z => GetMovesForGeneration(pk, chain, z));
var result = moves.Except(canlearn).Where(z => z != 0).ToArray();

View file

@ -0,0 +1,53 @@
using System;
namespace PKHeX.Core
{
public static class GenerationTraversal
{
/// <summary>
/// Gets the generation numbers in descending order for iterating over.
/// </summary>
public static int[] GetVisitedGenerationOrder(PKM pkm, int origin)
{
if (pkm.Format < 3)
return GetVisitedGenerationOrderGB(pkm, pkm.Format);
if (pkm.VC)
return GetVisitedGenerationOrderVC(pkm, origin);
return GetVisitedGenerationOrder(pkm.Format, origin);
}
private static int[] GetVisitedGenerationOrderVC(PKM pkm, int origin)
{
// VC case: check transfer games in reverse order (8, 7..) then past games.
int[] xfer = GetVisitedGenerationOrder(pkm.Format, 7);
int[] past = GetVisitedGenerationOrderGB(pkm, origin);
int end = xfer.Length;
Array.Resize(ref xfer, xfer.Length + past.Length);
past.CopyTo(xfer, end);
return xfer;
}
private static readonly int[] G2 = { 2 };
private static readonly int[] G12 = { 1, 2 };
private static readonly int[] G21 = { 2, 1 };
private static int[] GetVisitedGenerationOrderGB(PKM pkm, int originalGeneration)
{
if (originalGeneration == 2)
return pkm.Korean ? G2 : G21;
return G12; // RBY
}
private static int[] GetVisitedGenerationOrder(int start, int end)
{
if (end < 0)
return Array.Empty<int>();
if (start <= end)
return new[] { start };
var order = new int[start - end + 1];
for (int i = 0; i < order.Length; i++)
order[i] = start - i;
return order;
}
}
}

View file

@ -264,13 +264,13 @@ namespace PKHeX.Core
// Encapsulate arguments to simplify method calls
var moveInfo = new LearnInfo(pkm, source);
// Check moves going backwards, marking the move valid in the most current generation when it can be learned
int[] generations = GetGenMovesCheckOrder(pkm, info.EncounterOriginal.Generation);
int[] generations = GenerationTraversal.GetVisitedGenerationOrder(pkm, info.EncounterOriginal.Generation);
if (pkm.Format <= 2)
generations = generations.Where(z => z < info.EncounterMoves.LevelUpMoves.Length).ToArray();
if (reset != 0)
generations = generations.Where(z => z >= reset).ToArray();
int lastgen = generations.LastOrDefault();
int lastgen = generations.Length == 0 ? 0 : generations[generations.Length - 1];
foreach (var gen in generations)
{
ParseMovesByGeneration(pkm, res, gen, info, moveInfo, lastgen);
@ -813,51 +813,5 @@ namespace PKHeX.Core
return;
EncounterMoves.LevelUpMoves[2] = MoveList.GetValidMoves(pkm, info.EvoChainsAllGens[2], generation: 2, minLvLG2: defaultLvlG2, types: MoveSourceType.LevelUp).ToList();
}
/// <summary>
/// Gets the generation numbers in descending order for iterating over.
/// </summary>
public static int[] GetGenMovesCheckOrder(PKM pkm, int origin)
{
if (pkm.Format < 3)
return GetGenMovesCheckOrderGB(pkm, pkm.Format);
if (pkm.VC)
return GetGenMovesOrderVC(pkm);
return GetGenMovesOrder(pkm.Format, origin);
}
private static int[] GetGenMovesOrderVC(PKM pkm)
{
// VC case: check transfer games in reverse order (8, 7..) then past games.
int[] xfer = GetGenMovesOrder(pkm.Format, 7);
int[] past = GetGenMovesCheckOrderGB(pkm, pkm.Generation);
int end = xfer.Length;
Array.Resize(ref xfer, xfer.Length + past.Length);
past.CopyTo(xfer, end);
return xfer;
}
private static readonly int[] G2 = {2};
private static readonly int[] G12 = {1, 2};
private static readonly int[] G21 = {2, 1};
private static int[] GetGenMovesCheckOrderGB(PKM pkm, int originalGeneration)
{
if (originalGeneration == 2)
return pkm.Korean ? G2 : G21;
return G12; // RBY
}
private static int[] GetGenMovesOrder(int start, int end)
{
if (end < 0)
return Array.Empty<int>();
if (start <= end)
return new[] {start};
var order = new int[start - end + 1];
for (int i = 0; i < order.Length; i++)
order[i] = start - i;
return order;
}
}
}