This page will look up trainer battle AI procedures for the Generation IV Pokémon games – Pokémon Diamond, Pearl, Platinum, HeartGold, and SoulSilver. Trainer AI between each of these games is nearly identical, with only Diamond and Pearl having a single small difference from Platinum, HeartGold, and SoulSilver (in Icy Wind / Mud Shot / Rock Tomb!).
I recommend you read over this whole article at least once to be made aware of all the strange quirks and edge cases that affect the AI procedures.
The Battle AI uses a scoring system to determine which move to use (and which target to use it on, if applicable). Each move starts with a score of 100 (but this starting point is arbitrary), which is then added or subtracted to by the different parts of the scoring procedure.
Every move is scored on every target on the field (even if that doesn't make sense for the move, such as Swords Dance or Earthquake), with scores being independent for each target. After all scoring is completed, the move and target with the highest score is selected. If multiple moves tie for the highest score, one will be selected from them at random. In double battles, a move is selected for each target first, then if the scores between targets are tied, a target is selected at random.
Scoring is skipped if the target is fainted, or if the move being scored cannot be selected due to being out of PP or locked out by Taunt, Torment, Choice items, Encore, etc. However, procedures that compare one move to another, such as to check which deals more damage, do not consider if other moves are locked out of use.
The scoring process is nondeterministic: a given move and target may result in a different score each time it is checked due to randomness within the scoring functions.
See also: side article on switch-in and withdrawal AI.
The scoring procedure is split into four main modules: Basic, Strong, Expert, and Doubles (others exist as well, but they are not used for Frontier battles).
As a general rule:
Basic AI discourages using moves without effect: stat-boosting moves when already at +6, healing moves when already at full HP, Fling when not holding any item, etc.
Strong AI encourages doing the most damage possible and going for a KO if one is available.
Expert AI encourages using smarter strategies on a per-move basis.
Doubles AI discourages using attacks on the ally without a good reason, and checks if moves such as weather or Trick Room will help or harm the ally.
Doubles AI is split into two parts: a part for checking attacks against enemies, and a part for checking if there is a reason to attack the ally.
None, one, or several of these modules run their scoring procedure depending upon the context of the battle. Each module runs independently, and their scores stack together. Only Doubles AI will attempt to use moves on the ally. The "terminate" keyword within a module means that the scoring of the module is complete, but other modules may still continue on top of the work that it did.
In the Battle Frontier, the AI is enabled as follows:
Battle Tower: Always Basic + Strong + Expert AI.
Battle Hall:
Battle Factory, Battle Castle, and Battle Arcade:
"No AI" means that moves and targets are selected purely at random. Doubles AI is always enabled during double / multi battles, regardless of what other AI is enabled.
The AI always knows what item you're holding. It cheats to see it.
The AI always knows your exact current HP and max HP.
The AI does not know your moves until it sees you use them. Other methods that expose moves, such as Sleep Talk or the Forewarn ability, do not count.
The AI does not know your ability until it sees a text box with the ability name, such as: "... makes ground moves miss using LEVITATE", or "... FLASH FIRE made Flamethrower useless". If the AI does not know your ability, then most times it tries to check what your ability is, it will randomly guess one of the possible abilities your Pokémon's species can normally have. Abilities that modify damage but do not generate text, like Heatproof or Solid Rock, are not known to the AI even after damage is dealt. However, the AI is aware of the reduced damage that will be inflicted (e.g., for a Heatproof Bronzong, it will assume Levitate 50% of the time, but also will know that the Bronzong may survive a high-damage Fire attack that would KO if it had Levitate).
Rarely, the AI must specifically see your ability, or your species must not have any other possible ability, in order for a check to succeed; these cases are worded as "If the target's ability is certainly...".
There is one exception to this: the AI knows if your ability is Shadow Tag, Magnet Pull, or Arena Trap preventing it from switching.
The AI always knows the attack order of all Pokémon on the field, barring speed ties or Quick Claws. It knows if there will be a speed tie, but does not know who will win it. If the AI is checking if it will attack before or after another target, and there is a speed tie, it will randomly guess the outcome of the tie. For any Pokémon on the field with a Quick Claw, it will randomly guess the Quick Claw will activate 20% of the time, independent of if the Quick Claw will actually activate.
The AI always knows the exact abilities, items, and moves of itself and its ally.
If you switch out, the AI will forget its knowledge of your moves and abilities.
When the AI checks for move effectiveness, it does so regardless of if the move in question is actually damaging.
If the user has the ability Normalize, the move's type is changed to Normal. Otherwise, if the move is Natural Gift, Hidden Power, Judgment, or Weather Ball, the move is adjusted to its correct type.
The effectiveness multiplier is initialized to 1x, and then first adjusted by:
STAB
Adaptability
The move's calculated type is then checked against the target's type(s), applying:
Scrappy
Mold Breaker
Odor Sleuth / Foresight
Gastro Acid
Miracle Eye
Roost / Ingrain / Iron Ball / Gravity
Magnet Rise / Levitate
Wonder Guard
Subsequently, if the move deals damage at all (including OHKO moves, countering moves, etc.), the check applies:
Tinted Lens
Expert Belt
Filter / Solid Rock
The resulting multiplier is returned as the total effectiveness of the move.
All of these other factors are always ignored:
Water Absorb
Flash Fire
Motor Drive
Volt Absorb
Dry Skin
Soundproof
Damp
If the move deals fixed damage
The effectiveness check specifically cheats and checks if your ability is Filter, Solid Rock, Levitate, or Wonder Guard, and returns an appropriate multiplier. This means that, for example, if you have a Levitate Bronzong, the AI will see Earthquake as 0x effective. But, if you have a Heatproof Bronzong, the AI will see that Earthquake is 2x effective, but will still, 50% of the time, guess that it has Levitate and avoid picking it.
If the overall effectiveness as a result of this check is exactly 0.375x, 0.75x, 3x, or 6x, it makes the assumption that this must be STAB boosted and adjusts the effectiveness to 0.25x, 0.5x, 2x, and 4x, respectively. This adjustment occurs even if the move is not actually STAB boosted (for instance, 4x supereffective vs Solid Rock results in a 3x multiplier, which is then adjusted to 2x).
Therefore, when the AI runs a check on a condition such as "If the move is 4x supereffective...", this works normally for STAB and non-STAB moves, but may fail to trigger properly when Adaptability, Expert Belt, Tinted Lens, or Filter / Solid Rock are in effect.
When the AI checks how much damage a move will deal, it uses the game's normal and correct procedure for calculating damage for that move, but without any of the multipliers used on the output of damage calculation:
Critical hits
Life Orb / Metronome
STAB
Random variance
Type effectiveness
Solid Rock / Filter
Expert Belt
Tinted Lens
Damage-reducing berries
Instead, if the move does not deal fixed damage, the raw damage value is multiplied by the AI's effectiveness multiplier, with the step of adjusting STAB omitted. If the move deals fixed damage, it is properly checked for type immunities (i.e. Seismic Toss vs Ghosts) and then its fixed damage is returned as the result.
Importantly, the Life Orb and Metronome are ignored by the AI for purposes of checking the damage of a move.
Damage boosts in these situations are also ignored during damage calculation:
Recoil moves with the Reckless ability
Stomp against a target that has used Minimize
Facade while the user is statused
SmellingSalt against a paralyzed target
Wake-Up Slap against a sleeping target
Brine against a target that is under 50% HP
Surf or Whirlpool against a target using Dive
Gust or Twister against a target using Fly
Earthquake or Magnitude against a target using Dig
Rollout or Ice Ball after the user has used Defense Curl
Rollout when hitting consecutively
Fury Cutter when hitting consecutively
Weather Ball during sun, rain, hail, or sandstorm
Uniquely, damage calculation omits adjusting the type of Weather Ball, resulting in it possibly failing to properly receive STAB or type effectiveness.
The move Psywave generates a random power level for damage calculation.
The moves Future Sight and Doom Desire have their damage calculated applying STAB and effectiveness, ignoring their normal typeless properties.
Multi-strike moves only have a single strike considered.
Multipliers used for the input of damage calculation (such as: Choice items / Light Ball / Thick Club / type-boosting items, Light Screen / Reflect, Torrent / Huge Power / Thick Fat, weather type boosts, etc., etc.) are applied as normal. Embargo negating held item effects is also applied. For more information on this, check the Bulbapedia article on damage calculation, as well as the corresponding article for any specific ability or item to verify its exact mechanism of affecting the damage formula.
These moves are not accepted for damage calculation. Any attempt to calculate their damage will result in a damage of 0, except when checking the damage of the move previously used by another Pokémon (as in Copycat or Me First). If calculated in this way, moves with a nonregular base power such as Fling, Present, Flail, or Sheer Cold will be treated as having a base power of 1. Full HP will be assumed for Eruption and Water Spout. Magnitude uniquely will generate a random power level if its damage is calculated in this way.
Guillotine
Horn Drill
Counter
Fissure
Bide
Super Fang
Flail
Reversal
Present
Magnitude
Mirror Coat
Spit Up
Endeavor
Sheer Cold
Metal Burst
Fling
Trump Card
Wring Out
Punishment
Crush Grip
Selfdestruct
Explosion
Dream Eater
Razor Wind
Sky Attack
Hyper Beam
Hydro Cannon
Blast Burn
Frenzy Plant
Giga Impact
Rock Wrecker
Roar of Time
Skull Bash
SolarBeam
Spit Up
Focus Punch
Superpower
Eruption
Water Spout
Sucker Punch
Head Smash
When the AI checks something like "50% chance of score +1 and terminate", this means that the score change happens with probability 50%, but the termination happens regardless, unless noted otherwise.
When the AI checks for HP as a percentage, it first multiplies the current HP by 100, then divides by the max HP, then rounds down. Therefore, for example, a Blissey with 361/362 HP is considered to be at 99%, and 54/362 HP is considered to be at 14%. When checks specify "If the user's HP is over 20%...", this means the calculated HP percent must be 21-100.
Rarely, the AI will compare a Pokémon's HP to 0%; this is not the same thing as being fainted: a Pokémon with 1/101 HP is considered 0% after rounding.
When checking if a move deals damage, any move that deals damage in any way counts, including OHKO moves, fixed-damage moves, countering moves, and variable damage moves like Low Kick or Return.
When checking specifically if a Pokémon has a move to hit a target supereffectively, unlike the effectiveness multiplier calculation, this check actually works correctly in all situations by checking the flag that would generate the "It's super effective!" text. This means no Expert Belt, Filter / Solid Rock, Adaptability, fixed-damage move, or nondamaging move would interfere with the check.
The AI never considers the accuracy of a move.
If you are playing Pair / Link mode with another DS, and you are linking Diamond or Pearl with Platinum, HeartGold, or SoulSilver, then the PtHGSS game will always be in charge of battle AI decisions, due to having a newer version of the battle software.
If you specifically link Diamond or Pearl as the leader with Platinum, HeartGold, or SoulSilver as the guest, then an oversight in the battle setup will cause the trainers to have no AI except for Doubles.
There are multiple instances where a nonrandom check by the AI is executed multiple times, or is broken and never triggers properly; for clarity, these have been removed.
Many of these checks are implemented using a jumbled spaghetti system of branching and jumping; I have tried to transcribe them in a way that is as clear and concise as possible with little ambiguity in interpretation, but if something is worded unclearly, do contact me with requests for clarification.
Generally, read the checks from top to bottom, indenting in a block when the condition above is met, and stop whenever a branch reaches a "terminate".
Read the checks carefully! There are often times errors left in by the developers; for example, Expert AI scoring Facade checks if the target is afflicted by a status condition instead of the user.