/** * The browser's guess about where you are, checked against the server's * answer: the same maths, the same wall, and a disagreement that closes. */ import { expect, test } from "bun:test"; import { BODY_R, moveSpeed } from "./glide.ts"; import { makeGrid } from "./grid.ts"; import { advance, anchor, holdBack, insideWall, LEAD_MAX, makeLocal, project, reconcile, SNAP } from "../../static/engine/predict.js"; // A map of the shape a served one has: an outer wall, rooms, pillars and a // corridor, bound to the same grid the server plays on. The browser builds // the same world out of map.json: the same rows, the same tile, the same body. const TILE = 80; const ROWS = [ "##############################", "#........###......###........#", "#........###......###........#", "#........##........##........#", "#........##.#....#.##........#", "#........##........##........#", "#...##......#....#......##...#", "#..####....#......#....####..#", "#..####..###......###..####..#", "#..####..####....####..####..#", "#..####..####.##.####..####..#", "#........####.##.####........#", "#..####..####....####..####..#", "#..####..#####..#####..####..#", "#..####..#####..#####..####..#", "#..####..###......###..####..#", "#........##........##........#", "#............................#", "##############################", ]; const grid = makeGrid(ROWS, TILE); const { moveCircle, solidTile } = grid; const world = { solid: solidTile, tile: TILE, r: BODY_R }; test("nothing is drawn until the server has said where you are", () => { const local = makeLocal(); expect(local.ready).toBe(false); advance(local, 1, 0, 250, 1 / 60, world); expect(local).toMatchObject({ x: 0, y: 0 }); reconcile(local, { x: 1200, y: 640 }, 1 / 60); expect(local).toMatchObject({ x: 1200, y: 640, ready: true }); }); test("a frame of movement is the speed it was given, in the direction it was given", () => { const local = anchor(makeLocal(), { x: 1200, y: 640 }); advance(local, 1, 0, 250, 1 / 50, world); expect(local.x).toBeCloseTo(1205, 6); expect(local.y).toBeCloseTo(640, 6); // A diagonal is a direction, not a speed boost. const diagonal = anchor(makeLocal(), { x: 1200, y: 640 }); advance(diagonal, 1, 1, 250, 1 / 50, world); expect(Math.hypot(diagonal.x - 1200, diagonal.y - 640)).toBeCloseTo(5, 6); // Standing still, or moving at a speed the game has reduced to zero: nothing moves. const still = anchor(makeLocal(), { x: 1200, y: 640 }); advance(still, 0, 0, 250, 1 / 50, world); advance(still, 1, 0, moveSpeed({ stopped: true }), 1 / 50, world); expect(still).toMatchObject({ x: 1200, y: 640 }); }); test("the guess resolves a wall exactly the way the server does", () => { // Every open tile of the map, walked into from eight directions: the // browser's step and the server's land in the same place, or the camera // spends the round being dragged off whatever it guessed. const rows = grid.rows; const cols = grid.cols; let tried = 0; for (let row = 1; row < rows - 1; row++) for (let col = 1; col < cols - 1; col++) { if (solidTile(col, row)) continue; const x = col * TILE + TILE / 2; const y = row * TILE + TILE / 2; for (const [mx, my] of [ [1, 0], [-1, 0], [0, 1], [0, -1], [0.7, 0.7], [-0.7, 0.7], [0.7, -0.7], [-0.7, -0.7], ]) { const local = anchor(makeLocal(), { x, y }); advance(local, mx, my, 250, 1 / 15, world); const server = moveCircle(x, y, mx * 250 * (1 / 15), my * 250 * (1 / 15), BODY_R); expect(local.x).toBeCloseTo(server.x, 9); expect(local.y).toBeCloseTo(server.y, 9); tried += 1; } } expect(tried).toBeGreaterThan(1000); }); test("a disagreement is leaned off, not jumped", () => { const local = anchor(makeLocal(), { x: 1200, y: 640 }); reconcile(local, { x: 1210, y: 640 }, 1 / 60); expect(local.gap).toBeCloseTo(10, 6); // A frame moves a fraction of the way, never the whole way. expect(local.x).toBeGreaterThan(1200); expect(local.x).toBeLessThan(1203); // Held there, it closes. for (let i = 0; i < 60; i++) reconcile(local, { x: 1210, y: 640 }, 1 / 60); expect(local.x).toBeCloseTo(1210, 2); }); test("a real disagreement is taken outright: the server is the answer", () => { const local = anchor(makeLocal(), { x: 1200, y: 640 }); // A spawn, a teleport, a seat handed to somebody else mid-round. reconcile(local, { x: 1200 + SNAP + 1, y: 640 }, 1 / 60); expect(local.x).toBe(1200 + SNAP + 1); expect(local.gap).toBe(0); }); test("a snapshot is carried forward by the input held since it was sent", () => { const server = { x: 1200, y: 640 }; // Holding nothing, the answer is the answer: no lead, nothing invented. expect(project(server, 0, 0, 250, 0.1, world)).toEqual({ x: 1200, y: 640 }); expect(project(server, 1, 0, 0, 0.1, world)).toEqual({ x: 1200, y: 640 }); expect(project(server, 1, 0, 250, 0, world)).toEqual({ x: 1200, y: 640 }); // Holding east for 66ms, it is 66ms of running east ahead. const ahead = project(server, 1, 0, 250, 1 / 15, world); expect(ahead.x - 1200).toBeCloseTo(250 / 15, 6); // A connection that stopped talking is not a licence to run off the map. const far = project(server, 1, 0, 250, 30, world); expect(far.x - 1200).toBeLessThanOrEqual(250 * LEAD_MAX); // And the carry is the server's own step-and-slide: never through a wall. for (let y = 100; y < 1400; y += 37) { const from = { x: 300, y }; if (insideWall(from, world)) continue; // not a place anybody can stand expect(insideWall(project(from, -1, 0, 250, LEAD_MAX, world), world)).toBe(false); } }); test("the guess is never drawn past the answer it is running toward", () => { const target = { x: 1300, y: 640 }; // Ahead of the target along the way it is going: pulled back onto it. const past = anchor(makeLocal(), { x: 1320, y: 640 }); holdBack(past, target, 1, 0); expect(past.x).toBeCloseTo(1300, 9); // Short of it, or across it, or standing still: left alone. const short = anchor(makeLocal(), { x: 1280, y: 648 }); holdBack(short, target, 1, 0); expect(short).toMatchObject({ x: 1280, y: 648 }); const still = anchor(makeLocal(), { x: 1320, y: 640 }); holdBack(still, target, 0, 0); expect(still.x).toBe(1320); }); test("a guess running with the server stays within a stride of it, and never inside a wall", () => { // Sixty frames a second guessing, fifteen a second corrected, walking east // into the wall that bounds mid. This is the case that used to skip. const speed = 250; const start = { x: 1200, y: 640 }; let server = { ...start }; const local = anchor(makeLocal(), start); let worst = 0; let back = 0; let previous = local.x; for (let frame = 0; frame < 240; frame++) { advance(local, 1, 0, speed, 1 / 60, world); // The server ticks at 30Hz and is heard from at 15Hz. if (frame % 2 === 0) server = moveCircle(server.x, server.y, speed / 30, 0, BODY_R); if (frame % 4 === 0) reconcile(local, server, 1 / 15); worst = Math.max(worst, Math.hypot(local.x - server.x, local.y - server.y)); // The drawn body never walks backwards: that is what a skip looks like. back = Math.min(back, local.x - previous); previous = local.x; expect(insideWall(local, world)).toBe(false); } expect(worst).toBeLessThan(BODY_R); expect(back).toBeGreaterThan(-0.01); });