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
Check · Review · Land · Live done — Landed and live in production.
scout/cs:scout/49-the-bomb-s-blast-a-lethal-core-falloff-a → main 1 commit fork
9 files
changed +186
−31
README.md +9 −2
| @@ -74,8 +74,15 @@ wants one, but the running server never reads it. | ||
| 74 | 74 | beeps: about once a second right after the plant, quickening to a few a |
| 75 | 75 | second — and rising in pitch — as the fuse runs out, so a CT can walk to the |
| 76 | 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 | |
| 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 | |
| 79 | 86 | carry across the whole map. The leaderboard is a real SQLite table under |
| 80 | 87 | `FORGE_APP_DATA`, also served as plain JSON from `GET /api/leaderboard` for |
| 81 | 88 | 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 " | ||
| 3 | 3 | import * as map from "./map.ts"; |
| 4 | 4 | import type { Box } from "./map.ts"; |
| 5 | 5 | import type { Vec3 } from "./math.ts"; |
| 6 | import { BOMB_BLAST_RADIUS } from "./world.ts"; | |
| 7 | import { WEAPONS } from "./weapons.ts"; | |
| 6 | 8 | |
| 7 | 9 | const TICK = 1 / 64; |
| 8 | 10 | 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" | ||
| 115 | 117 | expect(armoured.health).toBeLessThan(centre * 0.6); |
| 116 | 118 | expect(armoured.armor).toBeGreaterThan(0); |
| 117 | 119 | }); |
| 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( | ||
| 129 | 129 | }; |
| 130 | 130 | } |
| 131 | 131 | |
| 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); | |
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * 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. | |
| 140 | 142 | */ |
| 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); | |
| 143 | 151 | 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); | |
| 145 | 153 | } |
src/client/hud.ts +2 −2
| @@ -415,8 +415,8 @@ export function drawHud( | ||
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | // --- 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. | |
| 420 | 420 | if (!world.me.alive && !over) { |
| 421 | 421 | const top = height * 0.38; |
| 422 | 422 | 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", () => | ||
| 50 | 50 | expect(watched(world, world.me.id)).toBe(world.me); |
| 51 | 51 | }); |
| 52 | 52 | |
| 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", () => { | |
| 54 | 54 | const world = dead(); |
| 55 | 55 | expect(deathNotice(world, world.me)).toBe(null); |
| 56 | 56 | 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 => | ||
| 31 | 31 | world.players.find((p) => p.id === watching) ?? world.me; |
| 32 | 32 | |
| 33 | 33 | /** |
| 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. | |
| 37 | 38 | */ |
| 38 | 39 | export function deathNotice(world: World, player: Player): FeedEntry | null { |
| 39 | 40 | for (let i = world.feed.length - 1; i >= 0; i -= 1) { |
src/client/weapons.ts +7 −3
| @@ -11,12 +11,12 @@ | ||
| 11 | 11 | import { rng } from "./math.ts"; |
| 12 | 12 | |
| 13 | 13 | export type WeaponId = |
| 14 | | "knife" | "glock" | "usp" | "deagle" | "mp9" | "ak47" | "m4a4" | "awp" | "he"; | |
| 14 | | "knife" | "glock" | "usp" | "deagle" | "mp9" | "ak47" | "m4a4" | "awp" | "he" | "bomb"; | |
| 15 | 15 | |
| 16 | 16 | export type Weapon = { |
| 17 | 17 | id: WeaponId; |
| 18 | 18 | name: string; |
| 19 | kind: "knife" | "pistol" | "smg" | "rifle" | "sniper" | "grenade"; | |
| 19 | kind: "knife" | "pistol" | "smg" | "rifle" | "sniper" | "grenade" | "bomb"; | |
| 20 | 20 | team: "t" | "ct" | "both"; |
| 21 | 21 | price: number; |
| 22 | 22 | damage: number; |
| @@ -49,6 +49,10 @@ const WEAPON_LIST: Weapon[] = [ | ||
| 49 | 49 | // last because a weapon's spray pattern is seeded from its place in this |
| 50 | 50 | // list, and every gun above it already has the pattern people have learned. |
| 51 | 51 | { 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 }, | |
| 52 | 56 | ]; |
| 53 | 57 | |
| 54 | 58 | export const WEAPONS: Record<WeaponId, Weapon> = Object.fromEntries( |
| @@ -56,7 +60,7 @@ export const WEAPONS: Record<WeaponId, Weapon> = Object.fromEntries( | ||
| 56 | 60 | ) as Record<WeaponId, Weapon>; |
| 57 | 61 | |
| 58 | 62 | 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)); | |
| 60 | 64 | |
| 61 | 65 | export const ARMOR_PRICE = 650; |
| 62 | 66 | export const HELMET_PRICE = 350; |
src/client/world.test.ts +102 −3
| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { GRENADE } from "./grenades.ts"; |
| 3 | 3 | import { siteAt, sites } from "./map.ts"; |
| 4 | import { SHORT } from "./match.ts"; | |
| 4 | import { SHORT, winReward } from "./match.ts"; | |
| 5 | 5 | import { WEAPONS } from "./weapons.ts"; |
| 6 | 6 | 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, | |
| 9 | 9 | } from "./world.ts"; |
| 10 | 10 | |
| 11 | 11 | const TICK = 1 / 64; |
| @@ -354,6 +354,7 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", ( | ||
| 354 | 354 | p.pos = [sites.A[0], sites.A[1], 2]; |
| 355 | 355 | p.alive = true; |
| 356 | 356 | p.health = 100; |
| 357 | p.hasBomb = false; // planted: nobody is still carrying it | |
| 357 | 358 | } |
| 358 | 359 | far.pos = [sites.B[0], sites.B[1], 2]; |
| 359 | 360 | world.effects.length = 0; |
| @@ -385,6 +386,104 @@ test("the detonation pushes exactly one explode", () => { | ||
| 385 | 386 | expect(soundsNamed(world, "explode")).toBe(1); |
| 386 | 387 | }); |
| 387 | 388 | |
| 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 | ||
| 388 | 487 | test("a planted bomb pushes beeps on that cadence, and a defuse stops them", () => { |
| 389 | 488 | const world = createWorld(SHORT, "me", "ct", 5, 71); |
| 390 | 489 | 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 | ||
| 326 | 326 | if (item === "helmet") return !player.helmet; |
| 327 | 327 | if (item === "kit") return player.team === "ct" && !player.defuseKit; |
| 328 | 328 | 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; | |
| 330 | 330 | if (weapon.team !== "both" && weapon.team !== player.team) return false; |
| 331 | 331 | if (weapon.kind === "grenade") return player.grenades < MAX_GRENADES; |
| 332 | 332 | return true; |
| @@ -691,8 +691,14 @@ function updateGrenades(world: World, dt: number): void { | ||
| 691 | 691 | // --- the bomb ------------------------------------------------------------- |
| 692 | 692 | |
| 693 | 693 | 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; | |
| 696 | 702 | |
| 697 | 703 | function updateBomb(world: World, p: Player, input: Input, dt: number): void { |
| 698 | 704 | const bomb = world.bomb; |
| @@ -850,13 +856,23 @@ export function update(world: World, dt: number, input: Input): void { | ||
| 850 | 856 | world.bomb.state = "detonated"; |
| 851 | 857 | world.effects.push({ kind: "sound", at: world.time, pos: world.bomb.pos, name: "explode" }); |
| 852 | 858 | 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) { | |
| 856 | 874 | 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); | |
| 860 | 876 | } |
| 861 | 877 | } |
| 862 | 878 | } |
new above
offline