Skip to Content
The Alchemy Project

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

A battle opens: watch the camera swing in as a Lizard King, a Harridan, and two Stone Soldiers are announced one after another and the Fight menu appears
A battle opens: watch the camera swing in as a Lizard King, a Harridan, and two Stone Soldiers are announced…

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

One turn against a Lizard King, a Harridan, and two Stone Soldiers: watch Ivan, Isaac, Garet, and Mia execute their commands in Agility order
One turn against a Lizard King, a Harridan, and two Stone Soldiers: watch Ivan, Isaac, Garet, and Mia execute…

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.

  1. Build (battle_action.c, battle_targeting.c): btActionRunBuild decides everything uncertain up front, including weapon unleashes, target spread, critical hits, and dodges, because each animates differently.
  2. Stage (battle_staging.c): btStagingDispatchAction runs the camera swing and the attacker's lunge or cast glow, installs btPlaybackRun on the tick pass, and launches the foreground battle effect.
  3. Impact and resolution (battle_playback.c, battle_ability.c): At the impact tick, the battle effect signals btPlaybackStart(). btPlaybackRun then resolves each target via btAbilityApplyToTarget (applying HP and status math at once and queueing reactions via btPlaybackPush) and drains flinches, damage numbers, and log lines while the spell finishes animating.

Decoupled battle effects and the playback queue

Isaac casts Quake: rock pillars rise under Garet, Ivan, and Mia and sink again. Watch the targets flinch before the screen has stopped shaking.
Isaac casts Quake: rock pillars rise under Garet, Ivan, and Mia and sink again

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 on sySynch(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.

A battle in Goma Cave ends: watch the last enemy dissolve, experience and coins tally up, and the cave return where the party stood
A battle in Goma Cave ends: watch the last enemy dissolve, experience and coins tally up, and the cave return…
  • 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 gmBattleRandom identically and trade commands over the link cable so that both consoles simulate the same turn deterministically.

Code references