Skip to content

Add a new bot

Damien Brebion edited this page Dec 6, 2023 · 3 revisions

This section will help you to create a new bot for the tool.

Step 1: Create the bot user control

Create a user control for your bot inside Panels folder. The base size for bot user control is X: 750 & Y: 280. Add custom field, combo box, .. If you want to use the PokemonFilterPanel that contains all the field and the comparator for pokemon, you should add it during the InitializeComponent of your user control. That's because if you include it directly inside your user control, if we made an update of the pokemon filter panel, it will cause errors.

Step 2: Create class

Create your class that implement the interface IBot.

Some information about events:

  • PokemonEncountered must be called when a pokemon is encountered (only 1 time)
  • PokemonFound must be called when a pokemon is found (only 1 time)
  • StateChanged must be called when the bot enabled state changed

Some information about methods & properties:

  • Enabled define if the bot is running or not
  • UseDelay define if the bot must use a delay (inside settings tab). Generally true for every bot (except if you need to use frame)
  • Start is the method that set enabled to true. It have to check if the bot can start. Otherwise, it must throw a BotException.
  • Stop is the method that set enabled to false.
  • Execute is the method called every frame or every delay (depends of the UseDelay method). Generally, this method call the runner.
  • GetPanel must return a UserControl (that's the interface use inside the tool window) - created at step 1
  • UpdateUI is the method used to update panel if necessary (be aware that can cause the bot to slowdown)

Execute method generally get and control elements and parameters. Then it send everything to the runner and the runner execute the action and return false if it's not finished (like the player is still moving or in action) and return true when the bot should start checking if it can stop or not. Here is an example from starter bot:

        public void Execute(PlayerData playerData, GameState state)
        {
            //Get starter
            var starter = GameVersion.VersionInfo.Starters.FirstOrDefault(x => x.PokemonId == Control.FilterPanel.Comparator.IndexPokemon);
            //Execute starter action runner (open bag, select starter and confirm choice)
            //Then return true when the starter is selected
            if (starter != null && GameVersion.Runner.ExecuteStarter(starter.PositionIndex))
            {
                //Now check if the pokemon has our selection criteria
                var pokemon = GameVersion.Memory.GetParty()[0];
                PokemonEncountered?.Invoke(pokemon);
                if (Control.FilterPanel.Comparator.Compare(pokemon))
                {
                    Log.Info(Messages.Pokemon_Found);
                    PokemonFound?.Invoke(pokemon);
                    Stop();
                }
                else
                {
                    if (APIContainer.EmuClient.LoadOrStop(GetSaveStateName()))
                    {
                        UpdateRNG();
                    }
                }
            }
        }

Step 3: Add new symbols (faculative)

If you need to use new symbols you can edit the IMemory class but you also need to edit all the classes that inherites this interace (even if you throw NotImplementedException). At the end, when you create your pull request you must specify all the symbols that you add to the tool so others developers can adapt their games to works with your bot.

Step 4: Add this bot to config

Inside app.config in the section BotTypes add the name of your bot and a code.

Step 5: Add the added code

Inside BotCode enumeration add your bot code. Then in BotFactory, add your bot to the switch statement.

Step 6: Final & Tests

Now everything should works. Run the tool and check it out. Follow these steps to test:

  • Go to bot tab and check if your bot is inside the combo box
  • Select your bot and check if your panel correctly show
  • Click start and check if the bot is running
  • Click start and check if the bot execute correctly and stop when you specify it
  • Click stop and check if the bot stop

If everything is working you can create a pull request and someone will review your code ! Be aware, you should specify in the pull request if you use new symbol that wasn't use before. This will happen if you have changed the IMemory interace to add new features.

Thank you for helping Pokebot develop! 🥳

Clone this wiki locally