PKHeX/Misc/PGT.cs

143 lines
4.9 KiB
C#
Raw Normal View History

2015-12-26 21:00:00 +00:00
using System;
namespace PKHeX
{
public class PCD
{
2016-02-10 08:33:42 +00:00
internal const int Size = 0x358; // 856
2015-12-26 21:00:00 +00:00
public byte[] Data;
public PCD(byte[] data = null)
{
Data = data ?? new byte[Size];
byte[] giftData = new byte[PGT.Size];
Array.Copy(Data, 0, giftData, 0, PGT.Size);
Gift = new PGT(giftData);
Information = new byte[Data.Length - PGT.Size];
Array.Copy(Data, PGT.Size, Information, 0, Information.Length);
}
public readonly PGT Gift;
2015-12-27 00:05:09 +00:00
public readonly byte[] Information;
2015-12-27 01:09:13 +00:00
/* Big thanks to Grovyle91's Pokémon Mystery Gift Editor, from which the structure was referenced.
2015-12-27 00:05:09 +00:00
* http://projectpokemon.org/forums/member.php?829-Grovyle91
* http://projectpokemon.org/forums/showthread.php?6524
2015-12-27 01:09:13 +00:00
* See also: http://tccphreak.shiny-clique.net/debugger/pcdfiles.htm
2015-12-27 00:05:09 +00:00
*/
2015-12-26 21:00:00 +00:00
}
public class PGT
{
2015-12-27 00:05:09 +00:00
internal static int Size = 0x104; // 260
2015-12-26 21:00:00 +00:00
public byte[] Data;
public PGT(byte[] data = null)
{
Data = data ?? new byte[Size];
2015-12-27 00:05:09 +00:00
byte[] ekdata = new byte[PK4.SIZE_PARTY];
Array.Copy(Data, 8, ekdata, 0, ekdata.Length);
2015-12-26 21:00:00 +00:00
// Decrypt PK4
PK = new PK4(PKM.decryptArray(ekdata, BitConverter.ToUInt16(ekdata, 6)));
2015-12-27 00:05:09 +00:00
Unknown = new byte[0x10];
Array.Copy(Data, 0xF4, Unknown, 0, 0x10);
2015-12-26 21:00:00 +00:00
}
public byte CardType { get { return Data[0]; } set { Data[0] = value; } }
// Unused 0x01
public byte Slot { get { return Data[2]; } set { Data[2] = value; } }
public byte Detail { get { return Data[3]; } set { Data[3] = value; } }
public PK4 PK;
2015-12-26 21:00:00 +00:00
public byte[] Unknown;
public bool IsPokémon { get { return CardType == 1; } set { if (value) CardType = 1; } }
2015-12-27 00:05:09 +00:00
public bool IsEgg { get { return CardType == 2; } set { if (value) CardType = 2; } }
public bool IsManaphyEgg { get { return CardType == 7; } set { if (value) CardType = 7; } }
public bool PokémonGift => IsPokémon || IsEgg || IsManaphyEgg;
2015-12-26 21:00:00 +00:00
public PK4 convertToPK4(SAV6 SAV)
{
2015-12-27 01:09:13 +00:00
if (!PokémonGift)
2015-12-26 21:00:00 +00:00
return null;
PK4 pk4 = new PK4(PK.Data);
2015-12-27 01:09:13 +00:00
if (!IsPokémon && Detail == 0)
2015-12-27 00:05:09 +00:00
{
pk4.OT_Name = "PKHeX";
pk4.TID = 12345;
pk4.SID = 54321;
pk4.OT_Gender = (int)(Util.rnd32()%2);
2015-12-27 00:05:09 +00:00
}
2015-12-27 01:09:13 +00:00
if (IsManaphyEgg)
{
// Since none of this data is populated, fill in default info.
pk4.Species = 490;
2015-12-27 01:09:13 +00:00
// Level 1 Moves
pk4.Move1 = 294;
pk4.Move2 = 145;
pk4.Move3 = 346;
pk4.FatefulEncounter = true;
pk4.Ball = 4;
pk4.Version = 10; // Diamond
pk4.Language = 2; // English
pk4.Nickname = "MANAPHY";
pk4.Egg_Location = 1; // Ranger (will be +3000 later)
2015-12-27 01:09:13 +00:00
}
2015-12-27 00:05:09 +00:00
// Generate IV
uint seed = Util.rnd32();
if (pk4.PID == 1) // Create Nonshiny
2015-12-27 00:05:09 +00:00
{
uint pid1 = PKM.LCRNG(ref seed) >> 16;
uint pid2 = PKM.LCRNG(ref seed) >> 16;
2015-12-27 00:05:09 +00:00
while ((pid1 ^ pid2 ^ pk4.TID ^ pk4.SID) < 8)
2015-12-27 00:05:09 +00:00
{
uint testPID = pid1 | pid2 << 16;
2015-12-27 00:05:09 +00:00
// Call the ARNG to change the PID
testPID = testPID * 0x6c078965 + 1;
pid1 = testPID & 0xFFFF;
pid2 = testPID >> 16;
}
pk4.PID = pid1 | (pid2 << 16);
2015-12-27 00:05:09 +00:00
}
// Generate IVs
if (pk4.IV32 == 0)
2015-12-27 00:05:09 +00:00
{
uint iv1 = PKM.LCRNG(ref seed) >> 16;
uint iv2 = PKM.LCRNG(ref seed) >> 16;
pk4.IV32 = (iv1 | iv2 << 16) & 0x3FFFFFFF;
2015-12-27 00:05:09 +00:00
}
// Generate Met Info
DateTime dt = DateTime.Now;
if (IsPokémon)
{
pk4.Met_Location = pk4.Egg_Location + 3000;
pk4.Egg_Location = 0;
pk4.Met_Day = dt.Day;
pk4.Met_Month = dt.Month;
pk4.Met_Year = dt.Year - 2000;
pk4.IsEgg = false;
2015-12-27 00:05:09 +00:00
}
else
{
pk4.Egg_Location = pk4.Egg_Location + 3000;
pk4.Egg_Day = dt.Day;
pk4.Egg_Month = dt.Month;
pk4.Egg_Year = dt.Year - 2000;
pk4.IsEgg = false;
// Met Location is modified when transferred to pk5; don't worry about it.
2015-12-27 00:05:09 +00:00
}
if (pk4.Species == 201) // Never will be true; Unown was never distributed.
pk4.AltForm = PKM.getUnownForm(pk4.PID);
2015-12-27 00:05:09 +00:00
return pk4;
2015-12-26 21:00:00 +00:00
}
}
}