formulas.js 9.2 KB

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