Eggs: explicitly pass generation instead of calculating

This commit is contained in:
Kurt 2021-04-17 20:09:02 -07:00
parent 9171d7f87e
commit 0719320e33
10 changed files with 25 additions and 21 deletions

View file

@ -91,12 +91,16 @@ namespace PKHeX.Core
/// Checks if the <see cref="species"/> can be obtained from a daycare egg. /// Checks if the <see cref="species"/> can be obtained from a daycare egg.
/// </summary> /// </summary>
/// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks> /// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks>
/// <param name="species">Current species</param>
public static bool CanHatchAsEgg(int species) => !NoHatchFromEgg.Contains(species); public static bool CanHatchAsEgg(int species) => !NoHatchFromEgg.Contains(species);
/// <summary> /// <summary>
/// Checks if the <see cref="species"/>-<see cref="form"/> can exist as a hatched egg in the requested <see cref="generation"/>. /// Checks if the <see cref="species"/>-<see cref="form"/> can exist as a hatched egg in the requested <see cref="generation"/>.
/// </summary> /// </summary>
/// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks> /// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks>
/// <param name="species">Current species</param>
/// <param name="form">Current form</param>
/// <param name="generation">Generation of origin</param>
public static bool CanHatchAsEgg(int species, int form, int generation) public static bool CanHatchAsEgg(int species, int form, int generation)
{ {
if (form == 0) if (form == 0)
@ -116,6 +120,9 @@ namespace PKHeX.Core
/// Checks if the <see cref="species"/>-<see cref="form"/> can exist as a hatched egg in the requested <see cref="game"/>. /// Checks if the <see cref="species"/>-<see cref="form"/> can exist as a hatched egg in the requested <see cref="game"/>.
/// </summary> /// </summary>
/// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks> /// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks>
/// <param name="species">Current species</param>
/// <param name="form">Current form</param>
/// <param name="game">Game of origin</param>
public static bool CanHatchAsEgg(int species, int form, GameVersion game) public static bool CanHatchAsEgg(int species, int form, GameVersion game)
{ {
// Sanity check form for origin // Sanity check form for origin

View file

@ -187,7 +187,7 @@ namespace PKHeX.Core
if (pkm.Version != (int)GameVersion.CXD) // no eggs in C/XD if (pkm.Version != (int)GameVersion.CXD) // no eggs in C/XD
{ {
foreach (var z in GenerateEggs(pkm)) foreach (var z in GenerateEggs(pkm, 3))
yield return z; yield return z;
} }

View file

@ -51,7 +51,7 @@ namespace PKHeX.Core
} }
if (pkm.WasBredEgg) if (pkm.WasBredEgg)
{ {
foreach (var z in GenerateEggs(pkm)) foreach (var z in GenerateEggs(pkm, 4))
yield return z; yield return z;
} }
foreach (var z in GetValidEncounterTrades(pkm, chain)) foreach (var z in GetValidEncounterTrades(pkm, chain))

View file

@ -25,7 +25,7 @@ namespace PKHeX.Core
if (pkm.WasBredEgg) if (pkm.WasBredEgg)
{ {
foreach (var z in GenerateEggs(pkm)) foreach (var z in GenerateEggs(pkm, 5))
{ yield return z; ++ctr; } { yield return z; ++ctr; }
if (ctr == 0) yield break; if (ctr == 0) yield break;
} }

View file

@ -48,7 +48,7 @@ namespace PKHeX.Core
if (pkm.WasBredEgg) if (pkm.WasBredEgg)
{ {
foreach (var z in GenerateEggs(pkm)) foreach (var z in GenerateEggs(pkm, 6))
{ yield return z; ++ctr; } { yield return z; ++ctr; }
if (ctr == 0) yield break; if (ctr == 0) yield break;
} }

View file

@ -114,7 +114,7 @@ namespace PKHeX.Core
if (pkm.WasBredEgg) if (pkm.WasBredEgg)
{ {
foreach (var z in GenerateEggs(pkm)) foreach (var z in GenerateEggs(pkm, 7))
{ yield return z; ++ctr; } { yield return z; ++ctr; }
if (ctr == 0) yield break; if (ctr == 0) yield break;
} }

View file

@ -33,7 +33,7 @@ namespace PKHeX.Core
if (pkm.WasBredEgg) if (pkm.WasBredEgg)
{ {
foreach (var z in GenerateEggs(pkm)) foreach (var z in GenerateEggs(pkm, 8))
{ yield return z; ++ctr; } { yield return z; ++ctr; }
if (ctr == 0) yield break; if (ctr == 0) yield break;
} }

View file

@ -230,7 +230,7 @@ namespace PKHeX.Core
yield break; yield break;
var eggs = gen == 2 var eggs = gen == 2
? EncounterEggGenerator2.GenerateEggs(pk, chain, all: true) ? EncounterEggGenerator2.GenerateEggs(pk, chain, all: true)
: EncounterEggGenerator.GenerateEggs(pk, chain, all: true); : EncounterEggGenerator.GenerateEggs(pk, chain, gen, all: true);
foreach (var egg in eggs) foreach (var egg in eggs)
{ {
if (needs.Count == 0) if (needs.Count == 0)

View file

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using static PKHeX.Core.Legal; using static PKHeX.Core.Legal;
@ -7,23 +6,20 @@ namespace PKHeX.Core
{ {
public static class EncounterEggGenerator public static class EncounterEggGenerator
{ {
public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, bool all = false) public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, int generation, bool all = false)
{ {
var table = EvolutionTree.GetEvolutionTree(pkm, Math.Max(2, pkm.Format)); var table = EvolutionTree.GetEvolutionTree(pkm, pkm.Format);
int maxSpeciesOrigin = GetMaxSpeciesOrigin(pkm.Generation); int maxSpeciesOrigin = GetMaxSpeciesOrigin(generation);
var evos = table.GetValidPreEvolutions(pkm, maxLevel: 100, maxSpeciesOrigin: maxSpeciesOrigin, skipChecks: true); var evos = table.GetValidPreEvolutions(pkm, maxLevel: 100, maxSpeciesOrigin: maxSpeciesOrigin, skipChecks: true);
return GenerateEggs(pkm, evos, all); return GenerateEggs(pkm, evos, generation, all);
} }
public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, IReadOnlyList<EvoCriteria> chain, bool all = false) public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, IReadOnlyList<EvoCriteria> chain, int generation, bool all = false)
{ {
System.Diagnostics.Debug.Assert(generation >= 3); // if generating Gen2 eggs, use the other generator.
int species = pkm.Species; int species = pkm.Species;
if (!Breeding.CanHatchAsEgg(species)) if (!Breeding.CanHatchAsEgg(species))
yield break; yield break;
int generation = pkm.Generation;
if (generation <= 2)
yield break; // can't get eggs; if generating Gen2 eggs, use the other generator.
if (!Breeding.CanHatchAsEgg(species, pkm.Form, generation)) if (!Breeding.CanHatchAsEgg(species, pkm.Form, generation))
yield break; // can't originate from eggs yield break; // can't originate from eggs

View file

@ -108,7 +108,7 @@ namespace PKHeX.Core
/// Gets the current <see cref="PKM.RelearnMoves"/> array of four moves that might be legal. /// Gets the current <see cref="PKM.RelearnMoves"/> array of four moves that might be legal.
/// </summary> /// </summary>
/// <remarks>Use <see cref="GetSuggestedRelearnMovesFromEncounter"/> instead of calling directly; this method just puts default values in without considering the final moveset.</remarks> /// <remarks>Use <see cref="GetSuggestedRelearnMovesFromEncounter"/> instead of calling directly; this method just puts default values in without considering the final moveset.</remarks>
public static IReadOnlyList<int> GetSuggestedRelearn(this IEncounterable enc, PKM pkm) public static IReadOnlyList<int> GetSuggestedRelearn(this IEncounterTemplate enc, PKM pkm)
{ {
if (VerifyRelearnMoves.ShouldNotHaveRelearnMoves(enc, pkm)) if (VerifyRelearnMoves.ShouldNotHaveRelearnMoves(enc, pkm))
return Empty; return Empty;
@ -148,7 +148,8 @@ namespace PKHeX.Core
// Split-breed species like Budew & Roselia may be legal for one, and not the other. // Split-breed species like Budew & Roselia may be legal for one, and not the other.
// If we're not a split-breed or are already legal, return. // If we're not a split-breed or are already legal, return.
var result = enc.GetEggRelearnMoves(parse, pkm); var result = enc.GetEggRelearnMoves(parse, pkm);
var split = Breeding.GetSplitBreedGeneration(enc.Generation); int generation = enc.Generation;
var split = Breeding.GetSplitBreedGeneration(generation);
if (!split.Contains(enc.Species) || enc.Generation <= 2) if (!split.Contains(enc.Species) || enc.Generation <= 2)
return result; return result;
@ -159,7 +160,7 @@ namespace PKHeX.Core
return result; return result;
// Try again with the other split-breed species if possible. // Try again with the other split-breed species if possible.
var incense = EncounterEggGenerator.GenerateEggs(tmp).FirstOrDefault(); var incense = EncounterEggGenerator.GenerateEggs(tmp, generation).FirstOrDefault();
if (incense is null || incense.Species == enc.Species) if (incense is null || incense.Species == enc.Species)
return result; return result;