// Central bridge between the imperative game core and the React UI. // // Data flows one way per concern: // game -> store state the UI renders (mode label, loco, telemetry, boot // milestones). Written via useGame.setState from game code; // high-frequency values (telemetry) are throttled at the // producer so React never re-renders at frame rate. // UI -> gameApi intents (robot selection, colour, locomotion, pause, // reset). The application binds it to RuntimeManager; // optional chaining keeps early-loading UI safe. import { create } from "zustand"; import { subscribeWithSelector } from "zustand/middleware"; export const useGame = create( subscribeWithSelector(() => ({ // Explicit simulation-shell lifecycle (legacy flags remain adapter-only). runtimePhase: "loading", paused: false, runtimeError: null, helpOpen: true, robotId: "microduck-legs", robots: [], inputBindings: [], presentationTheme: "comic", runtimeCapabilities: {}, actuationLabel: '', policyStatus: '', robotLicense: "", fallen: false, // Adapter-compatible boot diagnostics (no alternate boot screen). bootDone: false, bootFailed: false, // Game state mirrored for the UI modeLabel: "Run", loco: "legs", // "legs" | "rollers" - what the game is actually running locoWant: "legs", // what the quickbar asked for (game reconciles) locoSwitching: false, rollersLoading: false, // OSD line while the roller stack streams in variant: "classic", // Community move: null = all-official slots. Written by the game // (game.js loadCustomPolicyIntoGame / the watchdog) and by App.jsx for // the boot-time ?move= request. // { ref, name, status: "loading"|"active"|"error", error?, revertReason?, // kind?: "perpetual"|"episodic"|"script", slot?: "walk"|"sitstand"|"trick"|"script", // title?, author?, academy? (manifest.academy block) } customPolicy: null, padConnected: false, touchMode: false, ballActive: false, // Throttled telemetry block (4 Hz), bottom-right OSD telemetry: { fps: 0, ctrlHz: 0, speed: 0, odo: 0, peers: 0 }, })), ); // Imperative surface routed to the currently committed runtime. export const gameApi = {}; // Debug handles for the console / automated QA (same spirit as window.rl). if (typeof window !== "undefined") { window.__store = useGame; window.__gameApi = gameApi; } // Boot diagnostic log, shown on demand in the inline error panel. // Keeping it non-reactive avoids a render for every individual asset. // Entries: { label, status, raw, halt, progress } - status null = pending. export const bootLog = []; export const bootLine = (label) => { const entry = { label, status: null, raw: false, progress: null }; bootLog.push(entry); const done = (status = "OK") => { entry.status = status; }; done.progress = (p) => { entry.progress = p; }; return done; }; export const bootNote = (label) => { bootLog.push({ label, status: "", raw: true }); }; // Compatibility error helper for direct adapter callers; no alternate screen. export function bootHalt(detail) { const s = useGame.getState(); if (s.bootFailed) return; bootNote(`>> ${detail}`); bootLog.push({ label: "SYSTEM HALTED", status: "", raw: true, halt: true }); useGame.setState({ bootFailed: true, runtimePhase: "error", runtimeError: detail, }); }