Claude Claude Sonnet 5 commited on
Commit
1702aab
·
unverified ·
1 Parent(s): 86eb7aa

Sim core: fix stale balance comment, reuse queryNeighbors' scratch buffer

Browse files

- combat.js's comment claimed Bastion's range was 24; it's 44 (entities.js
bumped it, with its own comment admitting so nearby) - just a stale number.
- grid.js's queryNeighbors allocated a fresh result array on every call
(~3x per unit per tick across movement/separation/combat), in a file whose
own header states a no-per-tick-garbage goal. Now reuses one scratch array.
Verified every call site (movement.js, repair.js, separation.js, combat.js)
reads the result via an immediate, synchronous, non-nested for-of and
discards it before the next call - no caller holds a reference past that or
triggers a nested queryNeighbors call while iterating, so sharing the
buffer is safe. combat/sim/grid/separation/movement/determinism tests all
still pass unmodified.
- sim.js's tick() now guards against a missing UNITS[unit.type] def - belt
and suspenders; persist.js already strips unknown-type entities on load,
so this covers only a hypothetical future code path that bypasses it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dmxcvm178TEt2wVXrX5Cj1

Files changed (3) hide show
  1. engine/combat.js +1 -1
  2. engine/grid.js +14 -3
  3. engine/sim.js +6 -0
engine/combat.js CHANGED
@@ -90,7 +90,7 @@ export function updateCombat(state, unit, dt) {
90
  }
91
 
92
  // Only genuinely ranged units kite (Lancer 55 / Breacher 150 / Dreadnought 68);
93
- // short-range brawlers (Skiff 40, Bastion 24) stand and trade.
94
  const KITE_MIN_RANGE = 50;
95
 
96
  // Stutter-step kiting: while a ranged unit is reloading and an enemy UNIT has
 
90
  }
91
 
92
  // Only genuinely ranged units kite (Lancer 55 / Breacher 150 / Dreadnought 68);
93
+ // short-range brawlers (Skiff 40, Bastion 44) stand and trade.
94
  const KITE_MIN_RANGE = 50;
95
 
96
  // Stutter-step kiting: while a ranged unit is reloading and an enemy UNIT has
engine/grid.js CHANGED
@@ -47,10 +47,21 @@ export function buildUnitGrid(state) {
47
  return { cell: CELL, buckets };
48
  }
49
 
 
 
 
 
 
 
 
 
 
50
  // Candidate units in every cell overlapping the (radius, +1 ring of padding)
51
  // box around (x, y). A superset of the units within `radius` — callers filter
52
  // by exact distance. Cells are visited in a fixed numeric order and bucket
53
  // contents keep Map insertion order, so iteration is fully deterministic.
 
 
54
  /** @param {*} grid @param {number} x @param {number} y @param {number} radius @returns {(Unit)[]} */
55
  export function queryNeighbors(grid, x, y, radius) {
56
  const cell = grid.cell;
@@ -58,12 +69,12 @@ export function queryNeighbors(grid, x, y, radius) {
58
  const maxcx = Math.floor((x + radius) / cell) + 1;
59
  const mincy = Math.floor((y - radius) / cell) - 1;
60
  const maxcy = Math.floor((y + radius) / cell) + 1;
61
- const out = [];
62
  for (let cy = mincy; cy <= maxcy; cy++) {
63
  for (let cx = mincx; cx <= maxcx; cx++) {
64
  const arr = grid.buckets.get(cellKey(cx, cy));
65
- if (arr) for (const u of arr) out.push(u);
66
  }
67
  }
68
- return out;
69
  }
 
47
  return { cell: CELL, buckets };
48
  }
49
 
50
+ // Reused across calls instead of allocating a fresh result array every query:
51
+ // every caller (repair.js, movement.js, separation.js, combat.js) reads the
52
+ // returned array in an immediate for-of and discards it before making its next
53
+ // move — none stores it past that, and none makes a second queryNeighbors call
54
+ // while a prior result is still being iterated (all call sites are synchronous,
55
+ // single-threaded, read-then-discard) — so one shared scratch buffer is safe and
56
+ // matches this file's stated no-per-tick-garbage goal (see header comment).
57
+ const _scratch = [];
58
+
59
  // Candidate units in every cell overlapping the (radius, +1 ring of padding)
60
  // box around (x, y). A superset of the units within `radius` — callers filter
61
  // by exact distance. Cells are visited in a fixed numeric order and bucket
62
  // contents keep Map insertion order, so iteration is fully deterministic.
63
+ // Returns a reused scratch array (see _scratch above) — read it immediately
64
+ // and don't hold onto it past that; the next queryNeighbors call overwrites it.
65
  /** @param {*} grid @param {number} x @param {number} y @param {number} radius @returns {(Unit)[]} */
66
  export function queryNeighbors(grid, x, y, radius) {
67
  const cell = grid.cell;
 
69
  const maxcx = Math.floor((x + radius) / cell) + 1;
70
  const mincy = Math.floor((y - radius) / cell) - 1;
71
  const maxcy = Math.floor((y + radius) / cell) + 1;
72
+ _scratch.length = 0;
73
  for (let cy = mincy; cy <= maxcy; cy++) {
74
  for (let cx = mincx; cx <= maxcx; cx++) {
75
  const arr = grid.buckets.get(cellKey(cx, cy));
76
+ if (arr) for (const u of arr) _scratch.push(u);
77
  }
78
  }
79
+ return _scratch;
80
  }
engine/sim.js CHANGED
@@ -131,6 +131,12 @@ function updateUnit(state, unit, dt) {
131
  }
132
 
133
  const def = UNITS[unit.type];
 
 
 
 
 
 
134
  if (def.role === "combat") { updateCombat(state, unit, dt); return; }
135
  if (def.role === "support") { updateSupport(state, unit, def, dt); return; }
136
 
 
131
  }
132
 
133
  const def = UNITS[unit.type];
134
+ // Belt-and-suspenders: persist.js's cleanEntity already strips any unit with an
135
+ // unknown type before it reaches state.units on load (test/save-hardening.test.js),
136
+ // so def is always populated today. This just guards a future entity source that
137
+ // might bypass that path, matching the same "if (!def) skip" style repair.js uses
138
+ // for mender defs above.
139
+ if (!def) return;
140
  if (def.role === "combat") { updateCombat(state, unit, dt); return; }
141
  if (def.role === "support") { updateSupport(state, unit, def, dt); return; }
142