@rppdice/core
v1.2.0
Published
RppDice engine: universal dice grammar (2d6+3>=10, 4D6KH3, …) + declarative TOML system loader + step pipeline runner.
Maintainers
Readme
@rppdice/core
RppDice のエンジン本体 — 汎用ダイス文法 (2d6+3>=10、4D6KH3、3D10R6 …)、TOML 定義の TRPG システムローダー、ステップパイプラインランナー。純粋 TypeScript、ネイティブモジュールなし。全体像は リポジトリ README を参照。
インストール
npm install @rppdice/core(プレアルファ — 公開 API が固まるまで npm への publish は保留しています。)
2 つのエントリポイント
1. 汎用ダイス文法 — roll(command, opts?)
import { roll } from "@rppdice/core";
const r = roll("2d6+3", { seed: 42n });
// r.value → 13
// r.text → "2D6+3 > [4, 6]+3 = 13"
// r.rands → [{ kind: "normal", sides: 6, value: 4 }, …]
const c = roll("2d6+3>=10", { seed: 42n });
// c.comparison → { op: ">=", lhs: 13, rhs: 10, success: true }NdM、+ - * /、()、比較演算 (>=、<=、==、!= …)、修飾子 (KH N / KL N / DH N / DL N の keep / drop、R K / R>=K 等の爆発) をサポート。修飾子は記述順 (左→右) に適用されるため、4d6KH3R6 (keep → 爆発) と 4d6R6KH3 (爆発 → keep) は別物です。
完全な記法リファレンス: プレイグラウンドサイトの /docs/<lang>/basics 以下のセクション群、または roll の JSDoc を参照。
2. システム判定 — evalSystem(registry, id, command, opts?)
import { createRegistry, loadSystem, evalSystem } from "@rppdice/core";
import TB_TOML from "@rppdice/example/systems/three-band-2d6.toml";
const registry = createRegistry();
loadSystem(registry, TB_TOML);
const out = evalSystem(registry, "three-band-2d6", "TB+3", { seed: 42n, locale: "ja" });
// out.text → "2D6 [4, 6] 修正=3 合計=13 → 大成功"
// out.success → true (rule.verdict が flags.success に登録されているとき)TOML 「システム」は、正規表現コマンドパターン・ステップパイプライン (roll、eval、outcome、if、loop、repeat、accumulate、output)・i18n.<tag> ブロックをひとまとめにします。マッチした verdict は out.success / out.failure / out.critical / out.fumble のいずれかに反映され、テンプレ用には実行コンテキストの {verdict} 変数(ローカライズ済みラベル)で参照できます。登録済みパターンのいずれにも該当しない入力は、evalSystem が汎用文法へフォールスルーするので基本記法はそのまま動きます。
作成ガイドはサンプル一式を参照: packages/example/ の各 src/systems/*.toml が i18n ブロック・outcome.flags の使い方・テーブル参照・ループの典型を網羅しています。
トレース
evalSystem は tracer コールバックを受け付け、マッチしたパイプラインのトップレベルステップごとに TraceFrame を 1 個渡します。CI でのステップ単位検証、プレイグラウンド表示、@rppdice/cli の rppdice trace コマンドなどで利用します。
const frames: TraceFrame[] = [];
evalSystem(registry, "three-band-2d6", "TB+3", {
seed: 42n,
tracer: (f) => frames.push(f),
});エラー
スローされる全エラーは RppDiceError を継承し、ダウンストリーム i18n 用の { code, params } を持ちます。コードカタログは src/dice/error-codes.ts と src/expr/error-codes.ts にあります。
import { RppDiceError } from "@rppdice/core";
try {
roll("2d0");
} catch (e) {
if (e instanceof RppDiceError) {
e.code; // "dice.invalid_sides"
e.params; // { sides: 0 }
}
}ライセンス
MIT。LICENSE を参照。
