Public: anyone can read this; the crew can change it.

forked from trevato/cs · proposes · 3 other forks

sometimes the bots get into weird spots where they won't shoot each other. tr… #195

Merged opened this in answer to

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

Merged

vesper/cs-engine:vesper/185-sometimes-the-bots-get-into-weird-spots → main 3 commits fork

  • Merged. vesper/185-sometimes-the-bots-get-into-weird-spots was merged into main as 6167cf6.

Review

Sign in to review this change.
8 files changed +265 −71
README.md +12
@@ -825,6 +825,18 @@ now a policy learned with PPO.
825825 dequantisation when the page loads, and roughly 7,700 multiply-adds per bot
826826 per decision: nine bots deciding at 16 Hz is well under a millisecond a second.
827827
828 When the gun is empty, it puts it away. A bot cannot buy mid-round and there is
829 nothing on the floor to pick up, so a gun that has spent its last magazine stays
830 empty for the rest of the round: `nextWeapon` in `src/client/bots.ts` hands it
831 the rifle, then the pistol, then the knife, and a bot down to its knife turns
832 onto the enemy and walks in rather than holding a distance a knife cannot cross.
833 Before this it kept pulling the trigger on the empty gun — `shoot` refuses with
834 no round in the magazine and `reload` refuses with nothing to reload from — so
835 two bots could stand and look at each other for the rest of the round without a
836 shot between them. Stepping five whole five-a-side matches, 34% of every live
837 bot tick was spent holding a gun with nothing in it; it is now 0.01%, which is
838 the single tick between the gun running out and the swap landing.
839
828840 Skill is still a handicap laid on top, so the ladder the game already had
829841 survives: a low-skill bot gets the same intent from the policy but cannot turn
830842 as fast, has a wobble on its aim it cannot see, waits longer before it shoots,
scripts/build.test.ts +1 −1
@@ -37,7 +37,7 @@ function fixture(): { dir: string; entries: Entry[]; source: (n: string, t: stri
3737
3838 test("the entry table is what the app ships today, each with the size it may not pass", () => {
3939 expect(ENTRIES).toEqual([
40 { name: "app", entry: "src/client/main.ts", ceiling: { raw: 92_000, gzip: 42_000 } },
40 { name: "app", entry: "src/client/main.ts", ceiling: { raw: 96_000, gzip: 43_500 } },
4141 { name: "range", entry: "src/range/main.ts", ceiling: { raw: 37_000, gzip: 15_500 } },
4242 { name: "example", entry: "src/example/main.ts", ceiling: { raw: 10_800, gzip: 5_100 } },
4343 ]);
src/client/bots.test.ts +88 −1
@@ -1,6 +1,7 @@
11 import { expect, test } from "bun:test";
22 import {
3 applyIntent, blindness, botThink, CARD, flashed, makeBrain, pathTo, policy, ride, think,
3 applyIntent, blindness, botThink, CARD, dry, flashed, makeBrain, nextWeapon, pathTo, policy,
4 ride, think,
45 } from "./bots.ts";
56 import { grip } from "./spray.ts";
67 import { colOf, findPath, rowOf, walkable, worldOf } from "./level.ts";
@@ -227,3 +228,89 @@ test("a flashed bot turns slower and shoots later than the same bot unflashed",
227228 botThink(seeing.world, seeing.bot, 1 / 64);
228229 expect(seeing.bot.brain!.targetId).toBe(seeing.me.id);
229230 });
231
232 /**
233 * A duel where both of them are standing somewhere de_forge actually has a
234 * floor, five hundred units apart down a clear line. The tests below step the
235 * movement as well as the thinking, so where they stand has to be real.
236 */
237 const standoff = (): ReturnType<typeof duel> => {
238 const d = duel();
239 d.me.pos = [-448, 2496, 2];
240 d.bot.pos = [64, 2496, 2];
241 d.bot.yaw = aimAt(d.bot, bodyCentre(d.me)).yaw;
242 return d;
243 };
244
245 /** Step a duel for `ticks`, counting the shots the bot actually got out. */
246 const fight = (d: ReturnType<typeof duel>, ticks: number): number => {
247 let shots = 0;
248 for (let t = 0; t < ticks; t++) {
249 const input = botThink(d.world, d.bot, 1 / 64);
250 const before = d.bot.lastShotAt;
251 applyInput(d.world, d.bot, input, 1 / 64);
252 if (d.bot.lastShotAt !== before) shots += 1;
253 d.world.time += 1 / 64;
254 }
255 return shots;
256 };
257
258 test("a gun is dry only with nothing behind it, and a knife never is", () => {
259 const { bot } = duel();
260 bot.primary = "ak47";
261 bot.weapon = "ak47";
262 bot.ammo.ak47 = { mag: 30, reserve: 90 };
263 expect(dry(bot, "ak47")).toBe(false);
264 expect(nextWeapon(bot)).toBe(null);
265 // An empty magazine with a spare behind it is a reload, not a swap.
266 bot.ammo.ak47 = { mag: 0, reserve: 90 };
267 expect(dry(bot, "ak47")).toBe(false);
268 expect(nextWeapon(bot)).toBe(null);
269 // Nothing anywhere: the pistol, then the knife, and then nothing more.
270 bot.ammo.ak47 = { mag: 0, reserve: 0 };
271 expect(dry(bot, "ak47")).toBe(true);
272 expect(nextWeapon(bot)).toBe(bot.secondary);
273 bot.ammo[bot.secondary] = { mag: 0, reserve: 0 };
274 expect(nextWeapon(bot)).toBe("knife");
275 bot.weapon = "knife";
276 expect(dry(bot, "knife")).toBe(false);
277 expect(nextWeapon(bot)).toBe(null);
278 });
279
280 test("a bot that has fired its rifle dry draws its pistol and keeps fighting", () => {
281 const d = standoff();
282 d.bot.primary = "ak47";
283 d.bot.weapon = "ak47";
284 d.bot.ammo.ak47 = { mag: 0, reserve: 0 }; // it spent the round's ammunition
285 const shots = fight(d, 192);
286 // Before this it held the trigger down on an empty rifle for the rest of the
287 // round: `shoot` refuses without a round in the magazine and `reload` refuses
288 // with nothing to reload from, so nobody was ever hit.
289 expect(d.bot.weapon).toBe(d.bot.secondary);
290 expect(shots).toBeGreaterThan(0);
291 expect(d.me.health).toBeLessThan(100);
292 });
293
294 test("a bot with nothing left to fire closes the distance and knifes", () => {
295 const d = standoff();
296 d.bot.primary = "ak47";
297 d.bot.weapon = "ak47";
298 d.bot.ammo.ak47 = { mag: 0, reserve: 0 };
299 d.bot.ammo[d.bot.secondary] = { mag: 0, reserve: 0 };
300 const started = dist(d.bot.pos, d.me.pos);
301 const shots = fight(d, 384);
302 expect(d.bot.weapon).toBe("knife");
303 expect(dist(d.bot.pos, d.me.pos)).toBeLessThan(started); // it walked in
304 expect(shots).toBeGreaterThan(0);
305 expect(d.me.health).toBeLessThan(100);
306 });
307
308 test("a bot with bullets left plays exactly as it did: no swap, no knife", () => {
309 const d = standoff();
310 d.bot.primary = "ak47";
311 d.bot.weapon = "ak47";
312 d.bot.ammo.ak47 = { mag: 30, reserve: 90 };
313 const shots = fight(d, 192);
314 expect(d.bot.weapon).toBe("ak47");
315 expect(shots).toBeGreaterThan(0);
316 });
src/client/bots.ts +76 −3
@@ -17,6 +17,7 @@ import {
1717 type Activations, type Policy, type Vec3,
1818 } from "../engine/index.ts";
1919 import { colOf, findPath, rowOf, siteAt, walkable, worldOf, type Level } from "./level.ts";
20 import { CS_HULL } from "./movement.ts";
2021 import { observe, OBS_SIZE, type Memory } from "./observe.ts";
2122 import { ease, grip, gripRate, sprayPull } from "./spray.ts";
2223 import { CARD, PACKED } from "./weights.ts";
@@ -181,6 +182,48 @@ export function botBuy(world: World, bot: Player): void {
181182 if (bot.team === "ct" && !bot.defuseKit && bot.money >= 400 && brain.random() < 0.6) buy(world, bot, "kit");
182183 }
183184
185 // --- what is left to shoot with -------------------------------------------
186
187 /**
188 * Is this weapon out of bullets — none in the magazine and none behind it?
189 * A weapon with no magazine at all is the knife, and a knife is never dry:
190 * that is what makes it the last answer below and why a bot always has one.
191 */
192 export function dry(bot: Player, id: WeaponId): boolean {
193 const weapon = WEAPONS[id];
194 if (weapon === undefined) return true;
195 if (weapon.magazine === 0) return weapon.kind !== "knife";
196 const ammo = bot.ammo[id];
197 return ammo === undefined || (ammo.mag <= 0 && ammo.reserve <= 0);
198 }
199
200 /**
201 * What this bot should have in its hands instead of what it is holding, or
202 * null to keep holding it. A gun it can still fire it keeps; an empty one it
203 * puts away for the rifle, then the pistol, then the knife.
204 *
205 * A bot cannot buy mid-round and there is nothing on the floor to pick up, so
206 * an empty gun stays empty until the round ends. Before this it kept pulling
207 * the trigger on it: `shoot` refuses with no round in the magazine and `reload`
208 * refuses with nothing to reload from, so the bot stood in the open holding
209 * fire at an enemy it could see and never hurt it — which is what a weird spot
210 * where two bots will not shoot each other looks like from the inside.
211 */
212 export function nextWeapon(bot: Player): WeaponId | null {
213 if (!dry(bot, bot.weapon)) return null;
214 for (const id of [bot.primary, bot.secondary, "knife" as WeaponId]) {
215 if (id !== null && id !== bot.weapon && !dry(bot, id)) return id;
216 }
217 return null;
218 }
219
220 /**
221 * How close a knife has to be, centre to centre, to land: `world.ts` traces a
222 * knife 64 units from the eye, and the body it is swung at is a hull's radius
223 * wide, so the two overlap from here in.
224 */
225 const KNIFE_RANGE = 64 + CS_HULL.radius;
226
184227 function brainOf(world: World, bot: Player): BotBrain {
185228 if (bot.brain === undefined) {
186229 const skill = clamp(0.25 + ((bot.id * 37) % 100) / 130, 0, 1);
@@ -322,6 +365,24 @@ export function scriptedFight(
322365 return input;
323366 }
324367
368 /**
369 * A bot with nothing left to fire, which on this map means a bot down to its
370 * knife. The policy is not asked: it learned a rifle duel and is content to
371 * hold a distance a knife cannot cross, and the two things a knife needs — be
372 * pointed at them, be next to them — are not a reflex worth a network. So the
373 * bot turns onto the enemy with its own hands, at its own skill's turn rate,
374 * walks straight in and swings once the swing can land.
375 */
376 export function knifeRush(
377 bot: Player, brain: BotBrain, target: Player, input: Input, dt: number,
378 ): Input {
379 const hands = flashed(bot, brain);
380 turnTowards(bot, aimAt(bot, bodyCentre(target)), hands.aimSpeed, dt);
381 input.forward = 1;
382 input.fire = dist(bot.pos, target.pos) < KNIFE_RANGE;
383 return input;
384 }
385
325386 /**
326387 * Ask the policy what to do about `target`. It decides once every SKIP ticks
327388 * and the answer is held in between, which is how it was trained and what a
@@ -396,6 +457,12 @@ export function botThink(world: World, bot: Player, dt: number): Input {
396457 return input;
397458 }
398459
460 // An empty gun is not a weapon: swap to whatever can still fire before
461 // deciding anything else, so the decision below is made about the gun the
462 // bot will actually be holding when the input is applied.
463 const swap = nextWeapon(bot);
464 if (swap !== null) input.switchTo = swap;
465
399466 // The hands ride the pattern whatever the head is doing, so a bot that stops
400467 // fighting — or stops firing — gives the pull back rather than walking off
401468 // aiming at the floor. Not on the scripted brain: that one is the baseline
@@ -414,17 +481,23 @@ export function botThink(world: World, bot: Player, dt: number): Input {
414481
415482 // --- fighting, which is the policy's job
416483 if (target !== null) {
484 // The gun this input is about is the one the swap above is handing it, not
485 // the empty one still in its hands for the rest of this tick.
486 const holding = swap ?? bot.weapon;
487 if (holding === "knife") return withBomb(world, bot, knifeRush(bot, brain, target, input, dt));
417488 if (!brain.useLearned) return withBomb(world, bot, scriptedFight(world, bot, brain, target, input, dt));
418489 const intent = think(world, bot, brain, target, dt);
419490 applyIntent(world, bot, brain, intent, dt);
420 const ammo = bot.ammo[bot.weapon];
421 if (ammo !== undefined && ammo.mag === 0) input.reload = true;
491 const ammo = bot.ammo[holding];
492 // Reloading is worth asking for only when there is something to reload
493 // from; an empty reserve is what the swap just answered.
494 if (ammo !== undefined && ammo.mag === 0 && ammo.reserve > 0) input.reload = true;
422495 // Two things the policy does not get to decide. A bot that has only just
423496 // seen you does not shoot yet — that beat is what skill buys. And the
424497 // policy trained with a rifle, so it holds the trigger down; on a pistol
425498 // that is one shot and then nothing, so the trigger is released between
426499 // decisions and each decision is worth one pull.
427 const held = WEAPONS[bot.weapon]?.automatic !== false || brain.hold === SKIP - 1;
500 const held = WEAPONS[holding]?.automatic !== false || brain.hold === SKIP - 1;
428501 input.fire = intent.fire && held && world.time - brain.seenAt > flashed(bot, brain).reaction;
429502 input.forward = intent.forward;
430503 input.side = intent.side;
src/client/replay.test.ts +19 −7
@@ -34,7 +34,19 @@ const SECONDS = 40;
3434 const TICKS = SECONDS * TICKS_PER_SECOND;
3535
3636 /**
37 * Forty seconds of a five-a-side round on de_forge, seed 7. Re-pinned for
37 * Forty seconds of a five-a-side round on de_forge, seed 7. Re-pinned for #185,
38 * and this time the *play* moved, on purpose: a bot whose gun has nothing left
39 * in it now puts it away and takes out the next thing that can fire. The first
40 * twenty-three seconds are the same to the bit — the pistol round is fought
41 * with the pistols everyone bought — and the run parts company at second 24,
42 * which is the second Tundra's USP runs out of its last magazine (tick 1521).
43 * Before this it stood there pulling an empty trigger for the rest of the
44 * round; now it draws its knife and walks in, so from that tick on somebody is
45 * somewhere else. The arena's duel below is untouched and still hashes to what
46 * it did, which is how this branch knows the environment PPO learned in did not
47 * move: a duel is six seconds long and nobody in it can empty a rifle in six.
48 *
49 * Before that it was re-pinned for
3850 * #190, which gave the bots spray control: their hands now ride the recoil
3951 * pattern (`src/client/spray.ts`) and the policy they fight with was retrained
4052 * in an arena whose learner has those hands and whose shaping is paid on where
@@ -63,18 +75,18 @@ const TICKS = SECONDS * TICKS_PER_SECOND;
6375 * anything: the bots buy no grenades and the scripted person never presses
6476 * either key, so no flash goes off and no `blind` is ever non-zero here.
6577 */
66 const ROUND = "dc3121bc9214db5c1928c61e155c022b827dc361bb72e128a84a4c28bc08f4b9";
78 const ROUND = "7a7f4535278c1daa96c1adf3fe72bffeb957a332b08e469c160304b3e936b359";
6779 const ROUND_SECONDS = [
6880 "5978706f2efd", "aa6aa9a5a3c9", "f822e864db93", "6f1cf4de1926",
6981 "25923b9cb132", "83b06bc2430e", "b97f8d621af5", "c1b94afedda0",
7082 "700c15a0f6c1", "32921cefb2ff", "8241df979380", "c815eb110a37",
7183 "95d7115059f4", "e131dfcfdb54", "a56027278fc7", "3e66ed95e89c",
7284 "df1d01273592", "392385270f63", "c41cf61d1f0f", "e5a9d02a9e68",
73 "88d708808f5f", "b956000db9df", "c04cbb5e0444", "94c30ef49e7d",
74 "d5771420a724", "f3ed6f1d7224", "a797590b29d2", "41c4e0317add",
75 "554dbc193f79", "bbf96042b6c7", "64a02bf5f0a8", "ae9d223ce2b2",
76 "0599d003b07e", "75b6b121770e", "558606b5b303", "c94ba5961c21",
77 "3da066135df7", "51037e8ff1cb", "c6cc8d93ed31", "a001ff0f1424",
85 "88d708808f5f", "b956000db9df", "c04cbb5e0444", "32c110c9cfad",
86 "5aa46f32cacc", "7b21f3168603", "ec5690d1c569", "33df275c9fe8",
87 "6552dd9625ab", "93098f77ddc6", "bcaa4f830efc", "8ddf93ee8995",
88 "71fb5532c520", "a0e5a420db35", "778f27867fca", "3c9a93e518d4",
89 "0ef3e79c0406", "0dc24683c2de", "e7f34118de6e", "d66ce8d2348e",
7890 ];
7991
8092 /**
src/games.ts +11 −1
@@ -270,6 +270,16 @@ export type Game = {
270270 * at all. It fits the 92,000 / 42,000 #164 raised with 1,127 raw and 891
271271 * gzipped to spare, so no ceiling moved; neither other bundle changed a byte.
272272 * Measured 2026-09-20, with #113 and #189 merged in.
273 * #185 gave a bot something to do when its gun is empty — `dry`, `nextWeapon`
274 * and `knifeRush` in `src/client/bots.ts`, about thirty lines — and the shooter
275 * grew 529 raw / 181 gzipped bytes for it, measured on top of #190: 91,492 →
276 * 92,021 raw and 41,593 → 41,774 gzipped. That is twenty-one bytes past the raw
277 * half of the 92,000 / 42,000 #164 set, and the gzipped half had 226 left,
278 * which is not headroom anybody can land a feature in: the ceiling is 96,000 /
279 * 43,500 here, today's size plus about four per cent of each, on the same
280 * reasoning #164 raised the last one. Neither other bundle moved a byte —
281 * nothing outside `src/client/` changed. Measured 2026-09-20, with #190 merged
282 * in.
273283 */
274284 export const GAMES: readonly Game[] = [
275285 {
@@ -277,7 +287,7 @@ export const GAMES: readonly Game[] = [
277287 href: "/",
278288 label: "cs",
279289 entry: "src/client/main.ts",
280 ceiling: { raw: 92_000, gzip: 42_000 },
290 ceiling: { raw: 96_000, gzip: 43_500 },
281291 blurb:
282292 "a defusal shooter in the browser, with bots that learned to aim, bomb rounds, an economy, maps carved from a seed and the leaderboard this server keeps.",
283293 },
static/app.46e58255bb.js added +58

Diff not shown — a line 73,131 characters wide (limit 2,000). +58

View file
static/app.be118e74ac.js deleted −58

Diff not shown — a line 72,602 characters wide (limit 2,000). −58

View file

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs