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

What the ear is allowed to hear moves out of `main.ts`: a tested `hearing.ts` #124

Merged opened this

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

Merged

scout/cs:scout/96-what-the-ear-is-allowed-to-hear-moves-ou → main 1 commit fork

  • Merged. scout/96-what-the-ear-is-allowed-to-hear-moves-ou was merged into main as 5b44fba.

Review

Sign in to review this change.
4 files changed +166 −32
README.md +4
@@ -367,6 +367,10 @@ projections they feed cost 0.04 ms of a 16.7 ms frame, measured.
367367 the match as numbers), `ears` (a sound's pan and volume, as plain data),
368368 `hearing` (what a sound is worth to someone standing somewhere else: how loud
369369 it arrives through however many walls, and where they think it came from),
370 `earshot` (which of a tick's effects the person hears: the fixed ceilings a
371 footstep, a dry trigger, a plant and a defuse carry, the bomb's beep and its
372 blast carrying everywhere, and the cue it hands `main` — position, name,
373 weapon, pitch, amount — for `audio.ts` to play and `flinch` to feel),
370374 `settings` (mute, volume, sensitivity and difficulty: read, validated, and
371375 never NaN or an unknown name),
372376 `renderer`, `hud`, `input`, `audio`, `main`.
src/client/earshot.test.ts added +80
@@ -0,0 +1,80 @@
1 import { expect, test } from "bun:test";
2 import { cuesHeard, EMPTY_EARSHOT, FOOTSTEP_EARSHOT, SITE_EARSHOT } from "./earshot.ts";
3 import type { Effect } from "./world.ts";
4 import type { Vec3 } from "./math.ts";
5
6 const HERE: Vec3 = [0, 0, 0];
7
8 test("a footstep past its earshot is not heard, and one inside it is", () => {
9 const near: Effect = { kind: "sound", at: 1, pos: [FOOTSTEP_EARSHOT - 1, 0, 0], name: "step" };
10 const far: Effect = { kind: "sound", at: 1, pos: [FOOTSTEP_EARSHOT + 1, 0, 0], name: "step" };
11 expect(cuesHeard([near], 0, HERE, 1)).toEqual([{ kind: "step", pos: near.pos }]);
12 expect(cuesHeard([far], 0, HERE, 1)).toEqual([]);
13 });
14
15 test("a dry trigger has its own, shorter earshot", () => {
16 const near: Effect = { kind: "sound", at: 1, pos: [EMPTY_EARSHOT - 1, 0, 0], name: "empty" };
17 const far: Effect = { kind: "sound", at: 1, pos: [EMPTY_EARSHOT + 1, 0, 0], name: "empty" };
18 expect(cuesHeard([near], 0, HERE, 1)).toEqual([{ kind: "world", name: "empty", pos: near.pos, pitch: undefined }]);
19 expect(cuesHeard([far], 0, HERE, 1)).toEqual([]);
20 });
21
22 test("a plant and a defuse carry past the site, out to the same ceiling", () => {
23 const plant: Effect = { kind: "sound", at: 1, pos: [SITE_EARSHOT - 1, 0, 0], name: "plant" };
24 const defuse: Effect = { kind: "sound", at: 1, pos: [SITE_EARSHOT - 1, 0, 0], name: "defuse" };
25 const tooFar: Effect = { kind: "sound", at: 1, pos: [SITE_EARSHOT + 1, 0, 0], name: "plant" };
26 expect(cuesHeard([plant], 0, HERE, 1)).toHaveLength(1);
27 expect(cuesHeard([defuse], 0, HERE, 1)).toHaveLength(1);
28 expect(cuesHeard([tooFar], 0, HERE, 1)).toEqual([]);
29 });
30
31 test("the bomb's beep and its blast carry from anywhere at all", () => {
32 const beep: Effect = { kind: "sound", at: 1, pos: [1_000_000, 0, 0], name: "beep", pitch: 900 };
33 const blast: Effect = { kind: "sound", at: 1, pos: [1_000_000, 0, 0], name: "blast" };
34 expect(cuesHeard([beep], 0, HERE, 1)).toEqual([{ kind: "world", name: "beep", pos: beep.pos, pitch: 900 }]);
35 expect(cuesHeard([blast], 0, HERE, 1)).toEqual([{ kind: "world", name: "blast", pos: blast.pos, pitch: undefined }]);
36 });
37
38 test("an effect at or before the cutoff is never returned", () => {
39 const effect: Effect = { kind: "shot", at: 5, pos: HERE, weapon: "ak47", mine: false, shooter: 2 };
40 expect(cuesHeard([effect], 5, HERE, 1)).toEqual([]);
41 expect(cuesHeard([effect], 6, HERE, 1)).toEqual([]);
42 expect(cuesHeard([effect], 4, HERE, 1)).toHaveLength(1);
43 });
44
45 test("no effect is ever heard twice across two calls, one after the other", () => {
46 const first: Effect = { kind: "shot", at: 1, pos: HERE, weapon: "ak47", mine: false, shooter: 2 };
47 const second: Effect = { kind: "shot", at: 2, pos: HERE, weapon: "ak47", mine: false, shooter: 2 };
48 const effects = [first, second];
49 // The first call hears both; a second call against the same list, its
50 // cutoff moved on to the last thing it played, hears neither again — the
51 // resume bug this rule exists to close.
52 const firstPass = cuesHeard(effects, 0, HERE, 1);
53 expect(firstPass).toHaveLength(2);
54 const secondPass = cuesHeard(effects, 2, HERE, 1);
55 expect(secondPass).toEqual([]);
56 });
57
58 test("a hurt cue is only ever the local player's", () => {
59 const mine: Effect = { kind: "hurt", at: 1, victim: 1, from: [1, 0, 0], amount: 27 };
60 const theirs: Effect = { kind: "hurt", at: 1, victim: 9, from: [1, 0, 0], amount: 27 };
61 expect(cuesHeard([mine], 0, HERE, 1)).toEqual([{ kind: "hurt", from: [1, 0, 0], amount: 27 }]);
62 expect(cuesHeard([theirs], 0, HERE, 1)).toEqual([]);
63 });
64
65 test("distance is measured from wherever `listener` is passed as, not any fixed place", () => {
66 // Standing where a teammate's eyes are, spectating, rather than at the
67 // corpse: a footstep near the teammate is heard, the same one near the
68 // corpse position is not, because the corpse is not who is passed in.
69 const teammateEyes: Vec3 = [2000, 0, 0];
70 const step: Effect = { kind: "sound", at: 1, pos: [2000 + FOOTSTEP_EARSHOT - 1, 0, 0], name: "step" };
71 expect(cuesHeard([step], 0, HERE, 1)).toEqual([]);
72 expect(cuesHeard([step], 0, teammateEyes, 1)).toHaveLength(1);
73 });
74
75 test("a shot and a hitmark carry whatever audio.ts needs and nothing else", () => {
76 const shot: Effect = { kind: "shot", at: 1, pos: [10, 20, 30], weapon: "awp", mine: true, shooter: 1 };
77 const hit: Effect = { kind: "hitmark", at: 1, headshot: true };
78 expect(cuesHeard([shot], 0, HERE, 1)).toEqual([{ kind: "shot", pos: [10, 20, 30], weapon: "awp" }]);
79 expect(cuesHeard([hit], 0, HERE, 1)).toEqual([{ kind: "hit", headshot: true }]);
80 });
src/client/earshot.ts added +68
@@ -0,0 +1,68 @@
1 /**
2 * What the ear is allowed to hear: which of a tick's effects turn into a cue
3 * for `audio.ts` and which don't. Plain data in — the world's effects, the
4 * time before which everything has already been played, and where the
5 * listener stands — and plain cues out, one per thing that should be heard,
6 * carrying only what `audio.ts` needs to play it: position, name, weapon,
7 * pitch, amount. No Web Audio, no DOM, the shape `ears.ts` already set.
8 *
9 * `hearing.ts` is a bot's ears, wired for a node graph a rifle can turn its
10 * head toward across the whole map. This is a person's: a handful of fixed
11 * ceilings and a straight-line distance, because a human never had a node
12 * graph to look things up in — a beep behind a wall is still a beep.
13 */
14
15 import type { Effect, SoundName } from "./world.ts";
16 import { dist, type Vec3 } from "./math.ts";
17 import type { WorldSoundName } from "./audio.ts";
18 import type { WeaponId } from "./weapons.ts";
19
20 // A footstep further than this is not worth a node graph — about the width
21 // of a couple of site rooms in de_forge, not the whole map. A dry trigger
22 // carries about the same: it is the gun in your hands, or an enemy's right
23 // in front of you. A plant or a defuse is louder and more deliberate, heard
24 // well past the site but not the whole map. The bomb's own beep and the
25 // blast that ends it have no entry here at all: they carry everywhere, so a
26 // CT can walk to the site — or learn it just went off — by ear alone.
27 export const FOOTSTEP_EARSHOT = 1500;
28 export const EMPTY_EARSHOT = 900;
29 export const SITE_EARSHOT = 3000;
30
31 const EARSHOT: Partial<Record<SoundName, number>> = {
32 step: FOOTSTEP_EARSHOT, empty: EMPTY_EARSHOT, plant: SITE_EARSHOT, defuse: SITE_EARSHOT,
33 };
34
35 /** One thing worth playing: exactly what `audio.ts` and `flinch` need, and nothing else. */
36 export type Cue =
37 | { kind: "shot"; pos: Vec3; weapon: WeaponId }
38 | { kind: "hit"; headshot: boolean }
39 | { kind: "hurt"; from: Vec3; amount: number }
40 | { kind: "step"; pos: Vec3 }
41 | { kind: "world"; name: WorldSoundName; pos: Vec3; pitch?: number };
42
43 /**
44 * The cues a listener at `listener` picks up out of `effects`, having
45 * already heard everything at or before `since`. `localId` is whose pain
46 * counts: a `hurt` effect is only ever the local player's, whoever the
47 * camera is watching while they are dead.
48 */
49 export function cuesHeard(effects: Effect[], since: number, listener: Vec3, localId: number): Cue[] {
50 const cues: Cue[] = [];
51 for (const effect of effects) {
52 if (effect.at <= since) continue;
53 if (effect.kind === "shot") cues.push({ kind: "shot", pos: effect.pos, weapon: effect.weapon });
54 else if (effect.kind === "hitmark") cues.push({ kind: "hit", headshot: effect.headshot });
55 else if (effect.kind === "hurt") {
56 if (effect.victim === localId) cues.push({ kind: "hurt", from: effect.from, amount: effect.amount });
57 } else if (effect.kind === "sound") {
58 const limit = EARSHOT[effect.name];
59 if (limit !== undefined && dist(effect.pos, listener) > limit) continue;
60 cues.push(
61 effect.name === "step"
62 ? { kind: "step", pos: effect.pos }
63 : { kind: "world", name: effect.name, pos: effect.pos, pitch: effect.pitch },
64 );
65 }
66 }
67 return cues;
68 }
src/client/main.ts +14 −32
@@ -11,11 +11,12 @@
1111 import { createAudio } from "./audio.ts";
1212 import { ACTION_LABELS, decideBind, labelForCode } from "./bind.ts";
1313 import { buyMenu, type BuyRow } from "./buy.ts";
14 import { cuesHeard } from "./earshot.ts";
1415 import { attachFullscreen } from "./fullscreen.ts";
1516 import { drawHud } from "./hud.ts";
1617 import { ACTIONS, attachButtons, createControls, DEFAULT_BINDINGS, type Action } from "./input.ts";
1718 import { advance, TICK, waiting } from "./loop.ts";
18 import { clamp, dist } from "./math.ts";
19 import { clamp } from "./math.ts";
1920 import { COMPETITIVE, SHORT, type Rules } from "./match.ts";
2021 import { MAX_SCALE, nextRenderScale } from "./quality.ts";
2122 import { createRenderer, type Camera } from "./renderer.ts";
@@ -28,7 +29,7 @@ import { nextWatched, watched } from "./spectate.ts";
2829 import { summarize, type MatchSummary } from "./summary.ts";
2930 import {
3031 buy, canBuy, createWorld, DIFFICULTIES, eyePos, flinch, radioCall, setDifficulty, update,
31 type Difficulty, type Player, type SoundName, type World,
32 type Difficulty, type Player, type World,
3233 } from "./world.ts";
3334
3435 /**
@@ -647,38 +648,19 @@ function boot(): void {
647648 const saved = localStorage.getItem("cs.name");
648649 if (saved !== null && nameField.value === "") nameField.value = saved;
649650
650 // A footstep further than this is not worth a node graph — about the width
651 // of a couple of site rooms in de_forge, not the whole map. A dry trigger
652 // carries about the same: it is the gun in your hands, or an enemy's right
653 // in front of you. A plant or a defuse is louder and more deliberate, heard
654 // well past the site but not the whole map. The bomb's own beep and the
655 // blast that ends it have no entry here at all: they carry everywhere, so a
656 // CT can walk to the site — or learn it just went off — by ear alone.
657 const FOOTSTEP_EARSHOT = 1500;
658 const EMPTY_EARSHOT = 900;
659 const SITE_EARSHOT = 3000;
660 const EARSHOT: Partial<Record<SoundName, number>> = {
661 step: FOOTSTEP_EARSHOT, empty: EMPTY_EARSHOT, plant: SITE_EARSHOT, defuse: SITE_EARSHOT,
662 };
663
664651 // --- sound and flinch for whatever happened in the ticks we just ran
665652 function feelEffects(current: World, ears: Player): void {
666 for (const effect of current.effects) {
667 if (effect.at <= lastSound) continue;
668 // Sound comes from where the camera is: while dead that is a teammate's
669 // head, not the corpse the shots are no longer near.
670 const at = eyePos(ears);
671 if (effect.kind === "shot") audio.shot(effect.weapon, effect.pos, at, ears.yaw);
672 else if (effect.kind === "hitmark") audio.hit(effect.headshot);
673 else if (effect.kind === "hurt" && effect.victim === current.me.id) {
674 flinch(current.me, effect.from, effect.amount);
675 audio.pain(effect.amount);
676 } else if (effect.kind === "sound") {
677 const limit = EARSHOT[effect.name];
678 if (limit !== undefined && dist(effect.pos, at) > limit) continue;
679 if (effect.name === "step") audio.step(effect.pos, at, ears.yaw);
680 else audio.world(effect.name, effect.pos, at, ears.yaw, effect.pitch);
681 }
653 // Sound comes from where the camera is: while dead that is a teammate's
654 // head, not the corpse the shots are no longer near.
655 const at = eyePos(ears);
656 for (const cue of cuesHeard(current.effects, lastSound, at, current.me.id)) {
657 if (cue.kind === "shot") audio.shot(cue.weapon, cue.pos, at, ears.yaw);
658 else if (cue.kind === "hit") audio.hit(cue.headshot);
659 else if (cue.kind === "hurt") {
660 flinch(current.me, cue.from, cue.amount);
661 audio.pain(cue.amount);
662 } else if (cue.kind === "step") audio.step(cue.pos, at, ears.yaw);
663 else audio.world(cue.name, cue.pos, at, ears.yaw, cue.pitch);
682664 }
683665 lastSound = current.time;
684666 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs