From f64d31c557eb15dfdba54b17576603621f0ed834 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 5 Feb 2017 10:57:42 -0800 Subject: [PATCH] Faster G7TID generation #823 generate a random T7SID instead of brute forcing a pair --- .../Save Editors/Gen7/SAV_Trainer7.cs | 32 ++----------------- PKHeX/Saves/SaveUtil.cs | 27 ++++++++++++++++ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs index f33f69a6a..06cbe8684 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs @@ -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 RecordList = new Dictionary diff --git a/PKHeX/Saves/SaveUtil.cs b/PKHeX/Saves/SaveUtil.cs index 48282469c..67648f2c9 100644 --- a/PKHeX/Saves/SaveUtil.cs +++ b/PKHeX/Saves/SaveUtil.cs @@ -975,5 +975,32 @@ namespace PKHeX.Core return keys; } + + /// + /// Creates a 16bit TID/SID tuple for a given G7TID. + /// + /// Desired G7TID + /// Optional param to yield minimum SID. + /// 16bit TID/SID tuple + public static Tuple 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(TID, SID); + } } }