2022-06-26 06:08:28 +00:00
|
|
|
using System;
|
2020-01-19 00:46:38 +00:00
|
|
|
using System.Collections.Generic;
|
2018-03-31 07:43:41 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Trade Encounter data
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Trade data is fixed level in all cases except for the first few generations of games.
|
|
|
|
/// </remarks>
|
|
|
|
public abstract record EncounterTrade(GameVersion Version) : IEncounterable, IMoveset, IEncounterMatch
|
2016-11-08 16:43:57 +00:00
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
public ushort Species { get; init; }
|
|
|
|
public byte Form { get; init; }
|
2022-06-18 18:04:24 +00:00
|
|
|
public byte Level { get; init; }
|
|
|
|
public virtual byte LevelMin => Level;
|
|
|
|
public byte LevelMax => 100;
|
|
|
|
public abstract int Generation { get; }
|
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 abstract EntityContext Context { get; }
|
2016-11-08 16:43:57 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int CurrentLevel { get; init; } = -1;
|
|
|
|
public abstract int Location { get; }
|
2022-03-01 06:46:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public AbilityPermission Ability { get; init; }
|
|
|
|
public Nature Nature { get; init; } = Nature.Random;
|
|
|
|
public virtual Shiny Shiny => Shiny.Never;
|
|
|
|
public sbyte Gender { get; init; } = -1;
|
2020-08-30 22:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public sbyte OTGender { get; init; } = -1;
|
|
|
|
public bool IsNicknamed { get; init; } = true;
|
|
|
|
public bool EvolveOnTrade { get; init; }
|
|
|
|
public byte Ball { get; init; } = 4;
|
2022-03-01 06:46:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int EggLocation { get; init; }
|
2021-08-24 21:03:20 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public ushort TID { get; init; }
|
|
|
|
public ushort SID { get; init; }
|
2020-08-30 22:35:59 +00:00
|
|
|
|
2022-08-22 00:34:32 +00:00
|
|
|
public Moveset Moves { get; init; }
|
|
|
|
public IndividualValueSet IVs { get; init; }
|
2020-08-30 22:35:59 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public Ball FixedBall => (Ball)Ball;
|
|
|
|
public bool EggEncounter => false;
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public int TID7
|
|
|
|
{
|
|
|
|
init
|
2018-11-11 05:07:31 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
TID = (ushort) value;
|
|
|
|
SID = (ushort)(value >> 16);
|
2018-11-11 05:07:31 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-11-11 05:07:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private const string _name = "In-game Trade";
|
|
|
|
public string Name => _name;
|
|
|
|
public string LongName => _name;
|
|
|
|
public bool IsShiny => Shiny.IsShiny();
|
2017-03-29 07:48:32 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public IReadOnlyList<string> Nicknames { get; internal set; } = Array.Empty<string>();
|
|
|
|
public IReadOnlyList<string> TrainerNames { get; internal set; } = Array.Empty<string>();
|
|
|
|
public string GetNickname(int language) => (uint)language < Nicknames.Count ? Nicknames[language] : string.Empty;
|
|
|
|
public string GetOT(int language) => (uint)language < TrainerNames.Count ? TrainerNames[language] : string.Empty;
|
|
|
|
public bool HasNickname => Nicknames.Count != 0 && IsNicknamed;
|
|
|
|
public bool HasTrainerName => TrainerNames.Count != 0;
|
2018-03-27 05:23:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public PKM ConvertToPKM(ITrainerInfo tr) => ConvertToPKM(tr, EncounterCriteria.Unrestricted);
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public PKM ConvertToPKM(ITrainerInfo tr, EncounterCriteria criteria)
|
|
|
|
{
|
|
|
|
var pk = EntityBlank.GetBlank(Generation, Version);
|
|
|
|
tr.ApplyTo(pk);
|
2019-02-09 19:37:20 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
ApplyDetails(tr, criteria, pk);
|
|
|
|
return pk;
|
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected virtual void ApplyDetails(ITrainerInfo sav, EncounterCriteria criteria, PKM pk)
|
|
|
|
{
|
|
|
|
var version = this.GetCompatibleVersion((GameVersion)sav.Game);
|
|
|
|
int lang = (int)Language.GetSafeLanguage(Generation, (LanguageID)sav.Language, version);
|
|
|
|
int level = CurrentLevel > 0 ? CurrentLevel : LevelMin;
|
|
|
|
if (level == 0)
|
|
|
|
level = Math.Max((byte)1, LevelMin);
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
ushort species = Species;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (EvolveOnTrade)
|
|
|
|
species++;
|
|
|
|
|
|
|
|
pk.EncryptionConstant = Util.Rand32();
|
|
|
|
pk.Species = species;
|
|
|
|
pk.Form = Form;
|
|
|
|
pk.Language = lang;
|
|
|
|
pk.OT_Name = pk.Format == 1 ? StringConverter12.G1TradeOTStr : HasTrainerName ? GetOT(lang) : sav.OT;
|
|
|
|
pk.OT_Gender = HasTrainerName ? Math.Max(0, (int)OTGender) : sav.Gender;
|
|
|
|
pk.SetNickname(HasNickname ? GetNickname(lang) : string.Empty);
|
|
|
|
|
|
|
|
pk.CurrentLevel = level;
|
|
|
|
pk.Version = (int) version;
|
|
|
|
pk.TID = TID;
|
|
|
|
pk.SID = SID;
|
|
|
|
pk.Ball = Ball;
|
|
|
|
pk.OT_Friendship = pk.PersonalInfo.BaseFriendship;
|
|
|
|
|
|
|
|
SetPINGA(pk, criteria);
|
|
|
|
SetMoves(pk, version, level);
|
|
|
|
|
|
|
|
var time = DateTime.Now;
|
|
|
|
if (pk.Format != 2 || version == GameVersion.C)
|
2019-11-16 01:34:18 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
SetMetData(pk, level, Location, time);
|
2018-03-31 07:43:41 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2018-12-30 06:24:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
pk.OT_Gender = 0;
|
2019-02-09 19:37:20 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (EggLocation != 0)
|
|
|
|
SetEggMetData(pk, time);
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Format < 6)
|
|
|
|
return;
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
sav.ApplyHandlingTrainerInfo(pk, force: true);
|
|
|
|
pk.SetRandomEC();
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is IScaledSize s)
|
2018-12-30 06:24:34 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
s.HeightScalar = PokeSizeUtil.GetRandomScalar();
|
|
|
|
s.WeightScalar = PokeSizeUtil.GetRandomScalar();
|
2018-12-30 06:24:34 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk is PK6 pk6)
|
|
|
|
pk6.SetRandomMemory6();
|
|
|
|
}
|
2018-12-30 06:24:34 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected virtual void SetPINGA(PKM pk, EncounterCriteria criteria)
|
|
|
|
{
|
|
|
|
var pi = pk.PersonalInfo;
|
|
|
|
int gender = criteria.GetGender(Gender, pi);
|
|
|
|
int nature = (int)criteria.GetNature(Nature);
|
|
|
|
int ability = criteria.GetAbilityFromNumber(Ability);
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
PIDGenerator.SetRandomWildPID(pk, Generation, nature, ability, gender);
|
|
|
|
pk.Nature = pk.StatNature = nature;
|
|
|
|
pk.Gender = gender;
|
|
|
|
pk.RefreshAbility(ability);
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
SetIVs(pk);
|
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected void SetIVs(PKM pk)
|
|
|
|
{
|
2022-08-22 00:34:32 +00:00
|
|
|
if (IVs.IsSpecified)
|
|
|
|
pk.SetRandomIVsTemplate(IVs, 0);
|
2022-06-18 18:04:24 +00:00
|
|
|
else
|
2022-06-26 06:08:28 +00:00
|
|
|
pk.SetRandomIVs(minFlawless: 3);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void SetMoves(PKM pk, GameVersion version, int level)
|
|
|
|
{
|
2022-08-22 00:34:32 +00:00
|
|
|
if (Moves.HasMoves)
|
|
|
|
{
|
|
|
|
pk.SetMoves(Moves);
|
|
|
|
pk.SetMaximumPPCurrent(Moves);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var moves = MoveLevelUp.GetEncounterMoves(pk, level, version);
|
|
|
|
pk.SetMoves(moves);
|
|
|
|
pk.SetMaximumPPCurrent(moves);
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private void SetEggMetData(PKM pk, DateTime time)
|
|
|
|
{
|
|
|
|
pk.Egg_Location = EggLocation;
|
|
|
|
pk.EggMetDate = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void SetMetData(PKM pk, int level, int location, DateTime time)
|
|
|
|
{
|
|
|
|
pk.Met_Level = level;
|
|
|
|
pk.Met_Location = location;
|
|
|
|
pk.MetDate = time;
|
|
|
|
}
|
2019-11-16 01:34:18 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public virtual bool IsMatchExact(PKM pk, EvoCriteria evo)
|
|
|
|
{
|
2022-08-22 00:34:32 +00:00
|
|
|
if (IVs.IsSpecified)
|
2022-05-31 04:43:52 +00:00
|
|
|
{
|
2022-08-22 00:34:32 +00:00
|
|
|
if (!Legal.GetIsFixedIVSequenceValidSkipRand(IVs, pk))
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
2022-05-31 04:43:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!IsMatchNatureGenderShiny(pk))
|
|
|
|
return false;
|
|
|
|
if (TID != pk.TID)
|
|
|
|
return false;
|
|
|
|
if (SID != pk.SID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!IsMatchLevel(pk, evo))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CurrentLevel != -1 && CurrentLevel > pk.CurrentLevel)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Form != evo.Form && !FormInfo.IsFormChangeable(Species, Form, pk.Form, pk.Format))
|
|
|
|
return false;
|
|
|
|
if (OTGender != -1 && OTGender != pk.OT_Gender)
|
|
|
|
return false;
|
|
|
|
if (!IsMatchEggLocation(pk))
|
|
|
|
return false;
|
|
|
|
// if (z.Ability == 4 ^ pk.AbilityNumber == 4) // defer to Ability
|
|
|
|
// continue;
|
|
|
|
if (!Version.Contains((GameVersion)pk.Version))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected virtual bool IsMatchEggLocation(PKM pk)
|
|
|
|
{
|
|
|
|
var expect = EggLocation;
|
|
|
|
if (pk is PB8 && expect is 0)
|
|
|
|
expect = Locations.Default8bNone;
|
|
|
|
return pk.Egg_Location == expect;
|
|
|
|
}
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private bool IsMatchLevel(PKM pk, EvoCriteria evo)
|
|
|
|
{
|
|
|
|
if (!pk.HasOriginalMetLocation)
|
|
|
|
return evo.LevelMax >= Level;
|
2020-08-21 23:35:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Location != pk.Met_Location)
|
|
|
|
return false;
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Format < 5)
|
|
|
|
return evo.LevelMax >= Level;
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return pk.Met_Level == Level;
|
|
|
|
}
|
2018-12-27 09:00:08 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
protected virtual bool IsMatchNatureGenderShiny(PKM pk)
|
|
|
|
{
|
|
|
|
if (!Shiny.IsValid(pk))
|
|
|
|
return false;
|
|
|
|
if (Gender != -1 && Gender != pk.Gender)
|
|
|
|
return false;
|
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
|
|
|
if (Nature != Nature.Random && pk.Nature != (int)Nature)
|
|
|
|
return false;
|
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
|
|
|
return true;
|
2019-11-26 18:46:16 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
public EncounterMatchRating GetMatchRating(PKM pk)
|
|
|
|
{
|
|
|
|
if (IsMatchPartial(pk))
|
|
|
|
return EncounterMatchRating.PartialMatch;
|
|
|
|
if (IsMatchDeferred(pk))
|
|
|
|
return EncounterMatchRating.Deferred;
|
|
|
|
return EncounterMatchRating.Match;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual bool IsMatchDeferred(PKM pk) => false;
|
|
|
|
protected virtual bool IsMatchPartial(PKM pk) => false;
|
2016-11-08 16:43:57 +00:00
|
|
|
}
|