/** * Entity interpolation: a pure function of two samples and a time. * * What is checked here is what a browser cannot be asked about — that the * body walks between the pair at the server's own speed, that a missed * snapshot is carried exactly one interval and no further, that the turn is * taken the short way round, and that a time between the pair never draws * the body anywhere but between them. */ import { expect, test } from "bun:test"; import { arrive, bodyAt, delay, feed, GONE, LAG, LAG_MAX, LAG_MIN, makeTimeline, sample } from "../static/interp.js"; type Mark = { t: number; x: number; y: number; angle: number }; const pair = (a: Mark, b: Mark) => [a, b]; const walk = pair({ t: 10, x: 100, y: 200, angle: 0 }, { t: 10.1, x: 120, y: 200, angle: 1 }); test("between the pair it walks at the speed the server moved it", () => { expect(sample(walk, 10)).toMatchObject({ x: 100, y: 200 }); expect(sample(walk, 10.05).x).toBeCloseTo(110, 6); expect(sample(walk, 10.1)).toMatchObject({ x: 120, y: 200 }); // Constant speed: equal steps of time are equal steps of distance, which // is the whole difference from an exponential chase that never arrives. const a = sample(walk, 10.02).x; const b = sample(walk, 10.04).x; const c = sample(walk, 10.06).x; expect(b - a).toBeCloseTo(c - b, 6); }); test("a time inside the pair is never a position outside it", () => { const diagonal = pair({ t: 1, x: -30, y: 80, angle: 2 }, { t: 1.07, x: 45, y: -10, angle: -2 }); for (let when = 1; when <= 1.07; when += 0.002) { const at = sample(diagonal, when); expect(at.x).toBeGreaterThanOrEqual(-30); expect(at.x).toBeLessThanOrEqual(45); expect(at.y).toBeGreaterThanOrEqual(-10); expect(at.y).toBeLessThanOrEqual(80); } }); test("before the older sample it holds rather than running backwards", () => { expect(sample(walk, 9.5)).toMatchObject({ x: 100, y: 200 }); expect(sample(walk, 0)).toMatchObject({ x: 100, y: 200 }); }); test("a missed snapshot is carried one interval and then held", () => { // Half an interval late: half an interval of the pair's own velocity. expect(sample(walk, 10.15).x).toBeCloseTo(130, 6); // A whole one: exactly one more interval of it. expect(sample(walk, 10.2).x).toBeCloseTo(140, 6); // And never a step further, however long the silence lasts. expect(sample(walk, 10.3).x).toBeCloseTo(140, 6); expect(sample(walk, 30).x).toBeCloseTo(140, 6); expect(sample(walk, 30).angle).toBeCloseTo(2, 6); }); test("two samples at one instant are one sample", () => { const still = pair({ t: 5, x: 7, y: 8, angle: 0.5 }, { t: 5, x: 7, y: 8, angle: 0.5 }); expect(sample(still, 9)).toMatchObject({ x: 7, y: 8, angle: 0.5 }); }); test("the turn is taken the short way round", () => { const spin = pair({ t: 0, x: 0, y: 0, angle: 3.0 }, { t: 0.1, x: 0, y: 0, angle: -3.0 }); // Not back through zero: onward past π, by a sixth of a radian or so. expect(sample(spin, 0.05).angle).toBeCloseTo(3.0 + 0.2831853 / 2, 6); const back = pair({ t: 0, x: 0, y: 0, angle: -3.0 }, { t: 0.1, x: 0, y: 0, angle: 3.0 }); expect(sample(back, 0.05).angle).toBeCloseTo(-3.0 - 0.2831853 / 2, 6); }); // ------------------------------------------------------------- the timeline const body = (id: string, x: number, y = 0, angle = 0) => ({ id, x, y, angle }); test("a body is drawn where the server says until there are two of it", () => { const timeline = makeTimeline(); const first = body("a", 50); expect(bodyAt(timeline, first, 10)).toBe(first); feed(timeline, "a", first, 10); expect(bodyAt(timeline, first, 10.2)).toMatchObject({ x: 50, y: 0 }); feed(timeline, "a", body("a", 60), 10.1); expect(bodyAt(timeline, body("a", 60), 10.05).x).toBeCloseTo(55, 6); }); test("each body keeps its own answer: one can be held while another is asked", () => { const timeline = makeTimeline(); feed(timeline, "a", body("a", 0), 1); feed(timeline, "a", body("a", 10), 1.1); feed(timeline, "b", body("b", 100), 1); feed(timeline, "b", body("b", 110), 1.1); const a = bodyAt(timeline, body("a", 10), 1.05); const b = bodyAt(timeline, body("b", 110), 1.05); expect(a.x).toBeCloseTo(5, 6); expect(b.x).toBeCloseTo(105, 6); }); test("a body that was out of sight starts again rather than sliding back", () => { const timeline = makeTimeline(); feed(timeline, "a", body("a", 0), 1); feed(timeline, "a", body("a", 10), 1.1); feed(timeline, "a", body("a", 900), 1.1 + GONE + 0.1); expect(bodyAt(timeline, body("a", 900), 1.1 + GONE + 0.15).x).toBeCloseTo(900, 6); }); test("a jump no one could have run is a respawn, not a walk", () => { const timeline = makeTimeline(); feed(timeline, "a", body("a", 0), 1); feed(timeline, "a", body("a", 10), 1.1); // A third of a second's running is 80-odd units; 600 is a teleport. feed(timeline, "a", body("a", 610), 1.2); expect(bodyAt(timeline, body("a", 610), 1.25).x).toBeCloseTo(610, 6); }); test("a sample that arrives out of order is dropped", () => { const timeline = makeTimeline(); feed(timeline, "a", body("a", 0), 1); feed(timeline, "a", body("a", 10), 1.1); feed(timeline, "a", body("a", 999), 1.05); expect(bodyAt(timeline, body("a", 10), 1.1).x).toBeCloseTo(10, 6); }); test("the delay follows the rate the snapshots actually arrive at", () => { const slow = makeTimeline(); for (let i = 0; i < 60; i++) arrive(slow, 10 + i / 15); const fast = makeTimeline(); for (let i = 0; i < 60; i++) arrive(fast, 10 + i / 30); expect(delay(slow)).toBeGreaterThan(delay(fast)); expect(delay(slow)).toBeCloseTo((1 / 15) * LAG, 3); // Thirty a second wants less of a delay, and both stay inside the bounds. for (const d of [delay(slow), delay(fast)]) { expect(d).toBeGreaterThanOrEqual(LAG_MIN); expect(d).toBeLessThanOrEqual(LAG_MAX); } // A pause — a backgrounded tab, a reconnect — says nothing about the rate. arrive(fast, 40); expect(delay(fast)).toBeCloseTo(LAG_MIN, 3); }); test("the moment being drawn is always bracketed by samples on hand", () => { // Thirty snapshots a second, a frame every 16ms: the moment being drawn // always falls between the two samples on hand, never before them. const timeline = makeTimeline(); let x = 0; let last = { t: 0, x: 0 }; for (let i = 0; i < 40; i++) { const at = 10 + i / 30; arrive(timeline, at); x += 8; feed(timeline, "a", body("a", x), at); last = { t: at, x }; if (i < 3) continue; for (let f = 0; f < 2; f++) { const now = at + f * 0.016; const marks = timeline.bodies.get("a")!.marks as Mark[]; const when = now - delay(timeline); expect(when).toBeGreaterThanOrEqual(marks[0]!.t - 1e-9); expect(when).toBeLessThanOrEqual(marks[marks.length - 1]!.t + 1e-9); } } // And late is what late means: behind the newest word by the delay, which // at eight units an interval is between one and two of them. const drawn = bodyAt(timeline, body("a", last.x), last.t - delay(timeline)); expect(last.x - drawn.x).toBeGreaterThan(0); expect(last.x - drawn.x).toBeLessThan(20); }); // --------------------------------------------- against the real simulation /** * The match's own motion, sampled at the rate the server broadcasts it and * drawn at the rate a browser draws: what a bot walking across the map * actually looks like. The samples carry exactly what a snapshot carries — * a position rounded to a tenth, thirty times a second. * * The measure is the spread of the per-frame steps. A body drawn at the * server's speed takes even steps; the exponential chase this replaced took * a long step just after each snapshot and shorter ones after it, which is * choppiness with a number on it — twice this spread, measured the same way. */ test("the simulation's own motion is drawn at an even speed", async () => { const { runBots } = await import("./bots.ts"); const { newMatch, tick } = await import("./match.ts"); const round2 = (n: number) => Math.round(n * 10) / 10; // A match of its own: nothing here touches the arena everybody shares. const state = newMatch(); const step = () => { runBots(state, 1 / 30); tick(state, 1 / 30); }; for (let i = 0; i < 30 * 40 && state.phase !== "live"; i++) step(); const stream: { t: number; bodies: { id: string; x: number; y: number; angle: number }[] }[] = []; for (let i = 0; i < 60; i++) { step(); stream.push({ t: i / 30, bodies: state.players.map((p) => ({ id: p.id, x: round2(p.x), y: round2(p.y), angle: round2(p.angle) })), }); } const timeline = makeTimeline(); const paths = new Map(); let next = 0; for (let f = 0; f / 60 < 1.8; f++) { const now = f / 60; while (next < stream.length && stream[next]!.t <= now) { arrive(timeline, stream[next]!.t); for (const p of stream[next]!.bodies) feed(timeline, p.id, p, stream[next]!.t); next += 1; } for (const p of stream[Math.max(0, next - 1)]!.bodies) { const at = bodyAt(timeline, p, now - delay(timeline)); paths.set(p.id, [...(paths.get(p.id) ?? []), { x: at.x, y: at.y }]); } } // Whoever walked furthest in that second and a half. const walked = [...paths.values()] .filter((path) => path.length > 80) .map((path) => path.slice(1).map((at, i) => Math.hypot(at.x - path[i]!.x, at.y - path[i]!.y))) .map((steps) => steps.filter((step) => step > 0.001)) .sort((a, b) => b.reduce((s, x) => s + x, 0) - a.reduce((s, x) => s + x, 0))[0]!; expect(walked.length).toBeGreaterThan(30); const mean = walked.reduce((s, x) => s + x, 0) / walked.length; const spread = Math.sqrt(walked.reduce((s, x) => s + (x - mean) ** 2, 0) / walked.length) / mean; // A body moving at the server's own speed: about four units every sixtieth // of a second, which is the run speed. expect(mean * 60).toBeGreaterThan(150); expect(spread).toBeLessThan(0.15); });