PKHeX/PKHeX.Core/Legality/Moves/Breeding/EggSource.cs
Kurt 9166d0eb64
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves

In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.

The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.

The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.

* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 16:15:27 -07:00

205 lines
6.4 KiB
C#

using System;
using static PKHeX.Core.LearnMethod;
using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.Core;
/// <summary>
/// Egg Moveset Building Order for Generation 2
/// </summary>
public enum EggSource2 : byte
{
None,
/// <summary> Initial moveset for the egg's level. </summary>
Base,
/// <summary> Egg move inherited from the Father. </summary>
FatherEgg,
/// <summary> Technical Machine move inherited from the Father. </summary>
FatherTM,
/// <summary> Level Up move inherited from a parent. </summary>
ParentLevelUp,
/// <summary> Tutor move (Elemental Beam) inherited from a parent. </summary>
Tutor,
Max,
}
/// <summary>
/// Egg Moveset Building Order for Generation 3 &amp; 4
/// </summary>
public enum EggSource34 : byte
{
None,
/// <summary> Initial moveset for the egg's level. </summary>
Base,
/// <summary> Egg move inherited from the Father. </summary>
FatherEgg,
/// <summary> Technical Machine move inherited from the Father. </summary>
FatherTM,
/// <summary> Level Up move inherited from a parent. </summary>
ParentLevelUp,
Max,
/// <summary> Special Move applied at the end if certain conditions are satisfied. </summary>
VoltTackle,
}
/// <summary>
/// Egg Moveset Building Order for Generation 5
/// </summary>
public enum EggSource5 : byte
{
None,
/// <summary> Initial moveset for the egg's level. </summary>
Base,
/// <summary> Egg move inherited from the Father. </summary>
FatherEgg,
/// <summary> Technical Machine move inherited from the Father. </summary>
ParentLevelUp,
/// <summary> Technical Machine move inherited from the Father. </summary>
/// <remarks> After level up, unlike Gen3/4! </remarks>
FatherTM,
Max,
/// <summary> Special Move applied at the end if certain conditions are satisfied. </summary>
VoltTackle,
}
/// <summary>
/// Egg Moveset Building Order for Generation 6+
/// </summary>
public enum EggSource6 : byte
{
None,
/// <summary> Initial moveset for the egg's level. </summary>
Base,
/// <summary> Level Up move inherited from a parent. </summary>
ParentLevelUp,
/// <summary> Egg move inherited from a parent. </summary>
ParentEgg,
Max,
/// <summary> Special Move applied at the end if certain conditions are satisfied. </summary>
VoltTackle,
}
/// <summary>
/// Utility logic for converting a <see cref="MoveBreed"/> move result into a user friendly string.
/// </summary>
public static class EggSourceUtil
{
/// <summary>
/// Unboxes the parse result and returns a user friendly string for the move result.
/// </summary>
public static string GetSourceString(Array parse, int generation, int index)
{
if (index >= parse.Length)
return LMoveSourceEmpty;
return generation switch
{
2 => ((EggSource2[])parse)[index].GetSourceString(),
3 or 4 => ((EggSource34[])parse)[index].GetSourceString(),
5 => ((EggSource5[])parse)[index].GetSourceString(),
>= 6 => ((EggSource6[])parse)[index].GetSourceString(),
_ => LMoveSourceEmpty,
};
}
public static string GetSourceString(this EggSource2 source) => source switch
{
EggSource2.Base => LMoveRelearnEgg,
EggSource2.FatherEgg => LMoveEggInherited,
EggSource2.FatherTM => LMoveEggTMHM,
EggSource2.ParentLevelUp => LMoveEggLevelUp,
EggSource2.Tutor => LMoveEggInheritedTutor,
EggSource2.Max => "Any",
_ => LMoveEggInvalid,
};
public static string GetSourceString(this EggSource34 source) => source switch
{
EggSource34.Base => LMoveRelearnEgg,
EggSource34.FatherEgg => LMoveEggInherited,
EggSource34.FatherTM => LMoveEggTMHM,
EggSource34.ParentLevelUp => LMoveEggLevelUp,
EggSource34.Max => "Any",
EggSource34.VoltTackle => LMoveSourceSpecial,
_ => LMoveEggInvalid,
};
public static string GetSourceString(this EggSource5 source) => source switch
{
EggSource5.Base => LMoveRelearnEgg,
EggSource5.FatherEgg => LMoveEggInherited,
EggSource5.ParentLevelUp => LMoveEggLevelUp,
EggSource5.FatherTM => LMoveEggTMHM,
EggSource5.Max => "Any",
EggSource5.VoltTackle => LMoveSourceSpecial,
_ => LMoveEggInvalid,
};
public static string GetSourceString(this EggSource6 source) => source switch
{
EggSource6.Base => LMoveRelearnEgg,
EggSource6.ParentLevelUp => LMoveEggLevelUp,
EggSource6.ParentEgg => LMoveEggInherited,
EggSource6.Max => "Any",
EggSource6.VoltTackle => LMoveSourceSpecial,
_ => LMoveEggInvalid,
};
/// <summary>
/// Converts the parse result and returns a user friendly string for the move result.
/// </summary>
public static LearnMethod GetSource(byte value, int generation) => generation switch
{
2 => ((EggSource2)value).GetSource(),
3 or 4 => ((EggSource34)value).GetSource(),
5 => ((EggSource5)value).GetSource(),
>= 6 => ((EggSource6)value).GetSource(),
_ => None,
};
public static LearnMethod GetSource(this EggSource2 source) => source switch
{
EggSource2.Base => Initial,
EggSource2.FatherEgg => EggMove,
EggSource2.FatherTM => TMHM,
EggSource2.ParentLevelUp => InheritLevelUp,
EggSource2.Tutor => Tutor,
_ => None,
};
public static LearnMethod GetSource(this EggSource34 source) => source switch
{
EggSource34.Base => Initial,
EggSource34.FatherEgg => EggMove,
EggSource34.FatherTM => TMHM,
EggSource34.ParentLevelUp => InheritLevelUp,
EggSource34.VoltTackle => SpecialEgg,
_ => None,
};
public static LearnMethod GetSource(this EggSource5 source) => source switch
{
EggSource5.Base => Initial,
EggSource5.FatherEgg => EggMove,
EggSource5.ParentLevelUp => InheritLevelUp,
EggSource5.FatherTM => TMHM,
EggSource5.VoltTackle => SpecialEgg,
_ => None,
};
public static LearnMethod GetSource(this EggSource6 source) => source switch
{
EggSource6.Base => Initial,
EggSource6.ParentLevelUp => InheritLevelUp,
EggSource6.ParentEgg => EggMove,
EggSource6.VoltTackle => SpecialEgg,
_ => None,
};
}