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 23:15:27 +00:00
|
|
|
using System.Collections.Generic;
|
2020-10-25 16:30:42 +00:00
|
|
|
using static PKHeX.Core.Species;
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Contains logic for Alternate Form information.
|
|
|
|
/// </summary>
|
|
|
|
public static class FormInfo
|
2020-10-25 16:15:16 +00:00
|
|
|
{
|
2020-10-25 16:30:42 +00:00
|
|
|
/// <summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// Checks if the form cannot exist outside of a Battle.
|
2020-10-25 16:30:42 +00:00
|
|
|
/// </summary>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
|
|
|
/// <param name="format">Current generation format</param>
|
|
|
|
/// <returns>True if it can only exist in a battle, false if it can exist outside of battle.</returns>
|
2022-08-27 19:53:30 +00:00
|
|
|
public static bool IsBattleOnlyForm(ushort species, byte form, int format)
|
2020-10-25 16:15:16 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!BattleOnly.Contains(species))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Some species have battle only forms as well as out-of-battle forms (other than base form).
|
|
|
|
switch (species)
|
2020-10-25 16:15:16 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case (int)Slowbro when form == 2 && format >= 8: // this one is OK, Galarian Slowbro (not a Mega)
|
|
|
|
case (int)Darmanitan when form == 2 && format >= 8: // this one is OK, Galarian non-Zen
|
|
|
|
case (int)Zygarde when form < 4: // Zygarde Complete
|
|
|
|
case (int)Mimikyu when form == 2: // Totem disguise Mimikyu
|
|
|
|
case (int)Necrozma when form < 3: // Only mark Ultra Necrozma as Battle Only
|
2020-10-25 16:15:16 +00:00
|
|
|
return false;
|
2022-06-18 18:04:24 +00:00
|
|
|
case (int)Minior: return form < 7; // Minior Shields-Down
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
default:
|
|
|
|
return form != 0;
|
2020-10-25 16:15:16 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Reverts the Battle Form to the form it would have outside of Battle.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>Only call this if you've already checked that <see cref="IsBattleOnlyForm"/> returns true.</remarks>
|
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
|
|
|
/// <param name="format">Current generation format</param>
|
|
|
|
/// <returns>Suggested alt form value.</returns>
|
2022-08-27 06:43:36 +00:00
|
|
|
public static byte GetOutOfBattleForm(ushort species, byte form, int format) => species switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
(int)Darmanitan => (byte)(form & 2),
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Zygarde when format > 6 => 3,
|
2022-08-27 06:43:36 +00:00
|
|
|
(int)Minior => (byte)(form + 7),
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => 0,
|
|
|
|
};
|
|
|
|
|
2022-11-25 01:42:17 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Indicates if the entity should be prevented from being traded away.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
|
|
|
/// <param name="formArg">Entity form argument</param>
|
|
|
|
/// <param name="format">Current generation format</param>
|
|
|
|
/// <returns>True if it trading should be disallowed.</returns>
|
|
|
|
public static bool IsUntradable(ushort species, byte form, uint formArg, int format) => species switch
|
|
|
|
{
|
|
|
|
(int)Koraidon or (int)Miraidon when formArg == 1 => true, // Ride-able Box Legend
|
|
|
|
(int)Pikachu when form == 8 && format == 7 => true, // Let's Go Pikachu Starter
|
|
|
|
(int)Eevee when form == 1 && format == 7 => true, // Let's Go Eevee Starter
|
|
|
|
_ => IsFusedForm(species, form, format),
|
|
|
|
};
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="form"/> is a fused form, which indicates it cannot be traded away.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
|
|
|
/// <param name="format">Current generation format</param>
|
|
|
|
/// <returns>True if it is a fused species-form, false if it is not fused.</returns>
|
2022-08-27 19:53:30 +00:00
|
|
|
public static bool IsFusedForm(ushort species, byte form, int format) => species switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
(int)Kyurem when form != 0 && format >= 5 => true,
|
|
|
|
(int)Necrozma when form != 0 && format >= 7 => true,
|
|
|
|
(int)Calyrex when form != 0 && format >= 8 => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// <summary>Checks if the form may be different than the original encounter detail.</summary>
|
|
|
|
/// <param name="species">Original species</param>
|
|
|
|
/// <param name="oldForm">Original form</param>
|
|
|
|
/// <param name="newForm">Current form</param>
|
2022-12-08 07:28:47 +00:00
|
|
|
/// <param name="origin">Encounter context</param>
|
|
|
|
/// <param name="current">Current context</param>
|
|
|
|
public static bool IsFormChangeable(ushort species, byte oldForm, byte newForm, EntityContext origin, EntityContext current)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (FormChange.Contains(species))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Zygarde Form Changing
|
|
|
|
// Gen6: Introduced; no form changing.
|
|
|
|
// Gen7: Form changing introduced; can only change to Form 2/3 (Power Construct), never to 0/1 (Aura Break). A form-1 can be boosted to form-0.
|
|
|
|
// Gen8: Form changing improved; can pick any Form & Ability combination.
|
|
|
|
if (species == (int)Zygarde)
|
2020-11-02 22:46:26 +00:00
|
|
|
{
|
2022-12-08 07:28:47 +00:00
|
|
|
return current switch
|
2020-11-02 22:46:26 +00:00
|
|
|
{
|
2022-12-08 07:28:47 +00:00
|
|
|
EntityContext.Gen6 => false,
|
|
|
|
EntityContext.Gen7 => newForm >= 2 || (oldForm == 1 && newForm == 0),
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => true,
|
|
|
|
};
|
2020-11-02 22:46:26 +00:00
|
|
|
}
|
2022-12-08 07:28:47 +00:00
|
|
|
if (species is (int)Deerling or (int)Sawsbuck)
|
|
|
|
{
|
|
|
|
return origin switch
|
|
|
|
{
|
|
|
|
EntityContext.Gen5 => true,
|
|
|
|
EntityContext.Gen9 => true,
|
|
|
|
_ => false, // todo home sv
|
|
|
|
};
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-02 22:46:26 +00:00
|
|
|
|
2022-11-25 22:12:19 +00:00
|
|
|
public static bool IsFormChangeEgg(ushort species) => System.Array.IndexOf(FormChangeEgg, species) != -1;
|
|
|
|
|
|
|
|
private static readonly ushort[] FormChangeEgg =
|
|
|
|
{
|
|
|
|
(int)Burmy,
|
|
|
|
(int)Furfrou,
|
|
|
|
(int)Oricorio,
|
|
|
|
};
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Species that can change between their forms, regardless of origin.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>Excludes Zygarde as it has special conditions. Check separately.</remarks>
|
2022-11-25 22:12:19 +00:00
|
|
|
private static readonly HashSet<ushort> FormChange = new(FormChangeEgg)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// Sometimes considered for wild encounters
|
|
|
|
(int)Rotom,
|
|
|
|
|
|
|
|
(int)Deoxys,
|
|
|
|
(int)Dialga,
|
|
|
|
(int)Palkia,
|
|
|
|
(int)Giratina,
|
|
|
|
(int)Shaymin,
|
|
|
|
(int)Arceus,
|
|
|
|
(int)Tornadus,
|
|
|
|
(int)Thundurus,
|
|
|
|
(int)Landorus,
|
|
|
|
(int)Kyurem,
|
|
|
|
(int)Keldeo,
|
|
|
|
(int)Genesect,
|
|
|
|
(int)Hoopa,
|
|
|
|
(int)Silvally,
|
|
|
|
(int)Necrozma,
|
|
|
|
(int)Calyrex,
|
|
|
|
(int)Enamorus,
|
|
|
|
};
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Species that have an alternate form that cannot exist outside of battle.
|
|
|
|
/// </summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
private static readonly HashSet<ushort> BattleForms = new()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
(int)Castform,
|
|
|
|
(int)Cherrim,
|
|
|
|
(int)Darmanitan,
|
|
|
|
(int)Meloetta,
|
|
|
|
(int)Aegislash,
|
|
|
|
(int)Xerneas,
|
|
|
|
(int)Zygarde,
|
|
|
|
|
|
|
|
(int)Wishiwashi,
|
|
|
|
(int)Minior,
|
|
|
|
(int)Mimikyu,
|
|
|
|
|
|
|
|
(int)Cramorant,
|
|
|
|
(int)Morpeko,
|
|
|
|
(int)Eiscue,
|
|
|
|
|
|
|
|
(int)Zacian,
|
|
|
|
(int)Zamazenta,
|
|
|
|
(int)Eternatus,
|
2022-11-25 01:42:17 +00:00
|
|
|
|
|
|
|
(int)Palafin,
|
2022-06-18 18:04:24 +00:00
|
|
|
};
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Species that have a mega form that cannot exist outside of battle.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>Using a held item to change form during battle, via an in-battle transformation feature.</remarks>
|
2022-08-27 06:43:36 +00:00
|
|
|
private static readonly HashSet<ushort> BattleMegas = new()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// XY
|
|
|
|
(int)Venusaur, (int)Charizard, (int)Blastoise,
|
|
|
|
(int)Alakazam, (int)Gengar, (int)Kangaskhan, (int)Pinsir,
|
|
|
|
(int)Gyarados, (int)Aerodactyl, (int)Mewtwo,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Ampharos, (int)Scizor, (int)Heracross, (int)Houndoom, (int)Tyranitar,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Blaziken, (int)Gardevoir, (int)Mawile, (int)Aggron, (int)Medicham,
|
|
|
|
(int)Manectric, (int)Banette, (int)Absol, (int)Latios, (int)Latias,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Garchomp, (int)Lucario, (int)Abomasnow,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// AO
|
|
|
|
(int)Beedrill, (int)Pidgeot, (int)Slowbro,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Steelix,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Sceptile, (int)Swampert, (int)Sableye, (int)Sharpedo, (int)Camerupt,
|
|
|
|
(int)Altaria, (int)Glalie, (int)Salamence, (int)Metagross, (int)Rayquaza,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Lopunny, (int)Gallade,
|
|
|
|
(int)Audino, (int)Diancie,
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// USUM
|
|
|
|
(int)Necrozma, // Ultra Necrozma
|
|
|
|
};
|
2020-10-25 16:15:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Species that have a primal form that cannot exist outside of battle.
|
|
|
|
/// </summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
private static readonly HashSet<ushort> BattlePrimals = new() { (int)Kyogre, (int)Groudon };
|
2020-11-27 19:51:02 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static readonly HashSet<ushort> BattleOnly = GetBattleFormSet();
|
2020-11-27 19:51:02 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static HashSet<ushort> GetBattleFormSet()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
var hs = new HashSet<ushort>(BattleForms);
|
2022-06-18 18:04:24 +00:00
|
|
|
hs.UnionWith(BattleMegas);
|
|
|
|
hs.UnionWith(BattlePrimals);
|
|
|
|
return hs;
|
|
|
|
}
|
2020-11-27 19:51:02 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="form"/> for the <see cref="species"/> is a Totem form.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
2022-12-18 08:16:29 +00:00
|
|
|
/// <param name="context">Current generation format</param>
|
|
|
|
public static bool IsTotemForm(ushort species, byte form, EntityContext context) => context == EntityContext.Gen7 && IsTotemForm(species, form);
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="form"/> for the <see cref="species"/> is a Totem form.
|
|
|
|
/// </summary>
|
2022-12-18 08:16:29 +00:00
|
|
|
/// <remarks>Use <see cref="IsTotemForm(ushort,byte,EntityContext)"/> if you aren't 100% sure the format is 7.</remarks>
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
2022-08-27 19:53:30 +00:00
|
|
|
public static bool IsTotemForm(ushort species, byte form)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (form == 0)
|
|
|
|
return false;
|
|
|
|
if (!Legal.Totem_USUM.Contains(species))
|
|
|
|
return false;
|
|
|
|
if (species == (int)Mimikyu)
|
|
|
|
return form is 2 or 3;
|
|
|
|
if (Legal.Totem_Alolan.Contains(species))
|
|
|
|
return form == 2;
|
|
|
|
return form == 1;
|
|
|
|
}
|
2020-11-27 19:51:02 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the base <see cref="form"/> for the <see cref="species"/> when the Totem form is reverted (on transfer).
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
2022-08-27 06:43:36 +00:00
|
|
|
public static byte GetTotemBaseForm(ushort species, byte form)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (species == (int)Mimikyu)
|
2022-08-27 06:43:36 +00:00
|
|
|
return 0;
|
|
|
|
return --form;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-11-27 19:51:02 +00:00
|
|
|
|
2022-12-18 08:16:29 +00:00
|
|
|
public static bool IsLordForm(ushort species, byte form, EntityContext context)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-12-18 08:16:29 +00:00
|
|
|
if (context != EntityContext.Gen8a)
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
return IsLordForm(species, form);
|
|
|
|
}
|
2020-11-27 19:51:02 +00:00
|
|
|
|
2022-08-27 19:53:30 +00:00
|
|
|
private static bool IsLordForm(ushort species, byte form) => form != 0 && species switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
(int)Arcanine when form == 2 => true,
|
|
|
|
(int)Electrode when form == 2 => true,
|
|
|
|
(int)Lilligant when form == 2 => true,
|
|
|
|
(int)Avalugg when form == 2 => true,
|
|
|
|
(int)Kleavor when form == 1 => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="form"/> exists for the <see cref="species"/> without having an associated <see cref="PersonalInfo"/> index.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="species">Entity species</param>
|
|
|
|
/// <param name="form">Entity form</param>
|
|
|
|
/// <param name="format">Current generation format</param>
|
|
|
|
/// <seealso cref="HasFormValuesNotIndicatedByPersonal"/>
|
2022-08-27 19:53:30 +00:00
|
|
|
public static bool IsValidOutOfBoundsForm(ushort species, byte form, int format) => (Species) species switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
Unown => form < (format == 2 ? 26 : 28), // A-Z : A-Z?!
|
|
|
|
Mothim => form < 3, // Burmy base form is kept
|
|
|
|
|
|
|
|
Scatterbug => form <= Vivillon3DS.MaxWildFormID, // Vivillon Pre-evolutions
|
|
|
|
Spewpa => form <= Vivillon3DS.MaxWildFormID, // Vivillon Pre-evolutions
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if the <see cref="PKM"/> data should have a drop-down selection visible for the <see cref="PKM.Form"/> value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pi">Game specific personal info</param>
|
|
|
|
/// <param name="species"><see cref="Species"/> ID</param>
|
|
|
|
/// <param name="format"><see cref="PKM.Form"/> ID</param>
|
|
|
|
/// <returns>True if has forms that can be provided by <see cref="FormConverter.GetFormList"/>, otherwise false for none.</returns>
|
2022-08-27 06:43:36 +00:00
|
|
|
public static bool HasFormSelection(IPersonalFormInfo pi, ushort species, int format)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (format <= 3 && species != (int)Unown)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (HasFormValuesNotIndicatedByPersonal.Contains(species))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int count = pi.FormCount;
|
|
|
|
return count > 1;
|
2020-10-25 16:15:16 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <seealso cref="IsValidOutOfBoundsForm"/>
|
|
|
|
/// </summary>
|
2022-08-27 06:43:36 +00:00
|
|
|
private static readonly HashSet<ushort> HasFormValuesNotIndicatedByPersonal = new()
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
(int)Unown,
|
|
|
|
(int)Mothim, // (Burmy form is not cleared on evolution)
|
|
|
|
(int)Scatterbug, (int)Spewpa, // Vivillon pre-evos
|
|
|
|
};
|
2020-10-25 16:15:16 +00:00
|
|
|
}
|