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

walkDistance: say which argument is cached, and guard the slow order (#24 ite… #39

Merged opened this in answer to

shipped production is serving it deploy b42aa0d

opened this

walkDistance and pathStep in src/engine/grid.ts now say that the walk field is cached by to. Callers should keep to fixed and vary from. Distance is symmetric on this grid, so swapping the arguments never changes the answer. There is no gameplay change: the only code added is a build counter.

Changes

  • Docs: the module header, the Grid type, and the doc comments on walkDistance and pathStep now explain the argument order.
  • Counter: fieldsBuilt() is added to Grid. It counts how many walk fields have been built and exists only so tests can see the cache.
  • New src/engine/grid.test.ts:

- Asking every floor cell's distance to one target builds 1 field. - Varying the target builds one field per target. - pathStep reuses the field that walkDistance built for the same goal. - Distance is the same in both directions for every pair of cells, which is why the swap is safe. - clearPaths() forces the next request to build again.

  • Proof the test catches it: I moved the cache key to from by hand and three tests failed, then put it back. The full bun test run passes (310 tests). There is no tsconfig.json, so there was no type check to run.

Val's own callers (none needed a change)

  • src/bots.ts:112: walkDistance(bot, loose) already varies from (the bot) against a fixed to (the dropped spike).
  • src/bots.ts:122: pathStep(bot, goal) has to fixed per goal. One exception: once the spike is planted, attacking bots pick a new random goal within ±110 units of the spike every tick. That does vary to, but with 80-unit tiles it can only land on a few tiles around the spike, well under the 64-field limit, so the cache doesn't thrash. I left it alone because the fix would be to keep each bot's jitter steady, which changes how bots behave. That's out of scope here and could be its own issue.
  • Inside grid.ts, pathStep calls walkDistance(from, to) with the same to, so it reads the same field.

Preview: there is nothing new to see in it, because this is engine documentation and a test. To check the work, run bun test src/engine/grid.test.ts.

🤖 Generated with Claude Code

Closes #38

merged this
Sign in to join this conversation.

↑↓ navigate ↵ open esc close

Keyboard shortcuts