| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import { expect, test } from "bun:test"; |
| 7 |
import { BODY_R, moveSpeed } from "./glide.ts"; |
| 8 |
import { makeGrid } from "./grid.ts"; |
| 9 |
import { advance, anchor, holdBack, insideWall, LEAD_MAX, makeLocal, project, reconcile, SNAP } from "../../static/engine/predict.js"; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
const TILE = 80; |
| 15 |
const ROWS = [ |
| 16 |
"##############################", |
| 17 |
"#........###......###........#", |
| 18 |
"#........###......###........#", |
| 19 |
"#........##........##........#", |
| 20 |
"#........##.#....#.##........#", |
| 21 |
"#........##........##........#", |
| 22 |
"#...##......#....#......##...#", |
| 23 |
"#..####....#......#....####..#", |
| 24 |
"#..####..###......###..####..#", |
| 25 |
"#..####..####....####..####..#", |
| 26 |
"#..####..####.##.####..####..#", |
| 27 |
"#........####.##.####........#", |
| 28 |
"#..####..####....####..####..#", |
| 29 |
"#..####..#####..#####..####..#", |
| 30 |
"#..####..#####..#####..####..#", |
| 31 |
"#..####..###......###..####..#", |
| 32 |
"#........##........##........#", |
| 33 |
"#............................#", |
| 34 |
"##############################", |
| 35 |
]; |
| 36 |
const grid = makeGrid(ROWS, TILE); |
| 37 |
const { moveCircle, solidTile } = grid; |
| 38 |
const world = { solid: solidTile, tile: TILE, r: BODY_R }; |
| 39 |
|
| 40 |
test("nothing is drawn until the server has said where you are", () => { |
| 41 |
const local = makeLocal(); |
| 42 |
expect(local.ready).toBe(false); |
| 43 |
advance(local, 1, 0, 250, 1 / 60, world); |
| 44 |
expect(local).toMatchObject({ x: 0, y: 0 }); |
| 45 |
reconcile(local, { x: 1200, y: 640 }, 1 / 60); |
| 46 |
expect(local).toMatchObject({ x: 1200, y: 640, ready: true }); |
| 47 |
}); |
| 48 |
|
| 49 |
test("a frame of movement is the speed it was given, in the direction it was given", () => { |
| 50 |
const local = anchor(makeLocal(), { x: 1200, y: 640 }); |
| 51 |
advance(local, 1, 0, 250, 1 / 50, world); |
| 52 |
expect(local.x).toBeCloseTo(1205, 6); |
| 53 |
expect(local.y).toBeCloseTo(640, 6); |
| 54 |
|
| 55 |
const diagonal = anchor(makeLocal(), { x: 1200, y: 640 }); |
| 56 |
advance(diagonal, 1, 1, 250, 1 / 50, world); |
| 57 |
expect(Math.hypot(diagonal.x - 1200, diagonal.y - 640)).toBeCloseTo(5, 6); |
| 58 |
|
| 59 |
const still = anchor(makeLocal(), { x: 1200, y: 640 }); |
| 60 |
advance(still, 0, 0, 250, 1 / 50, world); |
| 61 |
advance(still, 1, 0, moveSpeed({ stopped: true }), 1 / 50, world); |
| 62 |
expect(still).toMatchObject({ x: 1200, y: 640 }); |
| 63 |
}); |
| 64 |
|
| 65 |
test("the guess resolves a wall exactly the way the server does", () => { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
const rows = grid.rows; |
| 70 |
const cols = grid.cols; |
| 71 |
let tried = 0; |
| 72 |
for (let row = 1; row < rows - 1; row++) |
| 73 |
for (let col = 1; col < cols - 1; col++) { |
| 74 |
if (solidTile(col, row)) continue; |
| 75 |
const x = col * TILE + TILE / 2; |
| 76 |
const y = row * TILE + TILE / 2; |
| 77 |
for (const [mx, my] of [ |
| 78 |
[1, 0], |
| 79 |
[-1, 0], |
| 80 |
[0, 1], |
| 81 |
[0, -1], |
| 82 |
[0.7, 0.7], |
| 83 |
[-0.7, 0.7], |
| 84 |
[0.7, -0.7], |
| 85 |
[-0.7, -0.7], |
| 86 |
]) { |
| 87 |
const local = anchor(makeLocal(), { x, y }); |
| 88 |
advance(local, mx, my, 250, 1 / 15, world); |
| 89 |
const server = moveCircle(x, y, mx * 250 * (1 / 15), my * 250 * (1 / 15), BODY_R); |
| 90 |
expect(local.x).toBeCloseTo(server.x, 9); |
| 91 |
expect(local.y).toBeCloseTo(server.y, 9); |
| 92 |
tried += 1; |
| 93 |
} |
| 94 |
} |
| 95 |
expect(tried).toBeGreaterThan(1000); |
| 96 |
}); |
| 97 |
|
| 98 |
test("a disagreement is leaned off, not jumped", () => { |
| 99 |
const local = anchor(makeLocal(), { x: 1200, y: 640 }); |
| 100 |
reconcile(local, { x: 1210, y: 640 }, 1 / 60); |
| 101 |
expect(local.gap).toBeCloseTo(10, 6); |
| 102 |
|
| 103 |
expect(local.x).toBeGreaterThan(1200); |
| 104 |
expect(local.x).toBeLessThan(1203); |
| 105 |
|
| 106 |
for (let i = 0; i < 60; i++) reconcile(local, { x: 1210, y: 640 }, 1 / 60); |
| 107 |
expect(local.x).toBeCloseTo(1210, 2); |
| 108 |
}); |
| 109 |
|
| 110 |
test("a real disagreement is taken outright: the server is the answer", () => { |
| 111 |
const local = anchor(makeLocal(), { x: 1200, y: 640 }); |
| 112 |
|
| 113 |
reconcile(local, { x: 1200 + SNAP + 1, y: 640 }, 1 / 60); |
| 114 |
expect(local.x).toBe(1200 + SNAP + 1); |
| 115 |
expect(local.gap).toBe(0); |
| 116 |
}); |
| 117 |
|
| 118 |
test("a snapshot is carried forward by the input held since it was sent", () => { |
| 119 |
const server = { x: 1200, y: 640 }; |
| 120 |
|
| 121 |
expect(project(server, 0, 0, 250, 0.1, world)).toEqual({ x: 1200, y: 640 }); |
| 122 |
expect(project(server, 1, 0, 0, 0.1, world)).toEqual({ x: 1200, y: 640 }); |
| 123 |
expect(project(server, 1, 0, 250, 0, world)).toEqual({ x: 1200, y: 640 }); |
| 124 |
|
| 125 |
const ahead = project(server, 1, 0, 250, 1 / 15, world); |
| 126 |
expect(ahead.x - 1200).toBeCloseTo(250 / 15, 6); |
| 127 |
|
| 128 |
const far = project(server, 1, 0, 250, 30, world); |
| 129 |
expect(far.x - 1200).toBeLessThanOrEqual(250 * LEAD_MAX); |
| 130 |
|
| 131 |
for (let y = 100; y < 1400; y += 37) { |
| 132 |
const from = { x: 300, y }; |
| 133 |
if (insideWall(from, world)) continue; |
| 134 |
expect(insideWall(project(from, -1, 0, 250, LEAD_MAX, world), world)).toBe(false); |
| 135 |
} |
| 136 |
}); |
| 137 |
|
| 138 |
test("the guess is never drawn past the answer it is running toward", () => { |
| 139 |
const target = { x: 1300, y: 640 }; |
| 140 |
|
| 141 |
const past = anchor(makeLocal(), { x: 1320, y: 640 }); |
| 142 |
holdBack(past, target, 1, 0); |
| 143 |
expect(past.x).toBeCloseTo(1300, 9); |
| 144 |
|
| 145 |
const short = anchor(makeLocal(), { x: 1280, y: 648 }); |
| 146 |
holdBack(short, target, 1, 0); |
| 147 |
expect(short).toMatchObject({ x: 1280, y: 648 }); |
| 148 |
const still = anchor(makeLocal(), { x: 1320, y: 640 }); |
| 149 |
holdBack(still, target, 0, 0); |
| 150 |
expect(still.x).toBe(1320); |
| 151 |
}); |
| 152 |
|
| 153 |
test("a guess running with the server stays within a stride of it, and never inside a wall", () => { |
| 154 |
|
| 155 |
|
| 156 |
const speed = 250; |
| 157 |
const start = { x: 1200, y: 640 }; |
| 158 |
let server = { ...start }; |
| 159 |
const local = anchor(makeLocal(), start); |
| 160 |
let worst = 0; |
| 161 |
let back = 0; |
| 162 |
let previous = local.x; |
| 163 |
for (let frame = 0; frame < 240; frame++) { |
| 164 |
advance(local, 1, 0, speed, 1 / 60, world); |
| 165 |
|
| 166 |
if (frame % 2 === 0) server = moveCircle(server.x, server.y, speed / 30, 0, BODY_R); |
| 167 |
if (frame % 4 === 0) reconcile(local, server, 1 / 15); |
| 168 |
worst = Math.max(worst, Math.hypot(local.x - server.x, local.y - server.y)); |
| 169 |
|
| 170 |
back = Math.min(back, local.x - previous); |
| 171 |
previous = local.x; |
| 172 |
expect(insideWall(local, world)).toBe(false); |
| 173 |
} |
| 174 |
expect(worst).toBeLessThan(BODY_R); |
| 175 |
expect(back).toBeGreaterThan(-0.01); |
| 176 |
}); |