PKHeX/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic8Nest.cs
Kurt 9166d0eb64
Refactoring: Move Source (Legality) (#3560)
Rewrites a good amount of legality APIs pertaining to:
* Legal moves that can be learned
* Evolution chains & cross-generation paths
* Memory validation with forgotten moves

In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data.

The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space.

The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation.

* `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game.
* `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`).
* Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 16:15:27 -07:00

120 lines
4.3 KiB
C#

using System;
using static PKHeX.Core.Encounters8Nest;
using static PKHeX.Core.AbilityPermission;
namespace PKHeX.Core;
/// <summary>
/// Generation 8 Nest Encounter (Raid)
/// </summary>
/// <inheritdoc cref="EncounterStatic"/>
public abstract record EncounterStatic8Nest<T>(GameVersion Version) : EncounterStatic(Version), IGigantamax, IDynamaxLevel where T : EncounterStatic8Nest<T>
{
public sealed override int Generation => 8;
public override EntityContext Context => EntityContext.Gen8;
public static Func<PKM, T, bool>? VerifyCorrelation { private get; set; }
public static Action<PKM, T, EncounterCriteria>? GenerateData { private get; set; }
public bool CanGigantamax { get; set; }
public byte DynamaxLevel { get; set; }
public override int Location { get => SharedNest; init { } }
public override bool IsMatchExact(PKM pk, EvoCriteria evo)
{
if (pk is PK8 d && d.DynamaxLevel < DynamaxLevel)
return false;
// Required Ability
if (Ability == OnlyHidden && pk.AbilityNumber != 4)
return false; // H
if (Version != GameVersion.SWSH && pk.Version != (int)Version && pk.Met_Location != SharedNest)
return false;
if (VerifyCorrelation != null && !VerifyCorrelation(pk, (T)this))
return false;
if (pk is IRibbonSetMark8 m8 && m8.HasMark())
return false;
if (pk.Species == (int)Core.Species.Shedinja && pk is IRibbonSetAffixed { AffixedRibbon: >= (int)RibbonIndex.MarkLunchtime })
return false;
return base.IsMatchExact(pk, evo);
}
protected sealed override EncounterMatchRating IsMatchDeferred(PKM pk)
{
if (Ability != Any12H)
{
// HA-Only is a strict match. Ability Capsule and Patch can potentially change these.
var num = pk.AbilityNumber;
if (num == 4)
{
if (Ability is not OnlyHidden && !AbilityVerifier.CanAbilityPatch(8, PersonalTable.SWSH.GetFormEntry(Species, Form).Abilities, pk.Species))
return EncounterMatchRating.DeferredErrors;
}
else if (Ability.IsSingleValue(out int index) && 1 << index != num) // Fixed regular ability
{
if (Ability is OnlyFirst or OnlySecond && !AbilityVerifier.CanAbilityCapsule(8, PersonalTable.SWSH.GetFormEntry(Species, Form).Abilities))
return EncounterMatchRating.DeferredErrors;
}
}
return base.IsMatchDeferred(pk);
}
protected override bool IsMatchPartial(PKM pk)
{
if (pk is PK8 and IGigantamax g && g.CanGigantamax != CanGigantamax && !g.CanToggleGigantamax(pk.Species, pk.Form, Species, Form))
return true;
if (Species == (int)Core.Species.Alcremie && pk is IFormArgument { FormArgument: not 0 })
return true;
if (Species == (int)Core.Species.Runerigus && pk is IFormArgument { FormArgument: not 0 })
return true;
switch (Shiny)
{
case Shiny.Never when pk.IsShiny:
case Shiny.Always when !pk.IsShiny:
return true;
}
return base.IsMatchPartial(pk);
}
protected override void ApplyDetails(ITrainerInfo tr, EncounterCriteria criteria, PKM pk)
{
base.ApplyDetails(tr, criteria, pk);
if (GenerateData == null)
pk.SetRandomEC();
}
protected sealed override void SetPINGA(PKM pk, EncounterCriteria criteria)
{
if (GenerateData != null)
{
GenerateData(pk, (T)this, criteria);
return;
}
base.SetPINGA(pk, criteria);
if (Species == (int) Core.Species.Toxtricity)
{
while (true)
{
var result = EvolutionMethod.GetAmpLowKeyResult(pk.Nature);
if (result == pk.Form)
break;
pk.Nature = Util.Rand.Next(25);
}
// Might be originally generated with a Neutral nature, then above logic changes to another.
// Realign the stat nature to Serious mint.
if (pk.Nature != pk.StatNature && ((Nature)pk.StatNature).IsNeutral())
pk.StatNature = (int)Nature.Serious;
}
var pid = pk.PID;
RaidRNG.ForceShinyState(pk, Shiny == Shiny.Always, ref pid);
pk.PID = pid;
}
}