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;
|
2018-06-24 05:00:01 +00:00
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies the <see cref="PKM.Ability"/> values.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class AbilityVerifier : Verifier
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.Ability;
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var result = VerifyAbility(data);
|
|
|
|
data.AddLine(result);
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private CheckResult VALID => GetValid(LAbilityFlag);
|
|
|
|
private CheckResult INVALID => GetInvalid(LAbilityMismatch);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private enum AbilityState : byte
|
|
|
|
{
|
|
|
|
CanMismatch,
|
|
|
|
MustMatch,
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private CheckResult VerifyAbility(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
2022-09-02 17:20:19 +00:00
|
|
|
var abilities = (IPersonalAbility12)data.PersonalInfo;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// Check ability is possible (within bounds)
|
|
|
|
int ability = pk.Ability;
|
2022-09-02 17:20:19 +00:00
|
|
|
int abilIndex = abilities.GetIndexOfAbility(ability);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (abilIndex < 0)
|
|
|
|
return GetInvalid(LAbilityUnexpected);
|
|
|
|
|
|
|
|
int format = pk.Format;
|
|
|
|
if (format >= 6)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-09-02 17:20:19 +00:00
|
|
|
var bitNum = pk.AbilityNumber;
|
|
|
|
if (!IsValidAbilityBits(bitNum))
|
2022-06-18 18:04:24 +00:00
|
|
|
return INVALID;
|
|
|
|
|
|
|
|
// Check AbilityNumber points to ability
|
2022-09-02 17:20:19 +00:00
|
|
|
int abilityIndex = bitNum >> 1;
|
|
|
|
if (abilityIndex >= abilities.AbilityCount || abilities.GetAbilityAtIndex(abilityIndex) != ability)
|
2022-06-18 18:04:24 +00:00
|
|
|
return INVALID;
|
|
|
|
|
|
|
|
// Check AbilityNumber for transfers without unique abilities
|
|
|
|
int gen = data.Info.Generation;
|
2022-09-02 17:20:19 +00:00
|
|
|
if (gen is 3 or 4 or 5 && bitNum != 4)
|
2020-10-10 19:30:57 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// To determine AbilityNumber [PK5->PK6], check if the first ability in Personal matches the ability.
|
|
|
|
// It is not possible to flip it to the other index as capsule requires unique abilities.
|
2022-09-02 17:20:19 +00:00
|
|
|
if (abilities.GetIsAbility12Same() && bitNum != 1)
|
2020-11-28 04:44:44 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check if any pre-evolution could have it flipped.
|
|
|
|
var evos = data.Info.EvoChainsAllGens.Gen6;
|
|
|
|
var pt = GameData.GetPersonal(pk.Context.GetSingleGameVersion());
|
|
|
|
if (!GetWasDual(evos, pt, pk))
|
|
|
|
return INVALID;
|
2020-11-28 04:44:44 +00:00
|
|
|
}
|
2020-10-10 19:30:57 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-10-10 19:30:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (format >= 8) // Ability Patch
|
|
|
|
{
|
|
|
|
var evos = data.Info.EvoChainsAllGens;
|
2022-09-02 17:20:19 +00:00
|
|
|
if (pk.AbilityNumber == 4 && IsAccessibleAbilityPatch(evos) && abilities is IPersonalAbility12H h)
|
2020-10-10 19:30:57 +00:00
|
|
|
{
|
2022-09-02 17:20:19 +00:00
|
|
|
if (CanAbilityPatch(format, h, pk.Species))
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetValid(LAbilityPatchUsed);
|
2021-04-22 20:47:33 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var e = data.EncounterOriginal;
|
2022-09-02 17:20:19 +00:00
|
|
|
if (e.Species != pk.Species)
|
|
|
|
{
|
|
|
|
var temp = (IPersonalAbility12H)PKX.Personal.GetFormEntry(e.Species, e.Form);
|
|
|
|
if (CanAbilityPatch(format, temp, e.Species))
|
|
|
|
return GetValid(LAbilityPatchUsed);
|
|
|
|
}
|
2021-12-09 07:43:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Verify later, it may be encountered with its hidden ability without using an ability patch.
|
2020-10-10 19:30:57 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-10-10 19:30:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var enc = data.EncounterMatch;
|
|
|
|
if (enc is MysteryGift {Generation: >= 4} g)
|
|
|
|
return VerifyAbilityMG(data, g, abilities);
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (format < 6)
|
|
|
|
return VerifyAbility345(data, enc, abilities, abilIndex);
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return VerifyAbility(data, abilities, abilIndex);
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
public static bool IsValidAbilityBits(int bitNum) => bitNum is 1 or 2 or 4;
|
2021-07-30 23:22:10 +00:00
|
|
|
|
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
|
|
|
private static bool GetWasDual(ReadOnlySpan<EvoCriteria> evos, IPersonalTable pt, ISpeciesForm pk)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
foreach (var evo in evos)
|
2021-03-21 00:49:16 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (evo.Species == pk.Species)
|
|
|
|
continue;
|
2021-03-21 00:49:16 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
var abilities = (IPersonalAbility12)pt.GetFormEntry(evo.Species, evo.Form);
|
|
|
|
if (CanAbilityCapsule(6, abilities))
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-21 00:49:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult VerifyAbility(LegalityAnalysis data, IPersonalAbility12 abilities, int abilIndex)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var enc = data.EncounterMatch;
|
|
|
|
var eabil = enc.Ability;
|
|
|
|
if (eabil >= 0)
|
|
|
|
{
|
|
|
|
if ((data.Entity.AbilityNumber == 4) != (eabil == AbilityPermission.OnlyHidden))
|
|
|
|
return GetInvalid(LAbilityHiddenFail);
|
|
|
|
if (eabil > 0)
|
|
|
|
return VerifyFixedAbility(data, abilities, AbilityState.CanMismatch, eabil, abilIndex);
|
2021-03-21 00:49:16 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var gen = enc.Generation;
|
|
|
|
return gen switch
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
5 => VerifyAbility5(data, enc, abilities),
|
|
|
|
6 => VerifyAbility6(data, enc),
|
|
|
|
7 => VerifyAbility7(data, enc),
|
|
|
|
8 when data.Entity.BDSP => VerifyAbility8BDSP(data, enc),
|
|
|
|
>=8 => VALID,
|
|
|
|
_ => CheckMatch(data.Entity, abilities, gen, AbilityState.CanMismatch, enc),
|
|
|
|
};
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult VerifyAbility345(LegalityAnalysis data, IEncounterable enc, IPersonalAbility12 abilities, int abilIndex)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
int format = pk.Format;
|
|
|
|
var state = AbilityState.MustMatch;
|
2022-09-02 17:20:19 +00:00
|
|
|
if (format is (3 or 4 or 5) && !abilities.GetIsAbility12Same()) // 3-4/5 and have 2 distinct abilities now
|
2022-06-18 18:04:24 +00:00
|
|
|
state = VerifyAbilityPreCapsule(data, abilities);
|
|
|
|
|
|
|
|
var encounterAbility = enc.Ability;
|
|
|
|
if (encounterAbility >= 0)
|
|
|
|
{
|
|
|
|
if ((pk.AbilityNumber == 4) != (encounterAbility == AbilityPermission.OnlyHidden))
|
|
|
|
return GetInvalid(LAbilityHiddenFail);
|
|
|
|
if (encounterAbility > 0)
|
|
|
|
return VerifyFixedAbility(data, abilities, state, encounterAbility, abilIndex);
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
int gen = enc.Generation;
|
|
|
|
if (gen == 5)
|
|
|
|
return VerifyAbility5(data, enc, abilities);
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return CheckMatch(pk, abilities, gen, state, enc);
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult VerifyFixedAbility(LegalityAnalysis data, IPersonalAbility12 abilities, AbilityState state, AbilityPermission encounterAbility, int abilIndex)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var enc = data.Info.EncounterMatch;
|
|
|
|
if (enc.Generation >= 6)
|
|
|
|
{
|
|
|
|
if (IsAbilityCapsuleModified(pk, abilities, encounterAbility, data.Info.EvoChainsAllGens))
|
|
|
|
return GetValid(LAbilityCapsuleUsed);
|
|
|
|
if (pk.AbilityNumber != 1 << encounterAbility.GetSingleValue())
|
|
|
|
return INVALID;
|
|
|
|
return VALID;
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if ((pk.AbilityNumber == 4) != (encounterAbility == AbilityPermission.OnlyHidden))
|
|
|
|
return GetInvalid(LAbilityHiddenFail);
|
|
|
|
|
|
|
|
bool hasEvolved = enc.Species != pk.Species;
|
|
|
|
if (hasEvolved && state != AbilityState.CanMismatch)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Evolving in Gen3 does not mutate the ability bit, so any mismatched abilities will stay mismatched.
|
|
|
|
if (enc.Generation == 3)
|
2018-10-06 18:24:46 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (encounterAbility.GetSingleValue() == abilIndex)
|
|
|
|
return VALID;
|
2018-10-06 18:24:46 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// If it is in a future game and does not match the fixed ability, then it must match the PID.
|
|
|
|
if (pk.Format != 3)
|
|
|
|
return GetPIDAbilityMatch(pk, abilities);
|
2021-03-20 18:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// No way to un-mismatch it while existing solely on Gen3 games.
|
|
|
|
return INVALID;
|
|
|
|
}
|
2021-03-20 18:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return CheckMatch(pk, abilities, enc.Generation, AbilityState.MustMatch, enc);
|
|
|
|
}
|
2021-03-20 18:22:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (encounterAbility.GetSingleValue() == abilIndex)
|
|
|
|
return VALID;
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.AbilityNumber == 1 << encounterAbility.GetSingleValue())
|
|
|
|
return VALID;
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (state == AbilityState.CanMismatch || encounterAbility == 0)
|
|
|
|
return CheckMatch(pk, abilities, enc.Generation, AbilityState.MustMatch, enc);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (IsAbilityCapsuleModified(pk, abilities, encounterAbility, data.Info.EvoChainsAllGens))
|
|
|
|
return GetValid(LAbilityCapsuleUsed);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return INVALID;
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private AbilityState VerifyAbilityPreCapsule(LegalityAnalysis data, IPersonalAbility12 abilities)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var info = data.Info;
|
|
|
|
// Gen4/5 origin
|
|
|
|
if (info.Generation != 3)
|
|
|
|
return AbilityState.MustMatch;
|
|
|
|
|
|
|
|
// Gen3 origin... a lot of edge cases to check.
|
|
|
|
var pk = data.Entity;
|
|
|
|
var format = pk.Format;
|
|
|
|
// CXD pokemon can have any ability without matching PID
|
|
|
|
if (format == 3)
|
|
|
|
{
|
|
|
|
if (pk.Version == (int)GameVersion.CXD)
|
|
|
|
return AbilityState.CanMismatch;
|
|
|
|
return AbilityState.MustMatch;
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Evovled in Gen4/5
|
|
|
|
if (pk.Species > Legal.MaxSpeciesID_3)
|
|
|
|
return AbilityState.MustMatch;
|
|
|
|
|
|
|
|
// If the species could not exist in Gen3, must match.
|
|
|
|
var g3 = info.EvoChainsAllGens.Gen3;
|
|
|
|
if (g3.Length == 0)
|
|
|
|
return AbilityState.MustMatch;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Fall through when gen3 pk transferred to gen4/5
|
|
|
|
var maxGen3Species = g3[0].Species;
|
|
|
|
return VerifyAbilityGen3Transfer(data, abilities, maxGen3Species);
|
|
|
|
}
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private AbilityState VerifyAbilityGen3Transfer(LegalityAnalysis data, IPersonalAbility12 abilities, int maxGen3Species)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
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
|
|
|
var pers = PersonalTable.E[maxGen3Species];
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pers.Ability1 != pers.Ability2) // Excluding Colosseum/XD, a Gen3 pk must match PID if it has 2 unique abilities
|
|
|
|
return pk.Version == (int) GameVersion.CXD ? AbilityState.CanMismatch : AbilityState.MustMatch;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Species != maxGen3Species) // it has evolved in either gen 4 or gen 5; the ability must match PID
|
|
|
|
return AbilityState.MustMatch;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var chain = data.Info.EvoChainsAllGens;
|
|
|
|
bool evolved45 = chain.Gen4.Length > 1 || (pk.Format == 5 && chain.Gen5.Length > 1);
|
|
|
|
if (evolved45)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Ability == pers.Ability1) // Could evolve in Gen4/5 and have a Gen3 only ability
|
|
|
|
return AbilityState.CanMismatch; // Not evolved in Gen4/5, doesn't need to match PIDAbility
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
if (pk.Ability == abilities.Ability2) // It could evolve in Gen4/5 and have Gen4 second ability
|
2022-06-18 18:04:24 +00:00
|
|
|
return AbilityState.MustMatch; // Evolved in Gen4/5, must match PIDAbility
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// If we reach here, it has not evolved in Gen4/5 games or has an invalid ability.
|
|
|
|
// The ability does not need to match the PIDAbility, but only Gen3 ability is allowed.
|
|
|
|
if (pk.Ability != pers.Ability1) // Not evolved in Gen4/5, but doesn't have Gen3 only ability
|
|
|
|
data.AddLine(GetInvalid(LAbilityMismatch3)); // probably bad to do this here
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return AbilityState.CanMismatch;
|
|
|
|
}
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult VerifyAbilityMG(LegalityAnalysis data, MysteryGift g, IPersonalAbility12 abilities)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (g is PCD d)
|
|
|
|
return VerifyAbilityPCD(data, abilities, d);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var pk = data.Entity;
|
|
|
|
if (g is PGT) // Ranger Manaphy
|
|
|
|
return (pk.Format >= 6 ? (pk.AbilityNumber == 1) : (pk.AbilityNumber < 4)) ? VALID : GetInvalid(LAbilityMismatchGift);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var cardType = g.AbilityType;
|
|
|
|
if (cardType == 4) // 1/2/H
|
|
|
|
return VALID;
|
|
|
|
int abilNumber = pk.AbilityNumber;
|
|
|
|
if (cardType == 3) // 1/2
|
|
|
|
return abilNumber == 4 ? GetInvalid(LAbilityMismatchGift) : VALID;
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Only remaining matches are fixed index abilities
|
|
|
|
int cardAbilIndex = 1 << cardType;
|
|
|
|
if (abilNumber == cardAbilIndex)
|
|
|
|
return VALID;
|
2021-03-21 00:49:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Can still match if the ability was changed via ability capsule...
|
|
|
|
// However, it can't change to/from Hidden Abilities.
|
|
|
|
if (abilNumber == 4 || cardType == 2)
|
|
|
|
return GetInvalid(LAbilityHiddenFail);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Ability can be flipped 0/1 if Ability Capsule is available, is not Hidden Ability, and Abilities are different.
|
|
|
|
if (pk.Format >= 6)
|
|
|
|
{
|
|
|
|
if (CanAbilityCapsule(6, abilities))
|
|
|
|
return GetValid(LAbilityCapsuleUsed);
|
|
|
|
|
|
|
|
// Maybe was evolved after using ability capsule.
|
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
|
|
|
var evos = data.Info.EvoChainsAllGens.Get(pk.Context);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (GetWasDual(evos, PKX.Personal, pk))
|
|
|
|
return GetValid(LAbilityCapsuleUsed);
|
2018-07-03 03:34:41 +00:00
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return pk.Format < 6 ? GetInvalid(LAbilityMismatchPID) : INVALID;
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult VerifyAbilityPCD(LegalityAnalysis data, IPersonalAbility12 abilities, PCD pcd)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var format = pk.Format;
|
|
|
|
if (format >= 6)
|
2018-07-03 03:34:41 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!CanAbilityCapsule(format, abilities))
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Gen3-5 transfer with same ability -> 1st ability that matches
|
|
|
|
if (pk.AbilityNumber == 1)
|
|
|
|
return VALID;
|
|
|
|
return CheckMatch(pk, abilities, 4, AbilityState.MustMatch, pcd); // evolved, must match
|
2018-07-03 03:34:41 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.AbilityNumber < 4) // Ability Capsule can change between 1/2
|
|
|
|
return GetValid(LAbilityCapsuleUsed);
|
2018-07-03 03:34:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pcd.Species != pk.Species)
|
|
|
|
return CheckMatch(pk, abilities, 4, AbilityState.MustMatch, pcd); // evolved, must match
|
2021-07-30 20:05:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Edge case (PID ability gift mismatch) -- must match gift ability.
|
|
|
|
return pk.Ability == pcd.Gift.PK.Ability ? VALID : INVALID;
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult VerifyAbility5(LegalityAnalysis data, IEncounterTemplate enc, IPersonalAbility12 abilities)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Eggs and Encounter Slots are not yet checked for Hidden Ability potential.
|
|
|
|
return enc switch
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
EncounterEgg e when pk.AbilityNumber == 4 && AbilityBreedLegality.BanHidden5.Contains(e.Species) => GetInvalid(LAbilityHiddenUnavailable),
|
|
|
|
_ => CheckMatch(data.Entity, abilities, 5, pk.Format == 5 ? AbilityState.MustMatch : AbilityState.CanMismatch, enc),
|
|
|
|
};
|
|
|
|
}
|
2018-07-04 01:43:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private CheckResult VerifyAbility6(LegalityAnalysis data, IEncounterTemplate enc)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
if (pk.AbilityNumber != 4)
|
|
|
|
return VALID;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Eggs and Encounter Slots are not yet checked for Hidden Ability potential.
|
|
|
|
return enc switch
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
EncounterEgg egg when AbilityBreedLegality.BanHidden6.Contains((ushort)(egg.Species | (egg.Form << 11))) => GetInvalid(LAbilityHiddenUnavailable),
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => VALID,
|
|
|
|
};
|
|
|
|
}
|
2019-09-23 23:56:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private CheckResult VerifyAbility7(LegalityAnalysis data, IEncounterTemplate enc)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
if (pk.AbilityNumber != 4)
|
|
|
|
return VALID;
|
2019-09-23 23:56:47 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return enc switch
|
2022-01-18 05:44:21 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
EncounterEgg egg when AbilityBreedLegality.BanHidden7.Contains((ushort)(egg.Species | (egg.Form << 11))) => GetInvalid(LAbilityHiddenUnavailable),
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => VALID,
|
|
|
|
};
|
|
|
|
}
|
2022-01-18 05:44:21 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private CheckResult VerifyAbility8BDSP(LegalityAnalysis data, IEncounterable enc)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
if (pk.AbilityNumber != 4)
|
|
|
|
return VALID;
|
2022-01-18 05:44:21 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return enc switch
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
EncounterEgg egg when AbilityBreedLegality.BanHidden8b.Contains((ushort)(egg.Species | (egg.Form << 11))) => GetInvalid(LAbilityHiddenUnavailable),
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => VALID,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Final checks assuming nothing else has flagged the ability.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pk">Pokémon</param>
|
|
|
|
/// <param name="abilities">Current abilities</param>
|
|
|
|
/// <param name="gen">Generation</param>
|
|
|
|
/// <param name="state">Permissive to allow ability to deviate under special circumstances</param>
|
|
|
|
/// <param name="enc">Encounter template the <see cref="pk"/> was matched to.</param>
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult CheckMatch(PKM pk, IPersonalAbility12 abilities, int gen, AbilityState state, IEncounterTemplate enc)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (gen is (3 or 4) && pk.AbilityNumber == 4)
|
|
|
|
return GetInvalid(LAbilityHiddenUnavailable);
|
2018-07-03 03:34:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// other cases of hidden ability already flagged, all that is left is 1/2 mismatching
|
|
|
|
if (state != AbilityState.MustMatch)
|
|
|
|
return VALID;
|
2020-10-19 18:36:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check that the ability bit is correct.
|
|
|
|
if (pk is G3PKM g3)
|
|
|
|
{
|
|
|
|
var abit = g3.AbilityBit;
|
|
|
|
// We've sanitized our personal data to replace "None" abilities with the first ability.
|
|
|
|
// Granbull, Vibrava, and Flygon have dual abilities being the same.
|
2022-09-02 17:20:19 +00:00
|
|
|
if (abilities.GetIsAbility12Same() && g3.Species is not ((int)Species.Granbull or (int)Species.Vibrava or (int)Species.Flygon)) // Not a dual ability
|
2020-10-19 18:36:57 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Must not have the Ability bit flag set.
|
|
|
|
// Shadow encounters set a random ability index; don't bother checking if it's a re-battle for ability bit flipping.
|
|
|
|
if (abit && enc is not EncounterStaticShadow)
|
|
|
|
return GetInvalid(LAbilityMismatchFlag, CheckIdentifier.PID);
|
2020-10-19 18:36:57 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2020-10-19 18:36:57 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Gen3 mainline origin sets the Ability index based on the PID, but only if it has two abilities.
|
|
|
|
// Version value check isn't factually correct, but there are no C/XD gifts with (Version!=15) that have two abilities.
|
|
|
|
// Pikachu, Celebi, Ho-Oh
|
|
|
|
if (pk.Version != (int)GameVersion.CXD && abit != ((pk.PID & 1) == 1))
|
|
|
|
return GetInvalid(LAbilityMismatchPID, CheckIdentifier.PID);
|
2020-10-19 18:36:57 +00:00
|
|
|
}
|
2021-03-20 18:22:56 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else if (pk.Format >= 6)
|
2021-03-20 18:22:56 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// 6+ already checked at the top of the verifier call stack
|
2018-07-03 03:34:41 +00:00
|
|
|
return VALID;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// 3-5
|
|
|
|
return GetPIDAbilityMatch(pk, abilities);
|
|
|
|
}
|
2022-05-31 04:43:52 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
private CheckResult GetPIDAbilityMatch(PKM pk, IPersonalAbility abilities)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
// Ability Number bits are already verified as clean.
|
2022-09-02 17:20:19 +00:00
|
|
|
var index = pk.AbilityNumber >> 1;
|
|
|
|
var abil = abilities.GetAbilityAtIndex(index);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (abil != pk.Ability)
|
|
|
|
return GetInvalid(LAbilityMismatchPID);
|
2022-05-31 04:43:52 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return VALID;
|
|
|
|
}
|
|
|
|
|
2022-08-04 01:17:46 +00:00
|
|
|
private static bool IsAccessibleAbilityPatch(EvolutionHistory evosAll)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-04 01:17:46 +00:00
|
|
|
return evosAll.HasVisitedSWSH || evosAll.HasVisitedBDSP;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 01:17:46 +00:00
|
|
|
private static bool IsAccessibleAbilityCapsule(EvolutionHistory evosAll)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-04 01:17:46 +00:00
|
|
|
if (evosAll.HasVisitedGen6 || evosAll.HasVisitedGen7)
|
2018-07-03 03:34:41 +00:00
|
|
|
return true;
|
2022-08-04 01:17:46 +00:00
|
|
|
return evosAll.HasVisitedSWSH || evosAll.HasVisitedBDSP;
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Ability Capsule can change between 1/2
|
2022-09-02 17:20:19 +00:00
|
|
|
private static bool IsAbilityCapsuleModified(PKM pk, IPersonalAbility12 abilities, AbilityPermission encounterAbility, EvolutionHistory evos)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-04 01:17:46 +00:00
|
|
|
if (!IsAccessibleAbilityCapsule(evos))
|
2022-06-18 18:04:24 +00:00
|
|
|
return false; // Not available.
|
|
|
|
if (!CanAbilityCapsule(pk.Format, abilities))
|
|
|
|
return false;
|
|
|
|
if (pk.AbilityNumber == 4)
|
|
|
|
return false; // Cannot alter to hidden ability.
|
|
|
|
if (encounterAbility == AbilityPermission.OnlyHidden)
|
|
|
|
return false; // Cannot alter from hidden ability.
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-10 19:30:57 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
public static bool CanAbilityCapsule(int format, IPersonalAbility12 abilities)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (format < 6) // Ability Capsule does not exist
|
|
|
|
return false;
|
2022-09-02 17:20:19 +00:00
|
|
|
return !abilities.GetIsAbility12Same(); // Cannot alter ability index if it is the same as the other ability.
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-11-04 02:48:29 +00:00
|
|
|
|
2022-09-02 17:20:19 +00:00
|
|
|
public static bool CanAbilityPatch(int format, IPersonalAbility12H abilities, ushort species)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (format < 8) // Ability Patch does not exist
|
|
|
|
return false;
|
2020-11-04 02:48:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Can alter ability index if it is different from the other abilities.
|
2022-09-02 17:20:19 +00:00
|
|
|
if (abilities.GetIsAbilityPatchPossible())
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Some species have a distinct hidden ability only on another form, and can change between that form and its current form.
|
|
|
|
return species switch
|
|
|
|
{
|
|
|
|
(int)Species.Giratina => true, // Form-0 is a/a/h
|
|
|
|
(int)Species.Tornadus => true, // Form-0 is a/a/h
|
|
|
|
(int)Species.Thundurus => true, // Form-0 is a/a/h
|
|
|
|
(int)Species.Landorus => true, // Form-0 is a/a/h
|
|
|
|
(int)Species.Enamorus => true, // Form-0 is a/a/h
|
|
|
|
_ => false,
|
|
|
|
};
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
|
|
|
}
|