2022-02-05 01:35:15 +00:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Encounter Slot found in <see cref="GameVersion.SWSH"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <inheritdoc cref="EncounterSlot"/>
|
2022-05-06 22:43:23 +00:00
|
|
|
public sealed record EncounterSlot8a : EncounterSlot, 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 18:23:00 +00:00
|
|
|
public SlotType Type => Area.Type;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
|
|
public bool IsAlpha { get => AlphaType is not 0; set => throw new InvalidOperationException("Do not mutate this field."); }
|
|
|
|
public byte FlawlessIVCount { get; }
|
|
|
|
public Gender Gender { get; }
|
|
|
|
public byte AlphaType { get; } // 0=Never, 1=Random, 2=Guaranteed
|
|
|
|
|
2022-03-07 07:25:47 +00:00
|
|
|
public EncounterSlot8a(EncounterArea8a area, ushort species, byte form, byte min, byte max, byte alphaType, byte flawlessIVs, Gender gender) : base(area, species, form, min, max)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
AlphaType = alphaType;
|
|
|
|
FlawlessIVCount = flawlessIVs;
|
|
|
|
Gender = gender;
|
|
|
|
}
|
|
|
|
|
2022-04-23 04:11:11 +00:00
|
|
|
public bool HasAlphaMove => IsAlpha && Type is not SlotType.Landmark;
|
|
|
|
|
2022-05-07 21:30:16 +00:00
|
|
|
protected override void ApplyDetails(ITrainerInfo sav, EncounterCriteria criteria, PKM pk)
|
|
|
|
{
|
|
|
|
base.ApplyDetails(sav, criteria, pk);
|
|
|
|
|
|
|
|
var pa = (PA8)pk;
|
|
|
|
if (IsAlpha)
|
|
|
|
pa.HeightScalarCopy = pa.HeightScalar = pa.WeightScalar = 255;
|
|
|
|
pa.ResetHeight();
|
|
|
|
pa.ResetWeight();
|
|
|
|
}
|
|
|
|
|
2022-05-06 22:43:23 +00:00
|
|
|
protected override void SetPINGA(PKM pk, EncounterCriteria criteria)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-05-06 22:43:23 +00:00
|
|
|
base.SetPINGA(pk, criteria);
|
2022-02-05 01:35:15 +00:00
|
|
|
if (Gender != Gender.Random)
|
|
|
|
pk.Gender = (int)Gender;
|
|
|
|
|
2022-04-23 04:11:11 +00:00
|
|
|
var para = GetParams();
|
2022-07-03 00:10:30 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var (_, slotSeed) = Overworld8aRNG.ApplyDetails(pk, criteria, para, HasAlphaMove);
|
|
|
|
if (LevelMin != LevelMax)
|
|
|
|
{
|
|
|
|
var lvl = Overworld8aRNG.GetRandomLevel(slotSeed, LevelMin, LevelMax);
|
|
|
|
if (criteria.ForceMinLevelRange && lvl != LevelMin)
|
|
|
|
continue;
|
|
|
|
pk.CurrentLevel = pk.Met_Level = lvl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-05-06 22:43:23 +00:00
|
|
|
}
|
2022-02-21 01:59:48 +00:00
|
|
|
|
2022-05-06 22:43:23 +00:00
|
|
|
protected override void SetEncounterMoves(PKM pk, GameVersion version, int level)
|
|
|
|
{
|
|
|
|
var pa8 = (PA8)pk;
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> moves = stackalloc ushort[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);
|
2022-09-19 05:00:46 +00:00
|
|
|
if (pa8.AlphaMove != 0)
|
|
|
|
pa8.SetMasteryFlagMove(pa8.AlphaMove);
|
2022-05-08 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public void LoadInitialMoveset(PA8 pa8, Span<ushort> moves, Learnset learn, int level)
|
2022-05-08 17:28:22 +00:00
|
|
|
{
|
2022-05-06 22:43:23 +00:00
|
|
|
if (pa8.AlphaMove != 0)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-05-06 22:43:23 +00:00
|
|
|
moves[0] = pa8.AlphaMove;
|
|
|
|
learn.SetEncounterMovesBackwards(level, moves, 1);
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
2022-05-06 22:43:23 +00:00
|
|
|
else
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-05-06 22:43:23 +00:00
|
|
|
learn.SetEncounterMoves(level, moves);
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
2022-05-08 17:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public (Learnset Learn, Learnset Mastery) GetLevelUpInfo()
|
|
|
|
{
|
|
|
|
var index = PersonalTable.LA.GetFormIndex(Species, Form);
|
|
|
|
var learn = Legal.LevelUpLA[index];
|
|
|
|
var mastery = Legal.MasteryLA[index];
|
|
|
|
return (learn, mastery);
|
2022-05-06 22:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void SetFormatSpecificData(PKM pk)
|
|
|
|
{
|
|
|
|
var pa8 = (PA8)pk;
|
|
|
|
if (IsAlpha)
|
|
|
|
pa8.IsAlpha = true;
|
|
|
|
pa8.HeightScalarCopy = pa8.HeightScalar;
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ApplyDetailsBall(PKM pk) => pk.Ball = (int)Ball.LAPoke;
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public override EncounterMatchRating GetMatchRating(PKM pk)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Gender is not Gender.Random && pk.Gender != (int)Gender)
|
2022-02-07 00:14:46 +00:00
|
|
|
return EncounterMatchRating.PartialMatch;
|
|
|
|
|
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-07 00:14:46 +00:00
|
|
|
{
|
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 EncounterMatchRating.DeferredErrors;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (FlawlessIVCount is not 0 && pk.FlawlessIVCount < FlawlessIVCount)
|
2022-02-05 01:35:15 +00:00
|
|
|
return EncounterMatchRating.DeferredErrors;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (IsFormArgMismatch(pk))
|
2022-03-06 02:54:50 +00:00
|
|
|
return EncounterMatchRating.DeferredErrors;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!IsForcedMasteryCorrect(pk))
|
2022-05-06 22:43:23 +00:00
|
|
|
return EncounterMatchRating.DeferredErrors;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return GetMoveCompatibility(pk);
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private bool IsFormArgMismatch(PKM pk) => pk.Species switch
|
2022-03-06 02:54:50 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
(int)Core.Species.Wyrdeer when Species is not (int)Core.Species.Wyrdeer && pk is IFormArgument { FormArgument: 0 } => true,
|
|
|
|
(int)Core.Species.Overqwil when Species is not (int)Core.Species.Overqwil && pk is IFormArgument { FormArgument: 0 } => true,
|
|
|
|
(int)Core.Species.Basculegion when Species is not (int)Core.Species.Basculegion && pk is IFormArgument { FormArgument: 0 } => true,
|
2022-03-06 02:54:50 +00:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private EncounterMatchRating GetMoveCompatibility(PKM pk)
|
2022-02-05 01:35:15 +00:00
|
|
|
{
|
|
|
|
// Check for Alpha move compatibility.
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is not PA8 pa)
|
2022-02-05 01:35:15 +00:00
|
|
|
return EncounterMatchRating.Match;
|
|
|
|
|
|
|
|
var alphaMove = pa.AlphaMove;
|
2022-02-05 18:23:00 +00:00
|
|
|
bool hasAlphaMove = alphaMove != 0;
|
2022-02-10 00:17:44 +00:00
|
|
|
if (!pa.IsAlpha || Type is SlotType.Landmark)
|
2022-02-05 18:23:00 +00:00
|
|
|
return !hasAlphaMove ? EncounterMatchRating.Match : EncounterMatchRating.DeferredErrors;
|
2022-02-05 01:35:15 +00:00
|
|
|
|
|
|
|
var pi = PersonalTable.LA.GetFormEntry(Species, Form);
|
|
|
|
var tutors = pi.SpecialTutors[0];
|
|
|
|
|
|
|
|
if (alphaMove is 0)
|
|
|
|
{
|
|
|
|
bool hasAnyTutor = Array.IndexOf(tutors, true) >= 0;
|
|
|
|
if (hasAnyTutor)
|
|
|
|
return EncounterMatchRating.Deferred;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var idx = pa.MoveShopPermitIndexes;
|
|
|
|
var index = idx.IndexOf(idx);
|
|
|
|
if (index == -1)
|
|
|
|
return EncounterMatchRating.Deferred;
|
|
|
|
if (!tutors[index])
|
|
|
|
return EncounterMatchRating.Deferred;
|
|
|
|
}
|
|
|
|
return EncounterMatchRating.Match;
|
|
|
|
}
|
2022-02-21 01:59:48 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public bool IsForcedMasteryCorrect(PKM pk)
|
2022-05-06 22:43:23 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is not IMoveShop8Mastery p)
|
2022-05-06 22:43:23 +00:00
|
|
|
return true; // Can't check.
|
|
|
|
|
2022-05-07 20:48:47 +00:00
|
|
|
bool allowAlphaPurchaseBug = Area.Type is not SlotType.OverworldMMO; // 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);
|
|
|
|
var learn = Legal.LevelUpLA[index];
|
2022-06-18 18:04:24 +00:00
|
|
|
ushort alpha = pk is PA8 pa ? pa.AlphaMove : (ushort)0;
|
2022-05-07 20:48:47 +00:00
|
|
|
if (!p.IsValidPurchasedEncounter(learn, level, alpha, allowAlphaPurchaseBug))
|
|
|
|
return false;
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
Span<ushort> moves = stackalloc ushort[4];
|
2022-05-07 20:48:47 +00:00
|
|
|
var mastery = Legal.MasteryLA[index];
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is PA8 { AlphaMove: not 0 } pa8)
|
2022-05-06 22:43:23 +00:00
|
|
|
{
|
2022-05-07 20:48:47 +00:00
|
|
|
moves[0] = pa8.AlphaMove;
|
2022-05-06 22:43:23 +00:00
|
|
|
learn.SetEncounterMovesBackwards(level, moves, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
learn.SetEncounterMoves(level, moves);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-23 04:11:11 +00:00
|
|
|
private OverworldParam8a GetParams()
|
2022-02-21 01:59:48 +00:00
|
|
|
{
|
2022-05-11 04:46:45 +00:00
|
|
|
var gender = GetGenderRatio();
|
2022-02-21 01:59:48 +00:00
|
|
|
return new OverworldParam8a
|
|
|
|
{
|
|
|
|
IsAlpha = IsAlpha,
|
|
|
|
FlawlessIVs = FlawlessIVCount,
|
|
|
|
Shiny = Shiny,
|
2022-04-23 04:11:11 +00:00
|
|
|
RollCount = GetRollCount(Type),
|
2022-02-21 01:59:48 +00:00
|
|
|
GenderRatio = gender,
|
|
|
|
};
|
|
|
|
}
|
2022-04-23 04:11:11 +00:00
|
|
|
|
2022-05-11 04:46:45 +00:00
|
|
|
private byte GetGenderRatio() => Gender switch
|
|
|
|
{
|
|
|
|
Gender.Male => PersonalInfo.RatioMagicMale,
|
|
|
|
Gender.Female => PersonalInfo.RatioMagicFemale,
|
|
|
|
_ => GetGenderRatioPersonal(),
|
|
|
|
};
|
|
|
|
|
|
|
|
private byte GetGenderRatioPersonal()
|
|
|
|
{
|
|
|
|
var pt = PersonalTable.LA;
|
|
|
|
var entry = pt.GetFormEntry(Species, Form);
|
|
|
|
return (byte)entry.Gender;
|
|
|
|
}
|
|
|
|
|
2022-04-23 04:11:11 +00:00
|
|
|
// hardcoded 7 to assume max dex progress + shiny charm.
|
|
|
|
private const int MaxRollCount = 7;
|
|
|
|
|
|
|
|
private static byte GetRollCount(SlotType type) => (byte)(MaxRollCount + type switch
|
|
|
|
{
|
2022-04-23 05:44:55 +00:00
|
|
|
SlotType.OverworldMMO => 12,
|
2022-04-23 04:11:11 +00:00
|
|
|
SlotType.OverworldMass => 25,
|
|
|
|
_ => 0,
|
|
|
|
});
|
2022-02-05 01:35:15 +00:00
|
|
|
}
|