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

Hear where they are: pan every world sound, and give players footsteps #22

Merged opened this

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

Merged

scout/cs:scout/21-hear-where-they-are-pan-every-world-soun → main 1 commit fork

  • Merged. scout/21-hear-where-they-are-pan-every-world-soun was merged into main as ef1e2b5.

Review

Sign in to review this change.
9 files changed +244 −74
README.md +8 −3
@@ -43,8 +43,12 @@ The bundle in `static/` is committed, so a fresh clone plays without building;
4343 loss bonuses. Hit feedback runs both ways: a hit marker and a beep when you hit
4444 someone, and when you are hit a view punch away from the shot, a wedge around
4545 the crosshair pointing at whoever fired, a vignette that reddens as your health
46 falls and a pain sound. The leaderboard is a real SQLite table under
47 `FORGE_APP_DATA`.
46 falls and a pain sound. Every world sound is panned around the listener's
47 view and fades with distance — a shot or a footstep to your left plays from
48 the left channel, to your right the right — and a running player who is not
49 walking, ducking or airborne leaves footsteps this way too, so someone
50 closing in from behind is something you hear before you see. The
51 leaderboard is a real SQLite table under `FORGE_APP_DATA`.
4852 - **Generated, not licensed.** The map (`de_forge`), the textures, the player and
4953 weapon models and every sound are produced in code at start-up. There are no
5054 asset files and nothing from the real game is used — the numbers follow CS's
@@ -91,7 +95,8 @@ pixels you are getting. Nothing on the page moves when it changes.
9195 `bots`, `observe`, `action`, `policy`, `weights`, `models`, `textures`,
9296 `press` (the input latch), `quality` (the render scale the frame time buys),
9397 `spectate` (whose eyes you borrow when you are dead), `summary` (the end of
94 the match as numbers), `renderer`, `hud`, `input`, `audio`, `main`.
98 the match as numbers), `ears` (a sound's pan and volume, as plain data),
99 `renderer`, `hud`, `input`, `audio`, `main`.
95100 Everything except the last five is headless and tested.
96101 - `src/rl/` — training, which the game never imports: `arena` is the environment
97102 (a real duel on de_forge with the round and the economy taken out), `ppo` is
src/client/audio.ts +35 −7
@@ -4,11 +4,13 @@
44 * context is only created once the person has clicked something.
55 */
66
7 import { dist, type Vec3 } from "./math.ts";
7 import { ears } from "./ears.ts";
8 import type { Vec3 } from "./math.ts";
89 import { WEAPONS, type WeaponId } from "./weapons.ts";
910
1011 export type Audio = {
11 shot(weapon: WeaponId, at: Vec3, listener: Vec3): void;
12 shot(weapon: WeaponId, at: Vec3, listener: Vec3, listenerYaw: number): void;
13 step(at: Vec3, listener: Vec3, listenerYaw: number): void;
1214 hit(headshot: boolean): void;
1315 pain(amount: number): void;
1416 beep(pitch: number, gain?: number): void;
@@ -45,15 +47,17 @@ export function createAudio(): Audio {
4547 const audio: Audio = {
4648 muted: false,
4749 resume: () => void ensure(),
48 shot(weapon, at, listener) {
50 shot(weapon, at, listener, listenerYaw) {
4951 if (audio.muted) return;
5052 const context = ensure();
5153 if (context === null || noise === null) return;
5254 const kind = WEAPONS[weapon]?.kind ?? "rifle";
5355 const tone = TONE[kind] ?? TONE.rifle;
54 const d = dist(at, listener);
55 const volume = Math.min(1, 900 / Math.max(120, d));
56 const { pan, volume } = ears(at, listener, listenerYaw);
5657 const now = context.currentTime;
58 const panner = context.createStereoPanner();
59 panner.pan.value = pan;
60 panner.connect(context.destination);
5761
5862 const source = context.createBufferSource();
5963 source.buffer = noise;
@@ -65,7 +69,7 @@ export function createAudio(): Audio {
6569 const gain = context.createGain();
6670 gain.gain.setValueAtTime(0.35 * volume, now);
6771 gain.gain.exponentialRampToValueAtTime(0.0008, now + tone.length);
68 source.connect(filter).connect(gain).connect(context.destination);
72 source.connect(filter).connect(gain).connect(panner);
6973 source.start(now);
7074 source.stop(now + tone.length);
7175
@@ -75,10 +79,34 @@ export function createAudio(): Audio {
7579 const thumpGain = context.createGain();
7680 thumpGain.gain.setValueAtTime(0.3 * volume, now);
7781 thumpGain.gain.exponentialRampToValueAtTime(0.0008, now + tone.length);
78 thump.connect(thumpGain).connect(context.destination);
82 thump.connect(thumpGain).connect(panner);
7983 thump.start(now);
8084 thump.stop(now + tone.length);
8185 },
86 /** A footstep: a short low tap, panned and faded the same way a shot is. */
87 step(at, listener, listenerYaw) {
88 if (audio.muted) return;
89 const context = ensure();
90 if (context === null || noise === null) return;
91 const { pan, volume } = ears(at, listener, listenerYaw);
92 const now = context.currentTime;
93 const length = 0.09;
94
95 const source = context.createBufferSource();
96 source.buffer = noise;
97 source.playbackRate.value = 0.5 + Math.random() * 0.15;
98 const filter = context.createBiquadFilter();
99 filter.type = "lowpass";
100 filter.frequency.value = 320;
101 const gain = context.createGain();
102 gain.gain.setValueAtTime(0.14 * volume, now);
103 gain.gain.exponentialRampToValueAtTime(0.0008, now + length);
104 const panner = context.createStereoPanner();
105 panner.pan.value = pan;
106 source.connect(filter).connect(gain).connect(panner).connect(context.destination);
107 source.start(now);
108 source.stop(now + length);
109 },
82110 hit(headshot) {
83111 audio.beep(headshot ? 1400 : 900, 0.08);
84112 },
src/client/ears.test.ts added +34
@@ -0,0 +1,34 @@
1 import { expect, test } from "bun:test";
2 import { ears } from "./ears.ts";
3
4 test("a source dead ahead pans centre", () => {
5 const { pan } = ears([100, 0, 0], [0, 0, 0], 0);
6 expect(pan).toBeCloseTo(0, 6);
7 });
8
9 test("a source 90 degrees to the left pans left, and to the right pans right", () => {
10 // Yaw grows anticlockwise (math.ts), so turning +90 from facing +x faces +y,
11 // which puts +y on the listener's left.
12 const left = ears([0, 100, 0], [0, 0, 0], 0);
13 expect(left.pan).toBeLessThan(0);
14 const right = ears([0, -100, 0], [0, 0, 0], 0);
15 expect(right.pan).toBeGreaterThan(0);
16 });
17
18 test("a source directly behind is roughly centred and no louder than the same distance ahead", () => {
19 const ahead = ears([100, 0, 0], [0, 0, 0], 0);
20 const behind = ears([-100, 0, 0], [0, 0, 0], 0);
21 expect(Math.abs(behind.pan)).toBeLessThan(0.05);
22 expect(behind.volume).toBeLessThanOrEqual(ahead.volume + 1e-9);
23 });
24
25 test("volume falls with distance, never exceeds 1, and never divides by zero at zero distance", () => {
26 const near = ears([50, 0, 0], [0, 0, 0], 0);
27 const far = ears([2000, 0, 0], [0, 0, 0], 0);
28 expect(near.volume).toBeLessThanOrEqual(1);
29 expect(far.volume).toBeLessThan(near.volume);
30 expect(far.volume).toBeGreaterThan(0);
31 const here = ears([0, 0, 0], [0, 0, 0], 0);
32 expect(Number.isFinite(here.volume)).toBe(true);
33 expect(here.volume).toBeLessThanOrEqual(1);
34 });
src/client/ears.ts added +26
@@ -0,0 +1,26 @@
1 /**
2 * Where a sound sits around the listener, as plain data — no Web Audio here,
3 * so it is tested headlessly. `pos` and `listener` are world positions; `yaw`
4 * is the listener's view yaw in degrees, the same convention `math.ts` uses.
5 */
6
7 import { angleDelta, dist2d, type Vec3 } from "./math.ts";
8
9 export type Ears = { pan: number; volume: number };
10
11 /** The same falloff `audio.shot` already used: full volume inside 120 units, gone by 900. */
12 const REFERENCE_DISTANCE = 900;
13 const MIN_DISTANCE = 120;
14
15 /** How a source at `pos` sounds to someone at `listener` looking along `yaw`. */
16 export function ears(pos: Vec3, listener: Vec3, yaw: number): Ears {
17 const bearing = (Math.atan2(pos[1] - listener[1], pos[0] - listener[0]) * 180) / Math.PI;
18 // Yaw grows anticlockwise (math.ts), so a source to the listener's left sits
19 // at a positive angle off the view; a StereoPannerNode's -1 is the left
20 // channel, so the sign is flipped to get there.
21 const rel = (angleDelta(yaw, bearing) * Math.PI) / 180;
22 const pan = -Math.sin(rel);
23 const d = dist2d(pos, listener);
24 const volume = Math.min(1, REFERENCE_DISTANCE / Math.max(MIN_DISTANCE, d));
25 return { pan, volume };
26 }
src/client/main.ts +9 −2
@@ -9,7 +9,7 @@
99 import { createAudio } from "./audio.ts";
1010 import { drawHud } from "./hud.ts";
1111 import { attachButtons, createControls } from "./input.ts";
12 import { clamp } from "./math.ts";
12 import { clamp, dist } from "./math.ts";
1313 import { COMPETITIVE, SHORT, type Rules } from "./match.ts";
1414 import { MAX_SCALE, nextRenderScale } from "./quality.ts";
1515 import { createRenderer, type Camera } from "./renderer.ts";
@@ -208,17 +208,24 @@ function boot(): void {
208208 const saved = localStorage.getItem("cs.name");
209209 if (saved !== null && nameField.value === "") nameField.value = saved;
210210
211 // A footstep further than this is not worth a node graph — about the width
212 // of a couple of site rooms in de_forge, not the whole map.
213 const FOOTSTEP_EARSHOT = 1500;
214
211215 // --- sound and flinch for whatever happened in the ticks we just ran
212216 function feelEffects(current: World, ears: Player): void {
213217 for (const effect of current.effects) {
214218 if (effect.at <= lastSound) continue;
215219 // Sound comes from where the camera is: while dead that is a teammate's
216220 // head, not the corpse the shots are no longer near.
217 if (effect.kind === "shot") audio.shot(effect.weapon, effect.pos, eyePos(ears));
221 const at = eyePos(ears);
222 if (effect.kind === "shot") audio.shot(effect.weapon, effect.pos, at, ears.yaw);
218223 else if (effect.kind === "hitmark") audio.hit(effect.headshot);
219224 else if (effect.kind === "hurt" && effect.victim === current.me.id) {
220225 flinch(current.me, effect.from, effect.amount);
221226 audio.pain(effect.amount);
227 } else if (effect.kind === "sound" && effect.name === "step" && dist(effect.pos, at) <= FOOTSTEP_EARSHOT) {
228 audio.step(effect.pos, at, ears.yaw);
222229 }
223230 }
224231 lastSound = current.time;
src/client/world.test.ts +45 −2
@@ -3,8 +3,8 @@ import { siteAt, sites } from "./map.ts";
33 import { SHORT } from "./match.ts";
44 import { WEAPONS } from "./weapons.ts";
55 import {
6 aimAt, bodyCentre, buy, createWorld, currentWeapon, eyePos, flinch, NO_INPUT, shoot, startRound,
7 update, type Input, type Player, type World,
6 aimAt, applyInput, bodyCentre, buy, createWorld, currentWeapon, eyePos, flinch, NO_INPUT, shoot,
7 startRound, update, type Input, type Player, type World,
88 } from "./world.ts";
99
1010 const TICK = 1 / 64;
@@ -242,6 +242,49 @@ test("a hit flinches the view away from where it came from, and stays bounded",
242242 expect(Math.abs(me.punch[1])).toBeLessThanOrEqual(6);
243243 });
244244
245 // applyInput is stepped directly here, the way arena.ts does — a plain flat
246 // floor and one player, with none of the round's bookkeeping (and none of
247 // the other nine bots' own footsteps) in the way of counting a stride.
248 function walkOnGround(world: World, seconds: number, input: Input): void {
249 world.boxes = [{ min: [-10000, -10000, -32], max: [10000, 10000, 0], mat: 0 }];
250 world.match.phase = "live";
251 world.me.pos = [0, 0, 2];
252 world.me.vel = [0, 0, 0];
253 world.me.onGround = true;
254 world.effects.length = 0;
255 for (let t = 0; t < seconds; t += TICK) {
256 world.time += TICK;
257 applyInput(world, world.me, input, TICK);
258 }
259 }
260
261 const stepsIn = (world: World): number =>
262 world.effects.filter((e) => e.kind === "sound" && e.name === "step").length;
263
264 test("running in a straight line for a second leaves a stride's worth of footsteps", () => {
265 const world = createWorld(SHORT, "me", "ct", 5, 53);
266 walkOnGround(world, 1, { ...NO_INPUT, forward: 1 });
267 expect(stepsIn(world)).toBeGreaterThanOrEqual(3);
268 expect(stepsIn(world)).toBeLessThanOrEqual(4);
269 });
270
271 test("walking or ducking leaves no footsteps at all", () => {
272 const walking = createWorld(SHORT, "me", "ct", 5, 59);
273 walkOnGround(walking, 1, { ...NO_INPUT, forward: 1, walk: true });
274 expect(stepsIn(walking)).toBe(0);
275
276 const ducking = createWorld(SHORT, "me", "ct", 5, 61);
277 walkOnGround(ducking, 1, { ...NO_INPUT, forward: 1, duck: true });
278 expect(stepsIn(ducking)).toBe(0);
279 });
280
281 test("half the speed leaves about half the footsteps", () => {
282 const world = createWorld(SHORT, "me", "ct", 5, 67);
283 walkOnGround(world, 1, { ...NO_INPUT, forward: 0.5 });
284 expect(stepsIn(world)).toBeGreaterThanOrEqual(1);
285 expect(stepsIn(world)).toBeLessThanOrEqual(2);
286 });
287
245288 test("a match plays itself to the end inside its own time", () => {
246289 const world = createWorld({ ...SHORT, winScore: 2, freezeTime: 1, roundTime: 40, endDelay: 1 }, "me", "ct", 5, 23);
247290 world.me.alive = false;
src/client/world.ts +29 −2
@@ -9,7 +9,7 @@
99
1010 import { applyHit, traceBullet, type Actor } from "./combat.ts";
1111 import * as map from "./map.ts";
12 import { angleVectors, angleDelta, clamp, dist, normalize, rng, sub, type Vec3 } from "./math.ts";
12 import { angleVectors, angleDelta, clamp, dist, dist2d, normalize, rng, sub, type Vec3 } from "./math.ts";
1313 import * as rules from "./match.ts";
1414 import { eyeHeight, hullHeight, move, type Body, type Wish, MOVE } from "./physics.ts";
1515 import { visible } from "./trace.ts";
@@ -47,6 +47,7 @@ export type Player = Actor & Body & {
4747 headshots: number;
4848 damage: number;
4949 spawnAt: number;
50 stepDistance: number; // ground covered since the last footstep, for cadence
5051 };
5152
5253 export type Bomb = {
@@ -135,7 +136,7 @@ function makePlayer(id: number, team: Team, name: string, bot: boolean): Player
135136 defuseKit: false, hasBomb: false,
136137 nextShotAt: 0, reloadUntil: 0, shotIndex: 0, lastShotAt: -99, scoped: false, firedThisPress: false,
137138 punch: [0, 0],
138 kills: 0, deaths: 0, headshots: 0, damage: 0, spawnAt: 0,
139 kills: 0, deaths: 0, headshots: 0, damage: 0, spawnAt: 0, stepDistance: 0,
139140 };
140141 }
141142
@@ -198,6 +199,7 @@ export function startRound(world: World): void {
198199 p.shotIndex = 0;
199200 p.punch = [0, 0];
200201 p.scoped = false;
202 p.stepDistance = 0;
201203 p.spawnAt = world.time;
202204 p.yaw = p.team === "t" ? 90 : -90;
203205 p.pitch = 0;
@@ -488,6 +490,29 @@ export function useProgress(world: World, p: Player): { label: string; fraction:
488490 return null;
489491 }
490492
493 // --- footsteps --------------------------------------------------------------
494
495 /** A running stride is roughly 60–70 units at 250 u/s; the middle of that. */
496 const STEP_STRIDE = 65;
497
498 /**
499 * A footstep every stride's worth of ground covered — never while ducking,
500 * walking (Shift) or airborne, the ways CS itself keeps you quiet. Distance
501 * drives the cadence rather than a timer, so it speeds up with the player;
502 * pushing one `Effect` a few times a second, not one a tick, is what keeps
503 * this affordable in the training arena that steps every player every tick.
504 */
505 function footsteps(world: World, p: Player, wish: Wish, before: Vec3): void {
506 if (!p.onGround || p.ducking || wish.walk) {
507 p.stepDistance = 0;
508 return;
509 }
510 p.stepDistance += dist2d(before, p.pos);
511 if (p.stepDistance < STEP_STRIDE) return;
512 p.stepDistance -= STEP_STRIDE;
513 world.effects.push({ kind: "sound", at: world.time, pos: [p.pos[0], p.pos[1], p.pos[2]], name: "step" });
514 }
515
491516 // --- the tick -------------------------------------------------------------
492517
493518 export function applyInput(world: World, p: Player, input: Input, dt: number): void {
@@ -511,7 +536,9 @@ export function applyInput(world: World, p: Player, input: Input, dt: number): v
511536 walk: input.walk || p.scoped,
512537 maxSpeed: Math.min(MOVE.maxSpeed, w.carrySpeed),
513538 };
539 const before: Vec3 = [p.pos[0], p.pos[1], p.pos[2]];
514540 move(p, wish, dt, world.boxes);
541 footsteps(world, p, wish, before);
515542 finishReload(world, p);
516543 if (input.reload) reload(world, p);
517544 if (w.zoom !== undefined && input.scope !== p.scoped && world.time > p.nextShotAt - 0.05) p.scoped = input.scope;
static/app.e8bccf8878.js deleted −58

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

View file
static/app.f7b32033ed.js added +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