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
Check · Review · Land · Live done — Landed and live in production.
scout/cs:scout/73-one-map-held-as-a-value-map-ts-stops-bei → main 1 commit fork
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. | ||
| 218 | 218 | |
| 219 | 219 | ## Shape |
| 220 | 220 | |
| 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), | |
| 222 | 225 | `physics`, `weapons`, `combat`, `grenades` (a thrown thing's flight: gravity, |
| 223 | 226 | bounce, fuse and how far a blast reaches), `match` (the rules), `world` (the simulation), |
| 224 | 227 | `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"; | ||
| 2 | 2 | import { |
| 3 | 3 | applyIntent, botThink, CARD, DIFFICULTIES, makeBrain, policy, restyleBrain, skillBand, think, |
| 4 | 4 | } from "./bots.ts"; |
| 5 | import * as map from "./map.ts"; | |
| 5 | import { DE_FORGE } from "./map.ts"; | |
| 6 | 6 | import { COMPETITIVE, SHORT } from "./match.ts"; |
| 7 | 7 | import { |
| 8 | 8 | 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", () => { | ||
| 200 | 200 | const world = createWorld(RULES, "you", "ct", 1, 4242); |
| 201 | 201 | world.match.phase = "live"; |
| 202 | 202 | 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 | |
| 204 | 204 | bot.hasBomb = false; |
| 205 | 205 | world.bomb = { ...world.bomb, state: "dropped", pos: [bot.pos[0] + 240, bot.pos[1], bot.pos[2] + 4] }; |
| 206 | 206 | 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 @@ | ||
| 13 | 13 | |
| 14 | 14 | import { decode, HEADS, STILL, type Intent } from "./action.ts"; |
| 15 | 15 | 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"; | |
| 17 | 17 | import { observe, OBS_SIZE, type Memory } from "./observe.ts"; |
| 18 | 18 | import { mustCommit, siteFor, type Site } from "./plan.ts"; |
| 19 | 19 | import { |
| @@ -186,7 +186,7 @@ const DETOUR = 1.3; | ||
| 186 | 186 | */ |
| 187 | 187 | function committed(world: World, bot: Player, brain: BotBrain): boolean { |
| 188 | 188 | 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; | |
| 190 | 190 | const walk = Math.max(crow, pathLeft(brain, bot.pos)); |
| 191 | 191 | return mustCommit(world.match.timer, walk, world.match.rules.plantTime); |
| 192 | 192 | } |
| @@ -195,9 +195,9 @@ function committed(world: World, bot: Player, brain: BotBrain): boolean { | ||
| 195 | 195 | const CONTACT_RANGE = 1600; |
| 196 | 196 | |
| 197 | 197 | /** 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); | |
| 201 | 201 | if (Math.min(a, b) > CONTACT_RANGE) return null; |
| 202 | 202 | return a <= b ? "A" : "B"; |
| 203 | 203 | } |
| @@ -217,7 +217,7 @@ function rotateTo(world: World, bot: Player): Site | null { | ||
| 217 | 217 | if (entry.killerTeam === bot.team || entry.at < freshest) continue; |
| 218 | 218 | for (const p of world.players) { |
| 219 | 219 | if (p.id !== entry.victimId || p.team !== bot.team) continue; |
| 220 | const where = contactSite(p.pos); | |
| 220 | const where = contactSite(world, p.pos); | |
| 221 | 221 | if (where === null) continue; |
| 222 | 222 | freshest = entry.at; |
| 223 | 223 | site = where; |
| @@ -232,22 +232,22 @@ function chooseGoal(world: World, bot: Player, brain: BotBrain): Vec3 { | ||
| 232 | 232 | if (bot.team === "t") { |
| 233 | 233 | if (bomb.state === "planted") { |
| 234 | 234 | // Hold near the bomb rather than walk onto it. |
| 235 | return nearby(brain, bomb.pos, 300); | |
| 235 | return nearby(world, brain, bomb.pos, 300); | |
| 236 | 236 | } |
| 237 | if (bot.hasBomb) return map.sites[brain.site]; | |
| 237 | if (bot.hasBomb) return world.map.sites[brain.site]; | |
| 238 | 238 | 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); | |
| 240 | 240 | } |
| 241 | 241 | if (bomb.state === "planted") return bomb.pos; |
| 242 | 242 | 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); | |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | const nearby = (brain: BotBrain, at: Vec3, spread: number): Vec3 => { | |
| 246 | const nearby = (world: World, brain: BotBrain, at: Vec3, spread: number): Vec3 => { | |
| 247 | 247 | for (let tries = 0; tries < 8; tries++) { |
| 248 | 248 | const x = at[0] + (brain.random() * 2 - 1) * spread; |
| 249 | 249 | 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]]; | |
| 251 | 251 | } |
| 252 | 252 | return at; |
| 253 | 253 | }; |
| @@ -380,7 +380,7 @@ function repath(world: World, bot: Player, brain: BotBrain): void { | ||
| 380 | 380 | world.time > brain.repathAt || objective !== brain.plannedFor; |
| 381 | 381 | if (!stale) return; |
| 382 | 382 | 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); | |
| 384 | 384 | brain.step = 0; |
| 385 | 385 | brain.plannedFor = objective; |
| 386 | 386 | brain.repathAt = world.time + 3 + brain.random() * 3; |
| @@ -484,7 +484,7 @@ export function botThink(world: World, bot: Player, dt: number): Input { | ||
| 484 | 484 | /** Planting and defusing: press use when standing where it matters. */ |
| 485 | 485 | function withBomb(world: World, bot: Player, input: Input): Input { |
| 486 | 486 | 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") { | |
| 488 | 488 | input.use = true; |
| 489 | 489 | input.forward = 0; |
| 490 | 490 | input.side = 0; |
src/client/grenades.test.ts +5 −6
| @@ -1,7 +1,6 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | 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"; | |
| 5 | 4 | import type { Vec3 } from "./math.ts"; |
| 6 | 5 | import { BOMB_BLAST_RADIUS } from "./world.ts"; |
| 7 | 6 | import { WEAPONS } from "./weapons.ts"; |
| @@ -64,16 +63,16 @@ test("a wall bounces it back, slower, and it never ends up inside one", () => { | ||
| 64 | 63 | }); |
| 65 | 64 | |
| 66 | 65 | 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; | |
| 68 | 67 | const eye: Vec3 = [at[0], at[1], 64]; |
| 69 | 68 | // Eight directions from a spawn, hard: none of them may put it outside. |
| 70 | 69 | for (let i = 0; i < 8; i++) { |
| 71 | 70 | 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); | |
| 73 | 72 | 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); | |
| 75 | 74 | 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); | |
| 77 | 76 | } |
| 78 | 77 | }); |
| 79 | 78 |
src/client/hud.ts +15 −12
| @@ -9,9 +9,9 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import { angleDelta, clamp, rad } from "./math.ts"; |
| 12 | import * as map from "./map.ts"; | |
| 12 | import { walkable, type MapDef } from "./map.ts"; | |
| 13 | 13 | import { MOVE } from "./physics.ts"; |
| 14 | import { buildRadar, RADAR_RADIUS } from "./radar.ts"; | |
| 14 | import { buildRadar } from "./radar.ts"; | |
| 15 | 15 | import { deathNotice } from "./spectate.ts"; |
| 16 | 16 | import { inaccuracy, WEAPONS } from "./weapons.ts"; |
| 17 | 17 | import { currentWeapon, eyePos, offeredPickup, useProgress, type Player, type World } from "./world.ts"; |
| @@ -102,13 +102,15 @@ const clock = (seconds: number): string => { | ||
| 102 | 102 | // The radar's map is a few hundred walkable cells, the same every frame: baked |
| 103 | 103 | // once into an offscreen canvas at the on-screen size and rotated by the |
| 104 | 104 | // 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. | |
| 107 | 108 | let radarMap: OffscreenCanvas | HTMLCanvasElement | null = null; |
| 108 | 109 | let radarMapSize = -1; |
| 110 | let radarMapFor: MapDef | null = null; | |
| 109 | 111 | |
| 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) { | |
| 112 | 114 | if (typeof OffscreenCanvas !== "undefined") radarMap = new OffscreenCanvas(size, size); |
| 113 | 115 | else if (typeof document !== "undefined") { |
| 114 | 116 | const canvas = document.createElement("canvas"); |
| @@ -117,12 +119,13 @@ function radarMapImage(size: number): CanvasImageSource | null { | ||
| 117 | 119 | radarMap = canvas; |
| 118 | 120 | } else return null; // no canvas to build one on, e.g. a headless test |
| 119 | 121 | 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; | |
| 121 | 123 | 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); | |
| 125 | 127 | radarMapSize = size; |
| 128 | radarMapFor = map; | |
| 126 | 129 | } |
| 127 | 130 | return radarMap; |
| 128 | 131 | } |
| @@ -152,7 +155,7 @@ function drawRadar( | ||
| 152 | 155 | ctx.beginPath(); |
| 153 | 156 | ctx.arc(cx, cy, radius, 0, Math.PI * 2); |
| 154 | 157 | ctx.clip(); |
| 155 | const image = radarMapImage(Math.max(1, Math.round(size))); | |
| 158 | const image = radarMapImage(world.map, Math.max(1, Math.round(size))); | |
| 156 | 159 | if (image !== null) { |
| 157 | 160 | // The image is baked with north up; turning it to match the view's yaw |
| 158 | 161 | // is one canvas rotation, not a re-walk of the grid. |
| @@ -165,7 +168,7 @@ function drawRadar( | ||
| 165 | 168 | |
| 166 | 169 | const teamColor = view.team === "t" ? T_COLOR : CT_COLOR; |
| 167 | 170 | ctx.textAlign = "center"; |
| 168 | for (const blip of buildRadar(world, view, RADAR_RADIUS)) { | |
| 171 | for (const blip of buildRadar(world, view)) { | |
| 169 | 172 | const px = cx + blip.x * radius, py = cy + blip.y * radius; |
| 170 | 173 | if (blip.kind === "site") { |
| 171 | 174 | ctx.fillStyle = "rgba(255, 255, 255, 0.9)"; |
src/client/map.test.ts +70 −25
| @@ -1,64 +1,65 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | 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, | |
| 4 | 4 | } from "./map.ts"; |
| 5 | 5 | |
| 6 | 6 | 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"); | |
| 11 | 11 | }); |
| 12 | 12 | |
| 13 | 13 | 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("#"); | |
| 17 | 17 | } |
| 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("#"); | |
| 21 | 21 | } |
| 22 | 22 | }); |
| 23 | 23 | |
| 24 | 24 | test("grid and world coordinates round-trip", () => { |
| 25 | 25 | 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); | |
| 29 | 29 | } |
| 30 | 30 | }); |
| 31 | 31 | |
| 32 | 32 | test("every walkable cell is reachable from T spawn", () => { |
| 33 | 33 | // 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]; | |
| 35 | 35 | 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])]; | |
| 37 | 37 | seen.add(queue[0]); |
| 38 | 38 | while (queue.length > 0) { |
| 39 | 39 | 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); | |
| 41 | 41 | 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; | |
| 44 | 44 | if (seen.has(j)) continue; |
| 45 | 45 | seen.add(j); |
| 46 | 46 | queue.push(j); |
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | 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++; | |
| 51 | 52 | expect(seen.size).toBe(walkables); |
| 52 | 53 | }); |
| 53 | 54 | |
| 54 | 55 | 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); | |
| 57 | 58 | }); |
| 58 | 59 | |
| 59 | 60 | 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); | |
| 62 | 63 | expect(path.length).toBeGreaterThan(4); |
| 63 | 64 | // Consecutive waypoints are neighbouring cells, never a jump across a wall. |
| 64 | 65 | for (let i = 1; i < path.length - 1; i++) { |
| @@ -69,5 +70,49 @@ test("a path from T spawn reaches both sites", () => { | ||
| 69 | 70 | }); |
| 70 | 71 | |
| 71 | 72 | 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); | |
| 73 | 118 | }); |
src/client/map.ts +149 −110
| @@ -4,6 +4,10 @@ | ||
| 4 | 4 | * everything — what you collide with, what gets drawn, where bots walk, where |
| 5 | 5 | * the bomb can be planted — so the map can never disagree with itself. |
| 6 | 6 | * |
| 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 | * | |
| 7 | 11 | * Layout is dust-shaped and original: two sites, a mid, a long right-hand |
| 8 | 12 | * corridor and a left-hand tunnel, T at the bottom of the grid, CT at the top. |
| 9 | 13 | */ |
| @@ -11,107 +15,129 @@ | ||
| 11 | 15 | import type { Vec3 } from "./math.ts"; |
| 12 | 16 | |
| 13 | 17 | export const CELL = 128; |
| 14 | export const GRID_W = 44; | |
| 15 | export const GRID_H = 44; | |
| 16 | 18 | export const WALL_HEIGHT = 384; |
| 17 | 19 | |
| 18 | 20 | export type Cell = "#" | "." | "x" | "X" | "A" | "B" | "t" | "c"; |
| 19 | 21 | |
| 20 | 22 | /** [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 }; | |
| 22 | 24 | |
| 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 | }; | |
| 43 | 33 | |
| 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 }; | |
| 52 | 36 | |
| 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) { | |
| 56 | 85 | const [c0, r0, c1, r1] = area.rect; |
| 57 | 86 | 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; | |
| 59 | 88 | } |
| 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; | |
| 61 | 90 | return g; |
| 62 | 91 | } |
| 63 | 92 | |
| 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); | |
| 68 | 97 | |
| 69 | 98 | /** 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); | |
| 72 | 101 | return c !== "#" && c !== "x" && c !== "X"; |
| 73 | 102 | }; |
| 74 | 103 | |
| 75 | 104 | /** 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, | |
| 79 | 108 | z, |
| 80 | 109 | ]; |
| 81 | 110 | |
| 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); | |
| 84 | 113 | |
| 85 | 114 | // --- geometry ------------------------------------------------------------- |
| 86 | 115 | |
| 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 | ||
| 90 | 116 | /** |
| 91 | 117 | * Greedy meshing: merge runs of identical solid cells across a row, then merge |
| 92 | 118 | * rows below that match the run exactly. A few hundred boxes instead of a few |
| 93 | 119 | * thousand, which is the difference between one buffer and a stutter. |
| 94 | 120 | */ |
| 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); | |
| 97 | 123 | 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; | |
| 102 | 128 | 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++; | |
| 104 | 130 | let h = 1; |
| 105 | grow: while (r + h < GRID_H) { | |
| 131 | grow: while (r + h < map.gridH) { | |
| 106 | 132 | 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; | |
| 109 | 135 | } |
| 110 | 136 | h++; |
| 111 | 137 | } |
| 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); | |
| 115 | 141 | out.push({ |
| 116 | 142 | min: [a[0] - CELL / 2, b[1] - CELL / 2, z0], |
| 117 | 143 | 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 | ||
| 122 | 148 | return out; |
| 123 | 149 | } |
| 124 | 150 | |
| 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) { | |
| 130 | 156 | c0 = Math.min(c0, c); c1 = Math.max(c1, c); |
| 131 | 157 | r0 = Math.min(r0, r); r1 = Math.max(r1, r); |
| 132 | 158 | } |
| 133 | const a = worldOf(c0, r0), b = worldOf(c1, r1); | |
| 159 | const a = worldOf(map, c0, r0), b = worldOf(map, c1, r1); | |
| 134 | 160 | return { |
| 135 | 161 | min: [a[0] - CELL / 2, b[1] - CELL / 2, 0], |
| 136 | 162 | max: [b[0] + CELL / 2, a[1] + CELL / 2, 2], |
| @@ -138,66 +164,79 @@ function siteSlab(fill: Cell): Box { | ||
| 138 | 164 | }; |
| 139 | 165 | } |
| 140 | 166 | |
| 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[] { | |
| 157 | 168 | 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)); | |
| 160 | 171 | return out; |
| 161 | 172 | } |
| 162 | 173 | |
| 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 | ||
| 171 | 174 | function centre(list: Vec3[]): Vec3 { |
| 172 | 175 | const s = list.reduce((a, p) => [a[0] + p[0], a[1] + p[1], 0] as Vec3, [0, 0, 0] as Vec3); |
| 173 | 176 | return [s[0] / list.length, s[1] / list.length, 0]; |
| 174 | 177 | } |
| 175 | 178 | |
| 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 | ||
| 176 | 215 | /** 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])); | |
| 179 | 218 | return c === "A" || c === "B" ? c : null; |
| 180 | 219 | } |
| 181 | 220 | |
| 182 | 221 | // --- pathfinding ---------------------------------------------------------- |
| 183 | 222 | |
| 184 | 223 | /** |
| 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. | |
| 188 | 227 | */ |
| 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]); | |
| 192 | 231 | if (start === goal) return [to]; |
| 193 | const n = GRID_W * GRID_H; | |
| 232 | const n = map.gridW * map.gridH; | |
| 194 | 233 | if (start < 0 || start >= n || goal < 0 || goal >= n) return []; |
| 195 | 234 | const g = new Float32Array(n).fill(Infinity); |
| 196 | 235 | const from_ = new Int32Array(n).fill(-1); |
| 197 | 236 | const open: number[] = [start]; |
| 198 | 237 | const f = new Float32Array(n).fill(Infinity); |
| 199 | 238 | 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)); | |
| 201 | 240 | g[start] = 0; |
| 202 | 241 | f[start] = h(start); |
| 203 | 242 | const seen = new Uint8Array(n); |
| @@ -208,14 +247,14 @@ export function findPath(from: Vec3, to: Vec3): Vec3[] { | ||
| 208 | 247 | const cur = open.splice(bestAt, 1)[0] as number; |
| 209 | 248 | if (cur === goal) break; |
| 210 | 249 | 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); | |
| 212 | 251 | for (let dr = -1; dr <= 1; dr++) { |
| 213 | 252 | for (let dc = -1; dc <= 1; dc++) { |
| 214 | 253 | if (dc === 0 && dr === 0) continue; |
| 215 | 254 | 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; | |
| 219 | 258 | if (seen[ni] === 1) continue; |
| 220 | 259 | const step = dc !== 0 && dr !== 0 ? 1.414 : 1; |
| 221 | 260 | const cost = (g[cur] as number) + step; |
| @@ -231,7 +270,7 @@ export function findPath(from: Vec3, to: Vec3): Vec3[] { | ||
| 231 | 270 | if (from_[goal] === -1) return []; |
| 232 | 271 | const out: Vec3[] = []; |
| 233 | 272 | 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))); | |
| 235 | 274 | out.push(to); |
| 236 | 275 | return out; |
| 237 | 276 | } |
src/client/observe.ts +8 −6
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | * so OBS_SIZE and the order below are part of the policy's contract. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import * as map from "./map.ts"; | |
| 15 | import { colOf, rowOf, walkable, type MapDef } from "./map.ts"; | |
| 16 | 16 | import { angleDelta, clamp, dist, type Vec3 } from "./math.ts"; |
| 17 | 17 | import { HULL } from "./physics.ts"; |
| 18 | 18 | import { inaccuracy, WEAPONS } from "./weapons.ts"; |
| @@ -29,11 +29,11 @@ export type Memory = { | ||
| 29 | 29 | const rad = Math.PI / 180; |
| 30 | 30 | |
| 31 | 31 | /** 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 => { | |
| 33 | 33 | const a = (yaw + offset) * rad; |
| 34 | 34 | const x = from[0] + Math.cos(a) * d; |
| 35 | 35 | 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; | |
| 37 | 37 | }; |
| 38 | 38 | |
| 39 | 39 | /** |
| @@ -81,9 +81,11 @@ export function observe( | ||
| 81 | 81 | }); |
| 82 | 82 | const magazine = Math.max(1, weapon.magazine); |
| 83 | 83 | 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; | |
| 87 | 89 | |
| 88 | 90 | out[at + 0] = 1; // a bias, so a dead layer still has an offset to lean on |
| 89 | 91 | out[at + 1] = target === null ? 0 : 1; |
src/client/radar.ts +5 −5
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | import { rad } from "./math.ts"; |
| 13 | import * as map from "./map.ts"; | |
| 13 | import { CELL } from "./map.ts"; | |
| 14 | 14 | import type { Player, World } from "./world.ts"; |
| 15 | 15 | |
| 16 | 16 | export type Blip = |
| @@ -19,8 +19,8 @@ export type Blip = | ||
| 19 | 19 | | { kind: "bomb"; x: number; y: number } |
| 20 | 20 | | { kind: "site"; x: number; y: number; label: "A" | "B" }; |
| 21 | 21 | |
| 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; | |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * 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, | ||
| 42 | 42 | * bomb once it is planted, and both sites, labelled. Never the other team — |
| 43 | 43 | * that is what turns a radar into a wallhack. |
| 44 | 44 | */ |
| 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[] { | |
| 46 | 46 | const blips: Blip[] = [{ kind: "self", x: 0, y: 0, facing: 0 }]; |
| 47 | 47 | |
| 48 | 48 | for (const p of world.players) { |
| @@ -57,7 +57,7 @@ export function buildRadar(world: World, view: Player, radius: number = RADAR_RA | ||
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | for (const label of ["A", "B"] as const) { |
| 60 | const site = map.sites[label]; | |
| 60 | const site = world.map.sites[label]; | |
| 61 | 61 | const at = project(site[0] - view.pos[0], site[1] - view.pos[1], view.yaw, radius); |
| 62 | 62 | if (at !== null) blips.push({ kind: "site", x: at[0], y: at[1], label }); |
| 63 | 63 | } |
src/client/renderer.ts +8 −2
| @@ -17,6 +17,7 @@ import { fallenMatrix, multiply, perspective, viewMatrix, modelMatrix, type Vec3 | ||
| 17 | 17 | import { |
| 18 | 18 | bombMesh, grenadeMesh, levelMesh, pickupMesh, playerMesh, viewModelMesh, type Mesh, |
| 19 | 19 | } from "./models.ts"; |
| 20 | import type { MapDef } from "./map.ts"; | |
| 20 | 21 | import { MAX_SCALE, MIN_SCALE } from "./quality.ts"; |
| 21 | 22 | import { LAYERS, TEX_SIZE, materialTextures } from "./textures.ts"; |
| 22 | 23 | import type { Player, World } from "./world.ts"; |
| @@ -175,8 +176,9 @@ type Gpu = { | ||
| 175 | 176 | pickup: Buffers; |
| 176 | 177 | grenade: Buffers; |
| 177 | 178 | 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. */ | |
| 179 | 180 | level: Buffers | null; |
| 181 | levelMap: MapDef | null; | |
| 180 | 182 | lineVao: WebGLVertexArrayObject; |
| 181 | 183 | lineBuffer: WebGLBuffer | null; |
| 182 | 184 | }; |
| @@ -237,6 +239,7 @@ function build(gl: WebGL2RenderingContext): Gpu { | ||
| 237 | 239 | grenade: upload(gl, grenadeMesh()), |
| 238 | 240 | viewModels: new Map<string, Buffers>(), |
| 239 | 241 | level: null, |
| 242 | levelMap: null, | |
| 240 | 243 | lineVao, |
| 241 | 244 | lineBuffer, |
| 242 | 245 | }; |
| @@ -337,7 +340,10 @@ export function createRenderer(canvas: HTMLCanvasElement, hooks: RendererHooks = | ||
| 337 | 340 | const look = viewMatrix(camera.pos, camera.yaw, camera.pitch); |
| 338 | 341 | const viewProj = multiply(proj, look); |
| 339 | 342 | |
| 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 | } | |
| 341 | 347 | const level = gpu.level; |
| 342 | 348 | |
| 343 | 349 | gl.useProgram(gpu.scene); |
src/client/world.test.ts +12 −12
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | 2 | import { GRENADE } from "./grenades.ts"; |
| 3 | import { siteAt, sites } from "./map.ts"; | |
| 3 | import { DE_FORGE, siteAt } from "./map.ts"; | |
| 4 | 4 | import { SHORT, winReward } from "./match.ts"; |
| 5 | 5 | import { WEAPONS } from "./weapons.ts"; |
| 6 | 6 | import { |
| @@ -87,8 +87,8 @@ test("the terrorists plant, and a planted bomb keeps the round alive past the cl | ||
| 87 | 87 | // Put the person on the site holding the bomb and press use. |
| 88 | 88 | for (const p of world.players) p.hasBomb = false; |
| 89 | 89 | 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"); | |
| 92 | 92 | // Standing still on the site for three seconds is how you get shot, and the |
| 93 | 93 | // CT bots do: they kill the planter a second in. The fight is not what this |
| 94 | 94 | // 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 | ||
| 105 | 105 | test("a defusing CT wins the round, and the money follows the rules", () => { |
| 106 | 106 | const world = createWorld(SHORT, "me", "ct", 5, 13); |
| 107 | 107 | 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 }; | |
| 109 | 109 | world.bombPlanted = true; |
| 110 | 110 | world.match.phase = "planted"; |
| 111 | 111 | 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]; | |
| 113 | 113 | world.me.defuseKit = true; |
| 114 | 114 | const before = world.me.money; |
| 115 | 115 | 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", ( | ||
| 344 | 344 | const world = createWorld(SHORT, "me", "ct", 5, 41); |
| 345 | 345 | const far = world.players[1]; |
| 346 | 346 | 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", | |
| 348 | 348 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, |
| 349 | 349 | }; |
| 350 | 350 | world.bombPlanted = true; |
| 351 | 351 | world.match.phase = "planted"; |
| 352 | 352 | world.match.timer = TICK / 2; |
| 353 | 353 | 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]; | |
| 355 | 355 | p.alive = true; |
| 356 | 356 | p.health = 100; |
| 357 | 357 | p.hasBomb = false; // planted: nobody is still carrying it |
| 358 | 358 | } |
| 359 | far.pos = [sites.B[0], sites.B[1], 2]; | |
| 359 | far.pos = [DE_FORGE.sites.B[0], DE_FORGE.sites.B[1], 2]; | |
| 360 | 360 | world.effects.length = 0; |
| 361 | 361 | update(world, TICK, NO_INPUT); |
| 362 | 362 | |
| @@ -367,13 +367,13 @@ test("the bomb going off hurts everyone inside its radius and nobody outside", ( | ||
| 367 | 367 | expect(hurts.length).toBe(1); // only the person's own damage is ever recorded |
| 368 | 368 | expect(hurts[0].victim).toBe(world.me.id); |
| 369 | 369 | 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]); | |
| 371 | 371 | }); |
| 372 | 372 | |
| 373 | 373 | test("the detonation pushes exactly one explode", () => { |
| 374 | 374 | const world = createWorld(SHORT, "me", "ct", 5, 42); |
| 375 | 375 | 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", | |
| 377 | 377 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, |
| 378 | 378 | }; |
| 379 | 379 | world.bombPlanted = true; |
| @@ -488,13 +488,13 @@ test("a planted bomb pushes beeps on that cadence, and a defuse stops them", () | ||
| 488 | 488 | const world = createWorld(SHORT, "me", "ct", 5, 71); |
| 489 | 489 | run(world, SHORT.freezeTime + 0.2); |
| 490 | 490 | 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", | |
| 492 | 492 | plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0, |
| 493 | 493 | }; |
| 494 | 494 | world.bombPlanted = true; |
| 495 | 495 | world.match.phase = "planted"; |
| 496 | 496 | 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]; | |
| 498 | 498 | world.me.defuseKit = true; |
| 499 | 499 | world.effects.length = 0; |
| 500 | 500 | 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"; | ||
| 13 | 13 | import { |
| 14 | 14 | blastDamage, GRENADE, step as stepGrenade, thrown, type Grenade, |
| 15 | 15 | } from "./grenades.ts"; |
| 16 | import * as map from "./map.ts"; | |
| 16 | import { DE_FORGE, siteAt, type MapDef } from "./map.ts"; | |
| 17 | 17 | import { angleVectors, angleDelta, clamp, dist, dist2d, normalize, rng, sub, type Vec3 } from "./math.ts"; |
| 18 | 18 | import * as rules from "./match.ts"; |
| 19 | 19 | import { eyeHeight, hullHeight, move, type Body, type Wish, MOVE } from "./physics.ts"; |
| @@ -141,7 +141,9 @@ export type World = { | ||
| 141 | 141 | feed: FeedEntry[]; |
| 142 | 142 | messages: { at: number; text: string }[]; |
| 143 | 143 | random: () => number; |
| 144 | boxes: typeof map.boxes; | |
| 144 | /** The layout this round is played on. */ | |
| 145 | map: MapDef; | |
| 146 | boxes: MapDef["boxes"]; | |
| 145 | 147 | bombPlanted: boolean; // in this round, for the economy |
| 146 | 148 | pendingBuy: boolean; |
| 147 | 149 | /** How hard the bots are. What that means is bots.ts's business, not this file's. */ |
| @@ -182,6 +184,7 @@ export function createWorld( | ||
| 182 | 184 | teamSize = 5, |
| 183 | 185 | seed = (Date.now() & 0xffff) >>> 0, |
| 184 | 186 | difficulty: Difficulty = DEFAULT_DIFFICULTY, |
| 187 | map: MapDef = DE_FORGE, | |
| 185 | 188 | ): World { |
| 186 | 189 | const match = rules.newMatch(ruleSet, playerTeam); |
| 187 | 190 | const players: Player[] = []; |
| @@ -205,7 +208,7 @@ export function createWorld( | ||
| 205 | 208 | bomb: { state: "carried", pos: [0, 0, 0], site: null, plantProgress: 0, defuseProgress: 0, defuser: null, beepAt: 0 }, |
| 206 | 209 | pickups: [], grenades: [], bodies: [], |
| 207 | 210 | effects: [], feed: [], messages: [], |
| 208 | random, boxes: map.boxes, bombPlanted: false, pendingBuy: false, | |
| 211 | random, map, boxes: map.boxes, bombPlanted: false, pendingBuy: false, | |
| 209 | 212 | difficulty, pendingDifficulty: null, |
| 210 | 213 | }; |
| 211 | 214 | startRound(world); |
| @@ -244,10 +247,10 @@ export function startRound(world: World): void { | ||
| 244 | 247 | world.difficulty = world.pendingDifficulty; |
| 245 | 248 | world.pendingDifficulty = null; |
| 246 | 249 | } |
| 247 | const spots = { t: [...map.spawns.t], ct: [...map.spawns.ct] }; | |
| 250 | const spots = { t: [...world.map.spawns.t], ct: [...world.map.spawns.ct] }; | |
| 248 | 251 | for (const p of world.players) { |
| 249 | 252 | 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]; | |
| 251 | 254 | p.pos = [at[0], at[1], 2]; |
| 252 | 255 | p.vel = [0, 0, 0]; |
| 253 | 256 | p.health = 100; |
| @@ -728,7 +731,7 @@ function updateBomb(world: World, p: Player, input: Input, dt: number): void { | ||
| 728 | 731 | const bomb = world.bomb; |
| 729 | 732 | if (bomb.state === "carried" && p.hasBomb && world.match.phase === "live") { |
| 730 | 733 | 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); | |
| 732 | 735 | if (input.use && site !== null && p.onGround) { |
| 733 | 736 | bomb.plantProgress += dt; |
| 734 | 737 | if (bomb.plantProgress >= world.match.rules.plantTime) { |
src/rl/arena.ts +3 −3
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | |
| 15 | 15 | import { decode, STILL, toInput, type Intent } from "../client/action.ts"; |
| 16 | 16 | 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"; | |
| 18 | 18 | import { angleDelta, clamp, dist, rng, type Vec3 } from "../client/math.ts"; |
| 19 | 19 | import { observe, OBS_SIZE, type Memory } from "../client/observe.ts"; |
| 20 | 20 | import type { Input, Player, World } from "../client/world.ts"; |
| @@ -36,8 +36,8 @@ export const SKIP = 4; | ||
| 36 | 36 | /** Every cell a player can stand in, once, so a reset is two array lookups. */ |
| 37 | 37 | const OPEN: Vec3[] = (() => { |
| 38 | 38 | 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)); | |
| 41 | 41 | return out; |
| 42 | 42 | })(); |
| 43 | 43 |
new above
offline