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 23:15:27 +00:00
|
|
|
using System;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generation 8 Static Encounter
|
|
|
|
/// </summary>
|
|
|
|
/// <inheritdoc cref="EncounterStatic"/>
|
2022-05-06 22:43:23 +00:00
|
|
|
public sealed record EncounterStatic8a(GameVersion Version) : EncounterStatic(Version), IAlpha, IMasteryInitialMoveShop8
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
public override int Generation => 8;
|
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 23:15:27 +00:00
|
|
|
public override EntityContext Context => EntityContext.Gen8a;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
|
|
public byte HeightScalar { get; }
|
|
|
|
public byte WeightScalar { get; }
|
|
|
|
public bool IsAlpha { get; set; }
|
2022-04-23 04:11:11 +00:00
|
|
|
public EncounterStatic8aCorrelation Method { get; init; }
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
|
|
public bool HasFixedHeight => HeightScalar != NoScalar;
|
|
|
|
public bool HasFixedWeight => WeightScalar != NoScalar;
|
|
|
|
private const byte NoScalar = 0;
|
|
|
|
|
|
|
|
public EncounterStatic8a(ushort species, ushort form, byte level, byte h = NoScalar, byte w = NoScalar) : this(GameVersion.PLA)
|
|
|
|
{
|
|
|
|
Species = species;
|
|
|
|
Form = form;
|
|
|
|
Level = level;
|
|
|
|
HeightScalar = h;
|
|
|
|
WeightScalar = w;
|
|
|
|
Shiny = Shiny.Never;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void ApplyDetails(ITrainerInfo tr, EncounterCriteria criteria, PKM pk)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
base.ApplyDetails(tr, criteria, pk);
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2022-05-06 22:43:23 +00:00
|
|
|
var pa = (PA8)pk;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2022-05-06 22:43:23 +00:00
|
|
|
if (IsAlpha)
|
|
|
|
pa.IsAlpha = true;
|
|
|
|
|
|
|
|
if (HasFixedHeight)
|
|
|
|
pa.HeightScalar = HeightScalar;
|
|
|
|
if (HasFixedWeight)
|
|
|
|
pa.WeightScalar = WeightScalar;
|
|
|
|
pa.HeightScalarCopy = pa.HeightScalar;
|
|
|
|
|
|
|
|
pa.ResetHeight();
|
|
|
|
pa.ResetWeight();
|
2022-02-05 23:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void SetPINGA(PKM pk, EncounterCriteria criteria)
|
|
|
|
{
|
2022-02-21 01:59:48 +00:00
|
|
|
var para = GetParams();
|
2022-04-23 04:11:11 +00:00
|
|
|
var (_, slotSeed) = Overworld8aRNG.ApplyDetails(pk, criteria, para, IsAlpha);
|
|
|
|
// We don't override LevelMin, so just handle the two species cases.
|
|
|
|
if (Species == (int)Core.Species.Zorua)
|
|
|
|
pk.CurrentLevel = pk.Met_Level = Overworld8aRNG.GetRandomLevel(slotSeed, 26, 28);
|
|
|
|
else if (Species == (int)Core.Species.Phione)
|
|
|
|
pk.CurrentLevel = pk.Met_Level = Overworld8aRNG.GetRandomLevel(slotSeed, 33, 36);
|
|
|
|
|
|
|
|
if (Method == EncounterStatic8aCorrelation.Fixed)
|
|
|
|
pk.EncryptionConstant = Util.Rand32();
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ApplyDetailsBall(PKM pk) => pk.Ball = Gift ? Ball : (int)Core.Ball.LAPoke;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override bool IsMatchExact(PKM pk, EvoCriteria evo)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!base.IsMatchExact(pk, evo))
|
2022-02-05 01:35:15 +00:00
|
|
|
return false;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is IScaledSize s)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
if (HasFixedHeight && s.HeightScalar != HeightScalar)
|
|
|
|
return false;
|
|
|
|
if (HasFixedWeight && s.WeightScalar != WeightScalar)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is IAlpha a && a.IsAlpha != IsAlpha)
|
2022-02-05 01:35:15 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchLocation(PKM pk)
|
2022-05-31 04:43:52 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is PK8)
|
|
|
|
return pk.Met_Location == Locations.HOME_SWLA;
|
|
|
|
if (pk is PB8 { Version: (int)GameVersion.PLA, Met_Location: Locations.HOME_SWLA })
|
2022-05-31 04:43:52 +00:00
|
|
|
return true;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return base.IsMatchLocation(pk);
|
2022-05-31 04:43:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override EncounterMatchRating GetMatchRating(PKM pk)
|
2022-02-07 00:14:46 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var result = GetMatchRatingInternal(pk);
|
|
|
|
var orig = base.GetMatchRating(pk);
|
2022-02-07 00:14:46 +00:00
|
|
|
return result > orig ? result : orig;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private EncounterMatchRating GetMatchRatingInternal(PKM pk)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Shiny != Shiny.Random && !Shiny.IsValid(pk))
|
2022-02-05 01:35:15 +00:00
|
|
|
return EncounterMatchRating.DeferredErrors;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Gift && pk.Ball != Ball)
|
2022-02-05 01:35:15 +00:00
|
|
|
return EncounterMatchRating.DeferredErrors;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var orig = base.GetMatchRating(pk);
|
2022-02-05 01:35:15 +00:00
|
|
|
if (orig is not EncounterMatchRating.Match)
|
|
|
|
return orig;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!IsForcedMasteryCorrect(pk))
|
2022-05-06 22:43:23 +00:00
|
|
|
return EncounterMatchRating.DeferredErrors;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (IsAlpha && pk is PA8 { AlphaMove: 0 })
|
2022-02-05 01:35:15 +00:00
|
|
|
return EncounterMatchRating.Deferred;
|
|
|
|
|
|
|
|
return EncounterMatchRating.Match;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool IsForcedMasteryCorrect(PKM pk)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-05-06 22:43:23 +00:00
|
|
|
ushort alpha = 0;
|
2022-05-06 03:21:21 +00:00
|
|
|
if (IsAlpha && Moves.Count != 0)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is PA8 pa && (alpha = pa.AlphaMove) != Moves[0])
|
2022-02-05 01:35:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is not IMoveShop8Mastery p)
|
2022-02-05 01:35:15 +00:00
|
|
|
return true;
|
|
|
|
|
2022-05-07 20:48:47 +00:00
|
|
|
const bool allowAlphaPurchaseBug = true; // Everything else Alpha is pre-1.1
|
2022-06-18 18:04:24 +00:00
|
|
|
var level = pk.Met_Level;
|
2022-05-06 22:43:23 +00:00
|
|
|
var index = PersonalTable.LA.GetFormIndex(Species, Form);
|
2022-05-06 23:11:52 +00:00
|
|
|
var learn = Legal.LevelUpLA[index];
|
2022-05-07 20:48:47 +00:00
|
|
|
if (!p.IsValidPurchasedEncounter(learn, level, alpha, allowAlphaPurchaseBug))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Span<int> moves = stackalloc int[4];
|
2022-05-06 22:43:23 +00:00
|
|
|
var mastery = Legal.MasteryLA[index];
|
|
|
|
if (Moves.Count != 0)
|
2022-05-06 23:11:52 +00:00
|
|
|
moves = (int[])Moves;
|
2022-05-06 22:43:23 +00:00
|
|
|
else
|
2022-05-06 23:11:52 +00:00
|
|
|
learn.SetEncounterMoves(level, moves);
|
2022-05-06 22:43:23 +00:00
|
|
|
|
2022-05-07 20:48:47 +00:00
|
|
|
return p.IsValidMasteredEncounter(moves, learn, mastery, level, alpha, allowAlphaPurchaseBug);
|
2022-05-06 22:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void SetEncounterMoves(PKM pk, GameVersion version, int level)
|
|
|
|
{
|
|
|
|
var pa8 = (PA8)pk;
|
|
|
|
Span<int> moves = stackalloc int[4];
|
2022-05-08 17:28:22 +00:00
|
|
|
var (learn, mastery) = GetLevelUpInfo();
|
|
|
|
LoadInitialMoveset(pa8, moves, learn, level);
|
|
|
|
pk.SetMoves(moves);
|
|
|
|
pk.SetMaximumPPCurrent(moves);
|
|
|
|
pa8.SetEncounterMasteryFlags(moves, mastery, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
public (Learnset Learn, Learnset Mastery) GetLevelUpInfo()
|
|
|
|
{
|
2022-05-06 22:43:23 +00:00
|
|
|
var index = PersonalTable.LA.GetFormIndex(Species, Form);
|
|
|
|
var learn = Legal.LevelUpLA[index];
|
2022-05-08 17:28:22 +00:00
|
|
|
var mastery = Legal.MasteryLA[index];
|
|
|
|
return (learn, mastery);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadInitialMoveset(PA8 pa8, Span<int> moves, Learnset learn, int level)
|
|
|
|
{
|
2022-05-09 02:08:44 +00:00
|
|
|
if (Moves.Count == 0)
|
2022-05-06 22:43:23 +00:00
|
|
|
learn.SetEncounterMoves(level, moves);
|
2022-05-09 02:08:44 +00:00
|
|
|
else
|
|
|
|
((int[])Moves).CopyTo(moves);
|
|
|
|
if (IsAlpha)
|
|
|
|
pa8.AlphaMove = (ushort)moves[0];
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
2022-02-21 01:59:48 +00:00
|
|
|
|
|
|
|
private OverworldParam8a GetParams()
|
|
|
|
{
|
2022-05-06 03:21:21 +00:00
|
|
|
var gender = GetGenderRatio();
|
2022-02-21 01:59:48 +00:00
|
|
|
return new OverworldParam8a
|
|
|
|
{
|
|
|
|
IsAlpha = IsAlpha,
|
2022-03-01 06:46:59 +00:00
|
|
|
FlawlessIVs = FlawlessIVCount,
|
2022-02-21 01:59:48 +00:00
|
|
|
Shiny = Shiny,
|
|
|
|
RollCount = 1, // Everything is shiny locked anyways
|
|
|
|
GenderRatio = gender,
|
|
|
|
};
|
|
|
|
}
|
2022-05-06 03:21:21 +00:00
|
|
|
|
|
|
|
private byte GetGenderRatio() => Gender switch
|
|
|
|
{
|
|
|
|
0 => PersonalInfo.RatioMagicMale,
|
|
|
|
1 => PersonalInfo.RatioMagicFemale,
|
|
|
|
_ => GetGenderRatioPersonal(),
|
|
|
|
};
|
|
|
|
|
|
|
|
private byte GetGenderRatioPersonal()
|
|
|
|
{
|
|
|
|
var pt = PersonalTable.LA;
|
|
|
|
var entry = pt.GetFormEntry(Species, Form);
|
|
|
|
return (byte)entry.Gender;
|
|
|
|
}
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
2022-04-23 04:11:11 +00:00
|
|
|
|
|
|
|
public enum EncounterStatic8aCorrelation : byte
|
|
|
|
{
|
|
|
|
WildGroup,
|
|
|
|
Fixed,
|
|
|
|
}
|