formulas.js 11 KB

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