One fight from the outside in: how a field step or a script op becomes a battle, how a round hands turns out, how the battle ends, and what the party walks away with. The per-state detail of an action lives on the action state machine page and the menu on the command flow page; this page is the loop that holds them.
FUN_801D9E1C (field overlay) on entering a new 128-unit tile3E FF <row>; the row is an index into the scene's own formation table0x8007BD0C..0F, filled by the encounter confirm FUN_801DA51C+0x16C, seeded from speed by FUN_801DA780; the max-key pick is FUN_801DABA4DAT_8007BD71 = 0xFE raised at the action SM's 0x5A gate; cause word _DAT_8007BD2C (0 monsters down, 5 party down)FUN_8004E568 (SCUS); the battle exits when its phase halfword ctx[+0x6CE] reaches 0x43World::tick → live_field_tick / live_battle_tick; kernels in engine-battle and engine-battle-vmRandom. A scene whose MAN carries encounter regions rolls per step: a per-region rate counter, a formation-range pick, and a one-step anti-repeat (record layout on the encounter record page). The anti-repeat is one global that only the roll writes, so a scene’s first roll still tests the last scene’s formation row. The roll's trigger branch is destructive - it draws RNG, latches the anti-repeat and re-seeds its counter before returning - so a roll with nothing to receive it is a fight silently thrown away. The port keeps a five-phase bracket around it (idle, transition, triggered, battling, post-battle grace) and logs every way a roll can fail to become a battle.
Scripted. Boss fights enter through field-VM op 0x3E with a first operand of 0xFF (any value below 100 runs the same body; 100 and up is the door warp). The op points the system entity's encounter-record slot at formation row op1 of the scene's own table and requests the battle mode switch. There is no boss battle-id global: the candidate, DAT_8007B7FC, has readers and no writer anywhere on the disc - it is a debug forced-battle id retail never sets.
Boss rows sit outside every region's rollable slice, so only the op reaches them, and they carry a non-zero first header byte. On that predicate the confirm ORs bit 0x80 into the per-battle flags byte DAT_8007BD60, which selects the boss intro style and, at battle init, becomes the scripted-fight byte ctx[+0x287].
Two state machines hand a round back and forth. In the command band the flow SM walks every living party member through the command ring while the action SM idles. In the execution band the action SM dispatches every combatant, party and monster alike, by the max-key pick, and consumes each key as its owner is dispatched.
So a monster that won initiative still waits for the last party commit: initiative orders the execution band, it never lets anyone act before the prompt. The one exception is a rolled back attack, which arms the round straight from the intro timer; with the party's keys zeroed by the side lockout, only the monsters act.
(speed + rand() % (speed/2 + 1) + 1) & 0xFFFF, stored in 16 bits with no ceiling, plus a wounded bonus that grows as HP falls, then halved under Slow0x8000 or more never wins0xFF arm bumps the round index and returns the flow byte to 0x14FUN_801D88CC, key re-seed if none is live, the per-round poison ticker FUN_801E752C on every round but the firstThe pick loop carries no HP or occupancy test of its own, which is the root of a traced turn-order oddity: when every remaining key is zero the result is a draw over the seats walked.
One per-frame World::tick drives the whole Field → Battle → Field round trip, and both play hosts arm it through one kernel, World::arm_live_loop: the scene label, the loop and player-battle flags, the Seru registry and the battle-music swap. Per frame:
World::finish_battle (below) restores the field; the scene host then re-runs the scene-entry script, as retail’s field reload does. That re-run is what dispatches post-battle beat records - a boss’s victory flags land from the scene’s own script bytes, not from engine code.The loop flag gates only the field side, the random roll. Once the world is in battle, the battle tick always runs, because battle entry is not gated - a scripted fight or a world-map encounter flips the mode on its own - and a gated battle driver could strand a session mid-fight.
Each actor carries its ailments as one packed halfword at +0x16E, mirrored for party slots into the character record.
| Status | Bits | Effect |
|---|---|---|
| Venom | 0x0001 | min(max_hp/32, 128) per round, never kills; combat rolls ×9/10 |
| Toxic | 0x0002 | min(max_hp/16, 256) per round, never kills; rolls ×7/10 |
| Stone | 0x0004 | cannot act or be damaged, counts as defeated, lasts the battle |
| Rot | one of 0x0008 / 0x0010 / 0x0020 | one random limb unusable (its attack command is refused) |
| AI delegation | 0x0380, as a unit | the action pick is handed to the AI (Rage passive, charm) |
| Numb | 0x0400 | paralysis; clears on being hit |
| Sleep | 0x0800 | asleep; wakes when hit |
| Curse | 0x1000 | blocks Magic |
The poison ticker subtracts against current - 1 before applying the cap, so a tick that would reach zero leaves the actor at 1 HP, and it draws no RNG. Bit 0x0400 has clearers all over the battle code and one retail setter, in a cast image: Kiss of Death’s miss arm (PROT 0955) ORs it in. A word-level decode of the executable and every other overlay finds only clears, so a static sweep alone reads Numb as unused. Faint is not a bit at all - it is current HP of zero.
0, so it still runs the opening states, the Done band and the 0x5A gate, with no action. A party member whose raw status word carries Stone / Numb / Sleep committed no command, since the ring refuses it; its turn arms category 0, which the action seed sends straight to Done. It is never auto-armed as a strike.FUN_801EC3E4), unless the target is Stone; a Stone target takes no damage.World::tick_battle_passive_sweep).rand() and wakes from Numb on a one-in-eight roll (FUN_801F45A4). A monster that is still Numb when its turn is picked arms nothing: the turn picker’s tail zeroes its category after every draw is spent.0x0F80 strip, the tick’s scripted-monster arm or the 0x400 waker, so Venom, Toxic, Rot and Curse last until cured, and Sleep and Confuse until a strike or a cure. Nothing clears the word at KO either: a fallen member keeps its ailment bits until an item (the Medicine cure arm, the revive arm) or the battle’s exit clears them. The tick has no Stone test either: a Stoned actor with Venom drains.The arts entry spends a per-turn budget. The port’s gauge grows one AP per ten levels - 4 AP at level 1, capped at 10 - and refills at every round start. Every art starter and art body costs one; system actions and the direction bytes are free. What the entry actually spends in retail is the battle actor’s action gauge, seeded at battle setup from the character’s live AGL (arts command gauge).
A Spirit turn extends that gauge in three steps, all inside the action state machine:
min(base × 7 / 5 + 8, 0x120). A Spirit turn raises the AP bar and its plate but never the readout bar an item turn raises, because the seed sends it past the item pre-arm. The Done band pays 8 into Spirit for every action and 0x20 for a Spirit turn, capped at 100.The port draws the bar through the arts-entry chrome builders on both hosts, so the Spirit bar is the arts bar, one pixel per AP between its end pieces.
Two routines. The action picker FUN_801E9FD4 counts the filled magic slots in the monster record and rolls rand % (1 + count): zero is a physical strike on a random party seat. Anything else rolls again to choose a slot and walks the monster's animation entries for the cast, checks the monster can afford it, and resolves the target from the spell's shape byte. The walk keeps going past its match, so each later cast entry matches again and redraws the target, and a cast with no valid target throws the whole pick away and starts over. The flee roll comes next, on every monster turn. A large switch on the monster id can then override the choice with bespoke scripted casts on cooldowns - each case is AI for one monster.
FUN_801E9FD4. A re-match also re-runs the MP test and the target arm with its draws, and the heal and all-ally arms leave the slot counter where the walk next reads it, so a later match can read another slot.0x28, 0x29, 0x2A, 0x7F, 0x85 roll a target, and only a member still holding a turn this round draws the second roll, over five, four or three by id; a Stoned member is passed by.8, 9, 0x0B with a budget of three. Monster 0xB6 picks by round through five cases; past them the generic pick stands.The target resolver FUN_801E7320 runs only for actors with the 0x380 delegation bits set. It expands the targeting class the picker left in actor +0x1DD: a living monster slot (rand % monster_count + 3 - pool slot 3 is the first monster whatever the party size), a living party slot, or an all-target code.
A data constraint hides in the cast path: after choosing magic the picker counts the rollable castable entries and divides by the count. A count of zero executes the divide-by-zero trap and the BIOS parks the machine. Retail data never has that shape; a rebuilt monster block must keep at least one rollable entry while its magic array is live.
ctx[+0x28A] gates multi-phase boss scripts. Its only writer is the action machine’s round-end arm, so it is a round counter, zero in the first round; advancing it walks a round-scripted boss to its next scripted cast.0x380 are not a monster flag. They are set only on party members whose record carries the Rage passive: the command ring skips that member, and at the round's start the auto assembler fills its turn with a random combo of its learned arts. A normal monster keeps them clear, so its scripted-cast cases fire.The cooldown latches behind those scripted casts are cleared only at battle init, so a boss self-heals at most once per fight. In the port the picker’s generic core (engine-battle::monster_ai::generic_core, the gate, the walk with its re-matches and the redrawn pass), its driver (World::pick_monster_action: swing budget, flee checkpoint, ring) and the per-monster switch (engine-battle::monster_ai) are separate; a scripted cast the active spell catalog cannot fold degrades to a physical strike.
Targeting stays retail by default. A single-target monster attack goes to a uniformly random living party member. The Smart monsters toggle redirects it to the lowest-HP member instead, after the faithful pick has been rolled in full, so the random stream is byte-identical either way (engine options).
The randomizer’s enemy-ally (“charm”) feature widens the monster-wipe scan’s down-mask from 0x4 to 0x384, so a living charmed monster counts as down and the player need not kill their own ally. That breaks an invariant the victory arm leans on: under the retail mask, a living acting actor at monster-wipe victory is always a party member, so the arm keeps the acting slot and reads its character id from the three-byte party roster to stage the win pose. When the charmed ally lands the last kill, the acting slot is a monster, the roster read runs past its three bytes, and the win-pose stream request is garbage - the battle wedges at the victory hand-off. Retail never reaches the state.
The port’s victory arm re-picks whenever the acting slot is not a living party slot, uniformly among eligible slots, so it cannot spin.
The battle does not exit on the frame the 0x5A gate raises the signal. The battle tick stops stepping the action SM and runs the results sequencer every frame instead:
| Frame from the signal | What happens |
|---|---|
+0 | the gate sets the signal and cause |
+0..+80 | CD loads (voice clip, jingle bank); camera frames the posing actor |
+80 | results frame: pose staged, downed members floored at 1 HP, XP / gold / drop / level-ups applied, result and level-up windows raised |
+80..+336 | hold |
+336 | exit fade starts (a subtractive fade to black) |
+402 | phase halfword reaches 0x43: game_mode = 2 |
The posing actor is the battle context’s acting slot, so the member who finished the fight poses - not the leader, and not whoever levelled. The action SM’s victory arm is the slot’s last writer (it re-picks a living member when the finisher is down), and captures of wins that different members finished each show that member posing. Escape runs a shorter arm with no results and the same exit gate.
monster.snd bank, which the sequencer streams into VAB slot 7 over the formation’s own bank. Nothing in the sequencer keys it: every win-pose clip’s cue track fires the cue that plays it (battle voices). The port carries the bank on the victory sequence and answers it for slot 7 while the sequence runs, on both hosts.The exit store is the same, wipe or no wipe. The fork lives one mode later, in the main-init scene setup FUN_8003AEB0: when the party-survived latch (DAT_8007BD60 bit 0x80) is clear and story flag 0 is clear, it switches to the card-init mode and the player lands on the title screen with the cursor on Continue. Retail's game over is a silent return to the title - no game-over art, no menu of its own. With story flag 0 set, the same wipe is a scripted loss and returns to the field standing, at 1 HP. The mode-18 “game over” overlay is a developer harness the retail path never enters.
The wipe frames the killing monster. The wipe arm stores no seat, so the acting slot still holds whoever acted last - usually the monster that landed the wiping blow - and the sequencer’s shared tail frames it with the in-fight camera arm for a monster seat. The results frame turns it with a fixed yaw, stages its knockout taunt (the first clip tagged 0x22, the taunt the action SM withheld from the wiping blow) and, for a set of bosses keyed on the first monster id, fires a bark once. The port takes the pose seat from the acting slot on a wipe and stages the same three. A party seat left in the slot (a member who downed the last member) makes retail index the monster table out of range; the port stages nothing there.
The port can build a state retail cannot: a battle with no seated party, whose hollow slots read as dead. It therefore lets the party-wipe arm fire only with at least one seated member, while a monster wipe still resolves.
World::finish_battle is the one exit, on both hosts:
rand() a level, so it changes which item drops.Retail leaves a battle only through that path; the port’s hosts can leave one early - a scene picker, a debug seat, a test running several fights on one runtime. Two resets keep it out of the next fight. Battle entry clears the results sequencer, as retail’s battle init zeroes the end-of-battle marker; a surviving sequence would otherwise own the new fight from its first frame, with no round prompt. And a scene load or New Game abandons a live battle, a port-only path that drops the sequencer, sessions, effects and fade and restores the field snapshot without storing any outcome.
The killing-blow absorb is decided inside the hit resolver FUN_801EC3E4 at the moment a physical hit resolves: killing blows only, by a party attacker, on a monster whose record carries a Seru id. The chance is a percent byte in the monster record, plus a flat 30 points when the attacker holds the Magic Boost passive, rolled as rand() % 100 < chance. The roll is drawn on every Seru kill, before the check for whether the character already knows the spell. The grant lands in the same action: the Done band of the action SM reads the captured id, prepends the spell to the character's list and holds the learn banner.
Which hit rolls. The kill compare runs once per landing, on the hit that lands the combo’s accumulated total: a combo that crosses the target’s HP on its second hit rolls once, after every damage draw of the chain. The “already known” check also answers yes outright for a slot without its Ra-Seru and in a no-reward battle.
The capture spells and capture items are a separate mechanism, and a synthetic catalog’s alone: no retail item descriptor carries a capture class, so a game on the disc’s data learns Seru magic through the killing-blow absorb and worn gear (the summon scans). The port models them with a per-character capture-point accumulator that banks points for every eligible party member and teaches the spell when a threshold is crossed, with a banner that plays out over the field; those thresholds are engine-side approximations, while the killing-blow absorb above is retail’s own roll. The catch-rate byte is a randomizer target.
Small modules the loop composes, each mirroring one retail piece:
| Module | What it is | Retail anchor |
|---|---|---|
engine-battle::encounter | transition bracket and region roll | FUN_801D9E1C |
engine-core::battle_round | round sweep and the two bands | FUN_801D88CC |
engine-battle::monster_ai | per-monster scripted AI | FUN_801E9FD4 |
engine-battle-vm::status_effects | typed statuses and the packed status word | FUN_801E752C |
engine-battle::ap_gauge | per-slot AP budget | record +0xC9 / +0xCA |
engine-battle::arts_command_input | per-press arts entry | actor queue +0x1DF |
engine-battle::battle_stats | equipment, passive and status fold into battle stats | FUN_80042558 |
engine-battle::seru_learning | capture-point accumulator and banner | - |
engine-battle::levelup | XP curve and growth (level-up) | FUN_801E9504 |
engine-menus::inventory_use | item-use session shared by the field menu and the Item arm | flow states 0x64, 0x5B / 0x5D |
engine-audio::sfx | cue ids to SPU one-shots, scheduled per tick | descriptor table DAT_8006F198 |
engine-menus::game_over | title hand-off after a wipe | stores at 0x8003B5D4 |
Battle sound cues touch only the SPU and draw no random numbers, so battle determinism does not depend on audio; a missing bank, VAB or free voice drops the cue silently.
The full write-up - encounter session phases, the port's host flags, the status-word writer inventory, the AP gauge, the charm softlock analysis and the port module map - is in docs/subsystems/battle-round-loop.md.