Skip to content

Commit

Permalink
PK1 <-> PK2 Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
SciresM committed Sep 4, 2016
1 parent 387ed9c commit 1bb5388
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
2 changes: 1 addition & 1 deletion PKHeX/Legality/Tables2.cs
Expand Up @@ -19,7 +19,7 @@ public static partial class Legal
191, 192, 193, 194, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249
};

internal static readonly ushort[] HeldItems_GSC = new ushort[1].Concat(Pouch_Items_GSC).Concat(Pouch_Ball_GSC).ToArray();
internal static readonly ushort[] HeldItems_GSC = new ushort[1].Concat(Pouch_Items_GSC).Concat(Pouch_Ball_GSC).Concat(Pouch_TMHM_GSC).ToArray();

internal static readonly int[] MovePP_GSC =
{
Expand Down
44 changes: 42 additions & 2 deletions PKHeX/PKM/PK1.cs
Expand Up @@ -120,8 +120,8 @@ public override int Species
get { return PKX.getG1Species(Data[0]); }
set
{
// Before updating catch rate, check if Special Yellow Version Pikachu
if (!(PKX.getG1Species(Data[0]) == 25 && value == 25 && Catch_Rate == 163))
// Before updating catch rate, check if non-standard
if (Catch_Rate == PersonalTable.RBY[Species].CatchRate)
Catch_Rate = PersonalTable.RBY[value].CatchRate;
Data[0] = (byte)PKX.setG1Species(value);
Type_A = PersonalTable.RBY[value].Types[0];
Expand Down Expand Up @@ -250,6 +250,46 @@ public override bool CanHoldItem(ushort[] ValidArray)
public override int CNT_Tough { get { return 0; } set { } }
public override int CNT_Sheen { get { return 0; } set { } }
#endregion

public PK2 convertToPK2()
{
PK2 pk2 = new PK2(null, Identifier, Japanese);
pk2.Species = Species;
Array.Copy(Data, 0x7, pk2.Data, 0x1, 0x1A);
// https://github.com/pret/pokecrystal/blob/master/engine/link.asm#L1132
if (!Legal.HeldItems_GSC.Contains((ushort)pk2.HeldItem))
switch (pk2.HeldItem)
{
case 0x19:
pk2.HeldItem = 0x92; // Leftovers
break;
case 0x2D:
pk2.HeldItem = 0x53; // Bitter Berry
break;
case 0x32:
pk2.HeldItem = 0xAE; // Leftovers
break;
case 0x5A:
case 0x64:
case 0x78:
case 0x87:
case 0xBE:
case 0xC3:
case 0xDC:
case 0xFA:
case 0xFF:
pk2.HeldItem = 0xAD; // Berry
break;
}
pk2.CurrentFriendship = PersonalTable.C[Species].BaseFriendship;
// Pokerus = 0
// Caught Data = 0
pk2.Stat_Level = Stat_Level;
Array.Copy(otname, 0, pk2.otname, 0, otname.Length);
Array.Copy(nick, 0, pk2.nick, 0, nick.Length);

return pk2;
}
}

public class PokemonList1
Expand Down
14 changes: 14 additions & 0 deletions PKHeX/PKM/PK2.cs
Expand Up @@ -325,6 +325,20 @@ public override int HPType
public override int CNT_Tough { get { return 0; } set { } }
public override int CNT_Sheen { get { return 0; } set { } }
#endregion

public PK1 convertToPK1()
{
PK1 pk1 = new PK1(null, Identifier, Japanese);
Array.Copy(Data, 0x1, pk1.Data, 0x7, 0x1A);
pk1.Species = Species; // This will take care of Typing :)
pk1.Stat_HPCurrent = Stat_HPCurrent;
pk1.Stat_Level = Stat_Level;
// Status = 0
Array.Copy(otname, 0, pk1.otname, 0, otname.Length);
Array.Copy(nick, 0, pk1.nick, 0, nick.Length);

return pk1;
}
}

public class PokemonList2
Expand Down
33 changes: 31 additions & 2 deletions PKHeX/PKM/PKMConverter.cs
Expand Up @@ -100,6 +100,9 @@ public static PKM getPKMfromBytes(byte[] data, string ident = null)
}
internal static PKM convertToFormat(PKM pk, int Format, out string comment)
{
string currentFormat = pk.Format.ToString();
PKM pkm = pk.Clone();

if (pk == null)
{
comment = "Null input. Aborting.";
Expand All @@ -110,6 +113,34 @@ internal static PKM convertToFormat(PKM pk, int Format, out string comment)
comment = "No need to convert, current format matches requested format.";
return pk;
}
if (pk.Format != Format && pk.Format <= 2 && Format <= 2)
{
if (Format == 2) // pk.Format == 1
{
pkm = ((PK1) pk).convertToPK2();
}
if (Format == 1) // pk.Format == 2
{
// Only convert if it's legal to do so.
if (1 <= pk.Species && pk.Species <= 151)
{
foreach (var move in new[] { pk.Move1, pk.Move2, pk.Move3, pk.Move4})
if (move < 1 || move > 165)
{
comment = $"Pokemon cannot be converted due to invalid move: {Main.movelist[move]}";
return null;
}
pkm = ((PK2) pk).convertToPK1();
}
else
{
comment =$"Cannot convert a {PKX.getSpeciesName(pk.Species, ((PK2)pk).Japanese ? 1 : 2)} to pk{Format}";
return null;
}
}
comment = $"Converted from pk{pk.Format} to pk{Format}";
return pkm;
}
if (pk.Format > Format)
{
comment = "Cannot convert a PKM backwards." + Environment.NewLine
Expand All @@ -128,8 +159,6 @@ internal static PKM convertToFormat(PKM pk, int Format, out string comment)
+ "Please wait for Sun/Moon to release and documentation to occur.";
return null;
}
string currentFormat = pk.Format.ToString();
PKM pkm = pk.Clone();
if (pkm.IsEgg) // force hatch
{
pkm.IsEgg = false;
Expand Down

0 comments on commit 1bb5388

Please sign in to comment.