Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/dev/ssot-registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@
- "## playtest_hypotheses"
- "## related_tests"

- id: feature-sortie
path: docs/product/features/sortie.md
title: Sortie Lifecycle & Combat End Conditions
keywords: [sortie, lifecycle, fsm, state-machine, timer, elapsedTicks, targetTicks, fixed_timestep, result-object, victory, defeat, combat, end-conditions, transient-output]
description: 戦闘(Sortie)の開始から終了(120秒生存または自機HP0)までの状態遷移、固定タイムステップタイマーの管理、および Transient Result Object の生成ルールの正本。永続化やUI表示に関する定義は持たない。
sections:
- "## Sortie State Machine (FSM)"
- "## End Conditions"
- "## Timer Authority"
- "## SortieResult"
- "## Non-Goals"
- "## planned_consumer_tests"

- id: game-overview
path: docs/product/game-overview.md
title: ゲーム全体像
Expand Down
254 changes: 254 additions & 0 deletions docs/product/features/sortie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
---
doc_id: feature-sortie
status: draft
parent_issue: "#483"
trace_links:
self_issue: "#485"
parent_issue: "#483"
sibling_specs:
- "#484"
implementation_consumers:
- "#489"
upstream_implementation:
- "#486"
- "#487"
- "#488"
validation:
- "#490"
existing_specs:
- docs/product/features/movement-projectile.md
- docs/product/features/combat-core.md
- docs/product/game-logic.md
- docs/adr/0001-architecture-baseline.md
---

# Sortie Lifecycle & Combat End Conditions

## Intent

`docs/product/features/sortie.md` は、1 回の戦闘(Sortie)の開始から終了(120秒生存または自機HP0)までの状態遷移、固定タイムステップタイマーの管理、および Transient Result Object の生成ルールを定義する正本である。永続化・UIレイアウト・upgrade・campaign への定義は持たない。

---

## Authority / Conflict Resolution

本 spec は M2 Combat MVP ゲートのために `docs/product/game-logic.md` の該当範囲を **refine and override** する。

> For M2 Combat MVP, this document refines and overrides `docs/product/game-logic.md`
> for sortie lifecycle, timer-based victory, and SortieResult persistence semantics.

具体的な override 範囲:

- `elapsedTicks >= targetTicks` が M2 における唯一の victory 条件。
`REQ-LOGIC-VICTORY-001` の "outpost destruction" / "all enemies destroyed" は M2 スコープ外に据え置き。
- SortieResult は transient output であり MUST NOT be persisted in M2.
`REQ-LOGIC-PERSISTENCE-001` の "snapshot at debrief" / "campaign state" は M2 スコープ外に据え置き。
- Resource conversion、debrief rewards、progression、upgrade persistence は後続 Issue に委譲。

これにより、#489 実装者は `game-logic.md` の BattleState(`combat -> debrief` 遷移、outpost victory、storage snapshot)を M2 では実装しないことを明示的に確認できる。

---

## Sortie State Machine (FSM)

### States

| State | Role |
|-----------|----------------------------------------------|
| `idle` | 戦闘未開始(初期状態) |
| `running` | 戦闘進行中。elapsedTicks が加算される |
| `victory` | Result-latched state(生存達成) |
| `defeat` | Result-latched state(自機HP0) |
| `ended` | Post-result acknowledgement state(結果確認後)|

**重要**: `victory` と `defeat` は **result-latched states であり、final FSM states ではない**。
これらの状態は戦闘シミュレーションを停止させ、immutable な SortieResult を 1 度だけ生成する。
`ended` が final state であり、`ACK_RESULT` イベントにより到達する。

```
`victory` and `defeat` are result-latched states, not final FSM states.
They stop combat simulation and expose exactly one immutable SortieResult.
`ended` is the acknowledged post-result state and is the true terminal state.
```

### Normative Transition Table

| From | Event / Guard | To | Notes |
|---------------|-------------------------------------------------|-----------|-------------------------------------------|
| `idle` | `START_SORTIE` | `running` | 戦闘開始。elapsedTicks=0 にリセット |
| `running` | `FIXED_TICK [playerHp <= 0]` | `defeat` | defeat が victory より優先(同一 tick 時も)|
| `running` | `FIXED_TICK [elapsedTicks >= targetTicks]` | `victory` | playerHp > 0 の場合のみ到達 |
| `victory` | `ACK_RESULT` | `ended` | 結果確認。result は変更されない |
| `defeat` | `ACK_RESULT` | `ended` | 結果確認。result は変更されない |

---

## End Conditions

### Victory

- Guard: `elapsedTicks >= targetTicks`(かつ `playerHp > 0`)
- 意味: 120秒相当の固定ティック数を playerHp > 0 の状態で生存した

### Defeat

- Guard: `playerHp <= 0`
- 意味: 自機の HP が 0 以下になった

### 同一 tick での両条件成立時の裁定

同一 `FIXED_TICK` で `playerHp <= 0` と `elapsedTicks >= targetTicks` が同時に成立した場合、**defeat が優先**される。

理由: 最終ティックで自機が被弾して HP が 0 になった場合、プレイヤーは生存しておらず victory 条件を充足していない("player did not survive the terminal tick")。

### Normative Per-Tick Evaluation Order

sortie.status == "running" の各 `FIXED_TICK` において、以下の順序で処理する:

```
For each FIXED_TICK while sortie.status == "running":

1. Run movement / projectile / enemy AI / collision / combat systems for this tick.
2. Apply all HP mutations and enemy defeat mutations produced by step 1.
3. Increment elapsedTicks by 1.
4. Evaluate terminal guards in this priority:
a. if player.hp <= 0 -> latch defeat (generate SortieResult, transition to defeat)
b. else if elapsedTicks >= targetTicks -> latch victory (generate SortieResult, transition to victory)
5. Once a result is latched (sortie.status != "running"):
- combat, projectile, enemy AI, spawn, and collision systems MUST NOT mutate state
on subsequent ticks.
```

この順序は一意に定義される。実装者は上記以外の順序でステップを実行しない。

---

## Timer Authority

### elapsedTicks の加算規則

- `elapsedTicks` は `running` 状態の fixed simulation steps 内でのみ加算される(Per-Tick Evaluation Order の step 3)。
- `idle`、`victory`、`defeat`、`ended` の各状態では加算されない。

### targetTicks の算出

```
targetTicks = ceil(120_000 / fixedDeltaMs)
```

- `fixedDeltaMs`: 固定タイムステップの 1 ステップ当たりのミリ秒数(アーキテクチャ定数、ADR 0001)
- 120_000 ms = 120秒

### durationMs の算出

```
durationMs = elapsedTicks * fixedDeltaMs
```

`durationMs` は表示・互換用の導出フィールドである。勝利条件の主キーは `elapsedTicks` であり、`durationMs` は `elapsedTicks * fixedDeltaMs` から算出する。

### wall-clock / rAF の利用範囲

**Outer render/infrastructure loop での利用(MAY):**

```
requestAnimationFrame, Date.now(), and performance.now() MAY be used by the
outer render/infrastructure loop to produce frame deltas for the SimulationLoop accumulator.
```

これは既存の `SimulationLoop` 設計(`fixedDeltaMs` ごとに step を実行)と整合する。

**SortieResult / sortie duration 計測での利用(MUST NOT):**

```
They MUST NOT be used to:
- increment sortie elapsed time (elapsedTicks),
- decide victory or defeat,
- populate SortieResult.durationMs,
- persist or compare sortie outcome duration.

The only authoritative duration source is the number of executed fixed ticks (elapsedTicks).
```

理由: これらはシミュレーション外の時間経過(pause、フレームスキップ、パニック処理、背景タブによる rAF 停止)を含むため、再現性と決定論性が保証されない。

### SimulationLoop の panic / maxFrameSkip 破棄時間

SimulationLoop の panic や maxFrameSkip により破棄されたフレームの時間は `durationMs` に含めない。`elapsedTicks` が `running` 状態の固定ステップ内でのみ加算されることにより、この要件は自動的に満たされる。

---

## SortieResult

### Shape

```typescript
interface SortieResult {
readonly outcome: "victory" | "defeat";
readonly durationMs: number;
readonly kills: number;
readonly shotsFired: number;
readonly playerHpRemaining: number;
}
```

### 不変条件

| 不変条件 | 規則 |
|----------|------|
| 生成回数 | result は `running` → `victory` または `running` → `defeat` への初回遷移で一度だけ生成される |
| immutability | result は生成後 MUST NOT be mutated(再生成・上書き禁止) |
| `kills` 算出元 | `defeatedAtTick <= terminalTick` の敵から導出される(terminal tick 以前に defeated 状態になった敵) |
| `shotsFired` | terminal tick 時点の snapshot(遷移後の変更を反映しない) |
| `playerHpRemaining` clamp | `[0, player.maxHp]` にクランプされる |
| defeat 時の HP | `defeat` 時: `playerHpRemaining === 0` |
| 永続化禁止 | result を persistence / resources / upgrades / localStorage / campaign への書き込みに使用しない |

### Transient Output

SortieResult は transient output として定義される。永続化は本 spec のスコープ外。

---

## Non-Goals

本 feature spec が定義しないもの:

- **Persistence**: SortieResult の localStorage への保存、セッションをまたぐ結果の保持
- **Upgrade / Progression**: 戦闘結果によるリソース獲得、アップグレードロジック
- **Campaign**: キャンペーンモード、ステージ進行、マップ遷移
- **Briefing UI**: 戦闘前ブリーフィング、結果表示 UI のレイアウト定義
- **Outpost destruction / all-enemy-destroyed victory**: M2 スコープ外(game-logic.md 経由で後続 Issue へ)

---

## Related Tests

Planned:

```yaml
- path: tests/sortie-system.test.ts
covers:
- transition: idle -> running
- victory at target tick
- defeat when player HP reaches 0
- same-tick defeat precedence (defeat wins)
- SortieResult generated exactly once
- no combat mutation after terminal result
- timer authority uses executed fixed ticks, not wall clock
```

---

## Related

- `docs/product/features/movement-projectile.md`: player 移動・発射の最小仕様
- `docs/product/features/combat-core.md`: CollisionSystem / CombatSystem contract(#484)
- `docs/product/game-logic.md`: ゲームロジック全体仕様(本 spec が M2 範囲を override)
- `docs/adr/0001-architecture-baseline.md`: 60Hz 固定タイムステップ・state/systems 分離
- Issue #485: この仕様の起票 Issue
- Issue #483: parent(M2 Combat MVP Gate)
- Issue #484: sibling spec(combat-core)
- Issue #486, #487, #488: upstream implementation
- Issue #489: implementation consumer(sortie-system 実装・テスト)
- Issue #490: validation
Loading