Split The code is public; the app answers only to the crew.

One map, held as a value: map.ts stops being a singleton so a second layout is possible #82

Merged opened this

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

Merged

scout/cs:scout/73-one-map-held-as-a-value-map-ts-stops-bei → main 1 commit fork

  • Merged. scout/73-one-map-held-as-a-value-map-ts-stops-bei was merged into main as 74ae28a.

Review

Sign in to review this change.
13 files changed +304 −204
README.md +4 −1
@@ -218,7 +218,10 @@ with the canvas as you turn; nobody re-walks the grid to draw it.
218218
219219 ## Shape
220220
221 - `src/client/` — the game, in modules named for one thing each: `math`, `map`,
221 - `src/client/` — the game, in modules named for one thing each: `math`, `map`
222 (a `MapDef` is a value — grid, boxes, spawns, sites — that `buildMap` makes
223 from a layout's areas and crates; `DE_FORGE` is the one the game plays, and
224 everything that reads a grid takes the map rather than reaching for one),
222225 `physics`, `weapons`, `combat`, `grenades` (a thrown thing's flight: gravity,
223226 bounce, fuse and how far a blast reaches), `match` (the rules), `world` (the simulation),
224227 `bots`, `plan` (which site a side commits to, and when the carrier has run out
src/client/bots.test.ts +2 −2
@@ -2,7 +2,7 @@ import { expect, test } from "bun:test";
22 import {
33 applyIntent, botThink, CARD, DIFFICULTIES, makeBrain, policy, restyleBrain, skillBand, think,
44 } from "./bots.ts";
5 import * as map from "./map.ts";
5 import { DE_FORGE } from "./map.ts";
66 import { COMPETITIVE, SHORT } from "./match.ts";
77 import {
88 aimAt, bodyCentre, createWorld, NO_INPUT, setDifficulty, startRound, update, type Player,
@@ -200,7 +200,7 @@ test("a bomb on the floor is still picked up by a living terrorist", () => {
200200 const world = createWorld(RULES, "you", "ct", 1, 4242);
201201 world.match.phase = "live";
202202 const bot = world.players.find((p) => p.team === "t") as Player;
203 world.me.pos = [map.sites.A[0], map.sites.A[1], 2]; // out of the way, and out of sight
203 world.me.pos = [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2]; // out of the way, and out of sight
204204 bot.hasBomb = false;
205205 world.bomb = { ...world.bomb, state: "dropped", pos: [bot.pos[0] + 240, bot.pos[1], bot.pos[2] + 4] };
206206 for (let t = 0; t < 64 * 6 && world.bomb.state === "dropped"; t++) update(world, 1 / 64, NO_INPUT);
src/client/bots.ts +14 −14
@@ -13,7 +13,7 @@
1313
1414 import { decode, HEADS, STILL, type Intent } from "./action.ts";
1515 import { angleDelta, clamp, dist, dist2d, rng, type Vec3 } from "./math.ts";
16 import * as map from "./map.ts";
16 import { findPath, siteAt, walkable, colOf, rowOf } from "./map.ts";
1717 import { observe, OBS_SIZE, type Memory } from "./observe.ts";
1818 import { mustCommit, siteFor, type Site } from "./plan.ts";
1919 import {
@@ -186,7 +186,7 @@ const DETOUR = 1.3;
186186 */
187187 function committed(world: World, bot: Player, brain: BotBrain): boolean {
188188 if (!bot.hasBomb || world.match.phase !== "live") return false;
189 const crow = dist2d(bot.pos, map.sites[brain.site]) * DETOUR;
189 const crow = dist2d(bot.pos, world.map.sites[brain.site]) * DETOUR;
190190 const walk = Math.max(crow, pathLeft(brain, bot.pos));
191191 return mustCommit(world.match.timer, walk, world.match.rules.plantTime);
192192 }
@@ -195,9 +195,9 @@ function committed(world: World, bot: Player, brain: BotBrain): boolean {
195195 const CONTACT_RANGE = 1600;
196196
197197 /** Which site a fight at `pos` is a fight over, if it is near enough to be one. */
198 function contactSite(pos: Vec3): Site | null {
199 const a = dist2d(pos, map.sites.A);
200 const b = dist2d(pos, map.sites.B);
198 function contactSite(world: World, pos: Vec3): Site | null {
199 const a = dist2d(pos, world.map.sites.A);
200 const b = dist2d(pos, world.map.sites.B);
201201 if (Math.min(a, b) > CONTACT_RANGE) return null;
202202 return a <= b ? "A" : "B";
203203 }
@@ -217,7 +217,7 @@ function rotateTo(world: World, bot: Player): Site | null {
217217 if (entry.killerTeam === bot.team || entry.at < freshest) continue;
218218 for (const p of world.players) {
219219 if (p.id !== entry.victimId || p.team !== bot.team) continue;
220 const where = contactSite(p.pos);
220 const where = contactSite(world, p.pos);
221221 if (where === null) continue;
222222 freshest = entry.at;
223223 site = where;
@@ -232,22 +232,22 @@ function chooseGoal(world: World, bot: Player, brain: BotBrain): Vec3 {
232232 if (bot.team === "t") {
233233 if (bomb.state === "planted") {
234234 // Hold near the bomb rather than walk onto it.
235 return nearby(brain, bomb.pos, 300);
235 return nearby(world, brain, bomb.pos, 300);
236236 }
237 if (bot.hasBomb) return map.sites[brain.site];
237 if (bot.hasBomb) return world.map.sites[brain.site];
238238 if (bomb.state === "dropped") return bomb.pos;
239 return nearby(brain, map.sites[brain.site], 200);
239 return nearby(world, brain, world.map.sites[brain.site], 200);
240240 }
241241 if (bomb.state === "planted") return bomb.pos;
242242 const contact = rotateTo(world, bot);
243 return nearby(brain, map.sites[contact ?? brain.site], 250);
243 return nearby(world, brain, world.map.sites[contact ?? brain.site], 250);
244244 }
245245
246 const nearby = (brain: BotBrain, at: Vec3, spread: number): Vec3 => {
246 const nearby = (world: World, brain: BotBrain, at: Vec3, spread: number): Vec3 => {
247247 for (let tries = 0; tries < 8; tries++) {
248248 const x = at[0] + (brain.random() * 2 - 1) * spread;
249249 const y = at[1] + (brain.random() * 2 - 1) * spread;
250 if (map.walkable(map.colOf(x), map.rowOf(y))) return [x, y, at[2]];
250 if (walkable(world.map, colOf(world.map, x), rowOf(world.map, y))) return [x, y, at[2]];
251251 }
252252 return at;
253253 };
@@ -380,7 +380,7 @@ function repath(world: World, bot: Player, brain: BotBrain): void {
380380 world.time > brain.repathAt || objective !== brain.plannedFor;
381381 if (!stale) return;
382382 brain.goal = chooseGoal(world, bot, brain);
383 brain.path = map.findPath(bot.pos, brain.goal);
383 brain.path = findPath(world.map, bot.pos, brain.goal);
384384 brain.step = 0;
385385 brain.plannedFor = objective;
386386 brain.repathAt = world.time + 3 + brain.random() * 3;
@@ -484,7 +484,7 @@ export function botThink(world: World, bot: Player, dt: number): Input {
484484 /** Planting and defusing: press use when standing where it matters. */
485485 function withBomb(world: World, bot: Player, input: Input): Input {
486486 const bomb = world.bomb;
487 if (bot.team === "t" && bot.hasBomb && map.siteAt(bot.pos) !== null && world.match.phase === "live") {
487 if (bot.team === "t" && bot.hasBomb && siteAt(world.map, bot.pos) !== null && world.match.phase === "live") {
488488 input.use = true;
489489 input.forward = 0;
490490 input.side = 0;
src/client/grenades.test.ts +5 −6
@@ -1,7 +1,6 @@
11 import { expect, test } from "bun:test";
22 import { blastDamage, blastFalloff, GRENADE, step, thrown, type Grenade } from "./grenades.ts";
3 import * as map from "./map.ts";
4 import type { Box } from "./map.ts";
3 import { colOf, DE_FORGE, rowOf, walkable, WALL_HEIGHT, type Box } from "./map.ts";
54 import type { Vec3 } from "./math.ts";
65 import { BOMB_BLAST_RADIUS } from "./world.ts";
76 import { WEAPONS } from "./weapons.ts";
@@ -64,16 +63,16 @@ test("a wall bounces it back, slower, and it never ends up inside one", () => {
6463 });
6564
6665 test("thrown at a wall of de_forge it stays in the map and comes to rest", () => {
67 const at = map.spawns.t[0] as Vec3;
66 const at = DE_FORGE.spawns.t[0] as Vec3;
6867 const eye: Vec3 = [at[0], at[1], 64];
6968 // Eight directions from a spawn, hard: none of them may put it outside.
7069 for (let i = 0; i < 8; i++) {
7170 const g = thrown(0, "t", eye, i * 45, -20, [0, 0, 0]);
72 fly(g, GRENADE.fuse, map.boxes);
71 fly(g, GRENADE.fuse, DE_FORGE.boxes);
7372 expect(Number.isFinite(g.pos[0]) && Number.isFinite(g.pos[2])).toBe(true);
74 expect(map.walkable(map.colOf(g.pos[0] as number), map.rowOf(g.pos[1] as number))).toBe(true);
73 expect(walkable(DE_FORGE, colOf(DE_FORGE, g.pos[0] as number), rowOf(DE_FORGE, g.pos[1] as number))).toBe(true);
7574 expect(g.pos[2] as number).toBeGreaterThan(0);
76 expect(g.pos[2] as number).toBeLessThan(map.WALL_HEIGHT);
75 expect(g.pos[2] as number).toBeLessThan(WALL_HEIGHT);
7776 }
7877 });
7978
src/client/hud.ts +15 −12
@@ -9,9 +9,9 @@
99 */
1010
1111 import { angleDelta, clamp, rad } from "./math.ts";
12 import * as map from "./map.ts";
12 import { walkable, type MapDef } from "./map.ts";
1313 import { MOVE } from "./physics.ts";
14 import { buildRadar, RADAR_RADIUS } from "./radar.ts";
14 import { buildRadar } from "./radar.ts";
1515 import { deathNotice } from "./spectate.ts";
1616 import { inaccuracy, WEAPONS } from "./weapons.ts";
1717 import { currentWeapon, eyePos, offeredPickup, useProgress, type Player, type World } from "./world.ts";
@@ -102,13 +102,15 @@ const clock = (seconds: number): string => {
102102 // The radar's map is a few hundred walkable cells, the same every frame: baked
103103 // once into an offscreen canvas at the on-screen size and rotated by the
104104 // canvas transform as the view turns, instead of walked and re-rotated by
105 // hand sixty times a second. Rebuilt only when that size changes, same as
106 // the vignette above.
105 // hand sixty times a second. Rebuilt only when the size or the map changes,
106 // same as the vignette above — keyed by the map itself, so a switch to a
107 // second layout can never draw the first one's walls.
107108 let radarMap: OffscreenCanvas | HTMLCanvasElement | null = null;
108109 let radarMapSize = -1;
110 let radarMapFor: MapDef | null = null;
109111
110 function radarMapImage(size: number): CanvasImageSource | null {
111 if (radarMap === null || radarMapSize !== size) {
112 function radarMapImage(map: MapDef, size: number): CanvasImageSource | null {
113 if (radarMap === null || radarMapSize !== size || radarMapFor !== map) {
112114 if (typeof OffscreenCanvas !== "undefined") radarMap = new OffscreenCanvas(size, size);
113115 else if (typeof document !== "undefined") {
114116 const canvas = document.createElement("canvas");
@@ -117,12 +119,13 @@ function radarMapImage(size: number): CanvasImageSource | null {
117119 radarMap = canvas;
118120 } else return null; // no canvas to build one on, e.g. a headless test
119121 const rctx = radarMap.getContext("2d") as CanvasRenderingContext2D;
120 const cw = size / map.GRID_W, ch = size / map.GRID_H;
122 const cw = size / map.gridW, ch = size / map.gridH;
121123 rctx.fillStyle = "rgba(190, 198, 212, 0.55)";
122 for (let r = 0; r < map.GRID_H; r++)
123 for (let c = 0; c < map.GRID_W; c++)
124 if (map.walkable(c, r)) rctx.fillRect(c * cw, r * ch, cw + 0.5, ch + 0.5);
124 for (let r = 0; r < map.gridH; r++)
125 for (let c = 0; c < map.gridW; c++)
126 if (walkable(map, c, r)) rctx.fillRect(c * cw, r * ch, cw + 0.5, ch + 0.5);
125127 radarMapSize = size;
128 radarMapFor = map;
126129 }
127130 return radarMap;
128131 }
@@ -152,7 +155,7 @@ function drawRadar(
152155 ctx.beginPath();
153156 ctx.arc(cx, cy, radius, 0, Math.PI * 2);
154157 ctx.clip();
155 const image = radarMapImage(Math.max(1, Math.round(size)));
158 const image = radarMapImage(world.map, Math.max(1, Math.round(size)));
156159 if (image !== null) {
157160 // The image is baked with north up; turning it to match the view's yaw
158161 // is one canvas rotation, not a re-walk of the grid.
@@ -165,7 +168,7 @@ function drawRadar(
165168
166169 const teamColor = view.team === "t" ? T_COLOR : CT_COLOR;
167170 ctx.textAlign = "center";
168 for (const blip of buildRadar(world, view, RADAR_RADIUS)) {
171 for (const blip of buildRadar(world, view)) {
169172 const px = cx + blip.x * radius, py = cy + blip.y * radius;
170173 if (blip.kind === "site") {
171174 ctx.fillStyle = "rgba(255, 255, 255, 0.9)";
src/client/map.test.ts +70 −25
@@ -1,64 +1,65 @@
11 import { expect, test } from "bun:test";
22 import {
3 boxes, cellAt, colOf, findPath, GRID_H, GRID_W, rowOf, siteAt, sites, spawns, walkable, worldOf,
3 buildMap, cellAt, colOf, DE_FORGE, findPath, rowOf, siteAt, walkable, worldOf, type Layout,
44 } from "./map.ts";
55
66 test("the map has two sites and two spawns", () => {
7 expect(spawns.t.length).toBeGreaterThan(4);
8 expect(spawns.ct.length).toBeGreaterThan(4);
9 expect(siteAt(sites.A)).toBe("A");
10 expect(siteAt(sites.B)).toBe("B");
7 expect(DE_FORGE.spawns.t.length).toBeGreaterThan(4);
8 expect(DE_FORGE.spawns.ct.length).toBeGreaterThan(4);
9 expect(siteAt(DE_FORGE, DE_FORGE.sites.A)).toBe("A");
10 expect(siteAt(DE_FORGE, DE_FORGE.sites.B)).toBe("B");
1111 });
1212
1313 test("the border is solid all the way round", () => {
14 for (let c = 0; c < GRID_W; c++) {
15 expect(cellAt(c, 0)).toBe("#");
16 expect(cellAt(c, GRID_H - 1)).toBe("#");
14 for (let c = 0; c < DE_FORGE.gridW; c++) {
15 expect(cellAt(DE_FORGE, c, 0)).toBe("#");
16 expect(cellAt(DE_FORGE, c, DE_FORGE.gridH - 1)).toBe("#");
1717 }
18 for (let r = 0; r < GRID_H; r++) {
19 expect(cellAt(0, r)).toBe("#");
20 expect(cellAt(GRID_W - 1, r)).toBe("#");
18 for (let r = 0; r < DE_FORGE.gridH; r++) {
19 expect(cellAt(DE_FORGE, 0, r)).toBe("#");
20 expect(cellAt(DE_FORGE, DE_FORGE.gridW - 1, r)).toBe("#");
2121 }
2222 });
2323
2424 test("grid and world coordinates round-trip", () => {
2525 for (const [c, r] of [[3, 7], [21, 21], [40, 38]] as [number, number][]) {
26 const p = worldOf(c, r);
27 expect(colOf(p[0])).toBe(c);
28 expect(rowOf(p[1])).toBe(r);
26 const p = worldOf(DE_FORGE, c, r);
27 expect(colOf(DE_FORGE, p[0])).toBe(c);
28 expect(rowOf(DE_FORGE, p[1])).toBe(r);
2929 }
3030 });
3131
3232 test("every walkable cell is reachable from T spawn", () => {
3333 // A room nobody can reach is a bug you only find by walking into it.
34 const start = spawns.t[0];
34 const start = DE_FORGE.spawns.t[0];
3535 const seen = new Set<number>();
36 const queue = [rowOf(start[1]) * GRID_W + colOf(start[0])];
36 const queue = [rowOf(DE_FORGE, start[1]) * DE_FORGE.gridW + colOf(DE_FORGE, start[0])];
3737 seen.add(queue[0]);
3838 while (queue.length > 0) {
3939 const i = queue.pop() as number;
40 const c = i % GRID_W, r = Math.floor(i / GRID_W);
40 const c = i % DE_FORGE.gridW, r = Math.floor(i / DE_FORGE.gridW);
4141 for (const [dc, dr] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
42 if (!walkable(c + dc, r + dr)) continue;
43 const j = (r + dr) * GRID_W + c + dc;
42 if (!walkable(DE_FORGE, c + dc, r + dr)) continue;
43 const j = (r + dr) * DE_FORGE.gridW + c + dc;
4444 if (seen.has(j)) continue;
4545 seen.add(j);
4646 queue.push(j);
4747 }
4848 }
4949 let walkables = 0;
50 for (let r = 0; r < GRID_H; r++) for (let c = 0; c < GRID_W; c++) if (walkable(c, r)) walkables++;
50 for (let r = 0; r < DE_FORGE.gridH; r++)
51 for (let c = 0; c < DE_FORGE.gridW; c++) if (walkable(DE_FORGE, c, r)) walkables++;
5152 expect(seen.size).toBe(walkables);
5253 });
5354
5455 test("greedy meshing collapses the walls into few boxes", () => {
55 expect(boxes.length).toBeGreaterThan(10);
56 expect(boxes.length).toBeLessThan(300);
56 expect(DE_FORGE.boxes.length).toBeGreaterThan(10);
57 expect(DE_FORGE.boxes.length).toBeLessThan(300);
5758 });
5859
5960 test("a path from T spawn reaches both sites", () => {
60 for (const site of [sites.A, sites.B]) {
61 const path = findPath(spawns.t[0], site);
61 for (const site of [DE_FORGE.sites.A, DE_FORGE.sites.B]) {
62 const path = findPath(DE_FORGE, DE_FORGE.spawns.t[0], site);
6263 expect(path.length).toBeGreaterThan(4);
6364 // Consecutive waypoints are neighbouring cells, never a jump across a wall.
6465 for (let i = 1; i < path.length - 1; i++) {
@@ -69,5 +70,49 @@ test("a path from T spawn reaches both sites", () => {
6970 });
7071
7172 test("a path into solid rock comes back empty rather than lying", () => {
72 expect(findPath(spawns.t[0], worldOf(0, 0))).toEqual([]);
73 expect(findPath(DE_FORGE, DE_FORGE.spawns.t[0], worldOf(DE_FORGE, 0, 0))).toEqual([]);
74 });
75
76 /** A hand-written layout small enough to check by eye: one room, one crate, two sites. */
77 const TOY: Layout = {
78 name: "toy",
79 gridW: 6,
80 gridH: 6,
81 areas: [
82 { name: "room", rect: [1, 1, 4, 4], fill: "." },
83 { name: "t spawn", rect: [1, 4, 1, 4], fill: "t" },
84 { name: "ct spawn", rect: [4, 1, 4, 1], fill: "c" },
85 { name: "a site", rect: [3, 3, 3, 3], fill: "A" },
86 { name: "b site", rect: [1, 1, 1, 1], fill: "B" },
87 ],
88 crates: [[2, 2, "x"]],
89 };
90
91 test("buildMap turns a layout into the cells, spawns and sites it describes", () => {
92 const toy = buildMap(TOY);
93 expect(toy.name).toBe("toy");
94 expect(toy.gridW).toBe(6);
95 expect(toy.gridH).toBe(6);
96 // The border nobody carved is still solid rock.
97 expect(cellAt(toy, 0, 0)).toBe("#");
98 expect(cellAt(toy, 5, 5)).toBe("#");
99 // The crate landed where it was placed, inside the room.
100 expect(cellAt(toy, 2, 2)).toBe("x");
101 expect(toy.spawns.t).toEqual([worldOf(toy, 1, 4)]);
102 expect(toy.spawns.ct).toEqual([worldOf(toy, 4, 1)]);
103 expect(siteAt(toy, toy.sites.A)).toBe("A");
104 expect(siteAt(toy, toy.sites.B)).toBe("B");
105 });
106
107 test("two maps built from two layouts hold no state in common", () => {
108 const a = buildMap({ ...TOY, name: "a" });
109 const b = buildMap({ ...TOY, gridW: 8, gridH: 8, name: "b", crates: [] });
110 expect(a).not.toBe(b);
111 expect(a.grid).not.toBe(b.grid);
112 expect(a.gridW).toBe(6);
113 expect(b.gridW).toBe(8);
114 expect(cellAt(a, 2, 2)).toBe("x"); // a has the crate
115 expect(cellAt(b, 2, 2)).toBe("."); // b was built with none
116 expect(a).not.toBe(DE_FORGE);
117 expect(a.grid).not.toBe(DE_FORGE.grid);
73118 });
src/client/map.ts +149 −110
@@ -4,6 +4,10 @@
44 * everything — what you collide with, what gets drawn, where bots walk, where
55 * the bomb can be planted — so the map can never disagree with itself.
66 *
7 * A map is a value, `MapDef`, built once by `buildMap` from a layout's areas
8 * and crates. `DE_FORGE` is the one the game plays; nothing below reaches for
9 * a module-level grid, so a second layout is just a second `MapDef`.
10 *
711 * Layout is dust-shaped and original: two sites, a mid, a long right-hand
812 * corridor and a left-hand tunnel, T at the bottom of the grid, CT at the top.
913 */
@@ -11,107 +15,129 @@
1115 import type { Vec3 } from "./math.ts";
1216
1317 export const CELL = 128;
14 export const GRID_W = 44;
15 export const GRID_H = 44;
1618 export const WALL_HEIGHT = 384;
1719
1820 export type Cell = "#" | "." | "x" | "X" | "A" | "B" | "t" | "c";
1921
2022 /** [col0, row0, col1, row1] inclusive, and what the cells become. */
21 type Area = { name: string; rect: [number, number, number, number]; fill: Cell };
23 export type Area = { name: string; rect: [number, number, number, number]; fill: Cell };
2224
23 const AREAS: Area[] = [
24 { name: "ct spawn", rect: [18, 2, 27, 6], fill: "c" },
25 { name: "ct mid", rect: [20, 6, 25, 11], fill: "." },
26 { name: "a link", rect: [27, 3, 32, 7], fill: "." },
27 { name: "a site", rect: [32, 3, 41, 11], fill: "A" },
28 { name: "long a", rect: [35, 11, 39, 31], fill: "." },
29 { name: "doors", rect: [30, 30, 39, 34], fill: "." },
30 { name: "t long", rect: [28, 34, 35, 38], fill: "." },
31 { name: "t spawn", rect: [16, 37, 28, 41], fill: "t" },
32 { name: "t mid", rect: [19, 27, 25, 37], fill: "." },
33 { name: "mid", rect: [19, 11, 25, 27], fill: "." },
34 { name: "catwalk", rect: [25, 14, 32, 17], fill: "." },
35 { name: "a ramp", rect: [30, 8, 33, 14], fill: "." },
36 { name: "b site", rect: [2, 3, 11, 11], fill: "B" },
37 { name: "b link", rect: [11, 5, 18, 9], fill: "." },
38 { name: "tunnels", rect: [4, 11, 8, 30], fill: "." },
39 { name: "lower tunnel", rect: [4, 30, 14, 35], fill: "." },
40 { name: "t tunnel", rect: [14, 33, 18, 38], fill: "." },
41 { name: "short", rect: [8, 20, 19, 23], fill: "." },
42 ];
25 export type Layout = {
26 name: string;
27 gridW: number;
28 gridH: number;
29 areas: Area[];
30 /** Cover, placed by hand: lower-case is jumpable, upper-case is not. */
31 crates: [number, number, Cell][];
32 };
4333
44 /** Cover, placed by hand: lower-case is jumpable, upper-case is not. */
45 const CRATES: [number, number, Cell][] = [
46 [34, 5, "X"], [35, 5, "x"], [38, 8, "x"], [39, 4, "X"], [33, 9, "x"],
47 [4, 5, "X"], [5, 5, "x"], [8, 8, "x"], [9, 4, "x"], [3, 9, "X"],
48 [21, 18, "x"], [23, 18, "x"], [22, 24, "X"],
49 [37, 20, "x"], [37, 26, "X"], [6, 18, "x"], [6, 25, "X"],
50 [20, 3, "x"], [25, 3, "x"], [18, 39, "x"], [26, 39, "x"], [28, 15, "x"],
51 ];
34 export const MAT = { FLOOR: 0, WALL: 1, CRATE: 2, SITE: 3, METAL: 4 } as const;
35 export type Box = { min: Vec3; max: Vec3; mat: number };
5236
53 function build(): Cell[] {
54 const g: Cell[] = new Array(GRID_W * GRID_H).fill("#");
55 for (const area of AREAS) {
37 /** A built map: the grid, its geometry, and the places bots and spawns care about. */
38 export type MapDef = {
39 name: string;
40 gridW: number;
41 gridH: number;
42 grid: Cell[];
43 /** Everything solid, in one list: the renderer draws it and bullets stop in it. */
44 boxes: Box[];
45 spawns: { t: Vec3[]; ct: Vec3[] };
46 sites: { A: Vec3; B: Vec3 };
47 };
48
49 const DE_FORGE_LAYOUT: Layout = {
50 name: "de_forge",
51 gridW: 44,
52 gridH: 44,
53 areas: [
54 { name: "ct spawn", rect: [18, 2, 27, 6], fill: "c" },
55 { name: "ct mid", rect: [20, 6, 25, 11], fill: "." },
56 { name: "a link", rect: [27, 3, 32, 7], fill: "." },
57 { name: "a site", rect: [32, 3, 41, 11], fill: "A" },
58 { name: "long a", rect: [35, 11, 39, 31], fill: "." },
59 { name: "doors", rect: [30, 30, 39, 34], fill: "." },
60 { name: "t long", rect: [28, 34, 35, 38], fill: "." },
61 { name: "t spawn", rect: [16, 37, 28, 41], fill: "t" },
62 { name: "t mid", rect: [19, 27, 25, 37], fill: "." },
63 { name: "mid", rect: [19, 11, 25, 27], fill: "." },
64 { name: "catwalk", rect: [25, 14, 32, 17], fill: "." },
65 { name: "a ramp", rect: [30, 8, 33, 14], fill: "." },
66 { name: "b site", rect: [2, 3, 11, 11], fill: "B" },
67 { name: "b link", rect: [11, 5, 18, 9], fill: "." },
68 { name: "tunnels", rect: [4, 11, 8, 30], fill: "." },
69 { name: "lower tunnel", rect: [4, 30, 14, 35], fill: "." },
70 { name: "t tunnel", rect: [14, 33, 18, 38], fill: "." },
71 { name: "short", rect: [8, 20, 19, 23], fill: "." },
72 ],
73 crates: [
74 [34, 5, "X"], [35, 5, "x"], [38, 8, "x"], [39, 4, "X"], [33, 9, "x"],
75 [4, 5, "X"], [5, 5, "x"], [8, 8, "x"], [9, 4, "x"], [3, 9, "X"],
76 [21, 18, "x"], [23, 18, "x"], [22, 24, "X"],
77 [37, 20, "x"], [37, 26, "X"], [6, 18, "x"], [6, 25, "X"],
78 [20, 3, "x"], [25, 3, "x"], [18, 39, "x"], [26, 39, "x"], [28, 15, "x"],
79 ],
80 };
81
82 function buildGrid(layout: Layout): Cell[] {
83 const g: Cell[] = new Array(layout.gridW * layout.gridH).fill("#");
84 for (const area of layout.areas) {
5685 const [c0, r0, c1, r1] = area.rect;
5786 for (let r = r0; r <= r1; r++)
58 for (let c = c0; c <= c1; c++) g[r * GRID_W + c] = area.fill;
87 for (let c = c0; c <= c1; c++) g[r * layout.gridW + c] = area.fill;
5988 }
60 for (const [c, r, cell] of CRATES) g[r * GRID_W + c] = cell;
89 for (const [c, r, cell] of layout.crates) g[r * layout.gridW + c] = cell;
6190 return g;
6291 }
6392
64 export const grid: Cell[] = build();
65
66 export const cellAt = (col: number, row: number): Cell =>
67 col < 0 || row < 0 || col >= GRID_W || row >= GRID_H ? "#" : (grid[row * GRID_W + col] as Cell);
93 export const cellAt = (map: MapDef, col: number, row: number): Cell =>
94 col < 0 || row < 0 || col >= map.gridW || row >= map.gridH
95 ? "#"
96 : (map.grid[row * map.gridW + col] as Cell);
6897
6998 /** Solid to a walking bot: walls and both kinds of crate. */
70 export const walkable = (col: number, row: number): boolean => {
71 const c = cellAt(col, row);
99 export const walkable = (map: MapDef, col: number, row: number): boolean => {
100 const c = cellAt(map, col, row);
72101 return c !== "#" && c !== "x" && c !== "X";
73102 };
74103
75104 /** Grid coordinates are north-down; the world is Y-north, centred on zero. */
76 export const worldOf = (col: number, row: number, z = 0): Vec3 => [
77 (col - GRID_W / 2 + 0.5) * CELL,
78 (GRID_H / 2 - row - 0.5) * CELL,
105 export const worldOf = (map: MapDef, col: number, row: number, z = 0): Vec3 => [
106 (col - map.gridW / 2 + 0.5) * CELL,
107 (map.gridH / 2 - row - 0.5) * CELL,
79108 z,
80109 ];
81110
82 export const colOf = (x: number): number => Math.floor(x / CELL + GRID_W / 2);
83 export const rowOf = (y: number): number => Math.floor(GRID_H / 2 - y / CELL);
111 export const colOf = (map: MapDef, x: number): number => Math.floor(x / CELL + map.gridW / 2);
112 export const rowOf = (map: MapDef, y: number): number => Math.floor(map.gridH / 2 - y / CELL);
84113
85114 // --- geometry -------------------------------------------------------------
86115
87 export const MAT = { FLOOR: 0, WALL: 1, CRATE: 2, SITE: 3, METAL: 4 } as const;
88 export type Box = { min: Vec3; max: Vec3; mat: number };
89
90116 /**
91117 * Greedy meshing: merge runs of identical solid cells across a row, then merge
92118 * rows below that match the run exactly. A few hundred boxes instead of a few
93119 * thousand, which is the difference between one buffer and a stutter.
94120 */
95 function mergeSolid(match: (c: Cell) => boolean, z0: number, z1: number, mat: number): Box[] {
96 const used = new Uint8Array(GRID_W * GRID_H);
121 function mergeSolid(map: MapDef, match: (c: Cell) => boolean, z0: number, z1: number, mat: number): Box[] {
122 const used = new Uint8Array(map.gridW * map.gridH);
97123 const out: Box[] = [];
98 for (let r = 0; r < GRID_H; r++) {
99 for (let c = 0; c < GRID_W; c++) {
100 const i = r * GRID_W + c;
101 if (used[i] === 1 || !match(cellAt(c, r))) continue;
124 for (let r = 0; r < map.gridH; r++) {
125 for (let c = 0; c < map.gridW; c++) {
126 const i = r * map.gridW + c;
127 if (used[i] === 1 || !match(cellAt(map, c, r))) continue;
102128 let w = 1;
103 while (c + w < GRID_W && used[i + w] === 0 && match(cellAt(c + w, r))) w++;
129 while (c + w < map.gridW && used[i + w] === 0 && match(cellAt(map, c + w, r))) w++;
104130 let h = 1;
105 grow: while (r + h < GRID_H) {
131 grow: while (r + h < map.gridH) {
106132 for (let k = 0; k < w; k++) {
107 const j = (r + h) * GRID_W + c + k;
108 if (used[j] === 1 || !match(cellAt(c + k, r + h))) break grow;
133 const j = (r + h) * map.gridW + c + k;
134 if (used[j] === 1 || !match(cellAt(map, c + k, r + h))) break grow;
109135 }
110136 h++;
111137 }
112 for (let dr = 0; dr < h; dr++) for (let dc = 0; dc < w; dc++) used[(r + dr) * GRID_W + c + dc] = 1;
113 const a = worldOf(c, r);
114 const b = worldOf(c + w - 1, r + h - 1);
138 for (let dr = 0; dr < h; dr++) for (let dc = 0; dc < w; dc++) used[(r + dr) * map.gridW + c + dc] = 1;
139 const a = worldOf(map, c, r);
140 const b = worldOf(map, c + w - 1, r + h - 1);
115141 out.push({
116142 min: [a[0] - CELL / 2, b[1] - CELL / 2, z0],
117143 max: [b[0] + CELL / 2, a[1] + CELL / 2, z1],
@@ -122,15 +148,15 @@ function mergeSolid(match: (c: Cell) => boolean, z0: number, z1: number, mat: nu
122148 return out;
123149 }
124150
125 function siteSlab(fill: Cell): Box {
126 let c0 = GRID_W, r0 = GRID_H, c1 = 0, r1 = 0;
127 for (let r = 0; r < GRID_H; r++)
128 for (let c = 0; c < GRID_W; c++)
129 if (cellAt(c, r) === fill) {
151 function siteSlab(map: MapDef, fill: Cell): Box {
152 let c0 = map.gridW, r0 = map.gridH, c1 = 0, r1 = 0;
153 for (let r = 0; r < map.gridH; r++)
154 for (let c = 0; c < map.gridW; c++)
155 if (cellAt(map, c, r) === fill) {
130156 c0 = Math.min(c0, c); c1 = Math.max(c1, c);
131157 r0 = Math.min(r0, r); r1 = Math.max(r1, r);
132158 }
133 const a = worldOf(c0, r0), b = worldOf(c1, r1);
159 const a = worldOf(map, c0, r0), b = worldOf(map, c1, r1);
134160 return {
135161 min: [a[0] - CELL / 2, b[1] - CELL / 2, 0],
136162 max: [b[0] + CELL / 2, a[1] + CELL / 2, 2],
@@ -138,66 +164,79 @@ function siteSlab(fill: Cell): Box {
138164 };
139165 }
140166
141 const half = (GRID_W / 2) * CELL;
142 const floor: Box = { min: [-half, -half, -32], max: [half, half, 0], mat: MAT.FLOOR };
143
144 /** Everything solid, in one list: the renderer draws it and bullets stop in it. */
145 export const boxes: Box[] = [
146 floor,
147 siteSlab("A"),
148 siteSlab("B"),
149 ...mergeSolid((c) => c === "#", 0, WALL_HEIGHT, MAT.WALL),
150 ...mergeSolid((c) => c === "x", 0, 64, MAT.CRATE),
151 ...mergeSolid((c) => c === "X", 0, 128, MAT.CRATE),
152 ];
153
154 // --- places ---------------------------------------------------------------
155
156 function cellsOf(fill: Cell): Vec3[] {
167 function cellsOf(map: MapDef, fill: Cell): Vec3[] {
157168 const out: Vec3[] = [];
158 for (let r = 0; r < GRID_H; r++)
159 for (let c = 0; c < GRID_W; c++) if (cellAt(c, r) === fill) out.push(worldOf(c, r));
169 for (let r = 0; r < map.gridH; r++)
170 for (let c = 0; c < map.gridW; c++) if (cellAt(map, c, r) === fill) out.push(worldOf(map, c, r));
160171 return out;
161172 }
162173
163 export const spawns: { t: Vec3[]; ct: Vec3[] } = { t: cellsOf("t"), ct: cellsOf("c") };
164
165 const siteCells = { A: cellsOf("A"), B: cellsOf("B") };
166 export const sites: { A: Vec3; B: Vec3 } = {
167 A: centre(siteCells.A),
168 B: centre(siteCells.B),
169 };
170
171174 function centre(list: Vec3[]): Vec3 {
172175 const s = list.reduce((a, p) => [a[0] + p[0], a[1] + p[1], 0] as Vec3, [0, 0, 0] as Vec3);
173176 return [s[0] / list.length, s[1] / list.length, 0];
174177 }
175178
179 const half = (w: number): number => (w / 2) * CELL;
180
181 /** Build one `MapDef` from a layout's areas and crates. */
182 export function buildMap(layout: Layout): MapDef {
183 const map: MapDef = {
184 name: layout.name,
185 gridW: layout.gridW,
186 gridH: layout.gridH,
187 grid: buildGrid(layout),
188 boxes: [],
189 spawns: { t: [], ct: [] },
190 sites: { A: [0, 0, 0], B: [0, 0, 0] },
191 };
192 const floor: Box = {
193 min: [-half(map.gridW), -half(map.gridH), -32],
194 max: [half(map.gridW), half(map.gridH), 0],
195 mat: MAT.FLOOR,
196 };
197 map.boxes = [
198 floor,
199 siteSlab(map, "A"),
200 siteSlab(map, "B"),
201 ...mergeSolid(map, (c) => c === "#", 0, WALL_HEIGHT, MAT.WALL),
202 ...mergeSolid(map, (c) => c === "x", 0, 64, MAT.CRATE),
203 ...mergeSolid(map, (c) => c === "X", 0, 128, MAT.CRATE),
204 ];
205 map.spawns = { t: cellsOf(map, "t"), ct: cellsOf(map, "c") };
206 map.sites = { A: centre(cellsOf(map, "A")), B: centre(cellsOf(map, "B")) };
207 return map;
208 }
209
210 /** The map de_forge plays on, built once from today's tables. */
211 export const DE_FORGE: MapDef = buildMap(DE_FORGE_LAYOUT);
212
213 // --- places ---------------------------------------------------------------
214
176215 /** Which bombsite a point stands in, if any. */
177 export function siteAt(pos: Vec3): "A" | "B" | null {
178 const c = cellAt(colOf(pos[0]), rowOf(pos[1]));
216 export function siteAt(map: MapDef, pos: Vec3): "A" | "B" | null {
217 const c = cellAt(map, colOf(map, pos[0]), rowOf(map, pos[1]));
179218 return c === "A" || c === "B" ? c : null;
180219 }
181220
182221 // --- pathfinding ----------------------------------------------------------
183222
184223 /**
185 * A* over the walkable cells, eight ways, no corner-cutting. The grid is 1936
186 * cells; a full search is well under a millisecond, and bots only ask for one
187 * when their goal changes.
224 * A* over the walkable cells, eight ways, no corner-cutting. de_forge's grid is
225 * 1936 cells; a full search is well under a millisecond, and bots only ask for
226 * one when their goal changes.
188227 */
189 export function findPath(from: Vec3, to: Vec3): Vec3[] {
190 const start = rowOf(from[1]) * GRID_W + colOf(from[0]);
191 const goal = rowOf(to[1]) * GRID_W + colOf(to[0]);
228 export function findPath(map: MapDef, from: Vec3, to: Vec3): Vec3[] {
229 const start = rowOf(map, from[1]) * map.gridW + colOf(map, from[0]);
230 const goal = rowOf(map, to[1]) * map.gridW + colOf(map, to[0]);
192231 if (start === goal) return [to];
193 const n = GRID_W * GRID_H;
232 const n = map.gridW * map.gridH;
194233 if (start < 0 || start >= n || goal < 0 || goal >= n) return [];
195234 const g = new Float32Array(n).fill(Infinity);
196235 const from_ = new Int32Array(n).fill(-1);
197236 const open: number[] = [start];
198237 const f = new Float32Array(n).fill(Infinity);
199238 const h = (i: number): number =>
200 Math.hypot((i % GRID_W) - (goal % GRID_W), Math.floor(i / GRID_W) - Math.floor(goal / GRID_W));
239 Math.hypot((i % map.gridW) - (goal % map.gridW), Math.floor(i / map.gridW) - Math.floor(goal / map.gridW));
201240 g[start] = 0;
202241 f[start] = h(start);
203242 const seen = new Uint8Array(n);
@@ -208,14 +247,14 @@ export function findPath(from: Vec3, to: Vec3): Vec3[] {
208247 const cur = open.splice(bestAt, 1)[0] as number;
209248 if (cur === goal) break;
210249 seen[cur] = 1;
211 const cc = cur % GRID_W, cr = Math.floor(cur / GRID_W);
250 const cc = cur % map.gridW, cr = Math.floor(cur / map.gridW);
212251 for (let dr = -1; dr <= 1; dr++) {
213252 for (let dc = -1; dc <= 1; dc++) {
214253 if (dc === 0 && dr === 0) continue;
215254 const nc = cc + dc, nr = cr + dr;
216 if (!walkable(nc, nr)) continue;
217 if (dc !== 0 && dr !== 0 && (!walkable(cc + dc, cr) || !walkable(cc, cr + dr))) continue;
218 const ni = nr * GRID_W + nc;
255 if (!walkable(map, nc, nr)) continue;
256 if (dc !== 0 && dr !== 0 && (!walkable(map, cc + dc, cr) || !walkable(map, cc, cr + dr))) continue;
257 const ni = nr * map.gridW + nc;
219258 if (seen[ni] === 1) continue;
220259 const step = dc !== 0 && dr !== 0 ? 1.414 : 1;
221260 const cost = (g[cur] as number) + step;
@@ -231,7 +270,7 @@ export function findPath(from: Vec3, to: Vec3): Vec3[] {
231270 if (from_[goal] === -1) return [];
232271 const out: Vec3[] = [];
233272 for (let i = goal; i !== start && i !== -1; i = from_[i] as number)
234 out.unshift(worldOf(i % GRID_W, Math.floor(i / GRID_W)));
273 out.unshift(worldOf(map, i % map.gridW, Math.floor(i / map.gridW)));
235274 out.push(to);
236275 return out;
237276 }
src/client/observe.ts +8 −6
@@ -12,7 +12,7 @@
1212 * so OBS_SIZE and the order below are part of the policy's contract.
1313 */
1414
15 import * as map from "./map.ts";
15 import { colOf, rowOf, walkable, type MapDef } from "./map.ts";
1616 import { angleDelta, clamp, dist, type Vec3 } from "./math.ts";
1717 import { HULL } from "./physics.ts";
1818 import { inaccuracy, WEAPONS } from "./weapons.ts";
@@ -29,11 +29,11 @@ export type Memory = {
2929 const rad = Math.PI / 180;
3030
3131 /** Is the cell `d` units along (yaw + offset) somewhere a bot could stand? */
32 const clearAt = (from: Vec3, yaw: number, offset: number, d: number): number => {
32 const clearAt = (map: MapDef, from: Vec3, yaw: number, offset: number, d: number): number => {
3333 const a = (yaw + offset) * rad;
3434 const x = from[0] + Math.cos(a) * d;
3535 const y = from[1] + Math.sin(a) * d;
36 return map.walkable(map.colOf(x), map.rowOf(y)) ? 1 : 0;
36 return walkable(map, colOf(map, x), rowOf(map, y)) ? 1 : 0;
3737 };
3838
3939 /**
@@ -81,9 +81,11 @@ export function observe(
8181 });
8282 const magazine = Math.max(1, weapon.magazine);
8383 const eye = eyePos(bot);
84 const ahead = (clearAt(eye, bot.yaw, 0, 64) + clearAt(eye, bot.yaw, 0, 128) + clearAt(eye, bot.yaw, 0, 224)) / 3;
85 const right = (clearAt(eye, bot.yaw, -90, 96) + clearAt(eye, bot.yaw, -50, 128)) / 2;
86 const left = (clearAt(eye, bot.yaw, 90, 96) + clearAt(eye, bot.yaw, 50, 128)) / 2;
84 const { map } = world;
85 const ahead =
86 (clearAt(map, eye, bot.yaw, 0, 64) + clearAt(map, eye, bot.yaw, 0, 128) + clearAt(map, eye, bot.yaw, 0, 224)) / 3;
87 const right = (clearAt(map, eye, bot.yaw, -90, 96) + clearAt(map, eye, bot.yaw, -50, 128)) / 2;
88 const left = (clearAt(map, eye, bot.yaw, 90, 96) + clearAt(map, eye, bot.yaw, 50, 128)) / 2;
8789
8890 out[at + 0] = 1; // a bias, so a dead layer still has an offset to lean on
8991 out[at + 1] = target === null ? 0 : 1;
src/client/radar.ts +5 −5
@@ -10,7 +10,7 @@
1010 */
1111
1212 import { rad } from "./math.ts";
13 import * as map from "./map.ts";
13 import { CELL } from "./map.ts";
1414 import type { Player, World } from "./world.ts";
1515
1616 export type Blip =
@@ -19,8 +19,8 @@ export type Blip =
1919 | { kind: "bomb"; x: number; y: number }
2020 | { kind: "site"; x: number; y: number; label: "A" | "B" };
2121
22 /** Covers the whole map from most standing points; a corner-to-corner shot is the rare miss. */
23 export const RADAR_RADIUS = map.GRID_W * map.CELL;
22 /** Covers the whole of `world.map` from most standing points; a corner-to-corner shot is the rare miss. */
23 const radarRadius = (world: World): number => world.map.gridW * CELL;
2424
2525 /**
2626 * A world offset (east, north), rotated by the view's yaw and dropped into
@@ -42,7 +42,7 @@ function project(ox: number, oy: number, yaw: number, radius: number): [number,
4242 * bomb once it is planted, and both sites, labelled. Never the other team —
4343 * that is what turns a radar into a wallhack.
4444 */
45 export function buildRadar(world: World, view: Player, radius: number = RADAR_RADIUS): Blip[] {
45 export function buildRadar(world: World, view: Player, radius: number = radarRadius(world)): Blip[] {
4646 const blips: Blip[] = [{ kind: "self", x: 0, y: 0, facing: 0 }];
4747
4848 for (const p of world.players) {
@@ -57,7 +57,7 @@ export function buildRadar(world: World, view: Player, radius: number = RADAR_RA
5757 }
5858
5959 for (const label of ["A", "B"] as const) {
60 const site = map.sites[label];
60 const site = world.map.sites[label];
6161 const at = project(site[0] - view.pos[0], site[1] - view.pos[1], view.yaw, radius);
6262 if (at !== null) blips.push({ kind: "site", x: at[0], y: at[1], label });
6363 }
src/client/renderer.ts +8 −2
@@ -17,6 +17,7 @@ import { fallenMatrix, multiply, perspective, viewMatrix, modelMatrix, type Vec3
1717 import {
1818 bombMesh, grenadeMesh, levelMesh, pickupMesh, playerMesh, viewModelMesh, type Mesh,
1919 } from "./models.ts";
20 import type { MapDef } from "./map.ts";
2021 import { MAX_SCALE, MIN_SCALE } from "./quality.ts";
2122 import { LAYERS, TEX_SIZE, materialTextures } from "./textures.ts";
2223 import type { Player, World } from "./world.ts";
@@ -175,8 +176,9 @@ type Gpu = {
175176 pickup: Buffers;
176177 grenade: Buffers;
177178 viewModels: Map<string, Buffers>;
178 /** Built on the first frame of a world, and once only: it is the map. */
179 /** Built the first time a map is drawn, and keyed by it: a switch of maps rebuilds it. */
179180 level: Buffers | null;
181 levelMap: MapDef | null;
180182 lineVao: WebGLVertexArrayObject;
181183 lineBuffer: WebGLBuffer | null;
182184 };
@@ -237,6 +239,7 @@ function build(gl: WebGL2RenderingContext): Gpu {
237239 grenade: upload(gl, grenadeMesh()),
238240 viewModels: new Map<string, Buffers>(),
239241 level: null,
242 levelMap: null,
240243 lineVao,
241244 lineBuffer,
242245 };
@@ -337,7 +340,10 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks =
337340 const look = viewMatrix(camera.pos, camera.yaw, camera.pitch);
338341 const viewProj = multiply(proj, look);
339342
340 if (gpu.level === null) gpu.level = upload(gl, levelMesh(world.boxes));
343 if (gpu.level === null || gpu.levelMap !== world.map) {
344 gpu.level = upload(gl, levelMesh(world.boxes));
345 gpu.levelMap = world.map;
346 }
341347 const level = gpu.level;
342348
343349 gl.useProgram(gpu.scene);
src/client/world.test.ts +12 −12
@@ -1,6 +1,6 @@
11 import { expect, test } from "bun:test";
22 import { GRENADE } from "./grenades.ts";
3 import { siteAt, sites } from "./map.ts";
3 import { DE_FORGE, siteAt } from "./map.ts";
44 import { SHORT, winReward } from "./match.ts";
55 import { WEAPONS } from "./weapons.ts";
66 import {
@@ -87,8 +87,8 @@ test("the terrorists plant, and a planted bomb keeps the round alive past the cl
8787 // Put the person on the site holding the bomb and press use.
8888 for (const p of world.players) p.hasBomb = false;
8989 world.me.hasBomb = true;
90 world.me.pos = [sites.A[0], sites.A[1], 2];
91 expect(siteAt(world.me.pos)).toBe("A");
90 world.me.pos = [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2];
91 expect(siteAt(DE_FORGE, world.me.pos)).toBe("A");
9292 // Standing still on the site for three seconds is how you get shot, and the
9393 // CT bots do: they kill the planter a second in. The fight is not what this
9494 // test is about, so the planter is kept on their feet for the plant.
@@ -105,11 +105,11 @@ test("the terrorists plant, and a planted bomb keeps the round alive past the cl
105105 test("a defusing CT wins the round, and the money follows the rules", () => {
106106 const world = createWorld(SHORT, "me", "ct", 5, 13);
107107 run(world, SHORT.freezeTime + 0.2);
108 world.bomb = { state: "planted", pos: [sites.B[0], sites.B[1], 2], site: "B", plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 };
108 world.bomb = { state: "planted", pos: [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2], site: "B", plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 };
109109 world.bombPlanted = true;
110110 world.match.phase = "planted";
111111 world.match.timer = 30;
112 world.me.pos = [sites.B[0], sites.B[1], 2];
112 world.me.pos = [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2];
113113 world.me.defuseKit = true;
114114 const before = world.me.money;
115115 run(world, SHORT.defuseTimeWithKit + 0.3, { ...NO_INPUT, use: true });
@@ -344,19 +344,19 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", (
344344 const world = createWorld(SHORT, "me", "ct", 5, 41);
345345 const far = world.players[1];
346346 world.bomb = {
347 state: "planted", pos: [sites.A[0], sites.A[1], 2], site: "A",
347 state: "planted", pos: [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2], site: "A",
348348 plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0,
349349 };
350350 world.bombPlanted = true;
351351 world.match.phase = "planted";
352352 world.match.timer = TICK / 2;
353353 for (const p of world.players) {
354 p.pos = [sites.A[0], sites.A[1], 2];
354 p.pos = [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2];
355355 p.alive = true;
356356 p.health = 100;
357357 p.hasBomb = false; // planted: nobody is still carrying it
358358 }
359 far.pos = [sites.B[0], sites.B[1], 2];
359 far.pos = [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2];
360360 world.effects.length = 0;
361361 update(world, TICK, NO_INPUT);
362362
@@ -367,13 +367,13 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", (
367367 expect(hurts.length).toBe(1); // only the person's own damage is ever recorded
368368 expect(hurts[0].victim).toBe(world.me.id);
369369 expect(hurts[0].amount).toBe(100);
370 expect(hurts[0].from).toEqual([sites.A[0], sites.A[1], 2]);
370 expect(hurts[0].from).toEqual([DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2]);
371371 });
372372
373373 test("the detonation pushes exactly one explode", () => {
374374 const world = createWorld(SHORT, "me", "ct", 5, 42);
375375 world.bomb = {
376 state: "planted", pos: [sites.A[0], sites.A[1], 2], site: "A",
376 state: "planted", pos: [DE_FORGE.sites.A[0], DE_FORGE.sites.A[1], 2], site: "A",
377377 plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0,
378378 };
379379 world.bombPlanted = true;
@@ -488,13 +488,13 @@ test("a planted bomb pushes beeps on that cadence, and a defuse stops them", ()
488488 const world = createWorld(SHORT, "me", "ct", 5, 71);
489489 run(world, SHORT.freezeTime + 0.2);
490490 world.bomb = {
491 state: "planted", pos: [sites.B[0], sites.B[1], 2], site: "B",
491 state: "planted", pos: [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2], site: "B",
492492 plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0,
493493 };
494494 world.bombPlanted = true;
495495 world.match.phase = "planted";
496496 world.match.timer = 40;
497 world.me.pos = [sites.B[0], sites.B[1], 2];
497 world.me.pos = [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2];
498498 world.me.defuseKit = true;
499499 world.effects.length = 0;
500500 run(world, 1.05); // just past the first beep interval at a fresh 40s
src/client/world.ts +9 −6
@@ -13,7 +13,7 @@ import { stepBeep } from "./cues.ts";
1313 import {
1414 blastDamage, GRENADE, step as stepGrenade, thrown, type Grenade,
1515 } from "./grenades.ts";
16 import * as map from "./map.ts";
16 import { DE_FORGE, siteAt, type MapDef } from "./map.ts";
1717 import { angleVectors, angleDelta, clamp, dist, dist2d, normalize, rng, sub, type Vec3 } from "./math.ts";
1818 import * as rules from "./match.ts";
1919 import { eyeHeight, hullHeight, move, type Body, type Wish, MOVE } from "./physics.ts";
@@ -141,7 +141,9 @@ export type World = {
141141 feed: FeedEntry[];
142142 messages: { at: number; text: string }[];
143143 random: () => number;
144 boxes: typeof map.boxes;
144 /** The layout this round is played on. */
145 map: MapDef;
146 boxes: MapDef["boxes"];
145147 bombPlanted: boolean; // in this round, for the economy
146148 pendingBuy: boolean;
147149 /** How hard the bots are. What that means is bots.ts's business, not this file's. */
@@ -182,6 +184,7 @@ export function createWorld(
182184 teamSize = 5,
183185 seed = (Date.now() & 0xffff) >>> 0,
184186 difficulty: Difficulty = DEFAULT_DIFFICULTY,
187 map: MapDef = DE_FORGE,
185188 ): World {
186189 const match = rules.newMatch(ruleSet, playerTeam);
187190 const players: Player[] = [];
@@ -205,7 +208,7 @@ export function createWorld(
205208 bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 },
206209 pickups: [], grenades: [], bodies: [],
207210 effects: [], feed: [], messages: [],
208 random, boxes: map.boxes, bombPlanted: false, pendingBuy: false,
211 random, map, boxes: map.boxes, bombPlanted: false, pendingBuy: false,
209212 difficulty, pendingDifficulty: null,
210213 };
211214 startRound(world);
@@ -244,10 +247,10 @@ export function startRound(world: World): void {
244247 world.difficulty = world.pendingDifficulty;
245248 world.pendingDifficulty = null;
246249 }
247 const spots = { t: [...map.spawns.t], ct: [...map.spawns.ct] };
250 const spots = { t: [...world.map.spawns.t], ct: [...world.map.spawns.ct] };
248251 for (const p of world.players) {
249252 const list = spots[p.team];
250 const at = list.splice(Math.floor(world.random() * list.length), 1)[0] ?? map.spawns[p.team][0];
253 const at = list.splice(Math.floor(world.random() * list.length), 1)[0] ?? world.map.spawns[p.team][0];
251254 p.pos = [at[0], at[1], 2];
252255 p.vel = [0, 0, 0];
253256 p.health = 100;
@@ -728,7 +731,7 @@ function updateBomb(world: World, p: Player, input: Input, dt: number): void {
728731 const bomb = world.bomb;
729732 if (bomb.state === "carried" && p.hasBomb && world.match.phase === "live") {
730733 bomb.pos = [p.pos[0], p.pos[1], p.pos[2]];
731 const site = map.siteAt(p.pos);
734 const site = siteAt(world.map, p.pos);
732735 if (input.use && site !== null && p.onGround) {
733736 bomb.plantProgress += dt;
734737 if (bomb.plantProgress >= world.match.rules.plantTime) {
src/rl/arena.ts +3 −3
@@ -14,7 +14,7 @@
1414
1515 import { decode, STILL, toInput, type Intent } from "../client/action.ts";
1616 import { makeBrain, botThink } from "../client/bots.ts";
17 import * as map from "../client/map.ts";
17 import { DE_FORGE, walkable, worldOf } from "../client/map.ts";
1818 import { angleDelta, clamp, dist, rng, type Vec3 } from "../client/math.ts";
1919 import { observe, OBS_SIZE, type Memory } from "../client/observe.ts";
2020 import type { Input, Player, World } from "../client/world.ts";
@@ -36,8 +36,8 @@ export const SKIP = 4;
3636 /** Every cell a player can stand in, once, so a reset is two array lookups. */
3737 const OPEN: Vec3[] = (() => {
3838 const out: Vec3[] = [];
39 for (let r = 0; r < map.GRID_H; r++)
40 for (let c = 0; c < map.GRID_W; c++) if (map.walkable(c, r)) out.push(map.worldOf(c, r, 2));
39 for (let r = 0; r < DE_FORGE.gridH; r++)
40 for (let c = 0; c < DE_FORGE.gridW; c++) if (walkable(DE_FORGE, c, r)) out.push(worldOf(DE_FORGE, c, r, 2));
4141 return out;
4242 })();
4343

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs