PKHeX/PKHeX.Core/MysteryGifts/WC6.cs

599 lines
26 KiB
C#
Raw Normal View History

2015-11-22 21:38:16 +00:00
using System;
using System.Collections.Generic;
using static System.Buffers.Binary.BinaryPrimitives;
2015-11-22 21:38:16 +00:00
namespace PKHeX.Core
2015-11-22 21:38:16 +00:00
{
/// <summary>
/// Generation 6 Mystery Gift Template File
/// </summary>
public sealed class WC6 : DataMysteryGift, IRibbonSetEvent3, IRibbonSetEvent4, ILangNick, IContestStats, IContestStatsMutable, INature, IMemoryOT
2015-11-22 21:38:16 +00:00
{
public const int Size = 0x108;
public const uint EonTicketConst = 0x225D73C2;
public override int Generation => 6;
2015-11-22 21:38:16 +00:00
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
public WC6() : this(new byte[Size]) { }
public WC6(byte[] data) : base(data) { }
2018-05-12 15:13:39 +00:00
public int RestrictLanguage { get; set; } // None
public byte RestrictVersion { get; set; } // Permit All
public bool CanBeReceivedByVersion(int v)
{
2021-08-22 08:26:28 +00:00
if (v is < (int)GameVersion.X or > (int)GameVersion.OR)
return false;
if (RestrictVersion == 0)
return true; // no data
var bitIndex = v - (int) GameVersion.X;
var bit = 1 << bitIndex;
return (RestrictVersion & bit) != 0;
}
2015-11-22 21:38:16 +00:00
// General Card Properties
public override int CardID
{
get => ReadUInt16LittleEndian(Data.AsSpan(0));
set => WriteUInt16LittleEndian(Data.AsSpan(0), (ushort)value);
}
public override string CardTitle
{
// Max len 36 char, followed by null terminator
get => StringConverter6.GetString(Data.AsSpan(2, 0x4A));
set => StringConverter6.SetString(Data.AsSpan(2, 0x4A), value.AsSpan(), 36, StringConverterOption.ClearZero);
}
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
internal uint RawDate
{
get => ReadUInt32LittleEndian(Data.AsSpan(0x4C));
set => WriteUInt32LittleEndian(Data.AsSpan(0x4C), value);
}
private uint Year
{
get => RawDate / 10000;
set => RawDate = SetDate(value, Month, Day);
}
private uint Month
{
get => RawDate % 10000 / 100;
set => RawDate = SetDate(Year, value, Day);
}
private uint Day
{
get => RawDate % 100;
set => RawDate = SetDate(Year, Month, value);
}
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
public static uint SetDate(uint year, uint month, uint day) => (year * 10000) + (month * 100) + day;
/// <summary>
/// Gets or sets the date of the card.
/// </summary>
public DateTime? Date
{
get
{
// Check to see if date is valid
2021-05-14 23:46:48 +00:00
if (!DateUtil.IsDateValid(Year, Month, Day))
return null;
return new DateTime((int)Year, (int)Month, (int)Day);
}
set
{
if (value.HasValue)
{
// Only update the properties if a value is provided.
Year = (ushort)value.Value.Year;
Month = (byte)value.Value.Month;
Day = (byte)value.Value.Day;
}
else
{
// Clear the Met Date.
// If code tries to access MetDate again, null will be returned.
Year = 0;
Month = 0;
Day = 0;
}
}
}
public int CardLocation { get => Data[0x50]; set => Data[0x50] = (byte)value; }
2015-11-22 21:38:16 +00:00
public int CardType { get => Data[0x51]; set => Data[0x51] = (byte)value; }
public override bool GiftUsed { get => Data[0x52] >> 1 > 0; set => Data[0x52] = (byte)((Data[0x52] & ~2) | (value ? 2 : 0)); }
public bool MultiObtain { get => Data[0x53] == 1; set => Data[0x53] = value ? (byte)1 : (byte)0; }
2015-11-22 21:38:16 +00:00
// Item Properties
public override bool IsItem { get => CardType == 1; set { if (value) CardType = 1; } }
public override int ItemID {
get => ReadUInt16LittleEndian(Data.AsSpan(0x68));
set => WriteUInt16LittleEndian(Data.AsSpan(0x68), (ushort)value); }
public override int Quantity {
get => ReadUInt16LittleEndian(Data.AsSpan(0x70));
set => WriteUInt16LittleEndian(Data.AsSpan(0x70), (ushort)value); }
2018-05-12 15:13:39 +00:00
2015-11-22 21:38:16 +00:00
// Pokémon Properties
public override bool IsEntity { get => CardType == 0; set { if (value) CardType = 0; } }
public override bool IsShiny => Shiny.IsShiny();
public override Shiny Shiny => IsEgg ? Shiny.Random : PIDType switch
{
ShinyType6.FixedValue => GetShinyXor() switch
{
0 => Shiny.AlwaysSquare,
<= 15 => Shiny.AlwaysStar,
_ => Shiny.Never,
},
ShinyType6.Random => Shiny.Random,
ShinyType6.Never => Shiny.Never,
ShinyType6.Always => Shiny.Always,
_ => throw new ArgumentOutOfRangeException(),
};
private int GetShinyXor()
{
// Player owned anti-shiny fixed PID
if (TID == 0 && SID == 0)
return int.MaxValue;
var pid = PID;
var psv = (int)(pid >> 16 ^ (pid & 0xFFFF));
var tsv = (TID ^ SID);
return psv ^ tsv;
}
public override int TID { get => ReadUInt16LittleEndian(Data.AsSpan(0x68)); set => WriteUInt16LittleEndian(Data.AsSpan(0x68), (ushort)value); }
public override int SID { get => ReadUInt16LittleEndian(Data.AsSpan(0x6A)); set => WriteUInt16LittleEndian(Data.AsSpan(0x6A), (ushort)value); }
public int OriginGame { get => Data[0x6C]; set => Data[0x6C] = (byte)value; }
public uint EncryptionConstant { get => ReadUInt32LittleEndian(Data.AsSpan(0x70)); set => WriteUInt32LittleEndian(Data.AsSpan(0x70), value); }
public override int Ball { get => Data[0x76]; set => Data[0x76] = (byte)value; }
public override int HeldItem { get => ReadUInt16LittleEndian(Data.AsSpan(0x78)); set => WriteUInt16LittleEndian(Data.AsSpan(0x78), (ushort)value); }
public int Move1 { get => ReadUInt16LittleEndian(Data.AsSpan(0x7A)); set => WriteUInt16LittleEndian(Data.AsSpan(0x7A), (ushort)value); }
public int Move2 { get => ReadUInt16LittleEndian(Data.AsSpan(0x7C)); set => WriteUInt16LittleEndian(Data.AsSpan(0x7C), (ushort)value); }
public int Move3 { get => ReadUInt16LittleEndian(Data.AsSpan(0x7E)); set => WriteUInt16LittleEndian(Data.AsSpan(0x7E), (ushort)value); }
public int Move4 { get => ReadUInt16LittleEndian(Data.AsSpan(0x80)); set => WriteUInt16LittleEndian(Data.AsSpan(0x80), (ushort)value); }
public override int Species { get => ReadUInt16LittleEndian(Data.AsSpan(0x82)); set => WriteUInt16LittleEndian(Data.AsSpan(0x82), (ushort)value); }
public override int Form { get => Data[0x84]; set => Data[0x84] = (byte)value; }
public int Language { get => Data[0x85]; set => Data[0x85] = (byte)value; }
public string Nickname
{
get => StringConverter6.GetString(Data.AsSpan(0x86, 0x1A));
set => StringConverter6.SetString(Data.AsSpan(0x86, 0x1A), value.AsSpan(), 12, StringConverterOption.ClearZero);
}
public int Nature { get => (sbyte)Data[0xA0]; set => Data[0xA0] = (byte)value; }
public override int Gender { get => Data[0xA1]; set => Data[0xA1] = (byte)value; }
public override int AbilityType { get => Data[0xA2]; set => Data[0xA2] = (byte)value; }
public ShinyType6 PIDType { get => (ShinyType6)Data[0xA3]; set => Data[0xA3] = (byte)value; }
public override int EggLocation { get => ReadUInt16LittleEndian(Data.AsSpan(0xA4)); set => WriteUInt16LittleEndian(Data.AsSpan(0xA4), (ushort)value); }
public int MetLocation { get => ReadUInt16LittleEndian(Data.AsSpan(0xA6)); set => WriteUInt16LittleEndian(Data.AsSpan(0xA6), (ushort)value); }
2015-11-22 21:38:16 +00:00
public byte CNT_Cool { get => Data[0xA9]; set => Data[0xA9] = value; }
public byte CNT_Beauty { get => Data[0xAA]; set => Data[0xAA] = value; }
public byte CNT_Cute { get => Data[0xAB]; set => Data[0xAB] = value; }
public byte CNT_Smart { get => Data[0xAC]; set => Data[0xAC] = value; }
public byte CNT_Tough { get => Data[0xAD]; set => Data[0xAD] = value; }
public byte CNT_Sheen { get => Data[0xAE]; set => Data[0xAE] = value; }
2015-11-22 21:38:16 +00:00
public int IV_HP { get => Data[0xAF]; set => Data[0xAF] = (byte)value; }
public int IV_ATK { get => Data[0xB0]; set => Data[0xB0] = (byte)value; }
public int IV_DEF { get => Data[0xB1]; set => Data[0xB1] = (byte)value; }
public int IV_SPE { get => Data[0xB2]; set => Data[0xB2] = (byte)value; }
public int IV_SPA { get => Data[0xB3]; set => Data[0xB3] = (byte)value; }
public int IV_SPD { get => Data[0xB4]; set => Data[0xB4] = (byte)value; }
public int OTGender { get => Data[0xB5]; set => Data[0xB5] = (byte)value; }
public override string OT_Name
{
get => StringConverter6.GetString(Data.AsSpan(0xB6, 0x1A));
set => StringConverter6.SetString(Data.AsSpan(0xB6, 0x1A), value.AsSpan(), 12, StringConverterOption.ClearZero);
}
public override byte Level { get => Data[0xD0]; set => Data[0xD0] = value; }
public override bool IsEgg { get => Data[0xD1] == 1; set => Data[0xD1] = value ? (byte)1 : (byte)0; }
public uint PID { get => ReadUInt32LittleEndian(Data.AsSpan(0xD4)); set => WriteUInt32LittleEndian(Data.AsSpan(0xD4), value); }
public int RelearnMove1 { get => ReadUInt16LittleEndian(Data.AsSpan(0xD8)); set => WriteUInt16LittleEndian(Data.AsSpan(0xD8), (ushort)value); }
public int RelearnMove2 { get => ReadUInt16LittleEndian(Data.AsSpan(0xDA)); set => WriteUInt16LittleEndian(Data.AsSpan(0xDA), (ushort)value); }
public int RelearnMove3 { get => ReadUInt16LittleEndian(Data.AsSpan(0xDC)); set => WriteUInt16LittleEndian(Data.AsSpan(0xDC), (ushort)value); }
public int RelearnMove4 { get => ReadUInt16LittleEndian(Data.AsSpan(0xDE)); set => WriteUInt16LittleEndian(Data.AsSpan(0xDE), (ushort)value); }
public byte OT_Intensity { get => Data[0xE0]; set => Data[0xE0] = value; }
public byte OT_Memory { get => Data[0xE1]; set => Data[0xE1] = value; }
public ushort OT_TextVar { get => ReadUInt16LittleEndian(Data.AsSpan(0xE2)); set => WriteUInt16LittleEndian(Data.AsSpan(0xE2), value); }
public byte OT_Feeling { get => Data[0xE4]; set => Data[0xE4] = value; }
2015-11-22 21:38:16 +00:00
public int EV_HP { get => Data[0xE5]; set => Data[0xE5] = (byte)value; }
public int EV_ATK { get => Data[0xE6]; set => Data[0xE6] = (byte)value; }
public int EV_DEF { get => Data[0xE7]; set => Data[0xE7] = (byte)value; }
public int EV_SPE { get => Data[0xE8]; set => Data[0xE8] = (byte)value; }
public int EV_SPA { get => Data[0xE9]; set => Data[0xE9] = (byte)value; }
public int EV_SPD { get => Data[0xEA]; set => Data[0xEA] = (byte)value; }
2017-01-06 02:33:11 +00:00
private byte RIB0 { get => Data[0x74]; set => Data[0x74] = value; }
private byte RIB1 { get => Data[0x75]; set => Data[0x75] = value; }
public bool RibbonChampionBattle { get => (RIB0 & (1 << 0)) == 1 << 0; set => RIB0 = (byte)((RIB0 & ~(1 << 0)) | (value ? 1 << 0 : 0)); }
public bool RibbonChampionRegional { get => (RIB0 & (1 << 1)) == 1 << 1; set => RIB0 = (byte)((RIB0 & ~(1 << 1)) | (value ? 1 << 1 : 0)); }
public bool RibbonChampionNational { get => (RIB0 & (1 << 2)) == 1 << 2; set => RIB0 = (byte)((RIB0 & ~(1 << 2)) | (value ? 1 << 2 : 0)); }
public bool RibbonCountry { get => (RIB0 & (1 << 3)) == 1 << 3; set => RIB0 = (byte)((RIB0 & ~(1 << 3)) | (value ? 1 << 3 : 0)); }
public bool RibbonNational { get => (RIB0 & (1 << 4)) == 1 << 4; set => RIB0 = (byte)((RIB0 & ~(1 << 4)) | (value ? 1 << 4 : 0)); }
public bool RibbonEarth { get => (RIB0 & (1 << 5)) == 1 << 5; set => RIB0 = (byte)((RIB0 & ~(1 << 5)) | (value ? 1 << 5 : 0)); }
public bool RibbonWorld { get => (RIB0 & (1 << 6)) == 1 << 6; set => RIB0 = (byte)((RIB0 & ~(1 << 6)) | (value ? 1 << 6 : 0)); }
public bool RibbonEvent { get => (RIB0 & (1 << 7)) == 1 << 7; set => RIB0 = (byte)((RIB0 & ~(1 << 7)) | (value ? 1 << 7 : 0)); }
public bool RibbonChampionWorld { get => (RIB1 & (1 << 0)) == 1 << 0; set => RIB1 = (byte)((RIB1 & ~(1 << 0)) | (value ? 1 << 0 : 0)); }
public bool RibbonBirthday { get => (RIB1 & (1 << 1)) == 1 << 1; set => RIB1 = (byte)((RIB1 & ~(1 << 1)) | (value ? 1 << 1 : 0)); }
public bool RibbonSpecial { get => (RIB1 & (1 << 2)) == 1 << 2; set => RIB1 = (byte)((RIB1 & ~(1 << 2)) | (value ? 1 << 2 : 0)); }
public bool RibbonSouvenir { get => (RIB1 & (1 << 3)) == 1 << 3; set => RIB1 = (byte)((RIB1 & ~(1 << 3)) | (value ? 1 << 3 : 0)); }
public bool RibbonWishing { get => (RIB1 & (1 << 4)) == 1 << 4; set => RIB1 = (byte)((RIB1 & ~(1 << 4)) | (value ? 1 << 4 : 0)); }
public bool RibbonClassic { get => (RIB1 & (1 << 5)) == 1 << 5; set => RIB1 = (byte)((RIB1 & ~(1 << 5)) | (value ? 1 << 5 : 0)); }
public bool RibbonPremier { get => (RIB1 & (1 << 6)) == 1 << 6; set => RIB1 = (byte)((RIB1 & ~(1 << 6)) | (value ? 1 << 6 : 0)); }
public bool RIB1_7 { get => (RIB1 & (1 << 7)) == 1 << 7; set => RIB1 = (byte)((RIB1 & ~(1 << 7)) | (value ? 1 << 7 : 0)); }
2015-11-22 21:38:16 +00:00
// Meta Accessible Properties
public override int[] IVs
2017-01-06 02:33:11 +00:00
{
get => new[] { IV_HP, IV_ATK, IV_DEF, IV_SPE, IV_SPA, IV_SPD };
2017-01-06 02:33:11 +00:00
set
{
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
if (value.Length != 6) return;
2017-01-06 02:33:11 +00:00
IV_HP = value[0]; IV_ATK = value[1]; IV_DEF = value[2];
IV_SPE = value[3]; IV_SPA = value[4]; IV_SPD = value[5];
}
}
2017-01-06 02:33:11 +00:00
public int[] EVs
{
get => new[] { EV_HP, EV_ATK, EV_DEF, EV_SPE, EV_SPA, EV_SPD };
2017-01-06 02:33:11 +00:00
set
{
PKHeX.Core Nullable cleanup (#2401) * Handle some nullable cases Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data) Make some classes have explicit constructors instead of { } initialization * Handle bits more obviously without null * Make SaveFile.BAK explicitly readonly again * merge constructor methods to have readonly fields * Inline some properties * More nullable handling * Rearrange box actions define straightforward classes to not have any null properties * Make extrabyte reference array immutable * Move tooltip creation to designer * Rearrange some logic to reduce nesting * Cache generated fonts * Split mystery gift album purpose * Handle more tooltips * Disallow null setters * Don't capture RNG object, only type enum * Unify learnset objects Now have readonly properties which are never null don't new() empty learnsets (>800 Learnset objects no longer created, total of 2400 objects since we also new() a move & level array) optimize g1/2 reader for early abort case * Access rewrite Initialize blocks in a separate object, and get via that object removes a couple hundred "might be null" warnings since blocks are now readonly getters some block references have been relocated, but interfaces should expose all that's needed put HoF6 controls in a groupbox, and disable * Readonly personal data * IVs non nullable for mystery gift * Explicitly initialize forced encounter moves * Make shadow objects readonly & non-null Put murkrow fix in binary data resource, instead of on startup * Assign dex form fetch on constructor Fixes legality parsing edge cases also handle cxd parse for valid; exit before exception is thrown in FrameGenerator * Remove unnecessary null checks * Keep empty value until init SetPouch sets the value to an actual one during load, but whatever * Readonly team lock data * Readonly locks Put locked encounters at bottom (favor unlocked) * Mail readonly data / offset Rearrange some call flow and pass defaults Add fake classes for SaveDataEditor mocking Always party size, no need to check twice in stat editor use a fake save file as initial data for savedata editor, and for gamedata (wow i found a usage) constrain eventwork editor to struct variable types (uint, int, etc), thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
if (value.Length != 6) return;
2017-01-06 02:33:11 +00:00
EV_HP = value[0]; EV_ATK = value[1]; EV_DEF = value[2];
EV_SPE = value[3]; EV_SPA = value[4]; EV_SPD = value[5];
}
}
public bool IsNicknamed => Nickname.Length > 0;
public override int Location { get => MetLocation; set => MetLocation = (ushort)value; }
public override IReadOnlyList<int> Moves
2015-11-22 21:38:16 +00:00
{
get => new[] { Move1, Move2, Move3, Move4 };
set
{
if (value.Count > 0) Move1 = value[0];
if (value.Count > 1) Move2 = value[1];
if (value.Count > 2) Move3 = value[2];
if (value.Count > 3) Move4 = value[3];
}
2015-11-22 21:38:16 +00:00
}
public override IReadOnlyList<int> Relearn
2015-11-22 21:38:16 +00:00
{
get => new[] { RelearnMove1, RelearnMove2, RelearnMove3, RelearnMove4 };
2015-11-22 21:38:16 +00:00
set
{
if (value.Count > 0) RelearnMove1 = value[0];
if (value.Count > 1) RelearnMove2 = value[1];
if (value.Count > 2) RelearnMove3 = value[2];
if (value.Count > 3) RelearnMove4 = value[3];
2015-11-22 21:38:16 +00:00
}
}
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
public bool IsLinkGift => MetLocation == Locations.LinkGift6;
public override PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria)
{
if (!IsEntity)
throw new ArgumentException(nameof(IsEntity));
var rnd = Util.Rand;
int currentLevel = Level > 0 ? Level : rnd.Next(1, 101);
var pi = PersonalTable.AO.GetFormEntry(Species, Form);
2020-12-22 01:17:56 +00:00
PK6 pk = new()
{
Species = Species,
HeldItem = HeldItem,
TID = TID,
SID = SID,
Met_Level = currentLevel,
Form = Form,
EncryptionConstant = EncryptionConstant != 0 ? EncryptionConstant : Util.Rand32(),
Version = OriginGame != 0 ? OriginGame : sav.Game,
Language = Language != 0 ? Language : sav.Language,
Ball = Ball,
Move1 = Move1, Move2 = Move2, Move3 = Move3, Move4 = Move4,
RelearnMove1 = RelearnMove1, RelearnMove2 = RelearnMove2,
RelearnMove3 = RelearnMove3, RelearnMove4 = RelearnMove4,
Met_Location = MetLocation,
Egg_Location = EggLocation,
CNT_Cool = CNT_Cool,
CNT_Beauty = CNT_Beauty,
CNT_Cute = CNT_Cute,
CNT_Smart = CNT_Smart,
CNT_Tough = CNT_Tough,
CNT_Sheen = CNT_Sheen,
OT_Name = OT_Name.Length > 0 ? OT_Name : sav.OT,
OT_Gender = OTGender != 3 ? OTGender % 2 : sav.Gender,
HT_Name = OT_Name.Length > 0 ? sav.OT : string.Empty,
HT_Gender = OT_Name.Length > 0 ? sav.Gender : 0,
CurrentHandler = OT_Name.Length > 0 ? 1 : 0,
2018-05-12 15:13:39 +00:00
2019-11-16 01:34:18 +00:00
EXP = Experience.GetEXP(Level, pi.EXPGrowth),
// Ribbons
RibbonCountry = RibbonCountry,
RibbonNational = RibbonNational,
RibbonEarth = RibbonEarth,
RibbonWorld = RibbonWorld,
RibbonClassic = RibbonClassic,
RibbonPremier = RibbonPremier,
RibbonEvent = RibbonEvent,
RibbonBirthday = RibbonBirthday,
RibbonSpecial = RibbonSpecial,
RibbonSouvenir = RibbonSouvenir,
RibbonWishing = RibbonWishing,
RibbonChampionBattle = RibbonChampionBattle,
RibbonChampionRegional = RibbonChampionRegional,
RibbonChampionNational = RibbonChampionNational,
RibbonChampionWorld = RibbonChampionWorld,
2018-05-12 15:13:39 +00:00
OT_Friendship = pi.BaseFriendship,
2016-05-08 21:29:08 +00:00
OT_Intensity = OT_Intensity,
OT_Memory = OT_Memory,
OT_TextVar = OT_TextVar,
OT_Feeling = OT_Feeling,
Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately (#3222) * Track a PKM's Box,Slot,StorageFlags,Identifier metadata separately Don't store within the object, track the slot origin data separately. Batch editing now pre-filters if using Box/Slot/Identifier logic; split up mods/filters as they're starting to get pretty hefty. - Requesting a Box Data report now shows all slots in the save file (party, misc) - Can now exclude backup saves from database search via toggle (separate from settings preventing load entirely) - Replace some linq usages with direct code * Remove WasLink virtual in PKM Inline any logic, since we now have encounter objects to indicate matching, rather than the proto-legality logic checking properties of a PKM. * Use Fateful to directly check gen5 mysterygift origins No other encounter types in gen5 apply Fateful * Simplify double ball comparison Used to be separate for deferral cases, now no longer needed to be separate. * Grab move/relearn reference and update locally Fix relearn move identifier * Inline defog HM transfer preference check HasMove is faster than getting moves & checking contains. Skips allocation by setting values directly. * Extract more met location metadata checks: WasBredEgg * Replace Console.Write* with Debug.Write* There's no console output UI, so don't include them in release builds. * Inline WasGiftEgg, WasEvent, and WasEventEgg logic Adios legality tags that aren't entirely correct for the specific format. Just put the computations in EncounterFinder.
2021-06-23 03:23:48 +00:00
FatefulEncounter = !IsLinkGift, // Link gifts do not set fateful encounter
2017-01-06 02:33:11 +00:00
2022-03-14 05:33:17 +00:00
EV_HP = EV_HP,
EV_ATK = EV_ATK,
EV_DEF = EV_DEF,
EV_SPE = EV_SPE,
EV_SPA = EV_SPA,
EV_SPD = EV_SPD,
};
if (sav is IRegionOrigin o)
{
pk.Country = o.Country;
pk.Region = o.Region;
pk.ConsoleRegion = o.ConsoleRegion;
}
else
{
pk.SetDefaultRegionOrigins();
}
pk.SetMaximumPPCurrent();
2016-11-17 17:12:17 +00:00
pk.MetDate = Date ?? DateTime.Now;
if ((sav.Generation > Generation && OriginGame == 0) || !CanBeReceivedByVersion(pk.Version))
{
// give random valid game
do { pk.Version = (int)GameVersion.X + rnd.Next(4); }
while (!CanBeReceivedByVersion(pk.Version));
}
2017-10-06 06:13:48 +00:00
if (!IsEgg)
2015-11-27 03:07:20 +00:00
{
if (pk.CurrentHandler == 0) // OT
{
pk.OT_Memory = 3;
pk.OT_TextVar = 9;
pk.OT_Intensity = 1;
pk.OT_Feeling = MemoryContext6.GetRandomFeeling6(pk.OT_Memory, 10); // 0-9
}
else
{
pk.HT_Memory = 3;
pk.HT_TextVar = 9;
pk.HT_Intensity = 1;
pk.HT_Feeling = MemoryContext6.GetRandomFeeling6(pk.HT_Memory, 10); // 0-9
pk.HT_Friendship = pk.OT_Friendship;
}
2015-11-27 03:07:20 +00:00
}
pk.IsNicknamed = IsNicknamed;
pk.Nickname = IsNicknamed ? Nickname : SpeciesName.GetSpeciesNameGeneration(Species, pk.Language, Generation);
SetPINGA(pk, criteria);
if (IsEgg)
SetEggMetData(pk);
pk.CurrentFriendship = pk.IsEgg ? pi.HatchCycles : pi.BaseFriendship;
pk.RefreshChecksum();
return pk;
}
private void SetEggMetData(PKM pk)
{
pk.IsEgg = true;
pk.EggMetDate = Date;
pk.Nickname = SpeciesName.GetSpeciesNameGeneration(0, pk.Language, Generation);
pk.IsNicknamed = true;
}
private void SetPINGA(PKM pk, EncounterCriteria criteria)
{
var pi = PersonalTable.AO.GetFormEntry(Species, Form);
pk.Nature = (int)criteria.GetNature((Nature)Nature);
pk.Gender = criteria.GetGender(Gender, pi);
2021-04-09 21:52:49 +00:00
var av = GetAbilityIndex(criteria);
pk.RefreshAbility(av);
SetPID(pk);
SetIVs(pk);
}
2021-04-09 21:52:49 +00:00
private int GetAbilityIndex(EncounterCriteria criteria) => AbilityType switch
{
00 or 01 or 02 => AbilityType, // Fixed 0/1/2
03 or 04 => criteria.GetAbilityFromNumber(Ability), // 0/1 or 0/1/H
_ => throw new ArgumentOutOfRangeException(nameof(AbilityType)),
};
public override AbilityPermission Ability => AbilityType switch
{
0 => AbilityPermission.OnlyFirst,
1 => AbilityPermission.OnlySecond,
2 => AbilityPermission.OnlyHidden,
3 => AbilityPermission.Any12,
_ => AbilityPermission.Any12H,
};
private void SetPID(PKM pk)
{
switch (PIDType)
{
case ShinyType6.FixedValue: // Specified
pk.PID = PID;
break;
case ShinyType6.Random: // Random
pk.PID = Util.Rand32();
break;
case ShinyType6.Always: // Random Shiny
pk.PID = Util.Rand32();
pk.PID = (uint)(((pk.TID ^ pk.SID ^ (pk.PID & 0xFFFF)) << 16) | (pk.PID & 0xFFFF));
break;
case ShinyType6.Never: // Random Nonshiny
pk.PID = Util.Rand32();
if (pk.IsShiny) pk.PID ^= 0x10000000;
break;
}
}
2022-03-14 02:24:08 +00:00
public override void GetIVs(Span<int> value)
{
if (value.Length != 6)
return;
value[0] = IV_HP;
value[1] = IV_ATK;
value[2] = IV_DEF;
value[3] = IV_SPE;
value[4] = IV_SPA;
value[5] = IV_SPD;
}
private void SetIVs(PKM pk)
{
Span<int> finalIVs = stackalloc int[6];
2022-03-14 02:24:08 +00:00
GetIVs(finalIVs);
var ivflag = finalIVs.Find(iv => (byte)(iv - 0xFC) < 3);
var rng = Util.Rand;
if (ivflag == 0) // Random IVs
{
2022-03-14 02:24:08 +00:00
for (int i = 0; i < finalIVs.Length; i++)
{
if (finalIVs[i] > 31)
finalIVs[i] = rng.Next(32);
}
}
else // 1/2/3 perfect IVs
{
int IVCount = ivflag - 0xFB;
2022-03-14 02:24:08 +00:00
do { finalIVs[rng.Next(6)] = 31; }
while (finalIVs.Count(31) < IVCount);
2022-03-14 02:24:08 +00:00
for (int i = 0; i < finalIVs.Length; i++)
{
if (finalIVs[i] != 31)
finalIVs[i] = rng.Next(32);
}
}
pk.SetIVs(finalIVs);
}
public override bool IsMatchExact(PKM pkm, EvoCriteria evo)
{
if (!IsEgg)
{
if (OTGender != 3)
{
// Skip ID check if ORASDEMO Simulated wc6
if (CardID != 0)
{
if (SID != pkm.SID) return false;
if (TID != pkm.TID) return false;
}
if (OTGender != pkm.OT_Gender) return false;
}
if (!string.IsNullOrEmpty(OT_Name) && OT_Name != pkm.OT_Name) return false;
if (PIDType == ShinyType6.FixedValue && pkm.PID != PID) return false;
if (!Shiny.IsValid(pkm)) return false;
if (OriginGame != 0 && OriginGame != pkm.Version) return false;
if (EncryptionConstant != 0 && EncryptionConstant != pkm.EncryptionConstant) return false;
if (Language != 0 && Language != pkm.Language) return false;
}
if (Form != evo.Form && !FormInfo.IsFormChangeable(Species, Form, pkm.Form, pkm.Format))
return false;
if (IsEgg)
{
if (EggLocation != pkm.Egg_Location) // traded
{
if (pkm.Egg_Location != Locations.LinkTrade6)
return false;
}
else if (PIDType == 0 && pkm.IsShiny)
{
return false; // can't be traded away for unshiny
}
if (pkm.IsEgg && !pkm.IsNative)
return false;
}
else
{
if (!IsMatchEggLocation(pkm)) return false;
if (MetLocation != pkm.Met_Location) return false;
}
if (Level != pkm.Met_Level) return false;
if (Ball != pkm.Ball) return false;
if (OTGender < 3 && OTGender != pkm.OT_Gender) return false;
2019-11-16 01:34:18 +00:00
if (Nature != -1 && pkm.Nature != Nature) return false;
if (Gender != 3 && Gender != pkm.Gender) return false;
if (pkm is IContestStats s && s.IsContestBelow(this))
return false;
return true;
}
protected override bool IsMatchDeferred(PKM pkm)
{
switch (CardID)
{
case 0525 when IV_HP == 0xFE: // Diancie was distributed with no IV enforcement & 3IVs
case 0504 when RibbonClassic != ((IRibbonSetEvent4)pkm).RibbonClassic: // Magmar with/without classic
return true;
}
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
return Species != pkm.Species;
}
protected override bool IsMatchPartial(PKM pkm)
{
if (RestrictLanguage != 0 && RestrictLanguage != pkm.Language)
return true;
if (!CanBeReceivedByVersion(pkm.Version))
return true;
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
return false;
}
2015-11-22 21:38:16 +00:00
}
}