feat: rebuild the orbital vault as an operational map

This commit is contained in:
2026-11-09 22:01:14 +07:00
parent 2129e80c38
commit fba4a1e628
25 changed files with 1342 additions and 1333 deletions

View File

@@ -1,137 +1,38 @@
import type { Vec2 } from "./galaxyLayout";
export type Rect = { x: number; y: number; width: number; height: number };
type Label = { id: string; x: number; y: number; width: number };
export const LABEL_HEIGHT = 24;
const GAP = 4;
export type LabelPlacement = {
x: number;
y: number;
};
const CHAR_W = 6.4;
const LABEL_H = 14;
const LABEL_PAD = 6;
function labelWidth(name: string): number {
const n = name.length > 16 ? 15 : name.length;
return n * CHAR_W;
function overlaps(a: Rect, b: Rect) {
return a.x < b.x + b.width + GAP && a.x + a.width + GAP > b.x &&
a.y < b.y + b.height + GAP && a.y + a.height + GAP > b.y;
}
function dist(a: LabelPlacement, b: LabelPlacement): number {
return Math.hypot(a.x - b.x, a.y - b.y);
}
/** Radial outward labels with simple bbox separation (lock mode). */
export function layoutLockLabels(
starPositions: Vec2[],
names: string[],
starR: number,
): LabelPlacement[] {
const vaultPad = 108;
const labels: LabelPlacement[] = starPositions.map((p, i) => {
const ang = Math.atan2(p.y, p.x);
const base = starR + 28;
return {
x: p.x + Math.cos(ang) * base,
y: p.y + Math.sin(ang) * base,
};
});
for (let iter = 0; iter < 48; iter++) {
for (let i = 0; i < labels.length; i++) {
const p = starPositions[i];
const r = dist(labels[i], { x: 0, y: 0 });
if (r < vaultPad) {
const ang = Math.atan2(labels[i].y, labels[i].x);
labels[i].x = Math.cos(ang) * vaultPad;
labels[i].y = Math.sin(ang) * vaultPad;
}
for (let j = i + 1; j < labels.length; j++) {
const wi = labelWidth(names[i]);
const wj = labelWidth(names[j]);
const dx = labels[j].x - labels[i].x;
const dy = labels[j].y - labels[i].y;
const needX = (wi + wj) / 2 + LABEL_PAD;
const needY = LABEL_H + LABEL_PAD;
if (Math.abs(dx) < needX && Math.abs(dy) < needY) {
const push = (needX - Math.abs(dx) + needY - Math.abs(dy)) * 0.35;
const ang = Math.atan2(dy, dx) || 0;
labels[i].x -= Math.cos(ang) * push;
labels[i].y -= Math.sin(ang) * push;
labels[j].x += Math.cos(ang) * push;
labels[j].y += Math.sin(ang) * push;
}
}
const outward = Math.atan2(
labels[i].y - starPositions[i].y,
labels[i].x - starPositions[i].x,
);
const minDist = starR + 26;
const dStar = dist(labels[i], starPositions[i]);
if (dStar < minDist) {
labels[i].x = starPositions[i].x + Math.cos(outward) * minDist;
labels[i].y = starPositions[i].y + Math.sin(outward) * minDist;
export function placeLabels(
candidates: Label[],
width: number,
height: number,
obstacles: Rect[],
): Label[] {
const occupied = [...obstacles];
const placed: Label[] = [];
for (const label of candidates) {
const { x, y, width: w } = label;
const offsets = [12, 28, 44].flatMap((gap) => [
[gap, -LABEL_HEIGHT / 2], [-w - gap, -LABEL_HEIGHT / 2],
[-w / 2, -LABEL_HEIGHT - gap], [-w / 2, gap],
[gap, -LABEL_HEIGHT - gap / 2], [-w - gap, gap / 2],
]);
for (const [dx, dy] of offsets) {
const rect = { x: x + dx, y: y + dy, width: w, height: LABEL_HEIGHT };
if (rect.x < GAP || rect.y < GAP || rect.x + w > width - GAP ||
rect.y + LABEL_HEIGHT > height - GAP || occupied.some((other) => overlaps(rect, other))) {
continue;
}
occupied.push(rect);
placed.push({ id: label.id, ...rect });
break;
}
}
const yMin = -160;
const yMax = 108;
const xMin = -230;
const xMax = 230;
for (let i = 0; i < labels.length; i++) {
const star = starPositions[i];
const hitsChrome =
labels[i].y < yMin ||
labels[i].y > yMax ||
labels[i].x < xMin ||
labels[i].x > xMax;
const hitsNeighbor = starPositions.some(
(other, k) => k !== i && dist(labels[i], other) < starR + 20,
);
if (hitsChrome || hitsNeighbor) {
const ang = Math.atan2(star.y, star.x);
labels[i] = {
x: star.x - Math.cos(ang) * (starR + 34),
y: star.y - Math.sin(ang) * (starR + 34),
};
}
labels[i].x = Math.max(xMin, Math.min(xMax, labels[i].x));
labels[i].y = Math.max(yMin, Math.min(yMax, labels[i].y));
const gap = dist(labels[i], star);
if (gap < starR + 16) {
const ang = Math.atan2(labels[i].y - star.y, labels[i].x - star.x) || 0;
labels[i].x = star.x + Math.cos(ang) * (starR + 18);
labels[i].y = Math.max(
yMin,
Math.min(yMax, star.y + Math.sin(ang) * (starR + 18)),
);
}
}
for (let iter = 0; iter < 24; iter++) {
for (let i = 0; i < labels.length; i++) {
for (let j = i + 1; j < labels.length; j++) {
const wi = labelWidth(names[i]);
const wj = labelWidth(names[j]);
const dx = labels[j].x - labels[i].x;
const dy = labels[j].y - labels[i].y;
const needX = (wi + wj) / 2 + 8;
const needY = LABEL_H + 6;
if (Math.abs(dx) < needX && Math.abs(dy) < needY) {
const push = (needX - Math.abs(dx)) * 0.55 + 1;
const dir = dx === 0 ? (i % 2 === 0 ? -1 : 1) : Math.sign(dx);
labels[i].x -= dir * push;
labels[j].x += dir * push;
}
}
labels[i].x = Math.max(xMin, Math.min(xMax, labels[i].x));
labels[i].y = Math.max(yMin, Math.min(yMax, labels[i].y));
}
}
return labels;
}
export function shortenOrbitName(name: string): string {
return name.length > 16 ? `${name.slice(0, 14)}…` : name;
return placed;
}