Exemple #1
0
        /// <summary>
        /// Updates the value of the egg RNG seed.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string UpdateSeed(byte[] data)
        {
            string seed = BitConverter.ToString(data.Reverse().ToArray()).Replace("-", "");

            Delg.SetText(EggSeed, seed);
            return(seed);
        }
        private void RunStop_Click(object sender, System.EventArgs e)
        {
            DisableControls();
            if (botworking)
            { // Stop bot
                Delg.SetEnabled(RunStop, false);
                Delg.SetText(RunStop, "Start Bot");
                botworking = false;
                userstop   = true;
            }
            else
            {
                string modemessage;
                switch (Mode.SelectedIndex)
                {
                case 0:
                    modemessage = "Simple: This bot will produce " + Eggs.Value + " eggs and deposit them in the pc, starting at the first available slot in box " + Box.Value + ".\r\n\r\n";
                    break;

                case 1:
                    modemessage = "Filter: This bot will start producing egss and deposit them in the pc, starting at the first available slot in box " + Box.Value + ". Then it will check against the selected filters and if it finds a match the bot will stop. The bot will also stop if it produces " + Eggs.Value + " eggs before finding a match.\r\n\r\n";
                    break;

                case 2:
                    modemessage = "ESV/TSV: This bot will start producing egss and deposit them in the pc, starting at the first available slot in box " + Box.Value + ". Then it will check the egg's ESV and if it finds a match with the values in the TSV list, the bot will stop. The bot will also stop if it produces " + Eggs.Value + " eggs before finding a match.\r\n\r\n";
                    break;

                case 3:
                    modemessage = "Accept/Reject: This bot will talk to the Nursery Lady and accept " + Accept.Value + " eggs, then it will reject " + Reject.Value + " eggs and stop.\r\n\r\n";
                    break;

                default:
                    modemessage = "No mode selected. Select one and try again.\r\n\r\n";
                    break;
                }
                DialogResult dialogResult;
                dialogResult = MessageBox.Show("This bot will start producing eggs from the Nursery using the following rules:\r\n\r\n" + modemessage + "Make sure that your party is full and the Party/Box option is set to Automatic. Please read the Wiki at Github before starting. Do you want to continue?", "Breeding bot", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dialogResult == DialogResult.OK && Mode.SelectedIndex >= 0)
                {
                    // Configure GUI
                    Delg.SetText(RunStop, "Stop Bot");
                    // Initialize variables
                    botworking   = true;
                    userstop     = false;
                    botState     = BotState.botstart;
                    attempts     = 0;
                    maxreconnect = 10;
                    // Run the bot
                    Program.gCmdWindow.SetBotMode(true);
                    RunBot();
                }
                else
                {
                    EnableControls();
                }
            }
        }
Exemple #3
0
        private async void Fill6()
        {
            SetControls(false);
            Delg.SetText(TB_Name, await waitName());
            Delg.SetValue(Num_TID, await waitTID());
            Delg.SetValue(Num_SID, await waitSID());
            Delg.SetValue(Num_Money, await waitMoney());
            Delg.SetValue(Num_Miles, await waitMiles());
            Delg.SetValue(Num_BP, await waitBP());
            Delg.SetSelectedValue(CB_Language, await waitLang());
            byte[] time = await waitTime();

            Delg.SetValue(Num_Hour, BitConverter.ToUInt16(time, 0));
            Delg.SetValue(Num_Min, time[2]);
            Delg.SetValue(Num_Sec, time[3]);
            Delg.SetEnabled(ReloadFields, true);
        }
 private void RunStop_Click(object sender, EventArgs e)
 {
     DisableControls();
     if (botworking)
     { // Stop bot
         Delg.SetEnabled(RunStop, false);
         Delg.SetText(RunStop, "Start Bot");
         botworking = false;
         userstop   = true;
     }
     else
     { // Run bot
         DialogResult dialogResult = MessageBox.Show("This scirpt will try to Wonder Trade " + Trades.Value + " pokémon, starting from the slot " + Slot.Value + " of box " + Box.Value + ". Remember to read the wiki for this bot in GitHub before starting.\r\n\r\nDo you want to continue?", "Wonder Trade Bot", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
         if (dialogResult == DialogResult.Yes && Trades.Value > 0)
         {
             if (Program.gCmdWindow.HaX)
             {
                 MessageBox.Show("Illegal mode enabled. If using a WT folder mode, it will write any pokémon to the game, regardless of legality. It will also attempt to Wonder Trade illegal pokémon it finds.", "Illegal mode", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }
             // Configure GUI
             Delg.SetText(RunStop, "Stop Bot");
             // Initialize variables
             botworking   = true;
             userstop     = false;
             botstate     = BotState.botstart;
             attempts     = 0;
             maxreconnect = 10;
             boxchange    = true;
             startbox     = Box.Value;
             startslot    = Slot.Value;
             starttrades  = Trades.Value;
             currentfile  = 0;
             tradewait    = 0;
             pklist       = new List <PKM> {
             };
             // Run the bot
             Program.gCmdWindow.SetBotMode(true);
             RunBot();
         }
         else
         {
             EnableControls();
         }
     }
 }
Exemple #5
0
        private void handleDataReady(object sender, DataReadyEventArgs e)
        {
            if (e.seq != querySeq) //somebody else's data
            {
                return;
            }

            recvData = e.data;
            if (DumpSaveCB.Checked)
            {
                string folderPath = @Application.StartupPath + "\\Digger\\";
                (new FileInfo(folderPath)).Directory.Create();
                string fileName = Path.Combine(folderPath, "dump " + StartAddrText.Text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bin");
                File.WriteAllBytes(fileName, recvData);
            }
            //Thanks to this, processing will be done in GUI thread
            //so we don't need to use SetBlahblahblah functions everywhere
            Delg.SetText(UglyMultithreadingHack, "b");
        }
Exemple #6
0
        /// <summary>
        /// Bot procedure.
        /// </summary>
        public async void RunBot()
        {
            try
            {
                Program.gCmdWindow.SetBotMode(true);
                while (botworking && Program.gCmdWindow.IsConnected)
                {
                    switch (botState)
                    {
                    case (int)BotState.BotStart:
                        Report("Bot: START Gen 7 Breding bot");
                        if (Mode.SelectedIndex >= 0 && Mode.SelectedIndex != 3 &&
                            Eggs.Value > 0)
                        {
                            Delg.SetValue(Slot, 1);
                            botState = BotState.SelectBox;
                        }
                        else if (Mode.SelectedIndex == 3 && (Accept.Value > 0 ||
                                                             Reject.Value > 0))
                        {
                            botState = BotState.EggSeed;
                        }
                        else
                        {
                            botresult = ErrorMessage.Finished;
                            botState  = BotState.BotExit;
                        }
                        break;

                    case BotState.SelectBox:
                        Report("Bot: Set start box");
                        waitTaskbool = Program.helper.waitNTRwrite(LookupTable
                                                                   .CurrentboxOffset, (uint)GetIndex(Box),
                                                                   Program.gCmdWindow.pid);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.ReadSlot;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.WriteError;
                            botState  = BotState.SelectBox;
                        }
                        break;

                    case BotState.ReadSlot:
                        Report("Bot: Search for empty slot");
                        waitTaskPKM = Program.helper.waitPokeRead(Box, Slot);
                        breedPoke   = await waitTaskPKM;
                        if (breedPoke == null)
                        {     // No data or invalid
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.ReadSlot;
                        }
                        else if (breedPoke.Species == 0)
                        {     // Empty space
                            Report("Bot: Empty slot");
                            attempts = 0;
                            botState = BotState.EggSeed;
                        }
                        else
                        {
                            GetNextSlot();
                            botState = BotState.ReadSlot;
                        }
                        break;

                    case BotState.EggSeed:
                        Report("Bot: Update Egg seed");
                        waitTaskbool = Program.helper.waitNTRmultiread(eggseedOff,
                                                                       0x10);
                        if (await waitTaskbool)
                        {
                            Report("Bot: Current seed - " + UpdateSeed(Program.helper
                                                                       .lastmultiread));
                            attempts = 0;
                            if (Mode.SelectedIndex != 3)
                            {
                                botState = BotState.GenerateEgg;
                            }
                            else
                            {
                                if (Accept.Value == 0 && Reject.Value == 0)
                                {
                                    botresult = ErrorMessage.Finished;
                                    botState  = BotState.BotExit;
                                }
                                else
                                {
                                    botState = BotState.GenerateEgg;
                                }
                            }
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.EggSeed;
                        }
                        break;

                    case BotState.GenerateEgg:
                        Report("Bot: Produce Egg in Nursery");
                        waitTaskbool = Program.helper.waitNTRwrite(EggOff, 0x01,
                                                                   Program.gCmdWindow.pid);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.TriggerDialog;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.WriteError;
                            botState  = BotState.GenerateEgg;
                        }
                        break;

                    case BotState.TriggerDialog:
                        Report("Bot: Start dialog");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.ButtonA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.TestDialog1;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.TriggerDialog;
                        }
                        break;

                    case BotState.TestDialog1:
                        Report("Bot: Test if dialog has started");
                        waitTaskbool = Program.helper.memoryinrange(DialogOff,
                                                                    DialogIn, 0x10000000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.ContinueDialog;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.TriggerDialog;
                        }
                        break;

                    case BotState.ContinueDialog:
                        Report("Bot: Continue dialog");
                        int maxi;
                        if (Mode.SelectedIndex == 3 && Accept.Value == 0)
                        {
                            key  = LookupTable.ButtonB;
                            maxi = 9;
                        }
                        else
                        {
                            key  = LookupTable.ButtonA;
                            maxi = 6;
                        }
                        int i;
                        for (i = 0; i < maxi; i++)
                        {
                            waitTaskbool = Program.helper.waitbutton(key);
                            if (!(await waitTaskbool))
                            {
                                break;
                            }
                        }
                        if (i == 6)
                        {
                            botState = BotState.CheckNoEgg;
                        }
                        if (i == 9)
                        {
                            botState = BotState.TestDialog2;
                        }
                        else
                        {
                            botState = BotState.FixDialog;
                        }
                        break;

                    case BotState.FixDialog:
                        waitTaskbool = Program.helper.waitbutton(key);
                        if (await waitTaskbool)
                        {
                            botState = BotState.CheckNoEgg;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.FixDialog;
                        }
                        break;

                    case BotState.CheckNoEgg:
                        waitTaskbool = Program.helper.memoryinrange(EggOff, 0x00,
                                                                    0x01);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            Report("Bot: Egg received");
                            botState = BotState.ExitDialog;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.FixDialog;
                        }
                        break;

                    case BotState.ExitDialog:
                        Report("Bot: Exit dialog");
                        await Task.Delay(1500);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.ButtonB);
                        if (await waitTaskbool)
                        {
                            waitTaskbool = Program.helper.waitbutton(LookupTable
                                                                     .ButtonB);
                            if (await waitTaskbool)
                            {
                                botState = BotState.TestDialog2;
                            }
                            else
                            {
                                attempts++;
                                botresult = ErrorMessage.ButtonError;
                                botState  = BotState.ExitDialog;
                            }
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.ExitDialog;
                        }
                        break;

                    case BotState.TestDialog2:
                        waitTaskbool = Program.helper.memoryinrange(DialogOff,
                                                                    DialogOut, 0x10000000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            Report("Bot: Dialog finished");
                            if (Mode.SelectedIndex != 3)
                            {
                                botState = BotState.Filter;
                            }
                            else
                            {
                                if (Accept.Value > 0)
                                {
                                    Delg.SetValue(Accept, Accept.Value - 1);
                                }
                                else
                                {
                                    Delg.SetValue(Reject, Reject.Value - 1);;
                                }
                                botState = BotState.EggSeed;
                            }
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.ExitDialog;
                        }
                        break;

                    case BotState.Filter:
                        bool testsok = false;
                        Report("Bot: Read recevied egg");
                        waitTaskPKM = Program.helper.waitPokeRead(Box, Slot);
                        breedPoke   = await waitTaskPKM;
                        if (breedPoke == null)
                        {     // No data or invalid
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.Filter;
                        }
                        else if (breedPoke.Species == 0)
                        {     // Empty space
                            Report("Bot: Error detected - slot is empty");
                            attempts  = 11;
                            botresult = ErrorMessage.GeneralError;
                            botState  = BotState.BotExit;
                        }
                        else
                        {
                            attempts = 0;
                            Delg.SetValue(Eggs, Eggs.Value - 1);
                            if (ReadESV.Checked || Mode.SelectedIndex == 2)
                            {
                                Delg.DataGridViewAddRow(esvList, Box.Value,
                                                        Slot.Value, breedPoke.PSV.ToString("D4"));
                                if (Mode.SelectedIndex == 2)
                                {
                                    testsok = ESV_TSV_check(breedPoke.PSV);
                                }
                            }
                            if (Mode.SelectedIndex == 1)
                            {
                                filternum = CheckFilters(breedPoke, filterList);
                                testsok   = filternum > 0;
                            }
                        }
                        if (testsok)
                        {
                            botState = BotState.TestsPassed;
                            break;
                        }
                        else if (Eggs.Value > 0)
                        {
                            GetNextSlot();
                            botState = BotState.ReadSlot;
                        }
                        else
                        {
                            if (Mode.SelectedIndex == 1 || Mode.SelectedIndex == 2)
                            {
                                Report("Bot: No match found");
                                botresult = ErrorMessage.NoMatch;
                            }
                            else
                            {
                                botresult = ErrorMessage.Finished;
                            }
                            botState = BotState.BotExit;
                        }
                        break;

                    case BotState.TestsPassed:
                        if (Mode.SelectedIndex == 1)
                        {
                            Report("Bot: All tests passed");
                            botresult        = ErrorMessage.FilterMatch;
                            finishmessage[0] = (int)Box.Value;
                            finishmessage[1] = (int)Slot.Value;
                            finishmessage[2] = filternum;
                        }
                        else if (Mode.SelectedIndex == 2)
                        {
                            Report("Bot: ESV/TSV match found");
                            botresult        = ErrorMessage.SVMatch;
                            finishmessage[0] = (int)Box.Value;
                            finishmessage[1] = (int)Slot.Value;
                            finishmessage[2] = breedPoke.PSV;
                        }
                        botState = BotState.BotExit;
                        break;

                    case BotState.BotExit:
                        Report("Bot: STOP Gen 7 Breding bot");
                        botworking = false;
                        break;

                    default:
                        Report("Bot: STOP Gen 7 Breding bot");
                        botresult  = ErrorMessage.GeneralError;
                        botworking = false;
                        break;
                    }
                    if (attempts > 10)
                    { // Too many attempts
                        if (maxreconnect > 0)
                        {
                            Report("Bot: Try reconnection to fix error");
                            waitTaskbool = Program.gCmdWindow.Reconnect();
                            maxreconnect--;
                            if (await waitTaskbool)
                            {
                                await Task.Delay(2500);

                                attempts = 0;
                            }
                            else
                            {
                                botresult  = ErrorMessage.GeneralError;
                                botworking = false;
                            }
                        }
                        else
                        {
                            Report("Bot: Maximum number of reconnection attempts reached");
                            Report("Bot: STOP Gen 7 Breeding bot");
                            botworking = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Report("Bot: Exception detected:");
                Report(ex.Source);
                Report(ex.Message);
                Report(ex.StackTrace);
                Report("Bot: STOP Gen 7 Breeding bot");
                MessageBox.Show(ex.Message);
                botworking = false;
                botresult  = ErrorMessage.GeneralError;
            }
            if (userstop)
            {
                botresult = ErrorMessage.UserStop;
            }
            else if (!Program.gCmdWindow.IsConnected)
            {
                botresult = ErrorMessage.Disconnect;
            }
            ShowResult("Breeding bot", botresult, finishmessage);
            Delg.SetText(RunStop, "Start Bot");
            Program.gCmdWindow.SetBotMode(false);
            EnableControls();
            Delg.SetEnabled(RunStop, true);
        }
        private void RunStop_Click(object sender, System.EventArgs e)
        {
            DisableControls();
            if (botworking)
            { // Stop bot
                Delg.SetEnabled(RunStop, false);
                Delg.SetText(RunStop, "Start Bot");
                botworking = false;
                userstop   = true;
            }
            else
            {
                string typemessage;
                switch (Mode.SelectedIndex)
                {
                case 0:
                    typemessage = "Event - Make sure you are in front of the man in the Pokémon Center. Also, you must only have one pokémon in your party.";
                    Program.gCmdWindow.SetRadioParty();
                    Program.gCmdWindow.SetResetLabel("Number of resets:");
                    break;

                case 1:
                    typemessage = "Type: Null - Make sure you are in front of Gladion at the Aether Paradise. Also, you must only have one pokémon in your party.\r\n\r\nThis mode can also be used for event pokémon.";
                    Program.gCmdWindow.SetRadioParty();
                    Program.gCmdWindow.SetResetLabel("Number of resets:");
                    break;

                case 2:
                    typemessage = "Tapus - Make sure you are in front of the statue at the ruins.";
                    Program.gCmdWindow.SetRadioOpponent();
                    Program.gCmdWindow.SetResetLabel("Number of resets:");
                    break;

                case 3:
                    typemessage = "Solgaleo/Lunala - Make sure you are in front of Solgaleo/Lunala at the Altar of the Sunne/Moone.";
                    Program.gCmdWindow.SetRadioOpponent();
                    Program.gCmdWindow.SetResetLabel("Number of resets:");
                    break;

                case 4:
                    typemessage = "Wild Pokémon - Make sure you are in the place where wild pokémon can appear. Also, check that Honey is the item at the top of your Item list and can be selected by just opening the menu and pressing A.";
                    Program.gCmdWindow.SetRadioOpponent();
                    Program.gCmdWindow.SetResetLabel("Total Encounters:");
                    break;

                case 5:
                    typemessage = "Ultra Beast/Necrozma - Make sure you are in the place where the Ultra Beast / Necrozma appears. Also, check that Honey is the item at the top of your Item list and can be selected by just opening the menu and pressing A.";
                    Program.gCmdWindow.SetRadioOpponent();
                    Program.gCmdWindow.SetResetLabel("Total Encounters:");
                    break;

                default:
                    typemessage = "No type - Select one type of soft-reset and try again.";
                    break;
                }

                DialogResult dialogResult = MessageBox.Show("This bot will trigger an encounter with a pokémon, and soft-reset if it doesn't match with the loaded filters.\r\n\r\nType: " + typemessage + "\r\n\r\nPlease read the wiki at GitHub before using this bot. Do you want to continue?", "Soft-reset bot", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dialogResult == DialogResult.OK && Mode.SelectedIndex >= 0)
                {
                    // Configure GUI
                    Delg.SetText(RunStop, "Stop Bot");
                    // Initialize variables
                    botworking    = true;
                    userstop      = false;
                    botState      = BotState.botstart;
                    attempts      = 0;
                    maxreconnect  = 10;
                    resetNo       = Program.gCmdWindow.GetResetNumber();
                    isub          = false;
                    honeynum      = 0;
                    finishmessage = null;
                    // Run the bot
                    Program.gCmdWindow.SetBotMode(true);
                    RunBot();
                }
                else
                {
                    EnableControls();
                }
            }
        }
        public async void RunBot()
        {
            try
            {
                while (botworking && Program.gCmdWindow.IsConnected)
                {
                    switch (botState)
                    {
                    case BotState.botstart:
                        Report("Bot: START Gen 7 Soft-reset bot");
                        botState = BotState.selectmode;
                        break;

                    case BotState.selectmode:
                        switch (Mode.SelectedIndex)
                        {
                        case 0:
                        case 1:
                            botState = BotState.startdialog;
                            break;

                        case 2:
                            botState = BotState.triggerbattle;
                            break;

                        case 3:
                            resetNo = resetNo == 0 ? 1 : resetNo;
                            Program.gCmdWindow.UpdateResetCounter(resetNo);
                            botState = BotState.soluna1;
                            break;

                        case 4:
                        case 5:
                            resetNo = resetNo == 0 ? 1 : resetNo;
                            Program.gCmdWindow.UpdateResetCounter(resetNo);
                            botState = BotState.writehoney;
                            break;

                        default:
                            botState = BotState.botexit;
                            break;
                        }
                        break;

                    case BotState.startdialog:
                        Report("Bot: Start dialog");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testdialog1;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.startdialog;
                        }
                        break;

                    case BotState.testdialog1:
                        Report("Bot: Test if dialog has started");
                        waitTaskbool = Program.helper.memoryinrange(dialogOff, dialogIn, 0x10000000);
                        if (await waitTaskbool)
                        {
                            if (Mode.SelectedIndex == 1)
                            {
                                attempts = -40;     // Type:Null dialog is longer
                            }
                            else
                            {
                                attempts = -15;
                            }
                            botState = BotState.continuedialog;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.startdialog;
                        }
                        break;

                    case BotState.continuedialog:
                        Report("Bot: Continue dialog");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testdialog2;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.continuedialog;
                        }
                        break;

                    case BotState.testdialog2:
                        Report("Bot: Test if dialog has finished");
                        waitTaskbool = Program.helper.memoryinrange(dialogOff, dialogOut, 0x10000000);
                        if (await waitTaskbool)
                        {
                            attempts = -10;
                            botState = BotState.readparty;
                        }
                        else if (Program.helper.lastRead >= 0x3F000000 && Program.helper.lastRead < 0x40000000)
                        {
                            attempts = -10;
                            botState = BotState.exitdialog;
                        }
                        else if (Program.helper.lastRead >= 0x3D000000 && Program.helper.lastRead < 0x3E000000)
                        {
                            attempts = 0;
                            botState = BotState.nickname;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.continuedialog;
                        }
                        break;

                    case BotState.exitdialog:
                        Report("Bot: Exit dialog");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyB);
                        if (await waitTaskbool)
                        {
                            botState = BotState.readparty;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.exitdialog;
                        }
                        break;

                    case BotState.readparty:
                        Report("Bot: Try to read party");
                        waitTaskPKM = Program.helper.waitPartyRead(2);
                        srPoke      = await waitTaskPKM;
                        if (srPoke == null)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.exitdialog;
                        }
                        else if (srPoke.Species == 0)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.exitdialog;
                        }
                        else
                        {
                            attempts = 0;
                            botState = BotState.filter;
                        }
                        break;

                    case BotState.filter:
                        filternum = CheckFilters(srPoke, filterList);
                        bool testsok = filternum > 0;
                        if (testsok)
                        {
                            botState = BotState.testspassed;
                        }
                        else if (Mode.SelectedIndex == 3 || Mode.SelectedIndex == 4 || Mode.SelectedIndex == 5)
                        {
                            botState = BotState.runbattle1;
                        }
                        else
                        {
                            botState = BotState.softreset;
                        }
                        break;

                    case BotState.testspassed:
                        Report("Bot: All tests passed!");
                        if (Mode.SelectedIndex == 3 || Mode.SelectedIndex == 4 || Mode.SelectedIndex == 5)
                        {
                            botresult = ErrorMessage.BattleMatch;
                        }
                        else
                        {
                            botresult = ErrorMessage.SRMatch;
                        }
                        finishmessage = new int[] { filternum, resetNo };
                        botState      = BotState.botexit;
                        break;

                    case BotState.softreset:
                        resetNo++;
                        Report("Bot: Sof-reset #" + resetNo.ToString());
                        Program.gCmdWindow.UpdateResetCounter(resetNo);
                        waitTaskbool = Program.helper.waitSoftReset();
                        if (await waitTaskbool)
                        {
                            botState = BotState.skiptitle;
                        }
                        else
                        {
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.botexit;
                        }
                        break;

                    case BotState.skiptitle:
                        await Task.Delay(7000);

                        Report("Bot: Open Menu");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.reconnect;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.skiptitle;
                        }
                        break;

                    case BotState.reconnect:
                        Report("Bot: Try reconnect");
                        waitTaskbool = Program.gCmdWindow.Reconnect();
                        if (await waitTaskbool)
                        {
                            await Task.Delay(1000);

                            botState = BotState.connpatch;
                        }
                        else
                        {
                            botresult = ErrorMessage.GeneralError;
                            botState  = BotState.botexit;
                        }
                        break;

                    case BotState.connpatch:
                        Report("Bot: Apply NFC patch");
                        waitTaskbool = Program.helper.waitNTRwrite(LookupTable.nfcOff, LookupTable.nfcVal, Program.gCmdWindow.pid);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.startgame;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.WriteError;
                            botState  = BotState.connpatch;
                        }
                        break;

                    case BotState.startgame:
                        Report("Bot: Start the game");
                        await Task.Delay(1000);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            await Task.Delay(3000);

                            attempts = 0;
                            botState = BotState.selectmode;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.startgame;
                        }
                        break;

                    case BotState.nickname:
                        Report("Bot: Nickname screen");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keySTART);
                        await Task.Delay(250);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)

                        {
                            botState = BotState.testdialog2;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.nickname;
                        }
                        break;

                    case BotState.triggerbattle:
                        Report("Bot: Try to trigger battle");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testdialog3;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.triggerbattle;
                        }
                        break;

                    case BotState.testdialog3:
                        Report("Bot: Test if dialog has started");
                        waitTaskbool = Program.helper.memoryinrange(dialogOff, dialogIn, 0x10000000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.continuedialog2;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.triggerbattle;
                        }
                        break;

                    case BotState.continuedialog2:
                        Report("Bot: Continue dialog");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.readopp;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.continuedialog2;
                        }
                        break;

                    case BotState.readopp:
                        Report("Bot: Try to read opponent");
                        srPoke      = null;
                        waitTaskPKM = Program.helper.waitPokeRead(opponentOff);
                        srPoke      = await waitTaskPKM;
                        if (srPoke == null)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.readopp;
                        }
                        else if (srPoke.Species == 0)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.continuedialog2;
                        }
                        else
                        {
                            attempts = 0;
                            botState = BotState.filter;
                        }
                        break;

                    case BotState.soluna1:
                        Report("Bot: Walk to legendary pokemon");
                        waitTaskbool = Program.helper.waitsitck(0, 100);
                        if (await waitTaskbool)
                        {
                            botState = BotState.soluna2;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.StickError;
                            botState  = BotState.soluna1;
                        }
                        break;

                    case BotState.soluna2:
                        Report("Bot: Trigger battle #" + resetNo);
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.soluna3;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.soluna2;
                        }
                        break;

                    case BotState.soluna3:
                        Report("Bot: Test if dialog has started");
                        waitTaskbool = Program.helper.memoryinrange(dialogOff, dialogIn, 0x10000000);
                        if (await waitTaskbool)
                        {
                            attempts = -0;
                            botState = BotState.soluna4;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.soluna2;
                        }
                        break;

                    case BotState.soluna4:
                        Report("Bot: Test if data is available");
                        waitTaskbool = Program.helper.timememoryinrange(battleOff, battleIn, 0x10000, 1000, 20000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.soluna5;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.soluna1;
                        }
                        break;

                    case BotState.soluna5:
                        Report("Bot: Try to read opponent");
                        srPoke      = null;
                        waitTaskPKM = Program.helper.waitPokeRead(opponentOff);
                        srPoke      = await waitTaskPKM;
                        if (srPoke == null)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.soluna5;
                        }
                        else if (srPoke.Species == 0)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.soluna2;
                        }
                        else
                        {
                            attempts = 0;
                            botState = BotState.filter;
                        }
                        break;

                    case BotState.runbattle1:
                        Report("Bot: Run from battle");
                        await Task.Delay(2000);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.DpadDOWN);
                        if (await waitTaskbool)
                        {
                            botState = BotState.runbattle2;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.runbattle1;
                        }
                        break;

                    case BotState.runbattle2:
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.runbattle3;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.runbattle2;
                        }
                        break;

                    case BotState.runbattle3:
                        Report("Bot: Test out from battle");
                        waitTaskbool = Program.helper.timememoryinrange(battleOff, battleOut, 0x10000, 1000, 10000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            resetNo++;
                            Program.gCmdWindow.UpdateResetCounter(resetNo);
                            if (Mode.SelectedIndex == 3)
                            {
                                botState = BotState.soluna1;
                                await Task.Delay(6000);
                            }
                            else if (isub)
                            {
                                botState = BotState.dismissmsg;
                                await Task.Delay(6000);
                            }
                            else
                            {
                                botState = BotState.writehoney;
                                await Task.Delay(5000);
                            }
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.runbattle1;
                        }
                        break;

                    case BotState.writehoney:
                        if (honeynum >= 10)
                        {
                            botState = BotState.openmenu;
                        }
                        else
                        {
                            Report("Bot: Give 999 honey");
                            waitTaskbool = Program.helper.waitNTRwrite(itemOff, honey, Program.gCmdWindow.pid);
                            if (await waitTaskbool)
                            {
                                attempts = 0;
                                honeynum = 999;
                                botState = BotState.openmenu;
                            }
                            else
                            {
                                attempts++;
                                botresult = ErrorMessage.WriteError;
                                botState  = BotState.writehoney;
                            }
                        }
                        break;

                    case BotState.openmenu:
                        Report("Bot: Open Menu");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyX);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testmenu;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.openmenu;
                        }
                        break;

                    case BotState.testmenu:
                        Report("Bot: Test if the menu is open");
                        waitTaskbool = Program.helper.timememoryinrange(menuOff, menuIn, 0x10000000, 1000, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.openbag;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.openmenu;
                        }
                        break;

                    case BotState.openbag:
                        Report("Bot: Open Bag");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testbag;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.openbag;
                        }
                        break;

                    case BotState.testbag:
                        Report("Bot: Test if the bag is open");
                        waitTaskbool = Program.helper.timememoryinrange(bagOff, bagIn, 0x10000, 1000, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.selecthoney;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.openbag;
                        }
                        break;

                    case BotState.selecthoney:
                        Report("Bot: Select Honey");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.activatehoney;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.selecthoney;
                        }
                        break;

                    case BotState.activatehoney:
                        Report("Bot: Trigger battle #" + resetNo);
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testwild;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.openbag;
                        }
                        break;

                    case BotState.testwild:
                        Report("Bot: Test if battle is triggered");
                        waitTaskbool = Program.helper.timememoryinrange(bagOff, bagOut, 0x10000, 1000, 10000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            honeynum--;
                            botState = BotState.waitwild;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.activatehoney;
                        }
                        break;

                    case BotState.waitwild:
                        Report("Bot: Test if data is available");
                        waitTaskbool = Program.helper.timememoryinrange(battleOff, battleIn, 0x10000, 1000, 20000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.readwild;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.activatehoney;
                        }
                        break;

                    case BotState.readwild:
                        Report("Bot: Try to read opponent");
                        srPoke      = null;
                        waitTaskPKM = Program.helper.waitPokeRead(opponentOff);
                        srPoke      = await waitTaskPKM;
                        if (srPoke == null)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.readwild;
                        }
                        else if (srPoke.Species == 0)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.readwild;
                        }
                        else if (srPoke.Species == WinFormsUtil.getIndex(Species))
                        {
                            attempts = 0;
                            isub     = Mode.SelectedIndex == 5;
                            botState = BotState.filter;
                        }
                        else
                        {
                            attempts = 0;
                            isub     = false;
                            botState = BotState.runbattle1;
                        }
                        break;

                    case BotState.dismissmsg:
                        Report("Bot: Dismiss message");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.writehoney;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.dismissmsg;
                        }
                        break;

                    case BotState.botexit:
                        Report("Bot: STOP Gen 7 Soft-reset bot");
                        botworking = false;
                        break;

                    default:
                        Report("Bot: STOP Gen 7 Soft-reset bot");
                        botresult  = ErrorMessage.GeneralError;
                        botworking = false;
                        break;
                    }
                    if (attempts > 10)
                    { // Too many attempts
                        if (maxreconnect > 0)
                        {
                            Report("Bot: Try reconnection to fix error");
                            waitTaskbool = Program.gCmdWindow.Reconnect();
                            maxreconnect--;
                            if (await waitTaskbool)
                            {
                                await Task.Delay(2500);

                                attempts = 0;
                            }
                            else
                            {
                                botresult  = ErrorMessage.GeneralError;
                                botworking = false;
                            }
                        }
                        else
                        {
                            Report("Bot: Maximum number of reconnection attempts reached");
                            Report("Bot: STOP Gen 7 Soft-reset bot");
                            botworking = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Report("Bot: Exception detected:");
                Report(ex.Source);
                Report(ex.Message);
                Report(ex.StackTrace);
                Report("Bot: STOP Gen 6 Soft-reset bot");
                MessageBox.Show(ex.Message);
                botworking = false;
                botresult  = ErrorMessage.GeneralError;
            }
            if (userstop)
            {
                botresult = ErrorMessage.UserStop;
            }
            else if (!Program.gCmdWindow.IsConnected)
            {
                botresult = ErrorMessage.Disconnect;
            }
            ShowResult("Soft-reset bot", botresult, finishmessage);
            Delg.SetText(RunStop, "Start Bot");
            Program.gCmdWindow.SetBotMode(false);
            EnableControls();
            Delg.SetEnabled(RunStop, true);
        }
Exemple #9
0
        public async void RunBot()
        {
            try
            {
                Program.gCmdWindow.SetBotMode(true);
                while (botworking && Program.gCmdWindow.IsConnected)
                {
                    switch (botState)
                    {
                    case BotState.botstart:
                        Report("Bot: START Gen 6 Soft-reset bot");
                        switch (Mode.SelectedIndex)
                        {
                        case 0:
                            botState = Resume.Checked ? BotState.trigger : BotState.pssmenush;
                            break;

                        case 1:
                            botState = Resume.Checked ? BotState.miragespot : BotState.pssmenush;
                            break;

                        case 2:
                            botState = Resume.Checked ? BotState.tev_start : BotState.pssmenush;
                            break;

                        case 3:
                            botState = Resume.Checked ? BotState.trigger : BotState.fixwifi;
                            break;

                        case 4:
                            if (Resume.Checked)
                            {
                                steps    = 0;
                                walk     = true;
                                botState = BotState.twk_start;
                            }
                            else
                            {
                                steps    = 0;
                                walk     = true;
                                botState = BotState.pssmenush;
                            }
                            break;

                        case 5:
                            soaringX    = 30;
                            soaringY    = -100;
                            soaringTime = 4000;
                            if (Resume.Checked)
                            {
                                botState = BotState.soaring_start;
                            }
                            else
                            {
                                botState = BotState.pssmenush;
                            }
                            break;

                        case 6:
                            soaringX    = 0;
                            soaringY    = 100;
                            soaringTime = 5000;
                            if (Resume.Checked)
                            {
                                botState = BotState.soaring_start;
                            }
                            else
                            {
                                botState = BotState.pssmenush;
                            }
                            break;

                        default:
                            botState = BotState.botexit;
                            break;
                        }
                        break;

                    case BotState.pssmenush:
                        Report("Bot: Test if the PSS menu is shown");
                        waitTaskbool = Program.helper.memoryinrange(psssmenu1Off, psssmenu1IN, 0x10000);
                        if (await waitTaskbool)
                        {
                            botState = BotState.fixwifi;
                        }
                        else
                        {
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.botexit;
                        }
                        break;

                    case BotState.fixwifi:
                        waitTaskbool = Program.helper.waitNTRwrite(0x0105AE4, 0x4770, 0x1A);
                        if (await waitTaskbool)
                        {
                            botState = Mode.SelectedIndex == 3 ? BotState.trigger : BotState.touchpssset;
                        }
                        else
                        {
                            botresult = ErrorMessage.WriteError;
                            Report("Bot: Error detected");
                            attempts = 11;
                        }
                        break;

                    case BotState.touchpssset:
                        Report("Bot: Touch Box View");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waittouch(240, 180);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testpssset;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botState  = BotState.touchpssset;
                        }
                        break;

                    case BotState.testpssset:
                        Report("Bot: Test if the PSS setings are shown");
                        waitTaskbool = Program.helper.timememoryinrange(pssettingsOff, pssettingsIN, 0x10000, 100, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.touchpssdis;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.touchpssset;
                        }
                        break;

                    case BotState.touchpssdis:
                        Report("Bot: Touch Disable PSS communication");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waittouch(160, pssdisableY);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testpssdis;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botState  = BotState.touchpssdis;
                        }
                        break;

                    case BotState.testpssdis:
                        Report("Bot: Test if PSS disable confirmation appears");
                        waitTaskbool = Program.helper.timememoryinrange(pssdisableOff, pssdisableIN, 0x10000, 100, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.touchpssconf;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.touchpssdis;
                        }
                        break;

                    case BotState.touchpssconf:
                        Report("Bot: Touch Yes");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waittouch(160, 120);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testpssout;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botState  = BotState.touchpssconf;
                        }
                        break;

                    case BotState.testpssout:
                        Report("Bot: Test if back to PSS screen");
                        waitTaskbool = Program.helper.timememoryinrange(pssettingsOff, pssettingsOUT, 0x10000, 100, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.returncontrol;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.touchpssconf;
                        }
                        break;

                    case BotState.returncontrol:
                        Report("Bot: Return contol to character");
                        await Task.Delay(6 *commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.touchsave;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.returncontrol;
                        }
                        break;

                    case BotState.touchsave:
                        Report("Bot: Touch Save button");
                        await Task.Delay(4 *commanddelay);

                        waitTaskbool = Program.helper.waittouch(220, 220);
                        if (await waitTaskbool)
                        {
                            botState = BotState.testsave;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botState  = BotState.touchsave;
                        }
                        break;

                    case BotState.testsave:
                        Report("Bot: Test if the save screen is shown");
                        waitTaskbool = Program.helper.timememoryinrange(savescrnOff, savescrnIN, 0x10000, 100, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.saveconf;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.touchsave;
                        }
                        break;

                    case BotState.saveconf:
                        Report("Bot: Press Yes");
                        await Task.Delay(8 *commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.saveout;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.saveconf;
                        }
                        break;

                    case BotState.saveout:
                        Report("Bot: Test if the save screen is not shown");
                        waitTaskbool = Program.helper.timememoryinrange(savescrnOff, savescrnOUT, 0x10000, 100, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            if (Mode.SelectedIndex == 2)
                            {
                                Report("Bot: Soft-reset for party data intialize");
                                botState = BotState.softreset;
                            }
                            else
                            {
                                botState = BotState.typesr;
                            }
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.saveconf;
                        }
                        break;

                    case BotState.typesr:
                        switch (Mode.SelectedIndex)
                        {
                        case 0:
                            botState = BotState.trigger;
                            break;

                        case 1:
                            botState = BotState.miragespot;
                            break;

                        case 2:
                            botState = BotState.tev_start;
                            break;

                        case 3:
                            botState = BotState.trigger;
                            break;

                        case 4:
                            botState = BotState.twk_start;
                            break;

                        case 5:
                        case 6:
                            botState = BotState.soaring_start;
                            break;

                        default:
                            botState = BotState.trigger;
                            break;
                        }
                        break;

                    case BotState.trigger:
                        Report("Bot: Try to trigger encounter");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.readopp;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.trigger;
                        }
                        break;

                    case BotState.miragespot:
                        Report("Bot: Try to trigger encounter in mirage spot");
                        int i;
                        for (i = 0; i < 4; i++)
                        {
                            await Task.Delay(2 *commanddelay);

                            waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                            if (!(await waitTaskbool))
                            {
                                break;
                            }
                        }
                        if (i == 4)
                        {
                            botState = BotState.readopp;
                        }
                        else
                        {
                            botState = BotState.trigger;
                        }
                        break;

                    case BotState.readopp:
                        Report("Bot: Try to read opponent");
                        srpoke = null;
                        await Task.Delay(2 *commanddelay);      // Wait for pokémon data

                        waitTaskPKM = Program.gCmdWindow.ReadOpponent();
                        srpoke      = await waitTaskPKM;
                        if (srpoke == null)
                        {     // No data received
                            if (walk)
                            {
                                steps++;
                                Report("Steps: " + steps);
                                if (steps >= 10)
                                {
                                    steps = 0;
                                    attempts++;
                                    botresult = ErrorMessage.ButtonError;
                                    botState  = BotState.twk_start;
                                }
                                else
                                {
                                    attempts++;
                                    botresult = ErrorMessage.ReadError;
                                    botState  = BotState.trigger;
                                }
                            }
                            else
                            {
                                attempts++;
                                botresult = ErrorMessage.ReadError;
                                botState  = BotState.trigger;
                            }
                        }
                        else if (srpoke.Species > 0)
                        {
                            attempts = 0;
                            botState = BotState.filter;
                        }
                        break;

                    case BotState.filter:
                        if (walk)
                        {
                            steps = 0;
                        }
                        filternum = CheckFilters(srpoke, filterList);
                        bool testsok = filternum > 0;
                        if (testsok)
                        {
                            botState = BotState.testspassed;
                        }
                        else
                        {
                            botState = BotState.softreset;
                        }
                        break;

                    case BotState.testspassed:
                        Report("Bot: All tests passed!");
                        finishmessage = new int[] { filternum, resetNo };
                        botresult     = ErrorMessage.SRMatch;
                        botState      = BotState.botexit;
                        break;

                    case BotState.softreset:
                        resetNo++;
                        Report("Bot: Soft-reset #" + resetNo.ToString());
                        Program.gCmdWindow.UpdateResetCounter(resetNo);
                        waitTaskbool = Program.helper.waitSoftReset();
                        if (await waitTaskbool)
                        {
                            botState = BotState.skipintro;
                        }
                        else
                        {
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.botexit;
                        }
                        break;

                    case BotState.skipintro:
                        await Task.Delay(44 *commanddelay);

                        Report("Bot: Skip intro cutscene");
                        Program.helper.quickbuton(LookupTable.keyA, commandtime);
                        await Task.Delay(commandtime + commanddelay);

                        botState = ORAS ? BotState.skiptitle : BotState.startgame;
                        break;

                    case BotState.skiptitle:
                        await Task.Delay(20 *commanddelay);

                        Report("Bot: Skip title screen");
                        Program.helper.quickbuton(LookupTable.keyA, commandtime);
                        await Task.Delay(commandtime + commanddelay);

                        botState = BotState.startgame;
                        break;

                    case BotState.startgame:
                        await Task.Delay(24 *commanddelay);

                        Report("Bot: Start game");
                        Program.helper.quickbuton(LookupTable.keyA, commandtime);
                        await Task.Delay(commandtime + commanddelay);

                        botState = BotState.reconnect;
                        break;

                    case BotState.reconnect:
                        await Task.Delay(16 *commanddelay);

                        waitTaskbool = Program.gCmdWindow.Reconnect();
                        if (await waitTaskbool)
                        {
                            await Task.Delay(8 *commanddelay);

                            botState = BotState.typesr;
                        }
                        else
                        {
                            botresult = ErrorMessage.GeneralError;
                            botState  = BotState.botexit;
                        }
                        break;

                    case BotState.tev_start:
                        Report("Bot: Trigger Dialog");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.tev_dialog;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.tev_start;
                        }
                        break;

                    case BotState.tev_dialog:
                        Report("Bot: Test if dialog has started");
                        waitTaskbool = Program.helper.timememoryinrange(dialogOff, dialogIN, 0x01, 100, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.tev_cont1;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.tev_start;
                        }
                        break;

                    case BotState.tev_cont1:
                        Report("Bot: Talk to lady");
                        await Task.Delay(2 *commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyB);
                        if (await waitTaskbool)
                        {
                            botState = BotState.tev_check;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.tev_cont1;
                        }
                        break;

                    case BotState.tev_check:
                        Report("Bot: Try to read party");
                        waitTaskPKM = Program.helper.waitPartyRead(2);
                        srpoke      = await waitTaskPKM;
                        if (srpoke == null)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.tev_check;
                        }
                        else if (srpoke.Species == 0)
                        {      // No data or invalid
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.tev_cont1;
                        }
                        else
                        {
                            attempts = 0;
                            botState = BotState.filter;
                        }
                        break;

                    case BotState.twk_start:
                        Report("Bot: Walk one step");
                        await Task.Delay(4 *commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.runUP);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.trigger;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.twk_start;
                        }
                        break;

                    case BotState.soaring_start:
                        Report("Bot: Activate Eon Flute");
                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyY);
                        if (await waitTaskbool)
                        {
                            botState = BotState.soaring_cont;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.soaring_start;
                        }
                        break;

                    case BotState.soaring_cont:
                        Report("Bot: Start cutscene");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            botState = BotState.soaring_check;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.soaring_cont;
                        }
                        break;

                    case BotState.soaring_check:
                        await Task.Delay(8000);

                        Report("Bot: Test if ready to move");
                        waitTaskbool = Program.helper.timememoryinrange(soaringOff, soaringIN, 0x10000, 1000, 10000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botState = BotState.soaring_move;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.soaring_start;
                        }
                        break;

                    case BotState.soaring_move:
                        Report("Bot: Start movement");
                        Program.helper.quickstick(soaringX, soaringY, soaringTime);
                        await Task.Delay(soaringTime + 2 *commanddelay);

                        botState = BotState.soaring_dialog;
                        break;

                    case BotState.soaring_dialog:
                        Report("Bot: Test dialog has started");
                        waitTaskbool = Program.helper.timememoryinrange(soaringdialogOff, soaringdialogIN, 0x10, 500, 5000);
                        if (await waitTaskbool)
                        {
                            botState = BotState.miragespot;
                        }
                        else
                        {
                            Report("Bot: Dialog failed, imposible to check position to continue, will check random encounter");
                            botState = BotState.random_enc_check;
                        }
                        break;

                    case BotState.random_enc_check:
                        Report("Bot: Test if random sky encounter");
                        srpoke = null;
                        await Task.Delay(2 *commanddelay);      // Wait for pokémon data

                        waitTaskPKM = Program.gCmdWindow.ReadOpponent();
                        srpoke      = await waitTaskPKM;
                        if (srpoke == null)
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botState  = BotState.random_enc_check;
                        }
                        if (srpoke.Species > 0)
                        {
                            botState = BotState.flee1;
                        }
                        else
                        {
                            Report("Bot: No random encounter, bot cannot continue");
                            botresult = ErrorMessage.GeneralError;
                            botState  = BotState.botexit;
                        }

                        break;

                    case BotState.flee1:
                        Report("Bot: Fleeing from random encounter, pressing down");
                        await Task.Delay(20 *commanddelay);         //30?

                        waitTaskbool = Program.helper.waitbutton(LookupTable.DpadDOWN);
                        if (await waitTaskbool)
                        {
                            botState = BotState.flee2;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.flee1;
                        }
                        break;

                    case BotState.flee2:
                        Report("Bot: Fleeing from random encounter, pressing right");
                        await Task.Delay(10 *commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.DpadRIGHT);
                        if (await waitTaskbool)
                        {
                            botState = BotState.flee3;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.flee2;
                        }
                        break;

                    case BotState.flee3:
                        Report("Bot: Fleeing from random encounter, pressing A");
                        await Task.Delay(commanddelay);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.keyA);
                        if (await waitTaskbool)
                        {
                            await Task.Delay(8 *commanddelay);

                            botState = BotState.soaring_move;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botState  = BotState.flee3;
                        }
                        break;

                    case BotState.botexit:
                        Report("Bot: STOP Gen 6 Soft-reset bot");
                        botworking = false;
                        break;

                    default:
                        Report("Bot: STOP Gen 6 Soft-reset bot");
                        botresult  = ErrorMessage.GeneralError;
                        botworking = false;
                        break;
                    }
                    if (attempts > 10)
                    { // Too many attempts
                        if (maxreconnect > 0)
                        {
                            Report("Bot: Try reconnection to fix error");
                            waitTaskbool = Program.gCmdWindow.Reconnect();
                            maxreconnect--;
                            if (await waitTaskbool)
                            {
                                await Task.Delay(10 *commanddelay);

                                attempts = 0;
                            }
                            else
                            {
                                botresult  = ErrorMessage.GeneralError;
                                botworking = false;
                            }
                        }
                        else
                        {
                            Report("Bot: Maximum number of reconnection attempts reached");
                            Report("Bot: STOP Gen 6 Soft-reset bot");
                            botworking = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Report("Bot: Exception detected:");
                Report(ex.Source);
                Report(ex.Message);
                Report(ex.StackTrace);
                Report("Bot: STOP Gen 6 Soft-reset bot");
                MessageBox.Show(ex.Message);
                botworking = false;
                botresult  = ErrorMessage.GeneralError;
            }
            if (userstop)
            {
                botresult = ErrorMessage.UserStop;
            }
            else if (!Program.gCmdWindow.IsConnected)
            {
                botresult = ErrorMessage.Disconnect;
            }
            ShowResult("Soft-reset bot", botresult, finishmessage);
            Delg.SetText(RunStop, "Start Bot");
            Program.gCmdWindow.SetBotMode(false);
            EnableControls();
            Delg.SetEnabled(RunStop, true);
        }
Exemple #10
0
        private void RunStop_Click(object sender, System.EventArgs e)
        {
            DisableControls();
            if (botworking)
            { // Stop bot
                Delg.SetEnabled(RunStop, false);
                Delg.SetText(RunStop, "Start Bot");
                botworking = false;
                userstop   = true;
            }
            else
            {
                string typemessage;
                string resumemessage;
                switch (Mode.SelectedIndex)
                {
                case 0:
                    typemessage   = "Regular - Make sure you are in front of the pokémon.";
                    resumemessage = "In front of pokémon, will press A to trigger start the battle";
                    Program.gCmdWindow.SetRadioOpponent();
                    break;

                case 1:
                    typemessage   = "Mirage Spot - Make sure you are in front of the hole.";
                    resumemessage = "In front of hole, will press A to trigger dialog";
                    Program.gCmdWindow.SetRadioOpponent();
                    break;

                case 2:
                    typemessage   = "Event - Make sure you are in front of the lady in the Pokémon Center. Also, you must only have one pokémon in your party.";
                    resumemessage = "In front of the lady, will press A to trigger dialog";
                    Program.gCmdWindow.SetRadioParty();
                    break;

                case 3:
                    typemessage   = "Groudon/Kyogre - You must disable the PSS communications manually due PokéNav malfunction. Go in front of Groudon/Kyogre and save game before starting the battle.";
                    resumemessage = "In front of Groudon/Kyogre, will press A to trigger dialog";
                    Program.gCmdWindow.SetRadioOpponent();
                    break;

                case 4:
                    typemessage   = "Walk - Make sure you are one step south of the pokémon.";
                    resumemessage = "One step south of the pokémon, will press up to trigger dialog";
                    Program.gCmdWindow.SetRadioOpponent();
                    break;

                case 5:
                    typemessage   = "Dialga/Palkia/Giratina - Make sure you are anywhere in Dewford Town and the Eon Flute is the only registered item.";
                    resumemessage = "In Dewford Town, will press Y to activate the Eon Flute";
                    Program.gCmdWindow.SetRadioOpponent();
                    break;

                case 6:
                    typemessage   = "Tornadus/Thundurus/Landorus - Make sure you are anywhere in Route 120 and the Eon Flute is the only registered item.";
                    resumemessage = "In Dewford Town, will press Y to activate the Eon Flute";
                    Program.gCmdWindow.SetRadioOpponent();
                    break;

                default:
                    typemessage   = "No type - Select one type of soft-reset and try again.";
                    resumemessage = "";
                    break;
                }
                DialogResult dialogResult = MessageBox.Show("This bot will trigger an encounter with a pokémon, and soft-reset if it doesn't match with the loaded filters.\r\n\r\nType: " + typemessage + "\r\nResume: " + resumemessage + "\r\n\r\nPlease read the wiki at GitHub before using this bot. Do you want to continue?", "Soft-reset bot", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dialogResult == DialogResult.OK && Mode.SelectedIndex >= 0)
                {
                    // Configure GUI
                    Delg.SetText(RunStop, "Stop Bot");
                    // Initialize variables
                    botworking    = true;
                    userstop      = false;
                    botState      = BotState.botstart;
                    attempts      = 0;
                    maxreconnect  = 10;
                    resetNo       = resetNo = Program.gCmdWindow.GetResetNumber();
                    walk          = false;
                    steps         = 0;
                    finishmessage = null;
                    // Run the bot
                    Program.gCmdWindow.SetBotMode(true);
                    RunBot();
                }
                else
                {
                    EnableControls();
                }
            }
        }
        public async void RunBot()
        {
            try
            {
                Program.gCmdWindow.SetBotMode(true);
                while (botworking && Program.gCmdWindow.IsConnected)
                {
                    switch (botstate)
                    {
                    case BotState.botstart:
                        Report("Bot: START Gen 6 Wonder Trade bot");
                        botstate = BotState.backup;
                        break;

                    case BotState.backup:
                        Report("Bot: Backup boxes");
                        waitTaskbool = Program.helper.waitNTRmultiread(pcpkmOff, 232 * 30 * 31);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            string fileName = "WTBefore-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".ek6";
                            backuppath = wtfolderpath + fileName;
                            Program.gCmdWindow.WriteDataToFile(Program.helper.lastmultiread, backuppath);
                            botstate = BotState.testpssmenu;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.backup;
                        }
                        break;

                    case BotState.testpssmenu:
                        Report("Bot: Test if the PSS menu is shown");
                        waitTaskbool = Program.helper.memoryinrange(psssmenu1Off, psssmenu1IN, 0x10000);
                        if (await waitTaskbool)
                        {
                            if (sourceBox.Checked)
                            {
                                botstate = BotState.readpoke;
                            }
                            else
                            {
                                botstate = BotState.readfolder;
                            }
                        }
                        else
                        {
                            botresult = ErrorMessage.NotInPSS;
                            botstate  = BotState.endbot;
                        }
                        break;

                    case BotState.readpoke:
                        Report("Bot: Look for pokemon to trade");
                        waitTaskPKM = Program.helper.waitPokeRead(Box, Slot);
                        WTpoke      = await waitTaskPKM;
                        if (WTpoke == null)
                        {     // No data or invalid
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.readpoke;
                        }
                        else if (WTpoke.Species == 0)
                        {     // Empty space
                            Report("Bot: Empty slot");
                            attempts = 0;
                            getNextSlot();
                        }
                        else
                        {     // Valid pkm, check legality
                            attempts = 0;
                            if (IsTradeable(WTpoke))
                            {
                                currentCHK = WTpoke.Checksum;
                                Report("Bot: Pokémon found - 0x" + currentCHK.ToString("X4"));
                                botstate = BotState.pressWTbutton;
                            }
                            else
                            {
                                if (Program.gCmdWindow.HaX)
                                {
                                    Report("Bot: Pokémon cannot be traded, is an egg or have special ribbons.");
                                }
                                else
                                {
                                    Report("Bot: Pokémon cannot be traded, is illegal or is an egg or have special ribbons.");
                                }
                                getNextSlot();
                            }
                        }
                        break;

                    case BotState.readfolder:
                        Report("Bot: Reading Wonder Trade folder");
                        pkfiles = Directory.GetFiles(wtfolderpath, "*.pk6");
                        if (pkfiles.Length > 0)
                        {
                            foreach (string pkf in pkfiles)
                            {
                                byte[] temp = File.ReadAllBytes(pkf);
                                if (temp.Length == 232)
                                {
                                    PK6 pkmn = new PK6(temp);
                                    if (IsTradeable(pkmn))
                                    {     // Legal pkm
                                        Report("Bot: Valid PK6 file");
                                        pklist.Add(pkmn);
                                    }
                                    else
                                    {     // Illegal pkm
                                        Report("Bot: File " + pkf + " cannot be traded");
                                    }
                                }
                                else
                                {     // Not valid file
                                    Report("Bot: File " + pkf + " is not a valid pk6 file");
                                }
                            }
                        }
                        if (pklist.Count > 0)
                        {
                            botstate = BotState.writefromfolder;
                        }
                        else
                        {
                            Report("Bot: No files detected");
                            botresult = ErrorMessage.Finished;
                            botstate  = BotState.endbot;
                        }
                        break;

                    case BotState.writefromfolder:
                        Report("Bot: Write pkm file from list");
                        if (sourceRandom.Checked)
                        {     // Select a random file
                            currentfile = RNG.Next() % pklist.Count;
                        }
                        waitTaskbool = Program.helper.waitNTRwrite(GetBoxOffset(pcpkmOff, Box, Slot), pklist[currentfile].EncryptedBoxData, Program.gCmdWindow.pid);
                        if (await waitTaskbool)
                        {
                            Program.gCmdWindow.UpdateDumpBoxes(Box, Slot);
                            Program.gCmdWindow.Pokemon = pklist[currentfile];
                            currentCHK = pklist[currentfile].Checksum;
                            if (sourceFolder.Checked)
                            {
                                currentfile++;
                                if (currentfile > pklist.Count - 1)
                                {
                                    currentfile = 0;
                                }
                            }
                            attempts = 0;
                            botstate = BotState.pressWTbutton;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.WriteError;
                            botstate  = BotState.writefromfolder;
                        }
                        break;

                    case BotState.pressWTbutton:
                        Report("Bot: Touch Wonder Trade button");
                        waitTaskbool = Program.helper.waittouch(240, 120);
                        if (await waitTaskbool)
                        {
                            botstate = BotState.testsavescrn;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botstate  = BotState.pressWTbutton;
                        }
                        break;

                    case BotState.testsavescrn:
                        Report("Bot: Test if the save screen is shown");
                        await Task.Delay(delaytime);

                        waitTaskbool = Program.helper.timememoryinrange(savescrnOff, savescrnIN, 0x10000, 500, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.confirmsave;
                        }
                        else
                        {     // If not in save screen, try again
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.pressWTbutton;
                        }
                        break;

                    case BotState.confirmsave:
                        Report("Bot: Press Yes");
                        await Task.Delay(delaytime);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.ButtonA);
                        if (await waitTaskbool)
                        {
                            botstate = BotState.testwtscrn;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botstate  = BotState.confirmsave;
                        }
                        break;

                    case BotState.testwtscrn:
                        Report("Bot: Test if Wonder Trade screen is shown");
                        waitTaskbool = Program.helper.timememoryinrange(wtconfirmationOff, wtconfirmationIN, 0x10000, 500, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.confirmwt;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.confirmsave;
                        }
                        break;

                    case BotState.confirmwt:
                        Report("Bot: Touch Yes");
                        await Task.Delay(delaytime);

                        waitTaskbool = Program.helper.waittouch(160, 100);
                        if (await waitTaskbool)
                        {
                            botstate = BotState.testboxes;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botstate  = BotState.confirmwt;
                        }
                        break;

                    case BotState.testboxes:
                        Report("Bot: Test if the boxes are shown");
                        waitTaskbool = Program.helper.timememoryinrange(wtboxesOff, wtboxesIN, 0x10000, 500, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.gotoboxchange;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.confirmwt;
                        }
                        break;

                    case BotState.gotoboxchange:
                        await Task.Delay(8 *delaytime);

                        if (boxchange)
                        {
                            botstate  = BotState.touchboxview;
                            boxchange = false;
                        }
                        else
                        {
                            botstate = BotState.touchpoke;
                        }
                        break;

                    case BotState.touchboxview:
                        Report("Bot: Touch Box View");
                        waitTaskbool = Program.helper.waittouch(30, 220);
                        if (await waitTaskbool)
                        {
                            botstate = BotState.testboxview;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botstate  = BotState.touchboxview;
                        }
                        break;

                    case BotState.testboxview:
                        Report("Bot: Test if box view is shown");
                        waitTaskbool = Program.helper.timememoryinrange(wtboxviewOff, wtboxviewIN, wtboxviewRange, 500, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.touchnewbox;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.touchboxview;
                        }
                        break;

                    case BotState.touchnewbox:
                        Report("Bot: Touch New Box");
                        await Task.Delay(delaytime);

                        waitTaskbool = Program.helper.waittouch(LookupTable.boxposX6[GetIndex(Box)], LookupTable.boxposY6[GetIndex(Box)]);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.selectnewbox;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.TouchError;
                            botstate  = BotState.touchboxview;
                        }
                        break;

                    case BotState.selectnewbox:
                        Report("Bot: Select New Box");
                        await Task.Delay(delaytime);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.ButtonA);
                        if (await waitTaskbool)
                        {
                            botstate = BotState.testboxviewout;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botstate  = BotState.confirmsave;
                        }
                        break;

                    case BotState.testboxviewout:
                        Report("Bot: Test if box view is not shown");
                        waitTaskbool = Program.helper.timememoryinrange(wtboxviewOff, wtboxviewOUT, wtboxviewRange, 500, 5000);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.touchpoke;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.touchnewbox;
                        }
                        break;

                    case BotState.touchpoke:
                        Report("Bot: Touch Pokémon");
                        await Task.Delay(delaytime);

                        waitTaskbool = Program.helper.waittouch(LookupTable.pokeposX6[GetIndex(Slot)], LookupTable.pokeposY6[GetIndex(Slot)]);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.selectrade;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.touchpoke;
                        }
                        break;

                    case BotState.selectrade:
                        Report("Bot: Select Trade");
                        await Task.Delay(2 *delaytime);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.ButtonA);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.confirmsend;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botstate  = BotState.selectrade;
                        }
                        break;

                    case BotState.confirmsend:
                        Report("Bot: Select Yes");
                        await Task.Delay(2 *delaytime);

                        waitTaskbool = Program.helper.waitbutton(LookupTable.ButtonA);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.testboxesout;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ButtonError;
                            botstate  = BotState.confirmsend;
                        }
                        break;

                    case BotState.testboxesout:
                        Report("Bot: Test if the boxes are not shown");
                        waitTaskbool = Program.helper.timememoryinrange(wtboxesOff, wtboxesOUT, 0x10000, 500, 5000);
                        if (await waitTaskbool)
                        {
                            attempts  = 0;
                            tradewait = 0;
                            botstate  = BotState.waitfortrade;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.ReadError;
                            botstate  = BotState.touchpoke;
                        }
                        break;

                    case BotState.waitfortrade:
                        Report("Bot: Wait for trade");
                        ushort newCHK;
                        waitTaskPKM = Program.helper.waitPokeRead(Box, Slot);
                        WTpoke      = await waitTaskPKM;
                        if (WTpoke == null)
                        {
                            newCHK = (ushort)(currentCHK + 1);
                        }
                        else if (WTpoke.Species == 0)
                        {
                            newCHK = (ushort)(currentCHK + 1);
                        }
                        else
                        {
                            newCHK = WTpoke.Checksum;
                        }
                        if (newCHK == currentCHK)
                        {
                            await Task.Delay(8 *delaytime);

                            tradewait++;
                            if (tradewait > 30)     // Too much time passed
                            {
                                attempts = 0;
                                botstate = BotState.notradepartner;
                            }
                        }
                        else
                        {
                            Report("Bot: Wait 30 seconds");
                            await Task.Delay(30000);

                            botstate = BotState.testbackpssmenu;
                        }
                        break;

                    case BotState.testbackpssmenu:
                        Report("Bot: Test if back to the PSS menu");
                        waitTaskbool = Program.helper.timememoryinrange(psssmenu1Off, psssmenu1IN, 0x10000, 2000, 10000);
                        if (await waitTaskbool)
                        {     // Trade sucessfull
                            attempts = 0;
                            getNextSlot();
                        }
                        else
                        {     // Still waiting
                            attempts++;
                            botresult = ErrorMessage.GeneralError;
                            botstate  = BotState.testbackpssmenu;
                        }
                        break;

                    case BotState.notradepartner:
                        Report("Bot: Test if back to the PSS menu");
                        waitTaskbool = Program.helper.timememoryinrange(psssmenu1Off, psssmenu1IN, 0x10000, 500, 3000);
                        if (await waitTaskbool)
                        {     // Back in menu
                            attempts = 0;
                            botstate = BotState.pressWTbutton;
                        }
                        else
                        {     // Still waiting
                            attempts++;
                            botresult = ErrorMessage.GeneralError;
                            Report("Bot: Select Yes");
                            Program.helper.quickbuton(LookupTable.ButtonA, commandtime);
                            await Task.Delay(commandtime + delaytime);

                            botstate = BotState.notradepartner;
                        }
                        break;

                    case BotState.dumpafter:
                        if (afterDump.Checked)
                        {
                            Report("Bot: Dump boxes");
                            waitTaskbool = Program.helper.waitNTRmultiread(pcpkmOff, 232 * 30 * 31);
                            if (await waitTaskbool)
                            {
                                attempts = 0;
                                string fileName = "WTAfter-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".ek6";
                                Program.gCmdWindow.WriteDataToFile(Program.helper.lastmultiread, wtfolderpath + fileName);
                                botstate = BotState.actionafter;
                            }
                            else
                            {
                                attempts++;
                                botresult = ErrorMessage.ReadError;
                                botstate  = BotState.dumpafter;
                            }
                        }
                        else
                        {
                            botstate = BotState.actionafter;
                        }
                        break;

                    case BotState.actionafter:
                        if (afterRestore.Checked)
                        {
                            botstate = BotState.restorebackup;
                        }
                        else if (afterDelete.Checked)
                        {
                            botstate = BotState.delete;
                        }
                        else
                        {
                            botresult = ErrorMessage.Finished;
                            botstate  = BotState.endbot;
                        }
                        break;

                    case BotState.restorebackup:
                        Report("Bot: Restore boxes backup");
                        byte[] restore = File.ReadAllBytes(backuppath);
                        if (restore.Length == 232 * 30 * 31)
                        {
                            waitTaskbool = Program.helper.waitNTRwrite(pcpkmOff, restore, Program.gCmdWindow.pid);
                            if (await waitTaskbool)
                            {
                                attempts  = 0;
                                botresult = ErrorMessage.Finished;
                                botstate  = BotState.endbot;
                            }
                            else
                            {
                                attempts++;
                                botresult = ErrorMessage.WriteError;
                                botstate  = BotState.restorebackup;
                            }
                        }
                        else
                        {
                            Report("Bot: Invalid boxes file");
                            botresult = ErrorMessage.GeneralError;
                            botstate  = BotState.endbot;
                        }
                        break;

                    case BotState.delete:
                        Report("Bot: Delete traded pokémon");
                        byte[] deletearray = new byte[232 * (int)starttrades];
                        for (int i = 0; i < starttrades; i++)
                        {
                            Program.gCmdWindow.SAV.BlankPKM.EncryptedBoxData.CopyTo(deletearray, i * 232);
                        }
                        waitTaskbool = Program.helper.waitNTRwrite(GetBoxOffset(pcpkmOff, Box, Slot), deletearray, Program.gCmdWindow.pid);
                        if (await waitTaskbool)
                        {
                            attempts = 0;
                            botstate = BotState.endbot;
                        }
                        else
                        {
                            attempts++;
                            botresult = ErrorMessage.WriteError;
                            botstate  = BotState.delete;
                        }
                        break;

                    case BotState.endbot:
                        Report("Bot: STOP Gen 6 Wonder Trade bot");
                        botworking = false;
                        break;

                    default:
                        Report("Bot: STOP Gen 6 Wonder Trade bot");
                        botresult  = ErrorMessage.GeneralError;
                        botworking = false;
                        break;
                    }
                    if (attempts > 10)
                    { // Too many attempts
                        if (maxreconnect > 0)
                        {
                            Report("Bot: Try reconnection to fix error");
                            waitTaskbool = Program.gCmdWindow.Reconnect();
                            maxreconnect--;
                            if (await waitTaskbool)
                            {
                                await Task.Delay(10 *delaytime);

                                attempts = 0;
                            }
                            else
                            {
                                botresult  = ErrorMessage.GeneralError;
                                botworking = false;
                            }
                        }
                        else
                        {
                            Report("Bot: Maximum number of reconnection attempts reached");
                            Report("Bot: STOP Gen 6 Wonder Trade bot");
                            botworking = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Report("Bot: Exception detected:");
                Report(ex.Source);
                Report(ex.Message);
                Report(ex.StackTrace);
                Report("Bot: STOP Gen 6 Wonder Trade bot");
                MessageBox.Show(ex.Message);
                botworking = false;
                botresult  = ErrorMessage.GeneralError;
            }
            if (userstop)
            {
                botresult = ErrorMessage.UserStop;
            }
            else if (!Program.gCmdWindow.IsConnected)
            {
                botresult = ErrorMessage.Disconnect;
            }
            ShowResult("Wonder Trade bot", botresult);
            Delg.SetText(RunStop, "Start Bot");
            Program.gCmdWindow.SetBotMode(false);
            EnableControls();
            Delg.SetEnabled(RunStop, true);
        }