rift.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // 里世界肉鸽:规则引擎(纯数据,不碰 DOM)。隔离局内,不继承主世界永久资源。
  2. // 一局 = 6 场遭遇(战斗/精英/奇遇/圣所),约 1~5 分钟。署名:FmodeCode
  3. import * as C from './config.js';
  4. import { mulberry32, clamp } from './util.js';
  5. export const RULES = ['knowledge', 'oath', 'belief', 'judge'];
  6. export const RULE_NAME = { knowledge: '知识', oath: '誓约', belief: '信念', judge: '裁决' };
  7. export const RULE_DESC = {
  8. knowledge: '知识 → 攻击:每点攻击 +1.5',
  9. oath: '誓约 → 生命:每点最大生命 +9',
  10. belief: '信念 → 生命:每点最大生命 +9',
  11. judge: '裁决 → 攻击:每点攻击 +2',
  12. };
  13. export const ENCOUNTERS = 6; // 一局推进 6 步,第 6 步为终局
  14. export const CAP = { knowledge: 30, oath: 30, belief: 30, judge: 30 };
  15. export const STARTER = 1; // 每项规则起步 1 点
  16. const MONSTERS = ['灰烬守卫', '蚀影猎犬', '残响歌者', '残影巡游者', '无人电梯', '空洞低语', '镜像拷问者'];
  17. const ELITES = ['蚀壳巨像', '规则提灯人', '终末回声', '零点行者'];
  18. const BOSSES = ['灰烬王座', '演算核心', '镜之城主人', '天劫化身'];
  19. export const TAUNTS = [
  20. '你不该记得上一次。', '规则站在我这边。', '树根不会再长出来了。', '你的誓约很轻。', '这一次也一样的。', '尘埃也想逃出去吗?',
  21. ];
  22. const EVENTS = [
  23. { id: 'pool', title: '断桥下的光池', text: '桥塌了一半,水面倒映出不是你的脸。光池里漂着一枚还在跳动的规则碎片。',
  24. opts: [
  25. { label: '伸手打捞', hint: '获得 2 点随机属性,损失 15% 生命', apply: { rules: 2, dmg: 0.15 } },
  26. { label: '绕路前行', hint: '恢复 25% 生命,能量 +2', apply: { heal: 0.25, energy: 2 } },
  27. ] },
  28. { id: 'altar', title: '无名的祭坛', text: '祭坛上刻着:献上你的一部分,换取它曾经的名字。',
  29. opts: [
  30. { label: '献上记忆', hint: '最高属性 +3,受到 20% 伤害', apply: { topRule: 3, dmg: 0.2 } },
  31. { label: '拿走供品', hint: '能量 +4', apply: { energy: 4 } },
  32. ] },
  33. { id: 'wanderer', title: '迷路的观测者', text: '穿旧外套的观测者正在记录星光。他说记得你轮回之前的样子。',
  34. opts: [
  35. { label: '交换情报', hint: '知识 +3、裁决 +1', apply: { add: { knowledge: 3, judge: 1 } } },
  36. { label: '请他疗伤', hint: '恢复 40% 生命', apply: { heal: 0.4 } },
  37. { label: '离开', hint: '能量 +1', apply: { energy: 1 } },
  38. ] },
  39. { id: 'mirror', title: '会说话的镜子', text: '镜子里是另一个你,它说:“你每次都死在同一个地方。”它没有说错。',
  40. opts: [
  41. { label: '击碎镜子', hint: '全部属性 +2,同时受到 15% 伤害', apply: { addAll: 2, dmg: 0.15 } },
  42. { label: '对它微笑', hint: '能量 +3', apply: { energy: 3 } },
  43. ] },
  44. { id: 'door', title: '三扇门', text: '三扇门后传来不同的声音:翻页声、祷词声、心跳声。',
  45. opts: [
  46. { label: '推开翻页的那扇', hint: '知识 +3', apply: { add: { knowledge: 3 } } },
  47. { label: '推开祷词的那扇', hint: '誓约 +3', apply: { add: { oath: 3 } } },
  48. { label: '推开心跳的那扇', hint: '信念 +3', apply: { add: { belief: 3 } } },
  49. ] },
  50. { id: 'forge', title: '灰烬铁匠铺', text: '炉火还温着。铁匠不在,桌上有一柄未完工的誓约之剑。',
  51. opts: [
  52. { label: '锻成剑刃', hint: '裁决 +3', apply: { add: { judge: 3 } } },
  53. { label: '带走防具', hint: '信念 +2、誓约 +1', apply: { add: { belief: 2, oath: 1 } } },
  54. ] },
  55. ];
  56. export const EXPLORE_GAIN = 1.4; // 里世界探索度总体缩放(配合 anchors 进入二/三/四阶的门槛)
  57. export const EXPLORE_MIN_FAIL = 3;
  58. const OATHS = [
  59. { id: 'battle', name: '破阵之愿', desc: '攻击 +3', add: { knowledge: 1, judge: 1 }, atkBonus: 1 },
  60. { id: 'warden', name: '守望之愿', desc: '最大生命 +40%,誓约、信念各 +1', hp: 0.4, add: { oath: 1, belief: 1 } },
  61. { id: 'seer', name: '先知之愿', desc: '事件强化 ×2(随机属性翻倍、治疗翻倍)', eventMul: 2 },
  62. ];
  63. const rnd = (r, n) => Math.floor(r() * n);
  64. const pick = (r, arr) => arr[rnd(r, arr.length)];
  65. export function atkOf(s) { return 10 + 1.5 * (s.attrs.knowledge + bonusOf(s, 'knowledge')) + 2 * (s.attrs.judge + bonusOf(s, 'judge')) + ((s.oathObj && s.oathObj.atkBonus) || 0); }
  66. export function hpMaxOf(s) { return Math.round((40 + 4 * (s.attrs.oath + bonusOf(s, 'oath')) + 1.5 * (s.attrs.belief + bonusOf(s, 'belief'))) * (1 + (s.oathObj?.hp || 0))); }
  67. /** 敌人威胁 pK:随步数与位面递增,与玩家属性无关(属性只放大你的攻击/生命) */
  68. export function threatOf(s, step, kind) {
  69. const a = s.attrs;
  70. const sum = a.knowledge + a.oath + a.belief + a.judge;
  71. const p = s.plane || 0, i = step ?? s.step ?? 0;
  72. // 敌方威胁 = 你的规则总和 ×(1+10%×步)×1.15^位面——敌人始终比你“多带一点规则”,位面越深越硬(与你的预算密度同序)
  73. const base = sum * (1 + 0.1 * i) * Math.pow(1.15, p);
  74. return Math.max(1, Math.round(base * (kind === 'boss' ? 1.1 : kind === 'elite' ? 1.05 : 1)));
  75. }
  76. export function clampAttr(v, k) { return clamp(+v.toFixed(2), 0, CAP[k]); }
  77. // 本位面在里世界的“规则浓度”:一阶 ×1.00 → 四阶 ×1.30;浓度越高,开局预算点越多(世界树等级的心智加成)
  78. export function planeDensity(plane) { return Math.pow(1.3, plane || 0); }
  79. export function riftNewRun(plane, budgetOverride) {
  80. const r = mulberry32((Math.random() * 1e9) >>> 0);
  81. return {
  82. seed: rnd(r, 1e9), plane, step: 0, state: 'build', cleared: false, dead: false,
  83. budget: budgetOverride != null ? budgetOverride : Math.round(20 * planeDensity(plane)), // 开局“本源点”(预算点):一阶 20、二阶 26、三阶 34、四阶 44
  84. density: planeDensity(plane),
  85. available: { knowledge: 2 + rnd(r, 4), oath: 2 + rnd(r, 4), belief: 2 + rnd(r, 4), judge: 2 + rnd(r, 4) }, // 每类可分配 2~5 格
  86. hp: 100, hpMul: 1, dmgMul: 1, energy: C.RIFT.energy + 8, maxEnergy: C.RIFT.energy + 8,
  87. attrs: { knowledge: 1, oath: 1, belief: 1, judge: 1 }, // 起步各 1 点,避免空表
  88. mouth: null, oathObj: null, immune: false,
  89. log: [], score: 0, turns: 0,
  90. };
  91. }
  92. /** 开局自由分配规则:总点数 = budget,每类不超过 available[k] */
  93. export function pointsSpent(s) { return RULES.reduce((a, k) => a + s.attrs[k], 0); }
  94. export function pointsLeft(s) { return s.budget - pointsSpent(s); }
  95. export function isBuildValid(s) { return pointsLeft(s) === 0 && RULES.every((k) => s.attrs[k] >= 1); }
  96. /** 事件加成(不计入威胁):存于 s.bonus,战斗生效 */
  97. export function bonusOf(s, k) { return (s.bonus && s.bonus[k]) || 0; }
  98. export function buildDone(s, oathIdx) {
  99. if (!isBuildValid(s)) return false;
  100. s.buildDone = true;
  101. const o = OATHS[clamp(oathIdx, 0, OATHS.length - 1)];
  102. s.oathObj = o;
  103. s.hp = hpMaxOf(s);
  104. s.state = 'run';
  105. riftPrepare(s, 0);
  106. return true;
  107. }
  108. function constDone(s) { return !!s.oathObj; }
  109. export function encounterKind(i) {
  110. if (i === ENCOUNTERS - 1) return 'boss';
  111. return ['normal', 'event', 'elite', 'normal', 'event'][i] || 'normal';
  112. }
  113. export function riftPrepare(s, i) {
  114. s.step = i;
  115. const r = mulberry32((s.seed + i * 7919) >>> 0);
  116. const kind = encounterKind(i);
  117. if (kind === 'event') {
  118. const ev = pick(r, EVENTS);
  119. s.state = 'event';
  120. s.mouth = { type: 'event', title: ev.title, text: ev.text, opts: ev.opts.map((o, j) => ({ label: o.label, hint: o.hint, j })), ev };
  121. return s;
  122. }
  123. s.state = 'fight';
  124. const scale = Math.pow(1.08, s.plane) * Math.pow(1.06, i);
  125. // 终局血量按终局威胁校准:恰好需要 3 次左右超杀(能赢,但有压力)
  126. const atk0 = atkOf(s);
  127. // 敌人血量/攻击 ∝ 你的攻击力(按全局强度计算,不吃本局临时加点):数值不脱钩,打起来始终“几到十几剑”
  128. const basis = (s.baseAtk || 10 + 1.5 * 1 + 2 * 1) * Math.pow(s.density || planeDensity(s.plane), 2.2);
  129. const gAtk = basis;
  130. const kHp = (kind === 'elite' ? 1.5 : 1.0) * Math.pow(1.06, i + s.plane);
  131. let hp = gAtk * kHp * 2.6;
  132. let dmg = gAtk * 0.04 * (kind === 'boss' ? 1.15 : 1);
  133. let name = pick(r, MONSTERS);
  134. if (kind === 'elite') name = pick(r, ELITES);
  135. if (kind === 'boss') { hp = Math.max(hp, threatOf(s, i, 'boss') * 4.0); name = pick(r, BOSSES); }
  136. s.mouth = {
  137. type: kind, name, hp: Math.round(hp), hpMax: Math.round(hp), dmg: Math.round(dmg * 10) / 10,
  138. threat: threatOf(s, i, kind), taunt: pick(r, TAUNTS), nextImmunity: !!s.immune, hp0: Math.round(hp),
  139. };
  140. return s;
  141. }
  142. export function kindName(k) { return { normal: '战斗', elite: '精英', boss: '终局', event: '奇遇' }[k] || k; }
  143. /**
  144. * 挥剑:结果 = 攻击 + 骰面(0..5);pK = 敌人威胁。
  145. * 拼招 结果 > pK 且 < pK×1.75 → 敌人 −结果,自己 −3
  146. * 超杀 结果 ≥ pK×1.75 → 敌人 −⌊结果×1.75⌋,自己 −1
  147. * 阻断 结果 ≤ pK → 敌人格挡,自己 −5,能量 +1
  148. * 誓约最大时,每场第一次受击豁免。
  149. */
  150. export function slash(s, rollVal) {
  151. const m = s.mouth;
  152. if (s.state !== 'fight' || !m) return null;
  153. const roll = clamp(Math.floor(rollVal), 0, 5);
  154. // 基础战力 = 攻击(含知识/裁决),每次挥剑再叠一个 0..5 的“临时战意”
  155. const value = Math.round(atkOf(s) + roll);
  156. const threat = m.threat;
  157. let res, dmg = 0, back = 0;
  158. const a = s.attrs;
  159. const maxK = RULES.reduce((x, y) => (a[y] > a[x] ? y : x), RULES[0]);
  160. if (value > threat) {
  161. const need = threat * 1.75;
  162. if (value >= need) { res = 'crit'; dmg = Math.floor(value * 1.75); back = 1; }
  163. else { res = 'win'; dmg = value; back = 3; }
  164. // 裁决(攻击属性):拼招/超杀时额外削减 = 裁决 × 1
  165. if (maxK === 'judge' || maxK === 'knowledge') dmg += Math.floor((maxK === 'judge' ? a.judge + bonusOf(s, 'judge') : a.knowledge + bonusOf(s, 'knowledge')) * 1);
  166. } else {
  167. res = 'block'; back = 5; s.energy = Math.min(s.maxEnergy, s.energy + 1);
  168. }
  169. if (back > 0 && maxK === 'oath' && !s.immune) { back = 0; s.immune = true; }
  170. m.hp = Math.max(0, m.hp - dmg);
  171. s.hp = Math.min(hpMaxOf(s), s.hp - back);
  172. if (maxK === 'belief' && back > 0) s.hp = Math.min(hpMaxOf(s), s.hp + a.belief * 0.5);
  173. s.turns++;
  174. s.score += res === 'crit' ? 25 : res === 'win' ? 12 : 0;
  175. m.lastLog = { res, roll, value, threat, dmg, back };
  176. const killed = m.hp <= 0, dead = s.hp <= 0;
  177. if (killed) s.score += m.type === 'boss' ? 120 : m.type === 'elite' ? 60 : 30;
  178. if (dead) s.dead = true;
  179. return { res, roll, value, threat, dmg, back, killed, dead, maxK, mHp: m.hp, mHpMax: m.hpMax, heroHp: s.hp, heroMax: hpMaxOf(s) };
  180. }
  181. /** 挥剑后的流程:死亡 → 结算;击杀 → 下一步 / 通关 */
  182. export function afterSlash(s, r) {
  183. if (r.dead) { s.state = 'result'; return { dead: true }; }
  184. if (r.killed) {
  185. if (s.step + 1 >= ENCOUNTERS) { s.state = 'result'; s.cleared = true; return { cleared: true }; }
  186. riftPrepare(s, s.step + 1);
  187. return { next: true };
  188. }
  189. return {};
  190. }
  191. /** 奇遇选择 */
  192. export function applyOption(s, j) {
  193. const m = s.mouth;
  194. if (s.state !== 'event' || !m || !m.ev) return null;
  195. const opt = m.ev.opts[j];
  196. if (!opt) return null;
  197. const r = mulberry32((s.seed + s.step * 53 + j) >>> 0);
  198. const mul = (s.oathObj && s.oathObj.eventMul) || 1;
  199. const a = opt.apply;
  200. const msgs = [];
  201. s.bonus = s.bonus || {};
  202. const addA = (k, n) => { s.bonus[k] = (s.bonus[k] || 0) + n * mul; msgs.push(`${RULE_NAME[k]} +${n * mul}`); };
  203. if (a.add) for (const k of Object.keys(a.add)) addA(k, a.add[k]);
  204. if (a.addAll) for (const k of RULES) addA(k, a.addAll);
  205. if (a.rules) for (let i = 0; i < a.rules; i++) addA(pick(r, RULES), 1);
  206. if (a.topRule) {
  207. const tot = (k) => s.attrs[k] + (s.bonus[k] || 0);
  208. const k = RULES.slice().sort((x, y) => tot(y) - tot(x))[0];
  209. addA(k, a.topRule);
  210. }
  211. if (a.heal) { const v = Math.round(hpMaxOf(s) * a.heal * mul); s.hp = Math.min(hpMaxOf(s), s.hp + v); msgs.push(`生命 +${v}`); }
  212. if (a.energy) { s.energy = Math.min(s.maxEnergy, s.energy + a.energy); msgs.push(`能量 +${a.energy}`); }
  213. if (a.dmg) { const v = Math.round(hpMaxOf(s) * a.dmg); s.hp = Math.max(1, s.hp - v); msgs.push(`生命 −${v}`); }
  214. s.score += 8;
  215. if (s.step + 1 >= ENCOUNTERS) { s.state = 'result'; s.cleared = true; return { cleared: true, msg: msgs.join(',') }; }
  216. riftPrepare(s, s.step + 1);
  217. return { next: true, msg: msgs.join(',') };
  218. }
  219. /**
  220. * 结算:探索度增量 = ⌊√(步数+1 + 分/20)⌋,通关 ×1.5(与 config.riftExploreDelta 同源)
  221. * 本源碎片 = max(本源秒产, 10) × 8 × 探索增量/4
  222. * 徽记:通关 1 枚;失败但分 ≥ 100 也 1 枚(失败也有价值);已锚定位面不再产出徽记
  223. */
  224. export function riftResult(s, prodPerSec, alreadyAnchored) {
  225. const steps = s.step + (s.cleared ? 1 : 0) + 1;
  226. // 失败也有价值:探索碎片至少 25% 的固定产出
  227. const delta = Math.max(EXPLORE_MIN_FAIL, Math.round(C.riftExploreDelta(steps, s.score, s.cleared) * EXPLORE_GAIN));
  228. const essence = Math.max(prodPerSec * 8, 10) * Math.max(1, delta / 16);
  229. const shard = alreadyAnchored ? 0 : (s.cleared ? 1 : s.score >= 100 ? 1 : 0);
  230. return { explore: delta, essence, score: s.score, shard, cleared: s.cleared, steps };
  231. }
  232. export { OATHS };