Faster G7TID generation

#823

generate a random T7SID instead of brute forcing a pair
This commit is contained in:
Kurt 2017-02-05 10:57:42 -08:00
parent 77a68c23b9
commit f64d31c557
2 changed files with 30 additions and 29 deletions

View file

@ -389,35 +389,9 @@ namespace PKHeX.WinForms
private void B_GenTID_Click(object sender, EventArgs e)
{
B_GenTID.Text = "Generating";
B_GenTID.Enabled = false;
int final, i_TID, i_SID;
Random random = new Random();
i_TID = random.Next(0, 65535);
bool matchFound = false;
while (!matchFound)
{
i_SID = 0;
while (i_SID < 65535 && !matchFound)
{
final = i_TID + (i_SID * 65536) % 1000000;
if (final == int.Parse(MT_G7TID.Text))
{
MT_TID.Text = i_TID.ToString();
MT_SID.Text = i_SID.ToString();
matchFound = true;
B_GenTID.Text = "Generate";
B_GenTID.Enabled = true;
}
i_SID++;
}
i_TID = (i_TID == 65535) ? 0 : i_TID + 1;
}
var tuple = SaveUtil.getTIDSID(Util.ToUInt32(MT_G7TID.Text), ModifierKeys == Keys.Control);
MT_TID.Text = tuple.Item1.ToString("D5");
MT_SID.Text = tuple.Item2.ToString("D5");
}
private readonly Dictionary<int, string> RecordList = new Dictionary<int, string>

View file

@ -975,5 +975,32 @@ namespace PKHeX.Core
return keys;
}
/// <summary>
/// Creates a 16bit TID/SID tuple for a given G7TID.
/// </summary>
/// <param name="G7TID">Desired G7TID</param>
/// <param name="minimizeSID">Optional param to yield minimum SID.</param>
/// <returns>16bit TID/SID tuple</returns>
public static Tuple<uint, uint> getTIDSID(uint G7TID, bool minimizeSID = false)
{
// 32 bit number = 4294 967295
// lowest 6 digits G7TID
// Bare minimum 32bit value to get ID, yields min SID
uint val = G7TID;
if (!minimizeSID) // randomize SID
{
uint s7 = 4294;
if (val > 967295)
s7 -= 1;
s7 = (uint)Util.rand.Next(0, (int)s7);
val += s7 * 1000000;
}
uint TID = val & 0xFFFF;
uint SID = val >> 16;
return new Tuple<uint, uint>(TID, SID);
}
}
}