BUILD REQUEST - PL-189 Tabletop Railway - please build this now I'm sending you this as my request: please build the one-file HTML app described below. Start now and do the whole build in your answer to this message. - This is a build job, not a document to summarize, review or discuss. Don't ask me anything first: if a detail is unclear, pick the most sensible option and keep going. - The spec is everything between the BEGIN SPEC and END SPEC lines. Follow all of it exactly. - If your tool can create files, also save the finished file as PL-189-tabletop-railway.html. ===== BEGIN SPEC ===== TABLETOP RAILWAY: Switches, Signals and Three Trains That Never Collide HOOK OBJECT: window.__rail Build "TABLETOP RAILWAY": a Three.js 3D model-train layout on a hobby-room table. Two oval loops are joined by two crossover switches and guarded by six block signals; three toy trains of four cars each run on a fixed track graph under a dispatcher that reserves blocks, throws switches and holds trains at red signals. It is fun to keep watching because the trains queue at signals, the switch blades snap over just in time, and not one of them ever touches another. LOOK - Style: warm hobby-room diorama with soft shadows. Palette: floor #5C4A3A, back wall #D9C9A8, table edge #B07A45, table top felt #4C8A52, ballast strip #9B9488, sleepers #5A3F2A, rails #C9D0D8, signal mast #2B2B2B, signal lamps red #FF3B30 and green #34D058 (emissive when lit, dark #3A1512 or #123A1C when off), trains Cedar #D9423A, Birch #2F6FD6 and Maple #F2C230 with wheels #222222, station house #E8D8B8 with roof #A8452F, trees trunk #5A3F2A with cone #2E6B3A, HUD bar #14100C with text #FFE9C7. - Plan coordinates: every position in this prompt and every hook uses the plan (x, y) of the tabletop, in meters, x to the right and y "up" the plan; the world position is (x, 0, -y), so the plan looks upright from the default camera. The table top is the rectangle x -7 to 7, y -4.5 to 4.5 (14 x 9) at height 0, 0.4 thick, with four legs. - Track (centerlines), all trains travel counter-clockwise. Outer loop: bottom straight y = -3 from x = -3 to 3, a half circle of radius 3 around (3, 0), top straight y = 3 from x = 3 to -3, a half circle of radius 3 around (-3, 0) (length 12 + 6 pi = 30.850). Inner loop: the same with radius 1.6 (bottom y = -1.6, top y = 1.6, length 12 + 3.2 pi = 22.053). Connector C1: a cubic Bezier with control points (-2, -3), (-1, -3), (-1, -1.6), (0, -1.6). Connector C2: control points (2, 1.6), (1, 1.6), (1, 3), (0, 3). Each connector is 2.557 long. - Segments (each one signal block, directed): O1 = outer loop from (-2, -3) counter-clockwise to (0, 3), length 17.425; O2 = outer from (0, 3) to (-2, -3), length 13.425; I1 = inner from (0, -1.6) to (2, 1.6), length 9.027; I2 = inner from (2, 1.6) to (0, -1.6), length 13.027; C1 from (-2, -3) to (0, -1.6); C2 from (2, 1.6) to (0, 3). Nodes: switch S1 at (-2, -3) lets O2 continue straight into O1 or diverge into C1; switch S2 at (2, 1.6) lets I1 continue straight into I2 or diverge into C2; merge M1 at (0, -1.6) joins C1 and I2 into I1; merge M2 at (0, 3) joins C2 and O1 into O2. Track is built from instanced sleepers (0.4 x 0.06, every 0.15), two rails (gauge 0.30) and a 0.5 wide ballast strip. A switch has visible point blades that slide over in 0.4 s, and a small lever box beside it (0.35 high). - Trains: Cedar, Birch, Maple, each a locomotive 0.5 long and 0.30 wide plus 3 cars 0.4 long, gaps 0.05, total length 1.85. The front of the locomotive is the head. Car centers lie 0.25, 0.75, 1.20 and 1.65 behind the head, measured along the route (walking back through earlier segments when needed). Routes (repeating): Cedar O1, O2; Birch I1, I2; Maple O2, C1, I1, C2. Start (all at rest, whole train inside one segment): Cedar head on O1 at offset 12.0 (offset = distance from the segment start), Birch head on I2 at offset 6.0, Maple head on O2 at offset 7.0. Each train's checkpoint segment is Cedar O1, Birch I1, Maple C1. - Signals: one mast beside the track at the end of each of the six segments, with a red and a green lamp (6 masts, 12 lamps). Scenery: the station house (1.6 x 0.9 x 0.7) centered at plan (0, 0) inside the inner loop and 10 trees at seeded positions on the table outside the outer loop, each chosen by rejection sampling at least 1.0 from every track centerline, with |x| <= 6.8 and |y| <= 4.3. - Camera: perspective, vertical field of view 40 degrees, looking at (0, 0, 0), position = D * (cos(el) sin(az), sin(el), cos(el) cos(az)). Default az 0, el 60, D 16 when width >= height, otherwise D 48. The canvas fills the window; a HUD bar 44 px high sits at the bottom with two lines of 14 px monospace (about 8.4 px per character): "CHECKPOINTS 12 STOPS 5" (23 characters, 193 px) and "SWITCHES 9 REFUSED 1 HOLDS 2" (30 characters, 252 px), both under the 374 px available at 390 px wide. - FIRST FRAME (t = 0, no input): the whole table from the default camera, all three trains standing at their start positions, all lamps showing, both switches straight, the HUD bar at the bottom with zeros. The projected box of the table (its 8 corners, x -7 to 7, height -0.4 to 0, y -4.5 to 4.5) is about 69% of the window height (499 px) and 79% of the width (1007 px) at 1280x720, ending 29 px above the HUD bar. On a phone in portrait the table is wide, so the same box is only about 23% of the height but 91% of the width: that is an accepted exception. Nothing overlaps the table. MOTION / PLAY - Controls: drag (mouse or touch) orbits: dragging right by dx px gives az = az - 0.3 dx, dragging down by dy px gives el = el + 0.3 dy, el clamped to 10 to 85; wheel or pinch zooms D = clamp(D * 1.1^(deltaY / 100), 8, 60). A tap or click picks the nearest of the two switch levers and the three locomotives whose projected center is within 28 CSS px: a lever is a user throw (throwSwitch), a locomotive holds that train for 3.0 s (holdTrain). - Dispatcher (exact rules, run every 1/60 s tick, trains handled in the order Cedar, Birch, Maple): 1. For a train with its head on segment X, Y is the next segment of its route and dEnd = length(X) - head offset. 2. A segment is occupied by a train while any part of that train (head to tail) lies on it. Only one train may be on, or have reserved, a segment at a time (a train never blocks itself). 3. Reservation: when dEnd <= 5.0 and the train holds no reservation for Y, it reserves Y if Y is not occupied by another train and not reserved by another train. A reservation ends when the train has entered Y and its tail has left Y. 4. Switches: S1 serves trains in O2, S2 serves trains in I1. As soon as a train in X holds the reservation for Y and the switch is not set toward Y, the dispatcher throws it: it takes 0.4 s (moving) and switchThrows += 1. A switch cannot be thrown, by the dispatcher or the user, while it is moving or while any part of another train is within 1.0 (measured along the track) of its switch point. A user throw is also refused while a train in X holds a reservation for its next segment. An accepted user throw toggles the switch (switchThrows += 1); a refused one does nothing but refusals += 1. 5. A train is cleared to leave X when it holds the reservation for Y and (X does not end at a switch, or the switch is set toward Y and not moving). The signal at the end of X shows GREEN for a cleared train and RED for a train that is not cleared. signals() reports for each segment id: "R" when some train's head is on it, within 5.0 of its end, and that train is not cleared; otherwise "G". 6. Speed: top speed 2.4 (1.2 while the head is on C1 or C2, which are only 2.557 long, less than the 1.92 braking distance from 2.4 plus the 1.3 stop line), acceleration 1.2, braking 1.5 per second squared. Each tick v = min(v + 1.2 dt, limit, stop profile, approach profile): limit = 2.4, or 1.2 on C1 and C2; stop profile (only when the train is not cleared) = sqrt(2 x 1.5 x max(0, dEnd - 1.3)); approach profile (only when the next segment Y is C1 or C2 and the head is not) = sqrt(1.2^2 + 2 x 1.5 x dEnd). So every slowdown is smooth: a train never loses more than 0.08 of speed in one tick except in the last tick before it stops. The stop line is 1.3 before the end of every segment: the two tracks of a merge are only 0.6 apart at 1.2 before the node, so a waiting train must stay clear of the train that is passing. A held train also has v = min(v, previous v - 1.5 dt), never below 0. The head only passes the end of X while cleared. 7. Counters as listed below. - ATTRACT MODE: the dispatcher runs from load; with no input the world runs 28.0 s (28.0 = 28; there is no separate hold phase) and then cuts back to the t = 0 state (trains at their start positions at rest, switches straight, reservations cleared) with the counters keeping their values, and runs again. Any input takes over (the cut is suspended); the world cuts back to the t = 0 state and the 28 s loop restarts after 8 s with no input. Hook calls are not user input. - The hook also exposes: reset() (full reset including counters), attract(on) (false turns the demo off), trains() returning an array of {name, speed, distance (total distance the head has traveled since the last cut-back), held (boolean), head: {seg, offset}, cars: [{x, y} x 4]}, signals() as described, switches() returning {S1: {pos: "straight" | "diverge", moving}, S2: {...}}, segments() returning [{id, length}], onTrack(x, y) (the distance from that plan point to the nearest track centerline), holdTrain(name, seconds) (true if accepted, false if that train is already held; holds += 1 when accepted), throwSwitch(id) (true if accepted), scenery() returning [{kind: "station" | "tree", x, y}], and layout() returning { table: {x, y, w, h}, levers: {S1: {x, y, r}, S2: {x, y, r}}, trains: {Cedar: {x, y, r}, Birch: {...}, Maple: {...}} } in CSS px (table = the projected box above; r = the tap radius, at least 28). setCameraOrbit(azimuthDeg, elevationDeg, distance) sets the camera with the same clamps. SOUND - A train reaches its checkpoint segment -> a bell ding (sine 1,200 Hz, 0.25 s). - A switch starts moving -> a ka-chunk (square 140 Hz, 70 ms, plus a short noise tick). - A train comes to rest at a red signal -> an air-brake hiss (noise through a 3 kHz high-pass fading over 0.4 s). - A refused throw -> a buzz (sawtooth 110 Hz, 0.15 s). A hold -> a soft click (1 kHz, 20 ms). - Always on: a low rolling rumble (noise through a 200 Hz low-pass) whose gain follows the sum of train speeds, 0.03 at most. - With no AudioContext (before the first tap, and in ?test=1) nothing is played, but the triggers are still counted. COUNTERS stats() returns { fps, checkpoints, signalStops, switchThrows, refusals, holds, minGap }. - checkpoints: +1 each time a train's head enters its checkpoint segment (never at load, the start positions do not count). - signalStops: +1 each time a train that was moving (speed above 0.01) comes to rest (speed at most 0.01) while its signal is RED and it is not held. - switchThrows: +1 each time a switch starts to move (dispatcher or accepted user throw). refusals: +1 each refused user throw. holds: +1 each accepted hold. - minGap: a gauge, not a counter: the current smallest center-to-center distance between any two cars of different trains. - On screen: checkpoints, signalStops, switchThrows, refusals and holds in the HUD bar, readable in a paused frame and never over the table. CHECKS for ?test=1 With ?test=1, run an accelerated self-test that measures real positions and real events (not self-set flags) and logs "TEST n PASS" or "TEST n FAIL: reason" to the console and to window.__testResults: Each check starts from a fresh load; advance() steps in 1/60 s increments. 1. After attract(false) at t = 0: checkpoints, signalStops, switchThrows, refusals and holds are 0 and minGap is between 1.3 and 1.8; segments() gives O1 17.425, O2 13.425, I1 9.027, I2 13.027, C1 2.557 and C2 2.557 (each within 0.01); trains() has 3 trains of 4 cars with speed 0 and heads at Cedar O1 offset 12.0, Birch I2 offset 6.0, Maple O2 offset 7.0 (each within 0.01); switches() reports S1 and S2 straight and not moving; signals() reports all six segments "G"; every car center has onTrack at most 0.02; scenery() has one station within 0.1 of (0, 0) and 10 trees, each with onTrack at least 0.9 and |x| <= 6.8, |y| <= 4.3; at 1280x720 layout().table.h is at least 432 (expected about 499), at 390x844 layout().table.w is between 331 and 390; every r in layout() is at least 28. 2. After attract(false), advance(8.0) frame by frame: on every frame each train's speed is between 0 and 2.401 (and drops by at most 0.08 per frame while above 0.15), the center distance from the locomotive to car 1 is 0.50 and from each car to the next is 0.45 (each within 0.04), and every car center has onTrack at most 0.02; at some frame a train's speed reaches 2.4 (within 0.01); at t = 8.0 Birch's head is on I2 with an offset between 0.4 and 1.2 and its speed is 2.4 (within 0.01). 3. After attract(false): throwSwitch("S2") returns true and after advance(0.5) switches().S2 is "diverge" and not moving and switchThrows is 1; at t = 4.0 Cedar's head is on O1 with an offset within 0.1 of 16.125 and speed at most 0.01, signals().O1 is "R" and signalStops is 1; at t = 7.0 Maple's head is on C1 with an offset within 0.1 of 1.257 and speed at most 0.01, signalStops is 2, signals().C1 is "R"; throwSwitch("S1") then returns false, refusals is 1, switches().S1 is still "diverge" and switchThrows is 3. 4. After attract(false), run 120 s (7,200 ticks) with this script: every 120 ticks call throwSwitch, alternating "S1" and "S2" (refusals are fine); at t = 10, 20, ... 110 s call holdTrain, cycling Cedar, Birch, Maple, with 3.0 s. On every tick: minGap is at least 0.6, every speed is between 0 and 2.401, no speed drops by more than 0.08 from one tick to the next while speed is above 0.15, and every car has onTrack at most 0.02. At the end: holds is 11; stats().checkpoints equals the number of times, counted by the test from trains() head.seg frame by frame, that a head entered its own checkpoint segment; stats().signalStops equals the number of events the test counts, frame by frame, where a train's speed went from above 0.01 to at most 0.01 while signals() for its head segment was "R" and held was false; signalStops is at least 8, checkpoints is at least 10, every train's distance is at least 100, and each train's distance is within 0.5 of the sum, over all ticks, of its speed / 60. 5. On a fresh load with the demo running: at t = 2.5 switchThrows is 1, signalStops 0 and checkpoints 0; at t = 4.3 signalStops is 1, checkpoints is 2 and switchThrows is 1; at t = 26.0 signalStops, checkpoints and switchThrows are each between 4 and 6; minGap is at least 1.0 on every tick of the 28 s; at t = 27.9 note the three counters, at t = 28.4 they are unchanged, switches() reports both straight, Cedar's head is within 0.3 of O1 offset 12.0, Birch's within 0.3 of I2 offset 6.0 and Maple's within 0.3 of O2 offset 7.0; and the run has zero console errors. KEEP IT SHORT - Keep the whole file under about 1,500 lines (soft target) so you can write it in one pass and finish well inside 15 minutes. - Save lines with helpers, loops and instancing, not by dropping features. Everything above must really work: no placeholders, no TODOs. BUILD RULES (all required): - Deliver ONE self-contained HTML file: inline CSS and JS, no build step, no local assets. Libraries only from a CDN. If you use Three.js, use exactly this import map: {"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"}} - It must open straight from disk in desktop Chrome and on an iPhone (Safari), fill the full screen, handle resize and touch, and stay smooth (target 60 fps). - It must look stunning in the very FIRST frame: no black screen, no loading wait, no tiny subject. The main subject fills at least 50% of the screen height. - SOUND (required): all sound is made in code with Web Audio (no audio files). Show a small "🔊 TAP FOR SOUND" chip; on the first tap create ONE AudioContext, route everything through a master gain of 0.6 into a DynamicsCompressor, and hide the chip. Also expose enableAudio() on the hook object named at the top. - Expose the hook object named at the top with: setCameraOrbit(azimuthDeg, elevationDeg, distance) (or an equivalent camera setter), advance(seconds) that steps the simulation exactly that much for still captures, enableAudio(), and stats() returning {fps, ...the counters named above}. Use a fixed random seed of 360 so every run is the same. - Every on-screen counter must count a real event you can see happen. No fake numbers. - No text overlapping the subject; any label must be readable in a paused frame. - No brand logos, no real people, no copyrighted characters, brand names or logos. FAILURE MODES TO AVOID - Trains that touch or pass through each other at a merge: a waiting train must stop 1.3 before the end of its segment, and two trains must never both hold the same segment. - Cars that do not follow the track: a train drawn as one rigid box that cuts across curves, cars that bunch up or stretch apart, or cars that jump when the train crosses from one segment to the next or through a switch. - Switches that move under a train, that change while a train is within 1.0 of the switch point, or blades that never visibly move. - Signals that are painted lights unrelated to the dispatcher, signals that show green while the train still stops, or a train that runs a red. - Trains that brake instantly (a speed that drops by more than 0.08 in one tick above 0.15, for example 2.4 to 1.9 on entering a connector), overshoot the stop line, or deadlock for good in the 28 s demo. - User input that breaks the rules: a throw accepted next to a train, a hold that lets a train stop inside another train's path, or a click that does nothing on touch. - Counters stuck at 0, ticking on a timer, or not matching what happened (checkpoints, signalStops, switchThrows, refusals, holds). - A tiny table in a big empty room (it must be about 69% of the height on a wide window), a black first frame, or the HUD drawn over the table. - Drag, wheel or pinch that does nothing, or touch that scrolls the page. - The same variable declared twice, or methods called on objects that don't exist yet. SELF-TEST LOOP - If you can run code: build the file, open it, read the console, run ?test=1, and take screenshots at 3 s, 10 s and 30 s at 1280x720 and 390x844. Look at the screenshots and fix what looks wrong. Fix and retest until all 5 checks pass with zero console errors. Mark anything you could not run as UNMEASURED; never invent results. - If you are chat-only: go through this prompt line by line, check your code against each line, and fix anything that would fail. Then write one corrected, complete file. - Final answer: ONE complete HTML file in ONE code block, then the 5 checks marked PASS, FAIL or UNMEASURED. No snippets, no "rest unchanged". ===== END SPEC ===== Now build it. Reply with ONE complete HTML file in ONE code block, then the 5 checks marked PASS, FAIL or UNMEASURED.