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