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

A bomb you can hear: a beep that quickens, a plant, a defuse, an explosion and the click of an empty gun #57

Merged opened this

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

Merged

scout/cs:scout/44-a-bomb-you-can-hear-a-beep-that-quickens → main 2 commits fork 1 thread · 1 unresolved

  • Merged. scout/44-a-bomb-you-can-hear-a-beep-that-quickens was merged into main as f902fa8.

Review

Sign in to review this change.
7 files changed +326 −87
README.md +9 −3
@@ -70,9 +70,15 @@ wants one, but the running server never reads it.
7070 view and fades with distance — a shot or a footstep to your left plays from
7171 the left channel, to your right the right — and a running player who is not
7272 walking, ducking or airborne leaves footsteps this way too, so someone
73 closing in from behind is something you hear before you see. The
74 leaderboard is a real SQLite table under `FORGE_APP_DATA`, also served as
75 plain JSON from `GET /api/leaderboard` for the terminal or any other client.
73 closing in from behind is something you hear before you see. A planted bomb
74 beeps: about once a second right after the plant, quickening to a few a
75 second — and rising in pitch — as the fuse runs out, so a CT can walk to the
76 site by ear with no map open. The plant, the defuse, the detonation and a
77 trigger pulled on an empty magazine each have their own sound too. A
78 footstep only carries so far, but the bomb's beep and the blast that ends it
79 carry across the whole map. The leaderboard is a real SQLite table under
80 `FORGE_APP_DATA`, also served as plain JSON from `GET /api/leaderboard` for
81 the terminal or any other client.
7682 - **Generated, not licensed.** The map (`de_forge`), the textures, the player and
7783 weapon models and every sound are produced in code at start-up. There are no
7884 asset files and nothing from the real game is used — the numbers follow CS's
src/client/audio.ts +102 −70
@@ -7,17 +7,25 @@
77 import { ears } from "./ears.ts";
88 import type { Vec3 } from "./math.ts";
99 import { WEAPONS, type WeaponId } from "./weapons.ts";
10 import type { SoundName } from "./world.ts";
11
12 /** Every named world sound `world()` below knows how to play — everything but a footstep, which keeps its own dedicated `step`. */
13 export type WorldSoundName = Exclude<SoundName, "step">;
1014
1115 export type Audio = {
1216 shot(weapon: WeaponId, at: Vec3, listener: Vec3, listenerYaw: number): void;
1317 step(at: Vec3, listener: Vec3, listenerYaw: number): void;
14 /** A grenade hitting something: a short hard tick, panned like everything else. */
15 bounce(at: Vec3, listener: Vec3, listenerYaw: number): void;
16 /** A grenade going off: a low boom under a long crack of noise. */
17 blast(at: Vec3, listener: Vec3, listenerYaw: number): void;
1818 hit(headshot: boolean): void;
1919 pain(amount: number): void;
2020 beep(pitch: number, gain?: number): void;
21 /**
22 * One named world sound — the bomb's own beep, a plant, a defuse, the
23 * blast that ends it, a grenade's bounce and its own blast, a dry trigger
24 * — panned and attenuated through `ears` exactly like `shot` and `step`.
25 * Each name has its own timbre; `pitch` overrides the default for
26 * `"beep"`, which climbs as the fuse runs down.
27 */
28 world(name: WorldSoundName, at: Vec3, listener: Vec3, listenerYaw: number, pitch?: number): void;
2129 resume(): void;
2230 /** Mute is silence at the one node everything goes through, not a per-sound skip. */
2331 setMuted(muted: boolean): void;
@@ -33,6 +41,17 @@ const TONE: Record<string, { cut: number; thump: number; length: number }> = {
3341 knife: { cut: 4200, thump: 400, length: 0.08 },
3442 };
3543
44 /** One tone per named world sound: a square blip for the bomb's own beep,
45 * two-note chirps for the plant and the defuse, a click for a dry trigger.
46 * `explode`, `blast` and `bounce` are noise, not a tone, and have no entry
47 * here — `world()` below synthesises those directly. */
48 const WORLD_TONE: Partial<Record<WorldSoundName, { freq: number; type: OscillatorType; length: number }>> = {
49 beep: { freq: 900, type: "square", length: 0.08 },
50 plant: { freq: 420, type: "sine", length: 0.22 },
51 defuse: { freq: 900, type: "sine", length: 0.3 },
52 empty: { freq: 220, type: "square", length: 0.04 },
53 };
54
3655 type Live = { ctx: AudioContext; noise: AudioBuffer; master: GainNode };
3756
3857 export function createAudio(): Audio {
@@ -139,72 +158,6 @@ export function createAudio(): Audio {
139158 source.start(now);
140159 source.stop(now + length);
141160 },
142 /** A grenade off a wall: a tick of filtered noise, shorter than a footstep. */
143 bounce(at, listener, listenerYaw) {
144 if (muted) return;
145 const live = ensure();
146 if (live === null) return;
147 const { ctx: context, noise, master } = live;
148 const { pan, volume } = ears(at, listener, listenerYaw);
149 const now = context.currentTime;
150 const length = 0.07;
151 const source = context.createBufferSource();
152 source.buffer = noise;
153 source.playbackRate.value = 1.4 + Math.random() * 0.3;
154 const filter = context.createBiquadFilter();
155 filter.type = "bandpass";
156 filter.frequency.value = 1700;
157 filter.Q.value = 2.4;
158 const gain = context.createGain();
159 gain.gain.setValueAtTime(0.11 * volume, now);
160 gain.gain.exponentialRampToValueAtTime(0.0008, now + length);
161 const panner = context.createStereoPanner();
162 panner.pan.value = pan;
163 source.connect(filter).connect(gain).connect(panner).connect(master);
164 source.start(now);
165 source.stop(now + length);
166 },
167 /**
168 * The blast: a sine falling through two octaves for the punch in the
169 * chest, and a long low-passed tail of noise over it for the debris.
170 */
171 blast(at, listener, listenerYaw) {
172 if (muted) return;
173 const live = ensure();
174 if (live === null) return;
175 const { ctx: context, noise, master } = live;
176 const { pan, volume } = ears(at, listener, listenerYaw);
177 const now = context.currentTime;
178 const length = 0.8;
179 const panner = context.createStereoPanner();
180 panner.pan.value = pan;
181 panner.connect(master);
182
183 const boom = context.createOscillator();
184 boom.type = "sine";
185 boom.frequency.setValueAtTime(140, now);
186 boom.frequency.exponentialRampToValueAtTime(32, now + length * 0.7);
187 const boomGain = context.createGain();
188 boomGain.gain.setValueAtTime(0.5 * volume, now);
189 boomGain.gain.exponentialRampToValueAtTime(0.0008, now + length);
190 boom.connect(boomGain).connect(panner);
191 boom.start(now);
192 boom.stop(now + length);
193
194 const tail = context.createBufferSource();
195 tail.buffer = noise;
196 tail.playbackRate.value = 0.8;
197 const filter = context.createBiquadFilter();
198 filter.type = "lowpass";
199 filter.frequency.setValueAtTime(3200, now);
200 filter.frequency.exponentialRampToValueAtTime(220, now + length);
201 const tailGain = context.createGain();
202 tailGain.gain.setValueAtTime(0.4 * volume, now);
203 tailGain.gain.exponentialRampToValueAtTime(0.0008, now + length);
204 tail.connect(filter).connect(tailGain).connect(panner);
205 tail.start(now);
206 tail.stop(now + length);
207 },
208161 hit(headshot) {
209162 audio.beep(headshot ? 1400 : 900, 0.08);
210163 },
@@ -271,6 +224,85 @@ export function createAudio(): Audio {
271224 osc.start(now);
272225 osc.stop(now + 0.1);
273226 },
227 world(name, at, listener, listenerYaw, pitch) {
228 if (muted) return;
229 const live = ensure();
230 if (live === null) return;
231 const { ctx: context, noise, master } = live;
232 const { pan, volume } = ears(at, listener, listenerYaw);
233 const now = context.currentTime;
234 const panner = context.createStereoPanner();
235 panner.pan.value = pan;
236 panner.connect(master);
237
238 // An explosion, whichever set it off: a sine falling through octaves
239 // for the punch in the chest, and a long low-passed tail of noise over
240 // it for the debris. The bomb and a thrown HE are the same event to an
241 // ear a hundred units away, so they are the same code.
242 if (name === "explode" || name === "blast") {
243 const length = 0.9;
244 const tail = context.createBufferSource();
245 tail.buffer = noise;
246 tail.playbackRate.value = 0.35;
247 const filter = context.createBiquadFilter();
248 filter.type = "lowpass";
249 filter.frequency.setValueAtTime(3200, now);
250 filter.frequency.exponentialRampToValueAtTime(220, now + length);
251 const tailGain = context.createGain();
252 tailGain.gain.setValueAtTime(0.45 * volume, now);
253 tailGain.gain.exponentialRampToValueAtTime(0.0008, now + length);
254 tail.connect(filter).connect(tailGain).connect(panner);
255 tail.start(now);
256 tail.stop(now + length);
257
258 const boom = context.createOscillator();
259 boom.type = "sine";
260 boom.frequency.setValueAtTime(140, now);
261 boom.frequency.exponentialRampToValueAtTime(30, now + length * 0.7);
262 const boomGain = context.createGain();
263 boomGain.gain.setValueAtTime(0.5 * volume, now);
264 boomGain.gain.exponentialRampToValueAtTime(0.0008, now + length);
265 boom.connect(boomGain).connect(panner);
266 boom.start(now);
267 boom.stop(now + length);
268 return;
269 }
270
271 // A grenade off a wall: a tick of filtered noise, shorter than a footstep.
272 if (name === "bounce") {
273 const length = 0.07;
274 const source = context.createBufferSource();
275 source.buffer = noise;
276 source.playbackRate.value = 1.4 + Math.random() * 0.3;
277 const filter = context.createBiquadFilter();
278 filter.type = "bandpass";
279 filter.frequency.value = 1700;
280 filter.Q.value = 2.4;
281 const gain = context.createGain();
282 gain.gain.setValueAtTime(0.11 * volume, now);
283 gain.gain.exponentialRampToValueAtTime(0.0008, now + length);
284 source.connect(filter).connect(gain).connect(panner);
285 source.start(now);
286 source.stop(now + length);
287 return;
288 }
289
290 const tone = WORLD_TONE[name];
291 if (tone === undefined) return;
292 const osc = context.createOscillator();
293 osc.type = tone.type;
294 const start = pitch ?? tone.freq;
295 osc.frequency.setValueAtTime(start, now);
296 // A plant climbs like it is arming; a defuse settles like it is done.
297 if (name === "plant") osc.frequency.exponentialRampToValueAtTime(start * 1.7, now + tone.length);
298 else if (name === "defuse") osc.frequency.exponentialRampToValueAtTime(start * 0.7, now + tone.length);
299 const gain = context.createGain();
300 gain.gain.setValueAtTime((name === "empty" ? 0.1 : 0.16) * volume, now);
301 gain.gain.exponentialRampToValueAtTime(0.0005, now + tone.length);
302 osc.connect(gain).connect(panner);
303 osc.start(now);
304 osc.stop(now + tone.length + 0.02);
305 },
274306 };
275307 return audio;
276308 }
src/client/cues.test.ts added +58
@@ -0,0 +1,58 @@
1 import { expect, test } from "bun:test";
2 import { beepInterval, beepPitch, stepBeep } from "./cues.ts";
3
4 const BOMB_TIME = 40; // COMPETITIVE.bombTime and SHORT.bombTime alike
5
6 test("the first beep lands right after the plant", () => {
7 const dt = 1 / 64;
8 let sinceLast = 0;
9 let elapsed = 0;
10 let pitch: number | null = null;
11 while (pitch === null && elapsed < 5) {
12 const step = stepBeep(sinceLast, BOMB_TIME, BOMB_TIME, dt);
13 sinceLast = step.sinceLast;
14 pitch = step.pitch;
15 elapsed += dt;
16 }
17 expect(pitch).not.toBeNull();
18 expect(elapsed).toBeGreaterThan(0.8);
19 expect(elapsed).toBeLessThan(1.2);
20 });
21
22 test("intervals are monotonically shorter as the fuse runs down", () => {
23 const points = [40, 30, 20, 10, 5, 1, 0];
24 for (let i = 1; i < points.length; i++) {
25 expect(beepInterval(points[i], BOMB_TIME)).toBeLessThanOrEqual(beepInterval(points[i - 1], BOMB_TIME));
26 }
27 expect(beepInterval(0, BOMB_TIME)).toBeLessThan(beepInterval(40, BOMB_TIME));
28 });
29
30 test("the pitch rises as the fuse runs down", () => {
31 const points = [40, 30, 20, 10, 5, 1, 0];
32 for (let i = 1; i < points.length; i++) {
33 expect(beepPitch(points[i], BOMB_TIME)).toBeGreaterThanOrEqual(beepPitch(points[i - 1], BOMB_TIME));
34 }
35 expect(beepPitch(0, BOMB_TIME)).toBeGreaterThan(beepPitch(40, BOMB_TIME));
36 });
37
38 test("no beep when nothing is planted", () => {
39 const step = stepBeep(0.99, null, BOMB_TIME, 1);
40 expect(step.pitch).toBeNull();
41 expect(step.sinceLast).toBe(0);
42 });
43
44 test("the rate is bounded however small dt is", () => {
45 const duration = 1;
46 const secondsLeft = 0; // the fastest cadence there is
47 const dt = 1 / 20000;
48 let sinceLast = 0;
49 let beeps = 0;
50 for (let t = 0; t < duration; t += dt) {
51 const step = stepBeep(sinceLast, secondsLeft, BOMB_TIME, dt);
52 sinceLast = step.sinceLast;
53 if (step.pitch !== null) beeps += 1;
54 }
55 const interval = beepInterval(secondsLeft, BOMB_TIME);
56 expect(beeps).toBeGreaterThan(0);
57 expect(beeps).toBeLessThanOrEqual(Math.ceil(duration / interval) + 1);
58 });
src/client/cues.ts added +42
@@ -0,0 +1,42 @@
1 /**
2 * When a planted bomb beeps: pure arithmetic over the seconds left on the
3 * fuse and how long it has been since the last beep. No audio, no world —
4 * `world.ts` calls this to decide whether to push a sound this tick, pitch
5 * and all, so `main.ts` only ever plays what it is told.
6 */
7
8 import { clamp } from "./math.ts";
9
10 const MAX_INTERVAL = 1; // about a second apart just after the plant
11 const MIN_INTERVAL = 0.2; // a few a second in the last moments
12 const MIN_PITCH = 500;
13 const MAX_PITCH = 1200;
14
15 /** Seconds between beeps with this many seconds left on a `bombTime`-second fuse. */
16 export function beepInterval(secondsLeft: number, bombTime: number): number {
17 const t = clamp(secondsLeft / bombTime, 0, 1);
18 return MIN_INTERVAL + (MAX_INTERVAL - MIN_INTERVAL) * t;
19 }
20
21 /** The beep's pitch with this many seconds left: rising as it quickens. */
22 export function beepPitch(secondsLeft: number, bombTime: number): number {
23 const t = clamp(secondsLeft / bombTime, 0, 1);
24 return MAX_PITCH - (MAX_PITCH - MIN_PITCH) * t;
25 }
26
27 export type BeepStep = { sinceLast: number; pitch: number | null };
28
29 /**
30 * Advance the beep clock by `dt`. `secondsLeft` is null when nothing is
31 * planted, which silences it and resets the clock so the next plant starts
32 * clean. However small `dt` is, this fires at most the whole intervals it
33 * actually crossed and carries only the remainder forward — an accumulator,
34 * the same shape `footsteps` in `world.ts` already uses for distance.
35 */
36 export function stepBeep(sinceLast: number, secondsLeft: number | null, bombTime: number, dt: number): BeepStep {
37 if (secondsLeft === null) return { sinceLast: 0, pitch: null };
38 const next = sinceLast + dt;
39 const interval = beepInterval(secondsLeft, bombTime);
40 if (next < interval) return { sinceLast: next, pitch: null };
41 return { sinceLast: next - interval, pitch: beepPitch(secondsLeft, bombTime) };
42 }
src/client/main.ts +16 −7
@@ -19,7 +19,7 @@ import { summarize, type MatchSummary } from "./summary.ts";
1919 import { buyable } from "./weapons.ts";
2020 import {
2121 buy, canBuy, canBuyItem, createWorld, eyePos, flinch, priceOf, update,
22 type BuyItem, type Player, type World,
22 type BuyItem, type Player, type SoundName, type World,
2323 } from "./world.ts";
2424
2525 const TICK = 1 / 64;
@@ -290,8 +290,18 @@ function boot(): void {
290290 if (saved !== null && nameField.value === "") nameField.value = saved;
291291
292292 // A footstep further than this is not worth a node graph — about the width
293 // of a couple of site rooms in de_forge, not the whole map.
293 // of a couple of site rooms in de_forge, not the whole map. A dry trigger
294 // carries about the same: it is the gun in your hands, or an enemy's right
295 // in front of you. A plant or a defuse is louder and more deliberate, heard
296 // well past the site but not the whole map. The bomb's own beep and the
297 // blast that ends it have no entry here at all: they carry everywhere, so a
298 // CT can walk to the site — or learn it just went off — by ear alone.
294299 const FOOTSTEP_EARSHOT = 1500;
300 const EMPTY_EARSHOT = 900;
301 const SITE_EARSHOT = 3000;
302 const EARSHOT: Partial<Record<SoundName, number>> = {
303 step: FOOTSTEP_EARSHOT, empty: EMPTY_EARSHOT, plant: SITE_EARSHOT, defuse: SITE_EARSHOT,
304 };
295305
296306 // --- sound and flinch for whatever happened in the ticks we just ran
297307 function feelEffects(current: World, ears: Player): void {
@@ -306,11 +316,10 @@ function boot(): void {
306316 flinch(current.me, effect.from, effect.amount);
307317 audio.pain(effect.amount);
308318 } else if (effect.kind === "sound") {
309 // A footstep is a room away at the most; a grenade is heard across the
310 // map, which is the point of one landing behind you.
311 if (effect.name === "step" && dist(effect.pos, at) <= FOOTSTEP_EARSHOT) audio.step(effect.pos, at, ears.yaw);
312 else if (effect.name === "bounce") audio.bounce(effect.pos, at, ears.yaw);
313 else if (effect.name === "blast") audio.blast(effect.pos, at, ears.yaw);
319 const limit = EARSHOT[effect.name];
320 if (limit !== undefined && dist(effect.pos, at) > limit) continue;
321 if (effect.name === "step") audio.step(effect.pos, at, ears.yaw);
322 else audio.world(effect.name, effect.pos, at, ears.yaw, effect.pitch);
314323 }
315324 }
316325 lastSound = current.time;
src/client/world.test.ts +59 −2
@@ -13,6 +13,10 @@ const run = (world: World, seconds: number, input: Input = NO_INPUT): void => {
1313 for (let t = 0; t < seconds; t += TICK) update(world, TICK, input);
1414 };
1515
16 /** How many `{ kind: "sound" }` effects with this name are sitting in the world right now. */
17 const soundsNamed = (world: World, name: string): number =>
18 world.effects.filter((e) => e.kind === "sound" && e.name === name).length;
19
1620 test("a world starts ten players, one bomb and a freeze time", () => {
1721 const world = createWorld(SHORT, "me", "ct", 5, 1);
1822 expect(world.players.length).toBe(10);
@@ -95,12 +99,13 @@ test("the terrorists plant, and a planted bomb keeps the round alive past the cl
9599 expect(world.bomb.state).toBe("planted");
96100 expect(world.match.phase).toBe("planted");
97101 expect(world.me.money).toBeGreaterThan(0);
102 expect(soundsNamed(world, "plant")).toBe(1);
98103 });
99104
100105 test("a defusing CT wins the round, and the money follows the rules", () => {
101106 const world = createWorld(SHORT, "me", "ct", 5, 13);
102107 run(world, SHORT.freezeTime + 0.2);
103 world.bomb = { state: "planted", pos: [sites.B[0], sites.B[1], 2], site: "B", plantProgress: 0, defuseProgress: 0, defuser: null };
108 world.bomb = { state: "planted", pos: [sites.B[0], sites.B[1], 2], site: "B", plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 };
104109 world.bombPlanted = true;
105110 world.match.phase = "planted";
106111 world.match.timer = 30;
@@ -112,6 +117,7 @@ test("a defusing CT wins the round, and the money follows the rules", () => {
112117 expect(world.match.winner).toBe("ct");
113118 expect(world.match.reason).toBe("defused");
114119 expect(world.me.money).toBe(before + 300 + 3500);
120 expect(soundsNamed(world, "defuse")).toBe(1);
115121 });
116122
117123 test("a new round puts everyone back with full health and a full magazine", () => {
@@ -339,7 +345,7 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", (
339345 const far = world.players[1];
340346 world.bomb = {
341347 state: "planted", pos: [sites.A[0], sites.A[1], 2], site: "A",
342 plantProgress: 0, defuseProgress: 0, defuser: null,
348 plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0,
343349 };
344350 world.bombPlanted = true;
345351 world.match.phase = "planted";
@@ -363,6 +369,57 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", (
363369 expect(hurts[0].from).toEqual([sites.A[0], sites.A[1], 2]);
364370 });
365371
372 test("the detonation pushes exactly one explode", () => {
373 const world = createWorld(SHORT, "me", "ct", 5, 42);
374 world.bomb = {
375 state: "planted", pos: [sites.A[0], sites.A[1], 2], site: "A",
376 plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0,
377 };
378 world.bombPlanted = true;
379 world.match.phase = "planted";
380 world.match.timer = TICK / 2;
381 for (const p of world.players) p.alive = false;
382 world.effects.length = 0;
383 update(world, TICK, NO_INPUT);
384 expect(world.bomb.state).toBe("detonated");
385 expect(soundsNamed(world, "explode")).toBe(1);
386 });
387
388 test("a planted bomb pushes beeps on that cadence, and a defuse stops them", () => {
389 const world = createWorld(SHORT, "me", "ct", 5, 71);
390 run(world, SHORT.freezeTime + 0.2);
391 world.bomb = {
392 state: "planted", pos: [sites.B[0], sites.B[1], 2], site: "B",
393 plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0,
394 };
395 world.bombPlanted = true;
396 world.match.phase = "planted";
397 world.match.timer = 40;
398 world.me.pos = [sites.B[0], sites.B[1], 2];
399 world.me.defuseKit = true;
400 world.effects.length = 0;
401 run(world, 1.05); // just past the first beep interval at a fresh 40s
402 expect(soundsNamed(world, "beep")).toBeGreaterThanOrEqual(1);
403
404 run(world, SHORT.defuseTimeWithKit + 0.3, { ...NO_INPUT, use: true });
405 expect(world.bomb.state).toBe("defused");
406 world.effects.length = 0;
407 run(world, 1.05);
408 expect(soundsNamed(world, "beep")).toBe(0);
409 });
410
411 test("a dry trigger pushes one empty per press, not one per tick", () => {
412 const world = createWorld(SHORT, "me", "ct", 5, 83);
413 const shooter = world.players.find((p) => p.team === "t") as Player;
414 faceOff(world, shooter);
415 world.me.primary = "m4a4";
416 world.me.weapon = "m4a4";
417 world.me.ammo.m4a4 = { mag: 0, reserve: 0 };
418 world.effects.length = 0;
419 for (let i = 0; i < 10; i++) shoot(world, world.me); // holding the trigger for ten ticks
420 expect(soundsNamed(world, "empty")).toBe(1);
421 });
422
366423 test("a hit flinches the view away from where it came from, and stays bounded", () => {
367424 const world = createWorld(SHORT, "me", "ct", 5, 43);
368425 const me = world.me;
src/client/world.ts +40 −5
@@ -9,6 +9,7 @@
99
1010 import { addBody, type Corpse } from "./bodies.ts";
1111 import { applyHit, traceBullet, type Actor } from "./combat.ts";
12 import { stepBeep } from "./cues.ts";
1213 import {
1314 blastDamage, GRENADE, step as stepGrenade, thrown, type Grenade,
1415 } from "./grenades.ts";
@@ -47,6 +48,7 @@ export type Player = Actor & Body & {
4748 lastShotAt: number;
4849 scoped: boolean;
4950 firedThisPress: boolean;
51 emptyThisPress: boolean; // the dry-fire click, latched the same way firedThisPress is
5052 punch: [number, number]; // view kick, decaying
5153 nextPickupAt: number; // no swapping guns again until this, so E cannot flip-flop
5254 kills: number;
@@ -64,6 +66,7 @@ export type Bomb = {
6466 plantProgress: number;
6567 defuseProgress: number;
6668 defuser: Player | null;
69 beepAt: number; // seconds since the last beep, an accumulator like footsteps'
6770 };
6871
6972 /** A gun lying on the floor: what it is, where it fell, and what was left in it. */
@@ -73,6 +76,9 @@ export type Pickup = {
7376 ammo: Ammo;
7477 };
7578
79 /** Every name a `{ kind: "sound" }` effect can carry — the vocabulary `audio.ts` plays. */
80 export type SoundName = "step" | "beep" | "plant" | "defuse" | "explode" | "empty" | "bounce" | "blast";
81
7682 export type Effect =
7783 | { kind: "tracer"; from: Vec3; to: Vec3; at: number }
7884 | { kind: "impact"; at: number; pos: Vec3; normal: Vec3 }
@@ -82,7 +88,9 @@ export type Effect =
8288 // Damage the person took: who took it, where it came from in the world, and
8389 // how much. Only ever pushed for the person — a bot being shot is silent.
8490 | { kind: "hurt"; at: number; victim: number; from: Vec3; amount: number }
85 | { kind: "sound"; at: number; pos: Vec3; name: string };
91 // `pitch` is only ever set on a "beep": it is the one sound whose tone is
92 // decided at the moment it happens (how close the fuse is), not by its name.
93 | { kind: "sound"; at: number; pos: Vec3; name: SoundName; pitch?: number };
8694
8795 // The ids are here as well as the names because the HUD asks the feed who
8896 // killed the person, and two players may answer to the same name.
@@ -155,7 +163,7 @@ function makePlayer(id: number, team: Team, name: string, bot: boolean): Player
155163 ammo: { knife: { mag: 0, reserve: 0 }, [secondary]: emptyAmmo(secondary) },
156164 grenades: 0,
157165 defuseKit: false, hasBomb: false,
158 nextShotAt: 0, reloadUntil: 0, shotIndex: 0, lastShotAt: -99, scoped: false, firedThisPress: false,
166 nextShotAt: 0, reloadUntil: 0, shotIndex: 0, lastShotAt: -99, scoped: false, firedThisPress: false, emptyThisPress: false,
159167 punch: [0, 0], nextPickupAt: 0,
160168 kills: 0, deaths: 0, headshots: 0, damage: 0, spawnAt: 0, stepDistance: 0,
161169 };
@@ -187,7 +195,7 @@ export function createWorld(
187195 match, players, me, time: 0,
188196 seed: seed >>> 0,
189197 plan: roundPlan(seed, match.round),
190 bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null },
198 bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 },
191199 pickups: [], grenades: [], bodies: [],
192200 effects: [], feed: [], messages: [],
193201 random, boxes: map.boxes, bombPlanted: false, pendingBuy: false,
@@ -245,7 +253,7 @@ export function startRound(world: World): void {
245253 const carrier = ts[Math.floor(world.random() * ts.length)];
246254 if (carrier !== undefined) {
247255 carrier.hasBomb = true;
248 world.bomb = { state: "carried", pos: [...carrier.pos] as Vec3, site: null, plantProgress: 0, defuseProgress: 0, defuser: null };
256 world.bomb = { state: "carried", pos: [...carrier.pos] as Vec3, site: null, plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 };
249257 }
250258 world.bombPlanted = false;
251259 world.pickups.length = 0;
@@ -419,6 +427,10 @@ export function shoot(world: World, p: Player): boolean {
419427 if (w.kind === "grenade") return throwGrenade(world, p);
420428 const ammo = p.ammo[w.id];
421429 if (w.magazine > 0 && (ammo === undefined || ammo.mag <= 0)) {
430 if (!p.emptyThisPress) {
431 p.emptyThisPress = true;
432 world.effects.push({ kind: "sound", at: world.time, pos: eyePos(p), name: "empty" });
433 }
422434 reload(world, p);
423435 return false;
424436 }
@@ -697,6 +709,8 @@ function updateBomb(world: World, p: Player, input: Input, dt: number): void {
697709 world.bombPlanted = true;
698710 p.money = rules.capMoney(p.money + rules.PLANT_REWARD, world.match.rules);
699711 rules.plantBomb(world.match);
712 bomb.beepAt = 0;
713 world.effects.push({ kind: "sound", at: world.time, pos: bomb.pos, name: "plant" });
700714 say(world, `The bomb has been planted at ${site}.`);
701715 }
702716 } else {
@@ -716,6 +730,7 @@ function updateBomb(world: World, p: Player, input: Input, dt: number): void {
716730 if (bomb.defuseProgress >= need) {
717731 bomb.state = "defused";
718732 p.money = rules.capMoney(p.money + rules.DEFUSE_REWARD, world.match.rules);
733 world.effects.push({ kind: "sound", at: world.time, pos: bomb.pos, name: "defuse" });
719734 finishRound(world, "ct", "defused");
720735 }
721736 } else if (bomb.defuser === p) {
@@ -725,6 +740,21 @@ function updateBomb(world: World, p: Player, input: Input, dt: number): void {
725740 }
726741 }
727742
743 /**
744 * The bomb's own beep, once a tick and not once per player: `updateBomb`
745 * above runs once per player and only ever changes state for the one whose
746 * action it is, but the beep belongs to the bomb, not to anyone standing
747 * near it, so it is stepped once from `update` instead.
748 */
749 function updateBombBeep(world: World, dt: number): void {
750 const secondsLeft = world.match.phase === "planted" ? world.match.timer : null;
751 const step = stepBeep(world.bomb.beepAt, secondsLeft, world.match.rules.bombTime, dt);
752 world.bomb.beepAt = step.sinceLast;
753 if (step.pitch !== null) {
754 world.effects.push({ kind: "sound", at: world.time, pos: world.bomb.pos, name: "beep", pitch: step.pitch });
755 }
756 }
757
728758 /** How far along the thing this player is doing is, 0..1, for the HUD bar. */
729759 export function useProgress(world: World, p: Player): { label: string; fraction: number } | null {
730760 const bomb = world.bomb;
@@ -800,7 +830,10 @@ export function applyInput(world: World, p: Player, input: Input, dt: number): v
800830 if (shoot(world, p)) p.firedThisPress = true;
801831 }
802832 }
803 if (!input.fire) p.firedThisPress = false;
833 if (!input.fire) {
834 p.firedThisPress = false;
835 p.emptyThisPress = false;
836 }
804837 if (world.time - p.lastShotAt > 0.35) p.shotIndex = 0;
805838 p.punch = [p.punch[0] * Math.pow(0.02, dt), p.punch[1] * Math.pow(0.02, dt)];
806839 updateBomb(world, p, input, dt);
@@ -815,6 +848,7 @@ export function update(world: World, dt: number, input: Input): void {
815848 if (event.type === "round_start") say(world, "Go go go!");
816849 if (event.type === "bomb_timeout") {
817850 world.bomb.state = "detonated";
851 world.effects.push({ kind: "sound", at: world.time, pos: world.bomb.pos, name: "explode" });
818852 for (const p of world.players) {
819853 if (dist(p.pos, world.bomb.pos) < BOMB_RADIUS && p.alive) {
820854 hurt(world, p, world.bomb.pos, p.health);
@@ -846,6 +880,7 @@ export function update(world: World, dt: number, input: Input): void {
846880 if (event.type === "round_end") announce(world, event.winner, event.reason);
847881 }
848882 if (world.match.phase === "freeze" && before === "over") startRound(world);
883 updateBombBeep(world, dt);
849884
850885 updateGrenades(world, dt);
851886 applyInput(world, world.me, world.me.alive ? input : NO_INPUT, dt);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs