formulas.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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) {
  7. return C.earlyLayer(state.maxLayerRound || 0, C.PLANE_RULES[state.currentPlane || 0]);
  8. }
  9. /** 推进基准(米魔卡点):前 15 层 ×1;之后每深入 1 层,单层推进时间 ×1.063(封顶 ×15) */
  10. export const GM_GRACE = 15;
  11. export const GM_EPS = 0.01;
  12. export function marchBase(layer, start) {
  13. // 推进基准 gm(“米魔”卡点):D = L − F
  14. // gm = D^1.5(能量谷包络):推进时间 ∝ √D,即层数 ∝ 推进量的 2/3 次方——每突破一格都需要速度乘区跟上
  15. // 跳关把推进量按时间乘区放大:层数对时间乘区的幂次 = 1.5(≈ 任务书“逃逸进度 ^1.5”)
  16. const D = Math.max(0, layer - (start || 0));
  17. return Math.pow(Math.max(1, D), 1.5);
  18. }
  19. /** 兵种技能与“已解锁兵种数”(用于羁绊 / 炼化乘区)——供 stats 与 UI 复用 */
  20. export function lifeSkill(state) {
  21. const lv = state.lives || {};
  22. let prod = 1, speed = 1, skip = 0, crystal = 1;
  23. const byRace = { 生命: 0, 星穹: 0, 虚空: 0 };
  24. let total = 0;
  25. for (const l of C.LIVES) {
  26. const n = lv[l.id] || 0;
  27. if (!n) continue;
  28. byRace[l.race]++;
  29. total += n;
  30. const strength = l.eff.per * n;
  31. if (l.eff.kind === 'prod') prod *= 1 + strength;
  32. else if (l.eff.kind === 'speed') speed *= 1 + strength;
  33. else if (l.eff.kind === 'skip') skip += strength;
  34. else if (l.eff.kind === 'crystal') crystal *= 1 + strength;
  35. }
  36. const stacks = {};
  37. for (const r of Object.keys(C.RACES)) stacks[r] = C.raceStacks(r, byRace[r]);
  38. return {
  39. prod, speed, skip, crystal, byRace, total, stacks,
  40. bondProd: Math.pow(C.RACE_BOND_MULT, stacks.生命),
  41. bondCrystal: Math.pow(C.RACE_BOND_MULT, stacks.星穹),
  42. bondSkip: Math.min(2, C.RACE_SKIP_BONUS * stacks.虚空),
  43. };
  44. }
  45. /** 跳关触发率(全部可加来源直接相加,封顶 60%) */
  46. export function skipProbOf(state, sk) {
  47. const rule = C.PLANE_RULES[state.currentPlane || 0];
  48. const l = sk || lifeSkill(state);
  49. const raw = C.canopySkip(state.upgrades?.canopy || 0) + l.skip + l.bondSkip + (rule.skipMult - 1);
  50. return Math.min(C.SKIP.cap, raw);
  51. }
  52. /** 炼化乘区 M(兵种技能 × 星穹羁绊 × 探索阶梯 × 跳关加成 × 自由轮回) */
  53. export function rebirthMOf(state, sk) {
  54. const p = state.currentPlane ?? 0;
  55. const l = sk || lifeSkill(state);
  56. const tier = C.exploreTierOf(state.planes?.[p]?.explore || 0);
  57. const skipNote = 1 + Math.min(C.REBIRTH.skipNoteCap, skipProbOf(state, l) * 0.5);
  58. // 见闻:所有位面的探索度之和参与炼化乘区(可回头刷低阶位面补强主世界)
  59. const insight = 1 + (state.exploreSum || 0) / 100;
  60. return l.crystal * l.bondCrystal * tier.crystal * skipNote * insight * C.freedomMul(state) * C.resonanceMul(state);
  61. }
  62. /** 速度乘区 M_spd(不含危机、不含卡点) */
  63. export function speedMulOf(state) {
  64. const p = state.currentPlane ?? 0;
  65. const l = lifeSkill(state);
  66. return C.treeSpeedMul(state.treeLv || 0) * C.rootSpeedMul(state.upgrades?.root || 0)
  67. * l.speed * C.PLANE_RULES[p].speedMult;
  68. }
  69. /**
  70. * 全部乘区与速率(GDD §6)。
  71. * 本源产出/秒(本轮)= Σ_{k<L−F} 1.05^k × P_root(R) × M_prod
  72. * 单层耗时 T(L) = (4+10L) × gm / M_spd;跳关后期望层耗时 = T / (1 + E[k])
  73. */
  74. export function computeStats(state) {
  75. const p = state.currentPlane ?? 0;
  76. const rule = C.PLANE_RULES[p];
  77. const layer = state.layer || 0;
  78. const start = F0(state);
  79. const now = nowSec();
  80. const sk = lifeSkill(state);
  81. const frozen = !!state.planeRun?.active;
  82. const explore = state.planes?.[p]?.explore || 0;
  83. const tier = C.exploreTierOf(explore);
  84. const cr = state.crisis || {};
  85. const crisisBuff = (cr.buffUntil || 0) > now;
  86. const crisisPenalty = (cr.penaltyUntil || 0) > now;
  87. const crisisMult = crisisBuff ? 1 + C.CRISIS.reward : crisisPenalty ? 1 - C.CRISIS.penalty : 1;
  88. const narrSig = state.magic?.sigUntil > now ? state.magic.sig || 0 : 0;
  89. const narrate = Math.pow(rule.narrateBoost || 1, Math.min(C.NARRATE_STACK_CAP, narrSig));
  90. // ── 产出乘区(独立相乘)
  91. const mults = {
  92. tree: C.treeProdMul(state.treeLv || 0),
  93. blood: C.multRebirth(state.rebirths || 0),
  94. explore: C.multExploration(explore),
  95. creature: C.multCreature(sk.total),
  96. bond: sk.bondProd,
  97. relic: C.relicMul(state.relics?.length || 0),
  98. freedom: C.freedomMul(state),
  99. plane: rule.prodMult,
  100. crisis: crisisMult,
  101. narrate,
  102. };
  103. const prodMul = Object.values(mults).reduce((a, b) => a * b, 1);
  104. const rootP = C.rootProdMul(state.upgrades?.root || 0);
  105. const rel = Math.max(0, layer - start);
  106. const base = rel > 0 ? (Math.pow(C.ESSENCE_GROWTH, rel) - 1) / Math.log(C.ESSENCE_GROWTH) : 0;
  107. const prodRaw = base * rootP * prodMul;
  108. // ── 推进速度
  109. const gm = marchBase(layer, start);
  110. const speedMul = speedMulOf(state) * (crisisPenalty ? 1 - C.CRISIS.penalty : 1);
  111. const layerSec = (C.visitTime(layer) * gm) / speedMul;
  112. const layersPerSec = speedMul / (C.visitTime(layer) * gm);
  113. const skipP = skipProbOf(state, sk);
  114. const skipExp = C.skipExpectation(skipP);
  115. const timeMul = 1 + skipExp;
  116. const rebirthM = rebirthMOf(state, sk);
  117. const reward = rebirthEstimate(state, rebirthM);
  118. return {
  119. plane: p, rule, start, rel, base, rootP, prodMul, mults, frozen,
  120. prodRaw, prod: frozen ? 0 : prodRaw,
  121. creatureLv: sk.total, skills: sk, tier, explore,
  122. gm, speedMul, layerSec, layersPerSec, minutesPerLayer: (layerSec * timeMul) / 60,
  123. skipP, skipExp, timeMul,
  124. rebirthM, reward, rebirthCost: C.rebirthCost(state.maxLayerRound || 0),
  125. crisisBuff, crisisPenalty, crisisMult, narrate, narrSig, anchored: explore >= C.EXPLORE_FULL,
  126. };
  127. }
  128. /** 单次炼化可得结晶(含衰减 / 自由轮回叠加)。state.rebirth() 使用同一函数:UI 预期 = 实得 */
  129. export function rebirthEstimate(state, M) {
  130. const p = state.currentPlane ?? 0;
  131. const mm = M ?? rebirthMOf(state);
  132. const base = C.rebirthReward({
  133. maxLayer: state.maxLayerRound || 0,
  134. exploreSum: state.exploreSum || 0,
  135. incomeMul: mm,
  136. ruleMod: C.PLANE_REBIRTH_MOD[p] || 1,
  137. });
  138. if (state.planeRun && state.planeRun.state !== 'result') return 0; // 里世界中主世界冻结:不可回溯
  139. return Math.max(1, base);
  140. }
  141. /** 本轮升级成本 / 批量成本 */
  142. export function upgradeCost(state, kind) {
  143. const rule = C.PLANE_RULES[state.currentPlane || 0];
  144. return Math.ceil(C.costOf(kind, state.upgrades?.[kind] || 0) * rule.costMult);
  145. }
  146. export function upgradeBatchCost(state, kind, n) {
  147. const rule = C.PLANE_RULES[state.currentPlane || 0];
  148. return Math.ceil(C.batchCost(kind, state.upgrades?.[kind] || 0, n) * rule.costMult);
  149. }
  150. /** 兵种升级成本(下一级) */
  151. export function lifeCost(state, lifeId, atLevel) {
  152. const def = C.LIVES.find((l) => l.id === lifeId);
  153. const n = atLevel ?? (state.lives?.[lifeId] || 0);
  154. const rule = C.PLANE_RULES[state.currentPlane || 0];
  155. return Math.ceil(C.lifeCostOf(def.cost, n + 1) * rule.costMult);
  156. }
  157. export function lifeUnlocked(state, idx) {
  158. if (idx === 0) return true;
  159. return (state.lives?.[C.LIVES[idx - 1].id] || 0) >= 3;
  160. }
  161. /** 永久投资成本(时间结晶) */
  162. export function permCost(state, kind) {
  163. if (kind === 'stem' || kind === 'tree') return C.treeCost(state.treeLv || 0);
  164. if (kind === 'relic') return C.relicCost(state.relics?.length || 0);
  165. return Infinity;
  166. }
  167. /** 转生后起始层 */
  168. export function rebirthFloor(state) {
  169. return C.earlyLayer(Math.max(state.maxLayerRound || 0, state.layer || 0), C.PLANE_RULES[state.currentPlane || 0]);
  170. }
  171. /** 主线逃逸进度(0~1):连续空间里“抵达的层数 / 1000” */
  172. export function escapeProgress(state) { return Math.min(1, (state.layer || 0) / C.TOTAL_LAYERS); }