Extract some logic, deduplicate

This commit is contained in:
Kurt 2024-11-10 13:25:30 -06:00
parent 3c574a45f1
commit 1ffbd46e3c

View file

@ -29,33 +29,24 @@ public sealed class BallVerifier : Verifier
private CheckResult VerifyBall(LegalityAnalysis data)
{
var Info = data.Info;
var enc = Info.EncounterMatch;
var info = data.Info;
var enc = info.EncounterOriginal;
var pk = data.Entity;
var ball = IsReplacedBall(enc, data.Entity);
var ball = IsReplacedBall(enc, pk);
if (ball != 0)
return VerifyBallEquals(data, ball);
return VerifyBallEquals(pk, ball);
// Capture / Inherit cases -- can be one of many balls
var pk = data.Entity;
if (pk.Species == (int)Species.Shedinja && enc.Species != (int)Species.Shedinja) // Shedinja. For Gen3, copy the ball from Nincada
{
// Only a Gen3 origin Shedinja can copy the wild ball.
// Evolution chains will indicate if it could have existed as Shedinja in Gen3.
// The special move verifier has a similar check!
if (pk is { HGSS: true, Ball: (int)Sport }) // Can evolve in D/P to retain the HG/SS ball (separate byte) -- not able to be captured in any other ball
return VerifyBallEquals(data, (int)Sport);
if (Info.Generation != 3 || Info.EvoChainsAllGens.Gen3.Length != 2) // not evolved in Gen3 Nincada->Shedinja
return VerifyBallEquals(data, (int)Poke); // Poké Ball Only
}
// Fixed ball cases -- can be only one ball ever
switch (enc)
{
case IFixedBall { FixedBall: not None } s:
return VerifyBallEquals(data, (byte)s.FixedBall);
case EncounterSlot8GO: // Already a strict match
return GetResult(true);
if (enc.Generation != 3 || info.EvoChainsAllGens.Gen3.Length != 2) // not evolved in Gen3 Nincada->Shedinja
return VerifyBallEquals(pk, (int)Poke); // Poké Ball Only
}
// Capturing with Heavy Ball is impossible in Sun/Moon for specific species.
@ -64,140 +55,107 @@ public sealed class BallVerifier : Verifier
return enc switch
{
EncounterSlot8GO => GetResult(true), // Already a strict match
EncounterInvalid => GetResult(true), // ignore ball, pass whatever
IFixedBall { FixedBall: not None } s => VerifyBallEquals(pk, (byte)s.FixedBall),
EncounterEgg => VerifyBallEgg(pk, enc), // Inheritance rules can vary.
EncounterStatic5Entree => VerifyBallEquals((Ball)pk.Ball, BallUseLegality.DreamWorldBalls),
EncounterEgg => VerifyBallEgg(data),
EncounterInvalid => VerifyBallEquals(data, pk.Ball), // ignore ball, pass whatever
_ => VerifyBallEquals((Ball)pk.Ball, BallUseLegality.GetWildBalls(data.Info.Generation, enc.Version)),
_ => VerifyBallEquals((Ball)pk.Ball, BallUseLegality.GetWildBalls(enc.Generation, enc.Version)),
};
}
private CheckResult VerifyBallEgg(LegalityAnalysis data)
private CheckResult VerifyBallEgg(PKM pk, IEncounterTemplate enc)
{
var pk = data.Entity;
var info = data.Info;
if (info.Generation < 6) // No inheriting Balls
return VerifyBallEquals(data, (int)Poke); // Must be Pokéball -- no ball inheritance.
if (enc.Generation < 6) // No inheriting Balls
return VerifyBallEquals(pk, (int)Poke); // Must be Poké Ball -- no ball inheritance.
return pk.Ball switch
{
(int)Master => GetInvalid(LBallEggMaster), // Master Ball
(int)Cherish => GetInvalid(LBallEggCherish), // Cherish Ball
_ => VerifyBallInherited(data, info.EncounterMatch.Context),
_ => VerifyBallInherited(pk, enc),
};
}
private CheckResult VerifyBallInherited(LegalityAnalysis data, EntityContext context) => context switch
private CheckResult VerifyBallInherited(PKM pk, IEncounterTemplate enc) => enc.Context switch
{
EntityContext.Gen6 => VerifyBallEggGen6(data), // Gen6 Inheritance Rules
EntityContext.Gen7 => VerifyBallEggGen7(data), // Gen7 Inheritance Rules
EntityContext.Gen8 => VerifyBallEggGen8(data),
EntityContext.Gen8b => VerifyBallEggGen8BDSP(data),
EntityContext.Gen9 => VerifyBallEggGen9(data),
EntityContext.Gen6 => VerifyBallEggGen6(pk, enc), // Gen6 Inheritance Rules
EntityContext.Gen7 => VerifyBallEggGen7(pk, enc), // Gen7 Inheritance Rules
EntityContext.Gen8 => VerifyBallEggGen8(pk, enc),
EntityContext.Gen8b => VerifyBallEggGen8BDSP(pk, enc),
EntityContext.Gen9 => VerifyBallEggGen9(pk, enc),
_ => GetInvalid(LBallNone),
};
private CheckResult VerifyBallEggGen6(LegalityAnalysis data)
private CheckResult VerifyBallEggGen6(PKM pk, IEncounterTemplate enc)
{
var pk = data.Entity;
var ball = (Ball)pk.Ball;
var instance = BallContext6.Instance;
if (ball > Dream)
return GetInvalid(LBallUnavailable);
var enc = data.EncounterMatch;
var result = instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return result switch
{
BallInheritanceResult.Valid => GetValid(LBallSpeciesPass),
BallInheritanceResult.BadAbility => GetInvalid(LBallAbility),
_ => GetInvalid(LBallSpecies),
};
var result = BallContext6.Instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return GetResult(result);
}
private CheckResult VerifyBallEggGen7(LegalityAnalysis data)
private CheckResult VerifyBallEggGen7(PKM pk, IEncounterTemplate enc)
{
var pk = data.Entity;
var ball = (Ball)pk.Ball;
var instance = BallContext7.Instance;
if (ball > Beast)
return GetInvalid(LBallUnavailable);
var enc = data.EncounterMatch;
var result = instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return result switch
{
BallInheritanceResult.Valid => GetValid(LBallSpeciesPass),
BallInheritanceResult.BadAbility => GetInvalid(LBallAbility),
_ => GetInvalid(LBallSpecies),
};
var result = BallContext7.Instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return GetResult(result);
}
private CheckResult VerifyBallEggGen8BDSP(LegalityAnalysis data)
private CheckResult VerifyBallEggGen8BDSP(PKM pk, IEncounterTemplate enc)
{
var species = data.EncounterMatch.Species;
var pk = data.Entity;
var ball = (Ball)pk.Ball;
if (ball > Beast)
return GetInvalid(LBallUnavailable);
var species = enc.Species;
if (species is (int)Species.Spinda) // Can't transfer via HOME.
return VerifyBallEquals(ball, BallUseLegality.WildPokeBalls4_HGSS);
var instance = BallContextHOME.Instance;
var result = BallContextHOME.Instance.CanBreedWithBall(species, enc.Form, ball, pk);
return GetResult(result);
}
private CheckResult VerifyBallEggGen8(PKM pk, IEncounterTemplate enc)
{
var ball = (Ball)pk.Ball;
if (ball > Beast)
return GetInvalid(LBallUnavailable);
var enc = data.EncounterMatch;
var result = instance.CanBreedWithBall(species, enc.Form, ball, pk);
return result switch
{
BallInheritanceResult.Valid => GetValid(LBallSpeciesPass),
BallInheritanceResult.BadAbility => GetInvalid(LBallAbility),
_ => GetInvalid(LBallSpecies),
};
var result = BallContextHOME.Instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return GetResult(result);
}
private CheckResult VerifyBallEggGen8(LegalityAnalysis data)
private CheckResult VerifyBallEggGen9(PKM pk, IEncounterTemplate enc)
{
var pk = data.Entity;
var ball = (Ball)pk.Ball;
var instance = BallContextHOME.Instance;
if (ball > Beast)
return GetInvalid(LBallUnavailable);
var enc = data.EncounterMatch;
var result = instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return result switch
{
BallInheritanceResult.Valid => GetValid(LBallSpeciesPass),
BallInheritanceResult.BadAbility => GetInvalid(LBallAbility),
_ => GetInvalid(LBallSpecies),
};
}
private CheckResult VerifyBallEggGen9(LegalityAnalysis data)
{
var enc = data.EncounterMatch;
var species = enc.Species;
var pk = data.Entity;
var ball = (Ball)pk.Ball;
// Paldea Starters: Only via GO (Adventures Abound)
var species = enc.Species;
if (species is >= (int)Species.Sprigatito and <= (int)Species.Quaquaval)
return VerifyBallEquals(ball, BallUseLegality.WildPokeballs8g_WithoutRaid);
var instance = BallContextHOME.Instance;
if (ball > Beast)
return GetInvalid(LBallUnavailable);
var result = instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return result switch
{
BallInheritanceResult.Valid => GetValid(LBallSpeciesPass),
BallInheritanceResult.BadAbility => GetInvalid(LBallAbility),
_ => GetInvalid(LBallSpecies),
};
var result = BallContextHOME.Instance.CanBreedWithBall(enc.Species, enc.Form, ball, pk);
return GetResult(result);
}
private CheckResult VerifyBallEquals(LegalityAnalysis data, byte ball) => GetResult(ball == data.Entity.Ball);
private CheckResult VerifyBallEquals(PKM pk, byte ball) => GetResult(ball == pk.Ball);
private CheckResult VerifyBallEquals(Ball ball, ulong permit) => GetResult(BallUseLegality.IsBallPermitted(permit, (byte)ball));
private CheckResult GetResult(bool valid) => valid ? GetValid(LBallEnc) : GetInvalid(LBallEncMismatch);
private CheckResult GetResult(BallInheritanceResult result) => result switch
{
BallInheritanceResult.Valid => GetValid(LBallSpeciesPass),
BallInheritanceResult.BadAbility => GetInvalid(LBallAbility),
_ => GetInvalid(LBallSpecies),
};
}