Claude Claude Opus 5 commited on
Commit
cdae5f9
Β·
unverified Β·
1 Parent(s): 492ec35

Tier 3b: rank formation slots per cluster, not globally

Browse files

DESIGN QUESTION: should range-layering rank per cluster or globally?

PER CLUSTER. formationSlots deliberately gives each spatial cluster its
own sub-formation β€” "spaced by each cluster's own footprint so
sub-formations can't overlap" β€” and clusterUnits promises it "preserves
each unit's relative position". rankSlotsByRange then re-paired GLOBALLY:
it sorted ALL followers by weapon range against ALL slots by forwardness,
with no cluster awareness in either half. Two armies far apart therefore
cross-swapped sub-formations and each walked the extra distance to the
other's slots. The resulting shape was still correct, which is why this
reads as pathing jank rather than as two features disagreeing. Ranking per
cluster honours both at once: each cluster keeps its members, and each is
internally range-layered, which is the entire point of the layering.

Two things the work corrected in me rather than in the code:

Clustering the FOLLOWERS was wrong β€” excluding the leader first splits its
own cluster-mates off into singletons, which re-creates the very
cross-cluster swap being fixed. The whole selection is clustered, exactly
as formationSlots does, and the leader's slot is held out of its group's
pool since spots[0] passes through untouched by design.

My first test was also wrong, and wrong in a way worth recording: it
compared each unit's DESTINATION against the ORIGIN centroids, but the
whole formation moves to one point, so every destination is far from both
origins and the assertion measured nothing real. The invariant that
actually expresses the decision is that ranking may PERMUTE slots within a
cluster β€” that is its job β€” but must never move a unit into another
cluster's set. Verified by mutation: restoring the global sort fails it.

Suite: 2006 pass, 0 fail. typecheck clean. determinism and
determinism-roster green (this path is player-only; the AI and grid paths
return before reaching it).

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

Files changed (2) hide show
  1. engine/commands.js +30 -5
  2. test/formation.test.js +36 -0
engine/commands.js CHANGED
@@ -9,7 +9,7 @@
9
  import { BUILDINGS, UNITS, canAfford, payCost, prereqsMet, canGatherType, canLogisticsType, canBuildCategory } from "./entities.js";
10
  import { canPlaceBuilding } from "./colliders.js";
11
  import { makeBuilding } from "./state.js";
12
- import { formationSlots, resolveHeading } from "./formation.js";
13
  import { FREIGHTER_AI_TECH, LOGI_PRIORITIES } from "./haul.js";
14
  import { canRecycle, beginRecycle, cancelRecycle } from "./recycle.js";
15
 
@@ -88,17 +88,42 @@ function groupSpeedCap(units) {
88
  function rankSlotsByRange(units, spots, x, y, formation) {
89
  const leaderSpot = spots[0];
90
  const heading = resolveHeading(units, x, y, formation);
91
- const byRange = units.slice(1).sort((a, b) => {
 
 
 
 
 
 
 
 
 
 
 
 
92
  const ra = UNITS[a.type]?.range ?? Infinity, rb = UNITS[b.type]?.range ?? Infinity;
93
  return ra !== rb ? ra - rb : (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
94
  });
95
- const byForwardness = spots.slice(1).sort((s1, s2) => {
96
  const f1 = (s1.x - leaderSpot.x) * heading.x + (s1.y - leaderSpot.y) * heading.y;
97
  const f2 = (s2.x - leaderSpot.x) * heading.x + (s2.y - leaderSpot.y) * heading.y;
98
  return f2 - f1;
99
  });
100
- const spotFor = new Map(byRange.map((u, i) => [u, byForwardness[i]]));
101
- return [leaderSpot, ...units.slice(1).map(u => spotFor.get(u))];
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  }
103
 
104
  // The shared machinery behind issueMove/issueAttackMove/issueHoldFormation: lay the group out
 
9
  import { BUILDINGS, UNITS, canAfford, payCost, prereqsMet, canGatherType, canLogisticsType, canBuildCategory } from "./entities.js";
10
  import { canPlaceBuilding } from "./colliders.js";
11
  import { makeBuilding } from "./state.js";
12
+ import { formationSlots, resolveHeading, clusterUnits } from "./formation.js";
13
  import { FREIGHTER_AI_TECH, LOGI_PRIORITIES } from "./haul.js";
14
  import { canRecycle, beginRecycle, cancelRecycle } from "./recycle.js";
15
 
 
88
  function rankSlotsByRange(units, spots, x, y, formation) {
89
  const leaderSpot = spots[0];
90
  const heading = resolveHeading(units, x, y, formation);
91
+ const followers = units.slice(1);
92
+ const slotOf = new Map(units.map((u, i) => [u, spots[i]]));
93
+
94
+ // Rank WITHIN each spatial cluster, not across the whole selection. formationSlots deliberately
95
+ // gives each cluster its own sub-formation β€” "spaced by each cluster's own footprint so
96
+ // sub-formations can't overlap" β€” and clusterUnits promises it "preserves each unit's relative
97
+ // position". A single global sort of all followers by range against all slots by forwardness had
98
+ // no cluster awareness in either half, so two armies far apart cross-swapped sub-formations and
99
+ // each walked the extra distance to the other's slots: the resulting shape was still correct, but
100
+ // units marched through each other to reach it, which reads as pathing jank rather than as two
101
+ // features disagreeing. Ranking per cluster honours both β€” each keeps its members, and each is
102
+ // internally range-layered, which is the whole point of the layering.
103
+ const byRange = us => us.slice().sort((a, b) => {
104
  const ra = UNITS[a.type]?.range ?? Infinity, rb = UNITS[b.type]?.range ?? Infinity;
105
  return ra !== rb ? ra - rb : (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
106
  });
107
+ const byForwardness = ss => ss.slice().sort((s1, s2) => {
108
  const f1 = (s1.x - leaderSpot.x) * heading.x + (s1.y - leaderSpot.y) * heading.y;
109
  const f2 = (s2.x - leaderSpot.x) * heading.x + (s2.y - leaderSpot.y) * heading.y;
110
  return f2 - f1;
111
  });
112
+
113
+ // Cluster the WHOLE selection, exactly as formationSlots does β€” not just the followers. Excluding
114
+ // the leader first splits its own cluster-mates off into singletons, which re-creates the very
115
+ // cross-cluster swap this is fixing. The leader's slot is then held out of its group's pool,
116
+ // since spots[0] passes through untouched by design.
117
+ const leader = units[0];
118
+ const spotFor = new Map();
119
+ for (const group of clusterUnits(units)) {
120
+ const members = group.filter(u => u !== leader);
121
+ if (!members.length) continue;
122
+ const ranked = byRange(members);
123
+ const ordered = byForwardness(members.map(u => slotOf.get(u)));
124
+ ranked.forEach((u, i) => spotFor.set(u, ordered[i]));
125
+ }
126
+ return [leaderSpot, ...followers.map(u => spotFor.get(u))];
127
  }
128
 
129
  // The shared machinery behind issueMove/issueAttackMove/issueHoldFormation: lay the group out
test/formation.test.js CHANGED
@@ -676,3 +676,39 @@ test("a follow-leader order (and squadLeader/squadFollowers) never survives save
676
  assert.equal(rLeader.squadFollowers, undefined);
677
  assert.equal(rFollower.squadLeader, undefined);
678
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
676
  assert.equal(rLeader.squadFollowers, undefined);
677
  assert.equal(rFollower.squadLeader, undefined);
678
  });
679
+
680
+ test("range-layered ranks stay INSIDE each nested cluster (T3)", () => {
681
+ // formationSlots deliberately gives each spatial cluster its own sub-formation β€” "spaced by each
682
+ // cluster's own footprint so sub-formations can't overlap" β€” and clusterUnits promises it
683
+ // "preserves each unit's relative position". rankSlotsByRange then re-paired GLOBALLY: it sorted
684
+ // ALL followers by weapon range against ALL slots by forwardness, with no cluster awareness in
685
+ // either half, so two armies far apart cross-swapped sub-formations and each walked the extra
686
+ // distance to the other's slots. The shape was still right; the units marched through each other
687
+ // to reach it, which reads as pathing jank rather than as two features disagreeing.
688
+ //
689
+ // DECISION: rank PER CLUSTER. The invariant that expresses it exactly β€” ranking may PERMUTE the
690
+ // slots within a cluster (that is its job) but must never move a unit into another cluster's set.
691
+ const a1 = fakePlayerUnit("a1", "skiff", 100, 100); // cluster A
692
+ const a2 = fakePlayerUnit("a2", "breacher", 130, 100);
693
+ const b1 = fakePlayerUnit("b1", "skiff", 900, 100); // cluster B, well past CLUSTER_RADIUS
694
+ const b2 = fakePlayerUnit("b2", "bastion", 930, 100);
695
+ const units = [a1, a2, b1, b2];
696
+
697
+ // The unranked layout, straight from formationSlots β€” this is what the clustering intends.
698
+ const baseSlots = formationSlots(units, 500, 900, { shape: "wedge" });
699
+ const key = p => `${Math.round(p.x)},${Math.round(p.y)}`;
700
+ const clusterA = new Set([key(baseSlots[0]), key(baseSlots[1])]);
701
+ const clusterB = new Set([key(baseSlots[2]), key(baseSlots[3])]);
702
+ assert.equal(clusterA.size + clusterB.size, 4, "fixture sanity: four distinct slots");
703
+
704
+ issueMove(units, 500, 900, false, { shape: "wedge" });
705
+ const dest = u => (u.order.type === "follow-leader"
706
+ ? { x: a1.order.x + u.order.offsetX, y: a1.order.y + u.order.offsetY }
707
+ : { x: u.order.x, y: u.order.y });
708
+
709
+ for (const [u, own, otherName] of [[a2, clusterA, "B"], [b1, clusterB, "A"], [b2, clusterB, "A"]]) {
710
+ assert.ok(own.has(key(dest(u))),
711
+ `${u.id} was ranked into cluster ${otherName}'s sub-formation β€” ranking may reorder slots ` +
712
+ `within a cluster, never move a unit across one`);
713
+ }
714
+ });