Split The code is public; the app answers only to the crew.
Movement between ticks: interpolate what is drawn, so 64 Hz does not judder at 60 #128
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/100-movement-between-ticks-interpolate-what → main 2 commits fork
12 files
changed +628
−56
README.md +16
| @@ -307,6 +307,20 @@ kills, and one line for the totals either way. When the match ends a card says | ||
| 307 | 307 | who won, the final score from your side and what you did, with a button to |
| 308 | 308 | play again and one back to the menu. |
| 309 | 309 | |
| 310 | The world steps at a fixed 64 Hz and your screen draws at 60, 120 or whatever | |
| 311 | it feels like, so most frames land partway between two ticks. What is drawn is | |
| 312 | put there: every other player, every grenade, the bomb and the dead toppling | |
| 313 | are drawn between where they were when the last tick ended and where they are | |
| 314 | now, at the sub-tick remainder the loop already had in hand. At 60 Hz against | |
| 315 | 64 that is the difference between a bot strafing across a doorway smoothly and | |
| 316 | one that jumps two ticks every fifteenth frame. **Your own camera is not | |
| 317 | interpolated**: your aim is this frame's mouse and your eye is the newest tick, | |
| 318 | because a smoother world is not worth 15 ms of lag on your own view. A round | |
| 319 | start, a respawn or anything else that moves a body further in one tick than it | |
| 320 | could walk snaps instead of sliding. The simulation is untouched by any of it — | |
| 321 | the step is still fixed, the bots still see what they saw, and the training | |
| 322 | arena, which draws nothing, does not pay for it at all. | |
| 323 | ||
| 310 | 324 | The counter in the corner of the view is the measured frame time. When a |
| 311 | 325 | machine cannot hold 60 fps the scene is drawn into a smaller buffer — half the |
| 312 | 326 | device pixel ratio at the worst, stepped at most twice a second, the HUD |
| @@ -357,6 +371,8 @@ projections they feed cost 0.04 ms of a 16.7 ms frame, measured. | ||
| 357 | 371 | it, and when the carrier has run out of clock), `observe`, `action`, `policy`, `weights`, `models`, `textures`, |
| 358 | 372 | `press` (the input latch), `loop` (how much simulation one frame is worth: |
| 359 | 373 | the fixed step, the caps, and a pause that banks nothing), |
| 374 | `interpolate` (what is drawn between two ticks: a position lerped, a yaw | |
| 375 | turned the short way round the circle, and a teleport that snaps), | |
| 360 | 376 | `quality` (the render scale the frame time buys), |
| 361 | 377 | `spectate` (whose eyes you borrow when you are dead), `bodies` (the dead of |
| 362 | 378 | this round, where they fell), `radar` (the blips the |
src/client/grenades.test.ts +24 −3
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { blastDamage, blastFalloff, GRENADE, step, thrown, type Grenade } from "./grenades.ts"; |
| 3 | import { TELEPORT } from "./interpolate.ts"; | |
| 3 | 4 | import { colOf, DE_FORGE, rowOf, walkable, WALL_HEIGHT, type Box } from "./map.ts"; |
| 4 | 5 | import type { Vec3 } from "./math.ts"; |
| 5 | 6 | import { BOMB_BLAST_RADIUS } from "./world.ts"; |
| @@ -77,7 +78,7 @@ test("thrown at a wall of de_forge it stays in the map and comes to rest", () => | ||
| 77 | 78 | }); |
| 78 | 79 | |
| 79 | 80 | test("a grenade dropped on the floor stops instead of trembling on it", () => { |
| 80 | const g: Grenade = { kind: "he", owner: 0, team: "t", pos: [0, 0, 120], vel: [0, 0, 0], fuse: 5 }; | |
| 81 | const g: Grenade = { kind: "he", owner: 0, team: "t", pos: [0, 0, 120], prev: [0, 0, 120], vel: [0, 0, 0], fuse: 5 }; | |
| 81 | 82 | fly(g, 1.5, FLOOR); |
| 82 | 83 | expect(g.vel).toEqual([0, 0, 0]); |
| 83 | 84 | expect(g.pos[2]).toBeCloseTo(GRENADE.radius, 6); |
| @@ -94,9 +95,9 @@ test("the fuse runs on the clock: nothing before 1.6s, and then it goes", () => | ||
| 94 | 95 | }); |
| 95 | 96 | |
| 96 | 97 | test("a hard hit sounds and a settling one does not", () => { |
| 97 | const fast: Grenade = { kind: "he", owner: 0, team: "t", pos: [0, 0, 60], vel: [0, 0, -400], fuse: 5 }; | |
| 98 | const fast: Grenade = { kind: "he", owner: 0, team: "t", pos: [0, 0, 60], prev: [0, 0, 60], vel: [0, 0, -400], fuse: 5 }; | |
| 98 | 99 | expect(fly(fast, 0.25, FLOOR)).toBeGreaterThan(0); |
| 99 | const settled: Grenade = { kind: "he", owner: 0, team: "t", pos: [0, 0, GRENADE.radius], vel: [0, 0, 0], fuse: 5 }; | |
| 100 | const settled: Grenade = { kind: "he", owner: 0, team: "t", pos: [0, 0, GRENADE.radius], prev: [0, 0, GRENADE.radius], vel: [0, 0, 0], fuse: 5 }; | |
| 100 | 101 | for (let i = 0; i < 10; i++) expect(step(settled, TICK, FLOOR).bounced).toBe(false); |
| 101 | 102 | }); |
| 102 | 103 | |
| @@ -134,3 +135,23 @@ test("the bomb's blast is the same function as the HE's, with its own radius and | ||
| 134 | 135 | const middle = BOMB_BLAST_RADIUS * 5 / 6; |
| 135 | 136 | expect(at(middle, 100).health).toBeLessThan(at(middle, 0).health); |
| 136 | 137 | }); |
| 138 | ||
| 139 | test("a grenade remembers where it was, so the renderer can draw it between two ticks", () => { | |
| 140 | const g = thrown(0, "t", [0, 0, 64], 0, 0, [0, 0, 0]); | |
| 141 | // It has not moved yet: the first frame draws it at the hand, not between | |
| 142 | // the hand and the origin. | |
| 143 | expect(g.prev).toEqual(g.pos); | |
| 144 | const held = g.prev; | |
| 145 | ||
| 146 | const was = [...g.pos] as Vec3; | |
| 147 | step(g, TICK, FLOOR); | |
| 148 | expect(g.prev).toEqual(was); | |
| 149 | expect(g.pos[0]).toBeGreaterThan(was[0]); // it flew | |
| 150 | // Twelve units at most in a tick, which is a move and not a teleport. | |
| 151 | expect(Math.hypot(g.pos[0] - g.prev[0], g.pos[1] - g.prev[1], g.pos[2] - g.prev[2])).toBeLessThan(TELEPORT); | |
| 152 | ||
| 153 | const then = [...g.pos] as Vec3; | |
| 154 | step(g, TICK, FLOOR); | |
| 155 | expect(g.prev).toEqual(then); | |
| 156 | expect(g.prev).toBe(held); // written into the array it already had | |
| 157 | }); |
src/client/grenades.ts +18 −5
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | 17 | import type { Box } from "./map.ts"; |
| 18 | import { rememberPos } from "./interpolate.ts"; | |
| 18 | 19 | import { angleVectors, clamp, type Vec3 } from "./math.ts"; |
| 19 | 20 | import { armorSplit, WEAPONS } from "./weapons.ts"; |
| 20 | 21 | |
| @@ -25,6 +26,13 @@ export type Grenade = { | ||
| 25 | 26 | pos: Vec3; |
| 26 | 27 | vel: Vec3; |
| 27 | 28 | fuse: number; // seconds left, counted down every step |
| 29 | /** | |
| 30 | * Where it was when the previous step ended, written by `step` itself before | |
| 31 | * it moves. Nothing in the simulation reads it: it is what lets the renderer | |
| 32 | * draw a grenade partway between two ticks instead of twelve units on from | |
| 33 | * the last one. | |
| 34 | */ | |
| 35 | prev: Vec3; | |
| 28 | 36 | }; |
| 29 | 37 | |
| 30 | 38 | export const GRENADE = { |
| @@ -78,6 +86,7 @@ function resolve(g: Grenade, box: Box, contact: Contact): void { | ||
| 78 | 86 | * caller drops the grenade from its list when it sees it. |
| 79 | 87 | */ |
| 80 | 88 | export function step(g: Grenade, dt: number, boxes: Box[]): Step { |
| 89 | rememberPos(g.prev, g.pos); | |
| 81 | 90 | g.fuse -= dt; |
| 82 | 91 | g.vel[2] -= GRENADE.gravity * dt; |
| 83 | 92 | g.pos[0] += (g.vel[0] as number) * dt; |
| @@ -111,15 +120,19 @@ export function thrown( | ||
| 111 | 120 | ): Grenade { |
| 112 | 121 | // Pitch is positive downwards, so lifting the throw subtracts from it. |
| 113 | 122 | const dir = angleVectors(yaw, pitch - GRENADE.throwLift).forward; |
| 123 | const from: Vec3 = [ | |
| 124 | eye[0] + dir[0] * GRENADE.throwFrom, | |
| 125 | eye[1] + dir[1] * GRENADE.throwFrom, | |
| 126 | eye[2] + dir[2] * GRENADE.throwFrom, | |
| 127 | ]; | |
| 114 | 128 | return { |
| 115 | 129 | kind: "he", |
| 116 | 130 | owner, |
| 117 | 131 | team, |
| 118 | pos: [ | |
| 119 | eye[0] + dir[0] * GRENADE.throwFrom, | |
| 120 | eye[1] + dir[1] * GRENADE.throwFrom, | |
| 121 | eye[2] + dir[2] * GRENADE.throwFrom, | |
| 122 | ], | |
| 132 | pos: [from[0], from[1], from[2]], | |
| 133 | // It has not moved yet, so it has not moved: a grenade's first frame is | |
| 134 | // drawn at the hand it left and never between there and the origin. | |
| 135 | prev: [from[0], from[1], from[2]], | |
| 123 | 136 | vel: [ |
| 124 | 137 | dir[0] * GRENADE.throwSpeed + velocity[0], |
| 125 | 138 | dir[1] * GRENADE.throwSpeed + velocity[1], |
src/client/hud.test.ts +23 −2
| @@ -64,8 +64,8 @@ const cameraFor = (view: Player): Camera => | ||
| 64 | 64 | |
| 65 | 65 | /** One frame of the HUD, drawing `view` — the person unless a spectator is watching. */ |
| 66 | 66 | const draw = (into: { ctx: CanvasRenderingContext2D }, world: World, view: Player = world.me, |
| 67 | scoreboard = false, crosshair?: CrosshairStyle): void => | |
| 68 | drawHud(into.ctx, world, view, cameraFor(view), W, H, scoreboard, crosshair); | |
| 67 | scoreboard = false, crosshair?: CrosshairStyle, alpha = 1): void => | |
| 68 | drawHud(into.ctx, world, view, cameraFor(view), W, H, scoreboard, crosshair, alpha); | |
| 69 | 69 | |
| 70 | 70 | /** Where the middle of a stroked wedge lands on the screen. */ |
| 71 | 71 | const wedgePoint = (call: Call): { x: number; y: number } => { |
| @@ -531,3 +531,24 @@ test("a plate across the map is a hint: fainter, and clamped small", () => { | ||
| 531 | 531 | draw(close, world); |
| 532 | 532 | expect(backing(close.calls).args[3]).toBeCloseTo(backing(near.calls).args[3], 6); |
| 533 | 533 | }); |
| 534 | ||
| 535 | test("a name plate sits over the body the renderer drew, not the tick it left", () => { | |
| 536 | // The scene is drawn between two ticks; a plate taken from the newest one | |
| 537 | // would swim above the head it belongs to. `alpha` is the same number both. | |
| 538 | const { world, mate, enemy } = plateWorld(); | |
| 539 | mate.alive = false; | |
| 540 | enemy.prev.pos = [600, -20, 0]; | |
| 541 | enemy.pos = [600, 20, 0]; | |
| 542 | ||
| 543 | const plateX = (alpha: number): number => { | |
| 544 | const frame = recorder(); | |
| 545 | draw(frame, world, world.me, false, undefined, alpha); | |
| 546 | const call = frame.calls.find((c) => c.name === "fillText" && c.text === enemy.name) as Call; | |
| 547 | return call.args[0] as number; | |
| 548 | }; | |
| 549 | ||
| 550 | const oldest = plateX(0), half = plateX(0.5), newest = plateX(1); | |
| 551 | // Looking east with the player crossing from right to left of the screen. | |
| 552 | expect(oldest).not.toBeCloseTo(newest, 0); | |
| 553 | expect(half).toBeCloseTo((oldest + newest) / 2, 0); | |
| 554 | }); |
src/client/hud.ts +11 −3
| @@ -9,7 +9,8 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import { crosshairShape, DEFAULT_CROSSHAIR, type CrosshairStyle } from "./crosshair.ts"; |
| 12 | import { angleDelta, clamp, rad } from "./math.ts"; | |
| 12 | import { lerpPos } from "./interpolate.ts"; | |
| 13 | import { angleDelta, clamp, rad, type Vec3 } from "./math.ts"; | |
| 13 | 14 | import { walkable, type MapDef } from "./map.ts"; |
| 14 | 15 | import { hullHeight, MOVE } from "./physics.ts"; |
| 15 | 16 | import { project, type Projected } from "./project.ts"; |
| @@ -240,12 +241,18 @@ function drawPlates( | ||
| 240 | 241 | width: number, |
| 241 | 242 | height: number, |
| 242 | 243 | scale: number, |
| 244 | alpha: number, | |
| 243 | 245 | ): void { |
| 244 | 246 | const plates: { player: Player; at: Projected }[] = []; |
| 247 | const blend: Vec3 = [0, 0, 0]; | |
| 245 | 248 | for (const p of world.players) { |
| 246 | 249 | if (p === view || !p.alive) continue; |
| 247 | 250 | if (!canSee(world, view, p)) continue; // one trace per player per frame |
| 248 | const head: [number, number, number] = [p.pos[0], p.pos[1], p.pos[2] + hullHeight(p) + PLATE_LIFT]; | |
| 251 | // Over the head the renderer drew, not the head the last tick left: a | |
| 252 | // plate that sat on the newest tick would swim above a body drawn between | |
| 253 | // two of them. `alpha` is the same number the scene was drawn with. | |
| 254 | lerpPos(p.prev.pos, p.pos, alpha, blend); | |
| 255 | const head: [number, number, number] = [blend[0], blend[1], blend[2] + hullHeight(p) + PLATE_LIFT]; | |
| 249 | 256 | const at = project(head, camera, width, height); |
| 250 | 257 | if (at !== null) plates.push({ player: p, at }); |
| 251 | 258 | } |
| @@ -295,6 +302,7 @@ export function drawHud( | ||
| 295 | 302 | height: number, |
| 296 | 303 | showScoreboard: boolean, |
| 297 | 304 | crosshair: CrosshairStyle = DEFAULT_CROSSHAIR, |
| 305 | alpha = 1, | |
| 298 | 306 | ): void { |
| 299 | 307 | ctx.clearRect(0, 0, width, height); |
| 300 | 308 | ctx.textBaseline = "alphabetic"; |
| @@ -376,7 +384,7 @@ export function drawHud( | ||
| 376 | 384 | // --- who is that: names over the players the eye can see, drawn under the |
| 377 | 385 | // rest of the HUD so a plate never covers the clock or the ammo, and not at |
| 378 | 386 | // all while the scoreboard — which says the same thing better — is up. |
| 379 | if (!showScoreboard) drawPlates(ctx, world, view, camera, width, height, scale); | |
| 387 | if (!showScoreboard) drawPlates(ctx, world, view, camera, width, height, scale, alpha); | |
| 380 | 388 | |
| 381 | 389 | // --- radar: bottom-left, clear of the money line above and the health and |
| 382 | 390 | // armour numbers below, at 360px of a 1280px-wide HUD — about 28% of the |
src/client/interpolate.test.ts added +96
| @@ -0,0 +1,96 @@ | ||
| 1 | /** | |
| 2 | * The arithmetic of drawing between two ticks, on its own: no world, no GL. | |
| 3 | * What matters here is the seam in the circle, the two ends of the blend being | |
| 4 | * exactly the states they are blends of, and a teleport never being a slide. | |
| 5 | */ | |
| 6 | ||
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { TICK } from "./loop.ts"; | |
| 9 | import { | |
| 10 | alphaOf, between, drawnAt, lerpPos, lerpYaw, pose, remember, rememberPos, teleported, TELEPORT, | |
| 11 | } from "./interpolate.ts"; | |
| 12 | import type { Vec3 } from "./math.ts"; | |
| 13 | ||
| 14 | test("alpha is the sub-tick remainder, clamped, and a nonsense clock draws the newest tick", () => { | |
| 15 | expect(alphaOf(TICK / 2, TICK)).toBeCloseTo(0.5, 12); | |
| 16 | expect(alphaOf(0, TICK)).toBe(0); | |
| 17 | // A backlog would draw a future the world has not simulated yet. | |
| 18 | expect(alphaOf(TICK * 3, TICK)).toBe(1); | |
| 19 | expect(alphaOf(-1, TICK)).toBe(0); | |
| 20 | expect(alphaOf(NaN, TICK)).toBe(1); | |
| 21 | expect(alphaOf(TICK / 2, 0)).toBe(1); | |
| 22 | }); | |
| 23 | ||
| 24 | test("a position is lerped, and both ends are exactly the state they came from", () => { | |
| 25 | const prev: Vec3 = [0.1, -4, 10]; | |
| 26 | const now: Vec3 = [0.2, 4, 12]; | |
| 27 | const out: Vec3 = [0, 0, 0]; | |
| 28 | expect(lerpPos(prev, now, 0.5, out)).toEqual([0.15000000000000002, 0, 11]); | |
| 29 | // Exact, not "close to": a frame at the end of a tick draws the tick, and a | |
| 30 | // renderer must not wobble by a float's last bit for interpolating at all. | |
| 31 | expect(lerpPos(prev, now, 1, out)).toEqual(now); | |
| 32 | expect(lerpPos(prev, now, 0, out)).toEqual(prev); | |
| 33 | // It writes into what it was given and returns it — no tuple is allocated. | |
| 34 | expect(lerpPos(prev, now, 0.5, out)).toBe(out); | |
| 35 | }); | |
| 36 | ||
| 37 | test("a yaw goes the short way round the circle, not the long way", () => { | |
| 38 | // The seam: −170° to +170° is 20° through south, and the midpoint is 180°. | |
| 39 | expect(lerpYaw(-170, 170, 0.5)).toBeCloseTo(-180, 9); | |
| 40 | expect(lerpYaw(170, -170, 0.5)).toBeCloseTo(180, 9); | |
| 41 | // A quarter of the way over the seam is five degrees, not eighty-five. | |
| 42 | expect(lerpYaw(-170, 170, 0.25)).toBeCloseTo(-175, 9); | |
| 43 | // And the ordinary case is the ordinary answer, both ends exact. | |
| 44 | expect(lerpYaw(10, 50, 0.5)).toBeCloseTo(30, 9); | |
| 45 | expect(lerpYaw(10, 50, 1)).toBe(50); | |
| 46 | expect(lerpYaw(10, 50, 0)).toBe(10); | |
| 47 | }); | |
| 48 | ||
| 49 | test("a move is interpolated and a teleport snaps", () => { | |
| 50 | const walked: Vec3 = [0, 0, 0]; | |
| 51 | const step: Vec3 = [4, 0, 0]; // a tick of running, about | |
| 52 | expect(teleported(walked, step)).toBe(false); | |
| 53 | // A spawn across the map, a respawn, anything further than a tick of walking. | |
| 54 | expect(teleported(walked, [0, 2000, 0])).toBe(true); | |
| 55 | expect(teleported(walked, [TELEPORT + 1, 0, 0])).toBe(true); | |
| 56 | expect(teleported(walked, [TELEPORT, 0, 0])).toBe(false); | |
| 57 | ||
| 58 | const out: Vec3 = [0, 0, 0]; | |
| 59 | expect(lerpPos(walked, [0, 2000, 0], 0.5, out)).toEqual([0, 2000, 0]); | |
| 60 | }); | |
| 61 | ||
| 62 | test("a pose blends both halves, and a teleport takes the newest of both", () => { | |
| 63 | const prev = pose([0, 0, 0], 0); | |
| 64 | const now = { pos: [10, 0, 0] as Vec3, yaw: 90 }; | |
| 65 | const out = pose(); | |
| 66 | expect(between(prev, now, 0.5, out)).toBe(out); | |
| 67 | expect(out.pos).toEqual([5, 0, 0]); | |
| 68 | expect(out.yaw).toBeCloseTo(45, 9); | |
| 69 | ||
| 70 | // Teleported: the yaw does not creep round either, because the body that | |
| 71 | // arrived is facing the way it is facing and was never turning to get there. | |
| 72 | const far = { pos: [900, 0, 0] as Vec3, yaw: 90 }; | |
| 73 | between(prev, far, 0.5, out); | |
| 74 | expect(out.pos).toEqual([900, 0, 0]); | |
| 75 | expect(out.yaw).toBe(90); | |
| 76 | }); | |
| 77 | ||
| 78 | test("remembering is written into the tuples that are already there", () => { | |
| 79 | const prev = pose([1, 2, 3], 45); | |
| 80 | const held = prev.pos; | |
| 81 | remember(prev, { pos: [9, 8, 7], yaw: -20 }); | |
| 82 | expect(prev.pos).toEqual([9, 8, 7]); | |
| 83 | expect(prev.yaw).toBe(-20); | |
| 84 | expect(prev.pos).toBe(held); // the same array: a tick allocates nothing | |
| 85 | ||
| 86 | const spot: Vec3 = [0, 0, 0]; | |
| 87 | rememberPos(spot, [5, 6, 7]); | |
| 88 | expect(spot).toEqual([5, 6, 7]); | |
| 89 | }); | |
| 90 | ||
| 91 | test("the clock a frame is drawn on is behind the simulation, never ahead of it", () => { | |
| 92 | expect(drawnAt(10, 1, TICK)).toBe(10); | |
| 93 | expect(drawnAt(10, 0, TICK)).toBeCloseTo(10 - TICK, 12); | |
| 94 | expect(drawnAt(10, 0.5, TICK)).toBeCloseTo(10 - TICK / 2, 12); | |
| 95 | expect(drawnAt(10, NaN, TICK)).toBe(10); | |
| 96 | }); |
src/client/interpolate.ts added +117
| @@ -0,0 +1,117 @@ | ||
| 1 | /** | |
| 2 | * What is drawn between two ticks. | |
| 3 | * | |
| 4 | * The world steps at a fixed 64 Hz and a screen draws at 60, 120 or whatever | |
| 5 | * else it feels like, so most frames land partway between two states. This is | |
| 6 | * the arithmetic that puts them there: `alpha` is the sub-tick remainder the | |
| 7 | * loop already hands back, 0 being the state the previous tick left and 1 the | |
| 8 | * state the newest one did, and everything here is exact at both ends — a | |
| 9 | * frame drawn at `alpha` of 1 is the same numbers as no interpolation at all. | |
| 10 | * | |
| 11 | * Two rules it keeps: | |
| 12 | * | |
| 13 | * - A yaw goes the short way round the circle. −170° to +170° is 20° across | |
| 14 | * the seam, not 340° the long way, so a player turning through south does | |
| 15 | * not spin all the way back. | |
| 16 | * - A teleport snaps. A round start, a respawn or anything that moves further | |
| 17 | * in one tick than a body could have walked is not a move to draw halfway | |
| 18 | * through: past `TELEPORT` the newest state is drawn and nothing slides. | |
| 19 | * | |
| 20 | * It is pure — no world, no GL, no clock — and it writes into the tuples it is | |
| 21 | * given rather than returning new ones, because a renderer calls it once per | |
| 22 | * player per frame and nothing here may allocate. | |
| 23 | */ | |
| 24 | ||
| 25 | import { angleDelta, clamp, type Vec3 } from "./math.ts"; | |
| 26 | ||
| 27 | /** Where something was and which way it faced: everything a mesh is placed by. */ | |
| 28 | export type Pose = { pos: Vec3; yaw: number }; | |
| 29 | ||
| 30 | /** | |
| 31 | * Further apart than one tick of movement can be. A player runs at 250 u/s and | |
| 32 | * falls no faster than a grenade flies at 780, which is 12 units in a 64 Hz | |
| 33 | * tick; 64 units a tick is 4096 u/s, which nothing in this game does. Past it, | |
| 34 | * the two states are two places, not a path between them. | |
| 35 | */ | |
| 36 | export const TELEPORT = 64; | |
| 37 | ||
| 38 | /** A fresh pose at the origin. Made once per thing that moves, never per tick. */ | |
| 39 | export const pose = (pos: Vec3 = [0, 0, 0], yaw = 0): Pose => ({ pos: [pos[0], pos[1], pos[2]], yaw }); | |
| 40 | ||
| 41 | /** | |
| 42 | * How far into the tick this frame is, from the accumulator `advance` returns. | |
| 43 | * Clamped to [0, 1]: a backlog would draw the future, which is worse than | |
| 44 | * drawing the past, and anything that is not a number draws the newest state. | |
| 45 | */ | |
| 46 | export function alphaOf(accumulator: number, tick: number): number { | |
| 47 | if (!Number.isFinite(accumulator) || !Number.isFinite(tick) || tick <= 0) return 1; | |
| 48 | return clamp(accumulator / tick, 0, 1); | |
| 49 | } | |
| 50 | ||
| 51 | /** Partway between two numbers, and exactly one of them at either end. */ | |
| 52 | const mix = (a: number, b: number, alpha: number): number => | |
| 53 | alpha <= 0 ? a : alpha >= 1 ? b : a + (b - a) * alpha; | |
| 54 | ||
| 55 | /** Is this a step, or a jump? Squared, so a frame never pays for a square root. */ | |
| 56 | export function teleported(a: Vec3, b: Vec3, limit: number = TELEPORT): boolean { | |
| 57 | const dx = b[0] - a[0], dy = b[1] - a[1], dz = b[2] - a[2]; | |
| 58 | return dx * dx + dy * dy + dz * dz > limit * limit; | |
| 59 | } | |
| 60 | ||
| 61 | /** Partway round the circle from `a` to `b`, the short way, in degrees. */ | |
| 62 | export const lerpYaw = (a: number, b: number, alpha: number): number => | |
| 63 | alpha <= 0 ? a : alpha >= 1 ? b : a + angleDelta(a, b) * alpha; | |
| 64 | ||
| 65 | /** | |
| 66 | * Partway from `prev` to `now`, written into `out` (which may be `out === now` | |
| 67 | * for a caller that has nowhere else to put it). A teleport is the newest | |
| 68 | * state and no blend at all. | |
| 69 | */ | |
| 70 | export function lerpPos(prev: Vec3, now: Vec3, alpha: number, out: Vec3): Vec3 { | |
| 71 | if (teleported(prev, now)) { | |
| 72 | out[0] = now[0]; | |
| 73 | out[1] = now[1]; | |
| 74 | out[2] = now[2]; | |
| 75 | return out; | |
| 76 | } | |
| 77 | out[0] = mix(prev[0], now[0], alpha); | |
| 78 | out[1] = mix(prev[1], now[1], alpha); | |
| 79 | out[2] = mix(prev[2], now[2], alpha); | |
| 80 | return out; | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * The pose to draw something at: its position lerped and its yaw turned the | |
| 85 | * short way, or the newest of both where it teleported. `now` is whatever | |
| 86 | * carries a `pos` and a `yaw` — a player is one already. | |
| 87 | */ | |
| 88 | export function between(prev: Pose, now: Pose, alpha: number, out: Pose): Pose { | |
| 89 | const jumped = teleported(prev.pos, now.pos); | |
| 90 | lerpPos(prev.pos, now.pos, alpha, out.pos); | |
| 91 | out.yaw = jumped ? now.yaw : lerpYaw(prev.yaw, now.yaw, alpha); | |
| 92 | return out; | |
| 93 | } | |
| 94 | ||
| 95 | /** Write `now` into the pose kept for it: the one copy a tick is allowed to make. */ | |
| 96 | export function remember(prev: Pose, now: Pose): void { | |
| 97 | prev.pos[0] = now.pos[0]; | |
| 98 | prev.pos[1] = now.pos[1]; | |
| 99 | prev.pos[2] = now.pos[2]; | |
| 100 | prev.yaw = now.yaw; | |
| 101 | } | |
| 102 | ||
| 103 | /** The same for something with a position and no facing: a grenade, the bomb. */ | |
| 104 | export function rememberPos(prev: Vec3, now: Vec3): void { | |
| 105 | prev[0] = now[0]; | |
| 106 | prev[1] = now[1]; | |
| 107 | prev[2] = now[2]; | |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * The clock a frame is drawn on: the previous tick's time plus `alpha` of a | |
| 112 | * tick. Animations measured off `world.time` — a body toppling, say — advance | |
| 113 | * by the frame rather than by the step when they use this, and it is never | |
| 114 | * ahead of the simulation, so nothing is drawn before it happened. | |
| 115 | */ | |
| 116 | export const drawnAt = (time: number, alpha: number, tick: number): number => | |
| 117 | time - tick * (1 - clamp(Number.isFinite(alpha) ? alpha : 1, 0, 1)); |
src/client/main.ts +11 −2
| @@ -14,6 +14,7 @@ import { buyMenu, type BuyRow } from "./buy.ts"; | ||
| 14 | 14 | import { cuesHeard } from "./earshot.ts"; |
| 15 | 15 | import { attachFullscreen } from "./fullscreen.ts"; |
| 16 | 16 | import { drawHud } from "./hud.ts"; |
| 17 | import { alphaOf } from "./interpolate.ts"; | |
| 17 | 18 | import { ACTIONS, attachButtons, createControls, DEFAULT_BINDINGS, type Action } from "./input.ts"; |
| 18 | 19 | import { advance, TICK, waiting } from "./loop.ts"; |
| 19 | 20 | import { clamp } from "./math.ts"; |
| @@ -770,7 +771,15 @@ function boot(): void { | ||
| 770 | 771 | renderer.setScale(next); |
| 771 | 772 | } |
| 772 | 773 | } |
| 773 | renderer.draw(world, camera, view, elapsed); | |
| 774 | // How far into the current tick this frame is. Taken after the steps have | |
| 775 | // run — `advance` hands back the remainder past the last one it reported — | |
| 776 | // so the renderer draws between the two ticks it has and never past the | |
| 777 | // newest one, which would be drawing a future the world has not simulated. | |
| 778 | // The camera above is not part of that: it is this frame's aim and the | |
| 779 | // newest eye position, because 15 ms of lag on your own view is the one | |
| 780 | // thing smoothing everybody else must not buy. | |
| 781 | const alpha = alphaOf(accumulator, TICK); | |
| 782 | renderer.draw(world, camera, view, elapsed, alpha); | |
| 774 | 783 | |
| 775 | 784 | // The HUD keeps the full device ratio whatever the scene costs: text that |
| 776 | 785 | // goes soft reads as a broken page, and 2D text is not what is slow. |
| @@ -783,7 +792,7 @@ function boot(): void { | ||
| 783 | 792 | } |
| 784 | 793 | // The same camera the scene was drawn with: the HUD's name plates have to |
| 785 | 794 | // agree with the pixels the renderer just put on the screen. |
| 786 | drawHud(hudCtx, world, view, camera, w, h, controls.scoreboard, settings.crosshair); | |
| 795 | drawHud(hudCtx, world, view, camera, w, h, controls.scoreboard, settings.crosshair, alpha); | |
| 787 | 796 | // The fps is the frame time and nothing else; the scale is an aside, shown |
| 788 | 797 | // only when it is costing you pixels, so a slow machine says why. |
| 789 | 798 | const note = renderScale < MAX_SCALE ? ` · ${Math.round(renderScale * 100)}% res` : ""; |
src/client/renderer.test.ts +140 −5
| @@ -18,10 +18,10 @@ import { expect, test } from "bun:test"; | ||
| 18 | 18 | import { addBody } from "./bodies.ts"; |
| 19 | 19 | import { advance, TICK, waiting } from "./loop.ts"; |
| 20 | 20 | import { SHORT } from "./match.ts"; |
| 21 | import { HELD_MUZZLE, levelMesh, muzzle, viewModelMesh } from "./models.ts"; | |
| 21 | import { grenadeMesh, HELD_MUZZLE, levelMesh, muzzle, playerMesh, viewModelMesh } from "./models.ts"; | |
| 22 | 22 | import { createRenderer, LINE_CAPACITY, type Camera, type Renderer, type RendererHooks } from "./renderer.ts"; |
| 23 | 23 | import { WEAPONS, type WeaponId } from "./weapons.ts"; |
| 24 | import { createWorld, eyePos, type Effect, type Player, type World } from "./world.ts"; | |
| 24 | import { createWorld, eyePos, startRound, type Effect, type Player, type World } from "./world.ts"; | |
| 25 | 25 | |
| 26 | 26 | type Call = { name: string; args: unknown[] }; |
| 27 | 27 | |
| @@ -155,7 +155,7 @@ test("a grenade in the air is one more draw, off a mesh uploaded before the fram | ||
| 155 | 155 | const empty = frame(canvas, calls, world, world.me).length; |
| 156 | 156 | world.grenades.push({ |
| 157 | 157 | kind: "he", owner: world.me.id, team: world.me.team, |
| 158 | pos: [64, 0, 40], vel: [300, 0, 120], fuse: 1.2, | |
| 158 | pos: [64, 0, 40], prev: [64, 0, 40], vel: [300, 0, 120], fuse: 1.2, | |
| 159 | 159 | }); |
| 160 | 160 | const flying = frame(canvas, calls, world, world.me); |
| 161 | 161 | expect(flying.length).toBe(empty + 1); |
| @@ -543,7 +543,7 @@ test("a frame's draws are the world's, one apiece, and its passes are two progra | ||
| 543 | 543 | addBody(world.bodies, { id: 30 + i, team: "t", pos: [i * 96, 0, 0], yaw: i * 40 }, world.boxes, world.time); |
| 544 | 544 | for (let i = 0; i < 6; i += 1) |
| 545 | 545 | world.pickups.push({ weapon: "ak47", pos: [i * 64, 128, 0], ammo: { mag: 30, reserve: 90 } }); |
| 546 | world.grenades.push({ kind: "he", owner: world.me.id, team: "ct", pos: [0, 0, 60], vel: [200, 0, 0], fuse: 1 }); | |
| 546 | world.grenades.push({ kind: "he", owner: world.me.id, team: "ct", pos: [0, 0, 60], prev: [0, 0, 60], vel: [200, 0, 0], fuse: 1 }); | |
| 547 | 547 | world.bomb.state = "planted"; |
| 548 | 548 | world.bomb.pos = [256, 256, 0]; |
| 549 | 549 | world.effects.push({ kind: "tracer", at: world.time, from: [0, 0, 40], to: [400, 0, 40] }); |
| @@ -603,7 +603,7 @@ test("the worst round this game can build stays inside the frame's draw budget", | ||
| 603 | 603 | world.pickups.push({ weapon: "m4a4", pos: [i * 48, 256, 0], ammo: { mag: 30, reserve: 90 } }); |
| 604 | 604 | // Every player's grenade in the air on the same frame. |
| 605 | 605 | for (const p of world.players) |
| 606 | world.grenades.push({ kind: "he", owner: p.id, team: p.team, pos: [p.pos[0], p.pos[1], 60], vel: [0, 0, 100], fuse: 2 }); | |
| 606 | world.grenades.push({ kind: "he", owner: p.id, team: p.team, pos: [p.pos[0], p.pos[1], 60], prev: [p.pos[0], p.pos[1], 60], vel: [0, 0, 100], fuse: 2 }); | |
| 607 | 607 | world.bomb.state = "planted"; |
| 608 | 608 | world.bomb.pos = [512, 512, 0]; |
| 609 | 609 | // A screen full of effects: forty tracers, forty sparks, twenty hits, and |
| @@ -638,3 +638,138 @@ test("the worst round this game can build stays inside the frame's draw budget", | ||
| 638 | 638 | expect(run.first + run.count).toBeLessThanOrEqual(LINE_CAPACITY); |
| 639 | 639 | expect(count(frameCalls, "bufferData")).toBe(0); |
| 640 | 640 | }); |
| 641 | ||
| 642 | // --- movement between ticks | |
| 643 | // | |
| 644 | // The world steps at 64 Hz and the screen draws at 60 or 120, so most frames | |
| 645 | // land partway between two states. What can be checked here is exactly that: | |
| 646 | // the matrix a mesh was drawn with, against the two states it was drawn from. | |
| 647 | ||
| 648 | /** Every model matrix a mesh of `indices` indices was drawn with, in order. */ | |
| 649 | function placesOf(calls: Call[], indices: number): Float32Array[] { | |
| 650 | const out: Float32Array[] = []; | |
| 651 | let last: Float32Array | null = null; | |
| 652 | for (const call of calls) { | |
| 653 | if (call.name === "uniformMatrix4fv") last = call.args[2] as Float32Array; | |
| 654 | else if (call.name === "drawElements" && call.args[1] === indices && last !== null) out.push(last); | |
| 655 | } | |
| 656 | return out; | |
| 657 | } | |
| 658 | ||
| 659 | /** One frame drawn `alpha` of the way into the current tick. */ | |
| 660 | function recordAt(renderer: Renderer, calls: Call[], world: World, view: Player, alpha: number): Call[] { | |
| 661 | calls.length = 0; | |
| 662 | renderer.draw(world, CAMERA, view, 1 / 60, alpha); | |
| 663 | return calls.slice(); | |
| 664 | } | |
| 665 | ||
| 666 | /** The yaw a `modelMatrix` was built from, in degrees. */ | |
| 667 | const yawOf = (m: Float32Array): number => (Math.atan2(m[1] as number, m[0] as number) * 180) / Math.PI; | |
| 668 | ||
| 669 | /** Everyone but the camera's own player and `kept` off their feet, so one mesh is drawn. */ | |
| 670 | function onlyOne(world: World, view: Player, kept: Player): void { | |
| 671 | for (const p of world.players) if (p !== view && p !== kept) p.alive = false; | |
| 672 | } | |
| 673 | ||
| 674 | test("another player is drawn between the tick that just ran and the one before it", () => { | |
| 675 | const { canvas, calls } = recorder(); | |
| 676 | const renderer = attach(canvas); | |
| 677 | const world = scene(); | |
| 678 | const mate = world.players.find((p) => p !== world.me && p.alive) as Player; | |
| 679 | onlyOne(world, world.me, mate); | |
| 680 | const indices = playerMesh().index.length; | |
| 681 | ||
| 682 | mate.prev.pos = [100, 0, 0]; | |
| 683 | mate.prev.yaw = 0; | |
| 684 | mate.pos = [110, 40, 0]; | |
| 685 | mate.yaw = 20; | |
| 686 | ||
| 687 | // Halfway through the tick is halfway along the step it took. | |
| 688 | const half = placesOf(recordAt(renderer, calls, world, world.me, 0.5), indices)[0] as Float32Array; | |
| 689 | expect(half[12]).toBeCloseTo(105, 6); | |
| 690 | expect(half[13]).toBeCloseTo(20, 6); | |
| 691 | expect(yawOf(half)).toBeCloseTo(10, 4); // read back out of a Float32Array | |
| 692 | ||
| 693 | // And the two ends are the two ticks themselves, to the bit: a frame at the | |
| 694 | // end of a tick draws that tick and not a float's-width away from it. | |
| 695 | const newest = placesOf(recordAt(renderer, calls, world, world.me, 1), indices)[0] as Float32Array; | |
| 696 | expect(newest[12]).toBe(110); | |
| 697 | expect(newest[13]).toBe(40); | |
| 698 | expect(yawOf(newest)).toBeCloseTo(20, 4); | |
| 699 | const oldest = placesOf(recordAt(renderer, calls, world, world.me, 0), indices)[0] as Float32Array; | |
| 700 | expect(oldest[12]).toBe(100); | |
| 701 | expect(oldest[13]).toBe(0); | |
| 702 | expect(yawOf(oldest)).toBeCloseTo(0, 4); | |
| 703 | ||
| 704 | // A yaw crossing the seam goes the short way: −170° to 170° is 20° of turn | |
| 705 | // through south, so the middle of it is 180° and never 0°. | |
| 706 | mate.prev.pos = [100, 0, 0]; | |
| 707 | mate.pos = [100, 0, 0]; | |
| 708 | mate.prev.yaw = -170; | |
| 709 | mate.yaw = 170; | |
| 710 | const seam = placesOf(recordAt(renderer, calls, world, world.me, 0.5), indices)[0] as Float32Array; | |
| 711 | expect(Math.abs(yawOf(seam))).toBeCloseTo(180, 4); | |
| 712 | }); | |
| 713 | ||
| 714 | test("a teleport snaps: a respawn is drawn where they are, not sliding across the map", () => { | |
| 715 | const { canvas, calls } = recorder(); | |
| 716 | const renderer = attach(canvas); | |
| 717 | const world = scene(); | |
| 718 | const mate = world.players.find((p) => p !== world.me && p.alive) as Player; | |
| 719 | onlyOne(world, world.me, mate); | |
| 720 | const indices = playerMesh().index.length; | |
| 721 | ||
| 722 | // The state a round start leaves behind before `startRound` resets it, and | |
| 723 | // anything else that moves a body further in a tick than it could walk. | |
| 724 | mate.prev.pos = [0, 0, 0]; | |
| 725 | mate.prev.yaw = -90; | |
| 726 | mate.pos = [1800, -1200, 2]; | |
| 727 | mate.yaw = 90; | |
| 728 | const mid = placesOf(recordAt(renderer, calls, world, world.me, 0.5), indices)[0] as Float32Array; | |
| 729 | expect(mid[12]).toBe(1800); | |
| 730 | expect(mid[13]).toBe(-1200); | |
| 731 | expect(mid[14]).toBe(2); | |
| 732 | expect(yawOf(mid)).toBeCloseTo(90, 4); | |
| 733 | ||
| 734 | // And the world itself never leaves that state lying around: a round start | |
| 735 | // puts the previous pose where the player now is. | |
| 736 | startRound(world); | |
| 737 | for (const p of world.players) { | |
| 738 | expect(p.prev.pos).toEqual(p.pos); | |
| 739 | expect(p.prev.yaw).toBe(p.yaw); | |
| 740 | } | |
| 741 | }); | |
| 742 | ||
| 743 | test("a grenade is drawn between its two ticks too", () => { | |
| 744 | const { canvas, calls } = recorder(); | |
| 745 | const renderer = attach(canvas); | |
| 746 | const world = scene(); | |
| 747 | world.grenades.push({ | |
| 748 | kind: "he", owner: world.me.id, team: "ct", | |
| 749 | pos: [12, 0, 60], prev: [0, 0, 64], vel: [768, 0, -256], fuse: 1, | |
| 750 | }); | |
| 751 | const indices = grenadeMesh().index.length; | |
| 752 | const mid = placesOf(recordAt(renderer, calls, world, world.me, 0.5), indices)[0] as Float32Array; | |
| 753 | expect(mid[12]).toBeCloseTo(6, 6); | |
| 754 | expect(mid[14]).toBeCloseTo(62, 6); | |
| 755 | }); | |
| 756 | ||
| 757 | test("a body topples by the frame and not by the step", () => { | |
| 758 | const { canvas, calls } = recorder(); | |
| 759 | const renderer = attach(canvas); | |
| 760 | const world = scene(); | |
| 761 | for (const p of world.players) if (p !== world.me) p.alive = false; | |
| 762 | addBody(world.bodies, { id: 51, team: "t", pos: [200, 0, 0], yaw: 0 }, world.boxes, world.time - 0.2); | |
| 763 | const indices = playerMesh().index.length; | |
| 764 | ||
| 765 | // The fall is measured off the clock the frame is drawn on — the previous | |
| 766 | // tick's time plus `alpha` of a tick — so a body halfway over is further | |
| 767 | // over at the end of a tick than it was at the start of it. | |
| 768 | const tip = (alpha: number): number => { | |
| 769 | const place = placesOf(recordAt(renderer, calls, world, world.me, alpha), indices)[0] as Float32Array; | |
| 770 | return -(place[2] as number); // sin(tip) * flatten: it grows as it goes over | |
| 771 | }; | |
| 772 | expect(tip(1)).toBeGreaterThan(tip(0)); | |
| 773 | expect(tip(0.5)).toBeGreaterThan(tip(0)); | |
| 774 | expect(tip(1)).toBeGreaterThan(tip(0.5)); | |
| 775 | }); |
src/client/renderer.ts +56 −13
| @@ -13,6 +13,8 @@ | ||
| 13 | 13 | * all, and `webglcontextrestored` builds the lot a second time. |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | import { between, drawnAt, lerpPos, pose, type Pose } from "./interpolate.ts"; | |
| 17 | import { TICK } from "./loop.ts"; | |
| 16 | 18 | import { fallenMatrix, multiply, perspective, rad, viewMatrix, modelMatrix, type Vec3 } from "./math.ts"; |
| 17 | 19 | import { |
| 18 | 20 | bombMesh, grenadeMesh, HELD_MUZZLE, levelMesh, muzzle, pickupMesh, playerMesh, viewModelMesh, |
| @@ -275,8 +277,20 @@ function build(gl: WebGL2RenderingContext): Gpu { | ||
| 275 | 277 | } |
| 276 | 278 | |
| 277 | 279 | export type Renderer = { |
| 278 | /** `view` is the player the camera is inside: their own mesh is not drawn. */ | |
| 279 | draw(world: World, camera: Camera, view: Player, dt: number): void; | |
| 280 | /** | |
| 281 | * `view` is the player the camera is inside: their own mesh is not drawn. | |
| 282 | * | |
| 283 | * `alpha` is how far into the current tick this frame is — `interpolate.ts` | |
| 284 | * reads it off the accumulator the loop banks — and everything in the world | |
| 285 | * that moves is drawn between where it was when the last tick ended and | |
| 286 | * where it is now. It defaults to 1, the newest tick and no blend at all, | |
| 287 | * because that is what a caller with no clock of its own means. | |
| 288 | * | |
| 289 | * The camera is not one of those things. It comes from this frame's input | |
| 290 | * and the newest eye position, both of them handed in by `main.ts`: your own | |
| 291 | * aim may never be a tick behind your own mouse. | |
| 292 | */ | |
| 293 | draw(world: World, camera: Camera, view: Player, dt: number, alpha?: number): void; | |
| 280 | 294 | resize(): void; |
| 281 | 295 | /** |
| 282 | 296 | * The fraction of the device pixel ratio the drawing buffer uses. `quality` |
| @@ -348,6 +362,15 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 348 | 362 | scale = Math.min(MAX_SCALE, Math.max(MIN_SCALE, next)); |
| 349 | 363 | }; |
| 350 | 364 | |
| 365 | // The four places a blended pose is put while it is being drawn from, made | |
| 366 | // once here rather than per frame: a renderer that interpolated into a fresh | |
| 367 | // tuple would allocate one per player, per flash and per grenade, every | |
| 368 | // frame, for numbers that are dead the moment the draw call is made. | |
| 369 | const drawn: Pose = pose(); | |
| 370 | const flashAt: Pose = pose(); | |
| 371 | const spot: Vec3 = [0, 0, 0]; | |
| 372 | const bombSpot: Vec3 = [0, 0, 0]; | |
| 373 | ||
| 351 | 374 | let bobPhase = 0; |
| 352 | 375 | // The gun the hands last had, and when they were given it: a change of |
| 353 | 376 | // weapon — or of the player the camera is inside — starts a raise. It is |
| @@ -356,7 +379,7 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 356 | 379 | let heldId = -1; |
| 357 | 380 | let raisedAt = -99; |
| 358 | 381 | |
| 359 | const draw = (world: World, camera: Camera, view: Player, dt: number): void => { | |
| 382 | const draw = (world: World, camera: Camera, view: Player, dt: number, alpha = 1): void => { | |
| 360 | 383 | // The flag the event set, asked once: a GL call on a lost context is at |
| 361 | 384 | // best ignored and `isContextLost()` is a synchronous question the frame |
| 362 | 385 | // path should never have to ask. |
| @@ -401,7 +424,12 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 401 | 424 | for (const p of world.players) { |
| 402 | 425 | if (!p.alive || p === view) continue; |
| 403 | 426 | const squash = hullHeight(p) / 72; |
| 404 | gl.uniformMatrix4fv(u.model, false, modelMatrix(p.pos, p.yaw, 1, 1, squash)); | |
| 427 | // Between the tick that just ran and the one before it: at 60 Hz against | |
| 428 | // 64, this is the difference between a body that walks and one that | |
| 429 | // stutters every fifteenth frame. A spawn or a respawn is further than a | |
| 430 | // tick of walking, and `between` snaps rather than sliding. | |
| 431 | const at = between(p.prev, p, alpha, drawn); | |
| 432 | gl.uniformMatrix4fv(u.model, false, modelMatrix(at.pos, at.yaw, 1, 1, squash)); | |
| 405 | 433 | const tint = TEAM_TINT[p.team] ?? [1, 1, 1]; |
| 406 | 434 | gl.uniform3f(u.tint, tint[0], tint[1], tint[2]); |
| 407 | 435 | gl.drawElements(gl.TRIANGLES, player.count, gl.UNSIGNED_INT, 0); |
| @@ -409,8 +437,13 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 409 | 437 | |
| 410 | 438 | // The dead of this round, on the same mesh and the same buffer: one draw |
| 411 | 439 | // each and not a byte of state beyond the matrix and the tint. |
| 440 | // A body lies where it fell and never moves again, so it has nothing to | |
| 441 | // interpolate but its own topple: `drawnAt` is the clock this frame is on | |
| 442 | // — the last tick's time plus `alpha` of a tick — so the fall advances by | |
| 443 | // the frame and not by the step, and it is never ahead of the simulation. | |
| 444 | const time = drawnAt(world.time, alpha, TICK); | |
| 412 | 445 | for (const body of world.bodies) { |
| 413 | const t = Math.min(1, Math.max(0, (world.time - body.at) / BODY_FALL)); | |
| 446 | const t = Math.min(1, Math.max(0, (time - body.at) / BODY_FALL)); | |
| 414 | 447 | const fall = t * t * (3 - 2 * t); // smoothstep: it goes over and settles |
| 415 | 448 | const place = fallenMatrix( |
| 416 | 449 | [body.pos[0], body.pos[1], body.pos[2] + BODY_LIFT * fall], |
| @@ -441,13 +474,19 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 441 | 474 | gl.uniform3f(u.tint, 0.8, 0.95, 0.72); |
| 442 | 475 | gl.bindVertexArray(grenade.vao); |
| 443 | 476 | for (const g of world.grenades) { |
| 444 | gl.uniformMatrix4fv(u.model, false, modelMatrix(g.pos, world.time * 520)); | |
| 477 | // A grenade crosses twelve units in a tick: it is the thing in this | |
| 478 | // world that judders hardest without this line. | |
| 479 | gl.uniformMatrix4fv(u.model, false, modelMatrix(lerpPos(g.prev, g.pos, alpha, spot), world.time * 520)); | |
| 445 | 480 | gl.drawElements(gl.TRIANGLES, grenade.count, gl.UNSIGNED_INT, 0); |
| 446 | 481 | } |
| 447 | 482 | } |
| 448 | 483 | |
| 484 | // A planted or dropped bomb is standing still and a carried one is not | |
| 485 | // drawn at all, so this blend is almost always the identity — but it is | |
| 486 | // the same blend, so the beam below and the mesh here agree on one place. | |
| 487 | const bombAt = lerpPos(world.bomb.prev, world.bomb.pos, alpha, bombSpot); | |
| 449 | 488 | if (world.bomb.state === "planted" || world.bomb.state === "dropped") { |
| 450 | gl.uniformMatrix4fv(u.model, false, modelMatrix(world.bomb.pos, 0)); | |
| 489 | gl.uniformMatrix4fv(u.model, false, modelMatrix(bombAt, 0)); | |
| 451 | 490 | gl.uniform3f(u.tint, 1, 1, 1); |
| 452 | 491 | gl.bindVertexArray(bomb.vao); |
| 453 | 492 | gl.drawElements(gl.TRIANGLES, bomb.count, gl.UNSIGNED_INT, 0); |
| @@ -511,8 +550,8 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 511 | 550 | } |
| 512 | 551 | if (world.bomb.state === "planted") { |
| 513 | 552 | const blink = Math.sin(world.time * 8) > 0 ? 1 : 0.2; |
| 514 | push(world.bomb.pos[0], world.bomb.pos[1], world.bomb.pos[2] + 14, 1, 0.2 * blink, 0.15); | |
| 515 | push(world.bomb.pos[0], world.bomb.pos[1], world.bomb.pos[2] + 140, 1 * blink, 0.15, 0.1); | |
| 553 | push(bombAt[0], bombAt[1], bombAt[2] + 14, 1, 0.2 * blink, 0.15); | |
| 554 | push(bombAt[0], bombAt[1], bombAt[2] + 140, 1 * blink, 0.15, 0.1); | |
| 516 | 555 | } |
| 517 | 556 | // Everything above this line is drawn as it is; everything below it is |
| 518 | 557 | // drawn added to what is there, so the two passes are split here. |
| @@ -541,10 +580,14 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 541 | 580 | let mx = effect.pos[0], my = effect.pos[1], mz = effect.pos[2]; |
| 542 | 581 | for (const p of world.players) { |
| 543 | 582 | if (p.id !== effect.shooter || !p.alive) continue; |
| 544 | const c = Math.cos(rad(p.yaw)), s = Math.sin(rad(p.yaw)); | |
| 545 | mx = p.pos[0] + HELD_MUZZLE[0] * c - HELD_MUZZLE[1] * s; | |
| 546 | my = p.pos[1] + HELD_MUZZLE[0] * s + HELD_MUZZLE[1] * c; | |
| 547 | mz = p.pos[2] + HELD_MUZZLE[2] * (hullHeight(p) / 72); | |
| 583 | // The pose the mesh was drawn at, not the newest one: a flash that sat | |
| 584 | // on the tick the gun is at rather than the one the shooter is drawn | |
| 585 | // at would float off the end of the barrel while they moved. | |
| 586 | const at = between(p.prev, p, alpha, flashAt); | |
| 587 | const c = Math.cos(rad(at.yaw)), s = Math.sin(rad(at.yaw)); | |
| 588 | mx = at.pos[0] + HELD_MUZZLE[0] * c - HELD_MUZZLE[1] * s; | |
| 589 | my = at.pos[1] + HELD_MUZZLE[0] * s + HELD_MUZZLE[1] * c; | |
| 590 | mz = at.pos[2] + HELD_MUZZLE[2] * (hullHeight(p) / 72); | |
| 548 | 591 | break; |
| 549 | 592 | } |
| 550 | 593 | const bright = 1 - age / FLASH_LIFE; |
src/client/world.test.ts +65 −21
| @@ -1,12 +1,14 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { GRENADE } from "./grenades.ts"; |
| 3 | import { teleported } from "./interpolate.ts"; | |
| 4 | import { dist } from "./math.ts"; | |
| 3 | 5 | import { DE_FORGE, siteAt } from "./map.ts"; |
| 4 | 6 | import { SHORT, winReward } from "./match.ts"; |
| 5 | 7 | import { WEAPONS } from "./weapons.ts"; |
| 6 | 8 | import { |
| 7 | 9 | aimAt, applyInput, bodyCentre, BOMB_BLAST_RADIUS, buy, createWorld, currentWeapon, damageDealt, |
| 8 | damageTaken, ENVIRONMENT, eyePos, flinch, NO_INPUT, offeredPickup, radioCall, shoot, startRound, | |
| 9 | update, | |
| 10 | damageTaken, ENVIRONMENT, eyePos, flinch, newBomb, NO_INPUT, offeredPickup, radioCall, shoot, | |
| 11 | startRound, update, | |
| 10 | 12 | type Input, type Player, type World, |
| 11 | 13 | } from "./world.ts"; |
| 12 | 14 | |
| @@ -108,7 +110,7 @@ test("the terrorists plant, and a planted bomb keeps the round alive past the cl | ||
| 108 | 110 | test("a defusing CT wins the round, and the money follows the rules", () => { |
| 109 | 111 | const world = createWorld(SHORT, "me", "ct", 5, 13); |
| 110 | 112 | run(world, SHORT.freezeTime + 0.2); |
| 111 | world.bomb = { state: "planted", pos: [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2], site: "B", plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 }; | |
| 113 | world.bomb = { ...newBomb([DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2]), state: "planted", site: "B" }; | |
| 112 | 114 | world.bombPlanted = true; |
| 113 | 115 | world.match.phase = "planted"; |
| 114 | 116 | world.match.timer = 30; |
| @@ -346,10 +348,7 @@ test("shooting a bot is silent: damage, but nothing for the HUD to read", () => | ||
| 346 | 348 | test("the bomb going off hurts everyone inside its radius and nobody outside", () => { |
| 347 | 349 | const world = createWorld(SHORT, "me", "ct", 5, 41); |
| 348 | 350 | const far = world.players[1]; |
| 349 | world.bomb = { | |
| 350 | state: "planted", pos: [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2], site: "A", | |
| 351 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, | |
| 352 | }; | |
| 351 | world.bomb = { ...newBomb([DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2]), state: "planted", site: "A" }; | |
| 353 | 352 | world.bombPlanted = true; |
| 354 | 353 | world.match.phase = "planted"; |
| 355 | 354 | world.match.timer = TICK / 2; |
| @@ -375,10 +374,7 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", ( | ||
| 375 | 374 | |
| 376 | 375 | test("the detonation pushes exactly one explode", () => { |
| 377 | 376 | const world = createWorld(SHORT, "me", "ct", 5, 42); |
| 378 | world.bomb = { | |
| 379 | state: "planted", pos: [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2], site: "A", | |
| 380 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, | |
| 381 | }; | |
| 377 | world.bomb = { ...newBomb([DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2]), state: "planted", site: "A" }; | |
| 382 | 378 | world.bombPlanted = true; |
| 383 | 379 | world.match.phase = "planted"; |
| 384 | 380 | world.match.timer = TICK / 2; |
| @@ -392,7 +388,7 @@ test("the detonation pushes exactly one explode", () => { | ||
| 392 | 388 | /** Put a planted bomb at `pos`, alone in the world but for `world.me`, and step once. */ |
| 393 | 389 | function detonateAt(world: World, pos: [number, number, number]): void { |
| 394 | 390 | for (const p of world.players) { p.alive = p === world.me; p.hasBomb = false; } |
| 395 | world.bomb = { state: "planted", pos: [...pos], site: "A", plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 }; | |
| 391 | world.bomb = { ...newBomb(pos), state: "planted", site: "A" }; | |
| 396 | 392 | world.bombPlanted = true; |
| 397 | 393 | world.match.phase = "planted"; |
| 398 | 394 | world.match.timer = TICK / 2; |
| @@ -490,10 +486,7 @@ test("a survivor of the blast is still alive when the next round starts", () => | ||
| 490 | 486 | test("a planted bomb pushes beeps on that cadence, and a defuse stops them", () => { |
| 491 | 487 | const world = createWorld(SHORT, "me", "ct", 5, 71); |
| 492 | 488 | run(world, SHORT.freezeTime + 0.2); |
| 493 | world.bomb = { | |
| 494 | state: "planted", pos: [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2], site: "B", | |
| 495 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, | |
| 496 | }; | |
| 489 | world.bomb = { ...newBomb([DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2]), state: "planted", site: "B" }; | |
| 497 | 490 | world.bombPlanted = true; |
| 498 | 491 | world.match.phase = "planted"; |
| 499 | 492 | world.match.timer = 40; |
| @@ -614,7 +607,7 @@ function armWithGrenade(world: World): void { | ||
| 614 | 607 | function blastAt(world: World, thrower: Player, pos: [number, number, number]): void { |
| 615 | 608 | world.grenades.push({ |
| 616 | 609 | kind: "he", owner: thrower.id, team: thrower.team, |
| 617 | pos: [...pos], vel: [0, 0, 0], fuse: TICK / 2, | |
| 610 | pos: [...pos], prev: [...pos], vel: [0, 0, 0], fuse: TICK / 2, | |
| 618 | 611 | }); |
| 619 | 612 | world.effects.length = 0; |
| 620 | 613 | update(world, TICK, NO_INPUT); |
| @@ -864,10 +857,7 @@ test("a player who both dealt and took appears in both lists", () => { | ||
| 864 | 857 | |
| 865 | 858 | test("the planted bomb's damage lands in the ledger with nobody credited", () => { |
| 866 | 859 | const world = createWorld(SHORT, "me", "ct", 5, 41); |
| 867 | world.bomb = { | |
| 868 | state: "planted", pos: [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2], site: "A", | |
| 869 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, | |
| 870 | }; | |
| 860 | world.bomb = { ...newBomb([DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2]), state: "planted", site: "A" }; | |
| 871 | 861 | world.bombPlanted = true; |
| 872 | 862 | world.match.phase = "planted"; |
| 873 | 863 | world.match.timer = TICK / 2; |
| @@ -935,3 +925,57 @@ test("a dead person still has the radio: the living four are who can act on it", | ||
| 935 | 925 | expect(world.radio).toEqual({ team: "ct", call: "B", at: 12 }); |
| 936 | 926 | expect(world.messages[world.messages.length - 1]?.text).toBe("You: go B"); |
| 937 | 927 | }); |
| 928 | ||
| 929 | // --- what the renderer draws between two ticks | |
| 930 | ||
| 931 | test("a tick remembers where everyone was before it moved them", () => { | |
| 932 | const world = createWorld(SHORT, "me", "ct", 5, 77); | |
| 933 | run(world, SHORT.freezeTime + 0.5, { ...NO_INPUT, forward: 1 }); | |
| 934 | const was = world.players.map((p) => [...p.pos] as [number, number, number]); | |
| 935 | const yaws = world.players.map((p) => p.yaw); | |
| 936 | update(world, TICK, { ...NO_INPUT, forward: 1 }); | |
| 937 | world.players.forEach((p, i) => { | |
| 938 | expect(p.prev.pos).toEqual(was[i]); | |
| 939 | expect(p.prev.yaw).toBe(yaws[i]); | |
| 940 | }); | |
| 941 | // And it is a tick behind and not a copy of now: the person walked. | |
| 942 | expect(dist(world.me.pos, world.me.prev.pos)).toBeGreaterThan(0.5); | |
| 943 | // Close enough to walk: the renderer will draw between the two, not snap. | |
| 944 | expect(teleported(world.me.prev.pos, world.me.pos)).toBe(false); | |
| 945 | }); | |
| 946 | ||
| 947 | test("the previous pose is written into the arrays it already has, not a new one", () => { | |
| 948 | const world = createWorld(SHORT, "me", "ct", 5, 78); | |
| 949 | run(world, SHORT.freezeTime + 0.2, { ...NO_INPUT, forward: 1 }); | |
| 950 | const held = world.players.map((p) => p.prev.pos); | |
| 951 | const bombHeld = world.bomb.prev; | |
| 952 | run(world, 0.5, { ...NO_INPUT, forward: 1 }); | |
| 953 | world.players.forEach((p, i) => expect(p.prev.pos).toBe(held[i])); | |
| 954 | expect(world.bomb.prev).toBe(bombHeld); | |
| 955 | }); | |
| 956 | ||
| 957 | test("a round start is a teleport: nobody is drawn sliding to their spawn", () => { | |
| 958 | const world = createWorld(SHORT, "me", "ct", 5, 79); | |
| 959 | run(world, SHORT.freezeTime + 2, { ...NO_INPUT, forward: 1 }); | |
| 960 | // Somewhere else entirely by now; the spawns are the other side of the map. | |
| 961 | const before = world.players.map((p) => [...p.pos] as [number, number, number]); | |
| 962 | startRound(world); | |
| 963 | world.players.forEach((p, i) => { | |
| 964 | expect(p.prev.pos).toEqual(p.pos); | |
| 965 | expect(p.prev.yaw).toBe(p.yaw); | |
| 966 | // The state that was reset is a real jump: without the reset, this is what | |
| 967 | // the renderer would have drawn a body sliding across. | |
| 968 | expect(teleported(before[i], p.pos)).toBe(true); | |
| 969 | }); | |
| 970 | }); | |
| 971 | ||
| 972 | test("the arena's applyInput does not pay for the renderer's convenience", () => { | |
| 973 | // The RL arena steps players through `applyInput` and draws nothing at all, | |
| 974 | // so the previous pose is `update`'s business and nothing is written here. | |
| 975 | const world = createWorld(SHORT, "me", "ct", 5, 80); | |
| 976 | world.match.phase = "live"; | |
| 977 | const kept = [...world.me.prev.pos] as [number, number, number]; | |
| 978 | for (let i = 0; i < 16; i++) applyInput(world, world.me, { ...NO_INPUT, forward: 1 }, TICK); | |
| 979 | expect(world.me.prev.pos).toEqual(kept); | |
| 980 | expect(dist(world.me.pos, kept)).toBeGreaterThan(1); // it did move | |
| 981 | }); |
src/client/world.ts +51 −2
| @@ -13,6 +13,7 @@ import { stepBeep } from "./cues.ts"; | ||
| 13 | 13 | import { |
| 14 | 14 | blastDamage, GRENADE, step as stepGrenade, thrown, type Grenade, |
| 15 | 15 | } from "./grenades.ts"; |
| 16 | import { pose, remember, rememberPos, type Pose } from "./interpolate.ts"; | |
| 16 | 17 | import { DE_FORGE, siteAt, type MapDef } from "./map.ts"; |
| 17 | 18 | import { angleVectors, angleDelta, clamp, dist, dist2d, normalize, rng, sub, type Vec3 } from "./math.ts"; |
| 18 | 19 | import * as rules from "./match.ts"; |
| @@ -59,6 +60,13 @@ export type Player = Actor & Body & { | ||
| 59 | 60 | damage: number; |
| 60 | 61 | spawnAt: number; |
| 61 | 62 | stepDistance: number; // ground covered since the last footstep, for cadence |
| 63 | /** | |
| 64 | * Where this player was when the previous tick ended. Written once a tick by | |
| 65 | * `update` and read by nothing but the renderer, which draws between it and | |
| 66 | * the newest state — the simulation itself never looks at it, and neither | |
| 67 | * does `observe.ts`, so the bots cannot see a rendering concern. | |
| 68 | */ | |
| 69 | prev: Pose; | |
| 62 | 70 | }; |
| 63 | 71 | |
| 64 | 72 | export type Bomb = { |
| @@ -69,6 +77,8 @@ export type Bomb = { | ||
| 69 | 77 | defuseProgress: number; |
| 70 | 78 | defuser: Player | null; |
| 71 | 79 | beepAt: number; // seconds since the last beep, an accumulator like footsteps' |
| 80 | /** Where it was when the previous tick ended, for the same reason a player has one. */ | |
| 81 | prev: Vec3; | |
| 72 | 82 | }; |
| 73 | 83 | |
| 74 | 84 | /** A gun lying on the floor: what it is, where it fell, and what was left in it. */ |
| @@ -190,6 +200,22 @@ const BOT_NAMES = [ | ||
| 190 | 200 | |
| 191 | 201 | const emptyAmmo = (id: WeaponId): Ammo => ({ mag: WEAPONS[id].magazine, reserve: WEAPONS[id].reserve }); |
| 192 | 202 | |
| 203 | /** | |
| 204 | * A bomb in a pair of hands at `pos`, every field of it in one place. Its | |
| 205 | * `prev` starts where it is, because a bomb that has never moved has not | |
| 206 | * moved: the renderer must draw it here and not between here and the origin. | |
| 207 | */ | |
| 208 | export const newBomb = (pos: Vec3): Bomb => ({ | |
| 209 | state: "carried", | |
| 210 | pos: [pos[0], pos[1], pos[2]], | |
| 211 | site: null, | |
| 212 | plantProgress: 0, | |
| 213 | defuseProgress: 0, | |
| 214 | defuser: null, | |
| 215 | beepAt: 0, | |
| 216 | prev: [pos[0], pos[1], pos[2]], | |
| 217 | }); | |
| 218 | ||
| 193 | 219 | function makePlayer(id: number, team: Team, name: string, bot: boolean): Player { |
| 194 | 220 | const secondary: WeaponId = team === "t" ? "glock" : "usp"; |
| 195 | 221 | return { |
| @@ -205,6 +231,7 @@ function makePlayer(id: number, team: Team, name: string, bot: boolean): Player | ||
| 205 | 231 | nextShotAt: 0, reloadUntil: 0, shotIndex: 0, lastShotAt: -99, scoped: false, firedThisPress: false, emptyThisPress: false, |
| 206 | 232 | punch: [0, 0], nextPickupAt: 0, |
| 207 | 233 | kills: 0, deaths: 0, headshots: 0, damage: 0, spawnAt: 0, stepDistance: 0, |
| 234 | prev: pose(), | |
| 208 | 235 | }; |
| 209 | 236 | } |
| 210 | 237 | |
| @@ -237,7 +264,7 @@ export function createWorld( | ||
| 237 | 264 | seed: seed >>> 0, |
| 238 | 265 | plan: roundPlan(seed, match.round), |
| 239 | 266 | radio: null, |
| 240 | bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 }, | |
| 267 | bomb: newBomb([0, 0, 0]), | |
| 241 | 268 | pickups: [], grenades: [], bodies: [], |
| 242 | 269 | damageLog: Array.from({ length: players.length + 1 }, () => |
| 243 | 270 | Array.from({ length: players.length }, () => ({ damage: 0, hits: 0, killed: false }))), |
| @@ -315,6 +342,10 @@ export function startRound(world: World): void { | ||
| 315 | 342 | p.nextPickupAt = 0; |
| 316 | 343 | p.yaw = p.team === "t" ? 90 : -90; |
| 317 | 344 | p.pitch = 0; |
| 345 | // A spawn is a teleport, so where they were last tick is where they are | |
| 346 | // now: without this the first frame of the round draws everyone sliding | |
| 347 | // across the map from wherever they died. | |
| 348 | remember(p.prev, p); | |
| 318 | 349 | // The magazines are rebuilt from the two slots rather than topped up in |
| 319 | 350 | // place: a gun taken off a body is in `p.primary`/`p.secondary` and nowhere |
| 320 | 351 | // else, and a gun swapped away is gone from the list the same way. |
| @@ -330,7 +361,7 @@ export function startRound(world: World): void { | ||
| 330 | 361 | const carrier = ts[Math.floor(world.random() * ts.length)]; |
| 331 | 362 | if (carrier !== undefined) { |
| 332 | 363 | carrier.hasBomb = true; |
| 333 | world.bomb = { state: "carried", pos: [...carrier.pos] as Vec3, site: null, plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 }; | |
| 364 | world.bomb = newBomb(carrier.pos); | |
| 334 | 365 | } |
| 335 | 366 | world.bombPlanted = false; |
| 336 | 367 | world.pickups.length = 0; |
| @@ -970,8 +1001,26 @@ export function applyInput(world: World, p: Player, input: Input, dt: number): v | ||
| 970 | 1001 | updatePickups(world, p, input); |
| 971 | 1002 | } |
| 972 | 1003 | |
| 1004 | /** | |
| 1005 | * What everything that moves looked like when the last tick ended, written | |
| 1006 | * into the fields it already has — never a new object, and never a new array. | |
| 1007 | * Ten players and a bomb is forty-four numbers a tick, and it is the whole | |
| 1008 | * price of a frame that can be drawn between two of them. | |
| 1009 | * | |
| 1010 | * It lives in `update` rather than in `applyInput` on purpose: the RL arena | |
| 1011 | * steps players through `applyInput` directly and draws nothing at all, so it | |
| 1012 | * pays nothing for this. Grenades remember themselves in `grenades.ts`, which | |
| 1013 | * is the code that moves them. | |
| 1014 | */ | |
| 1015 | function rememberTick(world: World): void { | |
| 1016 | for (const p of world.players) remember(p.prev, p); | |
| 1017 | rememberPos(world.bomb.prev, world.bomb.pos); | |
| 1018 | } | |
| 1019 | ||
| 973 | 1020 | /** One fixed simulation tick. `input` is the person's; bots think for themselves. */ |
| 974 | 1021 | export function update(world: World, dt: number, input: Input): void { |
| 1022 | // Before anything moves: this tick's "where it was" is last tick's "where it is". | |
| 1023 | rememberTick(world); | |
| 975 | 1024 | world.time += dt; |
| 976 | 1025 | const before = world.match.phase; |
| 977 | 1026 | for (const event of rules.tick(world.match, dt)) { |
new above
offline