Files
Crystal/docs/ARCHITECTURE.md
T
Sébastien QUEROLandClaude Opus 5.5 3456aa4e11
Checks / web (push) Canceled after 0s
Port field moves from the menu, SEL with Select, and moving Pokémon in Bill's PC
Field moves chosen in the party menu run their original checks and the
ROM's from-menu scripts (Cut, Surf, Strength, Flash, Whirlpool, Waterfall,
Sweet Scent), and Softboiled and Milk Drink share HP. SEL registers a key
item that Select then uses, replacing the Key Items shortcut, and Bill's
PC moves Pokémon between the party and every box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 17:22:56 +02:00

10 KiB

Architecture

The default application is a native TypeScript game. It loads no CPU interpreter, Game Boy hardware model or WebAssembly emulator. The reference emulator has a separate entry point and build.

flowchart LR
  ROM[User ROM] --> Gate[Exact revision validation]
  Gate --> Assets[ROM reader and asset decoders]
  Assets --> World[Native world rules]
  Assets --> Scripts[Resumable event, movement and text interpreters]
  Scripts --> World
  Input[Touch and keyboard] --> Runtime[Native runtime]
  Runtime --> World
  World --> View[Canvas world and DOM dialogue]
  Session[Application session] --> Runtime
  Session --> Saves[Native save storage]
  Reference[Separate reference emulator] -. local comparisons only .-> World
Module Responsibility
src/rom/validate.ts Exact cartridge identity, no networking
src/game/rom Banked reads, LZ, maps/tilesets/sprites/palettes and pinned addresses
src/game/world Overworld ticks, movement, collision, warps, connections, scenes, callbacks and save validation
src/game/scripts The game's event, movement and text formats and translated specials, no GB instructions
src/game/rendering Planar pixels, metatiles, connection strips, adaptive camera, sprites, emotes and minimap
src/game/party Party Pokémon generation, stat formulas, bag pockets and their validation
src/game/runtime.ts 30 Hz tick scheduling from real time, input routing and presentation updates
src/input Abstract buttons, multiple sources and browser bindings
src/app Runtime-independent load/play/pause/backup lifecycle
src/storage Versioned envelope, IndexedDB transactions and ordered writes
src/ui, src/main.ts Accessible game tabs, UI composition and DOM dialogue, no ROM offsets
src/platform Browser locks and future native platform adapters
src/reference, src/emulator Original-game reference, excluded from the native build
reference/public Licensed emulator files for the reference build only

GameRuntime is a lifecycle interface, not an emulator contract. Native state is validated JSON, schema version 3. It holds the map, position, facing, name/avatar, player ID and in-game tile count; event and engine flags and sprite variables; scene values, phone contacts, a queued special call, the rival's name, the game clock's minute offset, the weekday offset and DST flag, whether the introduction is still pending; positions of objects that scripts moved; and the party, bag, Pokédex sets, money, registered item and room decoration slots. Older saves without decorations migrate to the original bed and Town Map poster. Decoration imports validate slot categories, ownership and unique doll placement. Early version-3 backups without a money field migrate to the starting balance. It contains no graphics, music, dialogue, GB memory or ROM bytes. Versions 1 and 2 migrate with new-game defaults. Party Pokémon are validated by recomputing their stats from the ROM. The backup envelope retains the historical coreId field; native backups use it for their runtime/schema ID.

Saves made during an unfinished walk or jump retain its source tile and completed step count, so loading cannot skip a landing event or map transition. Directional mats wait for exit input; doors, stairs and caves force an animated downward step after arrival.

Native and reference sessions use different IndexedDB databases and Web Locks. Imports reject incompatible schemas and invalid positions before changing the world. A failed import restores the prior state. Original cartridge saves and emulator snapshots cannot be used as native saves.

GameWorld advances in overworld ticks of two video frames, as the original loop does. The runtime converts elapsed time to ticks. Tile events follow CheckTileEvent: a completed step checks connections, then warps, then coordinate events for the current scene. Idle ticks run the current scene script. Map entry runs the NEWMAP, TILES and OBJECTS callbacks on a copy first, so an unsupported callback refuses the step instead of entering a half-initialized map. Objects spawn from event flags at map load, and later flag changes leave them in place until appear/disappear or the next load.

Scripts are generators that evaluate against a private draft and yield presentation effects: pages, yes/no or weekday choices, movement, emotes and pauses. The world presents each effect and resumes the script with the answer, or after the animation's ticks. Unknown commands, movement commands and specials abort. Aborting discards the draft and restores the positions the scene changed. The draft commits only when the script ends. A save made during a scene contains the committed state, and coordinate events rewind to the tile before the triggering step, so the scene replays without partial rewards or flags. Specials are translated natively and selected by the routine that the ROM's special table points to.

Asset caches and the ROM reader remain in memory for lazy extraction. Neither ROMs nor extracted assets enter IndexedDB, the build or a backend. Vite blocks serving the root ROM and private reference checkout during development.

The native viewport follows its available CSS dimensions and draws additional world tiles at a bounded pixel resolution. Beyond a map's edge it draws the connected map with the current tileset, as the original connection strips do, and the border block elsewhere. Resizing also redraws paused sessions. The local minimap caches terrain from the loaded map and updates its marker from the same world position used by the player; the Map tab shares that view. It is a current-area map, not a full region atlas. Leaving Game pauses the session and releases held input; returning requires explicit play. The reference player retains its own fixed-screen styling.

game/battle now holds the native battle core and ROM-backed battle tables. Browser tests cover the overworld-to-battle flow using a test-only encounter fixture; Route 29 uses ROM encounter tables. Trainer battles share that core, with ROM trainer decoders, the trainer's send-out and switch AI, and explicit AI support checks. Map trainers (sight and talk) are started by game/world and run the ROM's own trainer scripts. Battles receive the script's draft party, bag and Pokédex, so a catch, a thrown ball or a nickname commits only with the script that started the battle. Introduce future modules with working features: game/audio, then features/link and features/walking. Party and bag models live in game/party; scripts change them only on their draft state. Rules must not import UI/browser modules. Transports and step providers must not write directly into world state.

The overworld extension keeps player travel mode, backup-warp data and Flash state in version-3 saves, with defaults for older saves. Forced terrain and turn-in-place timing run through the idle movement path. Dynamic warps and elevators retain their return destination. Block mutations use a per-map working copy and a revision counter, so rollback and the minimap see the same terrain. Surf, Cut and Strength use the ROM scripts. Select runs the registered item through the same module as the start menu. Start opens scripts/start-menu.ts, a native run on a script draft like the introduction: the party menu and pack change only the draft, which commits when the menu closes, and an item or field move that queues a script (the Bicycle, Cut, Surf …) starts it afterwards with the context the world checked (the Pokémon, the block to cut, the surfing state or a Sweet Scent encounter). The start menu, party menu and pocket cursors live in the world's memory, not in saves, and are cleared after each battle. scripts/evolution.ts translates EvolveAfterBattle, LearnLevelMoves and LearnMove over a draft. After a won battle the world runs it as an interlude on the battle's script draft, before the script receives the battle result; the start menu runs it inline for Rare Candy and stones. The evolve effect is a timed picture sequence that B may stop. The bedroom PC edits decorations on the script draft and reloads the room when the menu commits. scripts/intro.ts runs a new game's introduction as a native generator on a script draft; the session starts it only when nothing is saved, and the name, gender and clock offset commit when it ends. GameWorld reads a game clock: the device time plus that offset. scripts/pc.ts runs the Pokémon Center and house PCs on the same draft, so a refused step leaves boxes and items unchanged. party/storage.ts holds the boxes and the PC's items. A memscript effect queues Bill's call (like LoadMemScript); the world keeps it in memory only and runs it before scene scripts. The wall/poster Town Map is a ROM-backed canvas presented by a script effect.

TilesetAnimator translates the ROM animation command tables, advancing two video frames per overworld tick. It owns mutable graphics separately from the asset cache. The renderer records background color indices for grass sprite priority and applies special tileset palettes. Animation comparisons cover Johto, Ice Path, restless Ilex Forest and Tin Tower. The live forest engine flag selects the Celebi animation; the later story remains gated. Palette comparisons execute the original LoadMapPals routine across 192 cases. Movement traces compare event flags and check destinations across map-entry boundaries; controlled scenes also compare scripted pose visibility and lift.