Tiny world model generates games and stream ac plays them all on Pico 2W
Added the wasm for the networks
Sebastian Nae30e437860c0e
diff --git a/README.md b/README.mdindex 7385d31..604dc1e 100644--- a/README.md+++ b/README.md@@ -13,6 +13,7 @@ Every frame above is drawn by the dream network, not by game code. The actor net- [Quantization and the Pico](#quantization-and-the-pico)- [On the chip](#on-the-chip)- [Flashing a game and an agent](#flashing-a-game-and-an-agent)+- [In the browser](#in-the-browser)- [Reproduce](#reproduce) and [code layout](#code-layout)## The dream and the actor@@ -291,6 +292,29 @@ TWF_ENV=doomlite TWF_AGENT=doom_agent_roam_p0.1 cargo test --release # goldenKeys over USB: Breakout a/d/s, Doom-lite w/a/d/space/s, Flappy space, Pac-Man w/a/s/d, Pong w/s. Then `g` hands back to the agent, `1`–`4` set the loop count, `l` toggles on-device learning and `r` restarts it.+## In the browser++The same `no_std` int8 engine also compiles to WebAssembly (`tinyworldformer-rs/web/`). The module has no imports and no wasm-bindgen. It runs the firmware's loop: dream, agent, and stream-x learning on the fly. A static page draws the frames on a canvas.++```bash+rustup target add wasm32-unknown-unknown # once+cd tinyworldformer-rs/web+./build.sh # one module per env into www/ (or ./build.sh pong flappy)+python3 -m http.server -d www # open http://localhost:8000, ?env=pacman picks a game+```++- **Same dream:** with the same seed, the wasm build dreams the same episodes as the native build, frame for frame.+- **Speed:** 1–5 ms per dreamed frame, depending on the game.+- **Agents:** each module carries its game's default agent; `TWF_AGENT=... ./build.sh <env>` picks another.+- **Keys:** the same keys as on the Pico, plus arrow keys and on-screen buttons. `f` runs the dream as fast as it can, which is useful while learning (`l`).++`www/` is a plain static site, so it can be hosted as is. To publish it as a Hugging Face Space (the free CPU tier is enough, since everything runs in the visitor's browser):++```bash+uvx --from huggingface_hub hf auth login # once+./deploy.sh <user>/tinyworldformer # builds, creates the static Space if needed, uploads+```+## Reproduce```bash@@ -325,3 +349,4 @@ Settings that differ per game:| `src/tinyworldformer/rustexport/` | reusable PyTorch → `no_std` Rust export: QAT layers, the int8 spec, conversion, code generation || `tinyworldformer-rs/` | `no_std` int8 engine, dream loop, agent and learner; host demo (`src/main.rs`) || `tinyworldformer-rs/firmware/` | Pico 2 W firmware |+| `tinyworldformer-rs/web/` | WebAssembly build and the browser page (`www/`) |diff --git a/tinyworldformer-rs/web/.cargo/config.toml b/tinyworldformer-rs/web/.cargo/config.tomlnew file mode 100644index 0000000..f4e8c00--- /dev/null+++ b/tinyworldformer-rs/web/.cargo/config.toml@@ -0,0 +1,2 @@+[build]+target = "wasm32-unknown-unknown"diff --git a/tinyworldformer-rs/web/.gitignore b/tinyworldformer-rs/web/.gitignorenew file mode 100644index 0000000..80dfd6e--- /dev/null+++ b/tinyworldformer-rs/web/.gitignore@@ -0,0 +1,2 @@+www/*.wasm+www/envs.jsondiff --git a/tinyworldformer-rs/web/Cargo.lock b/tinyworldformer-rs/web/Cargo.locknew file mode 100644index 0000000..acb58ad--- /dev/null+++ b/tinyworldformer-rs/web/Cargo.lock@@ -0,0 +1,23 @@+# This file is automatically @generated by Cargo.+# It is not intended for manual editing.+version = 4++[[package]]+name = "libm"+version = "0.2.16"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"++[[package]]+name = "tinyworldformer-rs"+version = "0.1.0"+dependencies = [+ "libm",+]++[[package]]+name = "tinyworldformer-web"+version = "0.1.0"+dependencies = [+ "tinyworldformer-rs",+]diff --git a/tinyworldformer-rs/web/Cargo.toml b/tinyworldformer-rs/web/Cargo.tomlnew file mode 100644index 0000000..8637c9c--- /dev/null+++ b/tinyworldformer-rs/web/Cargo.toml@@ -0,0 +1,16 @@+[package]+name = "tinyworldformer-web"+version = "0.1.0"+edition = "2024"++[lib]+crate-type = ["cdylib"]++[dependencies]+tinyworldformer-rs = { path = "..", default-features = false }++[profile.release]+opt-level = 3+lto = "fat"+codegen-units = 1+panic = "abort"diff --git a/tinyworldformer-rs/web/build.sh b/tinyworldformer-rs/web/build.shnew file mode 100755index 0000000..538ee68--- /dev/null+++ b/tinyworldformer-rs/web/build.sh@@ -0,0 +1,23 @@+#!/usr/bin/env bash+# Build the browser demo: one WebAssembly module per exported env, into www/.+#+# ./build.sh every env, each with its game's default agent+# ./build.sh flappy pong only these+# TWF_AGENT=doom_agent_roam_p0.3 ./build.sh doomlite+# python3 -m http.server -d www then open http://localhost:8000+#+# Needs the wasm target once: rustup target add wasm32-unknown-unknown+set -euo pipefail+here="$(cd "$(dirname "$0")" && pwd)"+models="$here/../models"+envs=("$@")+[[ ${#envs[@]} -eq 0 ]] && envs=($(ls "$models/envs"))++cd "$here"+for env in "${envs[@]}"; do+ [[ -f "$models/envs/$env/weights.rs" ]] || { echo "no env '$env' (have: $(ls "$models/envs" | tr '\n' ' '))"; exit 1; }+ TWF_ENV="$env" cargo build --release+ cp target/wasm32-unknown-unknown/release/tinyworldformer_web.wasm "www/$env.wasm"+done+ls www/*.wasm | xargs -n1 basename | sed 's/\.wasm$//' | python3 -c 'import json,sys; print(json.dumps([l.strip() for l in sys.stdin]))' > www/envs.json+echo "built: $(cat www/envs.json)"diff --git a/tinyworldformer-rs/web/deploy.sh b/tinyworldformer-rs/web/deploy.shnew file mode 100755index 0000000..4ca6464--- /dev/null+++ b/tinyworldformer-rs/web/deploy.sh@@ -0,0 +1,40 @@+#!/usr/bin/env bash+# Publish the browser demo as a static Hugging Face Space. Everything runs in the visitor's+# browser (WebAssembly on the CPU), so the free cpu-basic Space with no server is enough.+#+# ./deploy.sh <user>/<space> build every env, create the Space if needed, upload+# ./deploy.sh <user>/<space> --no-build upload what is already in www/+#+# Needs a Hugging Face login once: uvx --from huggingface_hub hf auth login+set -euo pipefail+here="$(cd "$(dirname "$0")" && pwd)"+space="${1:?usage: ./deploy.sh <user>/<space> [--no-build]}"+hf() { uvx --from huggingface_hub hf "$@"; }++cd "$here"+[[ "${2:-}" == "--no-build" ]] || ./build.sh+ls www/*.wasm >/dev/null 2>&1 || { echo "nothing built in www/: run ./build.sh"; exit 1; }++# The Space is www/ plus the README whose front matter tells Hugging Face it is a static site.+stage="$here/target/space"+rm -rf "$stage" && mkdir -p "$stage" && cp www/index.html www/envs.json www/*.wasm "$stage/"+cat > "$stage/README.md" <<'EOF'+---+title: tinyworldformer+emoji: 🎮+colorFrom: indigo+colorTo: yellow+sdk: static+app_file: index.html+pinned: false+short_description: Games dreamed by a tiny int8 transformer, in your browser+---++Five games (Breakout, Doom-lite, Flappy Bird, Pac-Man lite, Pong) with no game code: every frame+is dreamed by the int8 looped transformer that runs on a Raspberry Pi Pico 2 W, compiled to+WebAssembly and run on your CPU. A stream-x agent plays; press any game key to take over.+EOF++hf repos create "$space" --type space --space-sdk static --exist-ok+hf upload "$space" "$stage" . --repo-type space --delete "*.wasm" --commit-message "Update the dreamed games"+echo "live at https://huggingface.co/spaces/$space"diff --git a/tinyworldformer-rs/web/src/lib.rs b/tinyworldformer-rs/web/src/lib.rsnew file mode 100644index 0000000..ecbfee2--- /dev/null+++ b/tinyworldformer-rs/web/src/lib.rs@@ -0,0 +1,211 @@+//! WebAssembly build: the dreamed game in a browser, with exactly the int8 engine that runs on+//! the Pico. Same loop as the firmware's core 1 (`../firmware/src/main.rs`); the page+//! (`www/index.html`) calls `tick` once per frame, sends keys with `key`, and draws `rgba`.+//!+//! No wasm-bindgen: plain `extern "C"` exports, so the module needs no imports at all. The env+//! and agent are baked in at build time like on the Pico (`TWF_ENV` / `TWF_AGENT`, see+//! `../build.rs`); `build.sh` builds one module per env.+#![no_std]++use core::ptr::addr_of_mut;++use tinyworldformer_rs::agent;+use tinyworldformer_rs::dream::{color, Dream};+use tinyworldformer_rs::learner::Learner;+use tinyworldformer_rs::model::Engine;+use tinyworldformer_rs::weights::{GAME, H, LOOPS, MAX_STEPS, W};+use tinyworldformer_rs::{AGENT_NAME, ENV_NAME};++const DOOM: bool = matches!(GAME.as_bytes(), b"doomlite");+const FLAPPY: bool = matches!(GAME.as_bytes(), b"flappy");+const PACMAN: bool = matches!(GAME.as_bytes(), b"pacman");+const PONG: bool = matches!(GAME.as_bytes(), b"pong");+/// Doom-lite, Flappy Bird and Pac-Man take one action per press (Pac-Man then keeps going);+/// Breakout and Pong hold the last direction.+const PER_PRESS: bool = DOOM || FLAPPY || PACMAN;++#[panic_handler]+fn panic(_: &core::panic::PanicInfo) -> ! {+ core::arch::wasm32::unreachable()+}++/// Everything the loop keeps between frames (the locals of the firmware's `dream_loop`).+struct State {+ dream: Dream,+ x: [f32; tinyworldformer_rs::agent_weights::OBS],+ score: i32,+ wins: i32,+ episode: u32,+ learn_steps: u32,+ recent: [i32; 10],+ n_recent: usize,+ human: bool,+ key_action: u8,+ learning: bool,+ loops: u8,+}++// Single-threaded: only the exports below touch these, one call at a time.+static mut ENGINE: Engine = Engine::new();+static mut LEARNER: Learner = Learner::zeroed(1);+static mut STATE: Option<State> = None;+static mut RGBA: [u8; H * W * 4] = [0; H * W * 4];++fn state() -> &'static mut State {+ // SAFETY: wasm is single-threaded and no reference outlives an export call.+ unsafe { (*addr_of_mut!(STATE)).as_mut().expect("init first") }+}++fn learner() -> &'static mut Learner {+ // SAFETY: as above.+ unsafe { &mut *addr_of_mut!(LEARNER) }+}++fn draw(grid: &[u8; H * W]) {+ // SAFETY: as above.+ let rgba = unsafe { &mut *addr_of_mut!(RGBA) };+ for (i, (px, &tok)) in rgba.chunks_exact_mut(4).zip(grid).enumerate() {+ let (r, g, b) = color(i / W, tok);+ px.copy_from_slice(&[r, g, b, 255]);+ }+}++/// Start (or restart) with a seed from the page.+#[unsafe(no_mangle)]+pub extern "C" fn init(seed: u32) {+ let seed = seed | 1;+ let dream = Dream::new(seed);+ let learner = learner();+ learner.reset(seed ^ 0x5eed);+ let x = learner.normalize(&agent::observe(&dream.grid), true);+ draw(&dream.grid);+ // SAFETY: as above.+ unsafe {+ *addr_of_mut!(STATE) = Some(State {+ dream, x, score: 0, wins: 0, episode: 0, learn_steps: 0, recent: [0; 10], n_recent: 0,+ human: false, key_action: 0, learning: false, loops: if LOOPS < 2 { LOOPS as u8 } else { 2 },+ });+ }+}++/// One frame: the agent (or the player) acts, the network dreams the next frame, the learner+/// learns from it when on. Same order as the firmware.+#[unsafe(no_mangle)]+pub extern "C" fn tick() {+ let s = state();+ let learner = learner();+ // SAFETY: as above.+ let engine = unsafe { &mut *addr_of_mut!(ENGINE) };+ let learning = s.learning && !s.human;+ let action = match (s.human, PER_PRESS) {+ (true, true) => core::mem::take(&mut s.key_action) as usize, // consumed: one step per press+ (true, false) => s.key_action as usize,+ (false, _) if learning => learner.act(&s.x, false),+ (false, _) => agent::act(&s.dream.grid),+ };+ let (reward, done) = s.dream.step(engine, action, s.loops as usize, MAX_STEPS);+ let x2 = learner.normalize(&agent::observe(&s.dream.grid), learning);+ if learning {+ let terminal = done && s.dream.steps < MAX_STEPS; // out of lives, not out of time+ let r = learner.scale_reward(reward as f32, terminal);+ learner.update(&s.x, action, r, &x2, terminal, done);+ s.learn_steps += 1;+ }+ s.x = x2;+ s.score += reward;+ s.wins += reward.max(0);+ draw(&s.dream.grid);+ if done {+ s.recent[s.n_recent % 10] = s.wins;+ s.n_recent += 1;+ s.dream.reset();+ s.x = learner.normalize(&agent::observe(&s.dream.grid), learning);+ (s.score, s.wins) = (0, 0);+ s.episode += 1;+ }+}++/// A key, as the firmware reads them over USB serial (the page maps arrows to w/a/s/d).+#[unsafe(no_mangle)]+pub extern "C" fn key(key: u8) {+ let s = state();+ let mut set_action = |a: u8| {+ s.key_action = a;+ s.human = true;+ };+ match key {+ b'w' if PONG => set_action(1),+ b's' if PONG => set_action(2),+ b'w' if PACMAN => set_action(1),+ b'd' if PACMAN => set_action(2),+ b's' if PACMAN => set_action(3),+ b'a' if PACMAN => set_action(4),+ b' ' | b'w' if FLAPPY => set_action(1),+ b's' => set_action(0),+ b' ' => set_action(if DOOM { 4 } else { 0 }),+ b'w' if DOOM => set_action(1),+ b'a' if !FLAPPY => set_action(if DOOM { 2 } else { 1 }),+ b'd' if !FLAPPY => set_action(if DOOM { 3 } else { 2 }),+ b'g' => s.human = false,+ b'l' => s.learning = !s.learning,+ b'r' => {+ learner().reset(s.dream.rng.next_u32() | 1);+ (s.learn_steps, s.n_recent) = (0, 0);+ s.x = learner().normalize(&agent::observe(&s.dream.grid), true);+ }+ b'1'..=b'9' if ((key - b'0') as usize) <= LOOPS => s.loops = key - b'0',+ _ => {}+ }+}++/// The current frame, `height() * width()` RGBA pixels.+#[unsafe(no_mangle)]+pub extern "C" fn rgba() -> *const u8 {+ addr_of_mut!(RGBA).cast()+}++#[unsafe(no_mangle)]+pub extern "C" fn width() -> u32 { W as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn height() -> u32 { H as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn max_loops() -> u32 { LOOPS as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn human_frame_ms() -> u32 { tinyworldformer_rs::weights::HUMAN_FRAME_MS as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn score() -> i32 { state().score }+#[unsafe(no_mangle)]+pub extern "C" fn wins() -> i32 { state().wins }+#[unsafe(no_mangle)]+pub extern "C" fn lives() -> u32 { state().dream.lives as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn episode() -> u32 { state().episode }+#[unsafe(no_mangle)]+pub extern "C" fn loops() -> u32 { state().loops as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn human() -> u32 { state().human as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn learning() -> u32 { state().learning as u32 }+#[unsafe(no_mangle)]+pub extern "C" fn learn_steps() -> u32 { state().learn_steps }++/// Mean score of the last 10 finished episodes (0 before the first).+#[unsafe(no_mangle)]+pub extern "C" fn avg10() -> i32 {+ let s = state();+ let n = s.n_recent.min(10);+ if n == 0 { 0 } else { s.recent[..n].iter().sum::<i32>() / n as i32 }+}++/// Names, as (pointer, length) pairs: `name_ptr(i)` / `name_len(i)` with 0 game, 1 env, 2 agent.+fn name(i: u32) -> &'static str {+ match i {+ 0 => GAME,+ 1 => ENV_NAME,+ _ => AGENT_NAME,+ }+}+#[unsafe(no_mangle)]+pub extern "C" fn name_ptr(i: u32) -> *const u8 { name(i).as_ptr() }+#[unsafe(no_mangle)]+pub extern "C" fn name_len(i: u32) -> u32 { name(i).len() as u32 }diff --git a/tinyworldformer-rs/web/www/index.html b/tinyworldformer-rs/web/www/index.htmlnew file mode 100644index 0000000..af0d8e6--- /dev/null+++ b/tinyworldformer-rs/web/www/index.html@@ -0,0 +1,144 @@+<!doctype html>+<html lang="en">+<head>+<meta charset="utf-8">+<meta name="viewport" content="width=device-width, initial-scale=1">+<title>tinyworldformer</title>+<style>+ :root { --bg: #f6f5f2; --fg: #1d1d1f; --muted: #6b6b70; --line: #d9d8d3; --btn: #ffffff; --accent: #c2410c; }+ @media (prefers-color-scheme: dark) { :root { --bg: #111113; --fg: #ececee; --muted: #9a9aa2; --line: #2c2c31; --btn: #1c1c20; --accent: #fb923c; } }+ * { box-sizing: border-box; }+ body { margin: 0; background: var(--bg); color: var(--fg); font: 15px/1.45 ui-sans-serif, system-ui, sans-serif; }+ main { max-width: 760px; margin: 0 auto; padding: 24px 16px 48px; }+ h1 { font-size: 22px; margin: 0 0 4px; }+ p.sub { margin: 0 0 18px; color: var(--muted); }+ .bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }+ select, button { font: inherit; color: var(--fg); background: var(--btn); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; }+ button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }+ canvas { display: block; width: 100%; max-width: 560px; margin: 0 auto; image-rendering: pixelated; border-radius: 6px; background: #000; touch-action: none; }+ .stats { font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; margin: 12px 0; white-space: pre-wrap; min-height: 3em; }+ .keys { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin: 12px 0; }+ .keys button { min-width: 64px; padding: 10px 14px; }+ .help { color: var(--muted); font-size: 13px; }+ kbd { font: 12px ui-monospace, monospace; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }+</style>+</head>+<body>+<main>+ <h1>tinyworldformer</h1>+ <p class="sub">There is no game code here. Every frame is dreamed by the int8 looped transformer that runs on the Pico 2 W, compiled to WebAssembly. Press any game key to take over from the agent.</p>+ <div class="bar">+ <select id="env" aria-label="game"></select>+ <button id="agent" title="g">agent plays</button>+ <button id="learn" title="l" aria-pressed="false">learn on the fly</button>+ <button id="relearn" title="r">learn from scratch</button>+ <select id="loops" aria-label="loops" title="1-4"></select>+ <button id="fast" title="f" aria-pressed="false">fast</button>+ </div>+ <canvas id="screen"></canvas>+ <div class="stats" id="stats">loading…</div>+ <div class="keys" id="pad"></div>+ <p class="help" id="help"></p>+</main>+<script>+// Per game: the keys the firmware reads (see web/src/lib.rs `key`) and what they do.+const KEYS = {+ breakout: [['a', '◀ left'], ['s', 'stop'], ['d', 'right ▶']],+ doomlite: [['a', '↺ turn'], ['w', '▲ forward'], ['d', 'turn ↻'], [' ', 'fire'], ['s', 'wait']],+ flappy: [[' ', 'flap']],+ pacman: [['a', '◀'], ['w', '▲'], ['s', '▼'], ['d', '▶'], [' ', 'keep going']],+ pong: [['w', '▲ up'], [' ', 'stop'], ['s', '▼ down']],+};+const WON = { breakout: 'bricks', doomlite: 'kills', flappy: 'pipes', pacman: 'pellets', pong: 'points' };+const ARROWS = { ArrowUp: 'w', ArrowLeft: 'a', ArrowDown: 's', ArrowRight: 'd' };++const $ = id => document.getElementById(id);+const screen = $('screen'), ctx = screen.getContext('2d');+let wasm = null, game = '', fast = false, lastStep = 0, modelMs = 0, generation = 0;++async function load(env) {+ const gen = ++generation;+ const { instance } = await WebAssembly.instantiateStreaming(fetch(`${env}.wasm`), {})+ .catch(async () => WebAssembly.instantiate(await (await fetch(`${env}.wasm`)).arrayBuffer(), {})); // host without application/wasm+ if (gen !== generation) return; // a newer pick won+ const e = instance.exports;+ e.init((Math.random() * 2 ** 32) >>> 0);+ const str = i => new TextDecoder().decode(new Uint8Array(e.memory.buffer, e.name_ptr(i), e.name_len(i)));+ wasm = e;+ game = str(0);+ screen.width = e.width();+ screen.height = e.height();+ screen.style.width = `min(100%, 560px, ${(70 * e.width() / e.height()).toFixed(1)}vh)`; // tall games fit the screen+ $('loops').innerHTML = Array.from({ length: e.max_loops() }, (_, i) => `<option value="${i + 1}">${i + 1} loop${i ? 's' : ''}</option>`).join('');+ $('pad').innerHTML = '';+ for (const [k, label] of KEYS[game] ?? []) {+ const b = document.createElement('button');+ b.textContent = label;+ b.addEventListener('pointerdown', ev => { ev.preventDefault(); press(k); });+ $('pad').append(b);+ }+ $('help').innerHTML = `env <b>${str(1)}</b>, pretrained agent <b>${str(2)}</b>. Keys: `+ + (KEYS[game] ?? []).map(([k, l]) => `<kbd>${k === ' ' ? 'space' : k}</kbd> ${l}`).join(', ')+ + ` (arrows work too); <kbd>g</kbd> agent, <kbd>l</kbd> learning, <kbd>r</kbd> learn from scratch, <kbd>1</kbd>-<kbd>${e.max_loops()}</kbd> loops, <kbd>f</kbd> fast.`;+ draw();+}++function press(k) {+ if (!wasm) return;+ if (k === 'f') { fast = !fast; return; }+ wasm.key(k.charCodeAt(0));+}++function draw() {+ const px = new Uint8ClampedArray(wasm.memory.buffer, wasm.rgba(), wasm.width() * wasm.height() * 4);+ ctx.putImageData(new ImageData(px, wasm.width(), wasm.height()), 0, 0);+ const who = wasm.human() ? 'you' : wasm.learning() ? 'stream-x LEARNING' : 'pretrained stream-x agent';+ $('stats').textContent =+ `score ${wasm.score()} ${WON[game] ?? 'wins'} ${wasm.wins()} lives ${wasm.lives()} episode ${wasm.episode()} | ${who}\n` ++ `${wasm.loops()} loops: ${modelMs.toFixed(2)} ms per dreamed frame${fast ? ' (fast)' : ''}\n` ++ (wasm.learning() || wasm.learn_steps() ? `learned for ${wasm.learn_steps()} steps, mean score of the last 10 episodes ${wasm.avg10()}` : '');+ $('agent').setAttribute('aria-pressed', String(!wasm.human()));+ $('learn').setAttribute('aria-pressed', String(!!wasm.learning()));+ $('fast').setAttribute('aria-pressed', String(fast));+ $('loops').value = wasm.loops();+}++// Human speed: one dreamed frame every HUMAN_FRAME_MS. Fast (not while you play): as many as fit in ~12 ms.+function frame(now) {+ requestAnimationFrame(frame);+ if (!wasm) return;+ const turbo = fast && !wasm.human();+ if (!turbo && now - lastStep < wasm.human_frame_ms()) return;+ lastStep = now;+ const t0 = performance.now();+ let n = 0;+ do { wasm.tick(); n++; } while (turbo && performance.now() - t0 < 12);+ modelMs = (performance.now() - t0) / n;+ draw();+}++addEventListener('keydown', ev => {+ if (ev.target.tagName === 'SELECT' || ev.ctrlKey || ev.metaKey || ev.altKey) return;+ const k = ARROWS[ev.key] ?? ev.key.toLowerCase();+ if (k.length !== 1) return;+ ev.preventDefault();+ press(k);+});+screen.addEventListener('pointerdown', () => window.focus()); // keys reach the page inside the Space's iframe+$('agent').onclick = () => press('g');+$('learn').onclick = () => press('l');+$('relearn').onclick = () => press('r');+$('fast').onclick = () => press('f');+$('loops').onchange = ev => { press(ev.target.value); ev.target.blur(); };+$('env').onchange = ev => { load(ev.target.value); ev.target.blur(); };++fetch('envs.json').then(r => r.json()).then(envs => {+ const pick = new URLSearchParams(location.search).get('env');+ $('env').innerHTML = envs.map(e => `<option>${e}</option>`).join('');+ $('env').value = envs.includes(pick) ? pick : envs.includes('breakout') ? 'breakout' : envs[0];+ load($('env').value);+}).catch(err => { $('stats').textContent = `could not load the models (${err}); run ./build.sh and serve www/ over http`; });+requestAnimationFrame(frame);+</script>+</body>+</html>