Split The code is public; the app answers only to the crew.
Getting shot tells you: a directional damage indicator, a hurt vignette and a pain sound #15
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/10-getting-shot-tells-you-a-directional-dam → main 1 commit fork
9 files
changed +463
−67
README.md +5 −1
| @@ -39,7 +39,11 @@ The bundle in `static/` is committed, so a fresh clone plays without building; | ||
| 39 | 39 | movement (ground friction, air acceleration, an 18-unit step, strafe jumping), |
| 40 | 40 | CS damage with range falloff, armour and hitgroups, deterministic spray patterns, |
| 41 | 41 | a 1:55 round with a 40-second bomb, plant and defuse timers, kill rewards and |
| 42 | loss bonuses. The leaderboard is a real SQLite table under `FORGE_APP_DATA`. | |
| 42 | loss bonuses. Hit feedback runs both ways: a hit marker and a beep when you hit | |
| 43 | someone, and when you are hit a view punch away from the shot, a wedge around | |
| 44 | the crosshair pointing at whoever fired, a vignette that reddens as your health | |
| 45 | falls and a pain sound. The leaderboard is a real SQLite table under | |
| 46 | `FORGE_APP_DATA`. | |
| 43 | 47 | - **Generated, not licensed.** The map (`de_forge`), the textures, the player and |
| 44 | 48 | weapon models and every sound are produced in code at start-up. There are no |
| 45 | 49 | asset files and nothing from the real game is used — the numbers follow CS's |
src/client/audio.ts +48
| @@ -10,6 +10,7 @@ import { WEAPONS, type WeaponId } from "./weapons.ts"; | ||
| 10 | 10 | export type Audio = { |
| 11 | 11 | shot(weapon: WeaponId, at: Vec3, listener: Vec3): void; |
| 12 | 12 | hit(headshot: boolean): void; |
| 13 | pain(amount: number): void; | |
| 13 | 14 | beep(pitch: number, gain?: number): void; |
| 14 | 15 | resume(): void; |
| 15 | 16 | muted: boolean; |
| @@ -26,6 +27,7 @@ const TONE: Record<string, { cut: number; thump: number; length: number }> = { | ||
| 26 | 27 | export function createAudio(): Audio { |
| 27 | 28 | let ctx: AudioContext | null = null; |
| 28 | 29 | let noise: AudioBuffer | null = null; |
| 30 | let lastPain = -1; | |
| 29 | 31 | |
| 30 | 32 | const ensure = (): AudioContext | null => { |
| 31 | 33 | if (ctx === null) { |
| @@ -80,6 +82,52 @@ export function createAudio(): Audio { | ||
| 80 | 82 | hit(headshot) { |
| 81 | 83 | audio.beep(headshot ? 1400 : 900, 0.08); |
| 82 | 84 | }, |
| 85 | /** | |
| 86 | * Taking damage: a low grunt sliding down, nothing like the square-wave | |
| 87 | * beep the shooter hears. A rifle burst lands four of these inside a | |
| 88 | * quarter of a second, so one every 140ms is all that is let through — | |
| 89 | * the rest would be noise on top of a sound already playing. | |
| 90 | */ | |
| 91 | pain(amount) { | |
| 92 | if (audio.muted) return; | |
| 93 | const context = ensure(); | |
| 94 | if (context === null || noise === null) return; | |
| 95 | const now = context.currentTime; | |
| 96 | if (now - lastPain < 0.14) return; | |
| 97 | lastPain = now; | |
| 98 | const hurt = Math.min(1, Math.max(0.15, amount / 50)); | |
| 99 | const length = 0.18 + hurt * 0.12; | |
| 100 | ||
| 101 | const osc = context.createOscillator(); | |
| 102 | osc.type = "sawtooth"; | |
| 103 | osc.frequency.setValueAtTime(240 + hurt * 90, now); | |
| 104 | osc.frequency.exponentialRampToValueAtTime(110, now + length); | |
| 105 | const shape = context.createBiquadFilter(); | |
| 106 | shape.type = "lowpass"; | |
| 107 | shape.frequency.value = 900; | |
| 108 | const gain = context.createGain(); | |
| 109 | gain.gain.setValueAtTime(0.0001, now); | |
| 110 | gain.gain.exponentialRampToValueAtTime(0.16 * hurt, now + 0.02); | |
| 111 | gain.gain.exponentialRampToValueAtTime(0.0005, now + length); | |
| 112 | osc.connect(shape).connect(gain).connect(context.destination); | |
| 113 | osc.start(now); | |
| 114 | osc.stop(now + length + 0.02); | |
| 115 | ||
| 116 | // A breath of noise under it, so it reads as a body and not a synth. | |
| 117 | const breath = context.createBufferSource(); | |
| 118 | breath.buffer = noise; | |
| 119 | breath.playbackRate.value = 0.6; | |
| 120 | const band = context.createBiquadFilter(); | |
| 121 | band.type = "bandpass"; | |
| 122 | band.frequency.value = 500; | |
| 123 | band.Q.value = 1.2; | |
| 124 | const breathGain = context.createGain(); | |
| 125 | breathGain.gain.setValueAtTime(0.07 * hurt, now); | |
| 126 | breathGain.gain.exponentialRampToValueAtTime(0.0005, now + length * 0.8); | |
| 127 | breath.connect(band).connect(breathGain).connect(context.destination); | |
| 128 | breath.start(now); | |
| 129 | breath.stop(now + length); | |
| 130 | }, | |
| 83 | 131 | beep(pitch, gain = 0.06) { |
| 84 | 132 | if (audio.muted) return; |
| 85 | 133 | const context = ensure(); |
src/client/hud.test.ts added +108
| @@ -0,0 +1,108 @@ | ||
| 1 | /** | |
| 2 | * The HUD against a recording 2D context: no browser, no canvas, just the | |
| 3 | * calls it would have made. It is the only way to check the damage indicator | |
| 4 | * points the right way round, which is the one thing about it that can be | |
| 5 | * wrong without looking wrong. | |
| 6 | */ | |
| 7 | ||
| 8 | import { expect, test } from "bun:test"; | |
| 9 | import { drawHud } from "./hud.ts"; | |
| 10 | import { SHORT } from "./match.ts"; | |
| 11 | import { createWorld, type World } from "./world.ts"; | |
| 12 | ||
| 13 | type Call = { name: string; args: number[] }; | |
| 14 | ||
| 15 | function recorder(): { ctx: CanvasRenderingContext2D; calls: Call[] } { | |
| 16 | const calls: Call[] = []; | |
| 17 | const log = (name: string) => (...args: unknown[]): void => { | |
| 18 | calls.push({ name, args: args.filter((a) => typeof a === "number") as number[] }); | |
| 19 | }; | |
| 20 | const ctx = { | |
| 21 | clearRect: log("clearRect"), fillRect: log("fillRect"), rect: log("rect"), | |
| 22 | beginPath: log("beginPath"), moveTo: log("moveTo"), lineTo: log("lineTo"), | |
| 23 | arc: log("arc"), stroke: log("stroke"), fill: log("fill"), | |
| 24 | fillText: log("fillText"), save: log("save"), restore: log("restore"), | |
| 25 | measureText: () => ({ width: 10 }), | |
| 26 | createRadialGradient: (...args: unknown[]) => { | |
| 27 | calls.push({ name: "createRadialGradient", args: args as number[] }); | |
| 28 | return { addColorStop: (): void => {} }; | |
| 29 | }, | |
| 30 | globalAlpha: 1, lineWidth: 1, fillStyle: "", strokeStyle: "", | |
| 31 | font: "", textAlign: "", textBaseline: "", | |
| 32 | }; | |
| 33 | return { ctx: ctx as unknown as CanvasRenderingContext2D, calls }; | |
| 34 | } | |
| 35 | ||
| 36 | const W = 1280, H = 720; | |
| 37 | ||
| 38 | /** Where the middle of a stroked wedge lands on the screen. */ | |
| 39 | const wedgePoint = (call: Call): { x: number; y: number } => { | |
| 40 | const [x, y, r, start, end] = call.args as [number, number, number, number, number]; | |
| 41 | const middle = (start + end) / 2; | |
| 42 | return { x: x + Math.cos(middle) * r, y: y + Math.sin(middle) * r }; | |
| 43 | }; | |
| 44 | ||
| 45 | const hurtWorld = (yaw: number, from: [number, number, number], victim: number): World => { | |
| 46 | const world = createWorld(SHORT, "me", "ct", 5, 71); | |
| 47 | world.me.pos = [0, 0, 0]; | |
| 48 | world.me.yaw = yaw; | |
| 49 | world.effects.length = 0; | |
| 50 | world.effects.push({ kind: "hurt", at: world.time, victim, from, amount: 30 }); | |
| 51 | return world; | |
| 52 | }; | |
| 53 | ||
| 54 | test("a hit from behind puts the wedge below the crosshair, one from the left on its left", () => { | |
| 55 | const behind = recorder(); | |
| 56 | drawHud(behind.ctx, hurtWorld(0, [-500, 0, 40], 0), W, H, false); | |
| 57 | const back = behind.calls.filter((c) => c.name === "arc").map(wedgePoint); | |
| 58 | expect(back.length).toBe(1); | |
| 59 | expect(back[0].y).toBeGreaterThan(H / 2 + 20); | |
| 60 | expect(Math.abs(back[0].x - W / 2)).toBeLessThan(20); | |
| 61 | ||
| 62 | const left = recorder(); | |
| 63 | drawHud(left.ctx, hurtWorld(0, [0, 500, 40], 0), W, H, false); | |
| 64 | const side = left.calls.filter((c) => c.name === "arc").map(wedgePoint); | |
| 65 | expect(side.length).toBe(1); | |
| 66 | expect(side[0].x).toBeLessThan(W / 2 - 20); | |
| 67 | expect(Math.abs(side[0].y - H / 2)).toBeLessThan(20); | |
| 68 | }); | |
| 69 | ||
| 70 | test("hits stack, and damage to anyone but the player being drawn is not drawn", () => { | |
| 71 | const two = recorder(); | |
| 72 | const world = hurtWorld(0, [-500, 0, 40], 0); | |
| 73 | world.effects.push({ kind: "hurt", at: world.time, victim: 0, from: [0, -500, 40], amount: 18 }); | |
| 74 | drawHud(two.ctx, world, W, H, false); | |
| 75 | expect(two.calls.filter((c) => c.name === "arc").length).toBe(2); | |
| 76 | ||
| 77 | const bot = recorder(); | |
| 78 | drawHud(bot.ctx, hurtWorld(0, [-500, 0, 40], 4), W, H, false); | |
| 79 | expect(bot.calls.filter((c) => c.name === "arc").length).toBe(0); | |
| 80 | }); | |
| 81 | ||
| 82 | test("an old hit has faded out by the time the effects list is trimmed", () => { | |
| 83 | const world = hurtWorld(0, [-500, 0, 40], 0); | |
| 84 | world.time += 1.5; // still in the list, which update keeps for two seconds | |
| 85 | const frame = recorder(); | |
| 86 | drawHud(frame.ctx, world, W, H, false); | |
| 87 | expect(frame.calls.filter((c) => c.name === "arc").length).toBe(0); | |
| 88 | }); | |
| 89 | ||
| 90 | test("the hurt vignette follows health and its gradient is built once, not per frame", () => { | |
| 91 | const healthy = recorder(); | |
| 92 | const world = createWorld(SHORT, "me", "ct", 5, 73); | |
| 93 | drawHud(healthy.ctx, world, W, H, false); | |
| 94 | const full = (calls: Call[]): Call[] => | |
| 95 | calls.filter((c) => c.name === "fillRect" && c.args[0] === 0 && c.args[1] === 0 && c.args[2] === W); | |
| 96 | expect(full(healthy.calls).length).toBe(0); | |
| 97 | ||
| 98 | world.me.health = 20; | |
| 99 | const hurt = recorder(); | |
| 100 | drawHud(hurt.ctx, world, W, H, false); | |
| 101 | expect(full(hurt.calls).length).toBe(1); | |
| 102 | ||
| 103 | // The second frame at the same size reuses the gradient the first one made. | |
| 104 | const again = recorder(); | |
| 105 | drawHud(again.ctx, world, W, H, false); | |
| 106 | expect(full(again.calls).length).toBe(1); | |
| 107 | expect(again.calls.filter((c) => c.name === "createRadialGradient").length).toBe(0); | |
| 108 | }); |
src/client/hud.ts +76 −2
| @@ -4,14 +4,85 @@ | ||
| 4 | 4 | * it never changes anything. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { clamp } from "./math.ts"; | |
| 7 | import { angleDelta, clamp } from "./math.ts"; | |
| 8 | 8 | import { MOVE } from "./physics.ts"; |
| 9 | 9 | import { inaccuracy, WEAPONS } from "./weapons.ts"; |
| 10 | import { currentWeapon, useProgress, type World } from "./world.ts"; | |
| 10 | import { currentWeapon, eyePos, useProgress, type Player, type World } from "./world.ts"; | |
| 11 | 11 | |
| 12 | 12 | const T_COLOR = "#e2b04a"; |
| 13 | 13 | const CT_COLOR = "#6fa8e8"; |
| 14 | 14 | |
| 15 | /** | |
| 16 | * How long a damage indicator lives. Well inside the two seconds `update` | |
| 17 | * keeps an effect for, so the trim window stays where it is. | |
| 18 | */ | |
| 19 | const HURT_FADE = 1.1; | |
| 20 | const rad = Math.PI / 180; | |
| 21 | ||
| 22 | // The vignette is one radial gradient per canvas size, kept between frames: | |
| 23 | // building it costs an allocation and drawHud runs sixty times a second, so it | |
| 24 | // is built with its colours at full strength and faded with globalAlpha. | |
| 25 | let vignette: CanvasGradient | null = null; | |
| 26 | let vignetteSize = ""; | |
| 27 | ||
| 28 | function hurtVignette(ctx: CanvasRenderingContext2D, width: number, height: number): CanvasGradient { | |
| 29 | const size = `${width}x${height}`; | |
| 30 | if (vignette === null || vignetteSize !== size) { | |
| 31 | const gradient = ctx.createRadialGradient( | |
| 32 | width / 2, height / 2, Math.min(width, height) * 0.22, | |
| 33 | width / 2, height / 2, Math.max(width, height) * 0.62, | |
| 34 | ); | |
| 35 | gradient.addColorStop(0, "rgba(140, 0, 0, 0)"); | |
| 36 | gradient.addColorStop(0.55, "rgba(160, 6, 6, 0.4)"); | |
| 37 | gradient.addColorStop(1, "rgba(190, 14, 14, 0.95)"); | |
| 38 | vignette = gradient; | |
| 39 | vignetteSize = size; | |
| 40 | } | |
| 41 | return vignette; | |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * The wedges around the crosshair pointing at whatever just hurt `me`, in its | |
| 46 | * own yaw frame, and the red vignette that deepens as its health drops. Several | |
| 47 | * hits stack: each is its own wedge, fading on its own clock. | |
| 48 | */ | |
| 49 | function drawDamage( | |
| 50 | ctx: CanvasRenderingContext2D, | |
| 51 | world: World, | |
| 52 | me: Player, | |
| 53 | width: number, | |
| 54 | height: number, | |
| 55 | ): void { | |
| 56 | const low = clamp((55 - me.health) / 55, 0, 1); | |
| 57 | if (me.alive && low > 0) { | |
| 58 | ctx.globalAlpha = 0.65 * low * low; | |
| 59 | ctx.fillStyle = hurtVignette(ctx, width, height); | |
| 60 | ctx.fillRect(0, 0, width, height); | |
| 61 | ctx.globalAlpha = 1; | |
| 62 | } | |
| 63 | ||
| 64 | const eye = eyePos(me); | |
| 65 | const radius = Math.min(width, height) * 0.14; | |
| 66 | for (const effect of world.effects) { | |
| 67 | if (effect.kind !== "hurt" || effect.victim !== me.id) continue; | |
| 68 | const age = world.time - effect.at; | |
| 69 | if (age < 0 || age > HURT_FADE) continue; | |
| 70 | const bearing = (Math.atan2(effect.from[1] - eye[1], effect.from[0] - eye[0]) * 180) / Math.PI; | |
| 71 | // Yaw grows anticlockwise, so a source to the left is a positive angle off | |
| 72 | // the view; the canvas turns clockwise from +x, so the wedge sits at minus | |
| 73 | // that angle, a quarter turn back from straight up. | |
| 74 | const middle = -angleDelta(me.yaw, bearing) * rad - Math.PI / 2; | |
| 75 | const half = (14 + clamp(effect.amount, 0, 60) * 0.2) * rad; | |
| 76 | ctx.globalAlpha = clamp(1 - age / HURT_FADE, 0, 1) * clamp(0.35 + effect.amount / 50, 0, 0.9); | |
| 77 | ctx.strokeStyle = "#ff4238"; | |
| 78 | ctx.lineWidth = Math.max(3, Math.round(radius * 0.13)); | |
| 79 | ctx.beginPath(); | |
| 80 | ctx.arc(width / 2, height / 2, radius, middle - half, middle + half); | |
| 81 | ctx.stroke(); | |
| 82 | } | |
| 83 | ctx.globalAlpha = 1; | |
| 84 | } | |
| 85 | ||
| 15 | 86 | const clock = (seconds: number): string => { |
| 16 | 87 | const s = Math.max(0, Math.ceil(seconds)); |
| 17 | 88 | return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; |
| @@ -30,6 +101,9 @@ export function drawHud( | ||
| 30 | 101 | const weapon = currentWeapon(me); |
| 31 | 102 | const pad = Math.max(12, Math.round(width * 0.02)); |
| 32 | 103 | |
| 104 | // --- being shot: where it came from, and how close to dead it left us | |
| 105 | drawDamage(ctx, world, me, width, height); | |
| 106 | ||
| 33 | 107 | // --- crosshair, opening up exactly as much as the gun is inaccurate |
| 34 | 108 | if (me.alive) { |
| 35 | 109 | const speed = Math.hypot(me.vel[0], me.vel[1]); |
src/client/main.ts +8 −4
| @@ -13,7 +13,7 @@ import { COMPETITIVE, SHORT, type Rules } from "./match.ts"; | ||
| 13 | 13 | import { createRenderer, type Camera } from "./renderer.ts"; |
| 14 | 14 | import { buyable } from "./weapons.ts"; |
| 15 | 15 | import { |
| 16 | buy, canBuy, createWorld, eyePos, priceOf, update, type BuyItem, type Player, type World, | |
| 16 | buy, canBuy, createWorld, eyePos, flinch, priceOf, update, type BuyItem, type Player, type World, | |
| 17 | 17 | } from "./world.ts"; |
| 18 | 18 | |
| 19 | 19 | const TICK = 1 / 64; |
| @@ -169,12 +169,16 @@ function boot(): void { | ||
| 169 | 169 | const saved = localStorage.getItem("cs.name"); |
| 170 | 170 | if (saved !== null && nameField.value === "") nameField.value = saved; |
| 171 | 171 | |
| 172 | // --- sound for whatever happened in the ticks we just ran | |
| 173 | function playSounds(current: World): void { | |
| 172 | // --- sound and flinch for whatever happened in the ticks we just ran | |
| 173 | function feelEffects(current: World): void { | |
| 174 | 174 | for (const effect of current.effects) { |
| 175 | 175 | if (effect.at <= lastSound) continue; |
| 176 | 176 | if (effect.kind === "shot") audio.shot(effect.weapon, effect.pos, eyePos(current.me)); |
| 177 | 177 | else if (effect.kind === "hitmark") audio.hit(effect.headshot); |
| 178 | else if (effect.kind === "hurt" && effect.victim === current.me.id) { | |
| 179 | flinch(current.me, effect.from, effect.amount); | |
| 180 | audio.pain(effect.amount); | |
| 181 | } | |
| 178 | 182 | } |
| 179 | 183 | lastSound = current.time; |
| 180 | 184 | } |
| @@ -213,7 +217,7 @@ function boot(): void { | ||
| 213 | 217 | accumulator -= TICK; |
| 214 | 218 | steps += 1; |
| 215 | 219 | } |
| 216 | playSounds(world); | |
| 220 | feelEffects(world); | |
| 217 | 221 | |
| 218 | 222 | // The buy window closes itself when the round goes live. |
| 219 | 223 | if (!buyPanel.hidden && !canBuy(world, me)) openBuy(false); |
src/client/world.test.ts +119 −1
| @@ -2,7 +2,10 @@ import { expect, test } from "bun:test"; | ||
| 2 | 2 | import { siteAt, sites } from "./map.ts"; |
| 3 | 3 | import { SHORT } from "./match.ts"; |
| 4 | 4 | import { WEAPONS } from "./weapons.ts"; |
| 5 | import { buy, createWorld, currentWeapon, NO_INPUT, shoot, startRound, update, type Input, type World } from "./world.ts"; | |
| 5 | import { | |
| 6 | aimAt, bodyCentre, buy, createWorld, currentWeapon, eyePos, flinch, NO_INPUT, shoot, startRound, | |
| 7 | update, type Input, type Player, type World, | |
| 8 | } from "./world.ts"; | |
| 6 | 9 | |
| 7 | 10 | const TICK = 1 / 64; |
| 8 | 11 | const run = (world: World, seconds: number, input: Input = NO_INPUT): void => { |
| @@ -118,6 +121,121 @@ test("a new round puts everyone back with full health and a full magazine", () = | ||
| 118 | 121 | expect(world.players.filter((p) => p.hasBomb).length).toBe(1); |
| 119 | 122 | }); |
| 120 | 123 | |
| 124 | /** The damage the person took, narrowed out of the effects list. */ | |
| 125 | const hurtsIn = (world: World): { at: number; victim: number; from: number[]; amount: number }[] => | |
| 126 | world.effects.flatMap((e) => (e.kind === "hurt" ? [e] : [])); | |
| 127 | ||
| 128 | /** Put a shooter in front of the person on an empty map, rifle up and aimed. */ | |
| 129 | function faceOff(world: World, shooter: Player, apart = 200): void { | |
| 130 | world.boxes = []; | |
| 131 | world.match.phase = "live"; | |
| 132 | for (const p of world.players) if (p !== shooter && p !== world.me) p.alive = false; | |
| 133 | world.me.pos = [0, 0, 0]; | |
| 134 | world.me.vel = [0, 0, 0]; | |
| 135 | shooter.pos = [apart, 0, 0]; | |
| 136 | shooter.vel = [0, 0, 0]; | |
| 137 | shooter.onGround = true; | |
| 138 | shooter.primary = "ak47"; | |
| 139 | shooter.weapon = "ak47"; | |
| 140 | shooter.ammo.ak47 = { mag: 30, reserve: 90 }; | |
| 141 | shooter.nextShotAt = 0; | |
| 142 | shooter.reloadUntil = 0; | |
| 143 | shooter.shotIndex = 0; | |
| 144 | const aim = aimAt(shooter, bodyCentre(world.me)); | |
| 145 | shooter.yaw = aim.yaw; | |
| 146 | shooter.pitch = aim.pitch; | |
| 147 | world.effects.length = 0; | |
| 148 | } | |
| 149 | ||
| 150 | test("a bullet that hits you pushes one hurt, from the shooter and worth the damage", () => { | |
| 151 | const world = createWorld(SHORT, "me", "ct", 5, 29); | |
| 152 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 153 | faceOff(world, shooter); | |
| 154 | const before = world.me.health; | |
| 155 | const from = eyePos(shooter); | |
| 156 | expect(shoot(world, shooter)).toBe(true); | |
| 157 | const hurts = hurtsIn(world); | |
| 158 | expect(world.me.health).toBeLessThan(before); | |
| 159 | expect(hurts.length).toBe(1); | |
| 160 | expect(hurts[0].victim).toBe(world.me.id); | |
| 161 | expect(hurts[0].from).toEqual(from); | |
| 162 | expect(hurts[0].amount).toBeCloseTo(before - world.me.health, 6); | |
| 163 | }); | |
| 164 | ||
| 165 | test("a bullet into a wall hurts nobody", () => { | |
| 166 | const world = createWorld(SHORT, "me", "ct", 5, 31); | |
| 167 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 168 | faceOff(world, shooter); | |
| 169 | world.boxes = [{ min: [90, -400, -200], max: [110, 400, 400], mat: 0 }]; | |
| 170 | const before = world.me.health; | |
| 171 | expect(shoot(world, shooter)).toBe(true); | |
| 172 | expect(hurtsIn(world).length).toBe(0); | |
| 173 | expect(world.effects.some((e) => e.kind === "impact")).toBe(true); | |
| 174 | expect(world.me.health).toBe(before); | |
| 175 | }); | |
| 176 | ||
| 177 | test("shooting a bot is silent: damage, but nothing for the HUD to read", () => { | |
| 178 | const world = createWorld(SHORT, "me", "ct", 5, 37); | |
| 179 | const target = world.players.find((p) => p.team === "t") as Player; | |
| 180 | faceOff(world, target); | |
| 181 | world.me.pos = [0, 0, 0]; | |
| 182 | const aim = aimAt(world.me, bodyCentre(target)); | |
| 183 | world.me.yaw = aim.yaw; | |
| 184 | world.me.pitch = aim.pitch; | |
| 185 | world.me.primary = "m4a4"; | |
| 186 | world.me.weapon = "m4a4"; | |
| 187 | world.me.ammo.m4a4 = { mag: 30, reserve: 90 }; | |
| 188 | expect(shoot(world, world.me)).toBe(true); | |
| 189 | expect(target.health).toBeLessThan(100); | |
| 190 | expect(hurtsIn(world).length).toBe(0); | |
| 191 | }); | |
| 192 | ||
| 193 | test("the bomb going off hurts everyone inside its radius and nobody outside", () => { | |
| 194 | const world = createWorld(SHORT, "me", "ct", 5, 41); | |
| 195 | const far = world.players[1]; | |
| 196 | world.bomb = { | |
| 197 | state: "planted", pos: [sites.A[0], sites.A[1], 2], site: "A", | |
| 198 | plantProgress: 0, defuseProgress: 0, defuser: null, | |
| 199 | }; | |
| 200 | world.bombPlanted = true; | |
| 201 | world.match.phase = "planted"; | |
| 202 | world.match.timer = TICK / 2; | |
| 203 | for (const p of world.players) { | |
| 204 | p.pos = [sites.A[0], sites.A[1], 2]; | |
| 205 | p.alive = true; | |
| 206 | p.health = 100; | |
| 207 | } | |
| 208 | far.pos = [sites.B[0], sites.B[1], 2]; | |
| 209 | world.effects.length = 0; | |
| 210 | update(world, TICK, NO_INPUT); | |
| 211 | ||
| 212 | expect(world.bomb.state).toBe("detonated"); | |
| 213 | expect(world.players.filter((p) => p.alive)).toEqual([far]); | |
| 214 | expect(far.health).toBe(100); | |
| 215 | const hurts = hurtsIn(world); | |
| 216 | expect(hurts.length).toBe(1); // only the person's own damage is ever recorded | |
| 217 | expect(hurts[0].victim).toBe(world.me.id); | |
| 218 | expect(hurts[0].amount).toBe(100); | |
| 219 | expect(hurts[0].from).toEqual([sites.A[0], sites.A[1], 2]); | |
| 220 | }); | |
| 221 | ||
| 222 | test("a hit flinches the view away from where it came from, and stays bounded", () => { | |
| 223 | const world = createWorld(SHORT, "me", "ct", 5, 43); | |
| 224 | const me = world.me; | |
| 225 | me.pos = [0, 0, 0]; | |
| 226 | me.yaw = 0; // looking down +x | |
| 227 | me.punch = [0, 0]; | |
| 228 | flinch(me, [0, 500, 60], 30); // shot from the left | |
| 229 | expect(me.punch[1]).toBeGreaterThan(0); | |
| 230 | expect(me.punch[0]).toBeGreaterThan(0); // swung to the right, away from it | |
| 231 | const once = me.punch[0]; | |
| 232 | flinch(me, [0, 500, 60], 30); | |
| 233 | expect(me.punch[0]).toBeGreaterThan(once); // a burst stacks | |
| 234 | for (let i = 0; i < 40; i++) flinch(me, [0, 500, 60], 100); | |
| 235 | expect(Math.abs(me.punch[0])).toBeLessThanOrEqual(6); | |
| 236 | expect(Math.abs(me.punch[1])).toBeLessThanOrEqual(6); | |
| 237 | }); | |
| 238 | ||
| 121 | 239 | test("a match plays itself to the end inside its own time", () => { |
| 122 | 240 | const world = createWorld({ ...SHORT, winScore: 2, freezeTime: 1, roundTime: 40, endDelay: 1 }, "me", "ct", 5, 23); |
| 123 | 241 | world.me.alive = false; |
src/client/world.ts +41 −1
| @@ -64,6 +64,9 @@ export type Effect = | ||
| 64 | 64 | | { kind: "blood"; at: number; pos: Vec3 } |
| 65 | 65 | | { kind: "shot"; at: number; pos: Vec3; weapon: WeaponId; mine: boolean } |
| 66 | 66 | | { kind: "hitmark"; at: number; headshot: boolean } |
| 67 | // Damage the person took: who took it, where it came from in the world, and | |
| 68 | // how much. Only ever pushed for the person — a bot being shot is silent. | |
| 69 | | { kind: "hurt"; at: number; victim: number; from: Vec3; amount: number } | |
| 67 | 70 | | { kind: "sound"; at: number; pos: Vec3; name: string }; |
| 68 | 71 | |
| 69 | 72 | export type FeedEntry = { at: number; killer: string; victim: string; weapon: WeaponId; headshot: boolean; killerTeam: Team }; |
| @@ -307,6 +310,39 @@ function finishReload(world: World, p: Player): void { | ||
| 307 | 310 | p.shotIndex = 0; |
| 308 | 311 | } |
| 309 | 312 | |
| 313 | /** | |
| 314 | * Say that `victim` took `amount` damage from `from`. Only the person's own | |
| 315 | * damage is recorded: a bot being shot has nobody to tell, and an effect per | |
| 316 | * bullet per bot would be a list nothing reads. | |
| 317 | */ | |
| 318 | function hurt(world: World, victim: Player, from: Vec3, amount: number): void { | |
| 319 | if (victim !== world.me) return; | |
| 320 | world.effects.push({ kind: "hurt", at: world.time, victim: victim.id, from: [...from] as Vec3, amount }); | |
| 321 | } | |
| 322 | ||
| 323 | /** How much view punch a point of damage is worth, and the most it can add up to. */ | |
| 324 | const FLINCH_PER_DAMAGE = 0.06; | |
| 325 | const FLINCH_MAX = 6; | |
| 326 | ||
| 327 | /** | |
| 328 | * Feel a hit: a small view punch, up and away from where the damage came from, | |
| 329 | * decaying on the same curve `applyInput` already runs `punch` down. It is a | |
| 330 | * function the client calls when it reads a `hurt` effect rather than part of | |
| 331 | * the tick, because the RL arena steps this same simulation with its learner | |
| 332 | * in `world.me`, and `punch` is two of the policy's 32 inputs: flinching inside | |
| 333 | * `update` would move the numbers the shipped weights were trained on. | |
| 334 | */ | |
| 335 | export function flinch(victim: Player, from: Vec3, amount: number): void { | |
| 336 | const to = sub(from, eyePos(victim)); | |
| 337 | const bearing = (Math.atan2(to[1], to[0]) * 180) / Math.PI; | |
| 338 | const rel = (angleDelta(victim.yaw, bearing) * Math.PI) / 180; | |
| 339 | const kick = clamp(amount, 0, 100) * FLINCH_PER_DAMAGE; | |
| 340 | victim.punch = [ | |
| 341 | clamp(victim.punch[0] + kick * 0.5 * Math.sin(rel), -FLINCH_MAX, FLINCH_MAX), | |
| 342 | clamp(victim.punch[1] + kick, -FLINCH_MAX, FLINCH_MAX), | |
| 343 | ]; | |
| 344 | } | |
| 345 | ||
| 310 | 346 | /** Fire one round if the weapon is ready. Returns true if a shot went out. */ |
| 311 | 347 | export function shoot(world: World, p: Player): boolean { |
| 312 | 348 | const w = currentWeapon(p); |
| @@ -352,6 +388,7 @@ export function shoot(world: World, p: Player): boolean { | ||
| 352 | 388 | p.damage += result.damage; |
| 353 | 389 | world.effects.push({ kind: "blood", at: world.time, pos: hit.point }); |
| 354 | 390 | if (p === world.me) world.effects.push({ kind: "hitmark", at: world.time, headshot: result.headshot }); |
| 391 | if (result.damage > 0) hurt(world, victim, from, result.damage); | |
| 355 | 392 | if (result.killed) onKill(world, p, victim, w.id, result.headshot); |
| 356 | 393 | } |
| 357 | 394 | return true; |
| @@ -377,6 +414,8 @@ function onKill(world: World, killer: Player, victim: Player, weapon: WeaponId, | ||
| 377 | 414 | // --- the bomb ------------------------------------------------------------- |
| 378 | 415 | |
| 379 | 416 | const PLANT_RANGE = 64; |
| 417 | /** Everyone this close to the bomb when it goes off dies with it. */ | |
| 418 | const BOMB_RADIUS = 1000; | |
| 380 | 419 | |
| 381 | 420 | function updateBomb(world: World, p: Player, input: Input, dt: number): void { |
| 382 | 421 | const bomb = world.bomb; |
| @@ -481,7 +520,8 @@ export function update(world: World, dt: number, input: Input): void { | ||
| 481 | 520 | if (event.type === "bomb_timeout") { |
| 482 | 521 | world.bomb.state = "detonated"; |
| 483 | 522 | for (const p of world.players) { |
| 484 | if (dist(p.pos, world.bomb.pos) < 1000 && p.alive) { | |
| 523 | if (dist(p.pos, world.bomb.pos) < BOMB_RADIUS && p.alive) { | |
| 524 | hurt(world, p, world.bomb.pos, p.health); | |
| 485 | 525 | p.alive = false; |
| 486 | 526 | p.health = 0; |
| 487 | 527 | p.deaths += 1; |
static/app.5401f6cc28.js added +58
Diff not shown — a line 57,887 characters wide (limit 2,000). +58
View filestatic/app.e8ca965610.js deleted −58
Diff not shown — a line 55,651 characters wide (limit 2,000). −58
View file
new above
offline