Battle & Effects
The battle engine runs every fight in Golden Sun: The Broken Seal through one turn-and-action pipeline decoupled from its visual spell effects.
Whether the party meets skeletons in Goma Cave, opens a Mimic, fights Saturos
atop Mercury Lighthouse, competes in Colosso, or two Game Boy Advances are
linked in the Battle Arena, the same engine (btRun in battle/) drives the
fight. Visual animations are kept beside it in battle_effect/, painting
glowing spells and summons onto a 128×128 brightness canvas with no knowledge of
HP or damage formulas.
This page follows a battle from formation setup through turn order, enemy AI, the four-step action pipeline, the hand-off between spell animations and damage playback, and zoomed battlefield backdrops.
Entering a battle and spawning formations

Random encounters, Mimics, and cutscene scripts all start a battle the same way.
They write a formation ID (Formations,
380 rows) into MainRoutineStateWork, snapshot field actors, request
ENGINE_BATTLE, and return to gmMainRoutineRun.
Each formation specifies up to 5 enemy species with minimum and maximum spawn
counts, fitting into 5 screen slots (giant bosses occupy two slots). Spawned
enemies are instantiated in the same 332-byte UNIT struct used by party
members
(RPG Data & Rules).
Turn loop, two-phase enemy AI, and the action pipeline

Each turn collects commands, sorts active fighters by Agility and priority, and checks for victory or defeat after every action, so a fight can end in the middle of a turn.
One design choice is easily overlooked: enemies choose twice. An enemy's turn-start pick holds only if it chooses to guard or flee, since both must take effect before the first blow lands. Otherwise, the AI picks again when its turn executes, so an enemy never wastes an attack on a party member who already fell earlier in the round.
- Build (
battle_action.c,battle_targeting.c):btActionRunBuilddecides everything uncertain up front, including weapon unleashes, target spread, critical hits, and dodges, because each animates differently. - Stage (
battle_staging.c):btStagingDispatchActionruns the camera swing and the attacker's lunge or cast glow, installsbtPlaybackRunon the tick pass, and launches the foreground battle effect. - Impact and resolution (
battle_playback.c,battle_ability.c): At the impact tick, the battle effect signalsbtPlaybackStart().btPlaybackRunthen resolves each target viabtAbilityApplyToTarget(applying HP and status math at once and queueing reactions viabtPlaybackPush) and drains flinches, damage numbers, and log lines while the spell finishes animating.
Decoupled battle effects and the playback queue

At the end of Quake in the recording above, the first target flinches while the ground is still shaking. That overlap follows from the clearest architectural boundary in the battle engine:
- The battle effect (
battle_effect_*.c) is foreground blocking C painting onto a 128×128 6-bit brightness canvas with palette ramps and VBlank trail decay while looping onsySynch(1). It knows nothing about damage. - The playback queue (
battle_playback.c) is a tick-pass event that knows nothing about rocks or fire.
They meet at one hand-off. At the exact impact frame (for Quake, 16 ticks
before its end), the effect calls btPlaybackStart(). From that tick
onward, the playback event resolves each target and drains hit shakes, HP drops,
damage lines, and enemy dissolves concurrently while the spell fades out.
Zoomed backgrounds, spoils, and link battles

- Zoomed two-half backgrounds: The battlefield is one flat backdrop on the GBA's single affine layer that zooms and slides via HBlank DMA as the camera swings between front and back monster poses. Units sit as sprites over it, and UI windows sit in front on a non-zooming layer.
- End of battle: Victory awards XP, coins, Djinn-kill bonuses, and random drops, running level-up windows inline. Defeat returns the party to the last Sanctum unless a cutscene set a custom loss destination.
- Link battles: Both GBAs seed
gmBattleRandomidentically and trade commands over the link cable so that both consoles simulate the same turn deterministically.
Code references
- Battle engine (
games/tbs/src/battle/): File map, turn, AI, action, staging, ability, and playback modules, weapon unleashes, zoomed backgrounds, spoils, auto-battle, and link battles. - Battle effects (
games/tbs/src/battle_effect/): The 128×128 brightness canvas, palette ramps, VBlank trail decay, assembly blitters, motes, and all 16 single-element summons.