balance.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // 平衡模拟:用游戏真实的状态层 / 公式跑一名“中等勤快”的玩家(每 0.8 秒点一次世界树,按轮换决定买什么),
  2. // 输出每颗星的复苏时刻、纪元节奏、根系进度。断言:地球 6–14 分钟复苏;6 小时内九颗星全部复苏;纪元门槛在完成地球后才打开。
  3. // 用法:node tests/balance.mjs [--verbose]
  4. import * as S from '../js/game/state.js';
  5. import * as F from '../js/core/formulas.js';
  6. import * as B from '../js/core/balance.js';
  7. import { PLANETS, threatOf } from '../js/data/planets.js';
  8. const DT = 1, TAP = 0.8, SESSION = 600;
  9. let seed = 20261115;
  10. const realRandom = Math.random;
  11. const rng = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };
  12. /** 地球引导期结束后进入的“正常循环”:一颗星从 0 打到复苏 */
  13. export function run({ maxSec = 6 * 3600, verbose = false, tapEvery = 0.8 } = {}) {
  14. Math.random = rng;
  15. const s = S.newState();
  16. s.tutorial.step = 99;
  17. s.scene.next = 1e12; // 单独验证“改造节奏”:随机事件不参与
  18. // 地球新手期(故事 + 引导约 10 分钟):玩家此刻的状态
  19. s.rootLv = 4; s.lives[0] = { moss: 10, firefly: 4 }; // 新手前 8 分钟:根系 4 级、两个生命
  20. s.planets[0].rem = 86; s.res = 0; s.planets[0].explore = 0;
  21. // 地球前半段是引导章节:时间从故事开场开始算
  22. const log = [];
  23. const clears = { '-1': 600 }; // 开场故事 + 分步引导约 10 分钟,之后才是模拟起点
  24. let reached = 0;
  25. let now = S.nowSec(), tapAcc = 0, chapterStart = 0;
  26. for (let el = 1; el <= maxSec; el++) {
  27. now += DT; tapAcc += DT;
  28. for (const ev of S.tick(s, now).world) {
  29. if (ev.type === 'planet-done') { clears[ev.planet] = el; reached++; log.push({ el, ev }); if (verbose) console.log(` ✔ ${PLANETS[ev.planet].name} 复苏 @ ${(el / 60).toFixed(1)}min root=${s.rootLv} lives=${JSON.stringify(s.lives[ev.planet])}`); }
  30. }
  31. if (tapAcc >= tapEvery) { tapAcc = 0; S.tap(s, now); }
  32. if (process.env.DBG && el % 30 === 0) console.log(el, 'cost', S.rootCostNow(s), 'p', s.curPlanet, 'rem', s.planets[s.curPlanet].rem.toFixed(1), 'res', s.res.toFixed(0), 'root', s.rootLv, JSON.stringify(s.lives[s.curPlanet]), 'expl', s.planets[s.curPlanet].explore, 'prod', F.prodOf(s, now).toFixed(1), 'gain', F.tapOf(s, now).toFixed(1), 'rate', F.growRateOf(s, s.curPlanet).toFixed(4), 'goal', F.currentGoal(s).key);
  33. const i = s.curPlanet, p = s.planets[i];
  34. if (p.rem <= 0) {
  35. // 先扎根:根系(及星系共振)是突破下一颗星球价格墙的关键
  36. const targetRoot = Math.min(B.ROOT.max, PLANETS[Math.min(PLANETS.length - 1, i + 1)].unlockLv + 10);
  37. const rc0 = S.rootCostNow(s);
  38. if (s.rootLv < targetRoot && s.res >= rc0) { S.buyRoot(s, 1); continue; }
  39. let moved = false;
  40. // 下一颗星:按顺序进入(等级不够时先扎根;本源先不花,留到新星球)
  41. if (i + 1 < PLANETS.length) {
  42. if (s.rootLv >= PLANETS[i + 1].unlockLv) { S.enterPlanet(s, i + 1); chapterStart = el; moved = true; }
  43. } else if (i === PLANETS.length - 1) { s.planets[i].rem = 100; }
  44. void moved;
  45. // 最后一颗星或暂时无法前进:保持在当前星球继续(根须继续生长,等待门槛)
  46. if (!moved && s.planets[i].rem <= 0 && s.curPlanet === PLANETS.length - 1) s.planets[i].rem = 100;
  47. continue;
  48. }
  49. // 勘探:进入一章 8 分钟后(或已到 55%)开始勘探,勘探次数按挑战需求推算出“中位玩家”的次数
  50. const need = threatOf(i).need;
  51. const needN = need.kind === 'rootLv' ? 4 : need.kind === 'life' ? 5 : need.n;
  52. const explTarget = Math.min(B.STAR.exploreMax, Math.ceil(needN * 3));
  53. const wantsExplore = (100 - p.rem >= 55 || el - chapterStart >= 600 || (el - chapterStart >= 90 && p.rem > 90)) && el - chapterStart >= 30 && p.explore < explTarget;
  54. // 买生命(从最新的开始)——先把本源花在生命上,剩下的才用于勘探
  55. let bought = false;
  56. for (const l of F.availableLives(s, i).slice().reverse()) {
  57. if (s.res >= S.lifeCostNow(s, l.id)) { S.buyLife(s, l.id); bought = true; break; }
  58. }
  59. if (!bought && wantsExplore && s.res >= S.exploreCostNow(s, i)) { S.explore(s); continue; }
  60. // 根系:为下一颗星的等级门槛升级(不足 15 级内时优先)
  61. const targetRoot = Math.min(B.ROOT.max, PLANETS[Math.min(PLANETS.length - 1, i + 1)].unlockLv + 10);
  62. if (!bought && s.rootLv < targetRoot && s.res >= S.rootCostNow(s) && (100 - p.rem >= 40)) { S.buyRoot(s, 1); continue; }
  63. }
  64. Math.random = realRandom;
  65. return { s, clears, log, reached };
  66. }
  67. const fmtMin = (sec) => (sec / 60).toFixed(1) + 'min';
  68. if (import.meta.url === `file://${process.argv[1]}`) {
  69. const verbose = process.argv.includes('--verbose');
  70. const r = run({ verbose });
  71. const c = r.clears;
  72. // 第二遍:休息型玩家(每 5 秒点一次,但升级与勘探决策相同)
  73. const r2 = run({ verbose: false, tapEvery: 5 });
  74. console.log(` 休息型玩家(每 5 秒点一次):${r2.reached}/9 颗星,用时 ${(Math.max(0, ...Object.values(r2.clears)) / 60).toFixed(0)} 分钟`);
  75. for (const k of Object.keys(r2.clears)) c[k] = c[k] ?? r2.clears[k];
  76. console.log('── 平衡模拟(每步 1 游戏秒,中等勤快玩家)──');
  77. for (const i of [0, 1, 2, 3, 4, 5, 6, 7, 8]) {
  78. if (c[i] === undefined) console.log(` ${PLANETS[i].name.padEnd(5)} 未复苏`);
  79. else console.log(` ${PLANETS[i].name.padEnd(5)} 复苏 @ ${fmtMin(c[i])}`);
  80. }
  81. console.log(`最终:根系 ${r.s.rootLv} 级,星辰共鸣 ×${F.resonanceMul(r.s).toFixed(2)}`);
  82. const earthOk = c[0] !== undefined && c[0] <= 20 * 60;
  83. const allOk = [0, 1, 2, 3, 4, 5, 6, 7, 8].every((i) => c[i] !== undefined && c[i] <= 6 * 3600);
  84. console.log(earthOk ? '✔ 从开场到地球复苏 ≤ 20 分钟(含 10 分钟引导)' : '✘ 地球耗时过长');
  85. console.log(allOk ? '✔ 六小时内九颗星全部复苏' : '✘ 六小时内未能全部复苏');
  86. if (!earthOk || !allOk) process.exitCode = 1;
  87. }