2017-02-02 04:13:25 +00:00
|
|
|
|
using System;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2017-02-02 04:13:25 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
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 sealed class QRPK7 : IEncounterInfo
|
2017-02-02 04:13:25 +00:00
|
|
|
|
{
|
2020-10-04 15:59:33 +00:00
|
|
|
|
public GameVersion Version => (GameVersion)CassetteVersion;
|
|
|
|
|
public bool EggEncounter => false;
|
2022-03-07 07:25:47 +00:00
|
|
|
|
public byte LevelMin => Level;
|
|
|
|
|
public byte LevelMax => Level;
|
2020-10-04 15:59:33 +00:00
|
|
|
|
public int Generation => Version.GetGeneration();
|
2021-05-18 17:36:45 +00:00
|
|
|
|
public bool IsShiny => false;
|
2020-10-04 15:59:33 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private readonly byte[] Data;
|
2020-10-04 15:59:33 +00:00
|
|
|
|
public const int SIZE = 0x30;
|
|
|
|
|
public QRPK7(byte[] d) => Data = (byte[])d.Clone();
|
2017-02-02 04:13:25 +00:00
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public uint EncryptionConstant => ReadUInt32LittleEndian(Data.AsSpan(0));
|
2022-03-06 20:01:47 +00:00
|
|
|
|
public byte HT_Flags => Data[4];
|
2017-02-02 04:13:25 +00:00
|
|
|
|
public int Unk_5 => Data[5];
|
|
|
|
|
public int Unk_6 => Data[6];
|
|
|
|
|
public int Unk_7 => Data[7];
|
|
|
|
|
public int Move1_PPUps => Data[8];
|
|
|
|
|
public int Move2_PPUps => Data[9];
|
|
|
|
|
public int Move3_PPUps => Data[0xA];
|
|
|
|
|
public int Move4_PPUps => Data[0xB];
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public uint IV32 { get => ReadUInt32LittleEndian(Data.AsSpan(0xC)); set => WriteUInt32LittleEndian(Data.AsSpan(0xC), value); }
|
2019-01-30 05:07:42 +00:00
|
|
|
|
public int IV_HP { get => (int)(IV32 >> 00) & 0x1F; set => IV32 = (uint)((IV32 & ~(0x1F << 00)) | (uint)((value > 31 ? 31 : value) << 00)); }
|
|
|
|
|
public int IV_ATK { get => (int)(IV32 >> 05) & 0x1F; set => IV32 = (uint)((IV32 & ~(0x1F << 05)) | (uint)((value > 31 ? 31 : value) << 05)); }
|
|
|
|
|
public int IV_DEF { get => (int)(IV32 >> 10) & 0x1F; set => IV32 = (uint)((IV32 & ~(0x1F << 10)) | (uint)((value > 31 ? 31 : value) << 10)); }
|
|
|
|
|
public int IV_SPE { get => (int)(IV32 >> 15) & 0x1F; set => IV32 = (uint)((IV32 & ~(0x1F << 15)) | (uint)((value > 31 ? 31 : value) << 15)); }
|
|
|
|
|
public int IV_SPA { get => (int)(IV32 >> 20) & 0x1F; set => IV32 = (uint)((IV32 & ~(0x1F << 20)) | (uint)((value > 31 ? 31 : value) << 20)); }
|
|
|
|
|
public int IV_SPD { get => (int)(IV32 >> 25) & 0x1F; set => IV32 = (uint)((IV32 & ~(0x1F << 25)) | (uint)((value > 31 ? 31 : value) << 25)); }
|
2022-01-03 05:35:59 +00:00
|
|
|
|
public uint PID => ReadUInt32LittleEndian(Data.AsSpan(0x10));
|
|
|
|
|
public int Species => ReadUInt16LittleEndian(Data.AsSpan(0x14));
|
|
|
|
|
public ushort HeldItem => ReadUInt16LittleEndian(Data.AsSpan(0x16));
|
|
|
|
|
public ushort Move1 => ReadUInt16LittleEndian(Data.AsSpan(0x18));
|
|
|
|
|
public ushort Move2 => ReadUInt16LittleEndian(Data.AsSpan(0x1A));
|
|
|
|
|
public ushort Move3 => ReadUInt16LittleEndian(Data.AsSpan(0x1C));
|
|
|
|
|
public ushort Move4 => ReadUInt16LittleEndian(Data.AsSpan(0x1E));
|
2017-02-02 04:13:25 +00:00
|
|
|
|
public int Unk_20 => Data[0x20];
|
|
|
|
|
public int AbilityIndex => Data[0x21];
|
|
|
|
|
public int Nature => Data[0x22];
|
2019-01-30 05:07:42 +00:00
|
|
|
|
public bool FatefulEncounter => (Data[0x23] & 1) == 1;
|
|
|
|
|
public int Gender => (Data[0x23] >> 1) & 3;
|
2020-12-11 04:42:30 +00:00
|
|
|
|
public int Form => Data[0x23] >> 3;
|
2017-02-02 04:13:25 +00:00
|
|
|
|
public int EV_HP => Data[0x24];
|
|
|
|
|
public int EV_ATK => Data[0x25];
|
|
|
|
|
public int EV_DEF => Data[0x26];
|
|
|
|
|
public int EV_SPE => Data[0x27];
|
|
|
|
|
public int EV_SPA => Data[0x28];
|
|
|
|
|
public int EV_SPD => Data[0x29];
|
|
|
|
|
public int Unk_2A => Data[0x2A];
|
2019-01-30 05:07:42 +00:00
|
|
|
|
public int Friendship => Data[0x2B];
|
2017-02-02 04:13:25 +00:00
|
|
|
|
public int Ball => Data[0x2C];
|
2022-03-07 07:25:47 +00:00
|
|
|
|
public byte Level => Data[0x2D];
|
2017-02-02 04:13:25 +00:00
|
|
|
|
public int CassetteVersion => Data[0x2E];
|
|
|
|
|
public int Language => Data[0x2F];
|
|
|
|
|
|
2020-10-04 15:59:33 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts the <see cref="Data"/> to a rough PKM.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public PKM ConvertToPKM(ITrainerInfo sav) => ConvertToPKM(sav, EncounterCriteria.Unrestricted);
|
2019-01-30 05:07:42 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts the <see cref="Data"/> to a rough PKM.
|
|
|
|
|
/// </summary>
|
2020-10-04 15:59:33 +00:00
|
|
|
|
public PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria)
|
2019-01-30 05:07:42 +00:00
|
|
|
|
{
|
|
|
|
|
var pk = new PK7
|
|
|
|
|
{
|
|
|
|
|
EncryptionConstant = EncryptionConstant,
|
|
|
|
|
PID = PID,
|
|
|
|
|
Language = Language,
|
|
|
|
|
Species = Species,
|
|
|
|
|
Gender = Gender,
|
|
|
|
|
Nature = Nature,
|
|
|
|
|
FatefulEncounter = FatefulEncounter,
|
2020-12-11 04:42:30 +00:00
|
|
|
|
Form = Form,
|
2019-01-30 05:07:42 +00:00
|
|
|
|
HyperTrainFlags = HT_Flags,
|
|
|
|
|
IV_HP = IV_HP,
|
|
|
|
|
IV_ATK = IV_ATK,
|
|
|
|
|
IV_DEF = IV_DEF,
|
|
|
|
|
IV_SPA = IV_SPA,
|
|
|
|
|
IV_SPD = IV_SPD,
|
|
|
|
|
IV_SPE = IV_SPE,
|
|
|
|
|
EV_HP = EV_HP,
|
|
|
|
|
EV_ATK = EV_ATK,
|
|
|
|
|
EV_DEF = EV_DEF,
|
|
|
|
|
EV_SPA = EV_SPA,
|
|
|
|
|
EV_SPD = EV_SPD,
|
|
|
|
|
EV_SPE = EV_SPE,
|
|
|
|
|
Move1 = Move1,
|
|
|
|
|
Move2 = Move2,
|
|
|
|
|
Move3 = Move3,
|
|
|
|
|
Move4 = Move4,
|
|
|
|
|
Move1_PPUps = Move1_PPUps,
|
|
|
|
|
Move2_PPUps = Move2_PPUps,
|
|
|
|
|
Move3_PPUps = Move3_PPUps,
|
|
|
|
|
Move4_PPUps = Move4_PPUps,
|
|
|
|
|
HeldItem = HeldItem,
|
|
|
|
|
HT_Friendship = Friendship,
|
|
|
|
|
OT_Friendship = Friendship,
|
|
|
|
|
Ball = Ball,
|
|
|
|
|
Version = CassetteVersion,
|
|
|
|
|
|
2020-10-04 15:59:33 +00:00
|
|
|
|
OT_Name = sav.OT,
|
|
|
|
|
HT_Name = sav.OT,
|
2019-01-30 05:07:42 +00:00
|
|
|
|
CurrentLevel = Level,
|
|
|
|
|
Met_Level = Level,
|
|
|
|
|
MetDate = DateTime.Now,
|
|
|
|
|
};
|
2022-04-09 08:39:34 +00:00
|
|
|
|
RecentTrainerCache.SetConsoleRegionData3DS(pk, sav);
|
2019-01-30 05:07:42 +00:00
|
|
|
|
|
|
|
|
|
pk.RefreshAbility(AbilityIndex >> 1);
|
2019-07-12 23:41:13 +00:00
|
|
|
|
pk.ForcePartyData();
|
2019-01-30 05:07:42 +00:00
|
|
|
|
|
|
|
|
|
pk.RefreshChecksum();
|
|
|
|
|
return pk;
|
|
|
|
|
}
|
2017-02-02 04:13:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|