Skip to Content
The Alchemy Project

Field Engine

This page is a guide to the games/tbs/src/field/ folder of the Alchemy project. The field engine in it runs every non-battle scene in Golden Sun: The Broken Seal, from walking through towns and dungeons to crossing the World Map.

At the centre of this folder is fdEngineRun, which coordinates up to 64 active actors, wall-sliding physics, height-based terrain, field Psynergy, random encounters, and Tolbi's Lucky Dice and Lucky Wheels minigames. Whenever Isaac explores Vale, pushes a pillar in a dungeon, or talks to a villager, this code is running.

Because so many parts share each frame, the field engine relies on strict event priorities and a ranked request queue. The sections below cover the tick order, the behaviour of actors and the World Map, and a full file map. The conceptual model is described in World, Scenes & Maps.

Event priorities and field requests

Priority offsetCallbackPer-tick job
+15fdPaletteFadeUpdateStep active palette fades toward target
+10fdActorUpdateAllOnFieldStep every actor's SC_* script and physics
+5fdMapGridUpdateLayerScroll (or fdWorldMapUpdateView)Follow camera actor and scroll map layers
+0fdActorSystemPlaceSprites (or fdActorSystemProjectSprites)Submit actor sprites to OAM depth buckets
+0mnWindowSystemUpdateAnimate windows and type dialogue
+0 (if animated)fdTilesetAnimationUpdateRepaint animated map tiles on timers
VBlank passmnWindowSystemUploadTilemapCopy window tilemap to VRAM

Running actors (+10) before camera scroll (+5) and sprite placement (+0) keeps Isaac locked to the camera instead of lagging a tick behind.

Between ticks, fdEngineRun services requests recorded in FieldStateWork in strict priority order:

  1. Exit
  2. Engine or scene override (e.g., poison fainting)
  3. Random battle (in TBS, checked before step and push)
  4. Step cue on the current metatile
  5. Push into a ledge, ladder, or door (after consecutive blocked ticks)
  6. A button (resolves to talk, examine, or field menu)
  7. Field menu (Select or A facing nothing)
  8. Talk to nearest actor roughly in front
  9. Examine metatile ahead
  10. Field Psynergy
  11. Field item
  12. Pause menu (Start)

Actors, World Map, encounters, and minigames

The World Map south of Vale separated into its two tilted affine layers and sprite layer
The World Map south of Vale separated into its two tilted affine layers and sprite layer
  • Actors (ActorsWork): 64 active slots map from 66 scene actor IDs (0–7 for characters, 8–65 for placements). Most NPCs select one of 7 stock scripts in actor_scripts.c (MovementTypeWanderScript, etc.), and targeting opcodes act on the actor's watched actor. vault.c contains a short custom script (the boy running around Vault's well).
  • Movement, collision, and targeting: SC_PLAYER eases acceleration and braking, probing 8 pixels ahead plus angled side probes (±22.5°, ±45°, and up to ±67.5°) so that Isaac slides along walls. It steps up at most 8 px, drops at most 12 px, and matches talk targets within 32 px (beyond 11 px, roughly in front).
  • World Map and map screen: world_map_lines.c (ARM in IWRAM) writes per-scanline affine parameters across all 160 lines while streaming four 512×512 chunks at a time. R opens the flat chart (map_screen_markers.c), scaling each visited area's own World Map coordinates directly onto the chart.
  • Random battles and overworld steps: encounter_rules.s selects one zone for each of the 7 terrain types on scene load. Every 10 metatiles outside towns restores PP and ticks poison.
  • Tolbi minigames: Standalone engines ENGINE_LUCKY_DICE (lucky_dice.c) and ENGINE_LUCKY_WHEELS (lucky_wheels.c).

File map

File(s)Role
field.c, field_internal.hParent translation unit #includeing the field_*.c parts below in ROM order
field_actors.c, field_actor_motion.c, field_actor_camera.c, field_player_control.c64-actor table, eased movement, camera tracking, and SC_PLAYER collision and wall-sliding
field_map_grid.c, field_terrain.c, field_tile_animation.c, field_world_map.cMap loading, height shapes, animated tiles, and World Map chunk streaming and view
field_decompress.c, field_sprite_animation.c, field_sprite_draw.c, field_view.cField graphics decompression, sprite animation, OAM placement, and sprite viewer
field_engine.c, field_engine_internal.h, field_engine_run.c, field_engine_walking.cfdEngineRun setup, walking loop, and request dispatcher
field_engine_cues.c, field_engine_actors.c, field_engine_targets.c, field_engine_encounter.cCue table scanning, actor spawning, talk and examine targeting, and random encounters
field_engine_psynergy.c, field_engine_items.c, field_engine_item_display.c, field_engine_treasure_spots.cField Psynergy and item triggers, found-item displays, Reveal sparkles, and chest spots
field_engine_speakers.c, field_engine_palette_fade.c, field_engine_screen_transition.c, field_engine_palette_editor.cDialogue portraits, fades, screen transitions, ground mask, and debug palette editor
script_commands.c, script_commands_*.cBlocking and non-blocking C script commands and all TBS field Psynergy implementations
actor_scripts.c, field_script_commands.c, facing.cSC_* bytecode interpreter, stock movement scripts, and 5-direction facing tables
actor_update_arm.c, world_map_lines.c, map_grid_convert.s, picture_decode.sHot ARM and IWRAM routines for actor ticks, World Map scanlines, and map and sprite decoding
scenes.s, map_grid_data.c, encounter_*.{s,c}, music_rules.s, *_rules.{s,c}, map_screen_markers.cScenesWork (201 scenes), MapsWork, and rule tables for areas, encounters, music, restarts, and map markers
lucky_dice.c, lucky_wheels.c, stubs.s, engine_stubs.s, lucky_*_stubs.sTolbi minigames and module stub tables

Source files

src/field/

FileDescription
actor_placement.cThe party leader's actor placement, which fdActorResetForLeader copies before a scene places its own actors, and the script it gives the leader.
actor_script.cActor scripts: the ones the field starts itself, and the command table.
actor_script_operands.cScript state values: the handler SC_SET, SC_ADD and SC_CMP call for each.
actor_scripts.cThe actor scripts the field's commands start: walking off and turning to face a set way, the dust puffs, the emote bubble's pop and the movement types fdActorSetMovementType starts by number; and, among them, the glyphs a message types before its speaker's first voice blip.
actor_update_arm.cUnused: an ARM copy of the actors' update, made to run from RAM.
battle_background_rules.cThe battle background of each scene (fdBattleBackgroundSelectForMap).
battle_music.cThe music a battle plays: the bosses, the guards of Crossbone Isle, the Colosso finals and the fights with Saturos have their own, and every other battle plays the usual battle music.
encounter_zones.cThe 104 encounter zones, by zone number.
facing.cWhich picture a sprite shows for the way it faces, and the small tables the actors read as they move: the direction pad's headings, the World Map camera's lean, a floating actor's bob, how long a footprint lasts and the order a sprite dissolves in.
field.cThe field object: the code that draws and moves everything in towns, dungeons and the world map.
field_actor_camera.cThe actor cameras, which place every actor's sprite each tick.
field_actor_motion.cActor motion, each tick, and the first actor script commands.
field_actors.cThe field's actors: the 64 slots and creating and releasing them, starting them on their scripts, setting their sprites' animations and looks, and dissolving sprites pixel by pixel.
field_decompress.cLZ decompression.
field_effect_tables.cA field Psynergy's cast colours, and two script tables of the field effects.
field_engine.cThe field engine's object: random encounters, a field scene's actors, the leader's walking and the requests they raise, fdEngineRun, the scene's cues, using items and Psynergy on the field, the map's treasure spots, and the screen transitions and palette fade that the field and battles share.
field_engine_actors.cThe party's sprites, placing a scene's actors, and the actor slots.
field_engine_cues.cFinding and running a scene's cues: talking, step cues, examine cues.
field_engine_encounter.cRandom encounters, and each scene's music, area name and battle background.
field_engine_ground_mask.cThe ground mask: raised ground in front of an actor hides the part of it behind, as a ledge in the Kalay Tunnel's treasure room hides Isaac's legs.
field_engine_internal.hWhat every part of field_engine.c shares: its includes and the macros and types more than one part uses.
field_engine_item_display.cShowing a found item, and Reveal's sparkles on hidden things.
field_engine_items.cUsing an item on the field.
field_engine_palette_editor.cThe debug palette editor.
field_engine_palette_fade.cThe palette fade: palettes moved toward target colours.
field_engine_psynergy.cUsing Psynergy on the field.
field_engine_run.cThe field engine: running a scene until the party leaves it, and its entrance.
field_engine_screen_transition.cHow a scene fades in and out: from or to black or the backdrop colour, through windows that open or close over the screen line by line, from or to the centre line, or by a dissolve, which no scene uses.
field_engine_speakers.cSpeaker definitions: voices and Halted lines by sprite.
field_engine_tables.cThe field engine's tables: the actor scripts its effects run, the colour curves of the palette tints, and the lookup tables of its transitions, speakers, weather and Psynergy.
field_engine_targets.cThe actor a field Psynergy or item aims at, and the helpers that follow it.
field_engine_treasure_spots.cThe map's treasure spots: chests and Psynergy Stones.
field_engine_walking.cField requests and each tick of the leader's walking: exits, encounters, damage.
field_internal.hWhat every part of the field object shares: its includes and the few names more than one part uses.
field_map_grid.cLayered maps: loading, drawing and scrolling, and copying metatiles.
field_player_control.cPlayer control: the configured keys and moving actors by the pad.
field_script_commands.cActor script commands: waits, jumps, flags, moves and state values.
field_sprite_animation.cSprites, their animations and the picture caches they draw from.
field_sprite_draw.cDrawing sprites: composing their animations and placing them on screen.
field_sprite_viewer.cThe debug sprite viewer: one sprite shown once for each way it can face, in a ring, with up to four sprite animations laid over it and the keys to change each one's sprite, animation, priority and colour mode.
field_terrain.cGround heights, and the collision of metatiles and the world map.
field_tile_animation.cTimed map changes: the tileset animations that redraw a map's tiles in turn, the blend script that changes over time how its layers blend, and the palette cycles, which nothing starts.
field_view.cThe screen shake, and two parts of the world map's view: its still image drawn from its tile maps, and its ground tilted away from the camera, worked out a screen line at a time.
field_world_map.cThe world map: its perspective view, its tiles, and the still image of the view BG1 shows while a menu or script runs.
lucky_dice.cLucky Dice, the dice game in Tolbi, drawn with the battles' 3D camera.
lucky_wheels.cLucky Wheels, Tolbi's game of chance: the player bets game tickets on paylines, pulls the lever up to five times and locks wheels between pulls to line up matching symbols.
map_grid_data.cMap ground: the height curve of each metatile kind and the world map's battle environments.
map_screen_markers.cThe map screen's markers (fdMapScreenShow).
palette_scale.cThe fade of the Lucky Dice and Lucky Wheels screens, which darken and brighten by scaling their palettes; each game's object includes it.
screen_transition_shapes.cThe window shapes of WINDOW_TRANSITION_SHAPE, one for each step of its progress (fdScreenTransitionUpdateScanlineTable): a clock wipe, a wedge from the screen's centre (120, 80) that sweeps round from twelve o'clock, over the right half in the first 16 steps and the left half in the next 16.
script_commands.cThe field Psynergy and script command object: the commands scripts run (waits, items, questions, destinations, actors, messages and the camera), moving on the field (views, jumps and climbing), the field's weather and line scrolls, Djinn joining the party, each field Psynergy, the field effect sprites its particles use, and the map screen.
script_commands_actor_commands.cScript commands on actors: placing, moving and animating them.
script_commands_arcs.cThe golden rings that swell around a caster and rise up its body: each ring is two arcs of SPRITE_GOLDEN_RINGS, its back half behind the caster and its front half, the same arc upside down, in front.
script_commands_avoid.cAvoid: the caster spins among falling arcs and flashes, and monsters attack less often until it wears off.
script_commands_camera.cThe camera a script moves: following an actor, panning to a point kept inside the map, and waiting for it to arrive.
script_commands_carry.cCarry: two hands lift the target, move it with the d-pad and drop it.
script_commands_catch.cCatch: a hand reaches out from the caster, grabs what is ahead and takes it off the map.
script_commands_climbing.cJumping and climbing on the field.
script_commands_cloak.cCloak: the leader blinks into a dark outline and stays hidden while it walks on metatiles that allow it (METATILE_BEHAVIOR_CLOAK).
script_commands_djinni.cA Djinni's hop before its battle, a Djinni joining the party, and the field effect sprites the joining lights are drawn with.
script_commands_douse.cDouse: a rain cloud floats out over the target and rains on it, then shrinks back into the caster.
script_commands_exit_animations.cAn actor's screen position, the encounter transition, and the leader leaving through an exit and arriving.
script_commands_field_particles.cField effect sprites: the particles field Psynergy moves and draws.
script_commands_field_psynergy.cRunning a field Psynergy by its kind, and helpers its kinds share.
script_commands_field_psynergy_state.cA field Psynergy's state: setting it up, its caster and target, cleaning up.
script_commands_force.cForce: a fist flies out from the caster and knocks what it hits back a metatile.
script_commands_frost.cFrost: snowflakes spiral in on the target, and a puddle freezes into an ice pillar.
script_commands_ground_particles.cThe ground particles (rain) start and the floating motes' update.
script_commands_growth.cGrowth: green sparks fly from the caster, and a sprout grows tall.
script_commands_halt.cHalt: a stopwatch flies out to the target, ticks and bursts into sparkles, and the target stands frozen, its colours flickering.
script_commands_internal.hWhat every part of script_commands.c shares: its includes and the macros and types more than one part uses.
script_commands_jupiter.cA Jupiter Djinni joining the party, and the first of the Mars Djinni's functions, linked after it: its shrinking and its sparks.
script_commands_lift.cLift: two hands grip the target and raise it, and Force's flashes and heavy impact, linked beside it.
script_commands_line_scroll.cThe line scroll: per-line scroll offsets fed by HBlank DMA.
script_commands_map_screen.cThe map screen R shows on the World Map: its area markers, the cursor and the name of the area nearest it.
script_commands_mars.cA Mars Djinni joining the party, and the first of the Venus Djinni's functions, linked after it: its shrinking, its falling motes and its rising particles.
script_commands_mercury.cA Mercury Djinni joining the party.
script_commands_messages.cThe scene scripts' speech: a message window opened beside the actor who speaks, with the speaker's portrait, a yes-or-no question, two speakers at once, and the speaker's voice.
script_commands_mind_read.cMind Read: scanline waves, shifting colours and flowing particles.
script_commands_motes.cWeather drawn in sprites on the field, and the storm's lightning: floating motes, rising sparkles, and the flashes of the palettes.
script_commands_move.cMove: a hand pushes the target, steered with the d-pad.
script_commands_movement_types.cActor scripts: moving, turning and the built-in movement types.
script_commands_ply.cPly cast in the field: eight winged figures spiral into one, which flashes, sheds sparks and flies off.
script_commands_requests.cBattles and destinations a script requests, and the queued sound.
script_commands_retreat.cRetreat, the caster spinning away in a spray of sparks, and the golden rings that fall round the caster of Avoid.
script_commands_reveal.cReveal: the world turns grey but for a rippling window round the leader, which ends when the leader walks out of it or its time runs out.
script_commands_scripts.cScript commands: waits, received items, questions and destinations.
script_commands_venus.cA Venus Djinni joining the party.
script_commands_view.cThe view bounds, zoom transitions and actors that ride another.
script_commands_weather.cThe weather on the field: the rain splashing round the leader and the lightning of the storm over Vale.
script_commands_whirlwind.cWhirlwind: an orb flies from the caster and spins up a ring of leaves that blows its target away.
sprite_color_tables.cThe colour tables of the sprite colour modes (SPR_COL_MODE) that remap a pixel's colour, 256 bytes each: five that swap a sprite's colour channels, SPRITE_COLOR_GRB to SPRITE_COLOR_BRG (also used by fdSpritePictureCacheLoad for variants 1 to 5), then the grey one and the blue, red and green tints.
sprite_viewer_scale.cUnused: the scale of the debug sprite viewer's ring of sprites, which nothing in the game opens (field_sprite_viewer.c).
terrain_tables.cThe ground's shape inside a metatile for two of its height kinds (enum TERRAIN_KIND): where a raised or sunken square rounds off at its corners, and which of three heights each pixel takes, split along two diagonals.
world_map_encounter_rules.cWhere the World Map's random battles come from: each region of the map, and its forests, take their own encounter zone.
world_map_lines.cThe World Map's tilt: each line of the screen draws the flat map at its own scale and turn, so the ground seems to stretch away to a horizon.
world_map_resources.cWorld map graphics: a palette and five tilesets for each of its two looks, the second the snowfield's (fdWorldMapLoadGraphics).

include/field/

FileDescription
actor.hActors, the actor snapshot, and the screen transition styles.
actor_script.hActor scripts: one Sint32 command word, then its operands, as ActorScriptCommandTable runs them.
actor_update_arm.hThe unused ARM copy of the actors' update (actor_update_arm.c), and ACTOR_ARM_VIEW, ACTOR as GCC 2.95.2 reads it.
curve.hThe step counts, value shift, and per-pixel tables of the terrain height curves (terrain_tables.c).
emote.hWhat an actor shows over it: the bubbles of fdActorShowEmoteById and the effects fdEffectSetAttached attaches.
encounter_zone.hThe encounter zones.
engine_stubs.hThe field engine module's stub calls: the commands scripts are written in (actors, messages, camera, destinations), field Psynergy, weather and the field's effects.
facing.hThe constant tables of facing.c: which picture of an animation an actor shows for each way it faces, and beside them the sprite dissolve's pixel order, the fixed camera's matrix, a floating actor's bob, the actor scripts' end script and the World Map camera's lean for the direction pad.
field.hThe field engine's shared functions, globals and actor scripts.
field_blend.hFIELD_BLEND_STATE, the field's colour blend state.
field_coordinate.hFIELD_COORDINATE, a position on the field.
field_effect.hField effects, their scripts and the flags that spawn them.
field_effect_scripts.hThe actor scripts field effects give their particles and fragments.
field_engine.hWhat the field engine's object (field_engine.c) defines for the code outside it: encounters, the field's actors, the leader's walking, the scene's cues, treasure spots, screen transitions and the palette fade.
field_engine_functions.hThe field-engine module's functions, declared in the order of its stub table.
field_engine_script.hThe field engine's script functions (field_engine_items.c, script_commands_field_psynergy.c, script_commands_scripts.c and script_commands_messages.c).
field_functions.hThe field module's functions, declared in the order of its stub table, without the tables and types field.h brings in.
field_particle.hThe particles field Psynergy moves and draws, each with a sprite of its own.
field_psynergy_state.hFIELD_EFFECT_STATE, the state of a field Psynergy.
field_state.hFIELD_STATE, the running scene's state, with its treasure spots and the raised cues the field engine handles itself.
ground_mask.hThe ground mask, which hides the parts of an actor that raised ground in front of it should cover.
lucky_dice.hLucky Dice, the dice game in Tolbi (lucky_dice.c), and the small fixed-point helpers it keeps for its 3D camera.
lucky_dice_stubs.hLucky Dice's entry point.
lucky_wheels.hLucky Wheels, Tolbi's game of chance (lucky_wheels.c), and the unused LZ packer kept beside it.
lucky_wheels_lzpack.hThe LZ packer's state: hash chains over a 1024-byte window, one chain per byte value, and the output it is building.
lucky_wheels_stubs.hThe Lucky Wheels entry point.
map_grid.hMap grids: their metatiles, layers, animations and blend scripts, and the map grid state.
particle.hParticle and SCREEN_POINT, the positions animations move through.
perspective.hThe world map's perspective view state.
scene_tables.hThe scene table formats: entrances, exits, actor placements and cues, and the macros that write them.
screen_transition.hWhat the field engine's parts share with the files that define their tables: the item-display and Reveal scripts, the palette fade's colour curves, and the screen transitions' shapes, tables and window values.
script.hThe calls scripts are written in: the constants and small inlines a scene's scripts share, over the main-image functions they call by those functions' own names (fdScriptStart, fdActorJump, fdScriptSetMessageId and the rest), whose declarations it includes.
script_commands.hWhat script_commands.c declares for the files that use it: the commands scripts run, moving on the field, weather and line scrolls, Djinn joining the party, each field Psynergy and its effects, and the map screen.
script_inline.hThe script inlines: one typed inline for each script command a script calls by its own name, passing it on to the main image's import (fdActorFaceEachOther calls fdActorTurnToFaceEachOther).
script_operands.hThe actor script interpreter and the operands its commands read.
speaker.hThe speaker lookup's view of an actor's sprite, the speaker definitions, and the palette fade's heap slot.
sprite.hAnimated sprites: their layers, pictures and colour modes, and the resource slots they draw from.
stubs.hThe field module's stub calls: actors and their sprites, movement, and the map and world map.
weather.hThe ground particles, motes of light and sparkles that the weather state holds.
world_map_affine.hThe world map's scanlines and affine lines.
world_map_encounter_rules.hWhich encounter zone each part of the World Map takes, and the Djinn battles' formations.