Skip to main content
The battle engine is the largest subsystem in pokeemerald. It is turn-based, event-driven, and built around a central callback loop in src/battle_main.c. Each participant — player, opponent, link partner, Safari Zone — is managed by a dedicated battle controller.

Architecture

A battle proceeds through phases managed by function pointers in gBattlerControllerFuncs. The top-level callback BattleMainCB2 in src/battle_main.c dispatches to gBattleMainFunc every frame. The global gBattleStruct (type struct BattleStruct *) holds all per-battle transient state: turn trackers, switch targets, Safari counters, arena points, and more.

Battle controllers

Each of the up to four battler positions is served by a controller that handles input, animations, and communication. Controllers are assigned at battle start based on gBattleTypeFlags. The shared controller infrastructure lives in src/battle_controllers.c.

Battle actions

Each battler selects one action per turn. The chosen action is stored in gChosenActionByBattler[].

Move targeting

The MOVE_TARGET_* constants define which battlers a move can hit.

Battle script engine

Battle scripts are sequences of bytecode commands that drive battle flow: dealing damage, printing messages, playing animations, checking conditions, and branching. The interpreter advances gBattlescriptCurrInstr each frame.
Scripts are authored using macros from asm/macros/battle_script.inc.

Battle AI

The AI runs scripts from data/battle_ai_scripts.s and scores each move candidate.
aiFlags is loaded from trainer data and enables optional behaviors such as smart switching and item use prediction.

Battle animations

Every visual effect during a move is handled by animation source files organized by elemental type. Animation scripts live in data/battle_anim_scripts.s; the coordinator is src/battle_anim.c.

src/battle_anim_fire.c

Fire-type move visual effects.

src/battle_anim_water.c

Water-type move visual effects.

src/battle_anim_electric.c

Electric-type move visual effects.

src/battle_anim_ice.c

Ice-type move visual effects.

src/battle_anim_psychic.c

Psychic-type move visual effects.

src/battle_anim_ghost.c

Ghost-type move visual effects.

src/battle_anim_mon_movement.c

Pokémon sprite movement during animations.

src/battle_anim_status_effects.c

Status condition visual effects.
Additional animation files: battle_anim_dragon.c, battle_anim_bug.c, battle_anim_dark.c, battle_anim_fight.c, battle_anim_flying.c, battle_anim_ground.c, battle_anim_normal.c, battle_anim_poison.c, battle_anim_rock.c, battle_anim_throw.c, battle_anim_effects_1.c, battle_anim_effects_2.c, battle_anim_effects_3.c.

Battle Frontier

Each Battle Frontier facility has its own source file implementing facility-specific rules, party generation, and streak recording.

Battle Arena

src/battle_arena.c — three-turn judge-scored battles; awards Mind and Skill points.

Battle Dome

src/battle_dome.c — tournament bracket with scouting; uses BATTLE_TYPE_DOME.

Battle Factory

src/battle_factory.c — rental Pokémon battles with post-round swapping.

Battle Palace

src/battle_palace.c — AI selects moves based on nature; player has no direct move control.

Battle Pike

src/battle_pike.c — series of rooms with randomized encounters, trainers, and healing.

Battle Pyramid

src/battle_pyramid.c — ascending floors with restricted items and darkness.

Battle Tower

src/battle_tower.c — streak-based single and multi battles with BP rewards.
The BATTLE_TYPE_FRONTIER composite flag ORs together all seven facility flags: BATTLE_TYPE_BATTLE_TOWER, BATTLE_TYPE_DOME, BATTLE_TYPE_PALACE, BATTLE_TYPE_ARENA, BATTLE_TYPE_FACTORY, BATTLE_TYPE_PIKE, and BATTLE_TYPE_PYRAMID.