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

The bomb's blast: a lethal core, falloff, armour, and a death the kill feed can name #66

Merged opened this

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

Merged

scout/cs:scout/49-the-bomb-s-blast-a-lethal-core-falloff-a → main 1 commit fork

  • Merged. scout/49-the-bomb-s-blast-a-lethal-core-falloff-a was merged into main as 42c7b16.

Review

Sign in to review this change.
9 files changed +186 −31
README.md +9 −2
@@ -74,8 +74,15 @@ wants one, but the running server never reads it.
7474 beeps: about once a second right after the plant, quickening to a few a
7575 second — and rising in pitch — as the fuse runs out, so a CT can walk to the
7676 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
77 trigger pulled on an empty magazine each have their own sound too. Going off
78 is the same blast rule the HE uses — falling linearly from full at the
79 centre to nothing at **900 units**, halved by armour — with its own, much
80 larger, centre damage: close enough in it kills through full kevlar, far
81 enough out you end the round alive and hurt. A wall does nothing to stop
82 it — the bomb ignores cover in CS, and this does too — and nobody is
83 credited for the kill, the way blowing yourself up with your own grenade
84 never is.
85 A footstep only carries so far, but the bomb's beep and the blast that ends it
7986 carry across the whole map. The leaderboard is a real SQLite table under
8087 `FORGE_APP_DATA`, also served as plain JSON from `GET /api/leaderboard` for
8188 the terminal or any other client — with a `guest` flag per row instead of the
src/client/grenades.test.ts +20
@@ -3,6 +3,8 @@ import { blastDamage, blastFalloff, GRENADE, step, thrown, type Grenade } from "
33 import * as map from "./map.ts";
44 import type { Box } from "./map.ts";
55 import type { Vec3 } from "./math.ts";
6 import { BOMB_BLAST_RADIUS } from "./world.ts";
7 import { WEAPONS } from "./weapons.ts";
68
79 const TICK = 1 / 64;
810 const FLOOR: Box[] = [{ min: [-10000, -10000, -64], max: [10000, 10000, 0], mat: 0 }];
@@ -115,3 +117,21 @@ test("damage is full at the centre, nothing at the radius, and armour halves it"
115117 expect(armoured.health).toBeLessThan(centre * 0.6);
116118 expect(armoured.armor).toBeGreaterThan(0);
117119 });
120
121 test("the bomb's blast is the same function as the HE's, with its own radius and damage", () => {
122 const bomb = WEAPONS.bomb;
123 const at = (distance: number, armor: number) =>
124 blastDamage(distance, armor, BOMB_BLAST_RADIUS, bomb.damage, bomb.armorPenetration);
125
126 // A lethal core: full armour still leaves less than nothing at its centre.
127 const centre = at(0, 100);
128 expect(centre.health).toBeGreaterThanOrEqual(100);
129
130 // Falling to zero at the edge, same as the HE's.
131 expect(at(BOMB_BLAST_RADIUS, 0).health).toBe(0);
132 expect(at(BOMB_BLAST_RADIUS * 2, 0).health).toBe(0);
133
134 // Armour still takes its share in between.
135 const middle = BOMB_BLAST_RADIUS * 5 / 6;
136 expect(at(middle, 100).health).toBeLessThan(at(middle, 0).health);
137 });
src/client/grenades.ts +16 −8
@@ -129,17 +129,25 @@ export function thrown(
129129 };
130130 }
131131
132 /** The share of a blast something `distance` from its centre takes: 1 to 0. */
133 export const blastFalloff = (distance: number): number =>
134 clamp(1 - distance / GRENADE.blastRadius, 0, 1);
132 /** The share of a blast something `distance` from its `radius` takes: 1 to 0. */
133 export const blastFalloff = (distance: number, radius: number = GRENADE.blastRadius): number =>
134 clamp(1 - distance / radius, 0, 1);
135135
136136 /**
137137 * What a blast `distance` away costs someone wearing `armor`. Full at the
138 * centre, nothing at the radius, and armour takes its share the way it does
139 * off a bullet — a vest is worth about half an HE.
138 * centre, nothing at `radius`, and armour takes its share the way it does off
139 * a bullet — a vest is worth about half an HE. The bomb calls this too, with
140 * its own radius and centre damage in place of the HE's: one blast rule, not
141 * two, so `distance` is the only thing either caller has to get right.
140142 */
141 export function blastDamage(distance: number, armor: number): { health: number; armor: number } {
142 const damage = WEAPONS.he.damage * blastFalloff(distance);
143 export function blastDamage(
144 distance: number,
145 armor: number,
146 radius: number = GRENADE.blastRadius,
147 centreDamage: number = WEAPONS.he.damage,
148 penetration: number = WEAPONS.he.armorPenetration,
149 ): { health: number; armor: number } {
150 const damage = centreDamage * blastFalloff(distance, radius);
143151 if (armor <= 0 || damage <= 0) return { health: Math.round(damage), armor: 0 };
144 return armorSplit(damage, armor, WEAPONS.he.armorPenetration);
152 return armorSplit(damage, armor, penetration);
145153 }
src/client/hud.ts +2 −2
@@ -415,8 +415,8 @@ export function drawHud(
415415 }
416416
417417 // --- dead: who killed you, and whose eyes you are borrowing until the round
418 // is over. The kill feed is where the killer comes from, so a death nobody
419 // was credited with — the bomb — simply says nothing about one.
418 // is over. The kill feed is where the killer comes from; the bomb credits
419 // nobody, so it names you as your own killer there and this reads that way.
420420 if (!world.me.alive && !over) {
421421 const top = height * 0.38;
422422 ctx.textAlign = "center";
src/client/spectate.test.ts +1 −1
@@ -50,7 +50,7 @@ test("the teammate you were watching dying moves you to one who has not", () =>
5050 expect(watched(world, world.me.id)).toBe(world.me);
5151 });
5252
53 test("the kill feed says who killed you, and says nothing about a bomb", () => {
53 test("the kill feed says who killed you, and nothing when it has no line for you", () => {
5454 const world = dead();
5555 expect(deathNotice(world, world.me)).toBe(null);
5656 const bot = world.players[1] as Player;
src/client/spectate.ts +4 −3
@@ -31,9 +31,10 @@ export const watched = (world: World, watching: number): Player =>
3131 world.players.find((p) => p.id === watching) ?? world.me;
3232
3333 /**
34 * The kill-feed line that killed `player`, or null — the bomb kills nobody by
35 * name, and the feed only keeps the last few lines, so this can be nothing
36 * even for a corpse.
34 * The kill-feed line that killed `player`, or null — the feed only keeps the
35 * last few lines, so this can be nothing even for a corpse. The bomb credits
36 * nobody, so its line names the victim as their own killer, the same way a
37 * grenade you killed yourself with does.
3738 */
3839 export function deathNotice(world: World, player: Player): FeedEntry | null {
3940 for (let i = world.feed.length - 1; i >= 0; i -= 1) {
src/client/weapons.ts +7 −3
@@ -11,12 +11,12 @@
1111 import { rng } from "./math.ts";
1212
1313 export type WeaponId =
14 | "knife" | "glock" | "usp" | "deagle" | "mp9" | "ak47" | "m4a4" | "awp" | "he";
14 | "knife" | "glock" | "usp" | "deagle" | "mp9" | "ak47" | "m4a4" | "awp" | "he" | "bomb";
1515
1616 export type Weapon = {
1717 id: WeaponId;
1818 name: string;
19 kind: "knife" | "pistol" | "smg" | "rifle" | "sniper" | "grenade";
19 kind: "knife" | "pistol" | "smg" | "rifle" | "sniper" | "grenade" | "bomb";
2020 team: "t" | "ct" | "both";
2121 price: number;
2222 damage: number;
@@ -49,6 +49,10 @@ const WEAPON_LIST: Weapon[] = [
4949 // last because a weapon's spray pattern is seeded from its place in this
5050 // list, and every gun above it already has the pattern people have learned.
5151 { id: "he", name: "HE Grenade", kind: "grenade", team: "both", price: 300, damage: 98, armorPenetration: 0.5, rangeModifier: 1, rpm: 120, magazine: 0, reserve: 0, reloadTime: 0, killReward: 300, carrySpeed: 245, spread: 0, moveSpread: 0, recoil: 0, automatic: false },
52 // Never bought, never held — this exists so the kill feed and the blast
53 // that reads `damage`/`armorPenetration` have one row to point at, the same
54 // way every gun does. `buyable` and `canBuyItem` refuse it by kind.
55 { id: "bomb", name: "C4", kind: "bomb", team: "both", price: 0, damage: 500, armorPenetration: 0.5, rangeModifier: 1, rpm: 0, magazine: 0, reserve: 0, reloadTime: 0, killReward: 0, carrySpeed: 0, spread: 0, moveSpread: 0, recoil: 0, automatic: false },
5256 ];
5357
5458 export const WEAPONS: Record<WeaponId, Weapon> = Object.fromEntries(
@@ -56,7 +60,7 @@ export const WEAPONS: Record<WeaponId, Weapon> = Object.fromEntries(
5660 ) as Record<WeaponId, Weapon>;
5761
5862 export const buyable = (team: "t" | "ct"): Weapon[] =>
59 WEAPON_LIST.filter((w) => w.kind !== "knife" && (w.team === "both" || w.team === team));
63 WEAPON_LIST.filter((w) => w.kind !== "knife" && w.kind !== "bomb" && (w.team === "both" || w.team === team));
6064
6165 export const ARMOR_PRICE = 650;
6266 export const HELMET_PRICE = 350;
src/client/world.test.ts +102 −3
@@ -1,11 +1,11 @@
11 import { expect, test } from "bun:test";
22 import { GRENADE } from "./grenades.ts";
33 import { siteAt, sites } from "./map.ts";
4 import { SHORT } from "./match.ts";
4 import { SHORT, winReward } from "./match.ts";
55 import { WEAPONS } from "./weapons.ts";
66 import {
7 aimAt, applyInput, bodyCentre, buy, createWorld, currentWeapon, eyePos, flinch, NO_INPUT,
8 offeredPickup, shoot, startRound, update, type Input, type Player, type World,
7 aimAt, applyInput, bodyCentre, BOMB_BLAST_RADIUS, buy, createWorld, currentWeapon, eyePos, flinch,
8 NO_INPUT, offeredPickup, shoot, startRound, update, type Input, type Player, type World,
99 } from "./world.ts";
1010
1111 const TICK = 1 / 64;
@@ -354,6 +354,7 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", (
354354 p.pos = [sites.A[0], sites.A[1], 2];
355355 p.alive = true;
356356 p.health = 100;
357 p.hasBomb = false; // planted: nobody is still carrying it
357358 }
358359 far.pos = [sites.B[0], sites.B[1], 2];
359360 world.effects.length = 0;
@@ -385,6 +386,104 @@ test("the detonation pushes exactly one explode", () => {
385386 expect(soundsNamed(world, "explode")).toBe(1);
386387 });
387388
389 /** Put a planted bomb at `pos`, alone in the world but for `world.me`, and step once. */
390 function detonateAt(world: World, pos: [number, number, number]): void {
391 for (const p of world.players) { p.alive = p === world.me; p.hasBomb = false; }
392 world.bomb = { state: "planted", pos: [...pos], site: "A", plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 };
393 world.bombPlanted = true;
394 world.match.phase = "planted";
395 world.match.timer = TICK / 2;
396 world.effects.length = 0;
397 update(world, TICK, NO_INPUT);
398 }
399
400 test("a player standing on the bomb dies with it, however armoured", () => {
401 const world = createWorld(SHORT, "me", "ct", 5, 313);
402 world.me.pos = [0, 0, 2];
403 world.me.armor = 100;
404 detonateAt(world, [0, 0, 2]);
405 expect(world.me.alive).toBe(false);
406 });
407
408 test("the blast has fallen to nothing at its own edge", () => {
409 const world = createWorld(SHORT, "me", "ct", 5, 317);
410 world.me.pos = [BOMB_BLAST_RADIUS, 0, 2];
411 detonateAt(world, [0, 0, 2]);
412 expect(world.me.alive).toBe(true);
413 expect(world.me.health).toBe(100);
414 });
415
416 test("armour takes less of the bomb's blast than none at all", () => {
417 const middle = BOMB_BLAST_RADIUS * 5 / 6;
418
419 const bare = createWorld(SHORT, "me", "ct", 5, 319);
420 bare.me.pos = [middle, 0, 2];
421 detonateAt(bare, [0, 0, 2]);
422 const bareDamage = 100 - bare.me.health;
423
424 const armoured = createWorld(SHORT, "me", "ct", 5, 319);
425 armoured.me.pos = [middle, 0, 2];
426 armoured.me.armor = 100;
427 detonateAt(armoured, [0, 0, 2]);
428 const armouredDamage = 100 - armoured.me.health;
429
430 expect(bareDamage).toBeGreaterThan(0);
431 expect(armouredDamage).toBeGreaterThan(0);
432 expect(armouredDamage).toBeLessThan(bareDamage);
433 });
434
435 test("a wall does not save you from the bomb: cover is a bullet's rule, not this one's", () => {
436 const world = createWorld(SHORT, "me", "ct", 5, 331);
437 world.boxes = [{ min: [100, -400, -100], max: [140, 400, 400], mat: 0 }];
438 world.me.pos = [200, 0, 2];
439 detonateAt(world, [0, 0, 2]);
440 expect(world.me.alive).toBe(false);
441 });
442
443 test("a bomb kill runs the same path as any other: a hurt, a named feed line, a corpse", () => {
444 const world = createWorld(SHORT, "me", "ct", 5, 337);
445 world.me.pos = [0, 0, 2];
446 world.me.armor = 100;
447 expect(world.bodies.length).toBe(0);
448 detonateAt(world, [0, 0, 2]);
449 expect(world.me.alive).toBe(false);
450
451 const hurts = hurtsIn(world);
452 expect(hurts.length).toBe(1);
453 expect(hurts[0].victim).toBe(world.me.id);
454
455 const entry = world.feed[world.feed.length - 1];
456 expect(entry?.weapon).toBe("bomb");
457 expect(entry?.victimId).toBe(world.me.id);
458 // Nobody planted this for a kill: the victim stands in for the killer,
459 // the same way a suicide by your own grenade does.
460 expect(entry?.killerId).toBe(world.me.id);
461
462 expect(world.bodies.length).toBe(1);
463 });
464
465 test("the round's winner, reason and rewards from a detonation are unchanged", () => {
466 const world = createWorld(SHORT, "me", "ct", 5, 341);
467 const t = world.players.find((p) => p.team === "t") as Player;
468 const before = t.money;
469 world.me.pos = [0, 0, 2];
470 detonateAt(world, [0, 0, 2]);
471 expect(world.match.winner).toBe("t");
472 expect(world.match.reason).toBe("detonated");
473 expect(t.money).toBe(before + winReward("detonated"));
474 });
475
476 test("a survivor of the blast is still alive when the next round starts", () => {
477 const world = createWorld(SHORT, "me", "ct", 5, 347);
478 world.me.pos = [BOMB_BLAST_RADIUS, 0, 2];
479 detonateAt(world, [0, 0, 2]);
480 expect(world.me.alive).toBe(true);
481 run(world, SHORT.endDelay + TICK);
482 expect(world.match.phase).toBe("freeze");
483 expect(world.match.round).toBe(2);
484 expect(world.me.alive).toBe(true);
485 });
486
388487 test("a planted bomb pushes beeps on that cadence, and a defuse stops them", () => {
389488 const world = createWorld(SHORT, "me", "ct", 5, 71);
390489 run(world, SHORT.freezeTime + 0.2);
src/client/world.ts +25 −9
@@ -326,7 +326,7 @@ export function canBuyItem(world: World, player: Player, item: BuyItem): boolean
326326 if (item === "helmet") return !player.helmet;
327327 if (item === "kit") return player.team === "ct" && !player.defuseKit;
328328 const weapon = WEAPONS[item];
329 if (weapon === undefined || weapon.kind === "knife") return false;
329 if (weapon === undefined || weapon.kind === "knife" || weapon.kind === "bomb") return false;
330330 if (weapon.team !== "both" && weapon.team !== player.team) return false;
331331 if (weapon.kind === "grenade") return player.grenades < MAX_GRENADES;
332332 return true;
@@ -691,8 +691,14 @@ function updateGrenades(world: World, dt: number): void {
691691 // --- the bomb -------------------------------------------------------------
692692
693693 const PLANT_RANGE = 64;
694 /** Everyone this close to the bomb when it goes off dies with it. */
695 const BOMB_RADIUS = 1000;
694 /**
695 * Where the bomb's blast has fallen to nothing — `blastDamage` from
696 * `grenades.ts` with this radius and `WEAPONS.bomb`'s own damage and
697 * penetration, the same falloff the HE uses. Close enough (a few hundred
698 * units) it is lethal through full armour regardless; a wall does not stop
699 * it — the bomb ignores cover in CS, and this does too, on purpose.
700 */
701 export const BOMB_BLAST_RADIUS = 900;
696702
697703 function updateBomb(world: World, p: Player, input: Input, dt: number): void {
698704 const bomb = world.bomb;
@@ -850,13 +856,23 @@ export function update(world: World, dt: number, input: Input): void {
850856 world.bomb.state = "detonated";
851857 world.effects.push({ kind: "sound", at: world.time, pos: world.bomb.pos, name: "explode" });
852858 for (const p of world.players) {
853 if (dist(p.pos, world.bomb.pos) < BOMB_RADIUS && p.alive) {
854 hurt(world, p, world.bomb.pos, p.health);
855 p.alive = false;
859 if (!p.alive) continue;
860 const range = dist(p.pos, world.bomb.pos);
861 if (range >= BOMB_BLAST_RADIUS) continue;
862 const { health, armor } = blastDamage(
863 range, p.armor, BOMB_BLAST_RADIUS, WEAPONS.bomb.damage, WEAPONS.bomb.armorPenetration,
864 );
865 if (health <= 0) continue;
866 const dealt = Math.min(health, p.health);
867 p.armor = Math.max(0, p.armor - armor);
868 p.health -= health;
869 hurt(world, p, world.bomb.pos, dealt);
870 // Nobody planted this for a kill: the bomb credits nobody, the same
871 // way killing yourself with your own grenade does not — `onKill`
872 // with the victim standing in for the killer.
873 if (p.health <= 0) {
856874 p.health = 0;
857 p.deaths += 1;
858 // The bomb kills nobody by name, but it still leaves them lying there.
859 addBody(world.bodies, p, world.boxes, world.time);
875 onKill(world, p, p, "bomb", false);
860876 }
861877 }
862878 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs