PKHeX/PKHeX.Core/Legality/Verifiers/Ribbons/RibbonVerifierUnique4.cs
Kurt 768047cd80
Legality: Rewrite Ribbon Verifier (#3570)
* Rewrite ribbon verification
* Explicitly verifies all ribbons instead of chained iterators.
* Verifies using only the stack, using `struct` and `Span<T>`. No allocation on heap, or `IEnumerable` iterators.
* Verifies all egg ribbons using a separate method, explicitly implemented. No reflection overhead.
* Separates each ribbon interface to separate `static` classes. Easier to identify code needing change on new game update.
* Extracted logic for specific ribbons. Can easily revise complicated ribbon's acquisition rules.
* Simplifies GiveAll/RemoveAll legal ribbon mutations. No reflection overhead, and no allocation.
* Can be expanded in the future if we need to track conditions for ribbon acquisition (was Sinnoh Champ received in BDSP or Gen4?)

End result is a more performant implementation and easier to maintain & reuse logic.
2022-08-15 21:04:30 -07:00

135 lines
6.3 KiB
C#

namespace PKHeX.Core;
/// <summary>
/// Parsing logic for <see cref="IRibbonSetUnique4"/>.
/// </summary>
public static class RibbonVerifierUnique4
{
public static void Parse(this IRibbonSetUnique4 r, RibbonVerifierArguments args, ref RibbonResultList list)
{
var evos = args.History;
if (!RibbonRules.IsAllowedBattleFrontier4(evos))
FlagAnyAbility(r, ref list);
if (RibbonRules.IsAllowedContest3(evos))
AddMissingContest3(r, ref list);
else
FlagAnyContest3(r, ref list);
if (RibbonRules.IsAllowedContest4(evos))
AddMissingContest4(r, ref list);
else
FlagAnyContest4(r, ref list);
}
public static void ParseEgg(this IRibbonSetUnique4 r, ref RibbonResultList list)
{
FlagAnyAbility(r, ref list);
FlagAnyContest3(r, ref list);
FlagAnyContest4(r, ref list);
}
private static void AddMissingContest3(IRibbonSetUnique4 r, ref RibbonResultList list)
{
static void CheckSet(bool Master, bool Hyper, bool Super, bool Initial, ref RibbonResultList list, RibbonIndex3 index)
{
bool top = Master;
if (Hyper)
top = true;
else if (top)
list.Add((RibbonIndex3)((byte)index + 2));
if (Super)
top = true;
else if (top)
list.Add((RibbonIndex3)((byte)index + 1));
if (top && !Initial)
list.Add(index);
}
CheckSet(r.RibbonG3CoolMaster, r.RibbonG3CoolHyper, r.RibbonG3CoolSuper, r.RibbonG3Cool, ref list, RibbonIndex3.Cool);
CheckSet(r.RibbonG3BeautyMaster, r.RibbonG3BeautyHyper, r.RibbonG3BeautySuper, r.RibbonG3Beauty, ref list, RibbonIndex3.Beauty);
CheckSet(r.RibbonG3CuteMaster , r.RibbonG3CuteHyper, r.RibbonG3CuteSuper, r.RibbonG3Cute, ref list, RibbonIndex3.Cute);
CheckSet(r.RibbonG3SmartMaster, r.RibbonG3SmartHyper, r.RibbonG3SmartSuper, r.RibbonG3Smart, ref list, RibbonIndex3.Smart);
CheckSet(r.RibbonG3ToughMaster, r.RibbonG3ToughHyper, r.RibbonG3ToughSuper, r.RibbonG3Tough, ref list, RibbonIndex3.Tough);
}
private static void AddMissingContest4(IRibbonSetUnique4 r, ref RibbonResultList list)
{
static void CheckSet(bool Master, bool Hyper, bool Super, bool Initial, ref RibbonResultList list, RibbonIndex4 index)
{
bool top = Master;
if (Hyper)
top = true;
else if (top)
list.Add((RibbonIndex4)((byte)index + 2));
if (Super)
top = true;
else if (top)
list.Add((RibbonIndex4)((byte)index + 1));
if (top && !Initial)
list.Add(index);
}
CheckSet(r.RibbonG3CoolMaster, r.RibbonG3CoolHyper, r.RibbonG3CoolSuper, r.RibbonG3Cool, ref list, RibbonIndex4.Cool);
CheckSet(r.RibbonG3BeautyMaster, r.RibbonG3BeautyHyper, r.RibbonG3BeautySuper, r.RibbonG3Beauty, ref list, RibbonIndex4.Beauty);
CheckSet(r.RibbonG3CuteMaster , r.RibbonG3CuteHyper, r.RibbonG3CuteSuper, r.RibbonG3Cute, ref list, RibbonIndex4.Cute);
CheckSet(r.RibbonG3SmartMaster, r.RibbonG3SmartHyper, r.RibbonG3SmartSuper, r.RibbonG3Smart, ref list, RibbonIndex4.Smart);
CheckSet(r.RibbonG3ToughMaster, r.RibbonG3ToughHyper, r.RibbonG3ToughSuper, r.RibbonG3Tough, ref list, RibbonIndex4.Tough);
}
private static void FlagAnyAbility(IRibbonSetUnique4 r, ref RibbonResultList list)
{
if (r.RibbonAbility)
list.Add(RibbonIndex4.Ability);
if (r.RibbonAbilityGreat)
list.Add(RibbonIndex4.AbilityGreat);
if (r.RibbonAbilityDouble)
list.Add(RibbonIndex4.AbilityDouble);
if (r.RibbonAbilityMulti)
list.Add(RibbonIndex4.AbilityMulti);
if (r.RibbonAbilityPair)
list.Add(RibbonIndex4.AbilityPair);
}
private static void FlagAnyContest3(IRibbonSetUnique4 r, ref RibbonResultList list)
{
static void CheckSet(bool Master, bool Hyper, bool Super, bool Initial, ref RibbonResultList list, RibbonIndex3 index)
{
if (Master)
list.Add((RibbonIndex3)((byte)index + 3));
if (Hyper)
list.Add((RibbonIndex3)((byte)index + 2));
if (Super)
list.Add((RibbonIndex3)((byte)index + 1));
if (Initial)
list.Add(index);
}
CheckSet(r.RibbonG3CoolMaster, r.RibbonG3CoolHyper, r.RibbonG3CoolSuper, r.RibbonG3Cool, ref list, RibbonIndex3.Cool);
CheckSet(r.RibbonG3BeautyMaster, r.RibbonG3BeautyHyper, r.RibbonG3BeautySuper, r.RibbonG3Beauty, ref list, RibbonIndex3.Beauty);
CheckSet(r.RibbonG3CuteMaster , r.RibbonG3CuteHyper, r.RibbonG3CuteSuper, r.RibbonG3Cute, ref list, RibbonIndex3.Cute);
CheckSet(r.RibbonG3SmartMaster, r.RibbonG3SmartHyper, r.RibbonG3SmartSuper, r.RibbonG3Smart, ref list, RibbonIndex3.Smart);
CheckSet(r.RibbonG3ToughMaster, r.RibbonG3ToughHyper, r.RibbonG3ToughSuper, r.RibbonG3Tough, ref list, RibbonIndex3.Tough);
}
private static void FlagAnyContest4(IRibbonSetUnique4 r, ref RibbonResultList list)
{
static void CheckSet(bool Master, bool Hyper, bool Super, bool Initial, ref RibbonResultList list, RibbonIndex4 index)
{
if (Master)
list.Add((RibbonIndex4)((byte)index + 3));
if (Hyper)
list.Add((RibbonIndex4)((byte)index + 2));
if (Super)
list.Add((RibbonIndex4)((byte)index + 1));
if (Initial)
list.Add(index);
}
CheckSet(r.RibbonG3CoolMaster, r.RibbonG3CoolHyper, r.RibbonG3CoolSuper, r.RibbonG3Cool, ref list, RibbonIndex4.Cool);
CheckSet(r.RibbonG3BeautyMaster, r.RibbonG3BeautyHyper, r.RibbonG3BeautySuper, r.RibbonG3Beauty, ref list, RibbonIndex4.Beauty);
CheckSet(r.RibbonG3CuteMaster , r.RibbonG3CuteHyper, r.RibbonG3CuteSuper, r.RibbonG3Cute, ref list, RibbonIndex4.Cute);
CheckSet(r.RibbonG3SmartMaster, r.RibbonG3SmartHyper, r.RibbonG3SmartSuper, r.RibbonG3Smart, ref list, RibbonIndex4.Smart);
CheckSet(r.RibbonG3ToughMaster, r.RibbonG3ToughHyper, r.RibbonG3ToughSuper, r.RibbonG3Tough, ref list, RibbonIndex4.Tough);
}
}