PKHeX/PKHeX.WinForms/Controls/PKM Editor/EditPK6.cs
Kurt ed1b934374 Misc refactoring
expose some useful IEnumerable methods (sorting / bin->pkm / copy) to
simplify some common operations
change some explicit arrays to IList for flexibility
remove old memecrypto bool (no longer necessary as XP is not supported)
rename some methods for more clarity
2017-09-28 22:20:27 -07:00

311 lines
14 KiB
C#

using System;
using PKHeX.Core;
namespace PKHeX.WinForms.Controls
{
public partial class PKMEditor
{
private void PopulateFieldsPK6()
{
PK6 pk6 = pkm as PK6;
if (pk6 == null)
return;
// Do first
pk6.Stat_Level = PKX.GetLevel(pk6.Species, pk6.EXP);
if (pk6.Stat_Level == 100 && !HaX)
pk6.EXP = PKX.GetEXP(pk6.Stat_Level, pk6.Species);
CB_Species.SelectedValue = pk6.Species;
TB_Level.Text = pk6.Stat_Level.ToString();
TB_EXP.Text = pk6.EXP.ToString();
// Load rest
TB_EC.Text = pk6.EncryptionConstant.ToString("X8");
CHK_Fateful.Checked = pk6.FatefulEncounter;
CHK_IsEgg.Checked = pk6.IsEgg;
CHK_Nicknamed.Checked = pk6.IsNicknamed;
Label_OTGender.Text = gendersymbols[pk6.OT_Gender];
Label_OTGender.ForeColor = GetGenderColor(pk6.OT_Gender);
TB_PID.Text = pk6.PID.ToString("X8");
CB_HeldItem.SelectedValue = pk6.HeldItem;
TB_AbilityNumber.Text = pk6.AbilityNumber.ToString();
CB_Ability.SelectedIndex = pk6.AbilityNumber < 6 ? pk6.AbilityNumber >> 1 : 0; // with some simple error handling
CB_Nature.SelectedValue = pk6.Nature;
TB_TID.Text = pk6.TID.ToString("00000");
TB_SID.Text = pk6.SID.ToString("00000");
TB_Nickname.Text = pk6.Nickname;
TB_OT.Text = pk6.OT_Name;
TB_OTt2.Text = pk6.HT_Name;
TB_Friendship.Text = pk6.CurrentFriendship.ToString();
if (pk6.CurrentHandler == 1) // HT
{
GB_nOT.BackgroundImage = mixedHighlight;
GB_OT.BackgroundImage = null;
}
else // = 0
{
GB_OT.BackgroundImage = mixedHighlight;
GB_nOT.BackgroundImage = null;
}
CB_Language.SelectedValue = pk6.Language;
CB_Country.SelectedValue = pk6.Country;
CB_SubRegion.SelectedValue = pk6.Region;
CB_3DSReg.SelectedValue = pk6.ConsoleRegion;
CB_GameOrigin.SelectedValue = pk6.Version;
CB_EncounterType.SelectedValue = pk6.Gen4 ? pk6.EncounterType : 0;
CB_Ball.SelectedValue = pk6.Ball;
CAL_MetDate.Value = pk6.MetDate ?? new DateTime(2000, 1, 1);
if (pk6.Egg_Location != 0)
{
// Was obtained initially as an egg.
CHK_AsEgg.Checked = true;
GB_EggConditions.Enabled = true;
CB_EggLocation.SelectedValue = pk6.Egg_Location;
CAL_EggDate.Value = pk6.EggMetDate ?? new DateTime(2000, 1, 1);
}
else { CAL_EggDate.Value = new DateTime(2000, 01, 01); CHK_AsEgg.Checked = GB_EggConditions.Enabled = false; CB_EggLocation.SelectedValue = 0; }
CB_MetLocation.SelectedValue = pk6.Met_Location;
// Set CT Gender to None if no CT, else set to gender symbol.
Label_CTGender.Text = pk6.HT_Name == "" ? "" : gendersymbols[pk6.HT_Gender % 2];
Label_CTGender.ForeColor = GetGenderColor(pk6.HT_Gender % 2);
TB_MetLevel.Text = pk6.Met_Level.ToString();
// Reset Label and ComboBox visibility, as well as non-data checked status.
Label_PKRS.Visible = CB_PKRSStrain.Visible = CHK_Infected.Checked = pk6.PKRS_Strain != 0;
Label_PKRSdays.Visible = CB_PKRSDays.Visible = pk6.PKRS_Days != 0;
// Set SelectedIndexes for PKRS
CB_PKRSStrain.SelectedIndex = pk6.PKRS_Strain;
CHK_Cured.Checked = pk6.PKRS_Strain > 0 && pk6.PKRS_Days == 0;
CB_PKRSDays.SelectedIndex = Math.Min(CB_PKRSDays.Items.Count - 1, pk6.PKRS_Days); // to strip out bad hacked 'rus
Contest.Cool = pk6.CNT_Cool;
Contest.Beauty = pk6.CNT_Beauty;
Contest.Cute = pk6.CNT_Cute;
Contest.Smart = pk6.CNT_Smart;
Contest.Tough = pk6.CNT_Tough;
Contest.Sheen = pk6.CNT_Sheen;
TB_HPIV.Text = pk6.IV_HP.ToString();
TB_ATKIV.Text = pk6.IV_ATK.ToString();
TB_DEFIV.Text = pk6.IV_DEF.ToString();
TB_SPEIV.Text = pk6.IV_SPE.ToString();
TB_SPAIV.Text = pk6.IV_SPA.ToString();
TB_SPDIV.Text = pk6.IV_SPD.ToString();
CB_HPType.SelectedValue = pk6.HPType;
TB_HPEV.Text = pk6.EV_HP.ToString();
TB_ATKEV.Text = pk6.EV_ATK.ToString();
TB_DEFEV.Text = pk6.EV_DEF.ToString();
TB_SPEEV.Text = pk6.EV_SPE.ToString();
TB_SPAEV.Text = pk6.EV_SPA.ToString();
TB_SPDEV.Text = pk6.EV_SPD.ToString();
CB_Move1.SelectedValue = pk6.Move1;
CB_Move2.SelectedValue = pk6.Move2;
CB_Move3.SelectedValue = pk6.Move3;
CB_Move4.SelectedValue = pk6.Move4;
CB_RelearnMove1.SelectedValue = pk6.RelearnMove1;
CB_RelearnMove2.SelectedValue = pk6.RelearnMove2;
CB_RelearnMove3.SelectedValue = pk6.RelearnMove3;
CB_RelearnMove4.SelectedValue = pk6.RelearnMove4;
CB_PPu1.SelectedIndex = pk6.Move1_PPUps;
CB_PPu2.SelectedIndex = pk6.Move2_PPUps;
CB_PPu3.SelectedIndex = pk6.Move3_PPUps;
CB_PPu4.SelectedIndex = pk6.Move4_PPUps;
TB_PP1.Text = pk6.Move1_PP.ToString();
TB_PP2.Text = pk6.Move2_PP.ToString();
TB_PP3.Text = pk6.Move3_PP.ToString();
TB_PP4.Text = pk6.Move4_PP.ToString();
// Set Form if count is enough, else cap.
CB_Form.SelectedIndex = CB_Form.Items.Count > pk6.AltForm ? pk6.AltForm : CB_Form.Items.Count - 1;
// Load Extrabyte Value
TB_ExtraByte.Text = pk6.Data[Convert.ToInt32(CB_ExtraBytes.Text, 16)].ToString();
UpdateStats();
TB_EXP.Text = pk6.EXP.ToString();
Label_Gender.Text = gendersymbols[pk6.Gender];
Label_Gender.ForeColor = GetGenderColor(pk6.Gender);
// Highlight the Current Handler
ClickGT(pk6.CurrentHandler == 1 ? GB_nOT : GB_OT, null);
if (HaX)
DEV_Ability.SelectedValue = pk6.Ability;
}
private PKM PreparePK6()
{
PK6 pk6 = pkm as PK6;
if (pk6 == null)
return null;
// Repopulate PK6 with Edited Stuff
CheckTransferPIDValid();
pk6.EncryptionConstant = Util.GetHexValue(TB_EC.Text);
pk6.Checksum = 0; // 0 CHK for now
// Block A
pk6.Species = WinFormsUtil.GetIndex(CB_Species);
pk6.HeldItem = WinFormsUtil.GetIndex(CB_HeldItem);
pk6.TID = Util.ToInt32(TB_TID.Text);
pk6.SID = Util.ToInt32(TB_SID.Text);
pk6.EXP = Util.ToUInt32(TB_EXP.Text);
if (CB_Ability.Text.Length >= 4)
{
pk6.Ability = (byte)Array.IndexOf(GameInfo.Strings.abilitylist, CB_Ability.Text.Remove(CB_Ability.Text.Length - 4));
pk6.AbilityNumber = Util.ToInt32(TB_AbilityNumber.Text); // Number
}
// pkx[0x16], pkx[0x17] are handled by the Medals UI (Hits & Training Bag)
pk6.PID = Util.GetHexValue(TB_PID.Text);
pk6.Nature = (byte)WinFormsUtil.GetIndex(CB_Nature);
pk6.FatefulEncounter = CHK_Fateful.Checked;
pk6.Gender = PKX.GetGenderFromPID(Label_Gender.Text);
pk6.AltForm = (MT_Form.Enabled ? Convert.ToInt32(MT_Form.Text) : CB_Form.Enabled ? CB_Form.SelectedIndex : 0) & 0x1F;
pk6.EV_HP = Util.ToInt32(TB_HPEV.Text); // EVs
pk6.EV_ATK = Util.ToInt32(TB_ATKEV.Text);
pk6.EV_DEF = Util.ToInt32(TB_DEFEV.Text);
pk6.EV_SPE = Util.ToInt32(TB_SPEEV.Text);
pk6.EV_SPA = Util.ToInt32(TB_SPAEV.Text);
pk6.EV_SPD = Util.ToInt32(TB_SPDEV.Text);
pk6.CNT_Cool = Contest.Cool;
pk6.CNT_Beauty = Contest.Beauty;
pk6.CNT_Cute = Contest.Cute;
pk6.CNT_Smart = Contest.Smart;
pk6.CNT_Tough = Contest.Tough;
pk6.CNT_Sheen = Contest.Sheen;
pk6.PKRS_Days = CB_PKRSDays.SelectedIndex;
pk6.PKRS_Strain = CB_PKRSStrain.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
pk6.Nickname = TB_Nickname.Text;
pk6.Move1 = WinFormsUtil.GetIndex(CB_Move1);
pk6.Move2 = WinFormsUtil.GetIndex(CB_Move2);
pk6.Move3 = WinFormsUtil.GetIndex(CB_Move3);
pk6.Move4 = WinFormsUtil.GetIndex(CB_Move4);
pk6.Move1_PP = WinFormsUtil.GetIndex(CB_Move1) > 0 ? Util.ToInt32(TB_PP1.Text) : 0;
pk6.Move2_PP = WinFormsUtil.GetIndex(CB_Move2) > 0 ? Util.ToInt32(TB_PP2.Text) : 0;
pk6.Move3_PP = WinFormsUtil.GetIndex(CB_Move3) > 0 ? Util.ToInt32(TB_PP3.Text) : 0;
pk6.Move4_PP = WinFormsUtil.GetIndex(CB_Move4) > 0 ? Util.ToInt32(TB_PP4.Text) : 0;
pk6.Move1_PPUps = WinFormsUtil.GetIndex(CB_Move1) > 0 ? CB_PPu1.SelectedIndex : 0;
pk6.Move2_PPUps = WinFormsUtil.GetIndex(CB_Move2) > 0 ? CB_PPu2.SelectedIndex : 0;
pk6.Move3_PPUps = WinFormsUtil.GetIndex(CB_Move3) > 0 ? CB_PPu3.SelectedIndex : 0;
pk6.Move4_PPUps = WinFormsUtil.GetIndex(CB_Move4) > 0 ? CB_PPu4.SelectedIndex : 0;
pk6.RelearnMove1 = WinFormsUtil.GetIndex(CB_RelearnMove1);
pk6.RelearnMove2 = WinFormsUtil.GetIndex(CB_RelearnMove2);
pk6.RelearnMove3 = WinFormsUtil.GetIndex(CB_RelearnMove3);
pk6.RelearnMove4 = WinFormsUtil.GetIndex(CB_RelearnMove4);
// 0x72 - Ribbon editor sets this flag (Secret Super Training)
// 0x73
pk6.IV_HP = Util.ToInt32(TB_HPIV.Text);
pk6.IV_ATK = Util.ToInt32(TB_ATKIV.Text);
pk6.IV_DEF = Util.ToInt32(TB_DEFIV.Text);
pk6.IV_SPE = Util.ToInt32(TB_SPEIV.Text);
pk6.IV_SPA = Util.ToInt32(TB_SPAIV.Text);
pk6.IV_SPD = Util.ToInt32(TB_SPDIV.Text);
pk6.IsEgg = CHK_IsEgg.Checked;
pk6.IsNicknamed = CHK_Nicknamed.Checked;
// Block C
pk6.HT_Name = TB_OTt2.Text;
// 0x90-0xAF
pk6.HT_Gender = PKX.GetGenderFromPID(Label_CTGender.Text) & 1;
// Plus more, set by MemoryAmie (already in buff)
// Block D
pk6.OT_Name = TB_OT.Text;
pk6.CurrentFriendship = Util.ToInt32(TB_Friendship.Text);
DateTime? egg_date = null;
int egg_location = 0;
if (CHK_AsEgg.Checked) // If encountered as an egg, load the Egg Met data from fields.
{
egg_date = CAL_EggDate.Value;
egg_location = WinFormsUtil.GetIndex(CB_EggLocation);
}
// Egg Met Data
pk6.EggMetDate = egg_date;
pk6.Egg_Location = egg_location;
// Met Data
pk6.MetDate = CAL_MetDate.Value;
pk6.Met_Location = WinFormsUtil.GetIndex(CB_MetLocation);
if (pk6.IsEgg && pk6.Met_Location == 0) // If still an egg, it has no hatch location/date. Zero it!
pk6.MetDate = null;
// 0xD7 Unknown
pk6.Ball = WinFormsUtil.GetIndex(CB_Ball);
pk6.Met_Level = Util.ToInt32(TB_MetLevel.Text);
pk6.OT_Gender = PKX.GetGenderFromPID(Label_OTGender.Text);
pk6.EncounterType = WinFormsUtil.GetIndex(CB_EncounterType);
pk6.Version = WinFormsUtil.GetIndex(CB_GameOrigin);
pk6.Country = WinFormsUtil.GetIndex(CB_Country);
pk6.Region = WinFormsUtil.GetIndex(CB_SubRegion);
pk6.ConsoleRegion = WinFormsUtil.GetIndex(CB_3DSReg);
pk6.Language = WinFormsUtil.GetIndex(CB_Language);
// 0xE4-0xE7
// Toss in Party Stats
Array.Resize(ref pk6.Data, pk6.SIZE_PARTY);
pk6.Stat_Level = Util.ToInt32(TB_Level.Text);
pk6.Stat_HPCurrent = Util.ToInt32(Stat_HP.Text);
pk6.Stat_HPMax = Util.ToInt32(Stat_HP.Text);
pk6.Stat_ATK = Util.ToInt32(Stat_ATK.Text);
pk6.Stat_DEF = Util.ToInt32(Stat_DEF.Text);
pk6.Stat_SPE = Util.ToInt32(Stat_SPE.Text);
pk6.Stat_SPA = Util.ToInt32(Stat_SPA.Text);
pk6.Stat_SPD = Util.ToInt32(Stat_SPD.Text);
// Unneeded Party Stats (Status, Flags, Unused)
pk6.Data[0xE8] = pk6.Data[0xE9] = pk6.Data[0xEA] = pk6.Data[0xEB] =
pk6.Data[0xED] = pk6.Data[0xEE] = pk6.Data[0xEF] =
pk6.Data[0xFE] = pk6.Data[0xFF] = pk6.Data[0x100] =
pk6.Data[0x101] = pk6.Data[0x102] = pk6.Data[0x103] = 0;
// Hax Illegality
if (HaX)
{
pk6.Ability = (byte)WinFormsUtil.GetIndex(DEV_Ability);
pk6.Stat_Level = (byte)Math.Min(Convert.ToInt32(MT_Level.Text), byte.MaxValue);
}
// Fix Moves if a slot is empty
pk6.FixMoves();
pk6.FixRelearn();
// Fix Handler (Memories & OT) -- no foreign memories for Pokemon without a foreign trainer (none for eggs)
if (ModifyPKM)
pk6.FixMemories();
// PKX is now filled
pk6.RefreshChecksum();
return pk6;
}
}
}