feat: add support for stat templates

Introduces a new `template` stat type that allows for table-based
lookups via CSV blocks in markdown files. When a template stat is
rolled, it uses a dice expression defined in the template to select
an entry, applies a label, and automatically updates associated
modifier stats using relative values.
This commit is contained in:
2026-07-09 10:26:54 +08:00
parent 19c66640b5
commit 1c69bf394c
10 changed files with 384 additions and 15 deletions
+39
View File
@@ -37,6 +37,7 @@ props:
| `enum` | 枚举选项,掷骰随机选择 | ✅ |
| `modifier` | 修饰值,自动加到 `target` 属性上 | ❌ |
| `derived` | 通过公式从其他属性计算 | ✅ |
| `template` | 查表属性,掷骰匹配范围并应用修饰符 | ✅ |
## 属性定义语法
@@ -229,6 +230,44 @@ weather,天气,enum,晴天\|阴天\|雨天\|暴风雨
公式中引用其他属性时,会自动使用其计算值(包含修饰符)。
支持的函数:`floor(x)`, `ceil(x)`, `round(x)`。
## 模板属性(template)详解
模板属性用于查表掷骰,例如年龄表、职业表等。
模板在独立的 CSV 代码块中定义,使用 `role=stat-template` 和 `file=模板名`。
**第一列是骰子表达式**(如 `1d10`),第二列是 `label`,其余列为修饰符:
```csv role=stat-template file=年龄
1d10,label,heart,mind,strength,speed
1-3,青少年,+20,-10,,
4-7,成年,,-10,,+20
8-9,老年,,+20,-10,
10,换躯者,,,+30,-10
```
- **第一列(header)**:骰子表达式,如 `1d10`、`2d6`
- **第一列(rows)**:匹配范围,支持 `1-3`(区间)、`4`(精确)、`1-3,5`(多个)
- **`label` 列**:显示名称
- **其余列**:修饰符键名,值为变化量(正数加、负数减、空表示不修改)
然后在属性定义中引用模板:
```yaml role=stat
- key: age
scope: player
label: 年龄
type: template
template: 年龄
```
`/stat roll age` 时:
1. 掷 `1d10`(模板头部的骰子表达式)
2. 匹配第一列获取条目
3. 发布 `set age=青少年`
4. 同时发布 `set alice:heart=52`(原始值 +20)、`set alice:mind=22`-10)等
修饰符值中的 `+`/`-` 前缀表示相对调整。如果 modifiers 值是 `+20`,会在当前值基础上加 20;如果是 `-10`,会减 10。
## 掷骰公式中的属性引用
`roll` 字段中的标识符会自动替换为当前属性值: