Split The code is public; the app answers only to the crew.
Pick the rifle up off the body: a weapon stays in the world when its owner dies #24
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/19-pick-the-rifle-up-off-the-body-a-weapon → main 1 commit fork
11 files
changed +393
−70
README.md +10 −2
| @@ -40,7 +40,14 @@ The bundle in `static/` is committed, so a fresh clone plays without building; | ||
| 40 | 40 | movement (ground friction, air acceleration, an 18-unit step, strafe jumping), |
| 41 | 41 | CS damage with range falloff, armour and hitgroups, deterministic spray patterns, |
| 42 | 42 | a 1:55 round with a 40-second bomb, plant and defuse timers, kill rewards and |
| 43 | loss bonuses. Hit feedback runs both ways: a hit marker and a beep when you hit | |
| 43 | loss bonuses. The economy has CS's loop: weapons drop. A player killed leaves | |
| 44 | the gun they were holding on the floor with whatever was left in the magazine | |
| 45 | — a knife never drops, and someone killed holding one leaves their primary — | |
| 46 | and it can be picked up. With no primary you take it by walking over it; with | |
| 47 | one already, <kbd>E</kbd> swaps, and the gun you were holding lands at your | |
| 48 | feet. There is no team rule: a CT who finds an AK-47 keeps it. The floor is | |
| 49 | swept at the start of the next round, and whatever you are carrying then comes | |
| 50 | back with a full magazine. Hit feedback runs both ways: a hit marker and a beep when you hit | |
| 44 | 51 | someone, and when you are hit a view punch away from the shot, a wedge around |
| 45 | 52 | the crosshair pointing at whoever fired, a vignette that reddens as your health |
| 46 | 53 | falls and a pain sound. Every world sound is panned around the listener's |
| @@ -69,7 +76,8 @@ The bundle in `static/` is committed, so a fresh clone plays without building; | ||
| 69 | 76 | |
| 70 | 77 | <kbd>WASD</kbd> move, <kbd>Space</kbd> jump, <kbd>Ctrl</kbd> crouch, |
| 71 | 78 | <kbd>Shift</kbd> walk, mouse aims and fires, <kbd>R</kbd> reload, |
| 72 | <kbd>E</kbd> plants or defuses, <kbd>B</kbd> opens the buy menu in the freeze | |
| 79 | <kbd>E</kbd> plants, defuses or takes the gun you are standing on (the HUD says | |
| 80 | which gun when it is one you have to ask for), <kbd>B</kbd> opens the buy menu in the freeze | |
| 73 | 81 | time, <kbd>1</kbd>/<kbd>2</kbd>/<kbd>3</kbd> switch guns, <kbd>Tab</kbd> shows |
| 74 | 82 | the scoreboard, <kbd>Esc</kbd> pauses. On a touch screen the left of the view |
| 75 | 83 | is a thumb stick, the right looks around, and the buttons along the bottom do |
src/client/hud.test.ts +26
| @@ -163,6 +163,32 @@ test("a spectator does not inherit the dead player's hit markers", () => { | ||
| 163 | 163 | expect(own.calls.filter((c) => c.name === "moveTo").length).toBeGreaterThan(4); |
| 164 | 164 | }); |
| 165 | 165 | |
| 166 | test("a gun under your feet that you would have to ask for says so", () => { | |
| 167 | const world = createWorld(SHORT, "me", "ct", 5, 89); | |
| 168 | world.me.primary = "m4a4"; | |
| 169 | world.pickups.push({ | |
| 170 | weapon: "ak47", | |
| 171 | pos: [world.me.pos[0], world.me.pos[1], world.me.pos[2] + 4], | |
| 172 | ammo: { mag: 12, reserve: 90 }, | |
| 173 | }); | |
| 174 | const carrying = recorder(); | |
| 175 | draw(carrying, world); | |
| 176 | expect(carrying.calls.some((c) => c.text === "E — take AK-47")).toBe(true); | |
| 177 | ||
| 178 | // With no primary it is taken by walking over it, so there is nothing to ask. | |
| 179 | world.me.primary = null; | |
| 180 | const empty = recorder(); | |
| 181 | draw(empty, world); | |
| 182 | expect(empty.calls.some((c) => c.text.includes("take AK-47"))).toBe(false); | |
| 183 | ||
| 184 | // And a gun across the map is not under anyone's feet. | |
| 185 | world.me.primary = "m4a4"; | |
| 186 | world.pickups[0].pos = [world.me.pos[0] + 400, world.me.pos[1], world.me.pos[2]]; | |
| 187 | const far = recorder(); | |
| 188 | draw(far, world); | |
| 189 | expect(far.calls.some((c) => c.text.includes("take AK-47"))).toBe(false); | |
| 190 | }); | |
| 191 | ||
| 166 | 192 | test("the round result stays up through the end delay and the freeze that follows", () => { |
| 167 | 193 | const world = createWorld(SHORT, "me", "ct", 5, 83); |
| 168 | 194 | world.match.phase = "over"; |
src/client/hud.ts +12 −1
| @@ -12,7 +12,7 @@ import { angleDelta, clamp } from "./math.ts"; | ||
| 12 | 12 | import { MOVE } from "./physics.ts"; |
| 13 | 13 | import { deathNotice } from "./spectate.ts"; |
| 14 | 14 | import { inaccuracy, WEAPONS } from "./weapons.ts"; |
| 15 | import { currentWeapon, eyePos, useProgress, type Player, type World } from "./world.ts"; | |
| 15 | import { currentWeapon, eyePos, offeredPickup, useProgress, type Player, type World } from "./world.ts"; | |
| 16 | 16 | |
| 17 | 17 | const T_COLOR = "#e2b04a"; |
| 18 | 18 | const CT_COLOR = "#6fa8e8"; |
| @@ -264,6 +264,17 @@ export function drawHud( | ||
| 264 | 264 | ctx.fillText(message.text, width / 2, height * 0.22); |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | // --- a gun under your feet that you would have to ask for. A gun you have | |
| 268 | // no slot for is taken by walking over it and says nothing, because by the | |
| 269 | // time it could be read it is already in your hands. | |
| 270 | const offered = !spectating && view.alive ? offeredPickup(world, view) : null; | |
| 271 | if (offered !== null) { | |
| 272 | ctx.textAlign = "center"; | |
| 273 | ctx.font = mid; | |
| 274 | ctx.fillStyle = "rgba(255,255,255,0.92)"; | |
| 275 | ctx.fillText(`E — take ${WEAPONS[offered.weapon].name}`, width / 2, height * 0.58); | |
| 276 | } | |
| 277 | ||
| 267 | 278 | // --- planting or defusing |
| 268 | 279 | const progress = useProgress(world, view); |
| 269 | 280 | if (progress !== null) { |
src/client/models.ts +14
| @@ -101,6 +101,20 @@ export function bombMesh(): Mesh { | ||
| 101 | 101 | return finish(b); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | /** | |
| 105 | * A gun lying on the floor, built around the origin at its own feet: a barrel, | |
| 106 | * a receiver and a magazine, small enough to read as "something to walk over" | |
| 107 | * and big enough to see from across a site. | |
| 108 | */ | |
| 109 | export function pickupMesh(): Mesh { | |
| 110 | const b = builder(); | |
| 111 | const GUN = 7, GRIP = 6; | |
| 112 | cube(b, 6, 0, 3, 26, 3, 3, GUN); // barrel and receiver | |
| 113 | cube(b, -4, 0, 3, 10, 4, 6, GUN); // body | |
| 114 | cube(b, -2, 0, -1, 5, 3, 6, GRIP); // magazine, hanging under it | |
| 115 | return finish(b); | |
| 116 | } | |
| 117 | ||
| 104 | 118 | /** |
| 105 | 119 | * A first-person weapon, in view space: X right, Y up, Z back, so the barrel |
| 106 | 120 | * runs down -Z and the model needs no rotation at all. |
src/client/renderer.ts +13 −1
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import { multiply, perspective, viewMatrix, modelMatrix, type Vec3 } from "./math.ts"; |
| 9 | import { bombMesh, levelMesh, playerMesh, viewModelMesh, type Mesh } from "./models.ts"; | |
| 9 | import { bombMesh, levelMesh, pickupMesh, playerMesh, viewModelMesh, type Mesh } from "./models.ts"; | |
| 10 | 10 | import { MAX_SCALE, MIN_SCALE } from "./quality.ts"; |
| 11 | 11 | import { LAYERS, TEX_SIZE, materialTextures } from "./textures.ts"; |
| 12 | 12 | import type { World } from "./world.ts"; |
| @@ -175,6 +175,7 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 175 | 175 | let levelBuffers: Buffers | null = null; |
| 176 | 176 | const player = upload(gl, playerMesh()); |
| 177 | 177 | const bomb = upload(gl, bombMesh()); |
| 178 | const pickup = upload(gl, pickupMesh()); | |
| 178 | 179 | const viewModels = new Map<string, Buffers>(); |
| 179 | 180 | |
| 180 | 181 | // One buffer for every line drawn this frame: tracers, sparks, the bomb beam. |
| @@ -254,6 +255,17 @@ export function createRenderer(canvas: HTMLCanvasElement): Renderer | null { | ||
| 254 | 255 | gl.drawElements(gl.TRIANGLES, player.count, gl.UNSIGNED_INT, 0); |
| 255 | 256 | } |
| 256 | 257 | |
| 258 | // Guns their owners are not carrying any more, turning slowly on the spot | |
| 259 | // so a rifle in a doorway reads as something to take and not as rubble. | |
| 260 | if (world.pickups.length > 0) { | |
| 261 | gl.uniform3f(u.tint, 1.1, 1.05, 0.85); | |
| 262 | gl.bindVertexArray(pickup.vao); | |
| 263 | for (const item of world.pickups) { | |
| 264 | gl.uniformMatrix4fv(u.model, false, modelMatrix(item.pos, world.time * 45)); | |
| 265 | gl.drawElements(gl.TRIANGLES, pickup.count, gl.UNSIGNED_INT, 0); | |
| 266 | } | |
| 267 | } | |
| 268 | ||
| 257 | 269 | if (world.bomb.state === "planted" || world.bomb.state === "dropped") { |
| 258 | 270 | gl.uniformMatrix4fv(u.model, false, modelMatrix(world.bomb.pos, 0)); |
| 259 | 271 | gl.uniform3f(u.tint, 1, 1, 1); |
src/client/world.test.ts +139 −2
| @@ -3,8 +3,8 @@ import { siteAt, sites } from "./map.ts"; | ||
| 3 | 3 | import { SHORT } from "./match.ts"; |
| 4 | 4 | import { WEAPONS } from "./weapons.ts"; |
| 5 | 5 | import { |
| 6 | aimAt, applyInput, bodyCentre, buy, createWorld, currentWeapon, eyePos, flinch, NO_INPUT, shoot, | |
| 7 | startRound, update, type Input, type Player, type World, | |
| 6 | aimAt, applyInput, bodyCentre, buy, createWorld, currentWeapon, eyePos, flinch, NO_INPUT, | |
| 7 | offeredPickup, shoot, startRound, update, type Input, type Player, type World, | |
| 8 | 8 | } from "./world.ts"; |
| 9 | 9 | |
| 10 | 10 | const TICK = 1 / 64; |
| @@ -127,6 +127,143 @@ test("a new round puts everyone back with full health and a full magazine", () = | ||
| 127 | 127 | expect(world.players.filter((p) => p.hasBomb).length).toBe(1); |
| 128 | 128 | }); |
| 129 | 129 | |
| 130 | /** | |
| 131 | * Guns on the floor. The pickup is made the way the game makes one — a kill — | |
| 132 | * so the drop and the take are tested together and not against a fixture. | |
| 133 | */ | |
| 134 | ||
| 135 | /** Kill the person, holding an M4 with seven left in it, and leave it there. */ | |
| 136 | function dropRifle(world: World): Player { | |
| 137 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 138 | faceOff(world, shooter); | |
| 139 | world.me.primary = "m4a4"; | |
| 140 | world.me.weapon = "m4a4"; | |
| 141 | world.me.ammo.m4a4 = { mag: 7, reserve: 30 }; | |
| 142 | world.me.health = 1; | |
| 143 | world.me.armor = 0; | |
| 144 | expect(shoot(world, shooter)).toBe(true); | |
| 145 | expect(world.me.alive).toBe(false); | |
| 146 | return shooter; | |
| 147 | } | |
| 148 | ||
| 149 | /** A live player standing where the person fell, with whatever they carry. */ | |
| 150 | function standOnIt(world: World, primary: "ak47" | null): Player { | |
| 151 | // One of the bots faceOff stood down, put back on its feet on top of the gun. | |
| 152 | const taker = world.players.find((p) => p.team === "t" && !p.alive) as Player; | |
| 153 | taker.alive = true; | |
| 154 | taker.health = 100; | |
| 155 | taker.pos = [world.me.pos[0], world.me.pos[1], world.me.pos[2]]; | |
| 156 | taker.primary = primary; | |
| 157 | taker.weapon = primary ?? taker.secondary; | |
| 158 | if (primary !== null) taker.ammo[primary] = { mag: 30, reserve: 90 }; | |
| 159 | world.time += 1; // past the swap cooldown, which starts at zero anyway | |
| 160 | return taker; | |
| 161 | } | |
| 162 | ||
| 163 | test("a kill leaves exactly one gun where its owner fell, with the ammo it had", () => { | |
| 164 | const world = createWorld(SHORT, "me", "ct", 5, 101); | |
| 165 | dropRifle(world); | |
| 166 | expect(world.pickups.length).toBe(1); | |
| 167 | const gun = world.pickups[0]; | |
| 168 | expect(gun.weapon).toBe("m4a4"); | |
| 169 | expect(gun.ammo).toEqual({ mag: 7, reserve: 30 }); | |
| 170 | expect(Math.hypot(gun.pos[0] - world.me.pos[0], gun.pos[1] - world.me.pos[1])).toBeLessThan(1); | |
| 171 | }); | |
| 172 | ||
| 173 | test("a knife kill leaves nothing: a knife is never a pickup", () => { | |
| 174 | const world = createWorld(SHORT, "me", "ct", 5, 103); | |
| 175 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 176 | faceOff(world, shooter); | |
| 177 | world.me.primary = null; | |
| 178 | world.me.weapon = "knife"; | |
| 179 | world.me.health = 1; | |
| 180 | world.me.armor = 0; | |
| 181 | expect(shoot(world, shooter)).toBe(true); | |
| 182 | expect(world.me.alive).toBe(false); | |
| 183 | expect(world.pickups.length).toBe(0); | |
| 184 | }); | |
| 185 | ||
| 186 | test("a pistol-only player walks over the gun and comes away holding it", () => { | |
| 187 | const world = createWorld(SHORT, "me", "ct", 5, 107); | |
| 188 | dropRifle(world); | |
| 189 | const taker = standOnIt(world, null); | |
| 190 | applyInput(world, taker, NO_INPUT, TICK); | |
| 191 | expect(taker.primary).toBe("m4a4"); | |
| 192 | expect(taker.weapon).toBe("m4a4"); | |
| 193 | expect(taker.ammo.m4a4).toEqual({ mag: 7, reserve: 30 }); | |
| 194 | expect(world.pickups.length).toBe(0); | |
| 195 | }); | |
| 196 | ||
| 197 | test("a pistol is never swept up — the pistol slot is never free — but a rifle is", () => { | |
| 198 | const world = createWorld(SHORT, "me", "ct", 5, 131); | |
| 199 | dropRifle(world); | |
| 200 | const taker = standOnIt(world, null); | |
| 201 | world.pickups[0].pos = [taker.pos[0] + 30, taker.pos[1], taker.pos[2] + 4]; | |
| 202 | world.pickups.push({ | |
| 203 | weapon: "deagle", | |
| 204 | pos: [taker.pos[0], taker.pos[1], taker.pos[2] + 4], | |
| 205 | ammo: { mag: 7, reserve: 35 }, | |
| 206 | }); | |
| 207 | applyInput(world, taker, NO_INPUT, TICK); | |
| 208 | expect(taker.primary).toBe("m4a4"); // the rifle a step away, not the one underfoot | |
| 209 | expect(taker.secondary).toBe("glock"); | |
| 210 | expect(world.pickups.map((g) => g.weapon)).toEqual(["deagle"]); | |
| 211 | }); | |
| 212 | ||
| 213 | test("a player who already has a primary does not sweep one up, and E swaps", () => { | |
| 214 | const world = createWorld(SHORT, "me", "ct", 5, 109); | |
| 215 | dropRifle(world); | |
| 216 | const taker = standOnIt(world, "ak47"); | |
| 217 | applyInput(world, taker, NO_INPUT, TICK); | |
| 218 | expect(taker.primary).toBe("ak47"); | |
| 219 | expect(world.pickups.length).toBe(1); | |
| 220 | expect(offeredPickup(world, taker)?.weapon).toBe("m4a4"); | |
| 221 | ||
| 222 | applyInput(world, taker, { ...NO_INPUT, use: true }, TICK); | |
| 223 | expect(taker.primary).toBe("m4a4"); | |
| 224 | expect(taker.ammo.m4a4).toEqual({ mag: 7, reserve: 30 }); | |
| 225 | expect(taker.ammo.ak47).toBeUndefined(); // it is on the floor, not in the bag | |
| 226 | expect(world.pickups.map((g) => g.weapon)).toEqual(["ak47"]); | |
| 227 | expect(world.pickups[0].ammo).toEqual({ mag: 30, reserve: 90 }); | |
| 228 | // Holding E does not flip the two guns back and forth at 64 Hz. | |
| 229 | for (let i = 0; i < 8; i++) applyInput(world, taker, { ...NO_INPUT, use: true }, TICK); | |
| 230 | expect(taker.primary).toBe("m4a4"); | |
| 231 | }); | |
| 232 | ||
| 233 | test("the next round clears the floor and hands the picked-up gun back loaded", () => { | |
| 234 | const world = createWorld(SHORT, "me", "ct", 5, 113); | |
| 235 | dropRifle(world); | |
| 236 | const taker = standOnIt(world, "ak47"); | |
| 237 | applyInput(world, taker, { ...NO_INPUT, use: true }, TICK); | |
| 238 | expect(taker.primary).toBe("m4a4"); | |
| 239 | ||
| 240 | startRound(world); | |
| 241 | expect(world.pickups.length).toBe(0); | |
| 242 | expect(taker.primary).toBe("m4a4"); | |
| 243 | expect(taker.weapon).toBe("m4a4"); | |
| 244 | expect(taker.ammo.m4a4).toEqual({ mag: WEAPONS.m4a4.magazine, reserve: WEAPONS.m4a4.reserve }); | |
| 245 | expect(taker.ammo.ak47).toBeUndefined(); | |
| 246 | expect(taker.ammo[taker.secondary].mag).toBe(WEAPONS[taker.secondary].magazine); | |
| 247 | }); | |
| 248 | ||
| 249 | test("a long round cannot grow the floor without bound", () => { | |
| 250 | const world = createWorld(SHORT, "me", "ct", 5, 127); | |
| 251 | const shooter = world.players.find((p) => p.team === "t") as Player; | |
| 252 | faceOff(world, shooter); | |
| 253 | for (let i = 0; i < 60; i++) { | |
| 254 | world.me.alive = true; | |
| 255 | world.me.health = 1; | |
| 256 | world.me.armor = 0; | |
| 257 | world.me.primary = "m4a4"; | |
| 258 | world.me.weapon = "m4a4"; | |
| 259 | world.me.ammo.m4a4 = { mag: 7, reserve: 30 }; | |
| 260 | shooter.nextShotAt = 0; | |
| 261 | world.time += 1; | |
| 262 | shoot(world, shooter); | |
| 263 | } | |
| 264 | expect(world.pickups.length).toBeLessThanOrEqual(24); | |
| 265 | }); | |
| 266 | ||
| 130 | 267 | /** The damage the person took, narrowed out of the effects list. */ |
| 131 | 268 | const hurtsIn = (world: World): { at: number; victim: number; from: number[]; amount: number }[] => |
| 132 | 269 | world.effects.flatMap((e) => (e.kind === "hurt" ? [e] : [])); |
src/client/world.ts +117 −5
| @@ -42,6 +42,7 @@ export type Player = Actor & Body & { | ||
| 42 | 42 | scoped: boolean; |
| 43 | 43 | firedThisPress: boolean; |
| 44 | 44 | punch: [number, number]; // view kick, decaying |
| 45 | nextPickupAt: number; // no swapping guns again until this, so E cannot flip-flop | |
| 45 | 46 | kills: number; |
| 46 | 47 | deaths: number; |
| 47 | 48 | headshots: number; |
| @@ -59,6 +60,13 @@ export type Bomb = { | ||
| 59 | 60 | defuser: Player | null; |
| 60 | 61 | }; |
| 61 | 62 | |
| 63 | /** A gun lying on the floor: what it is, where it fell, and what was left in it. */ | |
| 64 | export type Pickup = { | |
| 65 | weapon: WeaponId; | |
| 66 | pos: Vec3; | |
| 67 | ammo: Ammo; | |
| 68 | }; | |
| 69 | ||
| 62 | 70 | export type Effect = |
| 63 | 71 | | { kind: "tracer"; from: Vec3; to: Vec3; at: number } |
| 64 | 72 | | { kind: "impact"; at: number; pos: Vec3; normal: Vec3 } |
| @@ -107,6 +115,7 @@ export type World = { | ||
| 107 | 115 | me: Player; |
| 108 | 116 | time: number; |
| 109 | 117 | bomb: Bomb; |
| 118 | pickups: Pickup[]; | |
| 110 | 119 | effects: Effect[]; |
| 111 | 120 | feed: FeedEntry[]; |
| 112 | 121 | messages: { at: number; text: string }[]; |
| @@ -135,7 +144,7 @@ function makePlayer(id: number, team: Team, name: string, bot: boolean): Player | ||
| 135 | 144 | ammo: { knife: { mag: 0, reserve: 0 }, [secondary]: emptyAmmo(secondary) }, |
| 136 | 145 | defuseKit: false, hasBomb: false, |
| 137 | 146 | nextShotAt: 0, reloadUntil: 0, shotIndex: 0, lastShotAt: -99, scoped: false, firedThisPress: false, |
| 138 | punch: [0, 0], | |
| 147 | punch: [0, 0], nextPickupAt: 0, | |
| 139 | 148 | kills: 0, deaths: 0, headshots: 0, damage: 0, spawnAt: 0, stepDistance: 0, |
| 140 | 149 | }; |
| 141 | 150 | } |
| @@ -165,6 +174,7 @@ export function createWorld( | ||
| 165 | 174 | const world: World = { |
| 166 | 175 | match, players, me, time: 0, |
| 167 | 176 | bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null }, |
| 177 | pickups: [], | |
| 168 | 178 | effects: [], feed: [], messages: [], |
| 169 | 179 | random, boxes: map.boxes, bombPlanted: false, pendingBuy: false, |
| 170 | 180 | }; |
| @@ -201,12 +211,14 @@ export function startRound(world: World): void { | ||
| 201 | 211 | p.scoped = false; |
| 202 | 212 | p.stepDistance = 0; |
| 203 | 213 | p.spawnAt = world.time; |
| 214 | p.nextPickupAt = 0; | |
| 204 | 215 | p.yaw = p.team === "t" ? 90 : -90; |
| 205 | 216 | p.pitch = 0; |
| 206 | for (const id of Object.keys(p.ammo)) { | |
| 207 | const w = WEAPONS[id as WeaponId]; | |
| 208 | if (w !== undefined) p.ammo[id] = emptyAmmo(w.id); | |
| 209 | } | |
| 217 | // The magazines are rebuilt from the two slots rather than topped up in | |
| 218 | // place: a gun taken off a body is in `p.primary`/`p.secondary` and nowhere | |
| 219 | // else, and a gun swapped away is gone from the list the same way. | |
| 220 | p.ammo = { knife: { mag: 0, reserve: 0 }, [p.secondary]: emptyAmmo(p.secondary) }; | |
| 221 | if (p.primary !== null) p.ammo[p.primary] = emptyAmmo(p.primary); | |
| 210 | 222 | p.weapon = p.primary ?? p.secondary; |
| 211 | 223 | if (p.brain !== undefined) p.brain.reset = true; |
| 212 | 224 | } |
| @@ -217,6 +229,7 @@ export function startRound(world: World): void { | ||
| 217 | 229 | world.bomb = { state: "carried", pos: [...carrier.pos] as Vec3, site: null, plantProgress: 0, defuseProgress: 0, defuser: null }; |
| 218 | 230 | } |
| 219 | 231 | world.bombPlanted = false; |
| 232 | world.pickups.length = 0; | |
| 220 | 233 | world.feed = []; |
| 221 | 234 | for (const p of world.players) if (p.bot) botBuy(world, p); |
| 222 | 235 | } |
| @@ -426,9 +439,107 @@ function onKill(world: World, killer: Player, victim: Player, weapon: WeaponId, | ||
| 426 | 439 | world.bomb.state = "dropped"; |
| 427 | 440 | world.bomb.pos = [victim.pos[0], victim.pos[1], victim.pos[2] + 4]; |
| 428 | 441 | } |
| 442 | // The gun they were holding stays where they fell. A knife is never a pickup, | |
| 443 | // so someone killed holding one leaves their primary instead — or nothing. | |
| 444 | dropWeapon(world, victim, victim.weapon === "knife" ? victim.primary : victim.weapon); | |
| 429 | 445 | checkRoundEnd(world); |
| 430 | 446 | } |
| 431 | 447 | |
| 448 | // --- guns on the floor ---------------------------------------------------- | |
| 449 | ||
| 450 | /** | |
| 451 | * The rule, once: a gun you have no slot for is taken by walking over it, and a | |
| 452 | * gun that would replace one you are carrying takes an explicit `use`, which | |
| 453 | * leaves the one you were holding where you stand. Bots get no new code — the | |
| 454 | * automatic half is enough, and their paths already take them past bodies. | |
| 455 | * | |
| 456 | * There is no team rule. This match has no friendly fire and a CT who picks up | |
| 457 | * an AK-47 keeps it; inventing a restriction CS does not have would only make | |
| 458 | * the floor confusing. | |
| 459 | */ | |
| 460 | const PICKUP_RANGE = 48; | |
| 461 | /** A long round cannot grow the floor without bound; the oldest gun goes. */ | |
| 462 | const MAX_PICKUPS = 24; | |
| 463 | /** How long after a swap before another one: without it, holding E flip-flops. */ | |
| 464 | const PICKUP_COOLDOWN = 0.75; | |
| 465 | ||
| 466 | /** Which slot a gun lives in. A pistol is a secondary; everything else primary. */ | |
| 467 | export const pickupSlot = (id: WeaponId): "primary" | "secondary" => | |
| 468 | WEAPONS[id]?.kind === "pistol" ? "secondary" : "primary"; | |
| 469 | ||
| 470 | /** Leave `id` on the floor under `p`, with the ammunition `p` had left in it. */ | |
| 471 | function dropWeapon(world: World, p: Player, id: WeaponId | null): void { | |
| 472 | if (id === null || WEAPONS[id] === undefined || WEAPONS[id].kind === "knife") return; | |
| 473 | const ammo = p.ammo[id] ?? emptyAmmo(id); | |
| 474 | world.pickups.push({ | |
| 475 | weapon: id, | |
| 476 | pos: [p.pos[0], p.pos[1], p.pos[2] + 4], | |
| 477 | ammo: { mag: ammo.mag, reserve: ammo.reserve }, | |
| 478 | }); | |
| 479 | if (world.pickups.length > MAX_PICKUPS) world.pickups.shift(); | |
| 480 | } | |
| 481 | ||
| 482 | /** | |
| 483 | * The closest gun within arm's reach of `p`, if there is one. `freeSlotOnly` | |
| 484 | * asks for the closest one they have a hand free for — a rifle two steps away | |
| 485 | * beats the pistol they are standing on, which they could not take anyway. | |
| 486 | */ | |
| 487 | function nearestPickup(world: World, p: Player, freeSlotOnly = false): Pickup | null { | |
| 488 | let best: Pickup | null = null; | |
| 489 | let bestDistance = PICKUP_RANGE; | |
| 490 | for (const pickup of world.pickups) { | |
| 491 | if (freeSlotOnly && p[pickupSlot(pickup.weapon)] !== null) continue; | |
| 492 | const d = dist(p.pos, pickup.pos); | |
| 493 | if (d < bestDistance) { | |
| 494 | bestDistance = d; | |
| 495 | best = pickup; | |
| 496 | } | |
| 497 | } | |
| 498 | return best; | |
| 499 | } | |
| 500 | ||
| 501 | /** | |
| 502 | * The gun under your feet that you would have to ask for, if any: the HUD's | |
| 503 | * `E — take AK-47` prompt, and nothing else reads it. | |
| 504 | */ | |
| 505 | export function offeredPickup(world: World, p: Player): Pickup | null { | |
| 506 | if (world.pickups.length === 0 || !p.alive) return null; | |
| 507 | const pickup = nearestPickup(world, p); | |
| 508 | if (pickup === null) return null; | |
| 509 | return p[pickupSlot(pickup.weapon)] === null ? null : pickup; | |
| 510 | } | |
| 511 | ||
| 512 | function takePickup(world: World, p: Player, pickup: Pickup): void { | |
| 513 | const slot = pickupSlot(pickup.weapon); | |
| 514 | const held = p[slot]; | |
| 515 | const index = world.pickups.indexOf(pickup); | |
| 516 | if (index >= 0) world.pickups.splice(index, 1); | |
| 517 | if (held !== null && held !== pickup.weapon) dropWeapon(world, p, held); | |
| 518 | if (held !== null) delete p.ammo[held]; | |
| 519 | p[slot] = pickup.weapon; | |
| 520 | p.ammo[pickup.weapon] = { mag: pickup.ammo.mag, reserve: pickup.ammo.reserve }; | |
| 521 | p.weapon = pickup.weapon; | |
| 522 | p.shotIndex = 0; | |
| 523 | p.reloadUntil = 0; | |
| 524 | p.scoped = false; | |
| 525 | p.nextPickupAt = world.time + PICKUP_COOLDOWN; | |
| 526 | } | |
| 527 | ||
| 528 | /** | |
| 529 | * Walk over a gun, or ask for one. The empty list is the first thing tested: | |
| 530 | * the training arena steps this millions of times a second and never puts a | |
| 531 | * gun on its floor, so this costs one comparison there. | |
| 532 | */ | |
| 533 | function updatePickups(world: World, p: Player, input: Input): void { | |
| 534 | if (world.pickups.length === 0 || world.time < p.nextPickupAt) return; | |
| 535 | const free = nearestPickup(world, p, true); | |
| 536 | if (free !== null) return takePickup(world, p, free); | |
| 537 | // `use` is one button: while it is planting or defusing it is not swapping guns. | |
| 538 | if (!input.use || useProgress(world, p) !== null) return; | |
| 539 | const asked = nearestPickup(world, p); | |
| 540 | if (asked !== null) takePickup(world, p, asked); | |
| 541 | } | |
| 542 | ||
| 432 | 543 | // --- the bomb ------------------------------------------------------------- |
| 433 | 544 | |
| 434 | 545 | const PLANT_RANGE = 64; |
| @@ -552,6 +663,7 @@ export function applyInput(world: World, p: Player, input: Input, dt: number): v | ||
| 552 | 663 | if (world.time - p.lastShotAt > 0.35) p.shotIndex = 0; |
| 553 | 664 | p.punch = [p.punch[0] * Math.pow(0.02, dt), p.punch[1] * Math.pow(0.02, dt)]; |
| 554 | 665 | updateBomb(world, p, input, dt); |
| 666 | updatePickups(world, p, input); | |
| 555 | 667 | } |
| 556 | 668 | |
| 557 | 669 | /** One fixed simulation tick. `input` is the person's; bots think for themselves. */ |
src/pages/game.ts +1 −1
| @@ -137,7 +137,7 @@ export function gamePage(db: Database, options: PageOptions): string { | ||
| 137 | 137 | </div> |
| 138 | 138 | <div class="row"><button type="button" class="primary" id="play">Play</button></div> |
| 139 | 139 | <p class="hint"><kbd>WASD</kbd> move · <kbd>Space</kbd> jump · <kbd>Ctrl</kbd> crouch · <kbd>Shift</kbd> walk · |
| 140 | mouse to aim and fire · <kbd>R</kbd> reload · <kbd>E</kbd> plant or defuse · <kbd>B</kbd> buy · | |
| 140 | mouse to aim and fire · <kbd>R</kbd> reload · <kbd>E</kbd> plant, defuse or take a gun · <kbd>B</kbd> buy · | |
| 141 | 141 | <kbd>1</kbd><kbd>2</kbd><kbd>3</kbd> guns · <kbd>Tab</kbd> scores · <kbd>Esc</kbd> pause</p> |
| 142 | 142 | </div> |
| 143 | 143 | <div class="overlay" id="pause" hidden> |
src/rl/arena.ts +3
| @@ -167,6 +167,9 @@ export function reset(arena: Arena): void { | ||
| 167 | 167 | world.effects.length = 0; |
| 168 | 168 | world.feed.length = 0; |
| 169 | 169 | world.messages.length = 0; |
| 170 | // A duel that ended in a kill left a rifle on the floor. Nothing here wants | |
| 171 | // it, and an empty list is what keeps the pickup test in applyInput free. | |
| 172 | world.pickups.length = 0; | |
| 170 | 173 | arena.ticks = 0; |
| 171 | 174 | |
| 172 | 175 | equip(learner, "m4a4"); |
static/app.d616a602f9.js added +58
Diff not shown — a line 62,054 characters wide (limit 2,000). +58
View filestatic/app.f7b32033ed.js deleted −58
Diff not shown — a line 60,526 characters wide (limit 2,000). −58
View file
new above
offline