1
0
Fork 0

fix(npc-dialogue): bus-route return edges and spread detour lanes

Two routing defects on the dialogue canvas:

- Return edges entering the same handle detoured through identical
  corners, stacking into one thick bundle on the left of the target.
  Each edge now derives a stable lane offset from its id, so parallel
  detours run in visibly separate lanes.
- Long leftward runs relied on greedy corner-hopping alone, which could
  still leave a segment cutting under a card. Back edges now try
  structured bus routes (corridor above or below every card, then swing
  in to the entry handle) and score candidates by residual crossings,
  picking the first clean shape. The router also re-runs until clean
  (bounded to three passes) instead of trusting one greedy pass.

Verified on doc #2: all seven edges route with zero card crossings and
no stacked lanes; tech-tree edge-routing specs still pass.
This commit is contained in:
Jason Fraley 2026-10-06 14:38:42 -04:00
parent 3c70fa70d8
commit 28bb08f43e
2 changed files with 100 additions and 36 deletions

View file

@ -19,6 +19,7 @@ import {
buildRoutePath,
polylineMidpoint,
routeAroundObstacles,
ROUTE_CLEARANCE,
segmentCrossesRect,
type RoutePoint,
type RouteRect,
@ -27,6 +28,20 @@ import {
/** How far the line leaves a handle before it may bend (px). */
const STUB = 24;
/**
* Detour lanes come in three depths so edges that enter the same handle (or
* loop around the same card) don't collapse onto one identical lane and read
* as a single thick bundle. The depth is derived from the edge id, so it is
* stable across renders and differs between edges with overwhelming
* probability.
*/
const LANE_STEPS = [0, 16, 32];
function laneFor(id: string): number {
let h = 0;
for (let i = 0; i < id.length; i += 1) h = (h * 31 + id.charCodeAt(i)) | 0;
return LANE_STEPS[Math.abs(h) % LANE_STEPS.length];
}
function stubFor(point: RoutePoint, position: Position | null, length: number): RoutePoint {
switch (position) {
case Position.Left:
@ -76,36 +91,65 @@ export function RoutedFlowEdge({
const start: RoutePoint = { x: sourceX, y: sourceY };
const end: RoutePoint = { x: targetX, y: targetY };
const sStub = stubFor(start, sourcePosition, STUB);
const tStub = stubFor(end, targetPosition, STUB);
const isBackEdge = end.x < start.x;
const obstacles = isBackEdge ? allObstacles : forwardObstacles;
const lane = laneFor(id ?? "");
const margin = ROUTE_CLEARANCE + lane;
const base: RoutePoint[] = (() => {
if (isBackEdge) {
// Stub out of each handle, then a direct run the router bends around
// every card in between.
return [start, stubFor(start, sourcePosition, STUB), stubFor(end, targetPosition, STUB), end];
}
const sStub = stubFor(start, sourcePosition, STUB);
const tStub = stubFor(end, targetPosition, STUB);
const midX = (sStub.x + tStub.x) / 2;
const orthogonal: RoutePoint[] = [
start,
sStub,
{ x: midX, y: sStub.y },
{ x: midX, y: tStub.y },
tStub,
end,
];
const crossesCard = (points: RoutePoint[]) =>
points.some((p, i) =>
i < points.length - 1 ? forwardObstacles.some((r) => segmentCrossesRect(p, points[i + 1], r)) : false,
const routed = (() => {
const crossingCount = (points: RoutePoint[]) => {
let n = 0;
for (let i = 0; i < points.length - 1; i += 1) {
if (obstacles.some((r) => segmentCrossesRect(points[i], points[i + 1], r))) n += 1;
}
return n;
};
const candidates: RoutePoint[][] = [];
if (isBackEdge && obstacles.length > 0) {
// Structured "bus routes" for return edges: leave the source, run along
// a corridor above or below every card, then swing in to the target's
// entry handle. Greedy detouring alone thrashes on long leftward runs,
// so try the structured shapes first and let the router clean up.
const topY = Math.min(...obstacles.map((r) => r.y)) - ROUTE_CLEARANCE - lane;
const botY = Math.max(...obstacles.map((r) => r.y + r.height)) + ROUTE_CLEARANCE + lane;
const bus = (y: number): RoutePoint[] => [
start,
sStub,
{ x: sStub.x, y },
{ x: tStub.x, y },
tStub,
end,
];
candidates.push(bus(topY), bus(botY), [start, sStub, tStub, end]);
} else if (isBackEdge) {
candidates.push([start, sStub, tStub, end]);
} else {
// Forward edges prefer the tidy Z-step; a stubbed straight line is the
// fallback when the step would slice through cards.
const midX = (sStub.x + tStub.x) / 2;
candidates.push(
[start, sStub, { x: midX, y: sStub.y }, { x: midX, y: tStub.y }, tStub, end],
[start, sStub, tStub, end],
);
// The Z-shaped step looks best, but when it slices through cards a plain
// stubbed straight line routes cleaner after detouring.
return crossesCard(orthogonal) ? [start, sStub, tStub, end] : orthogonal;
}
let best: RoutePoint[] | null = null;
let bestScore = Number.POSITIVE_INFINITY;
for (const candidate of candidates) {
const cleaned = routeAroundObstacles(candidate, obstacles, margin);
const score = crossingCount(cleaned);
if (score < bestScore) {
best = cleaned;
bestScore = score;
}
if (score === 0) break;
}
return best ?? [start, end];
})();
const obstacles = isBackEdge ? allObstacles : forwardObstacles;
const routed = routeAroundObstacles(base, obstacles);
const path = buildRoutePath(routed);
const mid = polylineMidpoint(routed.length >= 2 ? routed : [start, end]);

View file

@ -26,7 +26,7 @@ export type RouteRect = {
/** Clearance kept around a node when routing around it. */
export const ROUTE_CLEARANCE = 22;
const MAX_DEPTH = 3;
const MAX_DEPTH = 4;
const EPS = 1;
/** Does segment a->b cross the interior of rect r? (exported for tests) */
@ -125,16 +125,16 @@ function detour(
];
}
/**
* Route a polyline around the given obstacle boxes. Returns a new point list;
* returns the input unchanged when there is nothing to avoid.
*/
export function routeAroundObstacles(
points: RoutePoint[],
obstacles: RouteRect[],
margin = ROUTE_CLEARANCE,
): RoutePoint[] {
if (points.length < 2 || obstacles.length === 0) return points;
/** Does any segment of the polyline cut one of the obstacles? */
function hasCrossing(points: RoutePoint[], obstacles: RouteRect[]): boolean {
for (let i = 0; i < points.length - 1; i += 1) {
if (obstacles.some((r) => crosses(points[i], points[i + 1], r))) return true;
}
return false;
}
/** One greedy pass: detour every base segment around the boxes it hits. */
function routePass(points: RoutePoint[], obstacles: RouteRect[], margin: number): RoutePoint[] {
const out: RoutePoint[] = [points[0]];
for (let i = 0; i < points.length - 1; i += 1) {
for (const p of detour(points[i], points[i + 1], obstacles, margin, 0)) {
@ -145,6 +145,26 @@ export function routeAroundObstacles(
return out;
}
/**
* Route a polyline around the given obstacle boxes. Returns a new point list;
* returns the input unchanged when there is nothing to avoid. A single greedy
* pass can leave a residual crossing when a chosen corner's continuation
* hits yet another box, so the result is re-routed until clean (bounded).
*/
export function routeAroundObstacles(
points: RoutePoint[],
obstacles: RouteRect[],
margin = ROUTE_CLEARANCE,
): RoutePoint[] {
if (points.length < 2 || obstacles.length === 0) return points;
let routed = points;
for (let pass = 0; pass < 3; pass += 1) {
routed = routePass(routed, obstacles, margin);
if (!hasCrossing(routed, obstacles)) break;
}
return routed;
}
// ---------------------------------------------------------------------------
// Path building
// ---------------------------------------------------------------------------