Files
jinzuanwork/portal/assets/universe/layout.js
T
ywsandClaude Opus 5.5 74bf490baa 全量细审修复:部署漏配防清空、scrypt 并发闸、发布冲突可解、价格文件只在改了才写、排位扩容
- cms.py:没设 YS_PORTAL_DIR / YS_ORDER_DIR 或缺 content/site.json 时拒绝工作(不建空的第一版,免得一发布清空网站);
  同时最多 2 个 scrypt(内存上限 256M);登录冷却记录定期清理;价格 / 付款只在这次发布真改了才写回
  (不覆盖 admin.py price 的改动)
- 后台页面:发布冲突二选一(丢掉我的草稿 / 确认后覆盖发布);草稿没存上、还有图在上传时不让发布
- layout.js:中圈排满后分档到外圈空槽之间找位置,40 条世界线也不重叠;去掉慢的 Math.hypot
- config.js:后台把世界线删光也照实显示;门户删掉旧启动仪式留下的死数据;版本号 v=3
- 测试:内容后台 78 / 回归 27 / 数据库 38 全过;端到端浏览器 36 全过
- 文档同步(交接文档、内容后台设计「实现对照」、后端交接说明、交付清单)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 21:55:13 +00:00

151 lines
8.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* =============================================================================
* 金钻宇宙 · 星系图排布(layout.js)—— 纯计算,不依赖 three.js,可以单独测试
* -----------------------------------------------------------------------------
* 决定每条世界线的星球放在哪、补给线从哪连过来。规则(2026-10-05):
*
* 1. 在 LOOKS 里写了 orbit: [离核心多远, 第几条旋臂] 的 —— 就放在那儿,补给线从核心连过去。(现在的 0000–0004)
* 2. 空槽(empty: true)—— 按**编号**放在外圈的固定槽位(从 GALAXY.firstSlot 号起,轮流分到各条旋臂)。
* 只看自己的编号,点亮其中一个,别的空槽不会挪。空槽的淡虚线接到离它最近、又不穿过别的星的那颗已点亮的星
* (没有就接核心)—— 这条淡线会随着新点亮的星「找到更近的落脚点」,空槽本身不动。
* 3. 已点亮、但没写 orbit 的(内容后台里「点亮一条」新加的)—— **自动排位**:
* · 在中圈的旋臂上挑一个位置:跟核心、已有星系(连同它的卫星轨道)都留足距离,尽量让各条旋臂均匀;
* · 补给线连到「比它更靠近核心、又离它最近」的那颗星(没有合适的就连核心),
* 而且这条线不能从别的星系中间穿过;
* · 按编号从小到大一颗一颗排,每一颗只看排在它前面的 —— 所以**新加一条,已有的星和线都不动**。
* · 中圈排满(二十来条以后)才会去外圈空槽之间找位置;那一档要避开空槽,所以改「空槽数」可能让这几颗挪一挪。
*
* 用法:import { layoutSystems } from './layout.js';
* const L = layoutSystems(window.JZ_UNIVERSE) → [{ u, look, x, z, kind, parent }]
* kind = 'pinned' | 'slot' | 'auto';parent = 补给线起点的编号,null = 核心
* ============================================================================= */
export const armAngle = (r, arm, G) => arm * Math.PI * 2 / G.arms + r * G.twist;
/** 一个星系「占的地方」半径:星球本身 + 最外面那颗卫星的轨道 */
export function footprint(look) {
const moons = (look.moons || []).map(m => (m.d || 0) + (m.r || 0));
return (look.r || 1) * 2.2 + (moons.length ? Math.max(...moons) : 0);
}
const dist = (a, b) => { const dx = a.x - b.x, dz = a.z - b.z; return Math.sqrt(dx * dx + dz * dz); }; // 别用 Math.hypot:V8 里慢好几倍
/** 点到线段的距离(补给线按直线近似;实际画的是稍微拱起来的弧,水平投影还是这条线) */
function segDist(p, a, b) {
const vx = b.x - a.x, vz = b.z - a.z, L2 = vx * vx + vz * vz || 1;
const t = Math.max(0, Math.min(1, ((p.x - a.x) * vx + (p.z - a.z) * vz) / L2));
const dx = p.x - (a.x + vx * t), dz = p.z - (a.z + vz * t);
return Math.sqrt(dx * dx + dz * dz);
}
export function layoutSystems(CFG) {
const G = CFG.GALAXY, R = G.radius, ARMS = G.arms, first = G.firstSlot ?? 5;
const LOOKS = CFG.LOOKS || {};
const lookOf = u => Object.assign({}, CFG.EMPTY_LOOK, LOOKS[u.no] || {});
const num = u => parseInt(u.no, 10) || 0;
const list = [...CFG.UNIVERSES].sort((a, b) => num(a) - num(b));
const CORE = { x: 0, z: 0, fp: 12, no: null };
const out = new Map();
const put = (u, look, r, a, kind, parent) => {
const s = { u, look, x: Math.cos(a) * r, z: Math.sin(a) * r, r, kind, parent, fp: footprint(look), no: u.no };
out.set(u.no, s); return s;
};
// 1) 写死位置的
for (const u of list) {
const o = (LOOKS[u.no] || {}).orbit;
if (!o) continue;
const [r, arm] = o;
put(u, lookOf(u), r, r ? armAngle(r, arm, G) : 0, 'pinned', null);
}
// 2) 空槽:按编号固定在外圈
for (const u of list) {
if (!u.empty || out.has(u.no)) continue;
const e = Math.max(0, num(u) - first);
const r = R * (0.8 + 0.12 * (e % 2));
put(u, lookOf(u), r, armAngle(r, e % ARMS, G) + 0.32 * Math.floor(e / ARMS), 'slot', null);
}
// 3) 自动排位:只避开「核心 + 写死的 + 排在前面的自动星」(空槽在外圈,自动星只用中圈,互不影响)
const placed = [...out.values()].filter(s => s.kind === 'pinned' && s.r > 0);
const armCount = new Array(ARMS).fill(0);
for (const s of placed) {
const a = Math.atan2(s.z, s.x);
let best = 0, bd = Infinity;
for (let k = 0; k < ARMS; k++) { const d = Math.abs(Math.atan2(Math.sin(a - armAngle(s.r, k, G)), Math.cos(a - armAngle(s.r, k, G)))); if (d < bd) { bd = d; best = k; } }
armCount[best]++;
}
const rMin = 16, rMax = R * 0.68, mid = R * 0.5; // 自动星只用中圈:外圈(0.8R 往外)留给空槽,两圈之间留出空隙
// 给一个候选位置挑补给线起点:比它更靠近核心的星(或核心),线不能穿过别的星系
function parentFor(c, others) {
const cands = [CORE, ...others.filter(o => o.r < c.r - 4)];
let best = null;
for (const p of cands) {
const len = dist(p, c);
let clear = Infinity;
for (const o of others) { if (o === p) continue; clear = Math.min(clear, segDist(o, p, c) - o.fp); }
if (clear < 1.5) continue;
// 同样不穿的情况下挑短的;连核心稍微加一点「距离」,让新星更愿意接在附近的星上,连线成网而不是全扎向中心
const score = len + (p === CORE ? 6 : 0);
if (!best || score < best.score) best = { p, score, clear };
}
return best;
}
// 候选位置:按「够不够宽敞」先粗筛打分(便宜),再只给排在前面的几十个检查补给线会不会穿星(贵)—— 40 条世界线也很快。
// 中圈排满了就分档放宽:到外圈空槽之间找位置(这时也要避开空槽)、间距要求逐步降低;最后兜底也保证不叠在别人身上。
const slots = [...out.values()].filter(s => s.kind === 'slot');
const PASSES = [
{ rMax: R * 0.68, need: 4, offs: [-0.2, -0.1, 0, 0.1, 0.2], slots: false },
{ rMax: R * 0.96, need: 3, offs: [-0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3], slots: true },
{ rMax: R * 0.98, need: 1.2, offs: [-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4], slots: true },
];
function place(fp, pass) {
const others = pass.slots ? placed.concat(slots) : placed, cands = [];
for (let arm = 0; arm < ARMS; arm++) {
for (let r = rMin; r <= pass.rMax; r += 1.25) {
for (const off of pass.offs) {
const a = armAngle(r, arm, G) + off;
const c = { x: Math.cos(a) * r, z: Math.sin(a) * r, r };
let clear = r - CORE.fp - fp;
for (const o of others) clear = Math.min(clear, dist(o, c) - o.fp - fp);
if (clear < pass.need) continue;
// 分数:够宽敞(封顶,不为了宽敞跑到边上)、靠近中圈、所在旋臂人少、在旋臂正中更好
cands.push({ c, a, r, arm, clear, score: Math.min(clear, 12) - 0.08 * Math.abs(r - mid) - 2.2 * armCount[arm] - 3 * Math.abs(off) });
}
}
}
cands.sort((x, y) => y.score - x.score || x.r - y.r || x.a - y.a);
let best = null;
for (const k of cands.slice(0, 60)) {
const par = parentFor(k.c, placed);
if (!par) continue;
const s = k.score + Math.min(par.clear, 6) * 0.3;
if (!best || s > best.s + 1e-9) best = { s, k, parent: par.p };
}
if (best) return best;
return cands.length ? { k: cands[0], parent: CORE } : null; // 线怎么都会擦到别的星:就近连核心,至少星不叠
}
for (const u of list) {
if (u.empty || out.has(u.no)) continue;
const look = lookOf(u), fp = footprint(look);
let got = null;
for (const pass of PASSES) { got = place(fp, pass); if (got) break; }
if (!got) { // 整张图都满了(几十条以后):退到最外圈,按编号错开
const e = Math.max(0, num(u) - first), r = R * 1.02 + 2 * Math.floor(e / ARMS);
placed.push(put(u, look, r, armAngle(r, e % ARMS, G) + 0.16, 'auto', null));
continue;
}
const s = put(u, look, got.k.r, got.k.a, 'auto', got.parent.no);
armCount[got.k.arm]++;
placed.push(s);
}
// 4) 空槽的淡线:接到最近的、不穿星的已点亮星(或核心)
const litNow = [...out.values()].filter(s => !s.u.empty && s.r > 0);
for (const s of out.values()) {
if (s.kind !== 'slot') continue;
const par = parentFor(s, litNow);
s.parent = par && par.p !== CORE ? par.p.no : null;
}
return list.map(u => out.get(u.no));
}