---
title: "ルールとリアクション"
description: "ワールドファイルのビヘイビアオブジェクト。when・conditions・then を持つリアクション、旧形式の rules、トリガー、演算子、効果。"
---

# ルールとリアクション

ルールはイベントに基づいてターン間に自動的に発火します。AIの関与なしに精密なメカニクスを処理します。

## ルールスキーマ

```json
{
  "id": "low-health-warning",
  "name": "Low Health Warning",
  "description": "Warn the player when health is critical",
  "trigger": {
    "type": "variable-crossed",
    "variableId": "health",
    "direction": "drops-below",
    "threshold": 20
  },
  "conditions": [],
  "conditionLogic": "all",
  "actions": [
    {
      "type": "notify-player",
      "style": "warning",
      "message": "Your vision blurs. You're barely standing."
    },
    {
      "type": "inject-directive",
      "directiveId": "critical-health",
      "content": "The player is near death. Describe their physical deterioration — stumbling, blurred vision, trembling hands.",
      "position": "after_char",
      "persistent": true
    }
  ],
  "priority": 50,
  "enabled": true,
  "cooldownTurns": null,
  "maxFireCount": null
}
```

## トリガータイプ

| タイプ | フィールド | 発火するとき |
|------|--------|-----------|
| `variable-crossed` | `variableId`、`direction`(`rises-above` \| `drops-below`)、`threshold` | 変数がしきい値を越える |
| `state-change` | `variableId`(任意) | 任意の変数が変化(または特定の変数) |
| `turn-count` | `atTurn`(number)、`everyNTurns`(number) | 特定のターン番号、またはNターンごと |
| `session-start` | — | 新しいセッションの最初のメッセージ |
| `keyword` | `keywords[]` | プレイヤーメッセージに任意のキーワードが含まれる |
| `ai-keyword` | `keywords[]` | AI応答に任意のキーワードが含まれる |
| `every-turn` | — | 毎ターンの後 |
| `action` | `actionId`(string) | 特定のアクションが実行される(例: カスタムUIボタンから) |
| `manual` | — | 他のルールによってトリガーされたときだけ発火 |

## 条件

ルールが発火するためにパスしなければならない、任意の状態チェック:

```json
"conditions": [
  {
    "variableId": "story-phase",
    "operator": "eq",
    "value": "act2"
  },
  {
    "variableId": "health",
    "operator": "gt",
    "value": 0
  }
]
```

演算子: `eq`、`neq`、`gt`、`lt`、`gte`、`lte`、`contains`

`conditionLogic`: `"all"`(AND)または `"any"`(OR)

## アクションタイプ

### `modify-variable`
変数値を変更。
```json
{
  "type": "modify-variable",
  "variableId": "hunger",
  "operation": "subtract",
  "value": 5
}
```
操作: `set`、`add`、`subtract`、`multiply`、`toggle`、`append`、`merge`、`push`、`delete`

### `inject-directive`
将来のターンに向けてAIのプロンプトに指示を追加。
```json
{
  "type": "inject-directive",
  "directiveId": "romance-mode",
  "content": "The character has developed feelings. Write romantic tension naturally.",
  "position": "after_char",
  "persistent": true
}
```
位置: `top`、`before_char`、`after_char`、`auto`、`depth`、`bottom`

`persistent: true` は明示的に削除されるまでディレクティブをアクティブに保ちます。`persistent: false`(デフォルト)= 1回限り、1ターン後に削除。

`duration`: 任意の数値。設定された場合、ディレクティブはNターン後に自動削除されます。

### `remove-directive`
以前に注入したディレクティブを削除。
```json
{
  "type": "remove-directive",
  "directiveId": "romance-mode"
}
```

### `notify-player`
トースト通知を表示。
```json
{
  "type": "notify-player",
  "style": "warning",
  "message": "You're running low on supplies."
}
```
スタイル: `info`、`achievement`、`warning`、`danger`

### `play-audio`
オーディオトラックをトリガー。
```json
{
  "type": "play-audio",
  "trackId": "bgm-battle",
  "action": "play"
}
```

### `toggle-entry`
ロアブックエントリを有効化または無効化。
```json
{
  "type": "toggle-entry",
  "entryId": "secret-lore",
  "enabled": true
}
```

### `toggle-rule`
別のルールを有効化または無効化(連鎖反応)。
```json
{
  "type": "toggle-rule",
  "ruleId": "phase-2-triggers",
  "enabled": true
}
```

### `send-context`
次のターンでAIに1回限りのコンテキストメッセージを送信。
```json
{
  "type": "send-context",
  "message": "The player just triggered a hidden event. React accordingly.",
  "role": "system"
}
```
`role`: `"system"`(デフォルト)または `"user"`。コンテキストがどう会話に注入されるかを決定。

## ルールのオプション

| フィールド | タイプ | 説明 |
|-------|------|-------------|
| `priority` | number | 高い方が先に発火(複数ルールがトリガーされたとき)(デフォルト: 0) |
| `cooldownTurns` | number \| null | 発火間の最小ターン数 |
| `maxFireCount` | number \| null | このルールが発火できる総回数(null = 無制限) |
| `enabled` | boolean | 他のルールから切り替え可能 |

## リアクション(イベントパターンシステム)

リアクションは、汎用イベントパターンに基づくより新しく柔軟なルールシステムです。

```json
{
  "id": "zone-enter-forest",
  "name": "Enter Forest",
  "when": {
    "eventType": "spatial:zone-enter",
    "zone": "forest"
  },
  "conditions": [],
  "conditionLogic": "all",
  "then": [
    {
      "type": "set",
      "path": "weather",
      "value": "foggy"
    },
    {
      "type": "set",
      "path": "@audio.ambient",
      "value": "forest-ambient",
      "operation": "set"
    },
    {
      "type": "emit",
      "event": { "type": "spatial:ambience-changed" }
    }
  ]
}
```

### システムエフェクトパス

リアクションは `@` パスでシステム機能をターゲットにできます。

| パス | エフェクト |
|------|--------|
| `@audio.bgm` | BGMトラックを再生 |
| `@audio.sfx` | SFXを再生 |
| `@audio.ambient` | アンビエントトラックを再生 |
| `@audio.stop` | トラックを停止 |
| `@prompt.directive.<id>` | ディレクティブを注入/削除 |
| `@prompt.entry.<id>` | エントリの表示を切り替え |
| `@prompt.context` | 1回限りのコンテキストメッセージ |
| `@rules.disabled.<id>` | ルール有効/無効を切り替え |
| `@ui.notification` | トースト通知を表示 |
| `@ai.request` | AI生成をトリガー |
| `@ai.context` | 次のAIメッセージにコンテキストを追加 |

## 毎ターン生成されるイベント

AIが応答した後、これらのイベントが発火され、すべてのルール/リアクションに対してチェックされます。

1. `message:user` — プレイヤーのメッセージ(キーワードマッチング用のコンテンツ付き)
2. `message:ai` — AIの応答(キーワードマッチング用のコンテンツ付き)
3. `turn:complete` — ターン終了(ターン数付き)
4. `state:changed` — 変更された変数ごとに1つ(variableId、oldValue、newValue付き)
