Invert breeding method result returns

clean:
if (!logical) return;

Existing logic had methods named to return true if fail, now method checks for passing, thus the logic now follows the above pattern.
This commit is contained in:
Kurt 2021-01-01 19:36:54 -08:00
parent 09089da14e
commit 2648560684
4 changed files with 35 additions and 15 deletions

View file

@ -10,6 +10,10 @@ namespace PKHeX.Core
/// </summary> /// </summary>
public static class Breeding public static class Breeding
{ {
/// <summary>
/// Checks if the game has a Daycare, and returns true if it does.
/// </summary>
/// <param name="game">Version ID to check for.</param>
public static bool CanGameGenerateEggs(GameVersion game) => GamesWithEggs.Contains(game); public static bool CanGameGenerateEggs(GameVersion game) => GamesWithEggs.Contains(game);
private static readonly HashSet<GameVersion> GamesWithEggs = new() private static readonly HashSet<GameVersion> GamesWithEggs = new()
@ -83,31 +87,47 @@ namespace PKHeX.Core
_ => Array.Empty<int>(), _ => Array.Empty<int>(),
}; };
public static bool NoHatchFromEggForm(int species, int form, int generation) /// <summary>
/// Checks if the <see cref="species"/> can be obtained from a daycare egg.
/// </summary>
/// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks>
public static bool CanHatchAsEgg(int species) => !NoHatchFromEgg.Contains(species);
/// <summary>
/// Checks if the <see cref="species"/>-<see cref="form"/> can exist as a hatched egg in the requested <see cref="generation"/>.
/// </summary>
/// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks>
public static bool CanHatchAsEgg(int species, int form, int generation)
{ {
if (form == 0) if (form == 0)
return false;
if (FormInfo.IsTotemForm(species, form, generation))
return true; return true;
if (FormInfo.IsTotemForm(species, form, generation))
return false;
if (species == (int)Pichu) if (species == (int)Pichu)
return true; // can't get Spiky Ear Pichu eggs return false; // can't get Spiky Ear Pichu eggs
if (species == (int)Sinistea || species == (int)Polteageist) // Antique = impossible if (species == (int)Sinistea || species == (int)Polteageist) // Antique = impossible
return true; // can't get Antique eggs return false; // can't get Antique eggs
return false;
return true;
} }
public static bool NoHatchFromEggForm(int species, int form, GameVersion game) /// <summary>
/// Checks if the <see cref="species"/>-<see cref="form"/> can exist as a hatched egg in the requested <see cref="game"/>.
/// </summary>
/// <remarks>Chained with the other 2 overloads for incremental checks with different parameters.</remarks>
public static bool CanHatchAsEgg(int species, int form, GameVersion game)
{ {
// Sanity check form for origin // Sanity check form for origin
var gameInfo = GameData.GetPersonal(game); var gameInfo = GameData.GetPersonal(game);
var entry = gameInfo.GetFormEntry(species, form); var entry = gameInfo.GetFormEntry(species, form);
return form >= entry.FormCount && !(species == (int)Rotom && form <= 5); return form < entry.FormCount || (species == (int)Rotom && form <= 5);
} }
/// <summary> /// <summary>
/// Species that cannot hatch from an egg. /// Species that cannot hatch from an egg.
/// </summary> /// </summary>
public static readonly HashSet<int> NoHatchFromEgg = new() private static readonly HashSet<int> NoHatchFromEgg = new()
{ {
// Gen1 // Gen1
(int)Ditto, (int)Ditto,

View file

@ -18,13 +18,13 @@ namespace PKHeX.Core
public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, IReadOnlyList<EvoCriteria> chain, bool all = false) public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, IReadOnlyList<EvoCriteria> chain, bool all = false)
{ {
int species = pkm.Species; int species = pkm.Species;
if (Breeding.NoHatchFromEgg.Contains(species)) if (!Breeding.CanHatchAsEgg(species))
yield break; yield break;
int generation = pkm.Generation; int generation = pkm.Generation;
if (generation <= 2) if (generation <= 2)
yield break; // can't get eggs; if generating Gen2 eggs, use the other generator. yield break; // can't get eggs; if generating Gen2 eggs, use the other generator.
if (Breeding.NoHatchFromEggForm(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
// version is a true indicator for all generation 3-5 origins // version is a true indicator for all generation 3-5 origins
@ -36,7 +36,7 @@ namespace PKHeX.Core
int max = GetMaxSpeciesOrigin(generation); int max = GetMaxSpeciesOrigin(generation);
var e = EvoBase.GetBaseSpecies(chain, 0); var e = EvoBase.GetBaseSpecies(chain, 0);
if (e.Species <= max && !Breeding.NoHatchFromEggForm(e.Species, e.Form, ver)) if (e.Species <= max && Breeding.CanHatchAsEgg(e.Species, e.Form, ver))
{ {
yield return new EncounterEgg(e.Species, e.Form, lvl, generation, ver); yield return new EncounterEgg(e.Species, e.Form, lvl, generation, ver);
if (generation > 5 && (pkm.WasTradedEgg || all) && HasOtherGamePair(ver)) if (generation > 5 && (pkm.WasTradedEgg || all) && HasOtherGamePair(ver))
@ -50,7 +50,7 @@ namespace PKHeX.Core
if (o.Species == e.Species) if (o.Species == e.Species)
yield break; yield break;
if (o.Species <= max && !Breeding.NoHatchFromEggForm(o.Species, o.Form, ver)) if (o.Species <= max && Breeding.CanHatchAsEgg(o.Species, o.Form, ver))
{ {
yield return new EncounterEggSplit(o.Species, o.Form, lvl, generation, ver, e.Species); yield return new EncounterEggSplit(o.Species, o.Form, lvl, generation, ver, e.Species);
if (generation > 5 && (pkm.WasTradedEgg || all) && HasOtherGamePair(ver)) if (generation > 5 && (pkm.WasTradedEgg || all) && HasOtherGamePair(ver))

View file

@ -18,7 +18,7 @@ namespace PKHeX.Core
public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, IReadOnlyList<EvoCriteria> chain, bool all = false) public static IEnumerable<EncounterEgg> GenerateEggs(PKM pkm, IReadOnlyList<EvoCriteria> chain, bool all = false)
{ {
int species = pkm.Species; int species = pkm.Species;
if (Breeding.NoHatchFromEgg.Contains(species)) if (!Breeding.CanHatchAsEgg(species))
yield break; yield break;
var canBeEgg = all || GetCanBeEgg(pkm); var canBeEgg = all || GetCanBeEgg(pkm);

View file

@ -89,7 +89,7 @@ namespace PKHeX.Core
private static CheckResult VerifyEncounterEgg(PKM pkm, int gen, bool checkSpecies = true) private static CheckResult VerifyEncounterEgg(PKM pkm, int gen, bool checkSpecies = true)
{ {
// Check Species // Check Species
if (checkSpecies && Breeding.NoHatchFromEgg.Contains(pkm.Species)) if (checkSpecies && !Breeding.CanHatchAsEgg(pkm.Species))
return new CheckResult(Severity.Invalid, LEggSpecies, CheckIdentifier.Encounter); return new CheckResult(Severity.Invalid, LEggSpecies, CheckIdentifier.Encounter);
return gen switch return gen switch