2015-12-28 05:26:07 +00:00
|
|
|
|
using System;
|
2016-08-28 03:12:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2015-12-28 05:26:07 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2015-12-28 05:26:07 +00:00
|
|
|
|
{
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Common logic for <see cref="PKM"/> data providing and manipulation.
|
|
|
|
|
/// </summary>
|
2016-08-08 20:47:56 +00:00
|
|
|
|
public static class PKX
|
2015-12-28 05:26:07 +00:00
|
|
|
|
{
|
2019-11-16 01:34:18 +00:00
|
|
|
|
internal static readonly PersonalTable Personal = PersonalTable.SWSH;
|
2019-11-16 16:25:05 +00:00
|
|
|
|
public const int Generation = 8;
|
2017-02-05 02:27:28 +00:00
|
|
|
|
|
2020-12-22 01:17:56 +00:00
|
|
|
|
private static readonly HashSet<int> Sizes = new()
|
2018-05-14 03:35:09 +00:00
|
|
|
|
{
|
2020-01-04 22:48:39 +00:00
|
|
|
|
PokeCrypto.SIZE_1JLIST, PokeCrypto.SIZE_1ULIST,
|
2020-10-03 01:08:40 +00:00
|
|
|
|
PokeCrypto.SIZE_2ULIST, PokeCrypto.SIZE_2JLIST, PokeCrypto.SIZE_2STADIUM,
|
2020-01-04 22:48:39 +00:00
|
|
|
|
PokeCrypto.SIZE_3STORED, PokeCrypto.SIZE_3PARTY,
|
|
|
|
|
PokeCrypto.SIZE_3CSTORED, PokeCrypto.SIZE_3XSTORED,
|
|
|
|
|
PokeCrypto.SIZE_4STORED, PokeCrypto.SIZE_4PARTY,
|
|
|
|
|
PokeCrypto.SIZE_5PARTY,
|
|
|
|
|
PokeCrypto.SIZE_6STORED, PokeCrypto.SIZE_6PARTY,
|
|
|
|
|
PokeCrypto.SIZE_8STORED, PokeCrypto.SIZE_8PARTY,
|
2018-05-14 03:35:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-08-08 21:44:05 +00:00
|
|
|
|
/// <summary>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// Determines if the given length is valid for a <see cref="PKM"/>.
|
2016-08-08 21:44:05 +00:00
|
|
|
|
/// </summary>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <param name="len">Data length of the file/array.</param>
|
|
|
|
|
/// <returns>A <see cref="bool"/> indicating whether or not the length is valid for a <see cref="PKM"/>.</returns>
|
2018-05-14 03:35:09 +00:00
|
|
|
|
public static bool IsPKM(long len) => Sizes.Contains((int)len);
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets randomized EVs for a given generation format
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="generation">Generation specific formatting option</param>
|
|
|
|
|
/// <returns>Array containing randomized EVs (H/A/B/S/C/D)</returns>
|
2018-02-27 05:22:35 +00:00
|
|
|
|
public static int[] GetRandomEVs(int generation = Generation)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2020-01-26 05:49:52 +00:00
|
|
|
|
var rnd = Util.Rand;
|
2017-02-05 02:27:28 +00:00
|
|
|
|
if (generation > 2)
|
2016-08-28 10:18:22 +00:00
|
|
|
|
{
|
2018-02-27 05:22:35 +00:00
|
|
|
|
var evs = new int[6];
|
2018-07-04 19:23:42 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
int max = 510;
|
|
|
|
|
for (int i = 0; i < evs.Length - 1; i++)
|
2020-01-26 05:49:52 +00:00
|
|
|
|
max -= evs[i] = (byte)Math.Min(rnd.Next(Math.Min(300, max)), 252);
|
2018-07-04 19:23:42 +00:00
|
|
|
|
evs[5] = max;
|
|
|
|
|
} while (evs[5] > 252);
|
2016-08-28 10:18:22 +00:00
|
|
|
|
Util.Shuffle(evs);
|
|
|
|
|
return evs;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-02-27 05:22:35 +00:00
|
|
|
|
var evs = new int[6];
|
2016-08-28 10:18:22 +00:00
|
|
|
|
for (int i = 0; i < evs.Length; i++)
|
2020-01-26 05:49:52 +00:00
|
|
|
|
evs[i] = rnd.Next(ushort.MaxValue + 1);
|
2016-08-28 10:18:22 +00:00
|
|
|
|
return evs;
|
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-02-05 02:27:28 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Translates a Gender string to Gender integer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="s">Gender string</param>
|
|
|
|
|
/// <returns>Gender integer</returns>
|
2017-10-03 06:13:40 +00:00
|
|
|
|
public static int GetGenderFromString(string s)
|
2015-12-28 05:26:07 +00:00
|
|
|
|
{
|
2019-01-21 05:55:28 +00:00
|
|
|
|
if (s.Length != 1)
|
|
|
|
|
return 2;
|
2021-01-02 01:08:49 +00:00
|
|
|
|
return s[0] switch
|
2019-01-21 05:55:28 +00:00
|
|
|
|
{
|
2020-12-25 01:12:08 +00:00
|
|
|
|
'♂' or 'M' => 0,
|
|
|
|
|
'♀' or 'F' => 1,
|
|
|
|
|
_ => 2,
|
|
|
|
|
};
|
2015-12-28 05:26:07 +00:00
|
|
|
|
}
|
2018-03-01 05:50:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the nature modification values and checks if they are equal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="nature">Nature</param>
|
|
|
|
|
/// <param name="incr">Increased stat</param>
|
|
|
|
|
/// <param name="decr">Decreased stat</param>
|
2018-03-09 05:18:32 +00:00
|
|
|
|
/// <returns>True if nature modification values are equal or the Nature is out of range.</returns>
|
2018-03-01 05:50:50 +00:00
|
|
|
|
public static bool GetNatureModification(int nature, out int incr, out int decr)
|
|
|
|
|
{
|
2018-07-29 20:27:48 +00:00
|
|
|
|
incr = (nature / 5) + 1;
|
|
|
|
|
decr = (nature % 5) + 1;
|
2018-03-09 05:18:32 +00:00
|
|
|
|
return incr == decr || nature >= 25; // invalid
|
2018-03-01 05:50:50 +00:00
|
|
|
|
}
|
2018-03-23 05:43:56 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates stats according to the specified nature.
|
|
|
|
|
/// </summary>
|
2018-12-04 04:59:48 +00:00
|
|
|
|
/// <param name="stats">Current stats to amplify if appropriate</param>
|
2018-03-23 05:43:56 +00:00
|
|
|
|
/// <param name="nature">Nature</param>
|
2018-12-04 04:59:48 +00:00
|
|
|
|
public static void ModifyStatsForNature(ushort[] stats, int nature)
|
2018-03-23 05:43:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (GetNatureModification(nature, out int incr, out int decr))
|
|
|
|
|
return;
|
2018-12-04 04:59:48 +00:00
|
|
|
|
stats[incr] *= 11; stats[incr] /= 10;
|
|
|
|
|
stats[decr] *= 9; stats[decr] /= 10;
|
2018-03-23 05:43:56 +00:00
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a random PID according to specifications.
|
|
|
|
|
/// </summary>
|
2020-01-26 05:49:52 +00:00
|
|
|
|
/// <param name="rnd">RNG to use</param>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <param name="species">National Dex ID</param>
|
2020-06-11 16:32:43 +00:00
|
|
|
|
/// <param name="gender">Current Gender</param>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <param name="origin">Origin Generation</param>
|
|
|
|
|
/// <param name="nature">Nature</param>
|
2020-12-11 04:42:30 +00:00
|
|
|
|
/// <param name="form">Form</param>
|
2020-06-11 16:32:43 +00:00
|
|
|
|
/// <param name="oldPID">Current PID</param>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <remarks>Used to retain ability bits.</remarks>
|
|
|
|
|
/// <returns>Rerolled PID.</returns>
|
2020-06-11 16:32:43 +00:00
|
|
|
|
public static uint GetRandomPID(Random rnd, int species, int gender, int origin, int nature, int form, uint oldPID)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2020-06-11 16:32:43 +00:00
|
|
|
|
// Gen6+ (and VC) PIDs do not tie PID to Nature/Gender/Ability
|
2016-06-20 04:22:43 +00:00
|
|
|
|
if (origin >= 24)
|
2020-01-26 05:49:52 +00:00
|
|
|
|
return Util.Rand32(rnd);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2020-06-11 16:32:43 +00:00
|
|
|
|
// Below logic handles Gen3-5.
|
|
|
|
|
|
|
|
|
|
int gt = Personal[species].Gender;
|
|
|
|
|
bool g34 = origin <= 15;
|
2020-06-17 02:46:22 +00:00
|
|
|
|
uint abilBitVal = g34 ? oldPID & 0x0000_0001 : oldPID & 0x0001_0000;
|
2020-06-11 16:32:43 +00:00
|
|
|
|
|
2019-12-09 01:39:19 +00:00
|
|
|
|
bool g3unown = origin <= 5 && species == (int)Species.Unown;
|
2020-12-25 20:30:26 +00:00
|
|
|
|
bool singleGender = gt is 0 or 254 or 255; // single gender, skip gender check
|
2016-06-20 04:22:43 +00:00
|
|
|
|
while (true) // Loop until we find a suitable PID
|
|
|
|
|
{
|
2020-01-26 05:49:52 +00:00
|
|
|
|
uint pid = Util.Rand32(rnd);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// Gen 3/4: Nature derived from PID
|
2020-06-11 16:32:43 +00:00
|
|
|
|
if (g34 && pid%25 != nature)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Gen 3 Unown: Letter/form derived from PID
|
|
|
|
|
if (g3unown)
|
|
|
|
|
{
|
2018-06-28 01:52:02 +00:00
|
|
|
|
var pidLetter = GetUnownForm(pid);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
if (pidLetter != form)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-06-11 16:32:43 +00:00
|
|
|
|
else if (g34)
|
2018-07-29 20:27:48 +00:00
|
|
|
|
{
|
2020-06-11 16:32:43 +00:00
|
|
|
|
if (abilBitVal != (pid & 0x0000_0001)) // keep ability bits
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-06-17 02:46:22 +00:00
|
|
|
|
else
|
2020-06-11 16:32:43 +00:00
|
|
|
|
{
|
|
|
|
|
if (abilBitVal != (pid & 0x0001_0000)) // keep ability bits
|
|
|
|
|
continue;
|
2018-07-29 20:27:48 +00:00
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
2020-06-11 16:32:43 +00:00
|
|
|
|
if (singleGender) // Set Gender(less)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
return pid; // PID can be anything
|
2017-05-19 02:16:11 +00:00
|
|
|
|
|
|
|
|
|
// Gen 3/4/5: Gender derived from PID
|
2020-06-11 16:32:43 +00:00
|
|
|
|
if (gender == GetGenderFromPIDAndRatio(pid, gt))
|
2017-05-19 02:16:11 +00:00
|
|
|
|
return pid;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-08 20:47:56 +00:00
|
|
|
|
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the Unown Forme ID from PID.
|
|
|
|
|
/// </summary>
|
2018-06-28 01:52:02 +00:00
|
|
|
|
/// <param name="pid">Personality ID</param>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <remarks>Should only be used for 3rd Generation origin specimens.</remarks>
|
|
|
|
|
/// <returns></returns>
|
2018-06-28 01:52:02 +00:00
|
|
|
|
public static int GetUnownForm(uint pid)
|
2015-12-28 05:26:07 +00:00
|
|
|
|
{
|
2018-07-29 20:27:48 +00:00
|
|
|
|
var val = (pid & 0x3000000) >> 18 | (pid & 0x30000) >> 12 | (pid & 0x300) >> 6 | (pid & 0x3);
|
2018-06-28 01:52:02 +00:00
|
|
|
|
return (int)(val % 28);
|
2015-12-28 05:26:07 +00:00
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the gender ID of the species based on the Personality ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="species">National Dex ID.</param>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
/// <param name="pid">Personality ID.</param>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <returns>Gender ID (0/1/2)</returns>
|
|
|
|
|
/// <remarks>This method should only be used for Generations 3-5 origin.</remarks>
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public static int GetGenderFromPID(int species, uint pid)
|
2017-05-19 02:16:11 +00:00
|
|
|
|
{
|
2019-02-12 05:49:05 +00:00
|
|
|
|
int gt = Personal[species].Gender;
|
2020-06-17 02:46:22 +00:00
|
|
|
|
return GetGenderFromPIDAndRatio(pid, gt);
|
2017-05-19 02:16:11 +00:00
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2021-01-02 01:08:49 +00:00
|
|
|
|
public static int GetGenderFromPIDAndRatio(uint pid, int gr) => gr switch
|
2015-12-28 05:26:07 +00:00
|
|
|
|
{
|
2021-01-02 01:08:49 +00:00
|
|
|
|
255 => 2,
|
|
|
|
|
254 => 1,
|
|
|
|
|
0 => 0,
|
|
|
|
|
_ => (pid & 0xFF) < gr ? 1 : 0
|
|
|
|
|
};
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an array of valid <see cref="PKM"/> file extensions.
|
|
|
|
|
/// </summary>
|
2018-05-12 19:28:48 +00:00
|
|
|
|
/// <param name="maxGeneration">Maximum Generation to permit</param>
|
2017-02-05 02:27:28 +00:00
|
|
|
|
/// <returns>Valid <see cref="PKM"/> file extensions.</returns>
|
2018-05-12 19:28:48 +00:00
|
|
|
|
public static string[] GetPKMExtensions(int maxGeneration = Generation)
|
2017-01-05 06:22:50 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new List<string>();
|
2020-12-25 20:30:26 +00:00
|
|
|
|
int min = maxGeneration is <= 2 or >= 7 ? 1 : 3;
|
2018-06-28 01:53:08 +00:00
|
|
|
|
for (int i = min; i <= maxGeneration; i++)
|
|
|
|
|
result.Add($"pk{i}");
|
|
|
|
|
|
|
|
|
|
if (maxGeneration >= 3)
|
|
|
|
|
{
|
2019-02-10 04:33:37 +00:00
|
|
|
|
result.Add("ck3"); // colosseum
|
|
|
|
|
result.Add("xk3"); // xd
|
2018-06-28 01:53:08 +00:00
|
|
|
|
}
|
|
|
|
|
if (maxGeneration >= 4)
|
2019-02-10 04:33:37 +00:00
|
|
|
|
result.Add("bk4"); // battle revolution
|
|
|
|
|
if (maxGeneration >= 7)
|
|
|
|
|
result.Add("pb7"); // let's go
|
2018-06-28 01:53:08 +00:00
|
|
|
|
|
2017-01-05 06:22:50 +00:00
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
2017-02-05 02:27:28 +00:00
|
|
|
|
|
2018-04-21 21:38:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Roughly detects the PKM format from the file's extension.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ext">File extension.</param>
|
|
|
|
|
/// <param name="prefer">Preference if not a valid extension, usually the highest acceptable format.</param>
|
|
|
|
|
/// <returns>Format hint that the file is.</returns>
|
|
|
|
|
public static int GetPKMFormatFromExtension(string ext, int prefer)
|
|
|
|
|
{
|
2019-02-10 07:22:22 +00:00
|
|
|
|
if (string.IsNullOrEmpty(ext))
|
|
|
|
|
return prefer;
|
|
|
|
|
return GetPKMFormatFromExtension(ext[ext.Length - 1], prefer);
|
2018-04-21 21:38:18 +00:00
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-04-21 21:38:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Roughly detects the PKM format from the file's extension.
|
|
|
|
|
/// </summary>
|
2019-04-14 16:04:40 +00:00
|
|
|
|
/// <param name="last">Last character of the file's extension.</param>
|
2018-04-21 21:38:18 +00:00
|
|
|
|
/// <param name="prefer">Preference if not a valid extension, usually the highest acceptable format.</param>
|
|
|
|
|
/// <returns>Format hint that the file is.</returns>
|
|
|
|
|
public static int GetPKMFormatFromExtension(char last, int prefer)
|
|
|
|
|
{
|
2020-12-29 08:37:59 +00:00
|
|
|
|
if (last is >= '1' and <= '9')
|
2018-04-21 21:38:18 +00:00
|
|
|
|
return last - '0';
|
|
|
|
|
return last == 'x' ? 6 : prefer;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-18 23:22:21 +00:00
|
|
|
|
internal static bool IsPKMPresentGB(byte[] data, int offset) => data[offset] != 0;
|
|
|
|
|
internal static bool IsPKMPresentGC(byte[] data, int offset) => BitConverter.ToUInt16(data, offset) != 0;
|
2018-12-16 03:37:19 +00:00
|
|
|
|
internal static bool IsPKMPresentGBA(byte[] data, int offset) => (data[offset + 0x13] & 0xFB) == 2; // ignore egg flag, must be FlagHasSpecies.
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2018-03-18 23:22:21 +00:00
|
|
|
|
internal static bool IsPKMPresent(byte[] data, int offset)
|
|
|
|
|
{
|
|
|
|
|
if (BitConverter.ToUInt32(data, offset) != 0) // PID
|
|
|
|
|
return true;
|
|
|
|
|
ushort species = BitConverter.ToUInt16(data, offset + 8);
|
|
|
|
|
return species != 0;
|
|
|
|
|
}
|
2018-07-29 20:27:48 +00:00
|
|
|
|
|
2019-02-24 21:57:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a function that can check a byte array (at an offset) to see if a <see cref="PKM"/> is possibly present.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="blank"></param>
|
2019-09-19 02:58:23 +00:00
|
|
|
|
/// <returns>Function that checks if a byte array (at an offset) has a <see cref="PKM"/> present</returns>
|
2018-03-18 23:22:21 +00:00
|
|
|
|
public static Func<byte[], int, bool> GetFuncIsPKMPresent(PKM blank)
|
|
|
|
|
{
|
|
|
|
|
if (blank.Format >= 4)
|
|
|
|
|
return IsPKMPresent;
|
|
|
|
|
if (blank.Format <= 2)
|
|
|
|
|
return IsPKMPresentGB;
|
2020-01-04 22:48:39 +00:00
|
|
|
|
if (blank.Data.Length <= PokeCrypto.SIZE_3PARTY)
|
2018-03-18 23:22:21 +00:00
|
|
|
|
return IsPKMPresentGBA;
|
|
|
|
|
return IsPKMPresentGC;
|
|
|
|
|
}
|
2018-07-02 21:37:21 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reorders (in place) the input array of stats to have the Speed value last rather than before the SpA/SpD stats.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">Input array to reorder</param>
|
|
|
|
|
/// <returns>Same array, reordered.</returns>
|
|
|
|
|
public static int[] ReorderSpeedLast(int[] value)
|
|
|
|
|
{
|
|
|
|
|
var spe = value[3];
|
|
|
|
|
value[3] = value[4];
|
|
|
|
|
value[4] = value[5];
|
|
|
|
|
value[5] = spe;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2015-12-28 05:26:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|