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-06-18 18:04:24 +00:00
|
|
|
using static PKHeX.Core.GroundTileAllowed;
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Generation 4 Static Encounter
|
|
|
|
/// </summary>
|
|
|
|
/// <inheritdoc cref="EncounterStatic"/>
|
|
|
|
public sealed record EncounterStatic4(GameVersion Version) : EncounterStatic(Version), IGroundTypeTile
|
|
|
|
{
|
|
|
|
public override int Generation => 4;
|
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.Gen4;
|
2020-08-30 23:10:24 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary> Indicates if the encounter is a Roamer (variable met location) </summary>
|
|
|
|
public bool Roaming { get; init; }
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary> <see cref="PK4.GroundTile"/> values permitted for the encounter. </summary>
|
|
|
|
public GroundTileAllowed GroundTile { get; init; } = None;
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchLocation(PKM pk)
|
|
|
|
{
|
|
|
|
if (!Roaming)
|
|
|
|
return base.IsMatchLocation(pk);
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Met location is lost on transfer
|
|
|
|
if (pk is not G4PKM pk4)
|
2021-05-21 20:57:07 +00:00
|
|
|
return true;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
return pk4.GroundTile switch
|
|
|
|
{
|
|
|
|
GroundTileType.Grass => IsMatchLocationGrass(Location, pk4.Met_Location),
|
|
|
|
GroundTileType.Water => IsMatchLocationWater(Location, pk4.Met_Location),
|
|
|
|
_ => false,
|
|
|
|
};
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-12-23 04:15:56 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private static bool IsMatchLocationGrass(int location, int met) => location switch
|
|
|
|
{
|
|
|
|
FirstS => IsMatchRoamerLocation(PermitGrassS, met, FirstS),
|
|
|
|
FirstJ => IsMatchRoamerLocation(PermitGrassJ, met, FirstJ),
|
|
|
|
FirstH => IsMatchRoamerLocation(PermitGrassH, met, FirstH),
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
private static bool IsMatchLocationWater(int location, int met) => location switch
|
|
|
|
{
|
|
|
|
FirstS => IsMatchRoamerLocation(PermitWaterS, met, FirstS),
|
|
|
|
FirstJ => IsMatchRoamerLocation(PermitWaterJ, met, FirstJ),
|
|
|
|
FirstH => IsMatchRoamerLocation(PermitWaterH, met, FirstH),
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchEggLocation(PKM pk)
|
|
|
|
{
|
|
|
|
if (!EggEncounter)
|
|
|
|
return base.IsMatchEggLocation(pk);
|
|
|
|
|
|
|
|
var eggloc = pk.Egg_Location;
|
|
|
|
// Transferring 4->5 clears Pt/HG/SS location value and keeps Faraway Place
|
|
|
|
if (pk is not G4PKM pk4)
|
2020-12-23 04:15:56 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (eggloc == Locations.LinkTrade4)
|
|
|
|
return true;
|
|
|
|
var cmp = Locations.IsPtHGSSLocationEgg(EggLocation) ? Locations.Faraway4 : EggLocation;
|
|
|
|
return eggloc == cmp;
|
2020-12-23 04:15:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!pk4.IsEgg) // hatched
|
|
|
|
return eggloc == EggLocation || eggloc == Locations.LinkTrade4;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Unhatched:
|
|
|
|
if (eggloc != EggLocation)
|
|
|
|
return false;
|
|
|
|
if (pk4.Met_Location is not (0 or Locations.LinkTrade4))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-21 23:35:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void ApplyDetails(ITrainerInfo tr, EncounterCriteria criteria, PKM pk)
|
|
|
|
{
|
|
|
|
base.ApplyDetails(tr, criteria, pk);
|
|
|
|
SanityCheckVersion(pk);
|
|
|
|
}
|
Fracture the encounter matching checks to allow progressive validation (#3137)
## Issue
We want to discard-but-remember any slots that aren't a perfect fit, on the off chance that a better one exists later in the search space. If there's no better match, then we gotta go with what we got.
## Example:
Wurmple exists in area `X`, and also has a more rare slot for Silcoon, with the same level for both slots.
* We have a Silcoon that we've leveled up a few times.
Was our Silcoon originally a Wurmple, or was it caught as a Silcoon?
* To be sure, we have to check the EC/PID if the Wurmple wouldn't evolve into Cascoon instead.
* We don't want to wholly reject that Wurmple slot, as maybe the Met Level isn't within Silcoon's slot range.
---
Existing implementation would store "deferred" matches in a list; we only need to keep 1 of these matches around (less allocation!). We also want to differentiate between a "good" deferral and a "bad" deferral; I don't think this is necessary but it's currently used by Mystery Gift matching (implemented for the Eeveelution mystery gifts which matter for evolution moves).
The existing logic didn't use inheritance, and instead had static methods being reused across generations. Quite kludgy. Also, the existing logic was a pain to modify the master encounter yield methods, as one generation's quirks had to not impact all other generations that used the method.
---
The new implementation splits out the encounter yielding methods to be separate for each generation / subset. Now, things don't have to check `WasLink` for Gen7 origin, because Pokémon Link wasn't a thing in Gen7.
---
## Future
Maybe refactoring yielders into "GameCores" that expose yielding behaviors / properties, rather than the static logic. As more generations and side-gamegroups get added (thanks LGPE/GO/GameCube), all this switch stuff gets annoying to maintain instead of just overriding/inheritance.
## Conclusion
This shouldn't impact any legality results negatively; if you notice any regressions, report them! This should reduce false flags where we didn't defer-discard an encounter when we should have (wild area mons being confused with raids).
2021-01-30 01:55:27 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void SanityCheckVersion(PKM pk)
|
|
|
|
{
|
|
|
|
// Unavailable encounters in DP, morph them to Pt so they're legal.
|
|
|
|
switch (Species)
|
2020-08-21 23:35:49 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case (int)Core.Species.Darkrai when Location == 079: // DP Darkrai
|
|
|
|
case (int)Core.Species.Shaymin when Location == 063: // DP Shaymin
|
|
|
|
pk.Version = (int)GameVersion.Pt;
|
|
|
|
return;
|
2020-08-21 23:35:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchLevel(PKM pk, EvoCriteria evo)
|
|
|
|
{
|
|
|
|
if (pk.Format != 4) // Met Level lost on PK4=>PK5
|
|
|
|
return Level <= evo.LevelMax;
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return pk.Met_Level == (EggEncounter ? 0 : Level);
|
|
|
|
}
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override bool IsMatchPartial(PKM pk)
|
|
|
|
{
|
|
|
|
if (Gift && pk.Ball != Ball)
|
|
|
|
return true;
|
|
|
|
return base.IsMatchPartial(pk);
|
|
|
|
}
|
2020-12-23 20:15:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected override void SetMetData(PKM pk, int level, DateTime today)
|
|
|
|
{
|
|
|
|
var pk4 = (PK4)pk;
|
2022-08-27 06:43:36 +00:00
|
|
|
pk4.GroundTile = Roaming ? GroundTileType.Grass : GroundTile.GetIndex();
|
|
|
|
pk.Met_Location = Location;
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.Met_Level = level;
|
|
|
|
pk.MetDate = today;
|
2020-08-21 23:35:49 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
public static bool IsMatchRoamerLocation(uint permit, int location, int first)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
var value = location - first;
|
|
|
|
if ((uint)value >= 64)
|
|
|
|
return false;
|
|
|
|
return (permit & (1u << value)) != 0;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
// Merged all locations into a bitmask for quick computation.
|
|
|
|
private const int FirstS = 16;
|
|
|
|
private const uint PermitGrassS = 0x8033FFFF; // 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 36, 37, 47, 49,
|
|
|
|
private const uint PermitWaterS = 0x803E3B9E; // 18, 19, 20, 23, 24, 25, 27, 28, 29, 33, 34, 35, 36, 37, 47, 49,
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private const int FirstJ = 177;
|
|
|
|
private const uint PermitGrassJ = 0x0003E7FF; // 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 190, 191, 192, 193, 194,
|
|
|
|
private const uint PermitWaterJ = 0x0001E06E; // 178, 179, 180, 182, 183, 190, 191, 192, 193,
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
private const int FirstH = 149;
|
|
|
|
private const uint PermitGrassH = 0x0AB3FFFF; // 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 169, 170, 172, 174, 176,
|
|
|
|
private const uint PermitWaterH = 0x0ABC1B28; // 152, 154, 157, 158, 160, 161, 167, 168, 169, 170, 172, 174, 176,
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|