Пример #1
0
        private void buttonSeedGenerate_Click(object sender, EventArgs e)
        {
            if (!ParametersInputCheck())
            {
                return;
            }

            #region Initialize

            if (comboBoxNature.Text == "Any")
            {
                MessageBox.Show("Please select a specific list of natures.");
                return;
            }
            List <uint> natures =
                (from t in comboBoxNature.CheckBoxItems where t.Checked select(uint) ((Nature)t.ComboBoxItem).Number).
                ToList();

            var  profile     = (Profile)comboBoxProfiles.SelectedItem;
            uint mac_partial = (uint)profile.MAC_Address & 0xFFFFFF;

            uint minFrame = uint.Parse(maskedTextBoxCapMinOffset.Text);
            uint maxFrame = uint.Parse(maskedTextBoxCapMaxOffset.Text);

            uint groupSize = uint.Parse(maskedTextBoxGroupSize.Text);

            uint seedCGear = uint.Parse(textBoxSeed.Text, NumberStyles.HexNumber);

            int generateYear = int.Parse(maskedTextBoxYear.Text);

            if (generateYear < 2000 || generateYear > 2099)
            {
                MessageBox.Show("Year must be a value between 2000 and 2099, inclusive.");
                return;
            }

            uint frameCGear = uint.Parse(maskedTextBoxCGearFrame.Text);
            //generate the CGear Seed Times
            uint ab   = seedCGear - mac_partial >> 24;
            uint cd   = (seedCGear - mac_partial & 0x00FF0000) >> 16;
            uint efgh = seedCGear - mac_partial & 0x0000FFFF;

            //  Get Delay
            uint delay = efgh + (uint)(2000 - generateYear);

            //  Get Calibration
            uint calibration = uint.Parse(maskedTextBoxDelayCalibration.Text);

            //  Store the Calibrated Delay and offset
            uint calibratedDelay = delay + calibration;

            long offset = -calibratedDelay / 60;

            //  Get Hour
            var hour = (int)cd;

            //  We need to check here, as a user could have entered a seed
            //  that is not possible (invalid hour) to lets warn and exit
            //  on it.
            if (hour > 23)
            {
                MessageBox.Show("This seed is invalid, please verify that you have entered it correctly and try again.",
                                "Invalid Seed", MessageBoxButtons.OK);

                return;
            }

            maskedTextBoxDelay.Text = delay.ToString(CultureInfo.InvariantCulture);

            List <List <ButtonComboType> > keypresses = GetKeypresses();

            iframes = new List <IFrameCapture>();
            var generator = new FrameGenerator
            {
                InitialSeed  = seedCGear,
                FrameType    = FrameType.Method5CGear,
                InitialFrame = frameCGear,
                MaxResults   = 1
            };

            GenderFilter genderFilter = checkBoxGenderless.Checked
                                            ? new GenderFilter("Genderless", 0xFF, GenderCriteria.DontCareGenderless)
                                            : new GenderFilter("Gendered", 0, GenderCriteria.DontCareGenderless);

            var possibleDates = new List <DateTime>();
            //  Loop through all months
            for (int month = 1; month <= 12; month++)
            {
                int daysInMonth = DateTime.DaysInMonth(generateYear, month);

                //  Loop through all days
                for (int day = 1; day <= daysInMonth; day++)
                {
                    //  Loop through all minutes
                    for (int minute = 0; minute <= 59; minute++)
                    {
                        //  Loop through all seconds
                        for (int second = 0; second <= 59; second++)
                        {
                            if (ab != ((month * day + minute + second) % 0x100))
                            {
                                continue;
                            }
                            var dateTime = new DateTime(generateYear, month, day, hour, minute, second);

                            // Standard seed time will be the C-Gear seed time, minus the delay
                            // We'll skip seeds that cross over into the next day and cause unwanted advances
                            // Added calibration to the delay to account for the fact that people aren't perfectly fast
                            DateTime possibleDate = dateTime.AddSeconds(offset);
                            if (dateTime.Day == possibleDate.Day)
                            {
                                possibleDates.Add(possibleDate);
                            }
                        }
                    }
                }
            }

            // Generate the IVs for the corresponding C-Gear seed first

            var rngIVs = new uint[6];

            frameCompare = new FrameCompare(
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                null,
                -1,
                false,
                false,
                false,
                null,
                new NoGenderFilter());

            List <Frame> IVs = generator.Generate(frameCompare, 0, 0);
            if (IVs.Count > 0)
            {
                rngIVs[0] = IVs[0].Hp;
                rngIVs[1] = IVs[0].Atk;
                rngIVs[2] = IVs[0].Def;
                rngIVs[3] = IVs[0].Spa;
                rngIVs[4] = IVs[0].Spd;
                rngIVs[5] = IVs[0].Spe;
            }

            // Now that each combo box item is a custom object containing the FrameType reference
            // We can simply retrieve the FrameType from the selected item
            generator.FrameType     = FrameType.Method5Natures;
            generator.EncounterType = EncounterType.Entralink;
            generator.RNGIVs        = rngIVs;

            generator.InitialFrame = minFrame;
            generator.MaxResults   = maxFrame - minFrame + 1;

            frameCompare = new FrameCompare(
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                null,
                -1,
                false,
                false,
                false,
                null,
                new NoGenderFilter());

            frameCompare = new FrameCompare(
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                0, CompareType.None,
                natures,
                -1,
                false,
                false,
                false,
                null,
                genderFilter);

            #endregion

            waitHandle = new EventWaitHandle(true, EventResetMode.ManualReset);

            jobs = new Thread[cpus];
            //divide the possible times into even groups
            int split = possibleDates.Count / cpus;
            for (int i = 0; i < cpus; ++i)
            {
                List <DateTime> dates = i < cpus - 1
                                           ? possibleDates.GetRange(i * split, split)
                                           : possibleDates.GetRange(i * split, split + possibleDates.Count % cpus);
                //if the last i make sure we add the remainder as well
                // technically supposed to copy profile and send in a copy because now the threads are
                // using a reference to the same profile but that's fine because the profile isn't getting
                // mutated anyway
                jobs[i] =
                    new Thread(
                        () =>
                        Generate(generator.Clone(), dates, minFrame, maxFrame,
                                 profile, groupSize, calibratedDelay));
                jobs[i].Start();
            }

            listBindingCap = new BindingSource {
                DataSource = iframes
            };
            dataGridViewCapValues.DataSource = listBindingCap;


            progressTotal =
                (ulong)
                (maxFrame - minFrame + 1) * (profile.Timer0Max - profile.Timer0Min + 1) * (ulong)keypresses.Count *
                (ulong)possibleDates.Count;
            var progressJob =
                new Thread(() => ManageProgress(listBindingCap, dataGridViewCapValues, generator.FrameType, 0));
            progressJob.Start();
            progressJob.Priority = ThreadPriority.Lowest;

            buttonSeedGenerate.Enabled = false;
        }