PKHeX/PKHeX.Core/Legality/Encounters/EncounterStatic/EncounterStatic.cs

313 lines
11 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
namespace PKHeX.Core
{
/// <summary>
/// Static Encounter Data
/// </summary>
/// <remarks>
/// Static Encounters are fixed position encounters with properties that are not subject to Wild Encounter conditions.
/// </remarks>
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
public abstract record EncounterStatic : IEncounterable, IMoveset, ILocation, IEncounterMatch
{
2020-12-22 01:05:05 +00:00
public int Species { get; init; }
public int Form { get; init; }
public virtual int Level { get; init; }
public virtual int LevelMin => Level;
public virtual int LevelMax => Level;
public abstract int Generation { get; }
public GameVersion Version { get; }
2020-12-22 01:05:05 +00:00
public virtual int Location { get; init; }
public int Ability { get; init; }
public Shiny Shiny { get; init; } = Shiny.Random;
public int Gender { get; init; } = -1;
public int EggLocation { get; init; }
public Nature Nature { get; init; } = Nature.Random;
public bool Gift { get; init; }
public int Ball { get; init; } = 4; // Only checked when is Gift
2020-12-22 01:05:05 +00:00
public IReadOnlyList<int> Moves { get; init; } = Array.Empty<int>();
public IReadOnlyList<int> IVs { get; init; } = Array.Empty<int>();
public int FlawlessIVCount { get; init; }
2020-12-22 01:05:05 +00:00
public int HeldItem { get; init; }
public int EggCycles { get; init; }
2020-12-22 01:05:05 +00:00
public bool Fateful { get; init; }
Gen 1 move analysis improvement. Adapted the valid moves to take into account that move deleter and move reminder do not exits in generation 1 (#1037) * Fix getMoves with min level, when SkipWhile and TakeWhile is used together the index i in TakeWhile is calculated from the enumerator that results of the SkipWhile function, not the index of the initial array, those giving an incorrect index to check Levels array in the TakeWhile * Fix getMoves when levelmin or levelmax is above max level in the levels array, TakeWhile and SkipWhile return empty list if the while goes beyond the last element of the array * Include player hatched egg in the list of possible encounters for parseMoves only if the pokemon was an egg Also change the valur of WasEgg for gen1,2,3 pokemon if the encounter analyzed is not an egg add the non egg encounters to the list instead of checking the non-egg encounter inside parseMovesWasEggPreRelearn * Fix for gen3 egg encounters Remove non-egg encounters without special moves if there is an egg encounter because egg encounter already cover every possible move combination Do not add daycare egg encounter for gen3 unhatched egg if there is another encounter, that means is an event or gift egg, not a daycare egg Remove duplicate encounters * Gift egg should not allow inherited moves even it they dont have special moves Those gift eggs are hatched only with the species base moves * Added getEncounterMoves functions, to be used for generation 1 encounters to find what moves have a pokemon at the moment of being caught because there is no move reminder in generation 1 * Added GBEncounterData, structure for refactor the tuples used in generation 1 and 2 encounters * Add LevelMin and LevelMax to IEncounterable to get the encounter moves by the min level of the generation 1 EncounterLink Add iGeneration to difference generation 1 and generation 2 encounters for GB Era pokemon * Mark generation in gen 1 and 2 encounters There is no need to mark the generation in gen 3 to 7 encounters because in that generations it match the pokemon generation number * Add min level for generation 1 moves in getMoves functions Add function to return the default moves for a GB encounters, for generation 1 games that included both moves from level up table and level 1 moves from personal table Fix getMoves with min level when the moves list is empty, like Raichu generation 1 * Add maxSpecies to getBaseSpecies function for gen1 pokemon with a gen2 egg encounter Refactor VC Encounter changing Tuples for GBData class * Fixed for gen 2 Checks Also do not search for generation1 encounter if the gen2 pokemon have met location from crystal * Fix VC wild encounters, should be stored as array or else other verifyEncounter functions wont work * Add generation 1 parse moves function including default moves * Clean-up get encounters * Verify empty moves for generation 1 encounters, in generation 1 does not exits move deleter That means when a move slot have been used by a level up move or a TM/HM/Tutor move it cant be empty again Does not apply if generation 2 tradeback is allowed, in generation 2 there is a move deleter * Added two edge cases for pokemon that learn in red/blue and yellow different moves at the same level, this combinations can not exits until a later level when they learn again one of the levels in the other game, only happen for flareon and vaporeon * Check incompatible moves between evolution species, it is for species that learn a move in a level as an evolved species and a move at a upper level as a preevolution Also added most edge cases for the min slots used for generation 1 games, i think every weird combination is already covered * Fix gen 1 eevee and evolutions move checks * Cleanup * Move the code to change valid moves for generation 1 to a function * Fix getMoveMinLevelGBEncounter * getUsedMoveSlots, removed wild Butterfree edge case, it is not possible * Filter the min level of the encounter by the possible games the pokemon could be originated, yellow pikachu and kadabra can be detected
2017-04-09 00:17:20 +00:00
public bool EggEncounter => EggLocation > 0;
2018-03-09 05:18:32 +00:00
private const string _name = "Static Encounter";
public string Name => _name;
public string LongName => Version == GameVersion.Any ? _name : $"{_name} ({Version})";
public bool IsShiny => Shiny.IsShiny();
public bool IsRandomUnspecificForm => Form >= FormDynamic;
private const int FormDynamic = FormVivillon;
internal const int FormVivillon = 30;
//protected const int FormRandom = 31;
protected EncounterStatic(GameVersion game) => Version = game;
protected virtual PKM GetBlank(ITrainerInfo tr) => PKMConverter.GetBlank(Generation, Version);
public PKM ConvertToPKM(ITrainerInfo sav) => ConvertToPKM(sav, EncounterCriteria.Unrestricted);
public PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria)
{
var pk = GetBlank(sav);
sav.ApplyTo(pk);
ApplyDetails(sav, criteria, pk);
return pk;
}
protected virtual void ApplyDetails(ITrainerInfo sav, EncounterCriteria criteria, PKM pk)
{
pk.EncryptionConstant = Util.Rand32();
pk.Species = Species;
pk.Form = Form;
var version = this.GetCompatibleVersion((GameVersion)sav.Game);
int lang = (int)Language.GetSafeLanguage(Generation, (LanguageID)sav.Language, version);
int level = GetMinimalLevel();
pk.Version = (int)version;
pk.Language = lang = GetEdgeCaseLanguage(pk, lang);
pk.Nickname = SpeciesName.GetSpeciesNameGeneration(Species, lang, Generation);
pk.CurrentLevel = level;
pk.Ball = Ball;
pk.HeldItem = HeldItem;
pk.OT_Friendship = pk.PersonalInfo.BaseFriendship;
var today = DateTime.Today;
SetMetData(pk, level, today);
if (EggEncounter)
SetEggMetData(pk, sav, today);
SetPINGA(pk, criteria);
SetEncounterMoves(pk, version, level);
if (Fateful)
pk.FatefulEncounter = true;
if (pk.Format < 6)
return;
if (this is IRelearn relearn)
pk.SetRelearnMoves(relearn.Relearn);
sav.ApplyHandlingTrainerInfo(pk);
2019-11-16 01:34:18 +00:00
if (this is IGigantamax g && pk is IGigantamax pg)
pg.CanGigantamax = g.CanGigantamax;
if (this is IDynamaxLevel d && pk is IDynamaxLevel pd)
pd.DynamaxLevel = d.DynamaxLevel;
}
2019-11-16 01:34:18 +00:00
protected virtual int GetMinimalLevel() => LevelMin;
protected virtual void SetPINGA(PKM pk, EncounterCriteria criteria)
{
2020-06-21 23:16:34 +00:00
var pi = pk.PersonalInfo;
int gender = criteria.GetGender(Gender, pi);
int nature = (int)criteria.GetNature(Nature);
int ability = criteria.GetAbilityFromNumber(Ability);
var pidtype = GetPIDType();
2019-11-16 01:34:18 +00:00
PIDGenerator.SetRandomWildPID(pk, pk.Format, nature, ability, gender, pidtype);
SetIVs(pk);
pk.StatNature = pk.Nature;
}
private void SetEggMetData(PKM pk, ITrainerInfo tr, DateTime today)
{
pk.Met_Location = Math.Max(0, EncounterSuggestion.GetSuggestedEggMetLocation(pk));
pk.Met_Level = EncounterSuggestion.GetSuggestedEncounterEggMetLevel(pk);
if (Generation >= 4)
{
bool traded = (int)Version == tr.Game;
2020-12-11 03:49:53 +00:00
pk.Egg_Location = EncounterSuggestion.GetSuggestedEncounterEggLocationEgg(Generation, traded);
pk.EggMetDate = today;
}
pk.Egg_Location = EggLocation;
pk.EggMetDate = today;
}
protected virtual void SetMetData(PKM pk, int level, DateTime today)
{
if (pk.Format <= 2)
return;
pk.Met_Location = Location;
pk.Met_Level = level;
if (pk.Format >= 4)
pk.MetDate = today;
}
private void SetEncounterMoves(PKM pk, GameVersion version, int level)
{
var moves = Moves.Count > 0 ? Moves : MoveLevelUp.GetEncounterMoves(pk, level, version);
pk.SetMoves(moves);
pk.SetMaximumPPCurrent(moves);
}
protected void SetIVs(PKM pk)
{
if (IVs.Count != 0)
pk.SetRandomIVs(IVs, FlawlessIVCount);
else if (FlawlessIVCount > 0)
pk.SetRandomIVs(flawless: FlawlessIVCount);
}
private int GetEdgeCaseLanguage(PKM pk, int lang)
{
2020-12-11 03:49:53 +00:00
switch (this)
{
2020-12-11 03:49:53 +00:00
// Cannot trade between languages
case IFixedGBLanguage e:
return e.Language == EncounterGBLanguage.Japanese ? 1 : 2;
// E-Reader was only available to Japanese games.
case EncounterStaticShadow {EReader: true}:
2020-12-11 03:49:53 +00:00
// Old Sea Map was only distributed to Japanese games.
case EncounterStatic3 when Species == (int)Core.Species.Mew:
pk.OT_Name = "ゲーフリ";
2020-12-11 03:49:53 +00:00
return (int)LanguageID.Japanese;
// Deoxys for Emerald was not available for Japanese games.
case EncounterStatic3 when Species == (int)Core.Species.Deoxys && Version == GameVersion.E && lang == 1:
pk.OT_Name = "GF";
return (int)LanguageID.English;
default:
return lang;
}
}
private PIDType GetPIDType()
{
switch (Generation)
{
2020-12-29 08:58:08 +00:00
case 3 when this is EncounterStatic3 {Roaming: true, Version: not GameVersion.E}: // Roamer IV glitch was fixed in Emerald
return PIDType.Method_1_Roamer;
case 4 when Shiny == Shiny.Always: // Lake of Rage Gyarados
return PIDType.ChainShiny;
2019-06-01 17:22:49 +00:00
case 4 when Species == (int)Core.Species.Pichu: // Spiky Eared Pichu
case 4 when Location == Locations.PokeWalker4: // Pokéwalker
return PIDType.Pokewalker;
case 5 when Shiny == Shiny.Always:
return PIDType.G5MGShiny;
default: return PIDType.None;
}
}
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
public virtual bool IsMatchExact(PKM pkm, DexLevel evo)
{
2019-11-16 01:34:18 +00:00
if (Nature != Nature.Random && pkm.Nature != (int) Nature)
return false;
if (!IsMatchEggLocation(pkm))
2019-11-16 01:34:18 +00:00
return false;
if (!IsMatchLocation(pkm))
return false;
if (!IsMatchLevel(pkm, evo))
2019-11-16 01:34:18 +00:00
return false;
if (!IsMatchGender(pkm))
return false;
if (!IsMatchForm(pkm, evo))
2019-11-16 01:34:18 +00:00
return false;
if (!IsMatchIVs(pkm))
return false;
if (this is IContestStats es && pkm is IContestStats s && s.IsContestBelow(es))
2019-11-16 01:34:18 +00:00
return false;
// Defer to EC/PID check
// if (e.Shiny != null && e.Shiny != pkm.IsShiny)
// continue;
// Defer ball check to later
// if (e.Gift && pkm.Ball != 4) // PokéBall
// continue;
return true;
}
private bool IsMatchIVs(PKM pkm)
{
if (IVs.Count == 0)
2019-11-16 01:34:18 +00:00
return true; // nothing to check, IVs are random
if (Generation <= 2 && pkm.Format > 2)
return true; // IVs are regenerated on VC transfer upward
return Legal.GetIsFixedIVSequenceValidSkipRand(IVs, pkm);
}
protected virtual bool IsMatchForm(PKM pkm, DexLevel evo)
2019-11-16 01:34:18 +00:00
{
if (IsRandomUnspecificForm)
2019-11-16 01:34:18 +00:00
return true;
return Form == evo.Form || FormInfo.IsFormChangeable(Species, Form, pkm.Form, pkm.Format);
2019-11-16 01:34:18 +00:00
}
// override me if the encounter type has any eggs
protected virtual bool IsMatchEggLocation(PKM pkm) => pkm.Egg_Location == 0;
2019-11-16 01:34:18 +00:00
private bool IsMatchGender(PKM pkm)
{
if (Gender == -1 || Gender == pkm.Gender)
return true;
2019-11-16 01:34:18 +00:00
if (Species == (int) Core.Species.Azurill && Generation == 4 && Location == 233 && pkm.Gender == 0)
return PKX.GetGenderFromPIDAndRatio(pkm.PID, 0xBF) == 1;
2019-11-16 01:34:18 +00:00
return false;
}
protected virtual bool IsMatchLocation(PKM pkm)
{
if (EggEncounter)
return true;
if (Location == 0)
return true;
if (!pkm.HasOriginalMetLocation)
return true;
return Location == pkm.Met_Location;
}
protected virtual bool IsMatchLevel(PKM pkm, DexLevel evo)
2019-11-16 01:34:18 +00:00
{
return pkm.Met_Level == Level;
}
2021-02-14 23:14:45 +00:00
public virtual EncounterMatchRating GetMatchRating(PKM pkm)
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
{
if (IsMatchPartial(pkm))
return EncounterMatchRating.PartialMatch;
if (IsMatchDeferred(pkm))
return EncounterMatchRating.Deferred;
return EncounterMatchRating.Match;
}
2021-02-25 02:08:03 +00:00
/// <summary>
/// Checks if the provided <see cref="pkm"/> might not be the best match, or even a bad match due to minor reasons.
/// </summary>
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
protected virtual bool IsMatchDeferred(PKM pkm) => false;
2021-02-25 02:08:03 +00:00
/// <summary>
/// Checks if the provided <see cref="pkm"/> is not an exact match due to minor reasons.
/// </summary>
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
protected virtual bool IsMatchPartial(PKM pkm)
{
if (pkm.Format >= 5 && pkm.AbilityNumber == 4 && this.IsPartialMatchHidden(pkm.Species, Species))
return true;
return pkm.FatefulEncounter != Fateful;
}
}
}