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
Check · Review · Land · Live done — Landed and live in production.
scout/cs:scout/21-hear-where-they-are-pan-every-world-soun → main 1 commit fork
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; | ||
| 43 | 43 | loss bonuses. Hit feedback runs both ways: a hit marker and a beep when you hit |
| 44 | 44 | someone, and when you are hit a view punch away from the shot, a wedge around |
| 45 | 45 | 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`. | |
| 48 | 52 | - **Generated, not licensed.** The map (`de_forge`), the textures, the player and |
| 49 | 53 | weapon models and every sound are produced in code at start-up. There are no |
| 50 | 54 | 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. | ||
| 91 | 95 | `bots`, `observe`, `action`, `policy`, `weights`, `models`, `textures`, |
| 92 | 96 | `press` (the input latch), `quality` (the render scale the frame time buys), |
| 93 | 97 | `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`. | |
| 95 | 100 | Everything except the last five is headless and tested. |
| 96 | 101 | - `src/rl/` — training, which the game never imports: `arena` is the environment |
| 97 | 102 | (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 @@ | ||
| 4 | 4 | * context is only created once the person has clicked something. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { dist, type Vec3 } from "./math.ts"; | |
| 7 | import { ears } from "./ears.ts"; | |
| 8 | import type { Vec3 } from "./math.ts"; | |
| 8 | 9 | import { WEAPONS, type WeaponId } from "./weapons.ts"; |
| 9 | 10 | |
| 10 | 11 | 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; | |
| 12 | 14 | hit(headshot: boolean): void; |
| 13 | 15 | pain(amount: number): void; |
| 14 | 16 | beep(pitch: number, gain?: number): void; |
| @@ -45,15 +47,17 @@ export function createAudio(): Audio { | ||
| 45 | 47 | const audio: Audio = { |
| 46 | 48 | muted: false, |
| 47 | 49 | resume: () => void ensure(), |
| 48 | shot(weapon, at, listener) { | |
| 50 | shot(weapon, at, listener, listenerYaw) { | |
| 49 | 51 | if (audio.muted) return; |
| 50 | 52 | const context = ensure(); |
| 51 | 53 | if (context === null || noise === null) return; |
| 52 | 54 | const kind = WEAPONS[weapon]?.kind ?? "rifle"; |
| 53 | 55 | 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); | |
| 56 | 57 | const now = context.currentTime; |
| 58 | const panner = context.createStereoPanner(); | |
| 59 | panner.pan.value = pan; | |
| 60 | panner.connect(context.destination); | |
| 57 | 61 | |
| 58 | 62 | const source = context.createBufferSource(); |
| 59 | 63 | source.buffer = noise; |
| @@ -65,7 +69,7 @@ export function createAudio(): Audio { | ||
| 65 | 69 | const gain = context.createGain(); |
| 66 | 70 | gain.gain.setValueAtTime(0.35 * volume, now); |
| 67 | 71 | 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); | |
| 69 | 73 | source.start(now); |
| 70 | 74 | source.stop(now + tone.length); |
| 71 | 75 | |
| @@ -75,10 +79,34 @@ export function createAudio(): Audio { | ||
| 75 | 79 | const thumpGain = context.createGain(); |
| 76 | 80 | thumpGain.gain.setValueAtTime(0.3 * volume, now); |
| 77 | 81 | thumpGain.gain.exponentialRampToValueAtTime(0.0008, now + tone.length); |
| 78 | thump.connect(thumpGain).connect(context.destination); | |
| 82 | thump.connect(thumpGain).connect(panner); | |
| 79 | 83 | thump.start(now); |
| 80 | 84 | thump.stop(now + tone.length); |
| 81 | 85 | }, |
| 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 | }, | |
| 82 | 110 | hit(headshot) { |
| 83 | 111 | audio.beep(headshot ? 1400 : 900, 0.08); |
| 84 | 112 | }, |
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 @@ | ||
| 9 | 9 | import { createAudio } from "./audio.ts"; |
| 10 | 10 | import { drawHud } from "./hud.ts"; |
| 11 | 11 | import { attachButtons, createControls } from "./input.ts"; |
| 12 | import { clamp } from "./math.ts"; | |
| 12 | import { clamp, dist } from "./math.ts"; | |
| 13 | 13 | import { COMPETITIVE, SHORT, type Rules } from "./match.ts"; |
| 14 | 14 | import { MAX_SCALE, nextRenderScale } from "./quality.ts"; |
| 15 | 15 | import { createRenderer, type Camera } from "./renderer.ts"; |
| @@ -208,17 +208,24 @@ function boot(): void { | ||
| 208 | 208 | const saved = localStorage.getItem("cs.name"); |
| 209 | 209 | if (saved !== null && nameField.value === "") nameField.value = saved; |
| 210 | 210 | |
| 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 | ||
| 211 | 215 | // --- sound and flinch for whatever happened in the ticks we just ran |
| 212 | 216 | function feelEffects(current: World, ears: Player): void { |
| 213 | 217 | for (const effect of current.effects) { |
| 214 | 218 | if (effect.at <= lastSound) continue; |
| 215 | 219 | // Sound comes from where the camera is: while dead that is a teammate's |
| 216 | 220 | // 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); | |
| 218 | 223 | else if (effect.kind === "hitmark") audio.hit(effect.headshot); |
| 219 | 224 | else if (effect.kind === "hurt" && effect.victim === current.me.id) { |
| 220 | 225 | flinch(current.me, effect.from, effect.amount); |
| 221 | 226 | 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); | |
| 222 | 229 | } |
| 223 | 230 | } |
| 224 | 231 | lastSound = current.time; |
src/client/world.test.ts +45 −2
| @@ -3,8 +3,8 @@ import { siteAt, sites } from "./map.ts"; | ||
| 3 | 3 | import { SHORT } from "./match.ts"; |
| 4 | 4 | import { WEAPONS } from "./weapons.ts"; |
| 5 | 5 | 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, | |
| 8 | 8 | } from "./world.ts"; |
| 9 | 9 | |
| 10 | 10 | const TICK = 1 / 64; |
| @@ -242,6 +242,49 @@ test("a hit flinches the view away from where it came from, and stays bounded", | ||
| 242 | 242 | expect(Math.abs(me.punch[1])).toBeLessThanOrEqual(6); |
| 243 | 243 | }); |
| 244 | 244 | |
| 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 | ||
| 245 | 288 | test("a match plays itself to the end inside its own time", () => { |
| 246 | 289 | const world = createWorld({ ...SHORT, winScore: 2, freezeTime: 1, roundTime: 40, endDelay: 1 }, "me", "ct", 5, 23); |
| 247 | 290 | world.me.alive = false; |
src/client/world.ts +29 −2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { applyHit, traceBullet, type Actor } from "./combat.ts"; |
| 11 | 11 | 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"; | |
| 13 | 13 | import * as rules from "./match.ts"; |
| 14 | 14 | import { eyeHeight, hullHeight, move, type Body, type Wish, MOVE } from "./physics.ts"; |
| 15 | 15 | import { visible } from "./trace.ts"; |
| @@ -47,6 +47,7 @@ export type Player = Actor & Body & { | ||
| 47 | 47 | headshots: number; |
| 48 | 48 | damage: number; |
| 49 | 49 | spawnAt: number; |
| 50 | stepDistance: number; // ground covered since the last footstep, for cadence | |
| 50 | 51 | }; |
| 51 | 52 | |
| 52 | 53 | export type Bomb = { |
| @@ -135,7 +136,7 @@ function makePlayer(id: number, team: Team, name: string, bot: boolean): Player | ||
| 135 | 136 | defuseKit: false, hasBomb: false, |
| 136 | 137 | nextShotAt: 0, reloadUntil: 0, shotIndex: 0, lastShotAt: -99, scoped: false, firedThisPress: false, |
| 137 | 138 | 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, | |
| 139 | 140 | }; |
| 140 | 141 | } |
| 141 | 142 | |
| @@ -198,6 +199,7 @@ export function startRound(world: World): void { | ||
| 198 | 199 | p.shotIndex = 0; |
| 199 | 200 | p.punch = [0, 0]; |
| 200 | 201 | p.scoped = false; |
| 202 | p.stepDistance = 0; | |
| 201 | 203 | p.spawnAt = world.time; |
| 202 | 204 | p.yaw = p.team === "t" ? 90 : -90; |
| 203 | 205 | p.pitch = 0; |
| @@ -488,6 +490,29 @@ export function useProgress(world: World, p: Player): { label: string; fraction: | ||
| 488 | 490 | return null; |
| 489 | 491 | } |
| 490 | 492 | |
| 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 | ||
| 491 | 516 | // --- the tick ------------------------------------------------------------- |
| 492 | 517 | |
| 493 | 518 | 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 | ||
| 511 | 536 | walk: input.walk || p.scoped, |
| 512 | 537 | maxSpeed: Math.min(MOVE.maxSpeed, w.carrySpeed), |
| 513 | 538 | }; |
| 539 | const before: Vec3 = [p.pos[0], p.pos[1], p.pos[2]]; | |
| 514 | 540 | move(p, wish, dt, world.boxes); |
| 541 | footsteps(world, p, wish, before); | |
| 515 | 542 | finishReload(world, p); |
| 516 | 543 | if (input.reload) reload(world, p); |
| 517 | 544 | 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 filestatic/app.f7b32033ed.js added +58
Diff not shown — a line 60,526 characters wide (limit 2,000). +58
View file
new above
offline