2022-08-27 06:43:36 +00:00
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Verifies the <see cref="PKM.Gender"/>.
|
|
|
|
/// </summary>
|
|
|
|
public sealed class GenderVerifier : Verifier
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.Gender;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var pi = pk.PersonalInfo;
|
|
|
|
if (pi.Genderless != (pk.Gender == 2))
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// DP/HGSS shedinja glitch -- only generation 4 spawns
|
|
|
|
bool ignore = pk.Format == 4 && pk.Species == (int)Species.Shedinja && pk.Met_Level != pk.CurrentLevel;
|
|
|
|
if (!ignore)
|
2018-09-01 21:11:12 +00:00
|
|
|
data.AddLine(GetInvalid(LGenderInvalidNone));
|
2022-06-18 18:04:24 +00:00
|
|
|
return;
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check for PID relationship to Gender & Nature if applicable
|
|
|
|
int gen = data.Info.Generation;
|
|
|
|
if (gen is 3 or 4 or 5)
|
2018-06-24 05:00:01 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// Gender-PID & Nature-PID relationship check
|
|
|
|
var result = IsValidGenderPID(data) ? GetValid(LPIDGenderMatch) : GetInvalid(LPIDGenderMismatch);
|
2018-06-24 05:00:01 +00:00
|
|
|
data.AddLine(result);
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (gen != 5)
|
|
|
|
VerifyNaturePID(data);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-24 05:00:01 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Check fixed gender cases
|
|
|
|
if ((pi.OnlyFemale && pk.Gender != 1) || (pi.OnlyMale && pk.Gender != 0))
|
|
|
|
data.AddLine(GetInvalid(LGenderInvalidNone));
|
|
|
|
}
|
2018-06-30 22:01:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void VerifyNaturePID(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
var result = pk.EncryptionConstant % 25 == pk.Nature
|
|
|
|
? GetValid(LPIDNatureMatch, CheckIdentifier.Nature)
|
|
|
|
: GetInvalid(LPIDNatureMismatch, CheckIdentifier.Nature);
|
|
|
|
data.AddLine(result);
|
|
|
|
}
|
2018-06-30 22:01:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static bool IsValidGenderPID(LegalityAnalysis data)
|
|
|
|
{
|
|
|
|
var pk = data.Entity;
|
|
|
|
bool genderValid = pk.IsGenderValid();
|
|
|
|
if (!genderValid)
|
|
|
|
return IsValidGenderMismatch(pk);
|
2018-06-30 22:01:16 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// check for mixed->fixed gender incompatibility by checking the gender of the original species
|
2022-08-27 06:43:36 +00:00
|
|
|
var original = data.EncounterMatch.Species;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Legal.FixedGenderFromBiGender.Contains(original))
|
|
|
|
return IsValidFixedGenderFromBiGender(pk, original);
|
2021-01-02 01:08:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-01-02 01:08:49 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static bool IsValidFixedGenderFromBiGender(PKM pk, ushort original)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
var current = pk.Gender;
|
|
|
|
if (current == 2) // shedinja, genderless
|
|
|
|
return true;
|
|
|
|
var gender = EntityGender.GetFromPID(original, pk.EncryptionConstant);
|
|
|
|
return gender == current;
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private static bool IsValidGenderMismatch(PKM pk) => pk.Species switch
|
|
|
|
{
|
|
|
|
// Shedinja evolution gender glitch, should match original Gender
|
|
|
|
(int) Species.Shedinja when pk.Format == 4 => pk.Gender == EntityGender.GetFromPIDAndRatio(pk.EncryptionConstant, 0x7F), // 50M-50F
|
|
|
|
|
|
|
|
// Evolved from Azurill after transferring to keep gender
|
|
|
|
(int) Species.Marill or (int) Species.Azumarill when pk.Format >= 6 => pk.Gender == 1 && (pk.EncryptionConstant & 0xFF) > 0x3F,
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
};
|
2018-06-24 05:00:01 +00:00
|
|
|
}
|