private byte[] preparepkx(bool click = true)
{
if (click)
tabMain.Select(); // hack to make sure comboboxes are set (users scrolling through and immediately setting causes this)
// Stuff the global byte array with our PKX form data
// Create a new storage so we don't muck up things with the original
if (buff.Length == 232) Array.Resize(ref buff, 260);
byte[] pkx = new byte[0x104];
Array.Copy(buff, pkx, 0x104);
// Repopulate PKX with Edited Stuff
if (Util.getIndex(CB_GameOrigin) < 24)
{
uint EC = Util.getHEXval(TB_EC);
uint PID = Util.getHEXval(TB_PID);
uint SID = Util.ToUInt32(TB_TID.Text);
uint TID = Util.ToUInt32(TB_TID.Text);
uint LID = PID & 0xFFFF;
uint HID = PID >> 16;
uint XOR = (TID ^ LID ^ SID ^ HID);
// Ensure we don't have a shiny.
if (XOR >> 3 == 1) // Illegal, fix. (not 16<XOR>=8)
{
// Keep as shiny, so we have to mod the PID
PID ^= XOR;
TB_PID.Text = PID.ToString("X8");
TB_EC.Text = PID.ToString("X8");
}
else if ((XOR ^ 0x8000) >> 3 == 1 && PID != EC)
TB_EC.Text = (PID ^ 0x80000000).ToString("X8");
else // Not Illegal, no fix.
TB_EC.Text = PID.ToString("X8");
}
Array.Copy(BitConverter.GetBytes(Util.getHEXval(TB_EC)), 0, pkx, 0, 4); // EC
Array.Copy(BitConverter.GetBytes(0), 0, pkx, 0x4, 4); // 0 CHK for now
// Block A
int species = Util.getIndex(CB_Species);
Array.Copy(BitConverter.GetBytes(species), 0, pkx, 0x08, 2); // Species
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_HeldItem)), 0, pkx, 0x0A, 2); // Held Item
Array.Copy(BitConverter.GetBytes(Util.ToUInt32(TB_TID.Text)), 0, pkx, 0x0C, 2); // TID
Array.Copy(BitConverter.GetBytes(Util.ToUInt32(TB_SID.Text)), 0, pkx, 0x0E, 2); // SID
Array.Copy(BitConverter.GetBytes(Convert.ToUInt32(TB_EXP.Text)), 0, pkx, 0x10, 4); // EXP
pkx[0x14] = (byte)Array.IndexOf(abilitylist, (CB_Ability.Text).Remove((CB_Ability.Text).Length - 4)); // Ability
pkx[0x15] = (byte)(Util.ToInt32((TB_AbilityNumber.Text))); // Number
// pkx[0x16], pkx[0x17] are handled by the Medals UI (Hits & Training Bag)
Array.Copy(BitConverter.GetBytes(Util.getHEXval(TB_PID)), 0, pkx, 0x18, 4); // PID
pkx[0x1C] = (byte)((Util.getIndex(CB_Nature))); // Nature
int fegform = Convert.ToInt32(CHK_Fateful.Checked); // Fateful
fegform |= (PKX.getGender(Label_Gender.Text) << 1); // Gender
fegform |= (Math.Min((MT_Form.Enabled) ? Convert.ToInt32(MT_Form.Text) : CB_Form.SelectedIndex, 32) << 3); // Form
pkx[0x1D] = (byte)fegform;
pkx[0x1E] = (byte)Util.ToInt32(TB_HPEV.Text); // EVs
pkx[0x1F] = (byte)Util.ToInt32(TB_ATKEV.Text);
pkx[0x20] = (byte)Util.ToInt32(TB_DEFEV.Text);
pkx[0x21] = (byte)Util.ToInt32(TB_SPEEV.Text);
pkx[0x22] = (byte)Util.ToInt32(TB_SPAEV.Text);
pkx[0x23] = (byte)Util.ToInt32(TB_SPDEV.Text);
pkx[0x24] = (byte)Util.ToInt32(TB_Cool.Text); // CNT
pkx[0x25] = (byte)Util.ToInt32(TB_Beauty.Text);
pkx[0x26] = (byte)Util.ToInt32(TB_Cute.Text);
pkx[0x27] = (byte)Util.ToInt32(TB_Smart.Text);
pkx[0x28] = (byte)Util.ToInt32(TB_Tough.Text);
pkx[0x29] = (byte)Util.ToInt32(TB_Sheen.Text);
int markings = CHK_Circle.Checked ? (1 << 0) : 0;
markings |= CHK_Triangle.Checked ? (1 << 1) : 0;
markings |= CHK_Square.Checked ? (1 << 2) : 0;
markings |= CHK_Heart.Checked ? (1 << 3) : 0;
markings |= CHK_Star.Checked ? (1 << 4) : 0;
markings |= CHK_Diamond.Checked ? (1 << 5) : 0;
pkx[0x2A] = (byte)markings;
pkx[0x2B] = (byte)(CB_PKRSStrain.SelectedIndex << 4 | CB_PKRSDays.SelectedIndex);
// Already in buff (then transferred to new pkx)
// 0x2C, 0x2D, 0x2E, 0x2F
// 0x30, 0x31, 0x32, 0x33
// 0x34, 0x35, 0x36, 0x37
// 0x38, 0x39
// Unused
// 0x3A, 0x3B
// 0x3C, 0x3D, 0x3E, 0x3F
// Block B
// Convert Nickname field back to bytes
string nicknamestr = TB_Nickname.Text;
{
nicknamestr = nicknamestr.Replace("\u2640", "\uE08F");
nicknamestr = nicknamestr.Replace("\u2642", "\uE08E");
nicknamestr = nicknamestr.Replace("\u0027", "\u2019"); // ' to ’
}
byte[] nicknamebytes = Encoding.Unicode.GetBytes(nicknamestr);
Array.Resize(ref nicknamebytes, nicknamebytes.Length + 2); // pad with zero byte terminator
Array.Copy(nicknamebytes, 0, pkx, 0x40, nicknamebytes.Length);
// 0x58, 0x59 unused
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_Move1)), 0, pkx, 0x5A, 2); // Move 1
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_Move2)), 0, pkx, 0x5C, 2); // Move 2
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_Move3)), 0, pkx, 0x5E, 2); // Move 3
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_Move4)), 0, pkx, 0x60, 2); // Move 4
pkx[0x62] = (byte)(Util.getIndex(CB_Move1) > 0 ? Util.ToInt32(TB_PP1.Text) : 0); // Max PP
pkx[0x63] = (byte)(Util.getIndex(CB_Move2) > 0 ? Util.ToInt32(TB_PP2.Text) : 0);
pkx[0x64] = (byte)(Util.getIndex(CB_Move3) > 0 ? Util.ToInt32(TB_PP3.Text) : 0);
pkx[0x65] = (byte)(Util.getIndex(CB_Move4) > 0 ? Util.ToInt32(TB_PP4.Text) : 0);
pkx[0x66] = (byte)(Util.getIndex(CB_Move1) > 0 ? CB_PPu1.SelectedIndex : 0); // PP Ups
pkx[0x67] = (byte)(Util.getIndex(CB_Move2) > 0 ? CB_PPu2.SelectedIndex : 0);
pkx[0x68] = (byte)(Util.getIndex(CB_Move3) > 0 ? CB_PPu3.SelectedIndex : 0);
pkx[0x69] = (byte)(Util.getIndex(CB_Move4) > 0 ? CB_PPu4.SelectedIndex : 0);
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_RelearnMove1)), 0, pkx, 0x6A, 2); // EggMove 1
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_RelearnMove2)), 0, pkx, 0x6C, 2); // EggMove 2
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_RelearnMove3)), 0, pkx, 0x6E, 2); // EggMove 3
Array.Copy(BitConverter.GetBytes(Util.getIndex(CB_RelearnMove4)), 0, pkx, 0x70, 2); // EggMove 4
// 0x72 - Ribbon editor sets this flag (Secret Super Training)
// 0x73
uint IV32 = Util.ToUInt32(TB_HPIV.Text) & 0x1F;
IV32 |= ((Util.ToUInt32(TB_ATKIV.Text) & 0x1F) << 5);
IV32 |= ((Util.ToUInt32(TB_DEFIV.Text) & 0x1F) << 10);
IV32 |= ((Util.ToUInt32(TB_SPEIV.Text) & 0x1F) << 15);
IV32 |= ((Util.ToUInt32(TB_SPAIV.Text) & 0x1F) << 20);
IV32 |= ((Util.ToUInt32(TB_SPDIV.Text) & 0x1F) << 25);
IV32 |= (Convert.ToUInt32(CHK_IsEgg.Checked) << 30);
IV32 |= (Convert.ToUInt32(CHK_Nicknamed.Checked) << 31);
Array.Copy(BitConverter.GetBytes(IV32), 0, pkx, 0x74, 4); // Copy in IVs
// Block C
// Convert Latest OT field back to bytes
byte[] OT2 = Encoding.Unicode.GetBytes(Util.TrimFromZero(TB_OTt2.Text).Replace("\u0027", "\u2019"));
Array.Resize(ref OT2, OT2.Length + 2); // Allow Trash
Array.Copy(OT2, 0, pkx, 0x78, OT2.Length);
// 0x90-0xAF
pkx[0x92] = Convert.ToByte(PKX.getGender(Label_CTGender.Text) == 1);
// Plus more, set by MemoryAmie (already in buff)
// Block D
// Convert OT field back to bytes
byte[] OT = Encoding.Unicode.GetBytes(TB_OT.Text.Replace("\u0027", "\u2019"));
Array.Resize(ref OT, OT.Length + 2); // Pad with \0000 to terminate
Array.Copy(OT, 0, pkx, 0xB0, OT.Length);
if (pkx[0x93] == 0)
pkx[0xCA] = (byte)(Util.ToInt32(TB_Friendship.Text) & 0xFF);
else // 1
pkx[0xA2] = (byte)(Util.ToInt32(TB_Friendship.Text) & 0xFF);
int egg_year = 2000; // Dates
int egg_month = 0;
int egg_day = 0;
int egg_location = 0;
if (CHK_AsEgg.Checked) // If encountered as an egg, load the Egg Met data from fields.
{
egg_year = CAL_EggDate.Value.Year;
egg_month = CAL_EggDate.Value.Month;
egg_day = CAL_EggDate.Value.Day;
egg_location = Util.getIndex(CB_EggLocation);
}
// Egg Met Data
pkx[0xD1] = (byte)(egg_year - 2000);
pkx[0xD2] = (byte)egg_month;
pkx[0xD3] = (byte)egg_day;
// Met Data
pkx[0xD4] = (byte)(CAL_MetDate.Value.Year - 2000);
pkx[0xD5] = (byte)CAL_MetDate.Value.Month;
pkx[0xD6] = (byte)CAL_MetDate.Value.Day;
if (CHK_IsEgg.Checked && CB_MetLocation.SelectedIndex == 0) // If still an egg, it has no hatch location/date. Zero it!
{
pkx[0xD4] = 0;
pkx[0xD5] = 0;
pkx[0xD6] = 0;
}
// 0xD7 Unknown
int met_location = Util.getIndex(CB_MetLocation); // Locations
Array.Copy(BitConverter.GetBytes(egg_location), 0, pkx, 0xD8, 2); // Egg Location
Array.Copy(BitConverter.GetBytes(met_location), 0, pkx, 0xDA, 2); // Met Location
pkx[0xDC] = (byte)Util.getIndex(CB_Ball);
pkx[0xDD] = (byte)(((Util.ToInt32(TB_MetLevel.Text) & 0x7F) | (Convert.ToInt32(PKX.getGender(Label_OTGender.Text) == 1) << 7)));
pkx[0xDE] = (byte)(Util.ToInt32(CB_EncounterType.SelectedValue.ToString()));
pkx[0xDF] = (byte)Util.getIndex(CB_GameOrigin);
pkx[0xE0] = (byte)Util.getIndex(CB_Country);
pkx[0xE1] = (byte)Util.getIndex(CB_SubRegion);
pkx[0xE2] = (byte)Util.getIndex(CB_3DSReg);
pkx[0xE3] = (byte)Util.getIndex(CB_Language);
// 0xE4-0xE7
Array.Resize(ref pkx, 260);
// Party Stats
pkx[0xE8] = 0; pkx[0xE9] = 0;
pkx[0xEA] = 0; pkx[0xEB] = 0;
pkx[0xEC] = (byte)Util.ToInt32(TB_Level.Text); // Level
pkx[0xED] = 0; pkx[0xEE] = 0; pkx[0xEF] = 0;
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_HP.Text), 65535)), 0, pkx, 0xF0, 2); // Current HP
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_HP.Text), 65535)), 0, pkx, 0xF2, 2); // Max HP
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_ATK.Text), 65535)), 0, pkx, 0xF4, 2); // ATK
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_DEF.Text), 65535)), 0, pkx, 0xF6, 2); // DEF
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_SPE.Text), 65535)), 0, pkx, 0xF8, 2); // SPE
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_SPA.Text), 65535)), 0, pkx, 0xFA, 2); // SPA
Array.Copy(BitConverter.GetBytes(Math.Min(Util.ToInt32(Stat_SPD.Text), 65535)), 0, pkx, 0xFC, 2); // SPD
pkx[0xFE] = 0; pkx[0xFF] = 0;
pkx[0x100] = 0; pkx[0x101] = 0; pkx[0x102] = 0; pkx[0x103] = 0;
// Hax Illegality
if (HaX)
{
pkx[0x14] = (byte)Util.getIndex(DEV_Ability); // Ability
pkx[0xEC] = (byte)Math.Min(Convert.ToInt32(MT_Level.Text), 255); // Level
}
// Fix Moves if a slot is empty
for (int i = 0; i < 3; i++)
{
if (BitConverter.ToUInt16(pkx, 0x5A + 2*i) != 0) continue;
Array.Copy(pkx, 0x5C + 2 * i, pkx, 0x5A + 2 * i, 2); // Shift moves down
Array.Copy(new byte[2], 0, pkx, 0x5C + 2 * i, 2); // Clear next move (error shifted down)
// Move PP and PP Ups down one byte.
pkx[0x62 + i] = pkx[0x63 + i]; pkx[0x63 + i] = 0; // PP
pkx[0x66 + i] = pkx[0x67 + i]; pkx[0x67 + i] = 0; // PP Ups
}
// Fix Relearn moves if a slot is empty
for (int i = 0; i < 3; i++)
{
if (BitConverter.ToUInt16(pkx, 0x6A + 2*i) != 0) continue;
Array.Copy(pkx, 0x6C + 2 * i, pkx, 0x6A + 2 * i, 2); // Shift moves down
Array.Copy(new byte[2], 0, pkx, 0x6C + 2 * i, 2); // Clear next move (error shifted down)
}
// No foreign memories for Pokemon without a foreign trainer
if (BitConverter.ToUInt16(pkx, 0x78) == 0)
{
pkx[0xA2] = pkx[0xA3] = // No Friendship/Affection
pkx[0xA8] = pkx[0xA9] = // No Memory Var
pkx[0xA4] = pkx[0xA5] = pkx[0xA6] = 0; // No Memory Types
}
// Now we fix the checksum!
ushort chk = 0;
for (int i = 8; i < 232; i += 2) // Loop through the entire PKX
chk += BitConverter.ToUInt16(pkx, i);
// Apply New Checksum
Array.Copy(BitConverter.GetBytes(chk), 0, pkx, 6, 2);
// PKX is now filled
return pkx;
}
// Drag & Drop Events