Split The code is public; the app answers only to the crew.

A radar in the corner: the map, your team, the sites and the bomb #23

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/cs:scout/18-a-radar-in-the-corner-the-map-your-team → main 2 commits fork

  • Merged. scout/18-a-radar-in-the-corner-the-map-your-team was merged into main as b27ea9a.

Review

Sign in to review this change.
7 files changed +356 −70
README.md +8 −1
@@ -88,13 +88,20 @@ device pixel ratio at the worst, stepped at most twice a second, the HUD
8888 text still at full resolution — and the counter says what fraction of the
8989 pixels you are getting. Nothing on the page moves when it changes.
9090
91 The radar in the bottom-left corner is your view, not the room: rotated so
92 the way you are facing is up, it shows your living teammates, the bomb once
93 it is planted, and both sites marked A and B — never the other team, because
94 a radar that shows the enemy is a wallhack. Its map is baked once and turned
95 with the canvas as you turn; nobody re-walks the grid to draw it.
96
9197 ## Shape
9298
9399 - `src/client/` — the game, in modules named for one thing each: `math`, `map`,
94100 `physics`, `weapons`, `combat`, `match` (the rules), `world` (the simulation),
95101 `bots`, `observe`, `action`, `policy`, `weights`, `models`, `textures`,
96102 `press` (the input latch), `quality` (the render scale the frame time buys),
97 `spectate` (whose eyes you borrow when you are dead), `summary` (the end of
103 `spectate` (whose eyes you borrow when you are dead), `radar` (the blips the
104 HUD's minimap draws, and nothing it draws them with), `summary` (the end of
98105 the match as numbers), `ears` (a sound's pan and volume, as plain data),
99106 `renderer`, `hud`, `input`, `audio`, `main`.
100107 Everything except the last five is headless and tested.
src/client/hud.test.ts +32 −6
@@ -26,6 +26,8 @@ function recorder(): { ctx: CanvasRenderingContext2D; calls: Call[] } {
2626 beginPath: log("beginPath"), moveTo: log("moveTo"), lineTo: log("lineTo"),
2727 arc: log("arc"), stroke: log("stroke"), fill: log("fill"),
2828 fillText: log("fillText"), save: log("save"), restore: log("restore"),
29 clip: log("clip"), translate: log("translate"), rotate: log("rotate"),
30 drawImage: log("drawImage"), closePath: log("closePath"),
2931 measureText: () => ({ width: 10 }),
3032 createRadialGradient: (...args: unknown[]) => {
3133 calls.push({ name: "createRadialGradient", args: args as number[], text: "" });
@@ -59,17 +61,22 @@ const hurtWorld = (yaw: number, from: [number, number, number], victim: number):
5961 return world;
6062 };
6163
64 // The damage wedges are arcs centred on the crosshair; the radar draws its
65 // own arcs too, off in the corner, so a plain count of "arc" calls is no
66 // longer enough to isolate them.
67 const wedgeArcs = (calls: Call[]): Call[] => calls.filter((c) => c.name === "arc" && c.args[0] === W / 2);
68
6269 test("a hit from behind puts the wedge below the crosshair, one from the left on its left", () => {
6370 const behind = recorder();
6471 draw(behind, hurtWorld(0, [-500, 0, 40], 0));
65 const back = behind.calls.filter((c) => c.name === "arc").map(wedgePoint);
72 const back = wedgeArcs(behind.calls).map(wedgePoint);
6673 expect(back.length).toBe(1);
6774 expect(back[0].y).toBeGreaterThan(H / 2 + 20);
6875 expect(Math.abs(back[0].x - W / 2)).toBeLessThan(20);
6976
7077 const left = recorder();
7178 draw(left, hurtWorld(0, [0, 500, 40], 0));
72 const side = left.calls.filter((c) => c.name === "arc").map(wedgePoint);
79 const side = wedgeArcs(left.calls).map(wedgePoint);
7380 expect(side.length).toBe(1);
7481 expect(side[0].x).toBeLessThan(W / 2 - 20);
7582 expect(Math.abs(side[0].y - H / 2)).toBeLessThan(20);
@@ -80,11 +87,11 @@ test("hits stack, and damage to anyone but the player being drawn is not drawn",
8087 const world = hurtWorld(0, [-500, 0, 40], 0);
8188 world.effects.push({ kind: "hurt", at: world.time, victim: 0, from: [0, -500, 40], amount: 18 });
8289 draw(two, world);
83 expect(two.calls.filter((c) => c.name === "arc").length).toBe(2);
90 expect(wedgeArcs(two.calls).length).toBe(2);
8491
8592 const bot = recorder();
8693 draw(bot, hurtWorld(0, [-500, 0, 40], 4));
87 expect(bot.calls.filter((c) => c.name === "arc").length).toBe(0);
94 expect(wedgeArcs(bot.calls).length).toBe(0);
8895 });
8996
9097 test("an old hit has faded out by the time the effects list is trimmed", () => {
@@ -92,7 +99,7 @@ test("an old hit has faded out by the time the effects list is trimmed", () => {
9299 world.time += 1.5; // still in the list, which update keeps for two seconds
93100 const frame = recorder();
94101 draw(frame, world);
95 expect(frame.calls.filter((c) => c.name === "arc").length).toBe(0);
102 expect(wedgeArcs(frame.calls).length).toBe(0);
96103 });
97104
98105 test("the hurt vignette follows health and its gradient is built once, not per frame", () => {
@@ -153,7 +160,10 @@ test("a spectator does not inherit the dead player's hit markers", () => {
153160 world.effects.push({ kind: "hitmark", at: world.time, headshot: false });
154161 const watching = recorder();
155162 draw(watching, world, mate);
156 expect(watching.calls.filter((c) => c.name === "moveTo").length).toBe(0);
163 // The radar draws its own moveTo near the corner for the self arrow; the
164 // hit marker's crosses are the ones centred on the crosshair.
165 const centred = watching.calls.filter((c) => c.name === "moveTo" && Math.abs(c.args[0] - W / 2) < 50);
166 expect(centred.length).toBe(0);
157167
158168 // Alive, the same effect is the marker it was drawn for.
159169 const alive = createWorld(SHORT, "me", "ct", 5, 79);
@@ -183,3 +193,19 @@ test("the round result stays up through the end delay and the freeze that follow
183193 draw(live, world);
184194 expect(live.calls.some((c) => c.text.includes("defused the bomb"))).toBe(false);
185195 });
196
197 test("the radar sits in the bottom-left corner and labels both sites", () => {
198 const world = createWorld(SHORT, "me", "ct", 5, 91);
199 const frame = recorder();
200 draw(frame, world);
201 const texts = frame.calls.filter((c) => c.name === "fillText").map((c) => c.text);
202 expect(texts).toContain("A");
203 expect(texts).toContain("B");
204
205 // The radar's border is a full circle, away from the money line, the kill
206 // feed and the clock, all of which live in the top half of the screen.
207 const border = frame.calls.find(
208 (c) => c.name === "arc" && c.args[0] < W / 2 && c.args[1] > H / 2 && c.args[3] === 0,
209 );
210 expect(border).toBeDefined();
211 });
src/client/hud.ts +119 −5
@@ -8,8 +8,10 @@
88 * it have to be that teammate's or the HUD belongs to a corpse.
99 */
1010
11 import { angleDelta, clamp } from "./math.ts";
11 import { angleDelta, clamp, rad } from "./math.ts";
12 import * as map from "./map.ts";
1213 import { MOVE } from "./physics.ts";
14 import { buildRadar, RADAR_RADIUS } from "./radar.ts";
1315 import { deathNotice } from "./spectate.ts";
1416 import { inaccuracy, WEAPONS } from "./weapons.ts";
1517 import { currentWeapon, eyePos, useProgress, type Player, type World } from "./world.ts";
@@ -26,8 +28,6 @@ const HURT_FADE = 1.1;
2628 /** How long a round message stays up when the clock is not holding it there. */
2729 const MESSAGE_FADE = 4;
2830
29 const rad = Math.PI / 180;
30
3131 // The vignette is one radial gradient per canvas size, kept between frames:
3232 // building it costs an allocation and drawHud runs sixty times a second, so it
3333 // is built with its colours at full strength and faded with globalAlpha.
@@ -82,8 +82,8 @@ function drawDamage(
8282 // Yaw grows anticlockwise, so a source to the left is a positive angle off
8383 // the view; the canvas turns clockwise from +x, so the wedge sits at minus
8484 // that angle, a quarter turn back from straight up.
85 const middle = -angleDelta(view.yaw, bearing) * rad - Math.PI / 2;
86 const half = (14 + clamp(effect.amount, 0, 60) * 0.2) * rad;
85 const middle = rad(-angleDelta(view.yaw, bearing)) - Math.PI / 2;
86 const half = rad(14 + clamp(effect.amount, 0, 60) * 0.2);
8787 ctx.globalAlpha = clamp(1 - age / HURT_FADE, 0, 1) * clamp(0.35 + effect.amount / 50, 0, 0.9);
8888 ctx.strokeStyle = "#ff4238";
8989 ctx.lineWidth = Math.max(3, Math.round(radius * 0.13));
@@ -99,6 +99,114 @@ const clock = (seconds: number): string => {
9999 return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
100100 };
101101
102 // The radar's map is a few hundred walkable cells, the same every frame: baked
103 // once into an offscreen canvas at the on-screen size and rotated by the
104 // canvas transform as the view turns, instead of walked and re-rotated by
105 // hand sixty times a second. Rebuilt only when that size changes, same as
106 // the vignette above.
107 let radarMap: OffscreenCanvas | HTMLCanvasElement | null = null;
108 let radarMapSize = -1;
109
110 function radarMapImage(size: number): CanvasImageSource | null {
111 if (radarMap === null || radarMapSize !== size) {
112 if (typeof OffscreenCanvas !== "undefined") radarMap = new OffscreenCanvas(size, size);
113 else if (typeof document !== "undefined") {
114 const canvas = document.createElement("canvas");
115 canvas.width = size;
116 canvas.height = size;
117 radarMap = canvas;
118 } else return null; // no canvas to build one on, e.g. a headless test
119 const rctx = radarMap.getContext("2d") as CanvasRenderingContext2D;
120 const cw = size / map.GRID_W, ch = size / map.GRID_H;
121 rctx.fillStyle = "rgba(190, 198, 212, 0.55)";
122 for (let r = 0; r < map.GRID_H; r++)
123 for (let c = 0; c < map.GRID_W; c++)
124 if (map.walkable(c, r)) rctx.fillRect(c * cw, r * ch, cw + 0.5, ch + 0.5);
125 radarMapSize = size;
126 }
127 return radarMap;
128 }
129
130 /**
131 * The radar: `view`'s map, rotated so its yaw points up, in a circle at
132 * `(x, y)` of `size` pixels across. Only ever `view`'s team, the bomb once
133 * it is planted, and the two sites — the data `buildRadar` hands back, with
134 * no enemy in it to draw even by mistake.
135 */
136 function drawRadar(
137 ctx: CanvasRenderingContext2D,
138 world: World,
139 view: Player,
140 x: number,
141 y: number,
142 size: number,
143 ): void {
144 const radius = size / 2;
145 const cx = x + radius, cy = y + radius;
146 ctx.fillStyle = "rgba(8, 10, 14, 0.6)";
147 ctx.beginPath();
148 ctx.arc(cx, cy, radius, 0, Math.PI * 2);
149 ctx.fill();
150
151 ctx.save();
152 ctx.beginPath();
153 ctx.arc(cx, cy, radius, 0, Math.PI * 2);
154 ctx.clip();
155 const image = radarMapImage(Math.max(1, Math.round(size)));
156 if (image !== null) {
157 // The image is baked with north up; turning it to match the view's yaw
158 // is one canvas rotation, not a re-walk of the grid.
159 ctx.save();
160 ctx.translate(cx, cy);
161 ctx.rotate(rad(view.yaw - 90));
162 ctx.drawImage(image, -radius, -radius, size, size);
163 ctx.restore();
164 }
165
166 const teamColor = view.team === "t" ? T_COLOR : CT_COLOR;
167 ctx.textAlign = "center";
168 for (const blip of buildRadar(world, view, RADAR_RADIUS)) {
169 const px = cx + blip.x * radius, py = cy + blip.y * radius;
170 if (blip.kind === "site") {
171 ctx.fillStyle = "rgba(255, 255, 255, 0.9)";
172 ctx.beginPath();
173 ctx.arc(px, py, Math.max(3, radius * 0.05), 0, Math.PI * 2);
174 ctx.fill();
175 ctx.font = `${Math.round(radius * 0.16)}px system-ui, sans-serif`;
176 ctx.fillText(blip.label, px, py - radius * 0.09);
177 } else if (blip.kind === "bomb") {
178 ctx.fillStyle = "#ff6b5b";
179 ctx.beginPath();
180 ctx.arc(px, py, Math.max(3, radius * 0.06), 0, Math.PI * 2);
181 ctx.fill();
182 } else if (blip.kind === "mate") {
183 ctx.fillStyle = teamColor;
184 ctx.beginPath();
185 ctx.arc(px, py, Math.max(2, radius * 0.045), 0, Math.PI * 2);
186 ctx.fill();
187 } else {
188 // self: an arrow, not a dot, pointing at `facing` — always straight up,
189 // since the whole radar is already turned to match the view's yaw.
190 const r = Math.max(4, radius * 0.08);
191 const dir = blip.facing - Math.PI / 2;
192 ctx.fillStyle = "#ffffff";
193 ctx.beginPath();
194 ctx.moveTo(px + Math.cos(dir) * r, py + Math.sin(dir) * r);
195 ctx.lineTo(px + Math.cos(dir + 2.4) * r * 0.6, py + Math.sin(dir + 2.4) * r * 0.6);
196 ctx.lineTo(px + Math.cos(dir - 2.4) * r * 0.6, py + Math.sin(dir - 2.4) * r * 0.6);
197 ctx.closePath();
198 ctx.fill();
199 }
200 }
201 ctx.restore();
202
203 ctx.strokeStyle = "rgba(255, 255, 255, 0.35)";
204 ctx.lineWidth = 2;
205 ctx.beginPath();
206 ctx.arc(cx, cy, radius, 0, Math.PI * 2);
207 ctx.stroke();
208 }
209
102210 export function drawHud(
103211 ctx: CanvasRenderingContext2D,
104212 world: World,
@@ -176,6 +284,12 @@ export function drawHud(
176284 ctx.fillRect(x, y, w, h);
177285 };
178286
287 // --- radar: bottom-left, clear of the money line above and the health and
288 // armour numbers below, at 360px of a 1280px-wide HUD — about 28% of the
289 // width, the same share at any size since it scales with everything else.
290 const radarSize = Math.round(360 * scale);
291 drawRadar(ctx, world, view, pad, height - pad - Math.round(78 * scale) - radarSize, radarSize);
292
179293 // --- round clock and score
180294 const barW = Math.round(240 * scale);
181295 panel(width / 2 - barW / 2, 0, barW, Math.round(44 * scale));
src/client/radar.test.ts added +73
@@ -0,0 +1,73 @@
1 import { expect, test } from "bun:test";
2 import { buildRadar } from "./radar.ts";
3 import { SHORT } from "./match.ts";
4 import { createWorld, type Player, type World } from "./world.ts";
5
6 /** A world with one live CT teammate to move around, and one T enemy nearby. */
7 function worldWithMate(): { world: World; me: Player; mate: Player; enemy: Player } {
8 const world = createWorld(SHORT, "me", "ct", 5, 11);
9 const me = world.me;
10 me.pos = [0, 0, 0];
11 me.yaw = 90;
12 const [mate, ...restCt] = world.players.filter((p) => p.team === "ct" && p !== me);
13 for (const p of restCt) p.alive = false; // one teammate on the radar, not four
14 const enemy = world.players.find((p) => p.team === "t") as Player;
15 enemy.pos = [0, 300, 0];
16 return { world, me, mate: mate as Player, enemy };
17 }
18
19 test("the person themself is always at the centre", () => {
20 const { world, me } = worldWithMate();
21 const self = buildRadar(world, me, 2000).find((b) => b.kind === "self");
22 expect(self).toEqual({ kind: "self", x: 0, y: 0, facing: 0 });
23 });
24
25 test("a teammate due north of a player facing north sits above the centre", () => {
26 const { world, me, mate } = worldWithMate();
27 mate.pos = [0, 500, 0]; // due north
28 me.yaw = 90; // facing north
29 const blip = buildRadar(world, me, 2000).find((b) => b.kind === "mate");
30 expect(blip).toBeDefined();
31 expect(blip!.y).toBeLessThan(-0.05); // up is negative y in canvas axes
32 expect(Math.abs(blip!.x)).toBeLessThan(0.05);
33 });
34
35 test("the same teammate sits to the left once the player turns to face east", () => {
36 const { world, me, mate } = worldWithMate();
37 mate.pos = [0, 500, 0]; // still due north — the player turned, the map did not
38 me.yaw = 0; // facing east
39 const blip = buildRadar(world, me, 2000).find((b) => b.kind === "mate");
40 expect(blip).toBeDefined();
41 expect(blip!.x).toBeLessThan(-0.05);
42 expect(Math.abs(blip!.y)).toBeLessThan(0.05);
43 });
44
45 test("a teammate beyond the radius is dropped, not clamped to the edge", () => {
46 const { world, me, mate } = worldWithMate();
47 mate.pos = [0, 5000, 0];
48 const blips = buildRadar(world, me, 2000);
49 expect(blips.some((b) => b.kind === "mate")).toBe(false);
50 });
51
52 test("enemies never appear, however close or however many are alive", () => {
53 const { world, me, enemy } = worldWithMate();
54 const blips = buildRadar(world, me, 5000);
55 expect(blips.filter((b) => b.kind === "mate").length).toBe(1); // the one live CT, not the T
56 expect(enemy.alive).toBe(true); // close and alive, and still not a blip
57 });
58
59 test("the bomb blips once planted, and not before", () => {
60 const { world, me } = worldWithMate();
61 world.bomb.pos = [0, 200, 0];
62 world.bomb.state = "carried";
63 expect(buildRadar(world, me, 2000).some((b) => b.kind === "bomb")).toBe(false);
64 world.bomb.state = "planted";
65 expect(buildRadar(world, me, 2000).some((b) => b.kind === "bomb")).toBe(true);
66 });
67
68 test("both sites are on the radar, marked A and B", () => {
69 const { world, me } = worldWithMate();
70 const blips = buildRadar(world, me);
71 const labels = blips.filter((b) => b.kind === "site").map((b) => (b as { label: string }).label);
72 expect(labels.sort()).toEqual(["A", "B"]);
73 });
src/client/radar.ts added +66
@@ -0,0 +1,66 @@
1 /**
2 * The radar: plain data about what your side is owed to know, in a frame
3 * rotated so the view's yaw points up. It draws nothing — `hud.ts` turns
4 * this list into pixels — so it can be tested without a canvas.
5 *
6 * Axes match the canvas hud.ts draws into rather than the world's: +x is the
7 * view's right, +y is down the screen. That way a blip's (x, y) is exactly
8 * where hud.ts multiplies by the radar's pixel radius and adds the centre,
9 * with no sign to remember at the call site.
10 */
11
12 import { rad } from "./math.ts";
13 import * as map from "./map.ts";
14 import type { Player, World } from "./world.ts";
15
16 export type Blip =
17 | { kind: "self"; x: number; y: number; facing: number }
18 | { kind: "mate"; x: number; y: number }
19 | { kind: "bomb"; x: number; y: number }
20 | { kind: "site"; x: number; y: number; label: "A" | "B" };
21
22 /** Covers the whole map from most standing points; a corner-to-corner shot is the rare miss. */
23 export const RADAR_RADIUS = map.GRID_W * map.CELL;
24
25 /**
26 * A world offset (east, north), rotated by the view's yaw and dropped into
27 * canvas axes, or null if it falls outside `radius` — dropped, not clamped
28 * to the edge, so a blip on the radar is always where it really is.
29 */
30 function project(ox: number, oy: number, yaw: number, radius: number): [number, number] | null {
31 const theta = rad(90 - yaw);
32 const cos = Math.cos(theta), sin = Math.sin(theta);
33 const rx = ox * cos - oy * sin;
34 const ry = ox * sin + oy * cos;
35 const x = rx / radius;
36 const y = -ry / radius;
37 return x * x + y * y > 1 ? null : [x, y];
38 }
39
40 /**
41 * The blips `view` is owed: itself at the centre, its living teammates, the
42 * bomb once it is planted, and both sites, labelled. Never the other team —
43 * that is what turns a radar into a wallhack.
44 */
45 export function buildRadar(world: World, view: Player, radius: number = RADAR_RADIUS): Blip[] {
46 const blips: Blip[] = [{ kind: "self", x: 0, y: 0, facing: 0 }];
47
48 for (const p of world.players) {
49 if (p === view || p.team !== view.team || !p.alive) continue;
50 const at = project(p.pos[0] - view.pos[0], p.pos[1] - view.pos[1], view.yaw, radius);
51 if (at !== null) blips.push({ kind: "mate", x: at[0], y: at[1] });
52 }
53
54 if (world.bomb.state === "planted") {
55 const at = project(world.bomb.pos[0] - view.pos[0], world.bomb.pos[1] - view.pos[1], view.yaw, radius);
56 if (at !== null) blips.push({ kind: "bomb", x: at[0], y: at[1] });
57 }
58
59 for (const label of ["A", "B"] as const) {
60 const site = map.sites[label];
61 const at = project(site[0] - view.pos[0], site[1] - view.pos[1], view.yaw, radius);
62 if (at !== null) blips.push({ kind: "site", x: at[0], y: at[1], label });
63 }
64
65 return blips;
66 }
static/app.642d83d9b8.js added +58

Diff not shown — a line 62,866 characters wide (limit 2,000). +58

View file
static/app.f7b32033ed.js deleted −58

Diff not shown — a line 60,526 characters wide (limit 2,000). −58

View file

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs