using System; using System.Collections.Generic; using static PKHeX.Core.LegalityCheckStrings; namespace PKHeX.Core { /// /// Verifies the values. /// public sealed class AbilityVerifier : Verifier { protected override CheckIdentifier Identifier => CheckIdentifier.Ability; public override void Verify(LegalityAnalysis data) { var result = VerifyAbility(data); data.AddLine(result); } private CheckResult VALID => GetValid(V115); private CheckResult INVALID => GetInvalid(V223); private enum AbilityState : byte { CanMismatch, MustMatch, } private CheckResult VerifyAbility(LegalityAnalysis data) { var pkm = data.pkm; var abilities = pkm.PersonalInfo.Abilities; // Check ability is possible (within bounds) int ability = pkm.Ability; int abilval = Array.IndexOf(abilities, ability); if (abilval < 0) return GetInvalid(V107); if (data.EncounterMatch is MysteryGift g && g.Format >= 4) return VerifyAbilityMG(data, abilities, g.AbilityType); if (pkm.Format < 6) return VerifyAbility345(data, abilities, abilval); // Check AbilityNumber points to ability int an = pkm.AbilityNumber >> 1; if (an >= abilities.Length || abilities[an] != ability) return GetInvalid(V114); return VerifyAbility(data, abilities, abilval); } private CheckResult VerifyAbility(LegalityAnalysis data, IReadOnlyList abilities, int abilnum) { var EncounterMatch = data.EncounterMatch; var eabil = GetEncounterFixedAbilityNumber(EncounterMatch); if (eabil >= 0) return VerifyFixedAbility(data, abilities, AbilityState.CanMismatch, eabil, abilnum); var gen = data.Info.Generation; switch (gen) { case 5: return VerifyAbility5(data, abilities); case 6: return VerifyAbility6(data); case 7: return VerifyAbility7(data); } return CheckMatch(data.pkm, abilities, gen, AbilityState.CanMismatch); } private CheckResult VerifyAbility345(LegalityAnalysis data, IReadOnlyList abilities, int abilnum) { var pkm = data.pkm; var state = AbilityState.MustMatch; if (3 <= pkm.Format && pkm.Format <= 5 && abilities[0] != abilities[1]) // 3-4/5 and have 2 distinct abilities now state = VerifyAbilityPreCapsule(data, abilities); var EncounterMatch = data.EncounterMatch; int eabil = GetEncounterFixedAbilityNumber(EncounterMatch); if (eabil >= 0) return VerifyFixedAbility(data, abilities, state, eabil, abilnum); int gen = data.Info.Generation; if (gen == 5) return VerifyAbility5(data, abilities); return CheckMatch(pkm, abilities, gen, state); } private CheckResult VerifyFixedAbility(LegalityAnalysis data, IReadOnlyList abilities, AbilityState state, int EncounterAbility, int abilval) { var pkm = data.pkm; if ((pkm.AbilityNumber == 4) != (EncounterAbility == 4)) return GetInvalid(V108); if (data.EncounterMatch.Species != pkm.Species && state != AbilityState.CanMismatch) // evolved return CheckMatch(pkm, abilities, data.Info.Generation, AbilityState.MustMatch); if (EncounterAbility == 1 << abilval) return GetValid(V115); if (pkm.AbilityNumber == EncounterAbility) return VALID; if (state == AbilityState.CanMismatch || EncounterAbility == 0) return CheckMatch(pkm, abilities, data.Info.Generation, AbilityState.MustMatch); if (IsAbilityCapsuleModified(pkm, abilities, EncounterAbility)) return GetValid(V109); return INVALID; } private AbilityState VerifyAbilityPreCapsule(LegalityAnalysis data, IReadOnlyList abilities) { var pkm = data.pkm; // CXD pokemon can have any ability without matching PID if (pkm.Version == (int)GameVersion.CXD && pkm.Format == 3) return AbilityState.CanMismatch; // Gen3 native or Gen4/5 origin if (pkm.Format == 3 || !pkm.InhabitedGeneration(3)) return AbilityState.MustMatch; // Evovled in Gen4/5 if (pkm.Species > Legal.MaxSpeciesID_3) return AbilityState.MustMatch; // If the species could not exist in Gen3, must match. if (data.Info.EvoChainsAllGens[3].Count == 0) return AbilityState.MustMatch; // Fall through when gen3 pkm transferred to gen4/5 return VerifyAbilityGen3Transfer(data, abilities, data.Info.EvoChainsAllGens[3][0].Species); } private AbilityState VerifyAbilityGen3Transfer(LegalityAnalysis data, IReadOnlyList abilities, int maxGen3Species) { var pkm = data.pkm; 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) { 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 } // 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 data.AddLine(GetInvalid(V373)); // probably bad to do this here return AbilityState.CanMismatch; } private CheckResult VerifyAbilityMG(LegalityAnalysis data, IReadOnlyList abilities, int cardtype) { if (data.EncounterMatch is PCD d) return VerifyAbilityPCD(data, abilities, d); var pkm = data.pkm; if (data.EncounterMatch is PGT) // Ranger Manaphy return (pkm.Format >= 6 ? (pkm.AbilityNumber == 1) : (pkm.AbilityNumber < 4)) ? VALID : GetInvalid(V110); int abilNumber = pkm.AbilityNumber; if (cardtype == 4) // 1/2/H return VALID; if (cardtype == 3) // 1/2 return abilNumber == 4 ? GetInvalid(V110) : VALID; // Only remaining matches are fixed index abilities int cardAbilIndex = 1 << cardtype; if (abilNumber == cardAbilIndex) return VALID; // 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(V108); // Ability can be flipped 0/1 if Ability Capsule is available, is not Hidden Ability, and Abilities are different. if (pkm.Format >= 6 && abilities[0] != abilities[1]) return GetValid(V109); return GetInvalid(pkm.Format < 6 ? V113 : V114); } private CheckResult VerifyAbilityPCD(LegalityAnalysis data, IReadOnlyList abilities, PCD pcd) { var pkm = data.pkm; if (pkm.Format >= 6) { if (abilities[0] == abilities[1]) { // Gen3-5 transfer with same ability -> 1st ability that matches if (pkm.AbilityNumber == 1) return GetValid(V115); return CheckMatch(pkm, abilities, 4, AbilityState.MustMatch); // evolved, must match } if (pkm.AbilityNumber < 4) // Ability Capsule can change between 1/2 return GetValid(V109); } if (pcd.Species != pkm.Species) return CheckMatch(pkm, abilities, 4, AbilityState.MustMatch); // evolved, must match // Edge case (PID ability gift mismatch) -- must match gift ability. return pkm.Ability == pcd.Gift.PK.Ability ? VALID : INVALID; } private CheckResult VerifyAbility5(LegalityAnalysis data, IReadOnlyList abilities) { var pkm = data.pkm; switch (data.EncounterMatch) { case EncounterSlot w: // Hidden Abilities for Wild Encounters are only available at a Hidden Grotto bool grotto = w.Type == SlotType.HiddenGrotto; if (pkm.AbilityNumber == 4 ^ grotto) return GetInvalid(grotto ? V217 : V108); break; case EncounterEgg e when pkm.AbilityNumber == 4: // Hidden Abilities for some are unbreedable or unreleased if (Legal.Ban_BreedHidden5.Contains(e.Species)) return GetInvalid(V112); break; } var state = pkm.Format == 5 ? AbilityState.MustMatch : AbilityState.CanMismatch; return CheckMatch(data.pkm, abilities, 5, state); } private CheckResult VerifyAbility6(LegalityAnalysis data) { var pkm = data.pkm; if (pkm.AbilityNumber != 4) return VALID; // hidden abilities var EncounterMatch = data.EncounterMatch; if (EncounterMatch is EncounterSlot slot) { bool valid = slot.Permissions.DexNav || slot.Type == SlotType.FriendSafari || slot.Type == SlotType.Horde; if (!valid) return GetInvalid(V300); } if (Legal.Ban_NoHidden6.Contains(pkm.SpecForm)) return GetInvalid(V112); return VALID; } private CheckResult VerifyAbility7(LegalityAnalysis data) { var pkm = data.pkm; var EncounterMatch = data.EncounterMatch; if (EncounterMatch is EncounterSlot slot && pkm.AbilityNumber == 4) { bool valid = slot.Type == SlotType.SOS; if (!valid) return GetInvalid(V111); } if (Legal.Ban_NoHidden7.Contains(pkm.SpecForm) && pkm.AbilityNumber == 4) return GetInvalid(V112); return VALID; } /// /// Final checks assuming nothing else has flagged the ability. /// /// Pokémon /// Current abilities /// Generation /// Permissive to allow ability to deviate under special circumstances private CheckResult CheckMatch(PKM pkm, IReadOnlyList abilities, int gen, AbilityState state) { if (3 <= gen && gen <= 4 && pkm.AbilityNumber == 4) return GetInvalid(V112); // other cases of hidden ability already flagged, all that is left is 1/2 mismatching if (state == AbilityState.MustMatch && abilities[pkm.AbilityNumber >> 1] != pkm.Ability) return GetInvalid(pkm.Format < 6 ? V113 : V114); return VALID; } // Ability Capsule can change between 1/2 private static bool IsAbilityCapsuleModified(PKM pkm, IReadOnlyList abilities, int EncounterAbility) { if (pkm.Format < 6) return false; // Ability Capsule does not exist if (abilities[0] == abilities[1]) return false; // Cannot alter ability index if it is the same as the other ability. if (pkm.AbilityNumber == 4) return false; // Cannot alter to hidden ability. if (EncounterAbility == 4) return false; // Cannot alter from hidden ability. return true; } private static int GetEncounterFixedAbilityNumber(IEncounterable enc) { switch (enc) { case EncounterStatic s: return s.Ability; case EncounterTrade t: return t.Ability; case EncounterLink l: return l.Ability; default: return -1; } } } }