walkDistance: say which argument is cached, and guard the slow order (#24 ite… #39
wren/val:wren/38-walkdistance-say-which-argument-is-cache → main 1 commit fork
-
Merged.
Review
Sign in to review this change.
2 files
changed +66
−2
src/engine/grid.test.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { makeGrid } from "./grid.ts"; | |
| 3 | ||
| 4 | const LINES = ["#########", "#.......#", "#.##.##.#", "#.......#", "#########"]; | |
| 5 | const cells = (g: ReturnType<typeof makeGrid>) => g.cellsOf("."); | |
| 6 | ||
| 7 | test("many distances to one fixed target build one field", () => { | |
| 8 | const g = makeGrid(LINES, 10); | |
| 9 | const target = cells(g)[0]!; | |
| 10 | for (const from of cells(g)) g.walkDistance(from, target); | |
| 11 | expect(g.fieldsBuilt()).toBe(1); | |
| 12 | }); | |
| 13 | ||
| 14 | test("varying the target builds a field per target", () => { | |
| 15 | const g = makeGrid(LINES, 10); | |
| 16 | const from = cells(g)[0]!; | |
| 17 | for (const to of cells(g)) g.walkDistance(from, to); | |
| 18 | expect(g.fieldsBuilt()).toBe(cells(g).length); | |
| 19 | }); | |
| 20 | ||
| 21 | test("pathStep shares the field walkDistance built for the goal", () => { | |
| 22 | const g = makeGrid(LINES, 10); | |
| 23 | const goal = cells(g).at(-1)!; | |
| 24 | for (const from of cells(g)) { | |
| 25 | g.walkDistance(from, goal); | |
| 26 | g.pathStep(from, goal); | |
| 27 | } | |
| 28 | expect(g.fieldsBuilt()).toBe(1); | |
| 29 | }); | |
| 30 | ||
| 31 | test("walk distance is symmetric, so swapping the arguments is safe", () => { | |
| 32 | const g = makeGrid(LINES, 10); | |
| 33 | for (const a of cells(g)) for (const b of cells(g)) expect(g.walkDistance(a, b)).toBe(g.walkDistance(b, a)); | |
| 34 | }); | |
| 35 | ||
| 36 | test("clearPaths forgets the fields, so the next ask builds again", () => { | |
| 37 | const g = makeGrid(LINES, 10); | |
| 38 | const [a, b] = cells(g); | |
| 39 | g.walkDistance(a!, b!); | |
| 40 | g.clearPaths(); | |
| 41 | g.walkDistance(a!, b!); | |
| 42 | expect(g.fieldsBuilt()).toBe(2); | |
| 43 | }); |
src/engine/grid.ts +23 −2
| @@ -10,6 +10,10 @@ | ||
| 10 | 10 | * `makeGrid` binds the maths to one set of rows and one tile size. The |
| 11 | 11 | * collision itself is `glide.ts`, which the browser runs too; the walk |
| 12 | 12 | * distances are cached per goal for the round and cleared by the caller. |
| 13 | * | |
| 14 | * Argument order matters for speed: a walk field is built for `to` and read | |
| 15 | * at `from`. Keep `to` fixed and vary `from`; walking is symmetric on this | |
| 16 | * grid, so swapping the two never changes the answer. | |
| 13 | 17 | */ |
| 14 | 18 | |
| 15 | 19 | import { circleBlocked, slideCircle } from "./glide.ts"; |
| @@ -39,9 +43,16 @@ export type Grid = { | ||
| 39 | 43 | blockedCircle: (x: number, y: number, r: number) => boolean; |
| 40 | 44 | wallBetween: (a: Point, b: Point) => boolean; |
| 41 | 45 | rayHitWall: (from: Point, angle: number, range: number) => Point; |
| 46 | /** | |
| 47 | * Tiles walked between two points, or -1. Cached by `to`: hold `to` fixed | |
| 48 | * and vary `from`, never the other way (symmetric, so swapping is safe). | |
| 49 | */ | |
| 42 | 50 | walkDistance: (from: Point, to: Point) => number; |
| 51 | /** The next point toward `to`; reads the same field, cached by `to`. */ | |
| 43 | 52 | pathStep: (from: Point, to: Point) => Point; |
| 44 | 53 | clearPaths: () => void; |
| 54 | /** How many walk fields have been built so far — for tests of the cache. */ | |
| 55 | fieldsBuilt: () => number; | |
| 45 | 56 | }; |
| 46 | 57 | |
| 47 | 58 | /** Closest approach of a segment to a point, as distance and travel along it. */ |
| @@ -132,11 +143,14 @@ export function makeGrid(lines: string[], tile: number): Grid { | ||
| 132 | 143 | // every few ticks; the field for a goal is built once and kept for the round. |
| 133 | 144 | const fields = new Map<number, Int32Array>(); |
| 134 | 145 | const clearPaths = (): void => void fields.clear(); |
| 146 | let built = 0; | |
| 147 | const fieldsBuilt = (): number => built; | |
| 135 | 148 | |
| 136 | 149 | function field(goalCol: number, goalRow: number): Int32Array { |
| 137 | 150 | const key = goalRow * cols + goalCol; |
| 138 | 151 | const cached = fields.get(key); |
| 139 | 152 | if (cached) return cached; |
| 153 | built++; | |
| 140 | 154 | const dist = new Int32Array(cols * rows).fill(-1); |
| 141 | 155 | if (!solidTile(goalCol, goalRow)) { |
| 142 | 156 | dist[key] = 0; |
| @@ -162,7 +176,12 @@ export function makeGrid(lines: string[], tile: number): Grid { | ||
| 162 | 176 | return dist; |
| 163 | 177 | } |
| 164 | 178 | |
| 165 | /** Walk distance in tiles between two points, or -1 when unreachable. */ | |
| 179 | /** | |
| 180 | * Walk distance in tiles between two points, or -1 when unreachable. The | |
| 181 | * field is built for `to` and read at `from`, so a caller asking about many | |
| 182 | * cells should pass them as `from` against one fixed `to`: one field, not | |
| 183 | * one per call. Distance is symmetric, so the swap is always safe. | |
| 184 | */ | |
| 166 | 185 | function walkDistance(from: Point, to: Point): number { |
| 167 | 186 | const f = tileOf(from.x, from.y); |
| 168 | 187 | const g = tileOf(to.x, to.y); |
| @@ -172,7 +191,8 @@ export function makeGrid(lines: string[], tile: number): Grid { | ||
| 172 | 191 | /** |
| 173 | 192 | * The next point to walk toward on the way to a goal: the centre of the |
| 174 | 193 | * neighbouring tile that is one step closer, so a walker rounds corners |
| 175 | * instead of grinding into them. | |
| 194 | * instead of grinding into them. Uses the field cached for `to`, like | |
| 195 | * `walkDistance`: many walkers to one goal cost one field. | |
| 176 | 196 | */ |
| 177 | 197 | function pathStep(from: Point, to: Point): Point { |
| 178 | 198 | const f = tileOf(from.x, from.y); |
| @@ -220,5 +240,6 @@ export function makeGrid(lines: string[], tile: number): Grid { | ||
| 220 | 240 | walkDistance, |
| 221 | 241 | pathStep, |
| 222 | 242 | clearPaths, |
| 243 | fieldsBuilt, | |
| 223 | 244 | }; |
| 224 | 245 | } |