balance-sim.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // 平衡模拟:用游戏真实的 tick / bestUpgrade / rebirth 跑一名“会点技能”的玩家。
  2. // 用法:node tests/balance-sim.mjs [小时数] [可选:玩家给自己设的层数上限 CAP,达到即回溯]
  3. // 输出:回溯样本、进入里世界的探索收益、每 30 分钟的行程、逃逸节点。
  4. import * as C from '/opt/data/git-repos/worldtree-escape/js/config.js';
  5. import * as F from '/opt/data/git-repos/worldtree-escape/js/formulas.js';
  6. import * as S from '/opt/data/git-repos/worldtree-escape/js/state.js';
  7. import * as R from '/opt/data/git-repos/worldtree-escape/js/rift.js';
  8. import * as FS from 'node:fs';
  9. function require2(s){ FS.appendFileSync('/opt/data/git-repos/worldtree-escape/.probe', [s.layer, s.maxLayerRound, s.maxLayerEver, s.chapterSeen, s.playSec, s.rebirths].join(',')+'\n'); }
  10. const H = +(process.argv[2] || 3);
  11. const CAP = +(process.argv[3] || 0);
  12. let seed = 7;
  13. Math.random = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };
  14. const s = S.newState();
  15. s.autoOn = true;
  16. s.autoEvery = +(process.env.AUTO || 120);
  17. const marks = [];
  18. const dt = 0.2;
  19. let upgT = 0, lastRift = -1e9, lastCapT = -1e9, capHits = 0;
  20. function buySmart() {
  21. for (let g = 0; g < 64; g++) {
  22. const b = F.bestUpgrade(s);
  23. if (!b) break;
  24. // 一次决策只推进一项(根须/树冠/兵种各一次点击);大额预算不在一帧内刷完
  25. if (b.kind === 'life') { if (!S.buyLife(s, b.id)) break; }
  26. else {
  27. const mult = C.piCostMul(F.piOf(s.layer || 0, F.F0(s)));
  28. const n = C.maxAffordable(b.kind, s.upgrades[b.kind] || 0, s.essence * 0.95 / mult);
  29. const cost = C.batchCost(b.kind, s.upgrades[b.kind] || 0, n) * mult;
  30. if (n <= 0 || cost > s.essence) break;
  31. s.essence -= cost; s.upgrades[b.kind] += n;
  32. }
  33. }
  34. const bp = F.bestPerm(s);
  35. if (bp && bp.cost <= s.crystals) { if (bp.kind === 'stem') S.buyTree(s); else S.buyRelic(s); }
  36. }
  37. function buyOnce(kind) {
  38. const cur = s.upgrades[kind] || 0;
  39. const mult = C.piCostMul(F.piOf(s.layer || 0, F.F0(s)));
  40. const n = C.maxAffordable(kind, cur, s.essence * 0.95 / mult);
  41. if (n <= 0) return;
  42. const cost = C.batchCost(kind, cur, n) * mult;
  43. if (cost > s.essence) return;
  44. s.essence -= cost; s.upgrades[kind] = cur + n;
  45. }
  46. function buyLifeOnce() {
  47. // 兵种按“收益/成本”挑一个可买的
  48. let best = null;
  49. for (const l of C.LIVES) { if (!F.lifeUnlocked(s, C.LIVES.indexOf(l))) continue; const c = F.lifeCost(s, l.id); if (c <= s.essence) { const v = l.eff.per / c; if (!best || v > best.v) best = { l, v }; } }
  50. if (best) S.buyLife(s, best.l.id);
  51. }
  52. function buyPermOnce() {
  53. for (let g = 0; g < 6; g++) { const bp = F.bestPerm(s); if (!bp || bp.cost > s.crystals) break; if (bp.kind === 'stem') S.buyTree(s); else S.buyRelic(s); }
  54. }
  55. function autoPlayRift() {
  56. for (let q = 3; q >= 0; q--) if (C.planeUnlocked(s, q)) { S.switchPlane(s, q); break; }
  57. const run = S.startRift(s);
  58. if (!run) return;
  59. R.autoSpend(run); R.buildDone(run, 1);
  60. let guard = 0;
  61. while (run.state !== 'result' && guard++ < 400) {
  62. if (run.state === 'fight') {
  63. const m = run.mouth, atk = R.atkOf(run);
  64. const crit = Math.ceil(m.threat * 1.75 - atk), win = Math.ceil(m.threat + 1 - atk);
  65. const roll = crit >= 0 && crit <= 5 ? crit : win >= 0 && win <= 5 ? win : 5;
  66. R.afterSlash(run, R.slash(run, roll));
  67. } else if (run.state === 'event') R.applyOption(run, 1);
  68. }
  69. const p = run.plane, before = s.planes[p].explore;
  70. S.endRift(s);
  71. marks.push(` · 里世界[${C.PLANES[p].name}] +${s.planes[p].explore - before} 探索 → ${s.planes.map((x) => x.explore | 0).join('/')}`);
  72. upgT = 0;
  73. }
  74. for (let t = 0; t < H * 3600; t += dt) {
  75. const o = S.tick(s, { run: s.planeRun }, dt);
  76. if (o.crisis) S.crisisRespond(s, 'relieve');
  77. for (const e of o.events) if (e.type === 'civ') S.civChoose(s, e.civIdx, 0);
  78. upgT -= dt;
  79. // 每 1 秒点一次升级:根须 / 树冠 / 兵种各最多一次(模拟真实玩家的点击节奏)
  80. if (upgT <= 0) { upgT = 1; buyOnce('root'); buyOnce('canopy'); buyLifeOnce(); buyPermOnce(); }
  81. if (CAP && s.layer >= CAP && !s.escaped && t - lastCapT > 30) {
  82. lastCapT = t; capHits++;
  83. if (capHits % 50 === 1) marks.push(` · 卡 ${CAP} 层 → 回溯 #${s.rebirths} t=${(t / 60) | 0}m +${F.rebirthEstimate(s)} 树D${s.treeLv}`);
  84. S.rebirth(s); continue;
  85. }
  86. if (s.escaped) { const L = s.layer, MR = s.maxLayerRound, T0 = t; if (!globalThis.__d) { globalThis.__d = 1; marks.push(`DBG escape t=${t.toFixed(1)} L=${L} MR=${MR} ch=${s.chapterSeen} reb=${s.rebirths} root=${s.upgrades.root}`); } S.rebirth(s); marks.push(`★ 逃逸完成 t=${(t / 60).toFixed(1)}m L=${L.toFixed(0)} mr=${MR.toFixed(0)} → F=${s.layer.toFixed(0)}(轮回 ${s.rebirths},树 D${s.treeLv})`); }
  87. if (o.auto) { const r = S.rebirth(s); if (s.rebirths % 10 === 0 || s.rebirths <= 4) marks.push(`回溯 #${s.rebirths} t=${(t / 60) | 0}m 本轮 T=${r.T.toFixed(0)}→F=${r.floor} +${r.gain} 树D${s.treeLv}`); }
  88. if (t > 300 && t - lastRift > 480 && !s.planeRun) { lastRift = t; autoPlayRift(); }
  89. if (t % 1800 < dt) {
  90. const st = F.computeStats(s);
  91. marks.push(`${(t / 60) | 0}m:本轮 L=${s.layer.toFixed(0)} (F=${st.start.toFixed(0)}) 产出=${st.prod.toExponential(1)} 分/层=${st.minutesPerLayer.toFixed(2)} 跳关=${st.skipP.toFixed(2)} 树=D${s.treeLv} 遗物=${s.relics.length} 结晶=${Math.floor(s.crystals)} 回溯=${s.rebirths} 逃逸=${s.escapeCount} 位面=${s.currentPlane}`);
  92. }
  93. }
  94. console.log(marks.join('\n'));
  95. console.log(`结束:layer=${s.layer.toFixed(0)} 逃逸=${s.escapeCount} 轮回=${s.rebirths} 树=D${s.treeLv} 探索=${s.planes.map((p) => p.explore | 0).join('/')}`);