| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- // 公式层:由存档状态组装运行时乘区,供 UI / Canvas / 测试复用(纯函数,无副作用)。
- // 与 GDD.md「数值表」逐条对应。署名:FmodeCode
- import * as C from './config.js';
- const nowSec = () => Date.now() / 1000;
- /** 本轮起始层 F */
- export function F0(state) { return C.earlyLayer(state.maxLayerRound || 0, C.PLANE_RULES[state.currentPlane || 0]); }
- /**
- * 推进基准(连续空间“能量谷包络”):前 GRACE 层 ×1;之后 gm = max( ENV_STEP^(D−GRACE)(封顶 ENV_CAP), (D/TOTAL_LAYERS)^GM_Q )
- * 这样全程难度单调且终点 gm ≤ ENV_CAP(不会出现“越走越快直接冲到终点”);D = L − F
- */
- export const GRACE = 15, ENV_STEP = 1.04, ENV_CAP = 50, GM_Q = 2, GM_END = 8;
- export function marchBase(layer, start) {
- const D = Math.max(0, layer - (start || 0));
- // 前 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))
- const Gm = C.TOTAL_LAYERS * (ENV_STEP - 1) / (GM_END - 1); // 见 config.TOTAL_LAYERS 注释
- // 指数包络只在 D < Gm 内生效(越走越快的“前段”);之后线性增长到终点 ×(GM_END−1)
- if (D >= Gm) return Math.min(ENV_CAP, 1 + (GM_END - 1) * Math.min(1, D / (0.8 * C.TOTAL_LAYERS)));
- return Math.max(1, Math.min(ENV_CAP, Math.pow(ENV_STEP, D - GRACE)), Math.pow(Math.max(1, D) / Gm, GM_Q));
- }
- /** 兵种技能、已解锁兵种数、种族羁绊 */
- export function lifeSkill(state) {
- const lv = state.lives || {};
- let prod = 1, spd = 1, skip = 0, crystal = 1, total = 0, awake = 1;
- const byRace = { 生命: 0, 星穹: 0, 虚空: 0 };
- for (const l of C.LIVES) {
- const n = lv[l.id] || 0;
- if (!n) continue;
- byRace[l.race]++; total += n;
- const k = 1 + l.eff.per * n;
- if (l.eff.kind === 'prod') prod *= k;
- else if (l.eff.kind === 'speed') spd *= k;
- else if (l.eff.kind === 'skip') skip += l.eff.per * n;
- else if (l.eff.kind === 'crystal') crystal *= k;
- else if (l.eff.kind === 'awake') awake += l.eff.per * n;
- }
- const stacks = {};
- for (const r of Object.keys(C.RACES)) stacks[r] = C.raceStacks(r, byRace[r]);
- return {
- prod, spd, skip, crystal, awake, total, byRace, stacks,
- bondProd: Math.max(1e-6, Math.pow(C.RACE_BOND_MULT, stacks.生命)),
- bondCrystal: Math.max(1e-6, Math.pow(C.RACE_BOND_MULT, stacks.星穹)),
- bondSkip: Math.min(2, C.RACE_SKIP_BONUS * stacks.虚空),
- };
- }
- /** 跳关触发率 p(封顶 60%) */
- export function skipProbOf(state, sk) {
- const rule = C.PLANE_RULES[state.currentPlane || 0];
- const l = sk || lifeSkill(state);
- return Math.min(C.SKIP.cap - 1e-6, C.canopySkip(state.upgrades?.canopy || 0) + l.skip + l.bondSkip + (rule.skipMult - 1));
- }
- /** 炼化乘区 M */
- export function rebirthMOf(state, sk) {
- const p = state.currentPlane ?? 0;
- const l = sk || lifeSkill(state);
- const tier = C.exploreTierOf(state.planes?.[p]?.explore || 0);
- const skipNote = 1 + Math.min(C.REBIRTH.skipNoteCap, 0.5 * skipProbOf(state, l)) * l.crystal;
- const insight = 1 + (state.exploreSum || 0) / 100;
- return Math.max(1e-6, l.crystal * l.bondCrystal * tier.crystal * skipNote * insight * C.freedomMul(state) * C.resonanceMul(state));
- }
- /** 推进速度乘区 M_spd(不含危机) */
- export function speedMulOf(state) {
- const l = lifeSkill(state);
- 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;
- }
- /**
- * 全部乘区与速率(GDD §6)。
- * 产出/秒 = essenceBase(L, P_root) × M_prod
- * 单层耗时 = (4+10L) × gm / M_spd;跳关后期望 = / (1+E[k])
- */
- export function computeStats(state) {
- const p = state.currentPlane ?? 0;
- const rule = C.PLANE_RULES[p];
- const L = state.layer || 0;
- const start = F0(state);
- const now = nowSec();
- const sk = lifeSkill(state);
- const frozen = !!(state.planeRun && state.planeRun.active);
- const explore = state.planes?.[p]?.explore || 0;
- const tier = C.exploreTierOf(explore);
- const cr = state.crisis || {};
- const crisisBuff = (cr.buffUntil || 0) > now;
- const crisisPenalty = (cr.penaltyUntil || 0) > now;
- const crisisMult = crisisBuff ? 1 + C.CRISIS.reward : crisisPenalty ? 1 - C.CRISIS.penalty : 1;
- const narrSig = state.magic?.sigUntil > now ? Math.min(C.NARRATE_STACK_CAP, state.magic.sig || 0) : 0;
- const narrate = Math.pow(rule.narrateBoost || 1, narrSig);
- const mults = {
- tree: C.treeProdMul(state.treeLv || 0),
- blood: C.multRebirth(state.rebirths || 0),
- explore: C.multExploration(explore),
- creature: C.multCreature(sk.total),
- bond: sk.bondProd,
- relic: C.relicMul(state.relics?.length || 0),
- freedom: C.freedomMul(state),
- plane: rule.prodMult,
- crisis: crisisMult,
- narrate,
- };
- const Mprod = Object.values(mults).reduce((a, b) => a * b, 1);
- const rootP = C.rootProdMul(state.upgrades?.root || 0);
- const base = C.essenceBase(L, rootP);
- const prodRaw = base * Mprod;
- const gm = marchBase(L, start);
- const Mspd = speedMulOf(state) * (crisisPenalty ? 1 - C.CRISIS.penalty : 1);
- const layerSec = C.visitTime(L) * gm / Mspd;
- const layersPerSec = Mspd / (C.visitTime(L) * gm);
- const skipP = skipProbOf(state, sk);
- const skipExp = C.skipExpectation(skipP);
- const timeMul = 1 + skipExp;
- const rebirthM = rebirthMOf(state, sk);
- const reward = rebirthEstimate(state, rebirthM);
- return {
- plane: p, rule, start, base, rootP, prodMul: Mprod, mults, frozen,
- prodRaw, prod: frozen ? 0 : prodRaw,
- creatureLv: sk.total, skills: sk, tier, explore,
- gm, speedMul: Mspd, layerSec, layersPerSec, minutesPerLayer: (layerSec * timeMul) / 60,
- skipP, skipExp, timeMul,
- rebirthM, reward, rebirthCost: C.rebirthCost(state.maxLayerRound || 0),
- crisisBuff, crisisPenalty, crisisMult, narrate, narrSig, anchored: explore >= C.EXPLORE_FULL,
- };
- }
- /** 单次炼化可得结晶(里世界中主世界冻结:返回 0)。state.rebirth() 使用同一函数:UI 预期 = 实得 */
- export function rebirthEstimate(state, M) {
- if (state.planeRun && state.planeRun.state !== 'result') return 0;
- const p = state.currentPlane ?? 0;
- const base = C.rebirthReward({
- maxLayer: state.maxLayerRound || 0,
- exploreSum: state.exploreSum || 0,
- incomeMul: M ?? rebirthMOf(state),
- ruleMod: C.PLANE_REBIRTH_MOD[p] || 1,
- });
- return Math.max(1, Math.floor(base * Math.pow(C.REBIRTH.decay, state.round?.refines || 0)));
- }
- export function upgradeCost(state, kind) { return Math.ceil(C.costOf(kind, state.upgrades?.[kind] || 0)); }
- export const piOf = C.piOf;
- export function lifeCost(state, lifeId, atLevel) {
- const idx = C.LIVES.findIndex((l) => l.id === lifeId);
- const n = atLevel ?? (state.lives?.[lifeId] || 0);
- return Math.ceil(C.lifeCostOf(C.lifeBase(idx), n + 1, piOf(state.layer || 0, F0(state))));
- }
- export function lifeUnlocked(state, idx) { return idx === 0 || (state.lives?.[C.LIVES[idx - 1].id] || 0) >= 3; }
- export function permCost(state, kind) {
- if (kind === 'stem' || kind === 'tree') return C.treeCost(state.treeLv || 0);
- if (kind === 'relic') return C.relicCost(state.relics?.length || 0);
- return Infinity;
- }
- export function rebirthFloor(state) { return C.earlyLayer(Math.max(state.maxLayerRound || 0, state.layer || 0), C.PLANE_RULES[state.currentPlane || 0]); }
- export function escapeProgress(state) { return Math.min(1, (state.layer || 0) / C.TOTAL_LAYERS); }
- /**
- * 本轮升级的“边际优化”决策(GDD §7.2):
- * 在 根须 / 树冠 / 每个已解锁兵种 之间,选择“单位成本乘区收益”最高且可在本轮阶段内(ETA×回归时间)赚回者。
- * @returns {object|null} { kind, id?, n? }
- */
- export function bestUpgrade(state, stats) {
- const s = state;
- const st = stats || computeStats(s);
- const income = Math.max(st.prod, 1e-9);
- const budget = s.essence;
- const cands = [];
- // 根须:买单级(批量策略由调用方按返回值逐级执行)
- const mult = C.piCostMul(piOf(s.layer || 0, st.start));
- const rCost = C.costOf('root', s.upgrades.root || 0) * mult;
- cands.push({ kind: 'root', n: 1, cost: rCost, v: Math.log(1 + C.COST.root.per) * 1.6 / rCost });
- // 树冠:跳关触发率 +0.5% → 时间乘区收益 log((1+E(p+δ))/(1+E(p)))(再做 1.0×0.5 权重:跳关不提升单层产出)
- const cCost = C.costOf('canopy', s.upgrades.canopy || 0) * mult; // 树冠不再随 π 涨价(跳关本身就是时间乘区)
- const p0 = st.skipP, d = C.COST.canopy.per;
- const dvSkip = p0 < C.SKIP.cap ? Math.log((1 + C.skipExpectation(p0 + d)) / (1 + st.skipExp)) * 0.5 : 0;
- const dvSpeed = Math.log(1.08) * 0.5;
- cands.push({ kind: 'canopy', n: 1, cost: cCost, v: (dvSkip + dvSpeed) / cCost });
- // 兵种:prod 类 ×1(产出乘区)、speed 类 ×0.5(进度乘区,产出随层数指数跟进)
- C.LIVES.forEach((l, i) => {
- if (!lifeUnlocked(s, i)) return;
- const cost = lifeCost(s, l.id);
- 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;
- cands.push({ kind: 'life', id: l.id, n: 1, cost, v: Math.log(1 + l.eff.per) * w / cost });
- });
- // 只买“等待时间”≤ ETA 秒的项(成本 ≤ ETA × 当前产出/秒);一次只返回一项最优
- 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);
- return ok[0] || null;
- }
- /** 永久投资:世界树 / 遗物 的下一项(按 收益/成本 最高) */
- export function bestPerm(state, stats) {
- const s = state;
- const st = stats || computeStats(s);
- const o = [];
- const tc = C.treeCost(s.treeLv || 0);
- // 世界树:产出 ×(1+0.05) 且 速度 ×(1+0.08) → 加权=生产速度×产出 → 用 0.5 权重估计速度对层的影响
- const tv = Math.log(1 + C.TREE.prodPer) + 0.5 * Math.log(1 + C.TREE.speedPer);
- o.push({ kind: 'stem', cost: tc, v: tv / tc });
- if ((s.relics || []).length < 8) { const rc = C.relicCost(s.relics.length); o.push({ kind: 'relic', cost: rc, v: Math.log(1.25) / rc }); }
- return o.sort((a, b) => b.v - a.v)[0];
- }
|