Replace the 40-element pooled-HTMLAudioElement shell-fire sound in app.cljs with a new robotwar.audio namespace built on the Web Audio API: - AudioContext constructed lazily on the first Enter keypress (autoplay policy), with a master GainNode ready for Slice B's volume mixing. - Sound files fetched as ArrayBuffers at page load; decoding is deferred until the context exists, since decodeAudioData requires a context and detaches its input buffer (decode exactly once). - play! creates a fresh AudioBufferSourceNode per playback (unlimited polyphony) and no-ops silently if the buffer isn't ready. - ogg-with-mp3-fallback canPlayType check preserved. Spec updated with resolved TACTICAL items and a Slice A addendum. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
RobotWar
A reverse-engineered version (in Clojure/ClojureScript) of Silas Warner's 1981 Apple II game, RobotWar.
In RobotWar, players write programs in a Forth-like language created specifically for the game. Those programs are compiled to virtual-machine code and used as AI brains for robots battling in an arena.
Resources:
- Original manual: ftp://ftp.apple.asimov.net/pub/apple_II/documentation/games/misc/Robotwar.pdf (also in this repo at resources/manual.txt)
- Overview article: http://www.filfre.net/2012/01/robot-war/
Current project layout
- Engine code:
src/main/robotwar/*.cljc(shared by JVM + CLJS) - Browser app:
src/main/robotwar/*.cljs - JVM tests:
src/test/robotwar/*.clj - Static site/assets:
public/
Program files and manifests
Robot programs are plain text files in:
public/programs/*.rw
Program discovery is manifest-driven:
public/programs/programs-live.json— programs shown in browser runtimepublic/programs/programs-test.json— test/dev fixturespublic/programs/programs.json— compatibility alias (currently mirrors live)
The browser app loads names from programs-live.json, then loads each selected .rw source file.
Deterministic ordering
Displayed program lists should be deterministic. Keep manifest ordering stable and intentional (typically lexicographic unless deliberately curated).
Running locally
Prerequisites
- Java (JDK 11+ recommended)
- Clojure CLI (
clj/clojure) - Node.js + npm
Install
git clone git@github.com:richardharrington/robotwar.git
cd robotwar
npm install
Dev workflow
Terminal 1 (watch build):
npx shadow-cljs watch app
Terminal 2 (serve static files from public/):
npx serve public -l 3000
Open http://localhost:3000/.
Tests
Unified test command:
npm test
This runs JVM tests and CLJS Node tests.
Build / release
Compile app:
npx shadow-cljs compile app
Production build:
npx shadow-cljs release app
Compiled assets are written to public/js/cljs-runtime/.
Deploying to Netlify
This repo includes netlify.toml with:
- Build command:
npx shadow-cljs release app - Publish directory:
public/
In Netlify, connect the repo and deploy. The checked-in netlify.toml should be auto-detected.
Architecture (engine overview)
Assembler
Lexes, parses, and assembles RobotWar source code into VM object code made of command/argument pairs.
Brain
Interprets and executes robot object code each tick.
Register
Implements accumulator/register behavior, including storage and I/O-like register semantics.
Robot
Holds robot state within the arena and coordinates per-robot ticking behavior.
World
Ticks robots, then ticks shells in flight, advancing the combined world state.
Example RobotWar source
; Note: # means !=
256 TO RANDOM ; All random numbers will now have as their maximum
; the width and height of the arena (in meters).
LOOP
0 TO SPEEDX TO SPEEDY ; Stop the robot (X and Y).
RANDOM TO A ; Store a random X-coordinate in the arena.
RANDOM TO B ; Store a random Y-coordinate in the arena.
MOVE
IF A # X GOSUB MOVEX ; If we're moving in the X direction, recalibrate SPEEDX.
TO N ; N is for no-op. (needed because there's no ELSE command).
IF B # Y GOSUB MOVEY ; If we're moving in the Y direction, recalibrate SPEEDY.
IF A = X GOTO LOOP ; A = X and B = Y, so we've stopped moving, so start over.
GOTO MOVE ; Continue to move.
MOVEX
A - X TO SPEEDX ; Distance to target sets X velocity.
ENDSUB
MOVEY
B - Y TO SPEEDY ; Distance to target sets Y velocity.
ENDSUB
Implementation gaps
This project is a partial reimplementation of the original Apple IIc game from 1982. The original manual is comprehensive but leaves some constants and behaviors unspecified. Gaps fall into two categories:
- Features clearly defined in the manual but not yet implemented
- Features the manual leaves vague — requiring arbitrary decisions about constants and behavior
Already implemented
- 256×256 arena, robot physics (position/aim/velocity), shell trajectories with timed fuses
- Assembler/VM: full instruction set, label resolution, call stack, accumulator semantics
- Registers: A–W/Z storage, X/Y read-only, AIM/SPEEDX/SPEEDY/DAMAGE, SHOT with cooldown, INDEX/DATA, RANDOM
- Combat: shell explosions with quadratic damage falloff (
30 * max(0, 1 - d/21)²within 21m), self-damage allowed, robot death (:alive?flag), victory/tie detection - Frontend: canvas renderer, animation loop with fast-forward, program loading, victory overlay, robot-status legend
Category 1 — Defined in manual, not yet implemented
These have explicit manual descriptions and TODO comments in code:
- RADAR register — Manual: directional beam, negative return on robot hit, positive on wall/miss. Stub exists in
register.cljc. - Wall collisions — Manual states walls prevent passage and cause damage. TODOs in
robot.cljc. Constants defined:MAX-WALL-DAMAGE 15.0,V-MAX 25.5. - Collision damage by angle — Current: flat 1-point damage. Manual: head-on = 25% damage scaled by angle. Constant defined:
MAX-COLLISION-DAMAGE 25.0. - Scoring system — Manual: 1 point per destroyed opponent per survivor, cumulative across battles. Not implemented.
- Multi-battle matches — Manual options 7 and 8 (schedule/resume matches). Not implemented.
- Assembler errors — Manual lists 8 specific errors. Current: generic "Invalid word or symbol".
- Number range validation — Manual specifies –1024..+1024 as valid range (LARGE NUMBER error). No validation in assembler.
- Program length cap — Manual: 256 instruction maximum. Not enforced.
- Recursion prevention — Call-stack model implies no recursion; not explicitly prevented.
- Test bench / simulator — Manual chapter: step-through, register tracer, fake radar/damage keys. No UI exists.
- Sound on/off toggle — Manual "option 4". Audio wired but no UI control.
- AIM-aligned shell origin —
shell.cljcTODO: start shells at robot radius offset. Currently starts at center.
Category 2a — Decisions made (constants in constants.cljc)
Arbitrary values chosen where the manual is silent. Revisit if play-testing suggests:
- Cannon reload:
GAME-SECONDS-PER-SHOT 20.0(manual: "cooling period") - Shell speed:
SHELL-SPEED 25.0m/s (manual silent on velocity) - Damage falloff: Quadratic,
MAX-BLAST-DAMAGE 30.0,BLAST-RADIUS 21.0 - Collision formula:
MAX-COLLISION-DAMAGE 25.0,V-MAX 25.5, scales as(approach_speed/51)² - Wall damage:
MAX-WALL-DAMAGE 15.0, scales as(v_perp/25.5)² - Robot size:
ROBOT-RADIUS 7.0m (manual claims 1.5m square — discrepancy unresolved) - VM speed: 1 instruction per world tick (manual gives no CPU/world-time ratio)
- Tick rate:
*GAME-SECONDS-PER-TICK* 0.033(~30 Hz) - RANDOM range:
[0, limit)— limit itself unreachable perrand-intsemantics - SPEEDX/SPEEDY bounds: Manual says –255..+255; current code accepts any value
- Starting positions: Pure
rand; manual silent on placement method
Category 2b — Decisions pending (blocked on Category 1 implementation)
These require choices when implementing remaining features:
- Radar beam geometry — Single ray vs. wedge width? Affects detection difficulty.
- Radar wall return — Manual implies positive number = wall distance, never explicit.
- Robot-shell overlap — Does a shell explode at t=0 if fired into own body while moving?