Split The code is public; the app answers only to the crew.
The bomb gets planted: one site for the team, an escort for the carrier and an eye on the clock #33
Check · Review · Land · Live done — Landed and live in production.
vesper/cs:vesper/27-the-bomb-gets-planted-one-site-for-the-t → main 1 commit fork 2 threads · 2 unresolved
README.md +20 −7
| @@ -2,8 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | A Counter-Strike-shaped defusal shooter that runs in the browser, and the |
| 4 | 4 | leaderboard of the matches people played. Five against five on one map: |
| 5 | bomb rounds, a buy menu and an economy, bots that walk to a site and shoot | |
| 6 | back. | |
| 5 | bomb rounds, a buy menu and an economy, bots that pick a site as a side, get | |
| 6 | the bomb down and shoot back. | |
| 7 | 7 | |
| 8 | 8 | ## Run it locally |
| 9 | 9 | |
| @@ -135,7 +135,8 @@ with the canvas as you turn; nobody re-walks the grid to draw it. | ||
| 135 | 135 | |
| 136 | 136 | - `src/client/` — the game, in modules named for one thing each: `math`, `map`, |
| 137 | 137 | `physics`, `weapons`, `combat`, `match` (the rules), `world` (the simulation), |
| 138 | `bots`, `observe`, `action`, `policy`, `weights`, `models`, `textures`, | |
| 138 | `bots`, `plan` (which site a side commits to, and when the carrier has run out | |
| 139 | of clock), `observe`, `action`, `policy`, `weights`, `models`, `textures`, | |
| 139 | 140 | `press` (the input latch), `quality` (the render scale the frame time buys), |
| 140 | 141 | `spectate` (whose eyes you borrow when you are dead), `bodies` (the dead of |
| 141 | 142 | this round, where they fell), `radar` (the blips the |
| @@ -174,10 +175,22 @@ starts the app. `forge.json` says how. | ||
| 174 | 175 | |
| 175 | 176 | ## The bots |
| 176 | 177 | |
| 177 | They are split in two. Getting to the site is a plan, and A* over the map grid | |
| 178 | already solves it, so that stayed as it was: pathing, buying, planting, | |
| 179 | defusing, listening for shots through walls. The fight is a reflex, and that is | |
| 180 | now a policy learned with PPO. | |
| 178 | They are split in two. Getting to the site is a plan — `src/client/plan.ts` | |
| 179 | decides it, A* over the map grid walks it, and the rest of it is ordinary code: | |
| 180 | buying, planting, defusing, picking a dropped bomb back up, listening for shots | |
| 181 | through walls. The fight is a reflex, and that is a policy learned with PPO. | |
| 182 | ||
| 183 | The plan is one site a side a round, rolled from the match's seed and the round | |
| 184 | number rather than by each bot for itself, so the five terrorists arrive | |
| 185 | together and the carrier is not walking in alone. The counter-terrorists stack | |
| 186 | that roll three and two, so neither site is ever empty, and they turn towards | |
| 187 | the site the last of them died at — the kill feed and the radar, nothing they | |
| 188 | could not see. The clock is part of it: once what is left of the round is no | |
| 189 | more than the walk plus the plant, the carrier stops trading ground for duels | |
| 190 | and goes, with the policy still aiming and still firing on the way. Measured | |
| 191 | headless over twenty seeded `SHORT` matches, before and after: the bomb is | |
| 192 | planted in 65% of rounds rather than 38%, and 10% of rounds end on the clock | |
| 193 | rather than 37%. | |
| 181 | 194 | |
| 182 | 195 | - **What it sees** — 32 numbers, all egocentric (`src/client/observe.ts`): the |
| 183 | 196 | sine and cosine of its aim error, the range, the enemy's health and whether it |
src/client/bots.test.ts +56 −2
| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { applyIntent, botThink, CARD, makeBrain, policy, think } from "./bots.ts"; |
| 3 | import { COMPETITIVE } from "./match.ts"; | |
| 4 | import { aimAt, bodyCentre, createWorld, type Player } from "./world.ts"; | |
| 3 | import * as map from "./map.ts"; | |
| 4 | import { COMPETITIVE, SHORT } from "./match.ts"; | |
| 5 | import { aimAt, bodyCentre, createWorld, NO_INPUT, update, type Player } from "./world.ts"; | |
| 5 | 6 | |
| 6 | 7 | const duel = (seed = 2024): { world: ReturnType<typeof createWorld>; me: Player; bot: Player } => { |
| 7 | 8 | const world = createWorld(COMPETITIVE, "you", "ct", 1, seed); |
| @@ -88,3 +89,56 @@ test("the card describes a real training run", () => { | ||
| 88 | 89 | expect(CARD.winRate).toBeLessThanOrEqual(100); |
| 89 | 90 | expect(CARD.trainedAt).toMatch(/^\d{4}-\d{2}-\d{2}$/); |
| 90 | 91 | }); |
| 92 | ||
| 93 | // --- the round's plan ----------------------------------------------------- | |
| 94 | ||
| 95 | /** | |
| 96 | * A round played out headlessly: every player, the person's slot included, is | |
| 97 | * driven by the same bot code, so this is a real round with nobody watching. | |
| 98 | * Three a side and a half-minute clock: the whole suite is a few seconds, and | |
| 99 | * a full 1:55 round with ten players in it is most of one on its own. | |
| 100 | */ | |
| 101 | const RULES = { ...SHORT, freezeTime: 0.5, roundTime: 30 }; | |
| 102 | ||
| 103 | function playRound(seed: number, teamSize = 3): { planted: boolean; left: number } { | |
| 104 | const world = createWorld(RULES, "you", "ct", teamSize, seed); | |
| 105 | for (let t = 0; t < 64 * 70; t++) { | |
| 106 | update(world, 1 / 64, botThink(world, world.me, 1 / 64)); | |
| 107 | if (world.bomb.state === "planted") return { planted: true, left: world.match.timer }; | |
| 108 | if (world.match.phase === "over" || world.match.phase === "match_over") break; | |
| 109 | } | |
| 110 | return { planted: false, left: 0 }; | |
| 111 | } | |
| 112 | ||
| 113 | test("a side walks in together: every terrorist reads the round's own site", () => { | |
| 114 | const world = createWorld(SHORT, "you", "ct", 5, 20240); | |
| 115 | for (let t = 0; t < 4; t++) for (const p of world.players) if (p.bot) botThink(world, p, 1 / 64); | |
| 116 | const ts = world.players.filter((p) => p.team === "t" && p.bot); | |
| 117 | expect(ts.length).toBe(5); | |
| 118 | for (const t of ts) expect(t.brain?.site).toBe(world.plan.push); | |
| 119 | // And the defence covers both sites rather than all standing on one. | |
| 120 | const cts = world.players.filter((p) => p.team === "ct" && p.bot).map((p) => p.brain?.site); | |
| 121 | expect(new Set(cts).size).toBe(2); | |
| 122 | }); | |
| 123 | ||
| 124 | test("the carrier gets the bomb down inside the round clock", () => { | |
| 125 | const seeds = [11, 23, 37, 51, 67]; | |
| 126 | const planted = seeds.filter((seed) => playRound(seed).planted); | |
| 127 | // Not every round: a carrier that loses the fight on the way is a round the | |
| 128 | // terrorists lost, and that is allowed to happen. Four of five, though — | |
| 129 | // where the code this replaced planted in none of these five, because a bot | |
| 130 | // that rolled its own site never arrived anywhere with anybody. | |
| 131 | expect(planted.length).toBeGreaterThanOrEqual(4); | |
| 132 | }); | |
| 133 | ||
| 134 | test("a bomb on the floor is still picked up by a living terrorist", () => { | |
| 135 | const world = createWorld(RULES, "you", "ct", 1, 4242); | |
| 136 | world.match.phase = "live"; | |
| 137 | const bot = world.players.find((p) => p.team === "t") as Player; | |
| 138 | world.me.pos = [map.sites.A[0], map.sites.A[1], 2]; // out of the way, and out of sight | |
| 139 | bot.hasBomb = false; | |
| 140 | world.bomb = { ...world.bomb, state: "dropped", pos: [bot.pos[0] + 240, bot.pos[1], bot.pos[2] + 4] }; | |
| 141 | for (let t = 0; t < 64 * 6 && world.bomb.state === "dropped"; t++) update(world, 1 / 64, NO_INPUT); | |
| 142 | expect(world.bomb.state).toBe("carried"); | |
| 143 | expect(bot.hasBomb).toBe(true); | |
| 144 | }); |
src/client/bots.ts +133 −31
| @@ -12,9 +12,10 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | 14 | import { decode, HEADS, STILL, type Intent } from "./action.ts"; |
| 15 | import { angleDelta, clamp, dist, rng, type Vec3 } from "./math.ts"; | |
| 15 | import { angleDelta, clamp, dist, dist2d, rng, type Vec3 } from "./math.ts"; | |
| 16 | 16 | import * as map from "./map.ts"; |
| 17 | 17 | import { observe, OBS_SIZE, type Memory } from "./observe.ts"; |
| 18 | import { mustCommit, siteFor, type Site } from "./plan.ts"; | |
| 18 | 19 | import { |
| 19 | 20 | forward, newActivations, sample, unpack, type Activations, type Policy, |
| 20 | 21 | } from "./policy.ts"; |
| @@ -52,7 +53,7 @@ export type BotBrain = { | ||
| 52 | 53 | strafeUntil: number; |
| 53 | 54 | suspect: Vec3 | null; |
| 54 | 55 | plannedFor: string; |
| 55 | site: "A" | "B"; | |
| 56 | site: Site; // the round's plan, read once when the round starts | |
| 56 | 57 | reset: boolean; |
| 57 | 58 | random: () => number; |
| 58 | 59 | // What the policy needs to run, one set per bot so nothing is shared. |
| @@ -115,6 +116,76 @@ function brainOf(world: World, bot: Player): BotBrain { | ||
| 115 | 116 | return bot.brain; |
| 116 | 117 | } |
| 117 | 118 | |
| 119 | /** This bot's place in its side's list, so the plan can hand out two sites. */ | |
| 120 | function slotOf(world: World, bot: Player): number { | |
| 121 | let slot = 0; | |
| 122 | for (const p of world.players) if (p.team === bot.team && p.id < bot.id) slot += 1; | |
| 123 | return slot; | |
| 124 | } | |
| 125 | ||
| 126 | /** What is left of the walk this bot has already planned, in map units. */ | |
| 127 | function pathLeft(brain: BotBrain, from: Vec3): number { | |
| 128 | let total = 0; | |
| 129 | let prev = from; | |
| 130 | for (let i = brain.step; i < brain.path.length; i++) { | |
| 131 | const at = brain.path[i] as Vec3; | |
| 132 | total += dist2d(prev, at); | |
| 133 | prev = at; | |
| 134 | } | |
| 135 | return total; | |
| 136 | } | |
| 137 | ||
| 138 | /** A crow flight underestimates a walk around a wall by about this much. */ | |
| 139 | const DETOUR = 1.3; | |
| 140 | ||
| 141 | /** | |
| 142 | * Has the carrier run out of clock? What is left of the round is no more than | |
| 143 | * the walk to its site plus the plant, so a duel on the way is a round lost on | |
| 144 | * the timer. The bot that reads true here keeps its gun — the policy still | |
| 145 | * points it and still fires it — but its feet stop negotiating. | |
| 146 | */ | |
| 147 | function committed(world: World, bot: Player, brain: BotBrain): boolean { | |
| 148 | if (!bot.hasBomb || world.match.phase !== "live") return false; | |
| 149 | const crow = dist2d(bot.pos, map.sites[brain.site]) * DETOUR; | |
| 150 | const walk = Math.max(crow, pathLeft(brain, bot.pos)); | |
| 151 | return mustCommit(world.match.timer, walk, world.match.rules.plantTime); | |
| 152 | } | |
| 153 | ||
| 154 | /** A death this far from a site centre is a fight over that site. */ | |
| 155 | const CONTACT_RANGE = 1600; | |
| 156 | ||
| 157 | /** Which site a fight at `pos` is a fight over, if it is near enough to be one. */ | |
| 158 | function contactSite(pos: Vec3): Site | null { | |
| 159 | const a = dist2d(pos, map.sites.A); | |
| 160 | const b = dist2d(pos, map.sites.B); | |
| 161 | if (Math.min(a, b) > CONTACT_RANGE) return null; | |
| 162 | return a <= b ? "A" : "B"; | |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * Where the round is being fought, as far as this side can tell: the site the | |
| 167 | * last of them to die fell at. It is only ever this side's own information — | |
| 168 | * who is dead and where they were, which is the kill feed and the radar — and | |
| 169 | * it is the answer to a side that commits to one site together. The first CT | |
| 170 | * down at A is what turns the B players around; without it, five together | |
| 171 | * beats three and two nearly every round. | |
| 172 | */ | |
| 173 | function rotateTo(world: World, bot: Player): Site | null { | |
| 174 | let site: Site | null = null; | |
| 175 | let freshest = -Infinity; | |
| 176 | for (const entry of world.feed) { | |
| 177 | if (entry.killerTeam === bot.team || entry.at < freshest) continue; | |
| 178 | for (const p of world.players) { | |
| 179 | if (p.id !== entry.victimId || p.team !== bot.team) continue; | |
| 180 | const where = contactSite(p.pos); | |
| 181 | if (where === null) continue; | |
| 182 | freshest = entry.at; | |
| 183 | site = where; | |
| 184 | } | |
| 185 | } | |
| 186 | return site; | |
| 187 | } | |
| 188 | ||
| 118 | 189 | /** Where this bot is trying to be, given what the round is doing. */ |
| 119 | 190 | function chooseGoal(world: World, bot: Player, brain: BotBrain): Vec3 { |
| 120 | 191 | const bomb = world.bomb; |
| @@ -128,7 +199,8 @@ function chooseGoal(world: World, bot: Player, brain: BotBrain): Vec3 { | ||
| 128 | 199 | return nearby(brain, map.sites[brain.site], 200); |
| 129 | 200 | } |
| 130 | 201 | if (bomb.state === "planted") return bomb.pos; |
| 131 | return nearby(brain, map.sites[brain.site], 250); | |
| 202 | const contact = rotateTo(world, bot); | |
| 203 | return nearby(brain, map.sites[contact ?? brain.site], 250); | |
| 132 | 204 | } |
| 133 | 205 | |
| 134 | 206 | const nearby = (brain: BotBrain, at: Vec3, spread: number): Vec3 => { |
| @@ -256,13 +328,51 @@ function turnTowards(bot: Player, target: { yaw: number; pitch: number }, speed: | ||
| 256 | 328 | bot.pitch = clamp(bot.pitch + clamp(dp, -maxTurn, maxTurn), -89, 89); |
| 257 | 329 | } |
| 258 | 330 | |
| 331 | /** | |
| 332 | * Ask for a path when there is none, when it runs out, when it goes stale, or | |
| 333 | * when the objective itself changed under the bot's feet. A* is only ever run | |
| 334 | * here, which is a few times a round and never per tick. | |
| 335 | */ | |
| 336 | function repath(world: World, bot: Player, brain: BotBrain): void { | |
| 337 | const objective = `${world.bomb.state}:${brain.site}`; | |
| 338 | const stale = | |
| 339 | brain.path.length === 0 || brain.step >= brain.path.length || | |
| 340 | world.time > brain.repathAt || objective !== brain.plannedFor; | |
| 341 | if (!stale) return; | |
| 342 | brain.goal = chooseGoal(world, bot, brain); | |
| 343 | brain.path = map.findPath(bot.pos, brain.goal); | |
| 344 | brain.step = 0; | |
| 345 | brain.plannedFor = objective; | |
| 346 | brain.repathAt = world.time + 3 + brain.random() * 3; | |
| 347 | } | |
| 348 | ||
| 349 | /** | |
| 350 | * Point the feet at the next waypoint and return how far off the view is from | |
| 351 | * it, in degrees, or null when the path has run out. The movement is in the | |
| 352 | * bot's own frame, so this works whether the view is following the path or | |
| 353 | * looking down the barrel of something else. | |
| 354 | */ | |
| 355 | function follow(bot: Player, brain: BotBrain, input: Input): number | null { | |
| 356 | const next = brain.path[brain.step]; | |
| 357 | if (next === undefined) { | |
| 358 | brain.goal = null; | |
| 359 | return null; | |
| 360 | } | |
| 361 | if (dist2d(bot.pos, next) < 48) brain.step += 1; | |
| 362 | const off = angleDelta(bot.yaw, aimAt(bot, [next[0], next[1], bot.pos[2] + 48]).yaw); | |
| 363 | input.forward = Math.cos((off * Math.PI) / 180); | |
| 364 | input.side = -Math.sin((off * Math.PI) / 180); | |
| 365 | return off; | |
| 366 | } | |
| 367 | ||
| 259 | 368 | /** One bot's decision for this tick, expressed as the same input a person gives. */ |
| 260 | 369 | export function botThink(world: World, bot: Player, dt: number): Input { |
| 261 | 370 | const brain = brainOf(world, bot); |
| 262 | 371 | const input: Input = { ...NO_INPUT }; |
| 263 | 372 | if (brain.reset) { |
| 264 | 373 | brain.reset = false; |
| 265 | brain.site = brain.random() < 0.5 ? "A" : "B"; | |
| 374 | // The round's plan, not a coin of this bot's own: the side arrives together. | |
| 375 | brain.site = siteFor(world.plan, bot.team, slotOf(world, bot)); | |
| 266 | 376 | brain.goal = null; |
| 267 | 377 | brain.path = []; |
| 268 | 378 | brain.targetId = null; |
| @@ -302,38 +412,30 @@ export function botThink(world: World, bot: Player, dt: number): Input { | ||
| 302 | 412 | // decisions and each decision is worth one pull. |
| 303 | 413 | const held = WEAPONS[bot.weapon]?.automatic !== false || brain.hold === SKIP - 1; |
| 304 | 414 | input.fire = intent.fire && held && world.time - brain.seenAt > brain.reaction; |
| 305 | input.forward = intent.forward; | |
| 306 | input.side = intent.side; | |
| 307 | input.walk = intent.walk; | |
| 308 | input.duck = intent.duck; | |
| 415 | if (committed(world, bot, brain)) { | |
| 416 | // Out of clock: the policy keeps the gun, the plan takes the feet. It | |
| 417 | // runs to the site shooting at whatever it is looking at rather than | |
| 418 | // trading ground for a duel it cannot afford to win slowly. | |
| 419 | repath(world, bot, brain); | |
| 420 | follow(bot, brain, input); | |
| 421 | input.walk = false; | |
| 422 | input.duck = false; | |
| 423 | } else { | |
| 424 | input.forward = intent.forward; | |
| 425 | input.side = intent.side; | |
| 426 | input.walk = intent.walk; | |
| 427 | input.duck = intent.duck; | |
| 428 | } | |
| 309 | 429 | return withBomb(world, bot, input); |
| 310 | 430 | } |
| 311 | 431 | |
| 312 | // --- moving. A path is asked for when there is none, when it runs out, when | |
| 313 | // it goes stale, or when the objective itself changed under the bot's feet. | |
| 314 | const objective = `${world.bomb.state}:${brain.site}`; | |
| 315 | const stale = | |
| 316 | brain.path.length === 0 || brain.step >= brain.path.length || | |
| 317 | world.time > brain.repathAt || objective !== brain.plannedFor; | |
| 318 | if (stale) { | |
| 319 | brain.goal = chooseGoal(world, bot, brain); | |
| 320 | brain.path = map.findPath(bot.pos, brain.goal); | |
| 321 | brain.step = 0; | |
| 322 | brain.plannedFor = objective; | |
| 323 | brain.repathAt = world.time + 3 + brain.random() * 3; | |
| 324 | } | |
| 325 | const next = brain.path[brain.step]; | |
| 326 | if (next !== undefined) { | |
| 327 | if (dist([bot.pos[0], bot.pos[1], 0], [next[0], next[1], 0]) < 48) brain.step += 1; | |
| 328 | const aim = aimAt(bot, [next[0], next[1], bot.pos[2] + 48]); | |
| 329 | const look = brain.suspect !== null ? aimAt(bot, brain.suspect) : { yaw: aim.yaw, pitch: 0 }; | |
| 432 | // --- moving | |
| 433 | repath(world, bot, brain); | |
| 434 | const off = follow(bot, brain, input); | |
| 435 | if (off !== null) { | |
| 436 | const look = brain.suspect !== null ? aimAt(bot, brain.suspect) : { yaw: bot.yaw + off, pitch: 0 }; | |
| 330 | 437 | turnTowards(bot, { yaw: look.yaw, pitch: clamp(look.pitch, -20, 20) }, brain.aimSpeed * 0.5, dt); |
| 331 | const off = angleDelta(bot.yaw, aim.yaw); | |
| 332 | input.forward = Math.cos((off * Math.PI) / 180); | |
| 333 | input.side = -Math.sin((off * Math.PI) / 180); | |
| 334 | 438 | input.walk = brain.suspect !== null && dist(bot.pos, brain.suspect) < 900; |
| 335 | } else { | |
| 336 | brain.goal = null; | |
| 337 | 439 | } |
| 338 | 440 | if (brain.suspect !== null && brain.random() < 0.004) brain.suspect = null; |
| 339 | 441 | return withBomb(world, bot, input); |
src/client/plan.test.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { COMPETITIVE } from "./match.ts"; | |
| 3 | import { | |
| 4 | COMMIT_MARGIN, mustCommit, otherSite, roundPlan, siteFor, timeToPlant, WALK_SPEED, | |
| 5 | } from "./plan.ts"; | |
| 6 | ||
| 7 | test("a side gets one site for the round, and every slot on it reads the same one", () => { | |
| 8 | const plan = roundPlan(4242, 3); | |
| 9 | const ts = [0, 1, 2, 3, 4].map((slot) => siteFor(plan, "t", slot)); | |
| 10 | expect(new Set(ts).size).toBe(1); | |
| 11 | expect(ts[0]).toBe(plan.push); | |
| 12 | }); | |
| 13 | ||
| 14 | test("the counter-terrorists stack the plan's site but never leave the other empty", () => { | |
| 15 | const plan = roundPlan(7, 1); | |
| 16 | const cts = [0, 1, 2, 3, 4].map((slot) => siteFor(plan, "ct", slot)); | |
| 17 | expect(cts.filter((s) => s === plan.hold).length).toBe(3); | |
| 18 | expect(cts.filter((s) => s === otherSite(plan.hold)).length).toBe(2); | |
| 19 | }); | |
| 20 | ||
| 21 | test("the plan is a roll per round, not a constant and not a coin per bot", () => { | |
| 22 | const pushes = []; | |
| 23 | for (let round = 1; round <= 12; round++) pushes.push(roundPlan(99, round).push); | |
| 24 | expect(pushes).toContain("A"); | |
| 25 | expect(pushes).toContain("B"); | |
| 26 | // Two matches do not march in step, and one match repeats itself exactly. | |
| 27 | const other = []; | |
| 28 | for (let round = 1; round <= 12; round++) other.push(roundPlan(100, round).push); | |
| 29 | expect(other).not.toEqual(pushes); | |
| 30 | expect(roundPlan(99, 4)).toEqual(roundPlan(99, 4)); | |
| 31 | ||
| 32 | // Neither site and neither pairing is favoured across many matches. | |
| 33 | let a = 0, agree = 0, n = 0; | |
| 34 | for (let seed = 0; seed < 500; seed++) { | |
| 35 | for (let round = 1; round <= 8; round++) { | |
| 36 | const plan = roundPlan(seed, round); | |
| 37 | n += 1; | |
| 38 | if (plan.push === "A") a += 1; | |
| 39 | if (plan.push === plan.hold) agree += 1; | |
| 40 | } | |
| 41 | } | |
| 42 | expect(Math.abs(a / n - 0.5)).toBeLessThan(0.06); | |
| 43 | expect(Math.abs(agree / n - 0.5)).toBeLessThan(0.06); | |
| 44 | }); | |
| 45 | ||
| 46 | test("the time a plant needs is the walk, the plant itself and a little slack", () => { | |
| 47 | const { plantTime } = COMPETITIVE; | |
| 48 | expect(timeToPlant(0, plantTime)).toBeCloseTo(plantTime + COMMIT_MARGIN, 5); | |
| 49 | expect(timeToPlant(WALK_SPEED * 10, plantTime)).toBeCloseTo(10 + plantTime + COMMIT_MARGIN, 5); | |
| 50 | // A longer walk is never cheaper, and a negative distance is not a discount. | |
| 51 | expect(timeToPlant(3000, plantTime)).toBeGreaterThan(timeToPlant(1500, plantTime)); | |
| 52 | expect(timeToPlant(-500, plantTime)).toBe(timeToPlant(0, plantTime)); | |
| 53 | }); | |
| 54 | ||
| 55 | test("the carrier only commits when the clock has no room for a fight left", () => { | |
| 56 | const { plantTime, roundTime } = COMPETITIVE; | |
| 57 | const walk = 2400; // roughly T spawn to a site | |
| 58 | expect(mustCommit(roundTime, walk, plantTime)).toBe(false); | |
| 59 | expect(mustCommit(timeToPlant(walk, plantTime) + 0.1, walk, plantTime)).toBe(false); | |
| 60 | expect(mustCommit(timeToPlant(walk, plantTime), walk, plantTime)).toBe(true); | |
| 61 | // Standing on the site is the cheapest walk there is, so it is the last | |
| 62 | // thing anyone has to hurry for; the far end of the map is the first. | |
| 63 | expect(mustCommit(20, 0, plantTime)).toBe(false); | |
| 64 | expect(mustCommit(20, 4000, plantTime)).toBe(true); | |
| 65 | }); |
src/client/plan.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** | |
| 2 | * The round's plan: which site each side commits to, and when the carrier has | |
| 3 | * run out of time to argue about it. | |
| 4 | * | |
| 5 | * A side that rolls a site per bot is five people walking five ways, and the | |
| 6 | * one holding the bomb walks in alone — which is why a third of rounds used to | |
| 7 | * end on the clock. So the site is decided once for the round, from the round | |
| 8 | * number and the match's own seed, and every bot on that side reads the same | |
| 9 | * answer. It is a roll per round and not a constant: a match pushes B in one | |
| 10 | * round and A in the next, and another match rolls a different order. | |
| 11 | * | |
| 12 | * Pure arithmetic, no map and no world: what it needs is a seed, a round, a | |
| 13 | * clock and a distance, so the whole thing can be checked in a test. | |
| 14 | */ | |
| 15 | ||
| 16 | import type { Team } from "./match.ts"; | |
| 17 | ||
| 18 | export type Site = "A" | "B"; | |
| 19 | ||
| 20 | export type Plan = { | |
| 21 | /** The site the terrorists take the bomb to, together. */ | |
| 22 | push: Site; | |
| 23 | /** The site the counter-terrorists weight their defence towards. */ | |
| 24 | hold: Site; | |
| 25 | }; | |
| 26 | ||
| 27 | /** One round of splitmix32: two different inputs give two unrelated outputs. */ | |
| 28 | function mix(x: number): number { | |
| 29 | let h = x >>> 0; | |
| 30 | h = (h + 0x9e3779b9) >>> 0; | |
| 31 | h = Math.imul(h ^ (h >>> 16), 0x21f0aaad) >>> 0; | |
| 32 | h = Math.imul(h ^ (h >>> 15), 0x735a2d97) >>> 0; | |
| 33 | return (h ^ (h >>> 15)) >>> 0; | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * What both sides intend this round. The two sides are rolled from the same | |
| 38 | * seed by different salts, so they agree about half the time: the terrorists | |
| 39 | * walk into the stack as often as they walk around it, which is the difference | |
| 40 | * between a plan and a guaranteed plant. | |
| 41 | */ | |
| 42 | export function roundPlan(seed: number, round: number): Plan { | |
| 43 | const base = mix((seed >>> 0) ^ Math.imul(round | 0, 0x45d9f3b)); | |
| 44 | return { | |
| 45 | push: (mix(base ^ 0x5bf03635) & 1) === 0 ? "A" : "B", | |
| 46 | hold: (mix(base ^ 0x1b56c4e9) & 1) === 0 ? "A" : "B", | |
| 47 | }; | |
| 48 | } | |
| 49 | ||
| 50 | export const otherSite = (site: Site): Site => (site === "A" ? "B" : "A"); | |
| 51 | ||
| 52 | /** | |
| 53 | * Where one bot goes, given the plan and its place in the side's list. The | |
| 54 | * terrorists all go to the push — that is the point. The counter-terrorists | |
| 55 | * cannot: two empty sites is one free plant, so the majority stacks the hold | |
| 56 | * and the rest cover the other one. Five CTs come out three and two. | |
| 57 | */ | |
| 58 | export function siteFor(plan: Plan, team: Team, slot: number): Site { | |
Medium — the split is 3/2 only when nobody is human, and the balance has flipped. |
||
| 59 | if (team === "t") return plan.push; | |
| 60 | return slot % 2 === 0 ? plan.hold : otherSite(plan.hold); | |
| 61 | } | |
| 62 | ||
| 63 | /** How fast a bot with a rifle covers a path with nothing in its way. */ | |
| 64 | export const WALK_SPEED = 170; | |
| 65 | /** Slack for a door, a turn, and the couple of seconds any plan is out by. */ | |
| 66 | export const COMMIT_MARGIN = 3; | |
| 67 | ||
| 68 | /** Seconds the carrier needs to walk `distance` and get the bomb down. */ | |
| 69 | export const timeToPlant = (distance: number, plantTime: number): number => | |
| 70 | Math.max(0, distance) / WALK_SPEED + plantTime + COMMIT_MARGIN; | |
| 71 | ||
| 72 | /** | |
| 73 | * True when the round clock no longer has room for a fight on the way: what is | |
| 74 | * left is not much more than the walk plus the plant. The carrier that reads | |
| 75 | * true keeps its gun — the policy still points it — but stops trading ground | |
| 76 | * for a duel and goes. | |
| 77 | */ | |
| 78 | export const mustCommit = (secondsLeft: number, distance: number, plantTime: number): boolean => | |
| 79 | secondsLeft <= timeToPlant(distance, plantTime); |
src/client/world.ts +11
| @@ -13,6 +13,7 @@ import * as map from "./map.ts"; | ||
| 13 | 13 | import { angleVectors, angleDelta, clamp, dist, dist2d, normalize, rng, sub, type Vec3 } from "./math.ts"; |
| 14 | 14 | import * as rules from "./match.ts"; |
| 15 | 15 | import { eyeHeight, hullHeight, move, type Body, type Wish, MOVE } from "./physics.ts"; |
| 16 | import { roundPlan, type Plan } from "./plan.ts"; | |
| 16 | 17 | import { visible } from "./trace.ts"; |
| 17 | 18 | import { |
| 18 | 19 | ARMOR_PRICE, DEFUSE_KIT_PRICE, HELMET_PRICE, WEAPONS, inaccuracy, recoilOffset, |
| @@ -115,6 +116,9 @@ export type World = { | ||
| 115 | 116 | players: Player[]; |
| 116 | 117 | me: Player; |
| 117 | 118 | time: number; |
| 119 | seed: number; | |
| 120 | /** Which site each side commits to this round; rolled in `startRound`. */ | |
| 121 | plan: Plan; | |
| 118 | 122 | bomb: Bomb; |
| 119 | 123 | pickups: Pickup[]; |
| 120 | 124 | bodies: Corpse[]; // the dead of this round, cleared when the next one starts |
| @@ -175,6 +179,8 @@ export function createWorld( | ||
| 175 | 179 | } |
| 176 | 180 | const world: World = { |
| 177 | 181 | match, players, me, time: 0, |
| 182 | seed: seed >>> 0, | |
| 183 | plan: roundPlan(seed, match.round), | |
| 178 | 184 | bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null }, |
| 179 | 185 | pickups: [], bodies: [], |
| 180 | 186 | effects: [], feed: [], messages: [], |
| @@ -198,6 +204,11 @@ function say(world: World, text: string): void { | ||
| 198 | 204 | // --- rounds --------------------------------------------------------------- |
| 199 | 205 | |
| 200 | 206 | export function startRound(world: World): void { |
| 207 | // The round's plan first, so a bot waking up in the freeze time already | |
| 208 | // knows where its side is going. It is a function of the seed and the round | |
| 209 | // rather than a draw from `world.random`: the training arena steps this same | |
| 210 | // world, and a stream that shifted would move numbers that mean something. | |
| 211 | world.plan = roundPlan(world.seed, world.match.round); | |
| 201 | 212 | const spots = { t: [...map.spawns.t], ct: [...map.spawns.ct] }; |
| 202 | 213 | for (const p of world.players) { |
| 203 | 214 | const list = spots[p.team]; |
Medium — the rotation is written but mostly arrives after the plant. The staleness key is still
${world.bomb.state}:${brain.site}, butchooseGoalnow also depends onrotateTo(world, bot). A CT whose teammate just died at the other site keeps walking its old path untilrepathAtfires — 3 to 6 seconds — so the defence turns around after the bomb is down. Fix: fold the contact site into the key, e.g.const contact = bot.team === "ct" ? rotateTo(world, bot) : null;held on the brain andconst objective =${world.bomb.state}:${brain.site}:${contact ?? ""}`, sochooseGoal` and the key agree. This is also the cheapest lever on the win-rate swing below.