Compare commits

..
19 Commits
Author SHA1 Message Date
jpmschweitzerandClaude Opus 4.6 615c3a9c17 chore(meta): release v0.1.20
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 23:52:38 +01:00
jpmschweitzerandClaude Opus 4.6 c8a0f1b327 chore(db): backup database after sprint 20 merges
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 23:51:51 +01:00
jpmschweitzer 3c634a937d Merge remote-tracking branch 'origin/server' 2026-02-25 23:50:56 +01:00
jpmschweitzerandClaude Opus 4.6 4ef1157b79 chore(meta): update changelog
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 23:35:35 +01:00
jpmschweitzerandClaude Opus 4.6 d808d95659 refactor(simulation): address PR #72 review suggestions
- Hoshe #3: replace O(n²) Vec scan in fallback NPC assignment with
  BTreeSet; prevent same NPC assigned to two roles in one triangle
- Hoshe #4: add From impls for RoleId, TriangleId, StableId, and
  TriangleCrisisEventWire — eliminate fragile .0 access on newtypes
- Hoshe #5: consolidate near-identical unit tests with integration
  counterparts — keep only unique tests in #[cfg(test)] module
- Tyre #3: replace O(N*M) scan in apply_resolve_triangle with
  BTreeMap<TriangleId, Entity> index for O(1) per-command lookup
- Tyre #4: document &mut World on generate_intra_template_triangles
- Observer snapshot uses TriangleCrisisEventWire::from instead of
  manual field mapping

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 23:35:23 +01:00
jpmschweitzerandClaude Opus 4.6 3a555a2eeb fix(simulation): validate dangling with_role in TriangleDef constraints
TriangleDef.validate() now rejects relationship constraints where
with_role references a role not in the triangle's three roles.
Catches authoring errors at load time instead of silently producing
broken constraint data.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:48:23 +01:00
jpmschweitzerandClaude Opus 4.6 1ea3d9c724 fix(simulation): persist TriangleState in SaveStateV1
Triangle phase and tension were silently lost on save/load. Now
serialized as triangle_states vec in SaveStateV1, sorted by
triangle_id for determinism (D-010). Dedicated triangle entities
are respawned on load.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:48:18 +01:00
jpmschweitzerandClaude Opus 4.6 9bc5925220 fix(simulation): wire triangle crisis event queue into observer snapshot
The TriangleCrisisEventQueue was populated by tick_triangle_escalation
but never drained into ObserverSnapshot — clients always saw an empty
vec despite protocol v16 advertising the field. Now drains the queue
each tick and converts to TriangleCrisisEventWire.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:48:11 +01:00
jpmschweitzerandClaude Opus 4.6 d34fc970a7 feat(simulation): add triangle escalation integration tests
16 tests covering escalation timing, crisis event emission,
resolve command, D-087 seed-dependent timing, D-089 no-cascade,
and edge cases (dormant skip, saturation, idempotent resolve).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:34:27 +01:00
jpmschweitzerandClaude Opus 4.6 918dffc643 chore(meta): update changelog
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:34:05 +01:00
jpmschweitzerandClaude Opus 4.6 89158f2f2a chore(simulation): regenerate msgpack fixtures for protocol v16
Updates all client-side msgpack test fixtures and server test
harnesses to include the new triangle_crisis_events field added
in protocol v16.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:33:32 +01:00
jpmschweitzerandClaude Opus 4.6 c436ae905f feat(simulation): wire triangle escalation and crisis events into protocol
Registers tick_triangle_escalation and apply_resolve_triangle systems
in SimulationPlugin. Adds TriangleCrisisEventWire to ObserverSnapshot
(protocol v16) for future client rendering of triangle crises (#250,
D-087). Observer emits empty vec by default; escalation system will
populate when triangles reach Active phase.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:33:21 +01:00
jpmschweitzerandClaude Opus 4.6 9eaca0b5a4 feat(simulation): persist template ownership and references in save state
Adds TemplateOwnership to NpcSaveState and TemplateReferenceMap to
SaveStateV1 so cross-template links survive save/load and tier
eviction (D-025, D-026). Both fields use serde(default) for backward
compatibility with existing saves.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:33:13 +01:00
jpmschweitzerandClaude Opus 4.6 0e8ac56fc1 feat(simulation): add social site template schema types
Implements #163 (RoleSchema), #164 (SpaceSpec), #165 (TemplateOwnership
+ TemplateReferenceMap), #106 (TriangleDef), #107 (intra-template
triangle generation), and #250 (triangle escalation system) as the
foundational Tier 2 template system per D-025.

New content/template module with YAML-deserializable schema types,
ECS components for ownership/triangle state, escalation system
running on game-minute boundaries, and TriangleCrisisEvent emission.
Sample YAML templates at server/data/templates/.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:33:06 +01:00
jpmschweitzerandClaude Opus 4.6 98100da0d0 chore(meta): update changelog
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:00:23 +01:00
jpmschweitzerandClaude Opus 4.6 de136fc1a5 refactor(client): unify duplicate YAML parsers into YamlParser (#560)
Extract shared YamlParser utility (client/scripts/util/yaml_parser.gd)
with parse() for nested typed dicts and parse_flat() for dotted-key
string format. UIStrings._parse_yaml() and ChecklistEvaluator's inline
parser both delegate to YamlParser, removing ~140 lines of duplication.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 22:00:05 +01:00
jpmschweitzerandClaude Opus 4.6 964bb9c459 refactor(client): extract SnapshotEventRouter from main.gd (#559)
New SnapshotEventRouter class (46 lines) provides callable-based
snapshot dispatch via register(), register_always(), and dispatch().
main.gd _process() now calls _router.dispatch(snapshot) instead of
15+ inline if-has blocks. Handlers registered in _ready().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 21:59:56 +01:00
jpmschweitzerandClaude Opus 4.6 6f9c97ca03 refactor(client): decouple dialogue_box from GameState and AudioManager (#558)
Replace 3 direct GameState.dialogue_active mutations and all
AudioManager.apply_dip/clear_dip calls with signals:
dialogue_state_changed, audio_dip_requested, audio_dip_cleared.
dialogue_box.gd now has zero references to GameState or AudioManager.
main.gd wires coordinator handlers in _ready() (D-020).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 21:59:47 +01:00
jpmschweitzerandClaude Opus 4.6 c8de1a0629 refactor(client): make stationary_ticks and zone_id server-authoritative (#557)
apply_snapshot() now reads stationary_ticks and zone_id directly from
the server snapshot when present (D-020 compliance). Client-side
accumulation and tile lookup retained as deprecated fallbacks until
the server populates these fields. Protocol.gd extended with decode
paths and TODO markers for the server team.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 21:59:38 +01:00
54 changed files with 4039 additions and 272 deletions
+22
View File
@@ -6,14 +6,36 @@ Format based on [Keep a Changelog](https://keepachangelog.com/).
## [Unreleased]
## [v0.1.20] — 2026-02-25
### Added
- Social site template schema — RoleSchema (#163), SpaceSpec (#164), TriangleDef (#106) with YAML deserialization, sample templates at server/data/templates/
- Single-ownership model — TemplateOwnership component, TemplateReferenceMap resource, cross-template reference links preserved across save/load and tier eviction (#165, D-025)
- Triangle generation — intra-template constraint satisfaction assigns NPCs to triangle roles, minimum 2 triangles per template with fallback on imperfect seeds (#107)
- Triangle escalation system — tick_triangle_escalation runs per game-minute, tension increments toward ToleranceThreshold, TriangleCrisisEvent emitted on Active phase entry, ResolveTriangle stub command (#250, D-087)
- Protocol v16 — TriangleCrisisEventWire on ObserverSnapshot for future client rendering of triangle crises
- D-093: Sova Transit District spatial layout — 4 social sites (Terminal, Bar, Gate Cluster, Sector 3), 2 encounter nodes, zone palette, gate cluster 7-zone spec, z-level scheme (z=0 maintenance, z=1 main, z=2 observation gallery), 3 investigation paths, corridor widths
- D-094: Spatial hierarchy — chunk (64×64 sim) → block (128×128 sim) → district (4×4 blocks, 256×256 visual), supersedes D-014 estimate
- D-095: Horizon stations and transport lore — span gates (human-built, dual-use), horizon stations (alien-built, 4-8 apertures), "The Ring" per-system naming, sequential hop travel, The Loop internal tram
- Generator architecture workshop brief (ticket #562) — top-down pipeline for district generation, targeting Q-036 resolution
- SnapshotEventRouter — callable-based snapshot dispatch replaces inline if-has blocks in main.gd (#559)
- YamlParser shared utility — unified YAML parsing for UI strings and checklist conditions (#560)
### Fixed
- Wire triangle crisis event queue into observer snapshot — clients now receive TriangleCrisisEventWire via protocol v16 (was always empty)
- Persist TriangleState in SaveStateV1 — triangle phase and tension survive save/load cycles
- Validate dangling with_role references in TriangleDef constraint validation
- Replace O(n²) fallback NPC assignment with BTreeSet; prevent same NPC assigned to two roles in one triangle
- Replace O(N*M) scan in apply_resolve_triangle with BTreeMap index for O(1) per-command lookup
- Add From impls for RoleId, TriangleId, StableId, TriangleCrisisEventWire — eliminate fragile .0 newtype access
- Consolidate near-identical unit tests with integration counterparts
### Changed
- Sova station profile updated — horizon gates located at The Krenn Ring (800 AU), not on Station Sova; Admin Hub houses transit processing facility only
- game_state.gd: stationary_ticks and zone_id now read from server snapshot with deprecated client-side fallbacks (#557, D-020)
- dialogue_box.gd: decoupled from GameState and AudioManager via signals — zero direct autoload references (#558, D-020)
- main.gd: snapshot dispatch via SnapshotEventRouter, dialogue signal coordinator handlers (#559, #558)
- ui_strings.gd and checklist_evaluator.gd: delegate to YamlParser, ~140 lines of duplication removed (#560)
## [v0.1.19] — 2026-02-25
+36 -18
View File
@@ -109,14 +109,17 @@ var medium_sound_events: Array = []
var close_sound_events: Array = []
# D-071 (#530): Consecutive ticks without player position change.
# Incremented per snapshot in apply_snapshot(). Reset to 0 on movement.
# D-020: Server-authoritative — read from snapshot "stationary_ticks" field.
# Fallback: client-side accumulation (deprecated, remove when server populates field).
# ListeningFocus boost activates at 30+ ticks (main.gd manages the dip).
var stationary_ticks: int = 0
# DEPRECATED: Only used by client-side accumulation fallback. Remove with fallback.
var _prev_player_position: Vector2 = Vector2(-1e9, -1e9) # sentinel: no previous position
# D-073 (#529): Server-authoritative zone_id from the player's current tile.
# Extracted in apply_snapshot() — avoids O(N) tile scan in main.gd per Tyre review.
# Empty string when zone_id field absent (server hasn't shipped OQ-09 yet).
# D-020: Read directly from snapshot "zone_id" field.
# Fallback: client-side tile lookup (deprecated, remove when server populates field).
# Empty string when zone_id field absent.
var current_zone_id: String = ""
func apply_snapshot(snapshot: Dictionary) -> void:
@@ -140,12 +143,19 @@ func apply_snapshot(snapshot: Dictionary) -> void:
push_warning("GameState: no Player entity found in %d entities" % [
visible_entities.size()])
# D-071 (#530): Track consecutive stationary ticks for ListeningFocus boost.
# Compares current player_position against previous snapshot's position.
if player_position == _prev_player_position:
stationary_ticks += 1
# D-020/D-071 (#530): Server-authoritative stationary_ticks for ListeningFocus boost.
# Prefer server-sent value; fall back to client-side accumulation until server populates.
if snapshot.has("stationary_ticks") and snapshot.stationary_ticks is int:
# D-020: direct field assignment from server-authoritative snapshot.
stationary_ticks = snapshot.stationary_ticks
else:
stationary_ticks = 0
# DEPRECATED fallback — client-side accumulation. Remove when server sends
# "stationary_ticks" in ObserverSnapshot (D-020 violation: derives behavior-
# driving state on the client). Server tracks this in ListeningFocus component.
if player_position == _prev_player_position:
stationary_ticks += 1
else:
stationary_ticks = 0
_prev_player_position = player_position
# Tiles for rendering: test mode sends "tiles", live server sends tile data in "visible_tiles"
@@ -295,16 +305,24 @@ func apply_snapshot(snapshot: Dictionary) -> void:
if snapshot.has("player_knowledge") and snapshot.player_knowledge is Dictionary:
player_knowledge = snapshot.player_knowledge
# D-073 (#529): O(1) zone_id lookup. Build coord→tile dict from member visible_tiles
# (populated above from either "tiles" test-mode key or "visible_tiles" live key).
# Must use the member var, not snapshot.visible_tiles, so test mode is covered.
var _tile_by_coord: Dictionary = {}
for vtile in visible_tiles:
if vtile is Dictionary and vtile.has("x") and vtile.has("y"):
_tile_by_coord[Vector2i(vtile.x, vtile.y)] = vtile
var player_pos_key := Vector2i(int(player_position.x), int(player_position.y))
var player_tile = _tile_by_coord.get(player_pos_key, null)
current_zone_id = player_tile.get("zone_id", "") if player_tile else ""
# D-020/D-073 (#529): Server-authoritative zone_id for zone ambient crossfade.
# Prefer server-sent top-level value; fall back to client-side tile lookup until
# server populates top-level "zone_id" in ObserverSnapshot.
if snapshot.has("zone_id") and snapshot.zone_id is String:
# D-020: direct field assignment from server-authoritative snapshot.
current_zone_id = snapshot.zone_id
else:
# DEPRECATED fallback — client-side tile lookup. Remove when server sends
# top-level "zone_id" in ObserverSnapshot (D-020 violation: derives zone
# identity on the client via tile iteration). Server sends zone_id per
# VisibleTile but not as a top-level snapshot field.
var _tile_by_coord: Dictionary = {}
for vtile in visible_tiles:
if vtile is Dictionary and vtile.has("x") and vtile.has("y"):
_tile_by_coord[Vector2i(vtile.x, vtile.y)] = vtile
var player_pos_key := Vector2i(int(player_position.x), int(player_position.y))
var player_tile = _tile_by_coord.get(player_pos_key, null)
current_zone_id = player_tile.get("zone_id", "") if player_tile else ""
# v2: visible_tiles with visibility sectors
# Derives visible_positions when not explicitly provided (real server mode)
+2 -40
View File
@@ -49,44 +49,6 @@ func reload() -> void:
## Parse YAML with arbitrary nesting depth.
## Returns flat Dictionary with dotted keys: { "section.sub.key": "value" }.
## Delegates to YamlParser.parse_flat() (#560).
static func _parse_yaml(text: String) -> Dictionary:
var strings := {}
var stack: Array = [] # [[indent, key], ...]
for line in text.split("\n"):
var stripped := line.strip_edges(false, true)
if stripped.is_empty() or stripped.begins_with("#"):
continue
var indent := line.length() - line.lstrip(" ").length()
var content := stripped.strip_edges()
var colon_pos := content.find(":")
if colon_pos < 0:
continue
var key := content.substr(0, colon_pos).strip_edges()
var val := content.substr(colon_pos + 1).strip_edges()
# Trailing comment without a value — treat as section header
if val.begins_with("#"):
val = ""
# Pop sections at same or deeper indent
while stack.size() > 0 and stack.back()[0] >= indent:
stack.pop_back()
if val.is_empty():
# Section header — push onto stack
stack.push_back([indent, key])
else:
# Leaf value — extract from quotes or strip inline comment
if val.begins_with("\""):
var end_quote := val.find("\"", 1)
if end_quote > 0:
val = val.substr(1, end_quote - 1)
else:
val = val.substr(1)
else:
var comment_pos := val.find(" #")
if comment_pos >= 0:
val = val.substr(0, comment_pos).strip_edges()
var dotted_key := ""
for entry in stack:
dotted_key += entry[1] + "."
dotted_key += key
strings[dotted_key] = val
return strings
return YamlParser.parse_flat(text)
+3 -105
View File
@@ -233,12 +233,7 @@ func _find_entity(entity_id: int) -> bool:
return false
# -- YAML parsing (checklist-specific) -----------------------------------------
# Handles the constrained checklist YAML format: top-level key:value pairs,
# a conditions array of flat dictionaries. No nested arrays or anchors.
#
# Limitation: unquoted values containing " #" are truncated at the comment marker.
# Use quoted strings ("value # with hash") if values must contain literal hashes.
# -- YAML parsing --------------------------------------------------------------
func _load_checklist_file(path: String) -> Dictionary:
if not FileAccess.file_exists(path):
@@ -252,103 +247,6 @@ func _load_checklist_file(path: String) -> Dictionary:
return parse_checklist_yaml(text)
## Delegates to YamlParser.parse() (#560).
static func parse_checklist_yaml(text: String) -> Dictionary:
var result := {}
var conditions: Array = []
var current_item: Dictionary = {}
var in_conditions := false
for line in text.split("\n"):
var stripped := line.strip_edges(false, true)
if stripped.is_empty() or stripped.strip_edges().begins_with("#"):
continue
var indent := line.length() - line.lstrip(" ").length()
var content := stripped.strip_edges()
# Detect conditions: array header
if content == "conditions:":
in_conditions = true
continue
if not in_conditions:
# Top-level key: value
var colon := content.find(":")
if colon >= 0:
var key := content.substr(0, colon).strip_edges()
var val_str := content.substr(colon + 1).strip_edges()
result[key] = _parse_value(val_str)
else:
if content.begins_with("- "):
# New array item — flush previous
if not current_item.is_empty():
conditions.append(current_item)
current_item = {}
var rest := content.substr(2).strip_edges()
var colon := rest.find(":")
if colon >= 0:
var key := rest.substr(0, colon).strip_edges()
var val_str := rest.substr(colon + 1).strip_edges()
current_item[key] = _parse_value(val_str)
elif indent >= 2 and not current_item.is_empty():
# Continuation of current array item
var colon := content.find(":")
if colon >= 0:
var key := content.substr(0, colon).strip_edges()
var val_str := content.substr(colon + 1).strip_edges()
current_item[key] = _parse_value(val_str)
elif indent == 0:
# Back to top level — shouldn't happen in valid checklist YAML
in_conditions = false
if not current_item.is_empty():
conditions.append(current_item)
current_item = {}
var colon := content.find(":")
if colon >= 0:
var key := content.substr(0, colon).strip_edges()
var val_str := content.substr(colon + 1).strip_edges()
result[key] = _parse_value(val_str)
# Flush last item
if not current_item.is_empty():
conditions.append(current_item)
if not conditions.is_empty():
result["conditions"] = conditions
return result
static func _parse_value(val: String) -> Variant:
if val.is_empty():
return ""
# Strip inline comments (not inside quotes)
if not val.begins_with("\""):
var comment_pos := val.find(" #")
if comment_pos >= 0:
val = val.substr(0, comment_pos).strip_edges()
# Quoted string
if val.begins_with("\""):
var end_quote := val.find("\"", 1)
if end_quote > 0:
return val.substr(1, end_quote - 1)
return val.substr(1)
# Boolean
if val == "true":
return true
if val == "false":
return false
# Float (contains decimal point)
if val.contains(".") and val.is_valid_float():
return val.to_float()
# Integer
if val.is_valid_int():
return val.to_int()
# Plain string
return val
return YamlParser.parse(text)
+86 -86
View File
@@ -33,6 +33,7 @@ var _flash_rect: ColorRect = null # #502/#501: ephemeral screen flash overlay (
var _teleport_in_progress: bool = false # #501/#117: forces camera snap (not lerp) on next _process frame
var _pending_record_inputs: Array = [] # #507: accumulates server-bound inputs across frames; flushed into record_tick() on snapshot arrival
var _current_zone: String = "" # D-073 (#529): zone tracking for ambient crossfades
var _router: SnapshotEventRouter # #559: callable-based snapshot dispatch
const LISTENING_FOCUS_TICKS: int = 30 # D-071: stationary ticks before ListeningFocus boost activates
@@ -65,11 +66,51 @@ func _ready() -> void:
dialogue_box.confrontation_monologue.connect(_on_confrontation_monologue)
dialogue_box.pause_requested.connect(_on_dialogue_pause_requested)
dialogue_box.unpause_requested.connect(_on_dialogue_unpause_requested)
# D-020 (#558): Decoupled signals — coordinator routes state changes.
dialogue_box.dialogue_state_changed.connect(_on_dialogue_state_changed)
dialogue_box.audio_dip_requested.connect(_on_audio_dip_requested)
dialogue_box.audio_dip_cleared.connect(_on_audio_dip_cleared)
# #496: Print gauntlet session summary on disconnect
if gauntlet_hud:
SimBridge.connection_state_changed.connect(_on_connection_state_changed)
# #559: Register snapshot dispatch handlers — replaces inline dispatch in _process().
_router = SnapshotEventRouter.new()
# Always-run: child nodes that update from GameState on every snapshot tick.
if world_renderer:
_router.register_always(world_renderer.update_from_state)
_router.register_always(_propagate_insert_state)
_router.register_always(_update_interaction_list)
if inventory_grid:
_router.register_always(inventory_grid.update_from_state)
if stance_indicator:
_router.register_always(stance_indicator.update_from_state)
if fog_entities:
_router.register_always(fog_entities.update_from_state)
_router.register_always(_play_recognition_chimes)
if gauntlet_hud:
_router.register_always(gauntlet_hud.update_from_state)
if checklist_overlay:
_router.register_always(checklist_overlay.update_from_state)
if time_display:
_router.register_always(time_display.update_from_state)
if journal_panel:
_router.register_always(journal_panel.update_from_state)
if debug_overlay:
_router.register_always(debug_overlay.update_from_state)
_router.register_always(_play_close_sound_events)
_router.register_always(_update_zone)
_router.register_always(_update_listening_focus)
_router.register_always(_consume_examine_result)
# Keyed: consume methods guarded by specific snapshot fields.
_router.register("current_monologue", _consume_monologue)
_router.register("current_dialogue", _consume_dialogue)
_router.register("conversation_events", _consume_conversation_events)
_router.register("conversation_ended", _consume_conversation_ended)
_router.register("dialogue_response", _consume_dialogue_response)
_router.register("save_result", _consume_save_result)
func _process(delta: float) -> void:
# Main game loop: poll snapshot, apply state, flush input
@@ -89,92 +130,9 @@ func _process(delta: float) -> void:
camera.global_position = GameState.player_position * Constants.TILE_SIZE
_camera_anchored = true
# Update renderers with new state
if world_renderer and world_renderer.has_method("update_from_state"):
world_renderer.update_from_state()
# OQ-07 (#522): propagate insert state to all z-layer-6 display nodes.
# Cursor shape still fires (D-056 option a) — only verb labels suppressed.
var insert_state := GameState.insert_active
if cursor_renderer and cursor_renderer.has_method("set_insert_active"):
cursor_renderer.set_insert_active(insert_state)
if interaction_list and interaction_list.has_method("set_insert_active"):
interaction_list.set_insert_active(insert_state)
if interaction_prompt and interaction_prompt.has_method("set_insert_active"):
interaction_prompt.set_insert_active(insert_state)
if minimap and minimap.has_method("set_insert_active"):
minimap.set_insert_active(insert_state)
# D-057: Update interaction list from game state
# Suppress during dialogue — player is in conversation, verb list is noise
if interaction_list and interaction_list.has_method("update_from_state"):
if dialogue_box and dialogue_box.is_dialogue_active():
if interaction_list.is_showing():
interaction_list.hide_list()
else:
interaction_list.update_from_state()
# D-065: Update inventory grid
if inventory_grid and inventory_grid.has_method("update_from_state"):
inventory_grid.update_from_state()
# D-053: Update stance indicator
if stance_indicator and stance_indicator.has_method("update_from_state"):
stance_indicator.update_from_state()
# D-059/D-060: Update fog entity visualization (#431)
if fog_entities and fog_entities.has_method("update_from_state"):
fog_entities.update_from_state()
# D-067: Recognition chime — fire sfx_monologue_chime on first fog recognition
_play_recognition_chimes()
# #496: Update gauntlet HUD (room timer + personal bests)
if gauntlet_hud and gauntlet_hud.has_method("update_from_state"):
gauntlet_hud.update_from_state()
# #503: Update checklist overlay (auto-checklist progress tracking)
if checklist_overlay and checklist_overlay.has_method("update_from_state"):
checklist_overlay.update_from_state()
# #263: Update time display (D-013, D-031)
if time_display and time_display.has_method("update_from_state"):
time_display.update_from_state()
# #264: Update journal panel — auto-close on dialogue, refresh if open
if journal_panel and journal_panel.has_method("update_from_state"):
journal_panel.update_from_state()
# #511: Update debug overlay (F3 toggle, dev tool)
if debug_overlay and debug_overlay.has_method("update_from_state"):
debug_overlay.update_from_state()
# D-018 #125: Play close-range sound events via positional 2D audio
_play_close_sound_events()
# D-073 (#529): Zone ambient crossfade — detect player tile zone, trigger set_zone on change.
_update_zone()
# D-071 (#530): ListeningFocus boost — stationary 30+ ticks boosts WorldSFX.
# Only activates when no dialogue/confrontation dip is active (D-070).
_update_listening_focus()
# #174: Show examine result if server sent one this tick (#242)
_consume_examine_result()
# Show monologue if server sent one this tick (#414)
_consume_monologue()
# D-061: Show dialogue if server sent one this tick (#434)
_consume_dialogue()
# #535: Consume overheard conversation events and responses
_consume_conversation_events()
_consume_conversation_ended()
_consume_dialogue_response()
# #554: Show save/load result notification
_consume_save_result()
# #559: Dispatch snapshot to registered handlers (router pattern).
# Always-run handlers update child nodes; keyed handlers fire for present fields.
_router.dispatch(snapshot)
# Track camera to player (D-015: locked, fixed-north).
# #117: Manual exponential smoothing — same pattern as EntityRenderer.LERP_SPEED.
@@ -247,6 +205,32 @@ func _process(delta: float) -> void:
_pending_record_inputs.clear()
# OQ-07 (#522): Propagate insert state to all z-layer-6 display nodes.
# Cursor shape still fires (D-056 option a) — only verb labels suppressed.
func _propagate_insert_state() -> void:
var insert_state := GameState.insert_active
if cursor_renderer:
cursor_renderer.set_insert_active(insert_state)
if interaction_list:
interaction_list.set_insert_active(insert_state)
if interaction_prompt:
interaction_prompt.set_insert_active(insert_state)
if minimap:
minimap.set_insert_active(insert_state)
# D-057: Update interaction list from game state.
# Suppress during dialogue — player is in conversation, verb list is noise.
func _update_interaction_list() -> void:
if not interaction_list:
return
if dialogue_box and dialogue_box.is_dialogue_active():
if interaction_list.is_showing():
interaction_list.hide_list()
else:
interaction_list.update_from_state()
# D-018 #125: Play close-range sound events — fired once per snapshot tick.
# Each event is passed to AudioManager.play_sound_event() for 2D positional playback
# on the WorldSFX bus. Events with no registered asset are silently skipped (D-038).
@@ -456,6 +440,22 @@ func _on_dialogue_dismissed() -> void:
})
# D-020 (#558): Coordinator handles dialogue state changes from dialogue_box.
# Synchronous signal — GameState.dialogue_active updates same frame (D-064).
func _on_dialogue_state_changed(active: bool) -> void:
GameState.dialogue_active = active
# D-020 (#558): Coordinator routes audio dip requests from dialogue_box.
func _on_audio_dip_requested(profile: String) -> void:
AudioManager.apply_dip(profile)
# D-020 (#558): Coordinator routes audio dip clear from dialogue_box.
func _on_audio_dip_cleared() -> void:
AudioManager.clear_dip()
# #496: Finalize gauntlet stats on disconnect
func _on_connection_state_changed(old_state: SimBridge.ConnectionState, new_state: SimBridge.ConnectionState) -> void:
if new_state == SimBridge.ConnectionState.DISCONNECTED and gauntlet_hud:
+20
View File
@@ -244,6 +244,24 @@ static func decode_snapshot(bytes: PackedByteArray) -> Variant:
"error": raw_save.get("error"),
}
# TODO(server): Send stationary_ticks in ObserverSnapshot (D-071, D-020).
# Server already tracks this in ListeningFocus component (server/src/simulation/listening.rs).
# When server populates this field, client-side accumulation fallback in game_state.gd
# can be removed — apply_snapshot() should contain only direct field assignments.
var stationary_ticks: Variant = null
var raw_st: Variant = raw.get("stationary_ticks")
if raw_st != null:
stationary_ticks = int(raw_st)
# TODO(server): Send top-level zone_id string in ObserverSnapshot (D-073, D-020).
# Server sends zone_id per VisibleTile but not as a top-level snapshot field.
# When server populates this, client-side tile iteration fallback in game_state.gd
# can be removed — apply_snapshot() should contain only direct field assignments.
var zone_id: Variant = null
var raw_zid: Variant = raw.get("zone_id")
if raw_zid is String:
zone_id = raw_zid
# v14: player_knowledge (#264, D-041) — partial KG dump for journal panel.
# {entities: [{entity_id, name, confidence, source, state, relationship, last_observed_tick}],
# facts: [{fact_id, confidence, source, state, acquired_tick}]}
@@ -302,6 +320,8 @@ static func decode_snapshot(bytes: PackedByteArray) -> Variant:
"examine_result": examine_result,
"player_knowledge": player_knowledge,
"save_result": save_result,
"stationary_ticks": stationary_ticks,
"zone_id": zone_id,
}
+45
View File
@@ -0,0 +1,45 @@
class_name SnapshotEventRouter
## Routes snapshot fields to registered handlers (D-020, #559).
##
## Decouples main.gd from knowing which child node handles which snapshot field.
## Handlers are registered in main.gd._ready(); dispatch() is called each snapshot tick.
##
## Two handler types:
## - Keyed: called only when the snapshot contains a specific field.
## - Always: called every dispatch (every snapshot tick), regardless of fields present.
##
## All handlers are zero-argument callables — they read from GameState directly.
## This preserves GameState as the single source of truth post-apply_snapshot().
## Keyed handlers: field_name → Array[Callable]
## Array per field allows multiple handlers on the same key (e.g., two consumers of same data).
var _keyed: Dictionary = {} # String → Array[Callable]
## Always handlers: called every dispatch regardless of snapshot content.
var _always: Array[Callable] = []
## Register a handler for a specific snapshot field key.
## Handler is called (with no arguments) when snapshot.has(field) is true.
## Multiple handlers per field are supported — they run in registration order.
func register(field: String, handler: Callable) -> void:
if not _keyed.has(field):
_keyed[field] = []
_keyed[field].append(handler)
## Register a handler that runs every dispatch tick (not keyed to a field).
## Use for child nodes that update from GameState on every snapshot, e.g. update_from_state().
func register_always(handler: Callable) -> void:
_always.append(handler)
## Dispatch a snapshot: call always handlers first, then keyed handlers for present fields.
## Handlers read from GameState.* directly — apply_snapshot() must be called before dispatch().
func dispatch(snapshot: Dictionary) -> void:
for handler in _always:
handler.call()
for field in _keyed:
if snapshot.has(field):
for handler in _keyed[field]:
handler.call()
+163
View File
@@ -0,0 +1,163 @@
class_name YamlParser
## Shared YAML parser — common subset used by ui_strings.gd and checklist_evaluator.gd.
##
## Handles: nested sections (maps), arrays of dict items (- key: val), typed values.
## Returns a hierarchical Dictionary. Use flatten() to convert to dotted-key format
## (as UIStrings._parse_yaml() requires).
##
## Limitations: single-line values only; no YAML anchors/aliases; no flow syntax.
## String values: quotes stripped. Booleans, ints, and floats are type-inferred.
##
## Spec ref: #560 (Sprint 20 — unify duplicate YAML parsers), D-030 (testability).
## Parse YAML text into a hierarchical Dictionary.
## Nested sections become nested dicts. Array items (- key: val) become Arrays.
## Values are type-inferred: bool, int, float, or String.
static func parse(text: String) -> Dictionary:
var root: Dictionary = {}
# Stack: [{indent: int, key: String}] — path of open section headers
var stack: Array = []
# Array state
var current_array: Variant = null # Array being built, or null
var current_item: Variant = null # Dict being built for current array item, or null
var array_parent_indent: int = -1 # indent of the "key:" line that owns the array
for raw_line in text.split("\n"):
var stripped := raw_line.strip_edges(false, true)
if stripped.is_empty() or stripped.strip_edges().begins_with("#"):
continue
var indent: int = raw_line.length() - raw_line.lstrip(" ").length()
var content: String = stripped.strip_edges()
# --- Array item (- key: value) ---
if content.begins_with("- "):
# First item: convert parent section's {} placeholder to []
if current_array == null and stack.size() > 0:
var parent := _node_at(root, stack, true)
var arr_key: String = stack.back()["key"]
var new_arr: Array = []
parent[arr_key] = new_arr
current_array = new_arr
array_parent_indent = stack.back()["indent"]
# Flush previous item and start a new one
if current_item != null:
current_array.append(current_item)
current_item = {}
var rest: String = content.substr(2).strip_edges()
var colon: int = rest.find(":")
if colon >= 0:
var k: String = rest.substr(0, colon).strip_edges()
var v: String = rest.substr(colon + 1).strip_edges()
current_item[k] = _parse_value(v)
continue
# --- Continuation line within current array item ---
if current_array != null and indent > array_parent_indent:
var colon: int = content.find(":")
if colon >= 0 and current_item != null:
var k: String = content.substr(0, colon).strip_edges()
var v: String = content.substr(colon + 1).strip_edges()
current_item[k] = _parse_value(v)
continue
# --- End of array (indent has returned to array level or above) ---
if current_array != null:
if current_item != null:
current_array.append(current_item)
current_item = null
current_array = null
array_parent_indent = -1
if stack.size() > 0:
stack.pop_back() # pop the array-owning key
# --- Regular key: value or section header ---
var colon: int = content.find(":")
if colon < 0:
continue
var key: String = content.substr(0, colon).strip_edges()
var val_str: String = content.substr(colon + 1).strip_edges()
# Pop sections at the same or deeper indent (we're back at a shallower level)
while stack.size() > 0 and stack.back()["indent"] >= indent:
stack.pop_back()
var node: Dictionary = _node_at(root, stack, false)
if val_str.is_empty() or val_str.begins_with("#"):
# Section header — create nested dict (may become Array if - items follow)
node[key] = {}
stack.push_back({"indent": indent, "key": key})
else:
node[key] = _parse_value(val_str)
# Flush the last array item if the file ended inside an array
if current_array != null and current_item != null:
current_array.append(current_item)
return root
## Convenience: parse text and flatten to dotted-key format in one call.
## Used by UIStrings._parse_yaml() — equivalent to flatten(parse(text)).
static func parse_flat(text: String) -> Dictionary:
return flatten(parse(text))
## Flatten a hierarchical dict to dotted-key format (for UIStrings compatibility).
## {"a": {"b": "v"}} → {"a.b": "v"}
## Arrays are skipped — dotted-key format does not represent them.
## All values are converted to String (UIStrings stores display text, not typed data).
static func flatten(d: Dictionary, prefix: String = "") -> Dictionary:
var result: Dictionary = {}
for k in d:
var full_key: String = (prefix + "." if not prefix.is_empty() else "") + str(k)
var v = d[k]
if v is Dictionary:
result.merge(flatten(v, full_key))
elif not v is Array:
result[full_key] = str(v)
return result
## Parse a single YAML value string into a typed GDScript value.
## Strips inline comments, handles quoted strings, infers bool/int/float/String.
static func _parse_value(val: String) -> Variant:
if val.is_empty():
return ""
# Strip inline comment outside quotes
if not val.begins_with("\""):
var comment_pos: int = val.find(" #")
if comment_pos >= 0:
val = val.substr(0, comment_pos).strip_edges()
# Quoted string — extract content between quotes
if val.begins_with("\""):
var end_quote: int = val.find("\"", 1)
if end_quote > 0:
return val.substr(1, end_quote - 1)
return val.substr(1)
# Boolean
if val == "true": return true
if val == "false": return false
# Float (must have decimal point)
if val.contains(".") and val.is_valid_float():
return val.to_float()
# Integer
if val.is_valid_int():
return val.to_int()
# Plain string
return val
## Navigate root following the stack key path.
## parent=true: navigate one level less (returns the parent node, not the leaf).
static func _node_at(root: Dictionary, stack: Array, parent: bool) -> Dictionary:
var node: Dictionary = root
var depth: int = stack.size() - (1 if parent else 0)
for i in range(depth):
var k: String = stack[i]["key"]
if node.has(k) and node[k] is Dictionary:
node = node[k]
else:
break
return node
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+74
View File
@@ -596,3 +596,77 @@ func test_is_dialogue_active_true_after_show_dialogue() -> void:
box.show_dialogue("NPC", "Speech.", _make_options(["Reply"]))
assert_bool(box.is_dialogue_active()).is_true()
box.queue_free()
# ---------------------------------------------------------------------------
# D-020 (#558): Signal decoupling — dialogue_box emits signals instead of
# directly mutating GameState or calling AudioManager.
# ---------------------------------------------------------------------------
func test_dialogue_state_changed_emits_true_on_show() -> void:
## D-020: show_dialogue() must emit dialogue_state_changed(true).
var box := _make_dialogue_box()
if box == null: return
var received: Array = []
box.dialogue_state_changed.connect(func(active): received.append(active))
box.show_dialogue("NPC", "Speech.", _make_options(["Reply"]))
assert_bool(received.has(true)).override_failure_message(
"dialogue_state_changed(true) must be emitted on show_dialogue"
).is_true()
box.queue_free()
func test_dialogue_state_changed_emits_false_on_hide() -> void:
## D-020: hide_dialogue() must emit dialogue_state_changed(false).
var box := _make_dialogue_box()
if box == null: return
var received: Array = []
box.dialogue_state_changed.connect(func(active): received.append(active))
box.show_dialogue("NPC", "Speech.", _make_options(["Reply"]))
box.hide_dialogue()
assert_bool(received.has(false)).override_failure_message(
"dialogue_state_changed(false) must be emitted on hide_dialogue"
).is_true()
box.queue_free()
func test_audio_dip_requested_emits_dialogue_on_show() -> void:
## D-020: show_dialogue() must emit audio_dip_requested("dialogue").
var box := _make_dialogue_box()
if box == null: return
var received: Array = []
box.audio_dip_requested.connect(func(profile): received.append(profile))
box.show_dialogue("NPC", "Speech.", _make_options(["Reply"]))
assert_bool(received.has("dialogue")).override_failure_message(
"audio_dip_requested('dialogue') must be emitted on show_dialogue"
).is_true()
box.queue_free()
func test_audio_dip_cleared_emits_on_hide() -> void:
## D-020: hide_dialogue() must emit audio_dip_cleared.
var box := _make_dialogue_box()
if box == null: return
var cleared := [false]
box.audio_dip_cleared.connect(func(): cleared[0] = true)
box.show_dialogue("NPC", "Speech.", _make_options(["Reply"]))
box.hide_dialogue()
assert_bool(cleared[0]).override_failure_message(
"audio_dip_cleared must be emitted on hide_dialogue"
).is_true()
box.queue_free()
func test_no_direct_game_state_mutation() -> void:
## D-020: dialogue_box must not directly mutate GameState.dialogue_active.
## After show_dialogue, GameState.dialogue_active should remain unchanged
## (only the coordinator updates it via signal handler).
var box := _make_dialogue_box()
if box == null: return
GameState.dialogue_active = false
box.show_dialogue("NPC", "Speech.", _make_options(["Reply"]))
assert_bool(GameState.dialogue_active).override_failure_message(
"GameState.dialogue_active must NOT be mutated directly by dialogue_box"
).is_false()
box.queue_free()
GameState.dialogue_active = false
+143
View File
@@ -0,0 +1,143 @@
## Sprint 20 #558: dialogue_box.gd decoupling tests.
##
## Verifies D-020 compliance: dialogue_box.gd emits signals instead of mutating
## GameState or calling AudioManager directly. main.gd wires the signal handlers.
##
## D-030: fixture-based, server-free, no subprocess required.
class_name TestDialogueSprint20
extends GdUnitTestSuite
func _make_dialogue_box() -> Control:
if not ResourceLoader.exists("res://ui/dialogue_box.tscn"):
push_warning("TestDialogueSprint20: dialogue_box.tscn not found — scene tests skipped")
return null
var node: Control = load("res://ui/dialogue_box.tscn").instantiate()
add_child(node)
return node
func before_test() -> void:
GameState.dialogue_active = false
func after_test() -> void:
GameState.dialogue_active = false
# -- dialogue_state_changed signal -------------------------------------------
func test_show_dialogue_emits_dialogue_state_changed_true() -> void:
## show_dialogue() must emit dialogue_state_changed(true) not mutate GameState directly.
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
var received: Variant = null
box.dialogue_state_changed.connect(func(active: bool): received = active)
box.show_dialogue("NPC", "Hello.", [])
assert_that(received).override_failure_message(
"show_dialogue() must emit dialogue_state_changed(true) (#558)"
).is_equal(true)
func test_show_dialogue_does_not_mutate_game_state_directly() -> void:
## Without a connected handler, GameState.dialogue_active must stay false.
## Proves dialogue_box.gd has zero direct GameState mutation (D-020 #558).
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
GameState.dialogue_active = false
box.show_dialogue("NPC", "Hello.", [])
assert_bool(GameState.dialogue_active).override_failure_message(
"dialogue_box must not mutate GameState.dialogue_active directly (D-020 #558)"
).is_false()
func test_hide_dialogue_emits_dialogue_state_changed_false() -> void:
## hide_dialogue() must emit dialogue_state_changed(false).
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
box.show_dialogue("NPC", "Hello.", [])
var received: Variant = null
box.dialogue_state_changed.connect(func(active: bool): received = active)
box.hide_dialogue()
assert_that(received).override_failure_message(
"hide_dialogue() must emit dialogue_state_changed(false) (#558)"
).is_equal(false)
# -- audio_dip_requested / audio_dip_cleared signals -------------------------
func test_show_dialogue_emits_audio_dip_requested_dialogue() -> void:
## show_dialogue() must emit audio_dip_requested("dialogue") not call AudioManager.
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
var received_profile: Variant = null
box.audio_dip_requested.connect(func(profile: String): received_profile = profile)
box.show_dialogue("NPC", "Hello.", [])
assert_that(received_profile).override_failure_message(
"show_dialogue() must emit audio_dip_requested('dialogue') (#558)"
).is_equal("dialogue")
func test_show_dialogue_does_not_call_audio_manager_directly() -> void:
## Without a connected handler, AudioManager state must be unchanged by show_dialogue().
## Verifies no direct AudioManager call in dialogue_box.gd (D-020 #558).
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
var dip_before := AudioManager.get_active_dip()
box.show_dialogue("NPC", "Hello.", [])
var dip_after := AudioManager.get_active_dip()
assert_str(dip_after).override_failure_message(
"dialogue_box must not call AudioManager.apply_dip() directly (D-020 #558)"
).is_equal(dip_before)
func test_hide_dialogue_emits_audio_dip_cleared() -> void:
## Ending a conversation must emit audio_dip_cleared not call AudioManager directly.
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
box.show_dialogue("NPC", "Hello.", [])
var cleared := false
box.audio_dip_cleared.connect(func(): cleared = true)
box.hide_dialogue()
assert_bool(cleared).override_failure_message(
"hide_dialogue() must emit audio_dip_cleared (#558)"
).is_true()
func test_audio_dip_cleared_count_on_conversation_end() -> void:
## Verify audio_dip_cleared fires when conversation ends.
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
var cleared_count := 0
box.audio_dip_cleared.connect(func(): cleared_count += 1)
box.show_dialogue("NPC", "Speak.", [])
box.hide_dialogue()
assert_int(cleared_count).override_failure_message(
"audio_dip_cleared must fire at least once when conversation ends (#558)"
).is_greater_equal(1)
# -- coordinator wiring verification -----------------------------------------
func test_signal_handler_wires_game_state() -> void:
## Simulate main.gd: connect dialogue_state_changed to update GameState.dialogue_active.
## Verifies the coordinator pattern works end-to-end (D-020 #558).
var box := _make_dialogue_box()
if box == null: return
auto_free(box)
box.dialogue_state_changed.connect(func(active: bool): GameState.dialogue_active = active)
box.show_dialogue("NPC", "Hello.", [])
assert_bool(GameState.dialogue_active).override_failure_message(
"With handler wired, GameState.dialogue_active must be true after show_dialogue (#558)"
).is_true()
box.hide_dialogue()
assert_bool(GameState.dialogue_active).override_failure_message(
"With handler wired, GameState.dialogue_active must be false after hide_dialogue (#558)"
).is_false()
+63 -4
View File
@@ -19,6 +19,8 @@ func before_each() -> void:
GameState.player_stance = "Walk"
GameState.player_inventory = []
GameState.stationary_ticks = 0
GameState._prev_player_position = Vector2(-1e9, -1e9)
GameState.current_zone_id = ""
GameState.insert_active = true
@@ -115,19 +117,49 @@ func test_apply_snapshot_inventory_absent_clears_list() -> void:
assert_that(GameState.player_inventory.size()).is_equal(0)
# -- Stationary tick counter (D-071) -------------------------------------
# -- Stationary ticks: server-authoritative (D-020/D-071) -----------------
func test_stationary_ticks_increments_when_player_position_unchanged() -> void:
func test_stationary_ticks_from_server_snapshot() -> void:
## D-020: When server sends stationary_ticks, client reads it directly.
GameState.apply_snapshot({"tick": 1, "entities": [], "stationary_ticks": 42})
assert_int(GameState.stationary_ticks).is_equal(42)
func test_stationary_ticks_server_value_overrides_client_accumulation() -> void:
## D-020: Server value takes priority — client must not accumulate on top of it.
var snapshot := {
"tick": 1,
"entities": [{"entity_id": 1, "x": 10.0, "y": 10.0, "z": 0, "kind": {"variant": "Player", "data": null}}],
"stationary_ticks": 5,
}
GameState.apply_snapshot(snapshot)
GameState.apply_snapshot(snapshot) # same position, but server sends 5 again
assert_int(GameState.stationary_ticks).is_equal(5) # server value, not 6
func test_stationary_ticks_missing_field_degrades_gracefully() -> void:
## D-020 fallback: when server omits stationary_ticks, no crash, defaults to 0.
GameState.stationary_ticks = 0
GameState.apply_snapshot({"tick": 1, "entities": []})
# No crash; field retains a valid integer value.
assert_int(GameState.stationary_ticks).is_greater_equal(0)
# -- Stationary ticks: deprecated client-side fallback (D-071) -----------
func test_stationary_ticks_fallback_increments_when_position_unchanged() -> void:
## Deprecated fallback: client accumulates when server omits the field.
var snapshot := {
"tick": 1,
"entities": [{"entity_id": 1, "x": 10.0, "y": 10.0, "z": 0, "kind": {"variant": "Player", "data": null}}],
}
GameState.apply_snapshot(snapshot) # first call: position changes from ZERO
GameState.apply_snapshot(snapshot) # first call: position changes from sentinel
GameState.apply_snapshot(snapshot) # second call: position unchanged → +1
assert_int(GameState.stationary_ticks).is_greater(0)
func test_stationary_ticks_resets_on_player_movement() -> void:
func test_stationary_ticks_fallback_resets_on_movement() -> void:
## Deprecated fallback: client resets on movement when server omits the field.
var s1 := {
"tick": 1,
"entities": [{"entity_id": 1, "x": 10.0, "y": 10.0, "z": 0, "kind": {"variant": "Player", "data": null}}],
@@ -143,6 +175,33 @@ func test_stationary_ticks_resets_on_player_movement() -> void:
assert_int(GameState.stationary_ticks).is_equal(0)
# -- Zone ID: server-authoritative (D-020/D-073) -------------------------
func test_zone_id_from_server_snapshot() -> void:
## D-020: When server sends top-level zone_id, client reads it directly.
GameState.apply_snapshot({"tick": 1, "entities": [], "zone_id": "zone_alpha"})
assert_that(GameState.current_zone_id).is_equal("zone_alpha")
func test_zone_id_server_value_overrides_tile_derivation() -> void:
## D-020: Server top-level zone_id takes priority over tile-derived zone_id.
var snapshot := {
"tick": 1,
"entities": [{"entity_id": 1, "x": 5.0, "y": 5.0, "z": 0, "kind": {"variant": "Player", "data": null}}],
"tiles": [{"x": 5, "y": 5, "z": 0, "type": "floor", "zone_id": "zone_from_tile"}],
"zone_id": "zone_from_server",
}
GameState.apply_snapshot(snapshot)
assert_that(GameState.current_zone_id).is_equal("zone_from_server")
func test_zone_id_missing_field_degrades_gracefully() -> void:
## D-020 fallback: when server omits zone_id and no tiles match, defaults to "".
GameState.current_zone_id = ""
GameState.apply_snapshot({"tick": 1, "entities": []})
assert_that(GameState.current_zone_id).is_equal("")
# -- insert_active (OQ-07, #522) -----------------------------------------
func test_apply_snapshot_insert_active_false() -> void:
+164
View File
@@ -0,0 +1,164 @@
## Sprint 20 #557: GameState.apply_snapshot() refactor tests.
##
## Verifies D-020 compliance: apply_snapshot() reads server-authoritative values
## for stationary_ticks and zone_id directly from the snapshot when present,
## and degrades gracefully when the server has not yet added these fields.
##
## Does NOT replace test_game_state.gd or test_snapshot_zone_id.gd — those cover
## existing behaviour. This file covers the new code paths added in Sprint 20.
##
## D-030: fixture-based, server-free, no subprocess required.
class_name TestGameStateSprint20
extends GdUnitTestSuite
func before_each() -> void:
GameState.stationary_ticks = 0
GameState._prev_player_position = Vector2(-1e9, -1e9)
GameState.current_zone_id = ""
GameState.player_position = Vector2.ZERO
GameState.visible_tiles = []
GameState.visible_positions = {}
GameState.visibility_sectors = {}
# -- stationary_ticks: server-authoritative path (D-020) ----------------------
func test_stationary_ticks_reads_server_value_when_present() -> void:
# When snapshot includes stationary_ticks, apply_snapshot() must use the
# server value directly without client-side accumulation (D-020).
var snapshot := {
"tick": 5,
"entities": [
{"entity_id": 1, "x": 10.0, "y": 10.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
"stationary_ticks": 42,
}
GameState.apply_snapshot(snapshot)
assert_int(GameState.stationary_ticks).override_failure_message(
"apply_snapshot() must read stationary_ticks=42 from snapshot (D-020)"
).is_equal(42)
func test_stationary_ticks_server_value_does_not_accumulate() -> void:
# Server-sent value must be assigned directly — NOT added to existing value.
# Two calls with stationary_ticks=10 must yield 10, not 20.
var snapshot := {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 5.0, "y": 5.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
"stationary_ticks": 10,
}
GameState.apply_snapshot(snapshot)
GameState.apply_snapshot(snapshot)
assert_int(GameState.stationary_ticks).override_failure_message(
"Server value must be assigned directly, not accumulated (D-020)"
).is_equal(10)
func test_stationary_ticks_server_can_reset_to_zero() -> void:
# Server sends 0 when player moves — client must accept this reset.
GameState.stationary_ticks = 50
var snapshot := {
"tick": 2,
"entities": [
{"entity_id": 1, "x": 5.0, "y": 5.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
"stationary_ticks": 0,
}
GameState.apply_snapshot(snapshot)
assert_int(GameState.stationary_ticks).override_failure_message(
"Server reset to 0 must override client-held value"
).is_equal(0)
# -- stationary_ticks: DEPRECATED fallback (graceful degradation) --------------
func test_stationary_ticks_fallback_when_field_absent() -> void:
# When snapshot has no stationary_ticks, client-side accumulation must still
# run (backward compat until server ships field). No crash.
var snapshot := {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 10.0, "y": 10.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
}
GameState.apply_snapshot(snapshot) # position changes from ZERO → resets to 0
GameState.apply_snapshot(snapshot) # position unchanged → increments
assert_int(GameState.stationary_ticks).override_failure_message(
"Client-side fallback must increment stationary_ticks when field absent"
).is_greater(0)
func test_apply_snapshot_missing_stationary_ticks_no_crash() -> void:
# Snapshots lacking stationary_ticks must not crash apply_snapshot().
var snapshot := {"tick": 1, "entities": []}
# No assertion needed beyond confirming no exception is raised.
GameState.apply_snapshot(snapshot)
assert_bool(true).is_true()
# -- zone_id: server-authoritative path (D-020/D-073) -------------------------
func test_current_zone_id_reads_server_value_when_present() -> void:
# When snapshot includes top-level zone_id, apply_snapshot() must use it
# directly without tile lookup (D-020).
var snapshot := {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 5.0, "y": 5.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
"zone_id": "zone_server_direct",
}
GameState.apply_snapshot(snapshot)
assert_str(GameState.current_zone_id).override_failure_message(
"apply_snapshot() must read zone_id='zone_server_direct' from snapshot (D-020)"
).is_equal("zone_server_direct")
func test_current_zone_id_server_value_overrides_tile_data() -> void:
# When snapshot has both zone_id and visible_tiles with a different zone,
# the top-level zone_id field takes priority.
var snapshot := {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 5.0, "y": 5.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
"zone_id": "zone_from_server",
"visible_tiles": [
{"x": 5, "y": 5, "z": 0, "type": "floor", "zone_id": "zone_from_tile"},
],
}
GameState.apply_snapshot(snapshot)
assert_str(GameState.current_zone_id).override_failure_message(
"Top-level zone_id must override tile-derived zone when both present"
).is_equal("zone_from_server")
# -- zone_id: DEPRECATED fallback (graceful degradation) ----------------------
func test_current_zone_id_fallback_to_tile_lookup_when_absent() -> void:
# When snapshot lacks top-level zone_id, tile lookup fallback must run.
# Existing test_snapshot_zone_id.gd covers detailed scenarios; this is a
# smoke test confirming the fallback path still works after Sprint 20 refactor.
var snapshot := {
"tick": 1,
"entities": [
{"entity_id": 1, "x": 3.0, "y": 3.0, "z": 0, "kind": {"variant": "Player", "data": null}},
],
"visible_tiles": [
{"x": 3, "y": 3, "z": 0, "type": "floor", "zone_id": "zone_tile_fallback"},
],
}
GameState.apply_snapshot(snapshot)
assert_str(GameState.current_zone_id).override_failure_message(
"Tile lookup fallback must populate zone_id when top-level field absent"
).is_equal("zone_tile_fallback")
func test_apply_snapshot_missing_zone_id_no_crash() -> void:
# Snapshots lacking both zone_id and visible_tiles must not crash.
var snapshot := {"tick": 1, "entities": []}
GameState.apply_snapshot(snapshot)
assert_str(GameState.current_zone_id).is_equal("")
@@ -0,0 +1,89 @@
## SnapshotEventRouter unit tests (#559).
## Verifies callable-based dispatch: keyed handlers receive correct field values,
## always handlers run on every dispatch, absent fields don't trigger handlers.
##
## D-030: fixture-based, server-free, no subprocess required.
class_name TestSnapshotEventRouter
extends GdUnitTestSuite
# -- Keyed handlers -----------------------------------------------------------
func test_keyed_handler_called_when_field_present() -> void:
var router := SnapshotEventRouter.new()
var received: Array = []
router.register("current_monologue", func(): received.append("monologue"))
router.dispatch({"current_monologue": {"text": "Test."}})
assert_that(received.size()).is_equal(1)
assert_that(received[0]).is_equal("monologue")
func test_keyed_handler_not_called_when_field_absent() -> void:
var router := SnapshotEventRouter.new()
var received: Array = []
router.register("current_monologue", func(): received.append("monologue"))
router.dispatch({"tick": 1})
assert_that(received.size()).is_equal(0)
func test_multiple_keyed_handlers_same_field() -> void:
## Multiple handlers on the same key run in registration order.
var router := SnapshotEventRouter.new()
var order: Array = []
router.register("save_result", func(): order.append("first"))
router.register("save_result", func(): order.append("second"))
router.dispatch({"save_result": {"success": true}})
assert_that(order).is_equal(["first", "second"])
func test_keyed_handlers_multiple_fields() -> void:
## Each keyed handler fires only for its registered field.
var router := SnapshotEventRouter.new()
var received: Array = []
router.register("current_monologue", func(): received.append("mono"))
router.register("current_dialogue", func(): received.append("dlg"))
router.dispatch({"current_monologue": {"text": "Hi."}})
assert_that(received).is_equal(["mono"])
received.clear()
router.dispatch({"current_dialogue": {"speech": "Hello."}, "current_monologue": {"text": "Hmm."}})
assert_bool(received.has("mono")).is_true()
assert_bool(received.has("dlg")).is_true()
# -- Always handlers ----------------------------------------------------------
func test_always_handler_called_on_every_dispatch() -> void:
var router := SnapshotEventRouter.new()
var count: Array[int] = [0]
router.register_always(func(): count[0] += 1)
router.dispatch({"tick": 1})
router.dispatch({"tick": 2})
router.dispatch({})
assert_int(count[0]).is_equal(3)
func test_always_handler_runs_before_keyed() -> void:
## Always handlers run before keyed handlers (dispatch order guarantee).
var router := SnapshotEventRouter.new()
var order: Array = []
router.register("current_monologue", func(): order.append("keyed"))
router.register_always(func(): order.append("always"))
router.dispatch({"current_monologue": {"text": "Hi."}})
assert_that(order[0]).is_equal("always")
assert_that(order[1]).is_equal("keyed")
# -- Empty dispatch ------------------------------------------------------------
func test_empty_snapshot_no_crash() -> void:
var router := SnapshotEventRouter.new()
router.register("foo", func(): pass)
router.register_always(func(): pass)
# Must not crash
router.dispatch({})
func test_no_handlers_no_crash() -> void:
var router := SnapshotEventRouter.new()
# Must not crash
router.dispatch({"tick": 1, "entities": []})
+261
View File
@@ -0,0 +1,261 @@
## YamlParser unit tests (#560).
## Verifies parse() (nested, typed) and parse_flat() (dotted keys, string values).
## Covers: maps, nested maps, arrays of dicts, type conversion, comments,
## quoted strings, empty input, edge cases.
##
## D-030: fixture-based, server-free, no subprocess required.
class_name TestYamlParser
extends GdUnitTestSuite
# -- parse(): basic key-value pairs -------------------------------------------
func test_parse_simple_key_value() -> void:
var result := YamlParser.parse("key: value")
assert_that(result["key"]).is_equal("value")
func test_parse_quoted_string() -> void:
var result := YamlParser.parse('key: "hello world"')
assert_that(result["key"]).is_equal("hello world")
func test_parse_empty_quoted_string() -> void:
var result := YamlParser.parse('key: ""')
assert_that(result["key"]).is_equal("")
func test_parse_integer_value() -> void:
var result := YamlParser.parse("count: 42")
assert_that(result["count"]).is_equal(42)
assert_that(typeof(result["count"])).is_equal(TYPE_INT)
func test_parse_negative_integer() -> void:
var result := YamlParser.parse("offset: -3")
assert_that(result["offset"]).is_equal(-3)
func test_parse_float_value() -> void:
var result := YamlParser.parse("radius: 2.5")
assert_that(typeof(result["radius"])).is_equal(TYPE_FLOAT)
assert_float(result["radius"]).is_equal_approx(2.5, 0.001)
func test_parse_boolean_true() -> void:
var result := YamlParser.parse("enabled: true")
assert_that(result["enabled"]).is_equal(true)
assert_that(typeof(result["enabled"])).is_equal(TYPE_BOOL)
func test_parse_boolean_false() -> void:
var result := YamlParser.parse("enabled: false")
assert_that(result["enabled"]).is_equal(false)
func test_parse_empty_input() -> void:
var result := YamlParser.parse("")
assert_that(result.size()).is_equal(0)
func test_parse_only_comments() -> void:
var result := YamlParser.parse("# comment\n# another")
assert_that(result.size()).is_equal(0)
func test_parse_inline_comment_stripped() -> void:
var result := YamlParser.parse("room_id: test # this is a comment")
assert_that(result["room_id"]).is_equal("test")
func test_parse_quoted_value_with_hash() -> void:
## Quoted strings preserve literal # characters.
var result := YamlParser.parse('color: "#e0e8ff"')
assert_that(result["color"]).is_equal("#e0e8ff")
func test_parse_value_with_colon() -> void:
var result := YamlParser.parse('time: "12:30"')
assert_that(result["time"]).is_equal("12:30")
# -- parse(): nested maps -----------------------------------------------------
func test_parse_nested_map() -> void:
var yaml := "section:\n key: value"
var result := YamlParser.parse(yaml)
assert_that(result.has("section")).is_true()
assert_that(result["section"] is Dictionary).is_true()
assert_that(result["section"]["key"]).is_equal("value")
func test_parse_deeply_nested() -> void:
var yaml := "a:\n b:\n c: deep"
var result := YamlParser.parse(yaml)
assert_that(result["a"]["b"]["c"]).is_equal("deep")
func test_parse_multiple_sections() -> void:
var yaml := "hud:\n mode: Mode\ninteraction:\n talk: Talk"
var result := YamlParser.parse(yaml)
assert_that(result["hud"]["mode"]).is_equal("Mode")
assert_that(result["interaction"]["talk"]).is_equal("Talk")
func test_parse_multiple_keys_per_section() -> void:
var yaml := "hud:\n a: 1\n b: 2\n c: 3"
var result := YamlParser.parse(yaml)
assert_that(result["hud"]["a"]).is_equal(1)
assert_that(result["hud"]["b"]).is_equal(2)
assert_that(result["hud"]["c"]).is_equal(3)
func test_parse_sibling_subsections() -> void:
var yaml := "states:\n a:\n x: 1\n b:\n x: 2"
var result := YamlParser.parse(yaml)
assert_that(result["states"]["a"]["x"]).is_equal(1)
assert_that(result["states"]["b"]["x"]).is_equal(2)
func test_parse_section_with_trailing_comment() -> void:
## "section: # comment" should be treated as a section header.
var yaml := "section: # comment\n key: value"
var result := YamlParser.parse(yaml)
assert_that(result["section"]["key"]).is_equal("value")
# -- parse(): arrays of dicts -------------------------------------------------
func test_parse_single_array_item() -> void:
var yaml := "conditions:\n - id: test-1\n type: near\n x: 10"
var result := YamlParser.parse(yaml)
assert_that(result.has("conditions")).is_true()
assert_that(result["conditions"] is Array).is_true()
assert_that(result["conditions"].size()).is_equal(1)
assert_that(result["conditions"][0]["id"]).is_equal("test-1")
assert_that(result["conditions"][0]["type"]).is_equal("near")
assert_that(result["conditions"][0]["x"]).is_equal(10)
func test_parse_multiple_array_items() -> void:
var yaml := "conditions:\n - id: a\n x: 1\n - id: b\n x: 2"
var result := YamlParser.parse(yaml)
assert_that(result["conditions"].size()).is_equal(2)
assert_that(result["conditions"][0]["id"]).is_equal("a")
assert_that(result["conditions"][1]["id"]).is_equal("b")
func test_parse_array_items_with_blank_lines() -> void:
var yaml := "conditions:\n - id: a\n x: 1\n\n - id: b\n x: 2"
var result := YamlParser.parse(yaml)
assert_that(result["conditions"].size()).is_equal(2)
func test_parse_top_level_plus_array() -> void:
## Checklist format: top-level key-value pairs followed by a conditions array.
var yaml := "room_id: warehouse\nconditions:\n - id: c1\n type: near\n x: 5"
var result := YamlParser.parse(yaml)
assert_that(result["room_id"]).is_equal("warehouse")
assert_that(result["conditions"].size()).is_equal(1)
assert_that(result["conditions"][0]["id"]).is_equal("c1")
func test_parse_array_typed_values() -> void:
var yaml := "items:\n - id: t\n x: 42\n radius: 2.5\n active: true"
var result := YamlParser.parse(yaml)
var item: Dictionary = result["items"][0]
assert_that(item["x"]).is_equal(42)
assert_that(typeof(item["radius"])).is_equal(TYPE_FLOAT)
assert_that(item["active"]).is_equal(true)
# -- parse_flat(): dotted keys -------------------------------------------------
func test_flat_simple() -> void:
var yaml := "section:\n key: value"
var result := YamlParser.parse_flat(yaml)
assert_that(result.has("section.key")).is_true()
assert_that(result["section.key"]).is_equal("value")
func test_flat_deeply_nested() -> void:
var yaml := "a:\n b:\n c: deep"
var result := YamlParser.parse_flat(yaml)
assert_that(result["a.b.c"]).is_equal("deep")
func test_flat_multiple_sections() -> void:
var yaml := "hud:\n mode: Mode\ninteraction:\n talk: Talk"
var result := YamlParser.parse_flat(yaml)
assert_that(result["hud.mode"]).is_equal("Mode")
assert_that(result["interaction.talk"]).is_equal("Talk")
func test_flat_values_are_strings() -> void:
## parse_flat returns all values as strings, unlike parse() which returns typed.
var yaml := "section:\n count: 42\n rate: 0.9\n active: true"
var result := YamlParser.parse_flat(yaml)
assert_that(result["section.count"]).is_equal("42")
assert_that(result["section.rate"]).is_equal("0.9")
assert_that(result["section.active"]).is_equal("true")
func test_flat_quoted_value() -> void:
var yaml := 'section:\n key: "hello world"'
var result := YamlParser.parse_flat(yaml)
assert_that(result["section.key"]).is_equal("hello world")
func test_flat_inline_comment() -> void:
var yaml := "hud:\n mode: Standard # default"
var result := YamlParser.parse_flat(yaml)
assert_that(result["hud.mode"]).is_equal("Standard")
func test_flat_empty_quoted_value() -> void:
var yaml := 'hud:\n prefix: "" # No prefix'
var result := YamlParser.parse_flat(yaml)
assert_that(result.has("hud.prefix")).is_true()
assert_that(result["hud.prefix"]).is_equal("")
func test_flat_skips_arrays() -> void:
## Arrays have no dotted-key representation — they are skipped in flat output.
var yaml := "room_id: test\nconditions:\n - id: c1\n x: 5"
var result := YamlParser.parse_flat(yaml)
assert_that(result.has("room_id")).is_true()
# No dotted keys for array contents
assert_that(result.has("conditions")).is_false()
assert_that(result.has("conditions.0")).is_false()
func test_flat_empty_input() -> void:
var result := YamlParser.parse_flat("")
assert_that(result.size()).is_equal(0)
# -- Dialogue theme format (regression) ----------------------------------------
func test_dialogue_theme_format() -> void:
## dialogue-theme.yaml has top-level values and one nested map (npc_colors).
var yaml := "player_color: \"#e0e8ff\"\nnpc_colors:\n 0: \"#4a9ebb\"\n 1: \"#6bc9a6\"\npassive_opacity: 0.9"
var flat := YamlParser.parse_flat(yaml)
assert_that(flat["player_color"]).is_equal("#e0e8ff")
assert_that(flat["npc_colors.0"]).is_equal("#4a9ebb")
assert_that(flat["npc_colors.1"]).is_equal("#6bc9a6")
assert_that(flat["passive_opacity"]).is_equal("0.9")
# -- Checklist format (regression) ---------------------------------------------
func test_checklist_format() -> void:
## checklist.yaml: top-level kv + conditions array with typed values.
var yaml := "room_id: inventory_warehouse\nconditions:\n - id: test-1\n condition_type: player_near\n x: 10\n y: 20\n radius: 3.0"
var result := YamlParser.parse(yaml)
assert_that(result["room_id"]).is_equal("inventory_warehouse")
var cond: Dictionary = result["conditions"][0]
assert_that(cond["id"]).is_equal("test-1")
assert_that(cond["condition_type"]).is_equal("player_near")
assert_that(cond["x"]).is_equal(10)
assert_that(cond["y"]).is_equal(20)
assert_that(typeof(cond["radius"])).is_equal(TYPE_FLOAT)
+17 -12
View File
@@ -15,6 +15,11 @@ signal dialogue_dismissed # Walk-away or conversation end
signal confrontation_monologue(text: String, duration: float) # D-063: beat monologue
signal pause_requested # D-061: auto-pause — main.gd routes through input recording (#507)
signal unpause_requested # D-061: auto-unpause
# D-020 (#558): Decoupled signals — dialogue_box emits, main.gd (coordinator) handles.
# Replaces direct GameState.dialogue_active mutation and AudioManager calls.
signal dialogue_state_changed(active: bool)
signal audio_dip_requested(profile: String)
signal audio_dip_cleared
@onready var panel: PanelContainer = $PanelContainer
@onready var dialogue_log: RichTextLabel = $PanelContainer/MarginContainer/VBoxContainer/DialogueLog
@@ -286,10 +291,10 @@ func show_dialogue(npc_name: String, speech: String, options: Array = []) -> voi
# Show panel
_ensure_visible()
mouse_filter = Control.MOUSE_FILTER_STOP
GameState.dialogue_active = true # D-064: block movement while in conversation
dialogue_state_changed.emit(true) # D-064: coordinator blocks movement
# D-069: Dialogue dip
AudioManager.apply_dip("dialogue")
# D-069: Dialogue dip — coordinator routes to AudioManager
audio_dip_requested.emit("dialogue")
# D-061: auto-pause — signal to main.gd for input recording (#507)
pause_requested.emit()
@@ -311,14 +316,14 @@ func _end_player_conversation() -> void:
entry.timestamp_msec = now
_log_dirty = true
# D-069: Clear dialogue/confrontation dip
AudioManager.clear_dip()
# D-069: Clear dialogue/confrontation dip — coordinator routes to AudioManager
audio_dip_cleared.emit()
# D-061: unpause — signal to main.gd for input recording (#507)
unpause_requested.emit()
# D-064: unblock movement immediately — log entries stay visible but don't block input.
GameState.dialogue_active = false
# D-064: unblock movement immediately — coordinator handles GameState update.
dialogue_state_changed.emit(false)
# If no entries remain, hide the panel with fade.
if _log_entries.is_empty():
@@ -331,11 +336,11 @@ func hide_dialogue() -> void:
_end_player_conversation()
return # _end_player_conversation may call hide_dialogue if log is empty
GameState.dialogue_active = false
dialogue_state_changed.emit(false) # D-020: coordinator handles GameState update
func is_dialogue_active() -> bool:
return _in_player_conversation or GameState.dialogue_active
return _in_player_conversation
func has_active_entries() -> bool:
@@ -428,12 +433,12 @@ func _start_confrontation_beat(response_id: String, text: String) -> void:
_active_tween.tween_property(panel, "modulate:a", CONFRONTATION_DIM_ALPHA, 0.2)
confrontation_monologue.emit(UIStrings.get_text(CONFRONTATION_MONOLOGUE_KEY), CONFRONTATION_BEAT_DURATION)
AudioManager.apply_dip("confrontation")
audio_dip_requested.emit("confrontation") # D-069: coordinator routes to AudioManager
_beat_tween = create_tween()
_beat_tween.tween_interval(CONFRONTATION_BEAT_DURATION)
_beat_tween.tween_callback(func():
AudioManager.clear_dip()
audio_dip_cleared.emit() # D-069: coordinator routes to AudioManager
option_selected.emit(response_id, text)
_end_player_conversation()
)
@@ -443,7 +448,7 @@ func _cancel_beat() -> void:
if _beat_tween and _beat_tween.is_valid():
_beat_tween.kill()
_beat_tween = null
AudioManager.clear_dip()
audio_dip_cleared.emit() # D-069: coordinator routes to AudioManager
# -- Log rendering --
Binary file not shown.
+1 -1
View File
@@ -1,5 +1,5 @@
name: The Settled Reach
version: 0.1.19
version: 0.1.20
repository: settled-reach
codename: commonwealth
+1 -1
View File
@@ -1092,7 +1092,7 @@ dependencies = [
[[package]]
name = "settled-reach-server"
version = "0.1.15"
version = "0.1.19"
dependencies = [
"bevy_app",
"bevy_ecs",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "settled-reach-server"
version = "0.1.19"
version = "0.1.20"
edition = "2021"
[dependencies]
+31
View File
@@ -0,0 +1,31 @@
# Sample role schema for the dock-worker role in the terminal social site.
# Spec ref: D-023 (Tier 2 templates), D-024 (10-axis model), D-025 (social site)
#
# Tile scale note (D-066): tile_count_* fields are in SIM tiles (0.5m each).
# 1 visual tile = 2 sim tiles. A 15-40 visual tile space = 30-80 sim tiles.
role_id: "dock-worker"
required_traits:
- Honest
- Social
skill_focus:
- Technical
- Observation
relationship_constraints:
- with_role: "logistics-manager"
kind: Colleague
required_trust:
min: -2
max: 4
- with_role: "ring-contact"
kind: Colleague
required_trust:
min: -4
max: 0
routine_template:
- phase: morning
location: "terminal-cargo-bay"
- phase: afternoon
location: "terminal-cargo-bay"
- phase: evening
location: "bar-last-shift"
@@ -0,0 +1,18 @@
# Sample space spec for the terminal social site (Sova Logistics Hub).
# Spec ref: D-025 (15-40 visual tiles = 30-80 sim tiles), D-064 (dual-scale grid)
#
# Tile scale (D-066): all tile counts are SIM tiles (0.5m each).
# Terminal: 44×28 visual tiles = 88×56 sim tiles = 4928 sim tiles.
# Using a subsection for one functional zone: ~44×8 visual = 88×16 sim = 1408 sim.
tile_count_min: 30
tile_count_max: 80
sightline_zones:
- name: "loading-floor"
radius: 8 # 4m clear sightline across the loading area
- name: "reception-desk"
radius: 4 # 2m clear sightline at the desk
- name: "cargo-staging"
radius: 6 # 3m clear sightline in staging area
privacy_level: SemiPrivate
traffic_pattern: Destination
@@ -0,0 +1,24 @@
# Sample triangle definition: T4 Drin-System-Ring (D-087 active fork).
# Spec ref: D-024 (triangles as atomic social unit), D-087 (T4 configuration),
# D-089 (self-contained, no cross-triangle cascade)
#
# Note: triangle_id is computed at world-gen time from seed + roles.
# The value below is a placeholder for YAML authoring — the runtime
# calls TriangleId::from_seed_and_roles() to derive the actual ID.
triangle_id: 0
roles:
- "ring-leader"
- "dock-worker"
- "logistics-manager"
conflict_type: ResourceCompetition
interest_axes:
- Want
- Secret
- Relationships
relationship_constraints:
- with_role: "dock-worker"
kind: Subordinate
required_trust:
min: -2
max: 2
+2
View File
@@ -313,6 +313,7 @@ mod tests {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
}
}
@@ -448,6 +449,7 @@ mod tests {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
};
let text = format_snapshot_text(&snap);
assert!(text.contains("Tick 0"));
+40 -2
View File
@@ -17,7 +17,7 @@ pub use crate::simulation::time::{DayPhase, TickRate};
/// negotiation is unnecessary. Client should reject snapshots with version !=
/// PROTOCOL_VERSION. New fields use #[serde(default)] only during the migration
/// period, then the default is removed once both sides are updated.
pub const PROTOCOL_VERSION: u8 = 15;
pub const PROTOCOL_VERSION: u8 = 16;
/// Handshake message sent as the very first framed message after connection (#555).
/// Client reads this before entering the normal tick loop and validates
@@ -50,10 +50,11 @@ pub struct HandshakeMessage {
/// examine_result (#242, character-filtered examine observation text),
/// player_knowledge (#264, partial KG dump for journal/knowledge panel).
/// v15 adds: save_result (#553, save/load operation result for client confirmation).
/// v16 adds: triangle_crisis_events (#250, D-087 triangle escalation for future client rendering).
/// Future fields: ambient sound events, HUD state (D-020 expansion).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObserverSnapshot {
/// Protocol version for forward compatibility. Current: 15.
/// Protocol version for forward compatibility. Current: 16.
pub version: u8,
/// Simulation tick when this snapshot was produced
pub tick: u64,
@@ -151,6 +152,11 @@ pub struct ObserverSnapshot {
/// Client shows a confirmation toast (success) or error modal (failure).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub save_result: Option<SaveLoadResultWire>,
/// Triangle crisis events this tick (#250, D-087).
/// Emitted when a triangle enters Active phase. Client may render
/// a narrative event or HUD indicator. Empty when no crises occur.
#[serde(default)]
pub triangle_crisis_events: Vec<TriangleCrisisEventWire>,
}
/// Game time data for client display (D-031)
@@ -676,6 +682,38 @@ pub struct SaveLoadResultWire {
pub error: Option<String>,
}
/// Triangle crisis event for future client rendering (#250, D-087).
///
/// Emitted when a triangle transitions from Simmering to Active.
/// Client may display a narrative beat, HUD indicator, or tension meter.
/// Wire format uses primitives for cross-boundary safety.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TriangleCrisisEventWire {
/// Triangle identifier (TriangleId as u64).
pub triangle_id: u64,
/// NPC role assignments: (role_slug, stable_npc_id).
pub role_assignments: Vec<(String, u64)>,
/// The NPC whose tolerance threshold triggered the crisis.
pub trigger_npc_id: u64,
/// Tick when the crisis was triggered.
pub tick: u64,
}
impl From<crate::content::template::TriangleCrisisEvent> for TriangleCrisisEventWire {
fn from(e: crate::content::template::TriangleCrisisEvent) -> Self {
Self {
triangle_id: e.triangle_id.into(),
role_assignments: e
.role_assignments
.into_iter()
.map(|(role, sid)| (String::from(role), u64::from(sid)))
.collect(),
trigger_npc_id: e.trigger_npc.into(),
tick: e.tick,
}
}
}
/// Snapshot buffer resource for staging outgoing ObserverSnapshots
#[derive(Resource, Debug, Default)]
pub struct SnapshotBuffer {
+1
View File
@@ -14,6 +14,7 @@ pub mod hot_reload;
pub mod line_pool;
pub mod loader;
pub mod spawn;
pub mod template;
pub mod types;
use bevy_app::prelude::*;
File diff suppressed because it is too large Load Diff
+6
View File
@@ -18,6 +18,12 @@ use crate::simulation::movement::TilePosition;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct StableId(pub u64);
impl From<StableId> for u64 {
fn from(id: StableId) -> Self {
id.0
}
}
/// Typed fact identifier for non-entity knowledge.
/// Format: "category.topic" (e.g., "contraband.ring_exists").
/// Lexicographic ordering in BTreeMap provides deterministic iteration.
+1
View File
@@ -213,6 +213,7 @@ mod tests {
world.init_resource::<ObservationEventQueue>();
world.init_resource::<VisibilityGeometry>();
world.init_resource::<ActivePerceptionMode>();
world.init_resource::<crate::content::template::TriangleCrisisEventQueue>();
world
}
+1
View File
@@ -168,6 +168,7 @@ mod tests {
world.init_resource::<EntityRegistry>();
world.init_resource::<VisibilityGeometry>();
world.init_resource::<ActivePerceptionMode>();
world.init_resource::<crate::content::template::TriangleCrisisEventQueue>();
world
}
+10
View File
@@ -27,6 +27,7 @@ use crate::simulation::monologue::{MonologueBuffer, SprintAnomalyQueue};
use crate::simulation::movement::{PlayerCharacter, TilePosition, WalkabilityMap};
use crate::simulation::poi::PointOfInterest;
use crate::simulation::rng::SimRng;
use crate::content::template::TriangleCrisisEventQueue;
use crate::simulation::sound::SoundEventQueue;
use crate::simulation::stance::Stance;
use crate::simulation::time::SimulationTime;
@@ -99,6 +100,7 @@ pub fn compute_observer_snapshot(
inventory_items: Query<(Entity, &CarriedBy, &ItemName, &InventorySlot)>,
poi_query: Query<&PointOfInterest>,
mut buffer: ResMut<SnapshotBuffer>,
mut crisis_queue: ResMut<TriangleCrisisEventQueue>,
sim_rng: Option<Res<SimRng>>,
pressure_query: Query<&crate::simulation::pressure::CharacterPressure, With<PlayerCharacter>>,
) {
@@ -387,6 +389,13 @@ pub fn compute_observer_snapshot(
// Consume pending save/load result for this tick (#553).
let save_result = buffer.pending_save_result.take();
// Drain triangle crisis events (#250) and convert to wire format.
let triangle_crisis_events = crisis_queue
.drain()
.into_iter()
.map(TriangleCrisisEventWire::from)
.collect();
buffer.snapshot = Some(ObserverSnapshot {
version: crate::bridge::types::PROTOCOL_VERSION,
tick: time.tick,
@@ -414,6 +423,7 @@ pub fn compute_observer_snapshot(
examine_result,
player_knowledge,
save_result,
triangle_crisis_events,
});
}
+1
View File
@@ -16,6 +16,7 @@ fn setup_world(width: i32, height: i32) -> World {
world.init_resource::<VisibilityGeometry>();
world.init_resource::<ActivePerceptionMode>();
world.init_resource::<crate::simulation::sound::SoundEventQueue>();
world.init_resource::<crate::content::template::TriangleCrisisEventQueue>();
world
}
+11
View File
@@ -51,6 +51,9 @@ impl Plugin for SimulationPlugin {
.init_resource::<follow::FollowEndEventQueue>()
.init_resource::<monologue::PostConversationQueue>()
.init_resource::<poi_discovery::PoiDiscoveryEventQueue>()
// Triangle escalation resources (#250)
.init_resource::<crate::content::template::TriangleCrisisEventQueue>()
.init_resource::<crate::content::template::ResolveTriangleQueue>()
// discover_pois reads VisibilityGeometry (also populated by PerceptionPlugin).
// Init here so SimulationPlugin works standalone in tests without PerceptionPlugin.
.init_resource::<crate::perception::query::VisibilityGeometry>()
@@ -96,6 +99,14 @@ impl Plugin for SimulationPlugin {
pressure::update_character_pressure
.after(crate::npc::awareness::detect_player_awareness)
.before(crate::perception::observer::compute_observer_snapshot),
// Triangle escalation (#250) — runs on game-minute boundaries (every 10 ticks)
crate::content::template::tick_triangle_escalation
.after(crate::npc::tolerance::check_tolerance_threshold)
.before(crate::perception::observer::compute_observer_snapshot),
// Triangle resolution (#250, D-089) — apply player resolve commands
crate::content::template::apply_resolve_triangle
.after(input::process_player_input)
.before(crate::perception::observer::compute_observer_snapshot),
time::advance_tick.after(path_follow::cleanup_path_blocked),
),
);
+26
View File
@@ -16,6 +16,7 @@ use thiserror::Error;
use crate::bridge::types::SaveLoadResultWire;
use crate::bridge::types::SnapshotBuffer;
use crate::content::template::{TemplateReferenceMap, TriangleState};
use crate::knowledge::graph::KnowledgeGraph;
use crate::knowledge::registry::EntityRegistry;
use crate::npc::Npc;
@@ -105,6 +106,20 @@ pub fn save_to_file(path: &Path, world: &mut World) -> Result<(), SaveLoadError>
npc_states.sort_by_key(|s| s.stable_id.0);
let npc_count = npc_states.len();
// Capture TemplateReferenceMap if present — default to empty if not yet initialised.
let template_references = world
.get_resource::<TemplateReferenceMap>()
.cloned()
.unwrap_or_default();
// Capture TriangleState components — sorted by triangle_id for determinism (D-010).
let mut triangle_states: Vec<TriangleState> = {
let mut q = world.query::<&TriangleState>();
q.iter(world).cloned().collect()
};
triangle_states.sort_by_key(|t| t.triangle_id.0);
let state = SaveStateV1 {
format_version: SAVE_FORMAT_VERSION,
tick,
@@ -113,6 +128,8 @@ pub fn save_to_file(path: &Path, world: &mut World) -> Result<(), SaveLoadError>
player_knowledge,
relationship_graph,
npc_states,
template_references,
triangle_states,
};
let bytes = state
@@ -200,6 +217,12 @@ pub fn load_from_file(path: &Path, world: &mut World) -> Result<(), SaveLoadErro
// Restore simulation resources.
world.insert_resource(state.relationship_graph);
world.insert_resource(state.template_references);
// Restore triangle states (#250) — spawn dedicated entities for each.
for ts in &state.triangle_states {
world.spawn(ts.clone());
}
{
let mut t = world.resource_mut::<SimulationTime>();
t.tick = state.tick;
@@ -487,6 +510,7 @@ mod tests {
#[test]
fn load_from_file_rejects_wrong_format_version() {
use crate::content::template::TemplateReferenceMap;
// Craft a save with a wrong format_version
let bad_state = SaveStateV1 {
format_version: 0xFF, // deliberately wrong
@@ -496,6 +520,8 @@ mod tests {
player_knowledge: KnowledgeGraph::new(),
relationship_graph: RelationshipGraph::new(),
npc_states: vec![],
template_references: TemplateReferenceMap::default(),
triangle_states: vec![],
};
let bytes = bad_state.to_bytes().expect("serialize");
let path = temp_path();
+30
View File
@@ -39,6 +39,7 @@ use bevy_ecs::entity::Entity;
use bevy_ecs::world::World;
use serde::{Deserialize, Serialize};
use crate::content::template::{TemplateOwnership, TemplateReferenceMap, TriangleState};
use crate::knowledge::graph::KnowledgeGraph;
use crate::knowledge::registry::StableEntityId;
use crate::knowledge::types::StableId;
@@ -83,6 +84,16 @@ pub struct SaveStateV1 {
/// Per-NPC summary state for each simulated NPC.
/// Order is deterministic (sorted by stable_id in ascending order).
pub npc_states: Vec<NpcSaveState>,
/// Cross-template reference links (#165).
/// Preserved across save/load so that tier-evicted templates retain their
/// relationship metadata even when their NPCs are not in Active tier.
#[serde(default)]
pub template_references: TemplateReferenceMap,
/// Triangle escalation states (#250).
/// Persisted so tension/phase survive save/load. Sorted by triangle_id
/// for deterministic serialization (D-010).
#[serde(default)]
pub triangle_states: Vec<TriangleState>,
}
/// Per-NPC state snapshot for `SaveStateV1`.
@@ -184,6 +195,12 @@ pub struct NpcSaveState {
/// Job performance score — drifts over time, persist across tier transitions.
#[serde(default)]
pub job_performance: Option<JobPerformance>,
/// Template ownership (#165): which template owns this NPC and which role it fills.
/// `None` for NPCs that predate the template system or were hand-authored without
/// template assignment. Preserved across tier transitions (D-025 single-ownership).
#[serde(default)]
pub template_ownership: Option<TemplateOwnership>,
}
impl SaveStateV1 {
@@ -258,6 +275,7 @@ pub fn serialize_npc_to_frozen(entity: Entity, world: &World) -> NpcSaveState {
combat_capability: world.get::<CombatCapability>(entity).cloned(),
mood_state: world.get::<MoodState>(entity).cloned(),
job_performance: world.get::<JobPerformance>(entity).cloned(),
template_ownership: world.get::<TemplateOwnership>(entity).cloned(),
}
}
@@ -353,6 +371,10 @@ pub fn deserialize_npc_from_frozen(state: &NpcSaveState, world: &mut World) -> E
if let Some(combat) = state.combat_capability.clone() {
em.insert(combat);
}
// Restore template ownership if present — never reassigned after initial spawn (D-025).
if let Some(ownership) = state.template_ownership.clone() {
em.insert(ownership);
}
}
entity
@@ -381,6 +403,8 @@ mod tests {
player_knowledge: KnowledgeGraph::new(),
relationship_graph: RelationshipGraph::new(),
npc_states: vec![],
template_references: TemplateReferenceMap::default(),
triangle_states: vec![],
}
}
@@ -449,6 +473,7 @@ mod tests {
combat_capability: None,
mood_state: None,
job_performance: None,
template_ownership: None,
},
NpcSaveState {
stable_id: StableId(202),
@@ -469,6 +494,7 @@ mod tests {
combat_capability: None,
mood_state: None,
job_performance: None,
template_ownership: None,
},
];
@@ -577,6 +603,7 @@ mod tests {
combat_capability: None,
mood_state: None,
job_performance: None,
template_ownership: None,
}];
let bytes = state.to_bytes().expect("serialize");
@@ -734,6 +761,7 @@ mod tests {
combat_capability: None,
mood_state: None,
job_performance: None,
template_ownership: None,
};
let mut world = World::new();
@@ -771,6 +799,8 @@ mod tests {
player_knowledge: KnowledgeGraph::new(),
relationship_graph: RelationshipGraph::new(),
npc_states: vec![frozen],
template_references: TemplateReferenceMap::default(),
triangle_states: vec![],
};
let bytes = save.to_bytes().expect("serialize");
+1
View File
@@ -72,6 +72,7 @@ fn snapshot_roundtrip_over_unix_socket() {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
};
bridge
+1
View File
@@ -58,6 +58,7 @@ fn snapshot_roundtrip_over_tcp() {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
};
bridge
+3
View File
@@ -48,6 +48,7 @@ fn fixture_snapshot(tick: u64, entities: Vec<VisibleEntity>) -> ObserverSnapshot
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
}
}
@@ -239,6 +240,7 @@ fn generate_msgpack_fixtures() {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
};
write_fixture(
"snapshot_v2_full",
@@ -400,6 +402,7 @@ fn generate_msgpack_fixtures() {
acquired_tick: 10,
}],
}),
triangle_crisis_events: vec![],
};
write_fixture(
"snapshot_full",
+2 -1
View File
@@ -73,7 +73,8 @@
"scan_events": [],
"sound_events": [],
"tick": 8,
"version": 15,
"triangle_crisis_events": [],
"version": 16,
"visible_tiles": [
{
"tile_kind": "Wall",
+4
View File
@@ -169,6 +169,7 @@ fn save_state_npc_kg_isolation() {
combat_capability: None,
mood_state: None,
job_performance: None,
template_ownership: None,
};
// NPC_B (Background tier) does not carry a KG.
@@ -191,6 +192,7 @@ fn save_state_npc_kg_isolation() {
combat_capability: None,
mood_state: None,
job_performance: None,
template_ownership: None,
};
let save = SaveStateV1 {
@@ -201,6 +203,8 @@ fn save_state_npc_kg_isolation() {
player_knowledge: KnowledgeGraph::new(),
relationship_graph: RelationshipGraph::new(),
npc_states: vec![npc_a_state, npc_b_state],
template_references: Default::default(),
triangle_states: vec![],
};
// Roundtrip: serialize → deserialize.
+4 -1
View File
@@ -36,6 +36,7 @@ fn test_snapshot(tick: u64, entities: Vec<VisibleEntity>) -> ObserverSnapshot {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
}
}
@@ -294,6 +295,7 @@ fn snapshot_v2_fields_roundtrip() {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
};
let bytes = rmp_serde::to_vec_named(&snapshot).expect("serialize");
@@ -348,7 +350,7 @@ fn protocol_version_constant_matches_snapshot() {
let snapshot = test_snapshot(0, vec![]);
assert_eq!(snapshot.version, PROTOCOL_VERSION);
assert_eq!(
PROTOCOL_VERSION, 15,
PROTOCOL_VERSION, 16,
"bump this assertion when protocol version changes"
);
}
@@ -398,6 +400,7 @@ fn all_facing_direction_variants_roundtrip() {
examine_result: None,
player_knowledge: None,
save_result: None,
triangle_crisis_events: vec![],
};
let bytes = rmp_serde::to_vec_named(&snapshot).expect("serialize");
let decoded: ObserverSnapshot = rmp_serde::from_slice(&bytes).expect("deserialize");
+639
View File
@@ -0,0 +1,639 @@
//! Integration tests for the template schema system (tickets #163, #164, #165, #106).
//!
//! Tests YAML round-trips, validation logic, and ECS component interactions
//! against the spec decisions:
//! - D-023: three-tier content model
//! - D-024: 10-axis NPC model, triangles as atomic unit
//! - D-025: social site / single-ownership model
//! - D-087: v0.1 triangle configuration
//! - D-089: self-contained triangle forks, no cross-triangle cascade
//! - D-010: determinism (no HashMap, FNV-1a IDs)
use settled_reach_server::content::template::{
validate_role_schemas_no_duplicate_ids, ConflictType, NpcAxis, PrivacyLevel,
RelationshipConstraint, RoleId, RoleSchema, SpaceSpec, TemplateId,
TemplateOwnership, TemplateReference, TemplateReferenceMap, TemplateRoutineEntry,
TrafficPattern, TriangleDef, TriangleId, TrustRange,
};
use settled_reach_server::npc::{PersonalityTrait, RelationshipKind, Skill};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
fn make_role_schema(id: &str) -> RoleSchema {
RoleSchema {
role_id: RoleId::new(id),
required_traits: vec![],
skill_focus: vec![],
relationship_constraints: vec![],
routine_template: vec![],
}
}
fn make_triangle(roles: [&str; 3], conflict: ConflictType) -> TriangleDef {
let role_arr = [
RoleId::new(roles[0]),
RoleId::new(roles[1]),
RoleId::new(roles[2]),
];
let triangle_id = TriangleId::from_seed_and_roles(42, &role_arr);
TriangleDef {
triangle_id,
roles: role_arr,
conflict_type: conflict,
interest_axes: [NpcAxis::Want, NpcAxis::Secret, NpcAxis::Relationships],
relationship_constraints: vec![],
}
}
// ---------------------------------------------------------------------------
// #163: Role definition schema — YAML round-trips
// ---------------------------------------------------------------------------
#[test]
fn role_schema_minimal_yaml_parse() {
let yaml = r#"
role_id: "guard"
skill_focus:
- Combat
- Observation
"#;
let schema: RoleSchema = serde_yaml::from_str(yaml).expect("minimal schema must parse");
assert_eq!(schema.role_id, RoleId::new("guard"));
assert_eq!(schema.skill_focus.len(), 2);
assert!(schema.required_traits.is_empty());
assert!(schema.relationship_constraints.is_empty());
assert!(schema.routine_template.is_empty());
}
#[test]
fn role_schema_full_yaml_parse() {
let yaml = r#"
role_id: "dock-worker"
required_traits:
- Cautious
- Honest
skill_focus:
- Technical
- Observation
relationship_constraints:
- with_role: "ring-contact"
kind: Colleague
required_trust:
min: -2
max: 2
routine_template:
- phase: "morning"
location: "terminal-cargo-bay"
activity: "freight-handling"
- phase: "evening"
location: "bar-last-shift"
"#;
let schema: RoleSchema = serde_yaml::from_str(yaml).expect("full schema must parse");
assert_eq!(schema.role_id, RoleId::new("dock-worker"));
assert_eq!(schema.required_traits.len(), 2);
assert_eq!(schema.required_traits[0], PersonalityTrait::Cautious);
assert_eq!(schema.skill_focus.len(), 2);
assert_eq!(schema.relationship_constraints.len(), 1);
assert_eq!(
schema.relationship_constraints[0].with_role,
RoleId::new("ring-contact")
);
assert_eq!(schema.relationship_constraints[0].required_trust.min, -2);
assert_eq!(schema.relationship_constraints[0].required_trust.max, 2);
assert_eq!(schema.routine_template.len(), 2);
assert_eq!(schema.routine_template[0].phase, "morning");
assert_eq!(schema.routine_template[0].activity, Some("freight-handling".to_string()));
assert_eq!(schema.routine_template[1].activity, None);
}
#[test]
fn role_schema_yaml_roundtrip_preserves_all_fields() {
let schema = RoleSchema {
role_id: RoleId::new("ring-contact"),
required_traits: vec![PersonalityTrait::Deceptive, PersonalityTrait::Social],
skill_focus: vec![Skill::Stealth, Skill::Persuasion],
relationship_constraints: vec![
RelationshipConstraint {
with_role: RoleId::new("dock-worker"),
kind: RelationshipKind::Colleague,
required_trust: TrustRange { min: 0, max: 4 },
},
RelationshipConstraint {
with_role: RoleId::new("ring-leader"),
kind: RelationshipKind::Superior,
required_trust: TrustRange { min: 1, max: 4 },
},
],
routine_template: vec![
TemplateRoutineEntry {
phase: "morning".into(),
location: "terminal-cargo-bay".into(),
activity: Some("oversight".into()),
},
TemplateRoutineEntry {
phase: "evening".into(),
location: "maintenance-corridor".into(),
activity: None,
},
],
};
let yaml = serde_yaml::to_string(&schema).expect("serialize");
let restored: RoleSchema = serde_yaml::from_str(&yaml).expect("deserialize");
assert_eq!(restored.role_id, schema.role_id);
assert_eq!(restored.required_traits, schema.required_traits);
assert_eq!(restored.skill_focus, schema.skill_focus);
assert_eq!(
restored.relationship_constraints.len(),
schema.relationship_constraints.len()
);
assert_eq!(
restored.relationship_constraints[0].required_trust,
schema.relationship_constraints[0].required_trust
);
assert_eq!(restored.routine_template.len(), schema.routine_template.len());
assert_eq!(
restored.routine_template[0].activity,
schema.routine_template[0].activity
);
}
// ---------------------------------------------------------------------------
// #163: Role definition schema — validation
// ---------------------------------------------------------------------------
#[test]
fn self_referential_constraint_rejected() {
let schema = RoleSchema {
role_id: RoleId::new("dock-worker"),
required_traits: vec![],
skill_focus: vec![],
relationship_constraints: vec![RelationshipConstraint {
with_role: RoleId::new("dock-worker"), // same as role_id
kind: RelationshipKind::Colleague,
required_trust: TrustRange { min: 0, max: 4 },
}],
routine_template: vec![],
};
let result = schema.validate();
assert!(result.is_err(), "self-referential constraint must be rejected");
assert!(
result.unwrap_err().contains("self-referential"),
"error must mention self-referential"
);
}
#[test]
fn invalid_trust_range_rejected() {
let schema = RoleSchema {
role_id: RoleId::new("guard"),
required_traits: vec![],
skill_focus: vec![],
relationship_constraints: vec![RelationshipConstraint {
with_role: RoleId::new("captain"),
kind: RelationshipKind::Superior,
required_trust: TrustRange { min: 3, max: 1 }, // invalid: min > max
}],
routine_template: vec![],
};
let result = schema.validate();
assert!(result.is_err(), "TrustRange min > max must be rejected");
assert!(
result.unwrap_err().contains("trust min"),
"error must mention trust min"
);
}
#[test]
fn collection_with_duplicate_role_ids_rejected() {
let schemas = vec![
make_role_schema("dock-worker"),
make_role_schema("ring-contact"),
make_role_schema("dock-worker"), // duplicate
];
let result = validate_role_schemas_no_duplicate_ids(&schemas);
assert!(result.is_err(), "duplicate role_ids must be rejected");
let msg = result.unwrap_err();
assert!(msg.contains("dock-worker"), "error must name the duplicate: {}", msg);
}
#[test]
fn collection_with_unique_role_ids_ok() {
let schemas = vec![
make_role_schema("dock-worker"),
make_role_schema("ring-contact"),
make_role_schema("logistics-manager"),
];
assert!(validate_role_schemas_no_duplicate_ids(&schemas).is_ok());
}
// ---------------------------------------------------------------------------
// #164: Spatial requirement specification — YAML round-trips
// ---------------------------------------------------------------------------
#[test]
fn space_spec_minimal_yaml_parse() {
let yaml = r#"
tile_count_min: 30
tile_count_max: 80
privacy_level: Public
traffic_pattern: Thoroughfare
"#;
let spec: SpaceSpec = serde_yaml::from_str(yaml).expect("minimal SpaceSpec must parse");
assert_eq!(spec.tile_count_min, 30);
assert_eq!(spec.tile_count_max, 80);
assert_eq!(spec.privacy_level, PrivacyLevel::Public);
assert_eq!(spec.traffic_pattern, TrafficPattern::Thoroughfare);
assert!(spec.sightline_zones.is_empty());
}
#[test]
fn space_spec_full_yaml_parse() {
let yaml = r#"
tile_count_min: 30
tile_count_max: 80
sightline_zones:
- name: "bar-counter"
radius: 4
- name: "corner-booth"
radius: 2
privacy_level: SemiPrivate
traffic_pattern: Destination
"#;
let spec: SpaceSpec = serde_yaml::from_str(yaml).expect("full SpaceSpec must parse");
assert_eq!(spec.sightline_zones.len(), 2);
assert_eq!(spec.sightline_zones[0].name, "bar-counter");
assert_eq!(spec.sightline_zones[0].radius, 4);
assert_eq!(spec.sightline_zones[1].name, "corner-booth");
assert_eq!(spec.sightline_zones[1].radius, 2);
assert_eq!(spec.privacy_level, PrivacyLevel::SemiPrivate);
assert_eq!(spec.traffic_pattern, TrafficPattern::Destination);
}
/// D-025 scale assertion: 15-40 visual tiles = 30-80 sim tiles (D-066).
#[test]
fn space_spec_d025_tile_count_range() {
let spec = SpaceSpec {
tile_count_min: 30,
tile_count_max: 80,
sightline_zones: vec![],
privacy_level: PrivacyLevel::Public,
traffic_pattern: TrafficPattern::Destination,
};
assert!(spec.validate().is_ok(), "D-025 tile range (30-80 sim) must be valid");
}
#[test]
fn space_spec_validation_min_gt_max_fails() {
let spec = SpaceSpec {
tile_count_min: 100,
tile_count_max: 50,
sightline_zones: vec![],
privacy_level: PrivacyLevel::Private,
traffic_pattern: TrafficPattern::Restricted,
};
let result = spec.validate();
assert!(result.is_err(), "min > max must fail validation");
let msg = result.unwrap_err();
assert!(msg.contains("tile_count_min"), "error must mention tile_count_min: {}", msg);
}
#[test]
fn all_privacy_levels_yaml_roundtrip() {
for level in &[PrivacyLevel::Public, PrivacyLevel::SemiPrivate, PrivacyLevel::Private] {
let yaml = serde_yaml::to_string(level).unwrap();
let decoded: PrivacyLevel = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(level, &decoded, "{:?} must survive YAML round-trip", level);
}
}
#[test]
fn all_traffic_patterns_yaml_roundtrip() {
for pattern in &[
TrafficPattern::Thoroughfare,
TrafficPattern::Destination,
TrafficPattern::Restricted,
] {
let yaml = serde_yaml::to_string(pattern).unwrap();
let decoded: TrafficPattern = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(pattern, &decoded, "{:?} must survive YAML round-trip", pattern);
}
}
// ---------------------------------------------------------------------------
// #165: Single-ownership model — TemplateId determinism
// ---------------------------------------------------------------------------
#[test]
fn template_id_fnv1a_stable_across_calls() {
let id = TemplateId::from_seed_and_slug(0, "");
assert_eq!(
id,
TemplateId::from_seed_and_slug(0, ""),
"empty slug + seed 0 must be stable"
);
let id2 = TemplateId::from_seed_and_slug(42, "the-terminal");
assert_eq!(
id2,
TemplateId::from_seed_and_slug(42, "the-terminal"),
"non-empty slug must be stable"
);
}
#[test]
fn template_ownership_component_single_owner_invariant() {
// D-025: NPCs are owned by exactly one template, never reassigned.
let seed = 1u64;
let tid = TemplateId::from_seed_and_slug(seed, "terminal");
let rid = RoleId::new("dock-worker");
let ownership = TemplateOwnership { template_id: tid, role_id: rid.clone() };
assert_eq!(ownership.template_id, tid);
assert_eq!(ownership.role_id, rid);
// Clone (as would happen in save-state) must preserve values.
let cloned = ownership.clone();
assert_eq!(cloned.template_id, ownership.template_id);
assert_eq!(cloned.role_id, ownership.role_id);
}
#[test]
fn template_reference_map_preserves_links_on_unload() {
// D-025: reference links must be preserved when a template is unloaded.
let mut map = TemplateReferenceMap::default();
let tid_a = TemplateId::from_seed_and_slug(1, "template-a");
let tid_b = TemplateId::from_seed_and_slug(1, "template-b");
map.add(TemplateReference {
from_template: tid_a,
to_template: tid_b,
via_role: RoleId::new("ring-contact"),
relationship_metadata: RelationshipKind::Colleague,
});
// Simulate "unload template-a" by cloning (the save path).
let preserved = map.clone();
assert_eq!(preserved.outgoing(tid_a).len(), 1);
assert_eq!(preserved.outgoing(tid_a)[0].to_template, tid_b);
}
#[test]
fn template_reference_map_btreemap_deterministic_ordering() {
// D-010: BTreeMap ensures deterministic iteration order.
let mut map = TemplateReferenceMap::default();
let tid_high = TemplateId(u64::MAX - 1);
let tid_low = TemplateId(1);
map.add(TemplateReference {
from_template: tid_high,
to_template: tid_low,
via_role: RoleId::new("role-a"),
relationship_metadata: RelationshipKind::Colleague,
});
map.add(TemplateReference {
from_template: tid_low,
to_template: tid_high,
via_role: RoleId::new("role-b"),
relationship_metadata: RelationshipKind::Colleague,
});
// Collect all references via all_references() (deterministic BTreeMap order).
let all: Vec<&TemplateReference> = map.all_references().collect();
assert_eq!(all.len(), 2);
// First entry's from_template must be the lower ID (BTreeMap key order).
assert!(
all[0].from_template <= all[1].from_template,
"BTreeMap must iterate in ascending key order"
);
}
// ---------------------------------------------------------------------------
// #106: Triangle definition schema — YAML round-trips
// ---------------------------------------------------------------------------
#[test]
fn triangle_def_yaml_parse_with_computed_id() {
// TriangleId is stored in YAML but computed at world-gen time.
// Authors use 0 as placeholder; runtime overwrites with computed value.
let yaml = r#"
triangle_id: 0
roles:
- "ring-smuggler"
- "dock-worker"
- "operations-manager"
conflict_type: ResourceCompetition
interest_axes:
- Want
- Secret
- Relationships
"#;
let def: TriangleDef = serde_yaml::from_str(yaml).expect("TriangleDef must parse from YAML");
assert_eq!(def.triangle_id, TriangleId(0));
assert_eq!(def.roles[0], RoleId::new("ring-smuggler"));
assert_eq!(def.conflict_type, ConflictType::ResourceCompetition);
assert!(def.relationship_constraints.is_empty());
}
#[test]
fn triangle_def_yaml_parse_with_constraints() {
let yaml = r#"
triangle_id: 0
roles:
- "ring-leader"
- "dock-worker"
- "logistics-manager"
conflict_type: LoyaltyConflict
interest_axes:
- Relationships
- Secret
- Tolerance
relationship_constraints:
- with_role: "dock-worker"
kind: Subordinate
required_trust:
min: -2
max: 2
"#;
let def: TriangleDef =
serde_yaml::from_str(yaml).expect("TriangleDef with constraints must parse");
assert_eq!(def.conflict_type, ConflictType::LoyaltyConflict);
assert_eq!(def.relationship_constraints.len(), 1);
assert_eq!(def.relationship_constraints[0].with_role, RoleId::new("dock-worker"));
assert_eq!(def.relationship_constraints[0].kind, RelationshipKind::Subordinate);
}
#[test]
fn triangle_def_all_conflict_types_yaml_roundtrip() {
let conflict_types = [
ConflictType::ResourceCompetition,
ConflictType::LoyaltyConflict,
ConflictType::SecretExposure,
ConflictType::AuthorityChallenge,
ConflictType::LatentTension,
];
for ct in &conflict_types {
let yaml = serde_yaml::to_string(ct).unwrap();
let decoded: ConflictType = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(ct, &decoded, "{:?} must round-trip", ct);
}
}
#[test]
fn triangle_def_all_npc_axes_yaml_roundtrip() {
let axes = [
NpcAxis::Want,
NpcAxis::Secret,
NpcAxis::Relationships,
NpcAxis::Tolerance,
NpcAxis::Routine,
NpcAxis::InformationInventory,
NpcAxis::Contentment,
NpcAxis::PersonalityTraits,
NpcAxis::TellSystem,
NpcAxis::SkillSet,
];
for axis in &axes {
let yaml = serde_yaml::to_string(axis).unwrap();
let decoded: NpcAxis = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(axis, &decoded, "{:?} must round-trip", axis);
}
}
/// D-087: T1-T5 triangle configuration must be expressible in the schema.
#[test]
fn d087_v01_triangle_configurations_expressible() {
// T1: Kael-Smuggler-Ring (ResourceCompetition, active fork)
let t1 = make_triangle(
["kael-davan", "smuggler", "ring-contact"],
ConflictType::ResourceCompetition,
);
assert!(t1.validate().is_ok(), "T1 must be valid: {:?}", t1.validate());
// T2: Sera-Detective-Commission (SecretExposure, active fork)
let t2 = make_triangle(
["sera-venn", "detective", "commission-inspector"],
ConflictType::SecretExposure,
);
assert!(t2.validate().is_ok(), "T2 must be valid: {:?}", t2.validate());
// T4: Drin-System-Ring (ResourceCompetition, active fork per D-087)
let t4 = make_triangle(
["drin", "ring-system", "dock-supervisor"],
ConflictType::ResourceCompetition,
);
assert!(t4.validate().is_ok(), "T4 must be valid: {:?}", t4.validate());
// T3: passive tension (LatentTension variant per D-087)
let t3 = make_triangle(["naia", "kael-davan", "hael"], ConflictType::LatentTension);
assert!(t3.validate().is_ok(), "T3 passive tension must be valid: {:?}", t3.validate());
// T5: background worried partner (LatentTension variant)
let t5 = make_triangle(
["worried-partner", "ring-member", "neighbor"],
ConflictType::LatentTension,
);
assert!(t5.validate().is_ok(), "T5 passive tension must be valid: {:?}", t5.validate());
}
/// D-089: TriangleDef must not contain cross-triangle cascade state.
#[test]
fn d089_no_cross_triangle_cascade_fields() {
let def = make_triangle(["role-a", "role-b", "role-c"], ConflictType::ResourceCompetition);
let yaml = serde_yaml::to_string(&def).expect("serialize");
assert!(!yaml.contains("cascade"), "no cascade field should appear in serialized TriangleDef");
assert!(!yaml.contains("cross_triangle"), "no cross_triangle field should appear");
assert!(!yaml.contains("triggers"), "no triggers field should appear");
}
// ---------------------------------------------------------------------------
// #165: ECS integration — spawn two templates with cross-references
// ---------------------------------------------------------------------------
#[test]
fn ecs_two_templates_with_cross_references_and_ownerships() {
use bevy_ecs::world::World;
let seed = 999u64;
let tid_terminal = TemplateId::from_seed_and_slug(seed, "terminal-social-site");
let tid_bar = TemplateId::from_seed_and_slug(seed, "last-shift-bar");
let mut world = World::new();
world.init_resource::<TemplateReferenceMap>();
// Spawn 3 NPCs: 2 in terminal, 1 in bar.
let npc_logistics = world
.spawn(TemplateOwnership {
template_id: tid_terminal,
role_id: RoleId::new("logistics-manager"),
})
.id();
let npc_dock = world
.spawn(TemplateOwnership {
template_id: tid_terminal,
role_id: RoleId::new("dock-worker"),
})
.id();
let npc_bar_regular = world
.spawn(TemplateOwnership {
template_id: tid_bar,
role_id: RoleId::new("bar-regular"),
})
.id();
// Add cross-template reference: dock-worker at terminal references bar-regular at bar.
{
let mut ref_map = world.resource_mut::<TemplateReferenceMap>();
ref_map.add(TemplateReference {
from_template: tid_terminal,
to_template: tid_bar,
via_role: RoleId::new("dock-worker"),
relationship_metadata: RelationshipKind::Colleague,
});
}
// Verify all TemplateOwnership components are correct.
let own_logistics = world.get::<TemplateOwnership>(npc_logistics).unwrap();
assert_eq!(
own_logistics.template_id, tid_terminal,
"logistics-manager must be owned by terminal"
);
assert_eq!(own_logistics.role_id, RoleId::new("logistics-manager"));
let own_dock = world.get::<TemplateOwnership>(npc_dock).unwrap();
assert_eq!(
own_dock.template_id, tid_terminal,
"dock-worker must be owned by terminal"
);
assert_eq!(own_dock.role_id, RoleId::new("dock-worker"));
let own_bar = world.get::<TemplateOwnership>(npc_bar_regular).unwrap();
assert_eq!(own_bar.template_id, tid_bar, "bar-regular must be owned by bar");
// Verify TemplateReferenceMap entries.
let ref_map = world.resource::<TemplateReferenceMap>();
let terminal_refs = ref_map.outgoing(tid_terminal);
assert_eq!(terminal_refs.len(), 1, "terminal should have 1 cross-template reference");
assert_eq!(terminal_refs[0].to_template, tid_bar);
assert_eq!(terminal_refs[0].via_role, RoleId::new("dock-worker"));
// Bar template has no outgoing references.
assert!(
ref_map.outgoing(tid_bar).is_empty(),
"bar template has no outgoing references"
);
}
#[test]
fn template_ownership_survives_clone_for_save_state() {
// D-026: TemplateOwnership must be preserved when tier drops to State-saved.
let tid = TemplateId::from_seed_and_slug(42, "terminal");
let rid = RoleId::new("dock-worker");
let ownership = TemplateOwnership { template_id: tid, role_id: rid.clone() };
let saved = ownership.clone();
assert_eq!(saved, ownership, "TemplateOwnership must survive clone (save path)");
}
+613
View File
@@ -0,0 +1,613 @@
//! Integration tests for the triangle escalation system (#250).
//!
//! Covers the public API from a black-box perspective:
//! - D-087: seed-dependent tension rates produce different 30-min arc timings
//! - D-089: resolution does not cascade (only targeted triangle changes)
//! - D-026: escalation only runs on Active-tier entities
//! - D-031: escalation runs once per game-minute (every 10 ticks)
//!
//! These tests complement the lib unit tests in `src/content/template.rs`
//! with integration-level coverage using the public crate API.
use std::collections::BTreeMap;
use bevy_ecs::{schedule::Schedule, world::World};
use settled_reach_server::{
content::template::{
apply_resolve_triangle, tick_triangle_escalation, ResolveTriangleCommand,
ResolveTriangleQueue, TemplateId, TriangleCrisisEventQueue, TriangleDef, TriangleId,
TrianglePhase, TriangleState,
},
knowledge::{registry::EntityRegistry, types::StableId, StableEntityId},
npc::ToleranceThreshold,
simulation::{
tier::ActiveSim,
time::SimulationTime,
},
};
// ---------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------
/// Minimal world with all resources required by `tick_triangle_escalation`.
fn make_escalation_world() -> World {
let mut world = World::new();
world.init_resource::<SimulationTime>();
world.init_resource::<TriangleCrisisEventQueue>();
world.init_resource::<EntityRegistry>();
world
}
/// Spawn an NPC with a known StableId and ToleranceThreshold.
fn spawn_npc_with_threshold(world: &mut World, stable_id_val: u64, threshold: i16) -> StableId {
let sid = StableId(stable_id_val);
let entity = world
.spawn((ActiveSim, StableEntityId(sid), ToleranceThreshold { current_stress: 0, threshold }))
.id();
world.resource_mut::<EntityRegistry>().register_existing(entity, sid);
sid
}
/// Spawn a triangle entity with the given state (ActiveSim marker included).
fn spawn_triangle(
world: &mut World,
triangle_id: u64,
tension: u8,
tension_rate: u8,
phase: TrianglePhase,
role_assignments: BTreeMap<settled_reach_server::content::template::RoleId, StableId>,
) -> bevy_ecs::entity::Entity {
world
.spawn((
ActiveSim,
TriangleState {
triangle_id: TriangleId(triangle_id),
role_assignments,
tension,
phase,
tension_rate,
template_id: TemplateId(1),
},
))
.id()
}
/// Run the escalation schedule at a specific tick.
fn run_at_tick(world: &mut World, schedule: &mut Schedule, tick: u64) {
world.resource_mut::<SimulationTime>().tick = tick;
schedule.run(world);
}
// ---------------------------------------------------------------------------
// #250: Escalation happy path
// ---------------------------------------------------------------------------
/// D-031: escalation runs once per game-minute. 10 ticks = 1 game-minute.
/// Tension should only increment on multiples of 10.
#[test]
fn escalation_only_fires_on_game_minute_boundaries() {
let mut world = make_escalation_world();
let entity = spawn_triangle(
&mut world,
1,
0,
5,
TrianglePhase::Simmering,
BTreeMap::new(),
);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
// Ticks 1-9: not a game-minute, tension must not change.
for tick in 1..10 {
run_at_tick(&mut world, &mut schedule, tick);
}
assert_eq!(
world.get::<TriangleState>(entity).unwrap().tension,
0,
"tension must not change on sub-minute ticks"
);
// Tick 10: first game-minute, tension should increment.
run_at_tick(&mut world, &mut schedule, 10);
assert_eq!(
world.get::<TriangleState>(entity).unwrap().tension,
5,
"tension must increment at tick 10 (first game-minute)"
);
}
/// Simmering → Active transition at the expected game-minute.
///
/// Known setup:
/// - tension_rate = 5, starting tension = 0
/// - Lowest NPC threshold = 25
/// - After 5 game-minutes (50 ticks): tension = 25, not > 25 → Simmering
/// - After 6 game-minutes (60 ticks): tension = 30, 30 > 25 → Active
#[test]
fn simmering_transitions_to_active_at_expected_minute() {
let mut world = make_escalation_world();
let npc_a = spawn_npc_with_threshold(&mut world, 1, 40);
let npc_b = spawn_npc_with_threshold(&mut world, 2, 25); // lowest
let npc_c = spawn_npc_with_threshold(&mut world, 3, 60);
use settled_reach_server::content::template::RoleId;
let mut assignments = BTreeMap::new();
assignments.insert(RoleId::new("role-a"), npc_a);
assignments.insert(RoleId::new("role-b"), npc_b);
assignments.insert(RoleId::new("role-c"), npc_c);
let entity = spawn_triangle(&mut world, 42, 0, 5, TrianglePhase::Simmering, assignments);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
// Run through 50 ticks (5 game-minutes): should remain Simmering.
for tick in 1..=50 {
run_at_tick(&mut world, &mut schedule, tick);
}
let state = world.get::<TriangleState>(entity).unwrap();
assert_eq!(
state.phase,
TrianglePhase::Simmering,
"after 5 game-minutes (tension=25), must still be Simmering (not > 25)"
);
assert_eq!(state.tension, 25);
// Run through tick 60 (6th game-minute): tension becomes 30, > 25 → Active.
for tick in 51..=60 {
run_at_tick(&mut world, &mut schedule, tick);
}
let state = world.get::<TriangleState>(entity).unwrap();
assert_eq!(
state.phase,
TrianglePhase::Active,
"at tick 60 (tension=30 > threshold=25), must transition to Active"
);
assert_eq!(state.tension, 30);
}
/// D-087: different seeds produce different escalation timings.
/// Verify that two triangles with different tension rates escalate at different times.
#[test]
fn d087_seed_dependent_escalation_timing() {
// Triangle A: slower escalation (rate 2)
// Triangle B: faster escalation (rate 8)
// Both share same NPC threshold (30).
// A triggers at: ceil(30 / 2) + 1 = 16th game-minute (tension hits 32 at minute 16)
// B triggers at: ceil(30 / 8) + 1 = 5th game-minute (tension hits 32 at minute 4)
let mut world = make_escalation_world();
let npc = spawn_npc_with_threshold(&mut world, 1, 30);
use settled_reach_server::content::template::RoleId;
let mut assignments = BTreeMap::new();
assignments.insert(RoleId::new("r"), npc);
// Spawn as separate triangles.
let slow = spawn_triangle(&mut world, 10, 0, 2, TrianglePhase::Simmering, assignments.clone());
let fast = spawn_triangle(&mut world, 20, 0, 8, TrianglePhase::Simmering, assignments);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
// Run 40 game-minutes (400 ticks).
for tick in 1..=400 {
run_at_tick(&mut world, &mut schedule, tick);
}
// Both should be Active by 400 ticks.
assert_eq!(world.get::<TriangleState>(slow).unwrap().phase, TrianglePhase::Active);
assert_eq!(world.get::<TriangleState>(fast).unwrap().phase, TrianglePhase::Active);
// Fast triangle should have activated earlier (higher tension accumulated faster).
let fast_tension = world.get::<TriangleState>(fast).unwrap().tension;
let slow_tension = world.get::<TriangleState>(slow).unwrap().tension;
assert!(
fast_tension > slow_tension,
"fast triangle (rate=8) should have higher tension than slow (rate=2) after equal time"
);
}
/// The trigger NPC in the crisis event is the one with the lowest threshold.
#[test]
fn crisis_event_trigger_npc_is_lowest_threshold() {
let mut world = make_escalation_world();
let npc_high = spawn_npc_with_threshold(&mut world, 1, 50); // high tolerance
let npc_low = spawn_npc_with_threshold(&mut world, 2, 10); // low tolerance — trigger
use settled_reach_server::content::template::RoleId;
let mut assignments = BTreeMap::new();
assignments.insert(RoleId::new("r-high"), npc_high);
assignments.insert(RoleId::new("r-low"), npc_low);
// tension_rate = 11 so after 1 game-minute tension = 11 > 10.
spawn_triangle(&mut world, 99, 0, 11, TrianglePhase::Simmering, assignments);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
let queue = world.resource::<TriangleCrisisEventQueue>();
assert_eq!(queue.events.len(), 1, "exactly one crisis event");
assert_eq!(
queue.events[0].trigger_npc, npc_low,
"trigger NPC must be the one with the lowest threshold"
);
assert_eq!(queue.events[0].tick, 10, "crisis tick must match the game-minute");
}
/// No crisis event when tension hasn't exceeded the threshold.
#[test]
fn no_crisis_event_below_threshold() {
let mut world = make_escalation_world();
let npc = spawn_npc_with_threshold(&mut world, 1, 100); // high threshold
use settled_reach_server::content::template::RoleId;
let mut assignments = BTreeMap::new();
assignments.insert(RoleId::new("r"), npc);
spawn_triangle(&mut world, 1, 0, 5, TrianglePhase::Simmering, assignments);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
let queue = world.resource::<TriangleCrisisEventQueue>();
assert!(queue.is_empty(), "no crisis event when tension (5) < threshold (100)");
}
// ---------------------------------------------------------------------------
// #250: Active phase behavior
// ---------------------------------------------------------------------------
/// Active triangle continues incrementing tension (narrative tracking).
/// No additional crisis event emitted.
#[test]
fn active_triangle_continues_incrementing_no_new_event() {
let mut world = make_escalation_world();
spawn_triangle(&mut world, 1, 50, 3, TrianglePhase::Active, BTreeMap::new());
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
run_at_tick(&mut world, &mut schedule, 20);
let queue = world.resource::<TriangleCrisisEventQueue>();
assert!(queue.is_empty(), "no crisis event for already-Active triangle");
}
/// Active triangle tension saturates at u8::MAX (255).
#[test]
fn active_triangle_tension_saturates_at_u8_max() {
let mut world = make_escalation_world();
spawn_triangle(&mut world, 1, 252, 10, TrianglePhase::Active, BTreeMap::new());
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
// First call: 252 + 10 = 262, saturates to 255
let entity = world.query::<bevy_ecs::entity::Entity>().iter(&world).next().unwrap();
// Can't query TriangleState after mutable borrow; check via resource
// (we verify by spawning directly and checking post-run)
let _ = entity; // entity used to ensure spawn worked
// Re-run test cleanly
let mut world2 = make_escalation_world();
let e2 = spawn_triangle(&mut world2, 2, 254, 50, TrianglePhase::Active, BTreeMap::new());
let mut sched2 = Schedule::default();
sched2.add_systems(tick_triangle_escalation);
run_at_tick(&mut world2, &mut sched2, 10);
let state = world2.get::<TriangleState>(e2).unwrap();
assert_eq!(state.tension, 255, "tension saturates at u8::MAX");
}
// ---------------------------------------------------------------------------
// #250: D-026 tier boundary
// ---------------------------------------------------------------------------
/// Triangles without ActiveSim marker are NOT escalated (D-026 tier boundary).
#[test]
fn d026_non_active_tier_triangle_not_escalated() {
let mut world = make_escalation_world();
world.resource_mut::<SimulationTime>().tick = 10;
// Spawn WITHOUT ActiveSim.
let entity = world
.spawn(TriangleState {
triangle_id: TriangleId(1),
role_assignments: BTreeMap::new(),
tension: 10,
phase: TrianglePhase::Simmering,
tension_rate: 5,
template_id: TemplateId(1),
})
.id();
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
schedule.run(&mut world);
assert_eq!(
world.get::<TriangleState>(entity).unwrap().tension,
10,
"D-026: triangle without ActiveSim must not be escalated"
);
}
/// Dormant triangles are skipped even when in Active tier.
#[test]
fn dormant_triangle_not_escalated() {
let mut world = make_escalation_world();
let entity = spawn_triangle(&mut world, 1, 0, 10, TrianglePhase::Dormant, BTreeMap::new());
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
assert_eq!(
world.get::<TriangleState>(entity).unwrap().tension,
0,
"Dormant triangle must not be escalated"
);
}
/// Resolved triangles are skipped (D-089: resolution is permanent).
#[test]
fn resolved_triangle_not_escalated() {
let mut world = make_escalation_world();
let entity = spawn_triangle(&mut world, 1, 50, 5, TrianglePhase::Resolved, BTreeMap::new());
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
assert_eq!(
world.get::<TriangleState>(entity).unwrap().tension,
50,
"Resolved triangle must not be escalated (D-089)"
);
}
// ---------------------------------------------------------------------------
// #250: Resolution (D-089)
// ---------------------------------------------------------------------------
/// ResolveTriangleCommand sets the target triangle to Resolved.
#[test]
fn resolve_command_sets_phase_to_resolved() {
let mut world = World::new();
world.init_resource::<ResolveTriangleQueue>();
let entity = world
.spawn(TriangleState {
triangle_id: TriangleId(100),
role_assignments: BTreeMap::new(),
tension: 50,
phase: TrianglePhase::Active,
tension_rate: 3,
template_id: TemplateId(1),
})
.id();
world.resource_mut::<ResolveTriangleQueue>().push(ResolveTriangleCommand(TriangleId(100)));
let mut schedule = Schedule::default();
schedule.add_systems(apply_resolve_triangle);
schedule.run(&mut world);
let state = world.get::<TriangleState>(entity).unwrap();
assert_eq!(state.phase, TrianglePhase::Resolved, "resolve command must set phase to Resolved");
assert_eq!(state.tension, 50, "tension must not change on resolve");
}
/// D-089: Resolution does NOT cascade to other triangles.
#[test]
fn d089_resolve_does_not_cascade() {
let mut world = World::new();
world.init_resource::<ResolveTriangleQueue>();
let target = world
.spawn(TriangleState {
triangle_id: TriangleId(100),
role_assignments: BTreeMap::new(),
tension: 50,
phase: TrianglePhase::Active,
tension_rate: 3,
template_id: TemplateId(1),
})
.id();
let bystander_a = world
.spawn(TriangleState {
triangle_id: TriangleId(200),
role_assignments: BTreeMap::new(),
tension: 20,
phase: TrianglePhase::Simmering,
tension_rate: 2,
template_id: TemplateId(1),
})
.id();
let bystander_b = world
.spawn(TriangleState {
triangle_id: TriangleId(300),
role_assignments: BTreeMap::new(),
tension: 80,
phase: TrianglePhase::Active,
tension_rate: 4,
template_id: TemplateId(1),
})
.id();
// Resolve only triangle 100.
world.resource_mut::<ResolveTriangleQueue>().push(ResolveTriangleCommand(TriangleId(100)));
let mut schedule = Schedule::default();
schedule.add_systems(apply_resolve_triangle);
schedule.run(&mut world);
assert_eq!(
world.get::<TriangleState>(target).unwrap().phase,
TrianglePhase::Resolved
);
assert_eq!(
world.get::<TriangleState>(bystander_a).unwrap().phase,
TrianglePhase::Simmering,
"D-089: bystander_a must remain Simmering"
);
assert_eq!(
world.get::<TriangleState>(bystander_b).unwrap().phase,
TrianglePhase::Active,
"D-089: bystander_b must remain Active"
);
}
/// Resolving the same triangle twice is idempotent.
#[test]
fn resolve_twice_is_idempotent() {
let mut world = World::new();
world.init_resource::<ResolveTriangleQueue>();
let entity = world
.spawn(TriangleState {
triangle_id: TriangleId(100),
role_assignments: BTreeMap::new(),
tension: 30,
phase: TrianglePhase::Active,
tension_rate: 1,
template_id: TemplateId(1),
})
.id();
let mut schedule = Schedule::default();
schedule.add_systems(apply_resolve_triangle);
world.resource_mut::<ResolveTriangleQueue>().push(ResolveTriangleCommand(TriangleId(100)));
schedule.run(&mut world);
world.resource_mut::<ResolveTriangleQueue>().push(ResolveTriangleCommand(TriangleId(100)));
schedule.run(&mut world);
assert_eq!(
world.get::<TriangleState>(entity).unwrap().phase,
TrianglePhase::Resolved,
"double-resolve must remain Resolved"
);
}
// ---------------------------------------------------------------------------
// #250: Crisis event queue behavior
// ---------------------------------------------------------------------------
/// Crisis events accumulate in the queue until drained.
#[test]
fn crisis_events_accumulate_until_drained() {
let mut world = make_escalation_world();
let npc = spawn_npc_with_threshold(&mut world, 1, 5);
use settled_reach_server::content::template::RoleId;
let mut assignments = BTreeMap::new();
assignments.insert(RoleId::new("r"), npc);
// Two triangles that will both escalate.
spawn_triangle(&mut world, 10, 0, 6, TrianglePhase::Simmering, assignments.clone());
spawn_triangle(&mut world, 20, 0, 6, TrianglePhase::Simmering, assignments);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
let queue = world.resource::<TriangleCrisisEventQueue>();
assert_eq!(
queue.events.len(),
2,
"both triangles should emit crisis events in the same game-minute"
);
}
/// `TriangleCrisisEventQueue::drain` clears the queue.
#[test]
fn crisis_queue_drain_clears_events() {
let mut world = make_escalation_world();
let npc = spawn_npc_with_threshold(&mut world, 1, 5);
use settled_reach_server::content::template::RoleId;
let mut assignments = BTreeMap::new();
assignments.insert(RoleId::new("r"), npc);
spawn_triangle(&mut world, 1, 0, 10, TrianglePhase::Simmering, assignments);
let mut schedule = Schedule::default();
schedule.add_systems(tick_triangle_escalation);
run_at_tick(&mut world, &mut schedule, 10);
// Drain the queue.
let drained = world.resource_mut::<TriangleCrisisEventQueue>().drain();
assert_eq!(drained.len(), 1, "drain should return the 1 event");
assert!(
world.resource::<TriangleCrisisEventQueue>().is_empty(),
"queue must be empty after drain"
);
}
// ---------------------------------------------------------------------------
// #250: YAML triangle def → escalation pipeline
// ---------------------------------------------------------------------------
/// End-to-end: TriangleDef from YAML can describe all 5 v0.1 triangles
/// (D-087) and those defs produce escalatable TriangleState instances.
#[test]
fn d087_all_v01_conflict_types_produce_escalatable_states() {
use settled_reach_server::content::template::{ConflictType, NpcAxis, RoleId};
let defs = [
("kael-davan", "smuggler", "ring-contact", ConflictType::ResourceCompetition),
("sera-venn", "detective", "commission-inspector", ConflictType::SecretExposure),
("naia", "kael-davan", "hael", ConflictType::LatentTension),
("drin", "ring-system", "dock-supervisor", ConflictType::ResourceCompetition),
("worried-partner", "ring-member", "neighbor", ConflictType::LatentTension),
];
for (r0, r1, r2, conflict) in &defs {
let roles = [RoleId::new(r0), RoleId::new(r1), RoleId::new(r2)];
let tid = settled_reach_server::content::template::TriangleId::from_seed_and_roles(42, &roles);
let def = TriangleDef {
triangle_id: tid,
roles: roles.clone(),
conflict_type: *conflict,
interest_axes: [NpcAxis::Want, NpcAxis::Secret, NpcAxis::Relationships],
relationship_constraints: vec![],
};
assert!(def.validate().is_ok(), "D-087 triangle must be valid: {:?}", def.validate());
// Can construct a TriangleState from the def.
let mut assignments = BTreeMap::new();
for role in &roles {
assignments.insert(role.clone(), StableId(0));
}
let state = TriangleState {
triangle_id: tid,
role_assignments: assignments,
tension: 0,
phase: TrianglePhase::Simmering,
tension_rate: 3,
template_id: TemplateId(1),
};
assert_eq!(
state.phase,
TrianglePhase::Simmering,
"{:?} triangle must start Simmering",
conflict
);
}
}