Split The code is public; the app answers only to the crew.
The body stays where it falls: a corpse until the round ends, and a camera that is not inside it #32
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/28-the-body-stays-where-it-falls-a-corpse-u → main 1 commit fork
10 files
changed +391
−19
README.md +10 −3
| @@ -108,7 +108,11 @@ be sitting on the radar, the health or the ammo. | ||
| 108 | 108 | When you die the camera moves to a teammate who is still up and the HUD becomes |
| 109 | 109 | theirs — their health, armour, ammo and gun, no crosshair — over a line naming |
| 110 | 110 | who killed you and with what. Clicking, or the fire key, moves on to the next |
| 111 | teammate. The round's result stays on the screen through the freeze time, and | |
| 111 | teammate. Whoever dies stays on the floor where they fell, lying the way they | |
| 112 | were facing and tinted their side, until the next round starts — so the rifle | |
| 113 | dropped in a doorway has a body under it and a fight leaves a record of itself. | |
| 114 | A body is decoration and nothing more: bullets go through it, it blocks nobody, | |
| 115 | and no bot has ever seen one. The round's result stays on the screen through the freeze time, and | |
| 112 | 116 | when the match ends a card says who won, the final score from your side and |
| 113 | 117 | what you did, with a button to play again and one back to the menu. |
| 114 | 118 | |
| @@ -130,11 +134,14 @@ with the canvas as you turn; nobody re-walks the grid to draw it. | ||
| 130 | 134 | `physics`, `weapons`, `combat`, `match` (the rules), `world` (the simulation), |
| 131 | 135 | `bots`, `observe`, `action`, `policy`, `weights`, `models`, `textures`, |
| 132 | 136 | `press` (the input latch), `quality` (the render scale the frame time buys), |
| 133 | `spectate` (whose eyes you borrow when you are dead), `radar` (the blips the | |
| 137 | `spectate` (whose eyes you borrow when you are dead), `bodies` (the dead of | |
| 138 | this round, where they fell), `radar` (the blips the | |
| 134 | 139 | HUD's minimap draws, and nothing it draws them with), `summary` (the end of |
| 135 | 140 | the match as numbers), `ears` (a sound's pan and volume, as plain data), |
| 136 | 141 | `renderer`, `hud`, `input`, `audio`, `main`. |
| 137 | Everything except the last five is headless and tested. | |
| 142 | Everything except the last five is headless and tested; `renderer` and `hud` | |
| 143 | are checked against recording contexts, which is how a frame's draws are | |
| 144 | counted without a GPU. | |
| 138 | 145 | - `src/rl/` — training, which the game never imports: `arena` is the environment |
| 139 | 146 | (a real duel on de_forge with the round and the economy taken out), `ppo` is |
| 140 | 147 | the algorithm, with its backward pass checked against finite differences, and |
src/client/bodies.test.ts added +113
| @@ -0,0 +1,113 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { addBody, MAX_BODIES, type Corpse } from "./bodies.ts"; | |
| 3 | import { traceBullet } from "./combat.ts"; | |
| 4 | import { SHORT } from "./match.ts"; | |
| 5 | import { move, MOVE, type Body, type Wish } from "./physics.ts"; | |
| 6 | import { | |
| 7 | aimAt, bodyCentre, createWorld, shoot, startRound, type Player, type World, | |
| 8 | } from "./world.ts"; | |
| 9 | ||
| 10 | /** Put a shooter in front of the person on an empty map, rifle up and aimed. */ | |
| 11 | function faceOff(world: World, shooter: Player, apart = 200): void { | |
| 12 | world.boxes = []; | |
| 13 | world.match.phase = "live"; | |
| 14 | for (const p of world.players) if (p !== shooter && p !== world.me) p.alive = false; | |
| 15 | world.bodies.length = 0; | |
| 16 | world.me.pos = [0, 0, 0]; | |
| 17 | world.me.vel = [0, 0, 0]; | |
| 18 | world.me.yaw = 180; | |
| 19 | shooter.pos = [apart, 0, 0]; | |
| 20 | shooter.vel = [0, 0, 0]; | |
| 21 | shooter.onGround = true; | |
| 22 | shooter.primary = "ak47"; | |
| 23 | shooter.weapon = "ak47"; | |
| 24 | shooter.ammo.ak47 = { mag: 30, reserve: 90 }; | |
| 25 | shooter.nextShotAt = 0; | |
| 26 | shooter.reloadUntil = 0; | |
| 27 | shooter.shotIndex = 0; | |
| 28 | const aim = aimAt(shooter, bodyCentre(world.me)); | |
| 29 | shooter.yaw = aim.yaw; | |
| 30 | shooter.pitch = aim.pitch; | |
| 31 | world.effects.length = 0; | |
| 32 | } | |
| 33 | ||
| 34 | /** Kill the person where they stand, and hand back the shooter. */ | |
| 35 | function killMe(world: World): Player { | |
| 36 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 37 | faceOff(world, shooter); | |
| 38 | world.me.health = 1; | |
| 39 | world.me.armor = 0; | |
| 40 | expect(shoot(world, shooter)).toBe(true); | |
| 41 | expect(world.me.alive).toBe(false); | |
| 42 | return shooter; | |
| 43 | } | |
| 44 | ||
| 45 | test("a kill leaves one body where and how the player fell", () => { | |
| 46 | const world = createWorld(SHORT, "me", "ct", 5, 211); | |
| 47 | killMe(world); | |
| 48 | expect(world.bodies.length).toBe(1); | |
| 49 | const body = world.bodies[0] as Corpse; | |
| 50 | expect(body.id).toBe(world.me.id); | |
| 51 | expect(body.team).toBe("ct"); | |
| 52 | expect(body.yaw).toBe(world.me.yaw); | |
| 53 | expect(body.at).toBe(world.time); | |
| 54 | expect(body.pos).toEqual([0, 0, 0]); | |
| 55 | // A copy, not the player: the corpse does not follow them to the next round. | |
| 56 | expect(body.pos).not.toBe(world.me.pos); | |
| 57 | }); | |
| 58 | ||
| 59 | test("the next round clears the bodies", () => { | |
| 60 | const world = createWorld(SHORT, "me", "ct", 5, 223); | |
| 61 | killMe(world); | |
| 62 | expect(world.bodies.length).toBe(1); | |
| 63 | startRound(world); | |
| 64 | expect(world.bodies.length).toBe(0); | |
| 65 | expect(world.me.alive).toBe(true); | |
| 66 | }); | |
| 67 | ||
| 68 | test("the list of bodies is bounded however long the world is stepped", () => { | |
| 69 | const world = createWorld(SHORT, "me", "ct", 5, 227); | |
| 70 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 71 | faceOff(world, shooter); | |
| 72 | for (let i = 0; i < 400; i++) { | |
| 73 | world.me.alive = true; | |
| 74 | world.me.health = 1; | |
| 75 | world.me.armor = 0; | |
| 76 | shooter.nextShotAt = 0; | |
| 77 | shooter.shotIndex = 0; // the first bullet of a spray, every time: it lands | |
| 78 | shooter.ammo.ak47 = { mag: 30, reserve: 90 }; | |
| 79 | world.time += 1; | |
| 80 | shoot(world, shooter); | |
| 81 | } | |
| 82 | expect(world.bodies.length).toBe(MAX_BODIES); | |
| 83 | // The oldest went first: what is left is the last MAX_BODIES kills. | |
| 84 | expect((world.bodies[0] as Corpse).at).toBeLessThan((world.bodies[MAX_BODIES - 1] as Corpse).at); | |
| 85 | }); | |
| 86 | ||
| 87 | test("someone killed in the air is put down on the floor under them", () => { | |
| 88 | const bodies: Corpse[] = []; | |
| 89 | const boxes = [{ min: [-200, -200, -64] as [number, number, number], max: [200, 200, 0] as [number, number, number], mat: 0 }]; | |
| 90 | addBody(bodies, { id: 3, team: "t", pos: [0, 0, 180], yaw: 45 }, boxes, 9); | |
| 91 | expect((bodies[0] as Corpse).pos).toEqual([0, 0, 0]); | |
| 92 | // Nothing under them at all — a hole in the world — leaves them where they died. | |
| 93 | addBody(bodies, { id: 4, team: "t", pos: [0, 0, 180], yaw: 0 }, [], 9); | |
| 94 | expect((bodies[1] as Corpse).pos[2]).toBe(180); | |
| 95 | }); | |
| 96 | ||
| 97 | test("a body is decoration: nobody collides with it and no bullet stops on it", () => { | |
| 98 | const world = createWorld(SHORT, "me", "ct", 5, 229); | |
| 99 | const shooter = killMe(world); | |
| 100 | expect(world.bodies.length).toBe(1); | |
| 101 | // `move` only ever collides with `world.boxes`, and a body is never one: the | |
| 102 | // only box in this world is the floor the body is lying on. | |
| 103 | world.boxes = [{ min: [-400, -400, -64], max: [400, 400, 0], mat: 0 }]; | |
| 104 | expect(world.boxes.length).toBe(1); | |
| 105 | const walker: Body = { pos: [0, -100, 0], vel: [0, 0, 0], onGround: true, ducking: false, duckAmount: 0 }; | |
| 106 | const wish: Wish = { forward: 1, side: 0, yaw: 90, jump: false, duck: false, walk: false, maxSpeed: MOVE.maxSpeed }; | |
| 107 | for (let i = 0; i < 128; i++) move(walker, wish, 1 / 64, world.boxes); | |
| 108 | expect(walker.pos[1]).toBeGreaterThan(0); // straight over where the body lies | |
| 109 | ||
| 110 | // A bullet along the same line finds nothing: only living players are actors. | |
| 111 | const hit = traceBullet([0, -300, 20], [0, 300, 20], world.boxes, world.players, shooter.id); | |
| 112 | expect(hit.kind).toBe("none"); | |
| 113 | }); |
src/client/bodies.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** | |
| 2 | * The dead, left where they fell. A body is a copy of four numbers taken at the | |
| 3 | * moment of the kill — never a reference to the player, who stands up again at | |
| 4 | * the next `startRound` — and it is decoration and nothing else: bullets go | |
| 5 | * through it, `move` never collides with it, and no bot has ever seen one. | |
| 6 | * | |
| 7 | * It lives in `world.bodies` rather than `world.effects` because the effects | |
| 8 | * list is trimmed to the last two seconds and a body lasts the round. | |
| 9 | */ | |
| 10 | ||
| 11 | import type { Box } from "./map.ts"; | |
| 12 | import type { Team } from "./match.ts"; | |
| 13 | import type { Vec3 } from "./math.ts"; | |
| 14 | import { HULL, groundUnder } from "./physics.ts"; | |
| 15 | ||
| 16 | export type Corpse = { | |
| 17 | id: number; // the player who fell, so the HUD or a camera could find them | |
| 18 | team: Team; // the tint, kept here because half time swaps the player's side | |
| 19 | pos: Vec3; // the feet, on the floor under where they died | |
| 20 | yaw: number; // the way they were facing: the body lies along it | |
| 21 | at: number; // world time of the kill | |
| 22 | }; | |
| 23 | ||
| 24 | /** | |
| 25 | * Ten a side is the ceiling for a round, but a world can be stepped for as long | |
| 26 | * as anyone likes, so the list has a hard end and the oldest body goes first. | |
| 27 | */ | |
| 28 | export const MAX_BODIES = 20; | |
| 29 | ||
| 30 | /** How far below a player killed in the air the floor is looked for. */ | |
| 31 | export const FALL_REACH = 512; | |
| 32 | ||
| 33 | /** What a body needs from the player it was; every field is copied out. */ | |
| 34 | export type Fallen = { id: number; team: Team; pos: Vec3; yaw: number }; | |
| 35 | ||
| 36 | /** | |
| 37 | * Add the body of `p` to `bodies`. Someone shot mid-jump is put down on the | |
| 38 | * floor under them — the only work here, and it happens once per kill, never | |
| 39 | * in a tick where nobody dies. | |
| 40 | */ | |
| 41 | export function addBody(bodies: Corpse[], p: Fallen, boxes: Box[], time: number): Corpse { | |
| 42 | const floor = groundUnder(p.pos, HULL.stand, boxes, FALL_REACH); | |
| 43 | const body: Corpse = { | |
| 44 | id: p.id, | |
| 45 | team: p.team, | |
| 46 | pos: [p.pos[0], p.pos[1], floor ?? p.pos[2]], | |
| 47 | yaw: p.yaw, | |
| 48 | at: time, | |
| 49 | }; | |
| 50 | bodies.push(body); | |
| 51 | if (bodies.length > MAX_BODIES) bodies.shift(); | |
| 52 | return body; | |
| 53 | } |
src/client/main.ts +1 −1
| @@ -339,7 +339,7 @@ function boot(): void { | ||
| 339 | 339 | renderer.setScale(next); |
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | renderer.draw(world, camera, elapsed); | |
| 342 | renderer.draw(world, camera, view, elapsed); | |
| 343 | 343 | |
| 344 | 344 | // The HUD keeps the full device ratio whatever the scene costs: text that |
| 345 | 345 | // goes soft reads as a broken page, and 2D text is not what is slow. |
src/client/math.test.ts +26 −1
| @@ -1,5 +1,12 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | import { angleDelta, angleVectors, cross, dot, mat4, multiply, normalize, perspective, rng, viewMatrix } from "./math.ts"; | |
| 2 | import { | |
| 3 | angleDelta, angleVectors, cross, dot, fallenMatrix, mat4, multiply, normalize, perspective, rng, | |
| 4 | viewMatrix, type Mat4, type Vec3, | |
| 5 | } from "./math.ts"; | |
| 6 | ||
| 7 | /** A point through a model matrix, the way the vertex shader would take it. */ | |
| 8 | const through = (m: Mat4, p: Vec3): Vec3 => | |
| 9 | [0, 1, 2].map((r) => m[r] * p[0] + m[4 + r] * p[1] + m[8 + r] * p[2] + m[12 + r]) as unknown as Vec3; | |
| 3 | 10 | |
| 4 | 11 | test("yaw 0 looks east, yaw 90 looks north", () => { |
| 5 | 12 | const east = angleVectors(0, 0).forward; |
| @@ -52,6 +59,24 @@ test("multiplying by identity changes nothing", () => { | ||
| 52 | 59 | for (let k = 0; k < 16; k++) expect(m[k]).toBeCloseTo(a[k], 5); |
| 53 | 60 | }); |
| 54 | 61 | |
| 62 | test("a tipped model lies along the way it was facing, flattened onto the floor", () => { | |
| 63 | // The head of a 72-unit player, tipped face down at yaw 90 (north) over a | |
| 64 | // body standing at the origin: it ends up 72 units north and on the floor. | |
| 65 | const head = through(fallenMatrix([0, 0, 7], 90, 90, 0.75), [0, 0, 72]); | |
| 66 | expect(head[0]).toBeCloseTo(0, 4); | |
| 67 | expect(head[1]).toBeCloseTo(72, 4); | |
| 68 | expect(head[2]).toBeCloseTo(7, 4); | |
| 69 | // Its own thickness is what gets flattened, and the feet stay at the pivot. | |
| 70 | const back = through(fallenMatrix([0, 0, 7], 90, 90, 0.75), [8, 0, 0]); | |
| 71 | expect(back[2]).toBeCloseTo(7 - 6, 4); | |
| 72 | expect(through(fallenMatrix([3, 4, 7], 37, 90, 1), [0, 0, 0])).toEqual([3, 4, 7]); | |
| 73 | // A tip of nothing is the plain standing transform again. | |
| 74 | const upright = through(fallenMatrix([0, 0, 0], 90, 0, 1), [10, 0, 5]); | |
| 75 | expect(upright[0]).toBeCloseTo(0, 4); | |
| 76 | expect(upright[1]).toBeCloseTo(10, 4); | |
| 77 | expect(upright[2]).toBeCloseTo(5, 4); | |
| 78 | }); | |
| 79 | ||
| 55 | 80 | test("normalize survives a zero vector", () => { |
| 56 | 81 | expect(normalize([0, 0, 0])).toEqual([0, 0, 0]); |
| 57 | 82 | }); |
src/client/math.ts +18
| @@ -111,6 +111,24 @@ export function modelMatrix(pos: Vec3, yaw: number, sx = 1, sy = 1, sz = 1): Mat | ||
| 111 | 111 | return m; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | /** | |
| 115 | * A model tipped over onto its face: translate · yaw · tip about the model's | |
| 116 | * own Y axis, with `flatten` squashing what is left of it against the world's | |
| 117 | * Z afterwards. A mesh built standing at the origin facing +X lies along the | |
| 118 | * way it was facing at a tip of 90°, which is how a body ends up on the floor. | |
| 119 | */ | |
| 120 | export function fallenMatrix(pos: Vec3, yaw: number, tip: number, flatten = 1): Mat4 { | |
| 121 | const c = Math.cos(rad(yaw)), s = Math.sin(rad(yaw)); | |
| 122 | const ct = Math.cos(rad(tip)), st = Math.sin(rad(tip)); | |
| 123 | const m = new Float32Array(16); | |
| 124 | m[0] = c * ct; m[1] = s * ct; m[2] = -st * flatten; | |
| 125 | m[4] = -s; m[5] = c; m[6] = 0; | |
| 126 | m[8] = c * st; m[9] = s * st; m[10] = ct * flatten; | |
| 127 | m[12] = pos[0]; m[13] = pos[1]; m[14] = pos[2]; | |
| 128 | m[15] = 1; | |
| 129 | return m; | |
| 130 | } | |
| 131 | ||
| 114 | 132 | /** |
| 115 | 133 | * A deterministic random source. Every spray pattern and every bot decision |
| 116 | 134 | * draws from one of these, so a seed reproduces a round exactly — which is |
src/client/renderer.test.ts added +110
| @@ -0,0 +1,110 @@ | ||
| 1 | /** | |
| 2 | * The renderer against a recording WebGL2 context: no browser, no GPU, just | |
| 3 | * the calls it would have made. What can be checked here is the shape of a | |
| 4 | * frame — which player is skipped, how many draws a body costs, and that a | |
| 5 | * body costs nothing at all in a round where nobody died. | |
| 6 | */ | |
| 7 | ||
| 8 | import { expect, test } from "bun:test"; | |
| 9 | import { addBody } from "./bodies.ts"; | |
| 10 | import { SHORT } from "./match.ts"; | |
| 11 | import { createRenderer, type Camera } from "./renderer.ts"; | |
| 12 | import { createWorld, type Player, type World } from "./world.ts"; | |
| 13 | ||
| 14 | type Call = { name: string; args: unknown[] }; | |
| 15 | ||
| 16 | function recorder(): { canvas: HTMLCanvasElement; calls: Call[] } { | |
| 17 | const calls: Call[] = []; | |
| 18 | const log = (name: string, result: unknown = undefined) => | |
| 19 | (...args: unknown[]): unknown => { | |
| 20 | calls.push({ name, args }); | |
| 21 | return result; | |
| 22 | }; | |
| 23 | const handle = (name: string) => log(name, { name }); | |
| 24 | const methods: Record<string, unknown> = { | |
| 25 | createShader: handle("shader"), shaderSource: log("shaderSource"), compileShader: log("compileShader"), | |
| 26 | getShaderParameter: log("getShaderParameter", true), getShaderInfoLog: log("getShaderInfoLog", ""), | |
| 27 | createProgram: handle("program"), attachShader: log("attachShader"), linkProgram: log("linkProgram"), | |
| 28 | getProgramParameter: log("getProgramParameter", true), getProgramInfoLog: log("getProgramInfoLog", ""), | |
| 29 | getUniformLocation: handle("uniform"), | |
| 30 | createTexture: handle("texture"), bindTexture: log("bindTexture"), texImage3D: log("texImage3D"), | |
| 31 | generateMipmap: log("generateMipmap"), texParameteri: log("texParameteri"), | |
| 32 | createVertexArray: handle("vao"), bindVertexArray: log("bindVertexArray"), | |
| 33 | createBuffer: handle("buffer"), bindBuffer: log("bindBuffer"), | |
| 34 | bufferData: log("bufferData"), bufferSubData: log("bufferSubData"), | |
| 35 | enableVertexAttribArray: log("enableVertexAttribArray"), vertexAttribPointer: log("vertexAttribPointer"), | |
| 36 | enable: log("enable"), cullFace: log("cullFace"), clearColor: log("clearColor"), | |
| 37 | viewport: log("viewport"), clear: log("clear"), useProgram: log("useProgram"), | |
| 38 | activeTexture: log("activeTexture"), uniform1i: log("uniform1i"), | |
| 39 | uniformMatrix4fv: log("uniformMatrix4fv"), uniform3fv: log("uniform3fv"), uniform3f: log("uniform3f"), | |
| 40 | drawElements: log("drawElements"), drawArrays: log("drawArrays"), | |
| 41 | }; | |
| 42 | const gl = new Proxy(methods, { | |
| 43 | // Anything the renderer reads that is not a method is a GL constant, and | |
| 44 | // nothing in a frame compares two of them. | |
| 45 | get: (target, prop) => | |
| 46 | typeof prop === "string" ? target[prop] ?? prop.length + 0x1000 : undefined, | |
| 47 | }); | |
| 48 | const canvas = { | |
| 49 | clientWidth: 1280, clientHeight: 720, width: 0, height: 0, | |
| 50 | getContext: () => gl, | |
| 51 | }; | |
| 52 | return { canvas: canvas as unknown as HTMLCanvasElement, calls }; | |
| 53 | } | |
| 54 | ||
| 55 | const CAMERA: Camera = { pos: [0, 0, 64], yaw: 0, pitch: 0, fov: 90 }; | |
| 56 | ||
| 57 | /** One frame, and the draws it made. */ | |
| 58 | function frame(canvas: HTMLCanvasElement, calls: Call[], world: World, view: Player): Call[] { | |
| 59 | const renderer = renderers.get(canvas) ?? createRenderer(canvas); | |
| 60 | renderers.set(canvas, renderer); | |
| 61 | calls.length = 0; | |
| 62 | (renderer as { draw: (w: World, c: Camera, v: Player, dt: number) => void }).draw(world, CAMERA, view, 1 / 60); | |
| 63 | return calls.filter((c) => c.name === "drawElements" || c.name === "drawArrays"); | |
| 64 | } | |
| 65 | const renderers = new Map<HTMLCanvasElement, ReturnType<typeof createRenderer>>(); | |
| 66 | ||
| 67 | /** A world stood still: everyone alive, nothing on the floor, no effects. */ | |
| 68 | function scene(): World { | |
| 69 | const world = createWorld(SHORT, "me", "ct", 5, 401); | |
| 70 | world.effects.length = 0; | |
| 71 | world.pickups.length = 0; | |
| 72 | return world; | |
| 73 | } | |
| 74 | ||
| 75 | // `window.devicePixelRatio` is all the renderer asks of the page. | |
| 76 | (globalThis as { window?: unknown }).window = { devicePixelRatio: 1 }; | |
| 77 | ||
| 78 | test("the player the camera is inside is the one that is not drawn", () => { | |
| 79 | const { canvas, calls } = recorder(); | |
| 80 | const world = scene(); | |
| 81 | const mate = world.players.find((p) => p.team === "ct" && p !== world.me) as Player; | |
| 82 | const mine = frame(canvas, calls, world, world.me).length; | |
| 83 | // Spectating a teammate draws the person's own mesh instead of theirs: the | |
| 84 | // same number of players, a different one skipped. | |
| 85 | const theirs = frame(canvas, calls, world, mate).length; | |
| 86 | expect(theirs).toBe(mine); | |
| 87 | ||
| 88 | // With the person dead the count drops by one, and it is the watched | |
| 89 | // teammate who is missing — the camera is not inside anybody else. | |
| 90 | world.me.alive = false; | |
| 91 | expect(frame(canvas, calls, world, mate).length).toBe(mine - 1); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a body is one more draw of the same mesh, and no body is no cost at all", () => { | |
| 95 | const { canvas, calls } = recorder(); | |
| 96 | const world = scene(); | |
| 97 | const quiet = frame(canvas, calls, world, world.me).length; | |
| 98 | for (let i = 0; i < 10; i++) { | |
| 99 | addBody(world.bodies, { id: 20 + i, team: i % 2 === 0 ? "t" : "ct", pos: [i * 64, 0, 0], yaw: i * 36 }, world.boxes, world.time); | |
| 100 | } | |
| 101 | expect(world.bodies.length).toBe(10); | |
| 102 | const withBodies = frame(canvas, calls, world, world.me); | |
| 103 | expect(withBodies.length).toBe(quiet + 10); | |
| 104 | // Every one of them is the player mesh already uploaded: one indexed draw, | |
| 105 | // no second program and no texture of its own. | |
| 106 | const programs = calls.filter((c) => c.name === "useProgram").length; | |
| 107 | const uploads = calls.filter((c) => c.name === "texImage3D" || c.name === "bufferData").length; | |
| 108 | expect(programs).toBeLessThanOrEqual(2); | |
| 109 | expect(uploads).toBe(0); | |
| 110 | }); |
src/client/renderer.ts +50 −13
| @@ -5,11 +5,11 @@ | ||
| 5 | 5 | * the frame loop except the numbers that change. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { multiply, perspective, viewMatrix, modelMatrix, type Vec3 } from "./math.ts"; | |
| 8 | import { fallenMatrix, multiply, perspective, viewMatrix, modelMatrix, type Vec3 } from "./math.ts"; | |
| 9 | 9 | import { bombMesh, levelMesh, pickupMesh, playerMesh, viewModelMesh, type Mesh } from "./models.ts"; |
| 10 | 10 | import { MAX_SCALE, MIN_SCALE } from "./quality.ts"; |
| 11 | 11 | import { LAYERS, TEX_SIZE, materialTextures } from "./textures.ts"; |
| 12 | import type { World } from "./world.ts"; | |
| 12 | import type { Player, World } from "./world.ts"; | |
| 13 | 13 | import { hullHeight } from "./physics.ts"; |
| 14 | 14 | import { WEAPONS } from "./weapons.ts"; |
| 15 | 15 | |
| @@ -128,10 +128,28 @@ const TEAM_TINT: Record<string, [number, number, number]> = { | ||
| 128 | 128 | ct: [0.72, 0.86, 1.2], |
| 129 | 129 | }; |
| 130 | 130 | |
| 131 | /** | |
| 132 | * A body, from the player mesh: tipped onto its face so it lies along the way | |
| 133 | * it was facing, pressed a little flatter than a standing player is deep, and | |
| 134 | * lifted by what is left of that so it rests on the floor instead of in it. | |
| 135 | * Team tint again, dimmed — a corpse should read as the side it was on and | |
| 136 | * never as somebody still in the round. | |
| 137 | * | |
| 138 | * It gets there over `BODY_FALL` seconds, from exactly where the living mesh | |
| 139 | * stood: a player who is drawn one frame and a body the next has to topple, or | |
| 140 | * the kill reads as a disappearance all over again. | |
| 141 | */ | |
| 142 | const BODY_TIP = 90; | |
| 143 | const BODY_FLATTEN = 0.75; | |
| 144 | const BODY_LIFT = 7; | |
| 145 | const BODY_SHADE = 0.66; | |
| 146 | const BODY_FALL = 0.4; | |
| 147 | ||
| 131 | 148 | const LINE_CAPACITY = 4096; // vertices |
| 132 | 149 | |
| 133 | 150 | export type Renderer = { |
| 134 | draw(world: World, camera: Camera, dt: number): void; | |
| 151 | /** `view` is the player the camera is inside: their own mesh is not drawn. */ | |
| 152 | draw(world: World, camera: Camera, view: Player, dt: number): void; | |
| 135 | 153 | resize(): void; |
| 136 | 154 | /** |
| 137 | 155 | * The fraction of the device pixel ratio the drawing buffer uses. `quality` |
| @@ -215,7 +233,7 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 215 | 233 | |
| 216 | 234 | let bobPhase = 0; |
| 217 | 235 | |
| 218 | const draw = (world: World, camera: Camera, dt: number): void => { | |
| 236 | const draw = (world: World, camera: Camera, view: Player, dt: number): void => { | |
| 219 | 237 | resize(); |
| 220 | 238 | const width = canvas.width, height = canvas.height; |
| 221 | 239 | gl.viewport(0, 0, width, height); |
| @@ -226,8 +244,8 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 226 | 244 | // A CS field of view is horizontal; the projection wants the vertical one. |
| 227 | 245 | const fovY = (2 * Math.atan(Math.tan((camera.fov * Math.PI) / 360) / aspect) * 180) / Math.PI; |
| 228 | 246 | const proj = perspective(fovY, aspect, 6, 12000); |
| 229 | const view = viewMatrix(camera.pos, camera.yaw, camera.pitch); | |
| 230 | const viewProj = multiply(proj, view); | |
| 247 | const look = viewMatrix(camera.pos, camera.yaw, camera.pitch); | |
| 248 | const viewProj = multiply(proj, look); | |
| 231 | 249 | |
| 232 | 250 | if (levelBuffers === null) levelBuffers = upload(gl, levelMesh(world.boxes)); |
| 233 | 251 | |
| @@ -245,9 +263,11 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 245 | 263 | gl.bindVertexArray(levelBuffers.vao); |
| 246 | 264 | gl.drawElements(gl.TRIANGLES, levelBuffers.count, gl.UNSIGNED_INT, 0); |
| 247 | 265 | |
| 266 | // The player the camera is inside is the one we skip — while dead that is | |
| 267 | // the teammate being watched, not the person, who is now a body below. | |
| 248 | 268 | gl.bindVertexArray(player.vao); |
| 249 | 269 | for (const p of world.players) { |
| 250 | if (!p.alive || p === world.me) continue; | |
| 270 | if (!p.alive || p === view) continue; | |
| 251 | 271 | const squash = hullHeight(p) / 72; |
| 252 | 272 | gl.uniformMatrix4fv(u.model, false, modelMatrix(p.pos, p.yaw, 1, 1, squash)); |
| 253 | 273 | const tint = TEAM_TINT[p.team] ?? [1, 1, 1]; |
| @@ -255,6 +275,23 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 255 | 275 | gl.drawElements(gl.TRIANGLES, player.count, gl.UNSIGNED_INT, 0); |
| 256 | 276 | } |
| 257 | 277 | |
| 278 | // The dead of this round, on the same mesh and the same buffer: one draw | |
| 279 | // each and not a byte of state beyond the matrix and the tint. | |
| 280 | for (const body of world.bodies) { | |
| 281 | const t = Math.min(1, Math.max(0, (world.time - body.at) / BODY_FALL)); | |
| 282 | const fall = t * t * (3 - 2 * t); // smoothstep: it goes over and settles | |
| 283 | const place = fallenMatrix( | |
| 284 | [body.pos[0], body.pos[1], body.pos[2] + BODY_LIFT * fall], | |
| 285 | body.yaw, | |
| 286 | BODY_TIP * fall, | |
| 287 | 1 + (BODY_FLATTEN - 1) * fall, | |
| 288 | ); | |
| 289 | gl.uniformMatrix4fv(u.model, false, place); | |
| 290 | const tint = TEAM_TINT[body.team] ?? [1, 1, 1]; | |
| 291 | gl.uniform3f(u.tint, tint[0] * BODY_SHADE, tint[1] * BODY_SHADE, tint[2] * BODY_SHADE); | |
| 292 | gl.drawElements(gl.TRIANGLES, player.count, gl.UNSIGNED_INT, 0); | |
| 293 | } | |
| 294 | ||
| 258 | 295 | // Guns their owners are not carrying any more, turning slowly on the spot |
| 259 | 296 | // so a rifle in a doorway reads as something to take and not as rubble. |
| 260 | 297 | if (world.pickups.length > 0) { |
| @@ -320,15 +357,15 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 320 | 357 | gl.drawArrays(gl.LINES, 0, n - (n % 2)); |
| 321 | 358 | } |
| 322 | 359 | |
| 323 | // --- the gun in your hands, on its own projection and its own depth | |
| 324 | const me = world.me; | |
| 325 | if (me.alive && !me.scoped) { | |
| 326 | const speed = Math.hypot(me.vel[0], me.vel[1]); | |
| 360 | // --- the gun in your hands, on its own projection and its own depth. It is | |
| 361 | // the gun of whoever the camera is inside: while dead, the teammate's. | |
| 362 | if (view.alive && !view.scoped) { | |
| 363 | const speed = Math.hypot(view.vel[0], view.vel[1]); | |
| 327 | 364 | bobPhase += dt * (4 + speed * 0.02); |
| 328 | 365 | const bobX = Math.sin(bobPhase) * Math.min(speed / 250, 1) * 0.05; |
| 329 | 366 | const bobY = Math.abs(Math.cos(bobPhase)) * Math.min(speed / 250, 1) * 0.04; |
| 330 | const kick = Math.max(0, 0.12 - (world.time - me.lastShotAt)) * 2.2; | |
| 331 | const kind = WEAPONS[me.weapon]?.kind ?? "rifle"; | |
| 367 | const kick = Math.max(0, 0.12 - (world.time - view.lastShotAt)) * 2.2; | |
| 368 | const kind = WEAPONS[view.weapon]?.kind ?? "rifle"; | |
| 332 | 369 | let model = viewModels.get(kind); |
| 333 | 370 | if (model === undefined) { |
| 334 | 371 | model = upload(gl, viewModelMesh(kind)); |
src/client/world.ts +7 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * WebGL, so it can be stepped in a test without a browser. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { addBody, type Corpse } from "./bodies.ts"; | |
| 10 | 11 | import { applyHit, traceBullet, type Actor } from "./combat.ts"; |
| 11 | 12 | import * as map from "./map.ts"; |
| 12 | 13 | import { angleVectors, angleDelta, clamp, dist, dist2d, normalize, rng, sub, type Vec3 } from "./math.ts"; |
| @@ -116,6 +117,7 @@ export type World = { | ||
| 116 | 117 | time: number; |
| 117 | 118 | bomb: Bomb; |
| 118 | 119 | pickups: Pickup[]; |
| 120 | bodies: Corpse[]; // the dead of this round, cleared when the next one starts | |
| 119 | 121 | effects: Effect[]; |
| 120 | 122 | feed: FeedEntry[]; |
| 121 | 123 | messages: { at: number; text: string }[]; |
| @@ -174,7 +176,7 @@ export function createWorld( | ||
| 174 | 176 | const world: World = { |
| 175 | 177 | match, players, me, time: 0, |
| 176 | 178 | bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null }, |
| 177 | pickups: [], | |
| 179 | pickups: [], bodies: [], | |
| 178 | 180 | effects: [], feed: [], messages: [], |
| 179 | 181 | random, boxes: map.boxes, bombPlanted: false, pendingBuy: false, |
| 180 | 182 | }; |
| @@ -230,6 +232,7 @@ export function startRound(world: World): void { | ||
| 230 | 232 | } |
| 231 | 233 | world.bombPlanted = false; |
| 232 | 234 | world.pickups.length = 0; |
| 235 | world.bodies.length = 0; | |
| 233 | 236 | world.feed = []; |
| 234 | 237 | for (const p of world.players) if (p.bot) botBuy(world, p); |
| 235 | 238 | } |
| @@ -424,6 +427,7 @@ function onKill(world: World, killer: Player, victim: Player, weapon: WeaponId, | ||
| 424 | 427 | victim.alive = false; |
| 425 | 428 | victim.deaths += 1; |
| 426 | 429 | victim.scoped = false; |
| 430 | addBody(world.bodies, victim, world.boxes, world.time); | |
| 427 | 431 | killer.kills += 1; |
| 428 | 432 | if (headshot) killer.headshots += 1; |
| 429 | 433 | killer.money = rules.capMoney(killer.money + WEAPONS[weapon].killReward, world.match.rules); |
| @@ -680,6 +684,8 @@ export function update(world: World, dt: number, input: Input): void { | ||
| 680 | 684 | p.alive = false; |
| 681 | 685 | p.health = 0; |
| 682 | 686 | p.deaths += 1; |
| 687 | // The bomb kills nobody by name, but it still leaves them lying there. | |
| 688 | addBody(world.bodies, p, world.boxes, world.time); | |
| 683 | 689 | } |
| 684 | 690 | } |
| 685 | 691 | } |
src/rl/arena.ts +3
| @@ -170,6 +170,9 @@ export function reset(arena: Arena): void { | ||
| 170 | 170 | // A duel that ended in a kill left a rifle on the floor. Nothing here wants |
| 171 | 171 | // it, and an empty list is what keeps the pickup test in applyInput free. |
| 172 | 172 | world.pickups.length = 0; |
| 173 | // And a body. Nothing here draws one, and the list would otherwise carry the | |
| 174 | // last twenty duels' dead through every episode that follows. | |
| 175 | world.bodies.length = 0; | |
| 173 | 176 | arena.ticks = 0; |
| 174 | 177 | |
| 175 | 178 | equip(learner, "m4a4"); |
new above
offline