formulas.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // 公式层:由存档状态组装运行时乘区,供 UI / Canvas / 测试复用(纯函数,无副作用)。
  2. // 与 GDD.md「数值表」逐条对应。署名:FmodeCode
  3. import * as C from './config.js';
  4. const nowSec = () => Date.now() / 1000;
  5. /** 本轮起始层 F */
  6. export function F0(state) { return C.earlyLayer(state.maxLayerRound || 0, C.PLANE_RULES[state.currentPlane || 0]); }
  7. /**
  8. * 推进基准(连续空间“能量谷包络”):前 GRACE 层 ×1;之后 gm = max( ENV_STEP^(D−GRACE)(封顶 ENV_CAP), (D/TOTAL_LAYERS)^GM_Q )
  9. * 这样全程难度单调且终点 gm ≤ ENV_CAP(不会出现“越走越快直接冲到终点”);D = L − F
  10. */
  11. export const GRACE = 15, ENV_STEP = 1.04, ENV_CAP = 50, GM_Q = 2, GM_END = 8;
  12. export function marchBase(layer, start) {
  13. const D = Math.max(0, layer - (start || 0));
  14. // 前 GRACE 层 ×1;gm = max(1, 指数包络 ENV_STEP^(D−GRACE)(封顶 ENV_CAP), 二次下界 (min(1, D/Gm))^GM_Q,Gm = TOTAL×(ENV_STEP−1)/(GM_END−1))
  15. const Gm = C.TOTAL_LAYERS * (ENV_STEP - 1) / (GM_END - 1); // 标记:Gm 由 TOTAL 与曲线参数共同决定 // 见 config.TOTAL_LAYERS 注释
  16. // 指数包络只在 D < Gm 内生效(越走越快的“前段”);之后线性增长到终点 ×(GM_END−1)
  17. if (D >= Gm) return Math.min(ENV_CAP, 1 + (GM_END - 1) * Math.min(1, D / (0.8 * C.TOTAL_LAYERS)));
  18. return Math.max(1, Math.min(ENV_CAP, Math.pow(ENV_STEP, D - GRACE)), Math.pow(Math.max(1, D) / Gm, GM_Q));
  19. }
  20. /** 兵种技能、已解锁兵种数、种族羁绊 */
  21. export function lifeSkill(state) {
  22. const lv = state.lives || {};
  23. let prod = 1, spd = 1, skip = 0, crystal = 1, total = 0, awake = 1;
  24. const byRace = { 生命: 0, 星穹: 0, 虚空: 0 };
  25. for (const l of C.LIVES) {
  26. const n = lv[l.id] || 0;
  27. if (!n) continue;
  28. byRace[l.race]++; total += n;
  29. const k = 1 + l.eff.per * n;
  30. if (l.eff.kind === 'prod') prod *= k;
  31. else if (l.eff.kind === 'speed') spd *= k;
  32. else if (l.eff.kind === 'skip') skip += l.eff.per * n;
  33. else if (l.eff.kind === 'crystal') crystal *= k;
  34. else if (l.eff.kind === 'awake') awake += l.eff.per * n;
  35. }
  36. const stacks = {};
  37. for (const r of Object.keys(C.RACES)) stacks[r] = C.raceStacks(r, byRace[r]);
  38. return {
  39. prod, spd, skip, crystal, awake, total, byRace, stacks,
  40. bondProd: Math.max(1e-6, Math.pow(C.RACE_BOND_MULT, stacks.生命)),
  41. bondCrystal: Math.max(1e-6, Math.pow(C.RACE_BOND_MULT, stacks.星穹)),
  42. bondSkip: Math.min(2, C.RACE_SKIP_BONUS * stacks.虚空),
  43. };
  44. }
  45. /** 跳关触发率 p(封顶 60%) */
  46. export function skipProbOf(state, sk) {
  47. const rule = C.PLANE_RULES[state.currentPlane || 0];
  48. const l = sk || lifeSkill(state);
  49. return Math.min(C.SKIP.cap - 1e-6, C.canopySkip(state.upgrades?.canopy || 0) + l.skip + l.bondSkip + (rule.skipMult - 1));
  50. }
  51. /** 炼化乘区 M */
  52. export function rebirthMOf(state, sk) {
  53. const p = state.currentPlane ?? 0;
  54. const l = sk || lifeSkill(state);
  55. const tier = C.exploreTierOf(state.planes?.[p]?.explore || 0);
  56. const skipNote = 1 + Math.min(C.REBIRTH.skipNoteCap, 0.5 * skipProbOf(state, l)) * l.crystal;
  57. const insight = 1 + (state.exploreSum || 0) / 100;
  58. return Math.max(1e-6, l.crystal * l.bondCrystal * tier.crystal * skipNote * insight * C.freedomMul(state) * C.resonanceMul(state));
  59. }
  60. /** 推进速度乘区 M_spd(不含危机) */
  61. export function speedMulOf(state) {
  62. const l = lifeSkill(state);
  63. return C.treeSpeedMul(state.treeLv || 0) * C.rootSpeedMul(state.upgrades?.root || 0) * C.canopySpeedMul(state.upgrades?.canopy || 0) * l.spd * l.awake * C.PLANE_RULES[state.currentPlane || 0].speedMult;
  64. }
  65. /**
  66. * 全部乘区与速率(GDD §6)。
  67. * 产出/秒 = essenceBase(L, P_root) × M_prod
  68. * 单层耗时 = (4+10L) × gm / M_spd;跳关后期望 = / (1+E[k])
  69. */
  70. export function computeStats(state) {
  71. const p = state.currentPlane ?? 0;
  72. const rule = C.PLANE_RULES[p];
  73. const L = state.layer || 0;
  74. const start = F0(state);
  75. const now = nowSec();
  76. const sk = lifeSkill(state);
  77. const frozen = !!(state.planeRun && state.planeRun.active);
  78. const explore = state.planes?.[p]?.explore || 0;
  79. const tier = C.exploreTierOf(explore);
  80. const cr = state.crisis || {};
  81. const crisisBuff = (cr.buffUntil || 0) > now;
  82. const crisisPenalty = (cr.penaltyUntil || 0) > now;
  83. const crisisMult = crisisBuff ? 1 + C.CRISIS.reward : crisisPenalty ? 1 - C.CRISIS.penalty : 1;
  84. const narrSig = state.magic?.sigUntil > now ? Math.min(C.NARRATE_STACK_CAP, state.magic.sig || 0) : 0;
  85. const narrate = Math.pow(rule.narrateBoost || 1, narrSig);
  86. const mults = {
  87. tree: C.treeProdMul(state.treeLv || 0),
  88. blood: C.multRebirth(state.rebirths || 0),
  89. explore: C.multExploration(explore),
  90. creature: C.multCreature(sk.total),
  91. bond: sk.bondProd,
  92. relic: C.relicMul(state.relics?.length || 0),
  93. freedom: C.freedomMul(state),
  94. plane: rule.prodMult,
  95. crisis: crisisMult,
  96. narrate,
  97. };
  98. const Mprod = Object.values(mults).reduce((a, b) => a * b, 1);
  99. const rootP = C.rootProdMul(state.upgrades?.root || 0);
  100. const base = C.essenceBase(L, rootP);
  101. // 兜底:从“零层起步”(新存档 / 离线后回到初始轮回)时,至少给 本源产出 1/秒,避免 F=L 导致产出恒为 0
  102. const prodRaw = (L - start < 1e-9 && L < 1) ? 1 : base * Mprod;
  103. const gm = marchBase(L, start);
  104. const Mspd = speedMulOf(state) * (crisisPenalty ? 1 - C.CRISIS.penalty : 1);
  105. const layerSec = C.visitTime(L) * gm / Mspd;
  106. const layersPerSec = Mspd / (C.visitTime(L) * gm);
  107. const skipP = skipProbOf(state, sk);
  108. const skipExp = C.skipExpectation(skipP);
  109. const timeMul = 1 + skipExp;
  110. const rebirthM = rebirthMOf(state, sk);
  111. const reward = rebirthEstimate(state, rebirthM);
  112. return {
  113. plane: p, rule, start, base, rootP, prodMul: Mprod, mults, frozen,
  114. prodRaw, prod: frozen ? 0 : prodRaw,
  115. creatureLv: sk.total, skills: sk, tier, explore,
  116. gm, speedMul: Mspd, layerSec, layersPerSec, minutesPerLayer: (layerSec * timeMul) / 60,
  117. skipP, skipExp, timeMul,
  118. rebirthM, reward, rebirthCost: C.rebirthCost(state.maxLayerRound || 0),
  119. crisisBuff, crisisPenalty, crisisMult, narrate, narrSig, anchored: explore >= C.EXPLORE_FULL,
  120. };
  121. }
  122. /** 单次炼化可得结晶(里世界中主世界冻结:返回 0)。state.rebirth() 使用同一函数:UI 预期 = 实得 */
  123. export function rebirthEstimate(state, M) {
  124. if (state.planeRun && state.planeRun.state !== 'result') return 0;
  125. const p = state.currentPlane ?? 0;
  126. const base = C.rebirthReward({
  127. maxLayer: state.maxLayerRound || 0,
  128. exploreSum: state.exploreSum || 0,
  129. incomeMul: M ?? rebirthMOf(state),
  130. ruleMod: C.PLANE_REBIRTH_MOD[p] || 1,
  131. });
  132. return Math.max(1, Math.floor(base * Math.pow(C.REBIRTH.decay, state.round?.refines || 0)));
  133. }
  134. export function upgradeCost(state, kind) { return Math.ceil(C.costOf(kind, state.upgrades?.[kind] || 0)); }
  135. export const piOf = C.piOf;
  136. export function lifeCost(state, lifeId, atLevel) {
  137. const idx = C.LIVES.findIndex((l) => l.id === lifeId);
  138. const n = atLevel ?? (state.lives?.[lifeId] || 0);
  139. return Math.ceil(C.lifeCostOf(C.lifeBase(idx), n + 1, piOf(state.layer || 0, F0(state))));
  140. }
  141. export function lifeUnlocked(state, idx) { return idx === 0 || (state.lives?.[C.LIVES[idx - 1].id] || 0) >= 3; }
  142. export function permCost(state, kind) {
  143. if (kind === 'stem' || kind === 'tree') return C.treeCost(state.treeLv || 0);
  144. if (kind === 'relic') return C.relicCost(state.relics?.length || 0);
  145. return Infinity;
  146. }
  147. export function rebirthFloor(state) { return C.earlyLayer(Math.max(state.maxLayerRound || 0, state.layer || 0), C.PLANE_RULES[state.currentPlane || 0]); }
  148. export function escapeProgress(state) { return Math.min(1, (state.layer || 0) / C.TOTAL_LAYERS); }
  149. /**
  150. * 本轮升级的“边际优化”决策(GDD §7.2):
  151. * 在 根须 / 树冠 / 每个已解锁兵种 之间,选择“单位成本乘区收益”最高且可在本轮阶段内(ETA×回归时间)赚回者。
  152. * @returns {object|null} { kind, id?, n? }
  153. */
  154. export function bestUpgrade(state, stats) {
  155. const s = state;
  156. const st = stats || computeStats(s);
  157. const income = Math.max(st.prod, 1e-9);
  158. const budget = s.essence;
  159. const cands = [];
  160. // 根须:买单级(批量策略由调用方按返回值逐级执行)
  161. const mult = C.piCostMul(piOf(s.layer || 0, st.start));
  162. const rCost = C.costOf('root', s.upgrades.root || 0) * mult;
  163. cands.push({ kind: 'root', n: 1, cost: rCost, v: Math.log(1 + C.COST.root.per) * 1.6 / rCost });
  164. // 树冠:跳关触发率 +0.5% → 时间乘区收益 log((1+E(p+δ))/(1+E(p)))(再做 1.0×0.5 权重:跳关不提升单层产出)
  165. const cCost = C.costOf('canopy', s.upgrades.canopy || 0) * mult; // 树冠不再随 π 涨价(跳关本身就是时间乘区)
  166. const p0 = st.skipP, d = C.COST.canopy.per;
  167. const dvSkip = p0 < C.SKIP.cap ? Math.log((1 + C.skipExpectation(p0 + d)) / (1 + st.skipExp)) * 0.5 : 0;
  168. const dvSpeed = Math.log(1.08) * 0.5;
  169. cands.push({ kind: 'canopy', n: 1, cost: cCost, v: (dvSkip + dvSpeed) / cCost });
  170. // 兵种:prod 类 ×1(产出乘区)、speed 类 ×0.5(进度乘区,产出随层数指数跟进)
  171. C.LIVES.forEach((l, i) => {
  172. if (!lifeUnlocked(s, i)) return;
  173. const cost = lifeCost(s, l.id);
  174. const w = l.eff.kind === 'prod' ? 1.0 : l.eff.kind === 'speed' ? 0.5 : l.eff.kind === 'crystal' ? 0.8 : l.eff.kind === 'awake' ? 0.45 : 0.6;
  175. cands.push({ kind: 'life', id: l.id, n: 1, cost, v: Math.log(1 + l.eff.per) * w / cost });
  176. });
  177. // 只买“等待时间”≤ ETA 秒的项(成本 ≤ ETA × 当前产出/秒);一次只返回一项最优
  178. const ok = cands.filter((c) => c.cost <= budget && c.cost <= income * (c.kind === 'life' ? C.LIFE_ETA : C.ETA)).sort((a, b) => b.v - a.v);
  179. return ok[0] || null;
  180. }
  181. /** 永久投资:世界树 / 遗物 的下一项(按 收益/成本 最高) */
  182. export function bestPerm(state, stats) {
  183. const s = state;
  184. const st = stats || computeStats(s);
  185. const o = [];
  186. const tc = C.treeCost(s.treeLv || 0);
  187. // 世界树:产出 ×(1+0.05) 且 速度 ×(1+0.08) → 加权=生产速度×产出 → 用 0.5 权重估计速度对层的影响
  188. const tv = Math.log(1 + C.TREE.prodPer) + 0.5 * Math.log(1 + C.TREE.speedPer);
  189. o.push({ kind: 'stem', cost: tc, v: tv / tc });
  190. if ((s.relics || []).length < 8) { const rc = C.relicCost(s.relics.length); o.push({ kind: 'relic', cost: rc, v: Math.log(1.25) / rc }); }
  191. return o.sort((a, b) => b.v - a.v)[0];
  192. }