PKHeX/PKHeX.Core/PKM/Interfaces/IBattleVersion.cs
Kurt 5bcccc6d92
HOME 2.0.0: Handle conversion behavior & restrictions (#3506)
* Revises legality checks to account for traveling between the three game islands (PLA/BDSP/SWSH)
* Adds conversion mechanisms between the three formats, as well as flexible conversion options to backfill missing data (thanks GameFreak/ILCA for opting for lossy conversion instead of updating the games).
* Adds API abstractions for HOME data storage format (EKH/PKH format 1, aka EH1/PH1).
* Revises some APIs for better usage:
  - `PKM` now exposes a `Context` to indicate the isolation context for legality purposes.
  - Some method signatures have changed to accept `Context` or `GameVersion` instead of a vague `int` for Generation.
  - Evolution History is now tracked in the Legality parse for specific contexts, rather than only per generation.
2022-05-30 21:43:52 -07:00

53 lines
2 KiB
C#

namespace PKHeX.Core
{
/// <summary>
/// Interface that exposes a <see cref="BattleVersion"/> for allowing a Pokémon into ranked battles if it originated from a prior game.
/// </summary>
public interface IBattleVersion
{
/// <summary>
/// Indicates which <see cref="GameVersion"/> the Pokémon's moves were reset on.
/// </summary>
byte BattleVersion { get; set; }
}
public static class BattleVersionExtensions
{
public static bool IsBattleVersionValid<T>(this T pk, EvolutionHistory h) where T : PKM, IBattleVersion => pk.BattleVersion switch
{
0 => true,
(int)GameVersion.SW or (int)GameVersion.SH => !(pk.SWSH || pk.BDSP || pk.LA) && pk.HasVisitedSWSH(h.Gen8),
_ => false,
};
/// <summary>
/// Resets the <see cref="PKM"/>'s moves and sets the requested version.
/// </summary>
/// <param name="v">Reference to the object to set the <see cref="version"/></param>
/// <param name="pk">Reference to the same object that gets moves reset</param>
/// <param name="version">Version to apply</param>
public static void AdaptToBattleVersion(this IBattleVersion v, PKM pk, GameVersion version)
{
var moves = MoveLevelUp.GetEncounterMoves(pk, pk.CurrentLevel, version);
pk.Move1 = pk.Move2 = pk.Move3 = pk.Move4 = 0;
pk.RelearnMove1 = pk.RelearnMove2 = pk.RelearnMove3 = pk.RelearnMove4 = 0;
pk.SetMoves(moves);
pk.FixMoves();
v.BattleVersion = (byte) version;
}
public static int GetMinGeneration(this IBattleVersion v)
{
var ver = v.BattleVersion;
if (ver == 0)
return 1;
var game = (GameVersion) ver;
if (!game.IsValidSavedVersion())
return -1;
var gen = game.GetGeneration();
if (gen >= 8)
return gen;
return -1;
}
}
}