This article describes the battle AI (artificial intelligence). That is, how computer-controlled opponents in battle (both wild Pokémon and enemy trainers) choose which moves to use and which items to use when. This article assumes the AI is for an enemy trainer, although it applies for wild Pokémon and partner trainers too.

This is a complicated topic, and unnecessary for most users. Do not attempt to modify the AI unless you have decent scripting knowledge.

Levels of AI

Better trainers should be cleverer than worse trainers. The way the AI decides how good a trainer is is by looking at how much money the player will receive by defeating them - the more money they give out, the better a trainer they are, and the better their AI is.

AI overview

The first thing the AI needs to do each turn is decide whether to use an attack, use an item, or switch their Pokémon with another one. This decision starts in the script section PokeBattle_Battle, in the def pbDefaultChooseEnemyCommand.

Firstly, the AI decides whether it should use an item (PokeBattle_BattlepbEnemyItemToUse). This def goes through each item the AI has, and decides whether to use it. There are separate clauses in this decision for each item the AI has been programmed to recognise - it can recognise (and therefore use) only the basic battle items. Note the order in which each recognised item is checked, e.g. Full Restore takes priority over Full Heal.

If the AI decides not to use an item, then it decides whether it can/should withdraw the Pokémon instead (PokeBattle_AIpbEnemyShouldWithdrawEx?). This def looks at several factors in turn, including how good the player's Pokémon's moves will do against it, whether it can use any moves itself, whether it is about to die of toxic poisoning or Perish Song, and so forth. This decision can flip-flop as it runs through each factor. Then the def decides which other Pokémon is the best choice to switch in with, and at the end does so.

If the AI won't switch Pokémon either, then it needs to choose a move to use (PokeBattle_AIpbChooseMoves). For each available move in turn, it calculates a score depending on various factors, including the function code and effectiveness of that move, the currently active abilities, move effects and weather, the user's held item and status, and how many Pokémon the player has left. Then it compares the scores for each move, and chooses which move to use depending on these scores. If there are no good moves to choose from, then there is another opportunity to switch the Pokémon out.

Just before it chooses a move to use, the AI will decide if it should Mega Evolve their Pokémon. By default, it will always choose to do so if possible.

Item recognition and effects

The AI needs to be programmed to recognise items. Currently it only recognises the basic Potions, Heal items and X stat raisers. This recognition is done in the script section PokeBattle_Battle, in the def pbEnemyItemToUse.

Items used by the AI also need effects - they are defined separately from the effects used when the player uses them. These effects are listed in the def pbEnemyUseItem, just below the above def. These effects should be the same as if the player had used them, for fairness.