附录 A:环境与复现速查

卷一的复现手册:本机实测环境(Python 3.14 + numpy/pandas、Node 26 原生 TypeScript、无 matplotlib、无 tsc/npm)、bastion 完整目录结构、全部实跑命令、跨语言逐位一致的三条硬规则、以及一份可一键复现的校验脚本。遇到「书里的数字对不上」时先看这一页。

本版配套:下载SLG 两卷完整教学快照 。先解压完整依赖,再按包内说明运行;文末的完整文件可单独用于修订。新补参数为教学假设,正文历史快照的固定输出需与当前配置区分。

这一页解决一个问题:书里的代码怎么在我自己的机器上跑起来,跑出来的数字为什么可能和书上不完全一样。

一、本机实测环境

这套书的环境前提是实测过的,不是「一般来说」:

项实测结果说明
Python3.14.6S 层仿真
numpy2.5.1数组与随机数
pandas3.0.3CSV 与数据表
matplotlib未安装所以图表走纯 Python SVG
scipy未安装拟合用 numpy 手写最小二乘
Node26.5.0P 层原型
tsc / tsx / vitest均未安装所以不用它们

一条结论:这套书不需要安装任何东西。 Python 有 numpy 和 pandas 就够了,Node 直接跑 TypeScript。没有 package.json,没有 npm install,没有 tsconfig.json。

先确认你机器上的前提:

# file: check_env.sh
# tier: S
# verify: sh check_env.sh
python3 -c "import numpy, pandas; print('numpy', numpy.__version__, 'pandas', pandas.__version__)"
node -v

期望输出类似:

numpy 2.5.1 pandas 3.0.3
v26.5.0

只要 Python 能 import numpy, pandas、Node 版本 ≥ 26,这套书的所有代码都能跑。

二、bastion 目录结构

bastion/
  config/
    resources.json      # 资源定义与上限
    buildings.json      # 建筑等级、产出曲线、升级成本
    units.json          # 兵种属性与克制环
    battle.json         # 战斗规则常量
    season.json         # 赛季参数(卷二使用)
    pricing.json        # 付费与定价(卷二使用)
  sim/
    bastion_core.py     # 仿真核心:产出、成本、结算、战斗
    svg_line.py         # 零依赖 SVG 折线图
    ch01_*.py ... ch15_*.py   # 各章仿真脚本
  proto/
    src/
      core.ts           # 原型核心:与 bastion_core.py 逐位一致
      report.ts         # 打印对照报告
      ch01_*.ts ... ch15_*.ts
    test/
      bastion.test.ts   # 核心断言
      ch01.test.ts ... ch15.test.ts
  out/                  # 仿真产物:JSON / CSV / SVG

config/ 是唯一真源,sim/ 与 proto/ 都只读它。

三、全部实跑命令

S 层(Python 仿真)

# file: run_sim.sh
# tier: S
# verify: sh run_sim.sh
set -e
cd "$(dirname "$0")"

# 1) 仿真核心:产出、结算、升级成本、一场战斗
python3 sim/bastion_core.py

# 2) 零依赖 SVG 折线图
python3 sim/svg_line.py

# 3) 各章仿真(按需)
python3 sim/ch12_loop_sim.py --config config/ --days 180

P 层(TypeScript 原型)

# file: run_proto.sh
# tier: P
# verify: sh run_proto.sh
set -e
cd "$(dirname "$0")"

# 1) 直接执行一个 .ts 文件
node proto/src/report.ts

# 2) 跑断言(注意:必须用 glob,目录形式不发现 .ts)
node --test 'proto/**/*.test.ts'

一个容易踩的坑:node --test proto/(传目录)不会发现 .ts 文件,会报 MODULE_NOT_FOUND。必须用 glob:node --test 'proto/**/*.test.ts',或者显式指定文件 node --test proto/test/ch01.test.ts。这是实测结论。

一键校验

把两侧的对照报告都跑出来,逐位比对:

# file: sim/verify_two_consumers.py
# tier: S
# verify: python3 sim/verify_two_consumers.py
"""验证「一份配置两处消费」:Python 与 TypeScript 读同一份 config,结果必须逐位一致。"""

from __future__ import annotations

import json
import os
import subprocess
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
BASTION = os.path.dirname(HERE)
sys.path.insert(0, HERE)

import bastion_core as core  # noqa: E402


def ts_report() -> dict:
    out = subprocess.run(
        ["node", os.path.join(BASTION, "proto", "src", "report.ts")],
        capture_output=True, text=True, check=True,
    )
    return json.loads(out.stdout)


def main() -> int:
    py = core.battle_report()
    ts = ts_report()
    if py == ts:
        print("OK 两侧逐位一致")
        print(json.dumps(py["productionPerHour"], ensure_ascii=False))
        print("battle:", py["battle"]["winner"], "rounds=", py["battle"]["rounds"])
        return 0
    print("FAIL 两侧不一致")
    for k in py:
        if py[k] != ts.get(k):
            print(f"  diff {k}: py={py[k]!r} ts={ts.get(k)!r}")
    return 1


if __name__ == "__main__":
    raise SystemExit(main())

本机实跑输出:

OK 两侧逐位一致
{"wood": 132, "stone": 110, "iron": 27, "grain": 199}
battle: a rounds= 1

四、跨语言逐位一致的三条硬规则

这是这套工程的地基。违反任意一条,「一份配置两处消费」就会变成「一份配置两个答案」。

规则一:配置里不写浮点系数,写整数比或整数百分比。

{ "outputBase": 60, "outputGrowthNum": 122, "outputGrowthDen": 100 }

不要写 "outputGrowth": 1.22。原因是浮点幂运算在不同运行时末位可能不一致,取整后就会差 1。

规则二:幂运算在 TypeScript 侧必须用 BigInt。

122^9 已经超过 2^53,用普通 number 会丢精度。Python 侧用原生大整数就没这个问题。

// snippet: proto/src/core.ts 中的整数幂实现
function ipow(base: number, exp: number): bigint {
  let acc = 1n;
  const b = BigInt(base);
  for (let i = 0; i < exp; i++) acc *= b;
  return acc;
}

规则三:除法一律整数地板除,上取整用整数公式。

  • 地板除:Python a // b,TypeScript Math.floor(a / b)(且 a、b 为整数)。
  • 上取整:(a + b - 1) // b,不要用浮点 ceil。
# snippet: sim/bastion_core.py 中的整数上取整
def alive_count(hp_pool: int, hp: int) -> int:
    if hp_pool <= 0:
        return 0
    return (hp_pool + hp - 1) // hp   # 整数上取整

五、数字对不上怎么办

先按这个顺序排查:

  1. 配置被改过吗? 这正是「一份配置两处消费」该有的行为。改 outputGrowthNum 从 122 到 125,5 级伐木场产出会从 132 变到另一个值,书上写的是 122 时的结果。
  2. 环境版本差得多吗? numpy/pandas 的大版本变化可能影响随机数序列。涉及随机的地方,本套都用自己实现的确定性 PRNG(如 mulberry32),不吃运行时随机数实现。
  3. 是不是「同机相对量级」的说明? 正文写「同机相对量级,绝对数值不可移植」的地方(比如结算耗时、吞吐),绝对值本来就依赖机器,只有相对关系可移植。
  4. 还不对,那就是书错了。 请告诉我具体的节号和数字。

六、两条纪律

复现这件事上有两条纪律,这套书从头守到尾:

  • 不编造竞品后台数字。真实竞品只作「公开可观察机制」的引用并标注观察时点,需要进后台才能拿到的数字一律不写,这类内容标为 X 层。
  • 所有数字来自本机实跑,正文不写「提升 N 倍」式的绝对话,只写「同机同输入下 A 比 B 少 X 个结算轮次」这类可复核的比较。

配置表的字段字典与完整 schema 见附录 B 。回到目录页 。

完整配套文件:proto/src/core.ts

这是共用核心的完整文件。它读附录 B 的完整配置,采用 BigInt 整数幂、整数结算与同时伤害。先保存本文件与配置,再运行双消费者检查。结果超出安全整数时明确拒绝;本教学模型不处理线上并发、存储和网络。

// file: proto/src/core.ts
import { readFileSync } from "node:fs";
export interface BuildingDef {
    id: string;
    resource: string;
    maxLevel: number;
    outputBase: number;
    outputGrowthNum: number;
    outputGrowthDen: number;
    upgradeCostBase: Record<string, number>;
    upgradeCostGrowthNum: number;
    upgradeCostGrowthDen: number;
}
export interface UnitDef {
    id: string;
    atk: number;
    def: number;
    hp: number;
    speed: number;
    load: number;
}
interface Units {
    units: UnitDef[];
    counter: Record<string, string>;
    counterMultiplierPct: number;
}
interface Rules {
    defenseFactorPct: number;
    minDamage: number;
    maxRounds: number;
}
export interface BattleResult {
    winner: string;
    rounds: number;
    aSurvivors: number;
    dSurvivors: number;
    aHp: number;
    dHp: number;
    log: {
        round: number;
        aHp: number;
        dHp: number;
        aDmg: number;
        dDmg: number;
    }[];
}
export function loadConfig<T = any>(name: string): T {
    if (!/^[a-zA-Z0-9_-]+$/.test(name))
        throw new Error("非法配置名");
    return JSON.parse(readFileSync(new URL(`../../config/${name}.json`, import.meta.url), "utf8"));
}
function nat(x: number, name: string, min = 0) {
    if (!Number.isSafeInteger(x) || x < min)
        throw new RangeError(name);
}
function scaled(base: number, num: number, den: number, exp: number): number {
    nat(base, "base");
    nat(num, "num", 1);
    nat(den, "den", 1);
    nat(exp, "exp");
    const result = BigInt(base) * BigInt(num) ** BigInt(exp) / (BigInt(den) ** BigInt(exp));
    if (result > BigInt(Number.MAX_SAFE_INTEGER))
        throw new RangeError("结果超出安全整数");
    return Number(result);
}
export function outputAtLevel(b: BuildingDef, level: number): number { nat(level, "level", 1); return scaled(b.outputBase, b.outputGrowthNum, b.outputGrowthDen, level - 1); }
export function upgradeCostAtLevel(b: BuildingDef, level: number): Record<string, number> { nat(level, "level", 1); return Object.fromEntries(Object.entries(b.upgradeCostBase).map(([r, v]) => [r, scaled(v, b.upgradeCostGrowthNum, b.upgradeCostGrowthDen, level - 1)])); }
export function productionPerHour(levels: Record<string, number>): Record<string, number> {
    const out: Record<string, number> = {};
    for (const b of loadConfig<{
        buildings: BuildingDef[];
    }>("buildings").buildings) {
        const lv = levels[b.id] ?? 0;
        nat(lv, "level");
        if (lv)
            out[b.resource] = (out[b.resource] ?? 0) + outputAtLevel(b, lv);
    }
    return out;
}
export function settle(levels: Record<string, number>, hours: number, start: Record<string, number> = {}): Record<string, number> {
    nat(hours, "hours");
    const stock = { ...start };
    const resources = loadConfig<{
        resources: {
            id: string;
            cap: number;
        }[];
    }>("resources").resources;
    const rates = productionPerHour(levels);
    for (const r of resources) {
        nat(stock[r.id] ?? 0, "stock");
        stock[r.id] ??= 0;
    }
    for (let h = 0; h < hours; h++)
        for (const [id, rate] of Object.entries(rates)) {
            const r = resources.find(r => r.id === id);
            if (!r)
                throw new Error("未知资源");
            stock[id] = Math.min(r.cap, stock[id] + rate);
        }
    return stock;
}
export function damagePerUnit(a: UnitDef, d: UnitDef, u: Units, r: Rules, terrainPct = 0): number {
    nat(terrainPct, "terrain");
    const pct = u.counter[a.id] === d.id ? u.counterMultiplierPct : 100;
    const raw = BigInt(a.atk) * BigInt(pct) - BigInt(d.def) * BigInt(r.defenseFactorPct);
    // BigInt truncates toward zero; clamp before division so negative damage still reaches the floor.
    return Math.max(r.minDamage, Number(raw * BigInt(100 + terrainPct) / 10000n));
}
export function resolveBattle(ai: string, ac: number, di: string, dc: number, terrainPct = 0): BattleResult {
    nat(ac, "aCount");
    nat(dc, "dCount");
    const u = loadConfig<Units>("units"), r = loadConfig<{
        rules: Rules;
    }>("battle").rules;
    const a = u.units.find(x => x.id === ai), d = u.units.find(x => x.id === di);
    if (!a || !d)
        throw new Error("未知兵种");
    let ah = a.hp * ac, dh = d.hp * dc, rounds = 0;
    const log: BattleResult["log"] = [];
    const alive = (hp: number, unit: UnitDef) => hp <= 0 ? 0 : Math.floor((hp + unit.hp - 1) / unit.hp);
    while (ah > 0 && dh > 0 && rounds < r.maxRounds) {
        rounds++;
        const ad = alive(ah, a) * damagePerUnit(a, d, u, r, terrainPct), dd = alive(dh, d) * damagePerUnit(d, a, u, r);
        ah -= dd;
        dh -= ad;
        log.push({ round: rounds, aHp: ah, dHp: dh, aDmg: ad, dDmg: dd });
    }
    return { winner: dh <= 0 && ah > 0 ? "a" : ah <= 0 && dh > 0 ? "d" : "draw", rounds, aSurvivors: alive(ah, a), dSurvivors: alive(dh, d), aHp: Math.max(0, ah), dHp: Math.max(0, dh), log };
}
export function battleReport() {
    const levels = { lumberyard: 5, quarry: 5, ironworks: 3, farm: 6 };
    const b = loadConfig<{
        buildings: BuildingDef[];
    }>("buildings").buildings.find(b => b.id === "lumberyard")!;
    return { version: 1, productionPerHour: productionPerHour(levels), settle24h: settle(levels, 24), lumberyardCostFrom5: upgradeCostAtLevel(b, 5), battle: resolveBattle("cavalry", 300, "archer", 300) };
}
export function mulberry32(seed: number): () => number {
    let a = seed >>> 0;
    return () => {
        a = (a + 0x6D2B79F5) >>> 0;
        let t = Math.imul(a ^ (a >>> 15), a | 1);
        t = (t ^ (t + Math.imul(t ^ (t >>> 7), t | 61))) >>> 0;
        return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
    };
}

完整配套文件:proto/src/report.ts

双消费者检查的 TS 入口,标准输出只包含报告 JSON。

// file: proto/src/report.ts
import { battleReport } from "./core.ts";
console.log(JSON.stringify(battleReport()));

完整配套文件:proto/test/core_contract.test.ts

核心边界测试:独立手算、零值、资源上限、非法分母、未知兵种与负数。它与正常双消费者对账互补。

// file: proto/test/core_contract.test.ts
// verify: node --test proto/test/core_contract.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { loadConfig, outputAtLevel, settle, resolveBattle } from "../src/core.ts";
test("增长公式用独立手算值 60×122⁴/100⁴ = 132", () => { const b = loadConfig("buildings").buildings[0]; assert.equal(outputAtLevel(b, 1), 60); assert.equal(outputAtLevel(b, 5), 132); assert.throws(() => outputAtLevel({ ...b, outputGrowthDen: 0 }, 2), RangeError); assert.throws(() => outputAtLevel(b, 0), RangeError); });
test("零时长与资源封顶", () => { assert.equal(settle({ lumberyard: 5 }, 0).wood, 0); const cap = loadConfig("resources").resources.find((r: any) => r.id === "wood").cap; assert.equal(settle({ lumberyard: 5 }, 1, { wood: cap - 1 }).wood, cap); assert.throws(() => settle({}, -1), RangeError); });
test("零兵力、未知兵种与非法计数", () => { assert.equal(resolveBattle("spearman", 0, "cavalry", 0).winner, "draw"); assert.throws(() => resolveBattle("missing", 1, "cavalry", 1)); assert.throws(() => resolveBattle("spearman", -1, "cavalry", 1), RangeError); });

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「slg」更多文章

  1. 12.3 留存与付费的联合分析
  2. 12.2 流失预警与召回
  3. 附录 C:全书公式、参数与数据集索引