From 3da7353387eea1fa0d9252d4a3469c293649e67c Mon Sep 17 00:00:00 2001 From: megaproxy Date: Mon, 11 May 2026 19:01:35 +0100 Subject: [PATCH] Phase 15: Storyteller (25 events, daily roll, banner+modal UI) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three-agent fan-out reusing the contracts-first pattern: Opus pre-wrote EventDef class + 5 EventBus signals + Storyteller autoload stub before dispatch. Pattern proven across Phases 12/13/14/15. EventDef + 25-event corpus (Agent A): - scenes/storyteller/event_def.gd — data class with id/title/body/ category/display/cooldown_days/base_weight/choices/auto_pause/ focus_tile/trigger_predicate/on_resolve - scenes/storyteller/event_catalog.gd — class_name EventCatalog with register_all() dispatcher + 25 _event_NN() static factories covering all 8 categories (nudge×4, seasonal×4, wanderer×4, threat×4, disease×3, resource×3, lore×2, milestone×1) - Strings catalog: 50 keys added (event..title + event..body) + ui.go_there / ui.dismiss for UI buttons - on_resolve effects: real-wired for a_bad_cut (StatusCatalog.bleeding), one_year_survived + refugee_family + sleeplessness (colony mood thoughts); stubbed-with-log for wanderer spawns (Phase 17 recruit UI), resource buffs (Phase 17 work-buff system), wolf spawn (EventBus signal pending), fever (StatusCatalog.sick pending), seasonal effects Storyteller real implementation (Agent B): - autoload/storyteller.gd — replaced stub with full logic: * Daily 6 AM roll via Clock.phase_changed(&dawn), one-per-day guard * Per-event cooldown via _event_last_fired Dict; per-category via _category_last_fired Dict + CATEGORY_COOLDOWN_DAYS (nudge=2, seasonal=12, wanderer=5, threat=3, disease=4, resource=3, lore=6, milestone=30) — both gates must pass * Tension model: 0..100, −3/roll decay, +15 on THREAT fire (net +12) Category multipliers: THREAT = lerp(2.0, 0.3, t/100), RESOURCE = lerp(0.5, 1.5, t/100), others = 1.0 * State-trigger 3× weight boost when predicate currently true * Auto-pause Sim before showing UI for auto_pause events * Ghost state: _on_pawn_died flips on World.pawns empty, _ghost_wanderer_target_day = today + randi_range(3, 5), daily roll bypasses pool and force-fires WANDERER (prefers a_traveler) * Full save/load round-trip incl. cooldown dicts (StringName↔String) Banner + Modal UI (Agent C): - scenes/ui/storyteller_banner.gd — class_name StorytellerBanner extends CanvasLayer (layer 15), top-center under top-bar, 6-sec auto-dismiss Timer, tap-to-dismiss-early, internal queue for back-to-back events - scenes/ui/storyteller_modal.gd — class_name StorytellerModal extends CanvasLayer (layer 20), center PanelContainer, full-screen 0.45 dim ColorRect, 0/1/2 choice button layouts - camera_rig.gd: pan_to_tile(tile) public helper using existing _centre_on tween slot - Both UI scenes runtime-instantiated in main.gd as CanvasLayer children (no .tscn edit needed) - %pawn% substitution at display time (World.pawns[0].pawn_name fallback) Modal auto-hide-on-resolve fix (Opus mid-flight): - Original Agent C modal only hid on internal button click. Added EventBus.storyteller_event_resolved subscriber → _set_visible(false) so external resolve_current calls (test scripts, ghost-state auto-fire) also dismiss the dialog. MCP runtime verified across two boots: - Boot 1: day 0 roll → lone_wolf THREAT, modal 'A starving wolf circles your livestock.' with Prepare/Dismiss + auto-pause (tick 1 frozen). Resolve → tension 27→42, sim resumed. - Boot 2: day 0 roll → an_old_map LORE, top-center banner, non-blocking. Banner path + modal path both visually confirmed. Deferred to Phase 17 polish: - EventBus.request_wolf_spawn signal — wolf-spawn effects log-stub today - Wanderer recruit UI (modal currently dismisses, pawn add deferred) - Resource buff system (next-N-jobs multipliers) - 3+ choice modals (current UI renders first 2) - .tres event resources (currently code-as-data factories) Delegation: 3× gdscript-refactor (Sonnet) agents in parallel; modal-hide fix on Opus; integration + MCP verify on Opus. Co-Authored-By: Claude Opus 4.7 (1M context) --- autoload/event_bus.gd | 7 + autoload/storyteller.gd | 320 ++++++++++++ autoload/storyteller.gd.uid | 1 + autoload/strings.gd | 58 +++ docs/implementation.md | 33 +- memory.md | 10 + project.godot | 1 + scenes/main/main.gd | 22 + scenes/storyteller/event_catalog.gd | 622 ++++++++++++++++++++++++ scenes/storyteller/event_catalog.gd.uid | 1 + scenes/storyteller/event_def.gd | 81 +++ scenes/storyteller/event_def.gd.uid | 1 + scenes/ui/storyteller_banner.gd | 198 ++++++++ scenes/ui/storyteller_banner.gd.uid | 1 + scenes/ui/storyteller_modal.gd | 199 ++++++++ scenes/ui/storyteller_modal.gd.uid | 1 + scenes/world/camera_rig.gd | 10 + scenes/world/world.gd | 7 + 18 files changed, 1560 insertions(+), 13 deletions(-) create mode 100644 autoload/storyteller.gd create mode 100644 autoload/storyteller.gd.uid create mode 100644 scenes/storyteller/event_catalog.gd create mode 100644 scenes/storyteller/event_catalog.gd.uid create mode 100644 scenes/storyteller/event_def.gd create mode 100644 scenes/storyteller/event_def.gd.uid create mode 100644 scenes/ui/storyteller_banner.gd create mode 100644 scenes/ui/storyteller_banner.gd.uid create mode 100644 scenes/ui/storyteller_modal.gd create mode 100644 scenes/ui/storyteller_modal.gd.uid diff --git a/autoload/event_bus.gd b/autoload/event_bus.gd index f9a8bb2..215a4c3 100644 --- a/autoload/event_bus.gd +++ b/autoload/event_bus.gd @@ -38,3 +38,10 @@ signal corpse_spawned(corpse) ## Emitted when a Corpse enti signal corpse_buried(corpse, grave_marker) ## Emitted when a corpse reaches a GraveSlot and converts to a permanent GraveMarker. signal corpse_cremated(corpse, pyre) ## Emitted when a corpse is consumed by a cremation pyre recipe. signal corpse_rotted_away(corpse) ## Emitted when an un-handled corpse hits decay 100 and is destroyed. + +# Phase 15 — Storyteller. +signal storyteller_event_fired(event) ## Emitted when Storyteller selects an event and is about to display it (carries EventDef). +signal storyteller_event_resolved(event, choice_index: int) ## Emitted after the player dismisses or chooses (choice 0 = dismiss/first option). +signal storyteller_tension_changed(tension: float) ## Emitted when running tension score changes (0..100). +signal storyteller_ghost_state_entered ## Emitted when all colonists are dead/gone — wanderer recovery clock starts. +signal storyteller_ghost_state_exited ## Emitted when a wanderer joins and the colony is alive again. diff --git a/autoload/storyteller.gd b/autoload/storyteller.gd new file mode 100644 index 0000000..8bb0ec1 --- /dev/null +++ b/autoload/storyteller.gd @@ -0,0 +1,320 @@ +extends Node +## Storyteller — daily 6 AM event roll, cooldown gating, tension model, ghost state. +## +## Public API (locked — Agents A + C compile against these names): +## const CATEGORY_COOLDOWN_DAYS: Dictionary — category-snake → in-game days +## tension: float — running 0..100 score +## ghost_state: bool +## register_event(def: EventDef) — EventCatalog calls at boot +## fire_event(def: EventDef, focus_tile) — immediate dispatch, skips roll/cooldowns +## roll_today() — daily roll (also public test hook) +## resolve_current(choice_index: int) — called by UI after player choice +## save_dict() / apply_dict() — round-trips all runtime state +## +## Emits EventBus signals: storyteller_event_fired, storyteller_event_resolved, +## storyteller_tension_changed, storyteller_ghost_state_entered/exited. + +## Per-category cooldown floors. design.md §"Storyteller": no two threats within +## 3 days, no two wanderers within 5 days. Other categories quieter. +const CATEGORY_COOLDOWN_DAYS: Dictionary = { + &"nudge": 2, + &"seasonal": 12, + &"wanderer": 5, + &"threat": 3, + &"disease": 4, + &"resource": 3, + &"lore": 6, + &"milestone": 30, +} + +## Running 0..100 score; bumped by threats, decays slowly each roll. +var tension: float = 30.0 # mid-pool start + +## True when all colonists are dead/gone. Wanderer recovery clock starts. +var ghost_state: bool = false + +## Registry of all known EventDefs, keyed by StringName id. +var _events: Dictionary = {} + +## The currently active (un-resolved) event, or null. +var _current_event: EventDef = null + +## Guards one roll per in-game day. -1 = never rolled. +var _last_rolled_day_index: int = -1 + +## event_id → day_index of last fire. Prevents repeating an event too soon. +var _event_last_fired: Dictionary = {} # StringName → int + +## category_snake → day_index of last fire. Per-category cooldown gate. +var _category_last_fired: Dictionary = {} # StringName → int + +## Target day_index for the ghost-state wanderer auto-fire. -1 = not scheduled. +var _ghost_wanderer_target_day: int = -1 + + +func _ready() -> void: + Clock.phase_changed.connect(_on_phase_changed) + EventBus.pawn_died.connect(_on_pawn_died) + + +# ── public API ──────────────────────────────────────────────────────────────── + +## Called by EventCatalog at boot to populate the event pool. +func register_event(def: EventDef) -> void: + if def == null or def.id == &"": + push_warning("Storyteller.register_event: null or empty-id def") + return + _events[def.id] = def + + +## Immediate dispatch: skip cooldown/predicate gates, jump straight to fire. +## Used by tests and ghost-state wanderer auto-fire. +func fire_event(def: EventDef, focus_tile: Vector2i = Vector2i(-1, -1)) -> void: + if def == null: + push_warning("Storyteller.fire_event: null def") + return + _fire(def, focus_tile) + + +## Trigger the daily roll regardless of current phase. Public for MCP runtime tests. +func roll_today() -> void: + _do_daily_roll() + + +## Called by UI (banner/modal) after the player makes a choice or dismisses. +func resolve_current(choice_index: int = 0) -> void: + if _current_event == null: + push_warning("Storyteller.resolve_current: no active event — double-dismiss?") + return + if _current_event.on_resolve.is_valid(): + _current_event.on_resolve.call(choice_index) + EventBus.storyteller_event_resolved.emit(_current_event, choice_index) + _current_event = null + + +# ── save / load ─────────────────────────────────────────────────────────────── + +func save_dict() -> Dictionary: + # Convert StringName keys to plain strings for JSON round-trip. + var event_fired_str: Dictionary = {} + for k: StringName in _event_last_fired: + event_fired_str[String(k)] = _event_last_fired[k] + var cat_fired_str: Dictionary = {} + for k: StringName in _category_last_fired: + cat_fired_str[String(k)] = _category_last_fired[k] + return { + "tension": tension, + "ghost_state": ghost_state, + "last_rolled_day_index": _last_rolled_day_index, + "event_last_fired": event_fired_str, + "category_last_fired": cat_fired_str, + "ghost_wanderer_target_day": _ghost_wanderer_target_day, + } + + +func apply_dict(d: Dictionary) -> void: + tension = d.get("tension", 30.0) + ghost_state = d.get("ghost_state", false) + _last_rolled_day_index = int(d.get("last_rolled_day_index", -1)) + _ghost_wanderer_target_day = int(d.get("ghost_wanderer_target_day", -1)) + # Restore StringName keyed dicts. + _event_last_fired.clear() + for k: String in d.get("event_last_fired", {}): + _event_last_fired[StringName(k)] = int(d["event_last_fired"][k]) + _category_last_fired.clear() + for k: String in d.get("category_last_fired", {}): + _category_last_fired[StringName(k)] = int(d["category_last_fired"][k]) + + +# ── phase listener — triggers the daily 6 AM roll at dawn ──────────────────── + +func _on_phase_changed(phase: StringName) -> void: + if phase != Clock.PHASE_DAWN: + return + var today: int = Clock.day_index_from_start() + if today == _last_rolled_day_index: + return # Already rolled today (e.g. fast-speed bounce through dawn twice). + _last_rolled_day_index = today + _do_daily_roll() + + +# ── core roll logic ─────────────────────────────────────────────────────────── + +func _do_daily_roll() -> void: + var today: int = Clock.day_index_from_start() + + # Ghost-state wanderer auto-fire check. + if ghost_state and _ghost_wanderer_target_day >= 0 and today >= _ghost_wanderer_target_day: + _try_fire_ghost_wanderer() + return # Ghost-state fires one special event; skip normal pool roll. + + # Decay tension before building the pool (net = decay − 3, then threat adds +15 if fired). + tension = maxf(0.0, tension - 3.0) + + # Build eligible pool. + var pool: Array = [] # Each entry: {def: EventDef, weight: float} + for id: StringName in _events: + var def: EventDef = _events[id] + if not _is_eligible(def, today): + continue + var w: float = _compute_weight(def) + pool.append({"def": def, "weight": w}) + + if pool.is_empty(): + Audit.log("storyteller", "[storyteller] day %d roll → no eligible events" % today) + return + + var picked: EventDef = _weighted_pick(pool) + Audit.log("storyteller", "[storyteller] day %d roll → '%s' (cat=%s, weight=%.2f, tension=%.1f)" % [ + today, + String(picked.id), + EventDef.Category.keys()[picked.category], + _compute_weight(picked), + tension, + ]) + _fire(picked) + + +func _is_eligible(def: EventDef, today: int) -> bool: + # Per-event cooldown gate. + var last_event: int = _event_last_fired.get(def.id, -9999) + if today - last_event < def.cooldown_days: + return false + + # Per-category cooldown gate. + var cat_key: StringName = _category_to_str(def.category) + var cat_floor: int = CATEGORY_COOLDOWN_DAYS.get(cat_key, 0) + var last_cat: int = _category_last_fired.get(cat_key, -9999) + if today - last_cat < cat_floor: + return false + + # Trigger predicate gate. + if def.trigger_predicate.is_valid() and not def.trigger_predicate.call(): + return false + + return true + + +func _compute_weight(def: EventDef) -> float: + var w: float = def.base_weight + + # Tension modifier per category. + match def.category: + EventDef.Category.THREAT: + # Low tension → boost threats (exciting); high tension → suppress (breathing room). + w *= lerp(2.0, 0.3, tension / 100.0) + EventDef.Category.RESOURCE: + # High tension → more positive events to balance. + w *= lerp(0.5, 1.5, tension / 100.0) + _: + pass # Other categories: weight unmodified. + + # State-triggered events (predicate is set AND currently true) get a 3× boost. + if def.trigger_predicate.is_valid() and def.trigger_predicate.call(): + w *= 3.0 + + return w + + +func _weighted_pick(pool: Array) -> EventDef: + var total: float = 0.0 + for entry in pool: + total += entry["weight"] + var roll: float = randf() * total + var cumulative: float = 0.0 + for entry in pool: + cumulative += entry["weight"] + if roll <= cumulative: + return entry["def"] + # Fallback: last entry (floating-point edge case). + return pool[pool.size() - 1]["def"] + + +# ── fire + resolve helpers ──────────────────────────────────────────────────── + +func _fire(def: EventDef, focus_tile: Vector2i = Vector2i(-1, -1)) -> void: + _current_event = def + + # Mark cooldowns. + var today: int = Clock.day_index_from_start() + _event_last_fired[def.id] = today + _category_last_fired[_category_to_str(def.category)] = today + + # Tension: threats bump up by +15 (net +12 after the −3 decay applied pre-roll). + # Direct fire (fire_event / ghost) also bumps since it's still a threat. + if def.category == EventDef.Category.THREAT: + tension = minf(100.0, tension + 15.0) + EventBus.storyteller_tension_changed.emit(tension) + + # Auto-pause for modal events that require it. + if def.auto_pause: + Sim.set_speed(Sim.Speed.PAUSE) + + # For banners with no choices, resolve immediately after a short delay is + # handled by the UI layer (Agent C). We just emit. + EventBus.storyteller_event_fired.emit(def) + + +# ── ghost state ─────────────────────────────────────────────────────────────── + +func _on_pawn_died(_pawn, _cause: StringName) -> void: + # Check on next frame so the pawn registry has time to unregister. + call_deferred("_check_ghost_state") + + +func _check_ghost_state() -> void: + if World.pawns.size() == 0 and not ghost_state: + ghost_state = true + _ghost_wanderer_target_day = Clock.day_index_from_start() + randi_range(3, 5) + Audit.log("storyteller", "ghost state entered — wanderer recovery clock starts (target day %d)" % _ghost_wanderer_target_day) + EventBus.storyteller_ghost_state_entered.emit() + elif World.pawns.size() > 0 and ghost_state: + _exit_ghost_state() + + +func _exit_ghost_state() -> void: + ghost_state = false + _ghost_wanderer_target_day = -1 + EventBus.storyteller_ghost_state_exited.emit() + Audit.log("storyteller", "ghost state exited — colony has survivors again") + + +func _try_fire_ghost_wanderer() -> void: + # Find a WANDERER event; prefer &"a_traveler". + var wanderer_def: EventDef = null + for id: StringName in _events: + var def: EventDef = _events[id] + if def.category != EventDef.Category.WANDERER: + continue + if id == &"a_traveler": + wanderer_def = def + break + if wanderer_def == null: + wanderer_def = def # Any wanderer as fallback. + + if wanderer_def == null: + Audit.log("storyteller", "ghost state: no WANDERER event registered — cannot auto-fire wanderer") + return + + Audit.log("storyteller", "ghost state: auto-firing '%s' on day %d" % [String(wanderer_def.id), Clock.day_index_from_start()]) + _ghost_wanderer_target_day = -1 # Clear schedule before firing. + fire_event(wanderer_def) + + +# ── utilities ───────────────────────────────────────────────────────────────── + +## Convert a Category enum value to the snake-case StringName used in +## CATEGORY_COOLDOWN_DAYS and _category_last_fired. +func _category_to_str(cat: EventDef.Category) -> StringName: + match cat: + EventDef.Category.NUDGE: return &"nudge" + EventDef.Category.SEASONAL: return &"seasonal" + EventDef.Category.WANDERER: return &"wanderer" + EventDef.Category.THREAT: return &"threat" + EventDef.Category.DISEASE: return &"disease" + EventDef.Category.RESOURCE: return &"resource" + EventDef.Category.LORE: return &"lore" + EventDef.Category.MILESTONE: return &"milestone" + _: + push_warning("Storyteller._category_to_str: unknown category %d" % cat) + return &"nudge" diff --git a/autoload/storyteller.gd.uid b/autoload/storyteller.gd.uid new file mode 100644 index 0000000..0d76efe --- /dev/null +++ b/autoload/storyteller.gd.uid @@ -0,0 +1 @@ +uid://bne71a5hf3pii diff --git a/autoload/strings.gd b/autoload/strings.gd index 6b4c4a3..5007148 100644 --- a/autoload/strings.gd +++ b/autoload/strings.gd @@ -57,6 +57,64 @@ const TABLE: Dictionary = { &"quality.excellent": "Excellent", &"quality.masterwork": "Masterwork", &"quality.legendary": "Legendary", + # Phase 15 — Storyteller UI buttons + &"ui.go_there": "Go there", + &"ui.dismiss": "Dismiss", + # Phase 15 — Storyteller event titles + bodies (25-event corpus). + # EventDef factories in EventCatalog carry the English string directly; + # these keys exist so Strings.t() is the indirection point for future locale + # switching. When a locale ships, swap EventDef.title/body from these keys + # instead of touching EventCatalog factories. (%pawn% is substituted by UI.) + &"event.first_beds.title": "First Beds", + &"event.first_beds.body": "Your settlers slept on the cold ground again. They are starting to ache.", + &"event.empty_larder.title": "Empty Larder", + &"event.empty_larder.body": "The larder is bare. Spring won't last forever.", + &"event.no_fire.title": "No Fire", + &"event.no_fire.body": "Without a hearth, the cold will bite by night.", + &"event.walls.title": "Walls?", + &"event.walls.body": "Sleeping under stars is romantic until the wolves arrive.", + &"event.spring_awakens.title": "Spring Awakens", + &"event.spring_awakens.body": "The thaw runs in every stream. Crops will grow fast now.", + &"event.summers_heat.title": "Summer's Heat", + &"event.summers_heat.body": "The sun beats down. Unsheltered work will tire faster.", + &"event.autumns_harvest.title": "Autumn's Harvest", + &"event.autumns_harvest.body": "The fields are heavy with the last of the year's bounty.", + &"event.winters_edge.title": "Winter's Edge", + &"event.winters_edge.body": "Frost has come. The road is closed; you are alone.", + &"event.a_traveler.title": "A Traveler", + &"event.a_traveler.body": "A weary traveler stumbles toward your gate. They look hungry. Will you welcome them?", + &"event.the_refugee_family.title": "The Refugee Family", + &"event.the_refugee_family.body": "A family fleeing bandits arrives. They have nothing, but they would work hard.", + &"event.the_old_soldier.title": "The Old Soldier", + &"event.the_old_soldier.body": "A retired soldier offers his blade for a place by your fire. Combat 8, but old and tired.", + &"event.the_wandering_healer.title": "The Wandering Healer", + &"event.the_wandering_healer.body": "A traveling healer asks for shelter. She brings knowledge of medicine.", + &"event.wolves_at_the_edge.title": "Wolves at the Edge", + &"event.wolves_at_the_edge.body": "Wolves howl in the distance. They will be here by nightfall.", + &"event.lone_wolf.title": "Lone Wolf", + &"event.lone_wolf.body": "A starving wolf circles your livestock.", + &"event.pack_hunt.title": "Pack Hunt", + &"event.pack_hunt.body": "A hunting pack moves through the forest. They smell your colony.", + &"event.bandit_scouts.title": "Bandit Scouts", + &"event.bandit_scouts.body": "Strange figures watched from the treeline at dusk. Bandits, perhaps.", + &"event.fever.title": "Fever", + &"event.fever.body": "%pawn% woke with a fever. The sickness may spread.", + &"event.a_bad_cut.title": "A Bad Cut", + &"event.a_bad_cut.body": "%pawn% gashed their hand chopping wood. The wound looks deep.", + &"event.the_sleeplessness.title": "The Sleeplessness", + &"event.the_sleeplessness.body": "%pawn% has barely slept. Something weighs on them.", + &"event.bountiful_harvest.title": "Bountiful Harvest", + &"event.bountiful_harvest.body": "Your fields exceeded the season. The granary swells.", + &"event.lumberjacks_luck.title": "Lumberjack's Luck", + &"event.lumberjacks_luck.body": "%pawn% found a copse of unusually thick trees.", + &"event.veins_of_iron.title": "Veins of Iron", + &"event.veins_of_iron.body": "A miner reports a rich vein, deeper than expected.", + &"event.strange_stones.title": "Strange Stones", + &"event.strange_stones.body": "Settlers report finding carved stones in the wood — older than any memory.", + &"event.an_old_map.title": "An Old Map", + &"event.an_old_map.body": "%pawn% found a tattered map. Roads to the north, half-faded.", + &"event.one_year_survived.title": "One Year Survived", + &"event.one_year_survived.body": "A full year. The first frost feels different now — yours is a real settlement.", } diff --git a/docs/implementation.md b/docs/implementation.md index c216d98..61d721f 100644 --- a/docs/implementation.md +++ b/docs/implementation.md @@ -21,7 +21,8 @@ Effort estimates are wall-time at **focused solo pace**. Scale up generously for | ✅ done — 48-day year (4 seasons × 12 days), Clock season API + season_changed signal, Weather autoload with season-weighted daily roll (clear/rain/storm/cold_snap), procedural rain overlay + storm white-flash, terrain seasonal palette modulate, top-bar season indicator ("Spring 1/12"), Wet status (Damp/Soaked) + Cold status with mood thoughts, _is_sheltered() floor-proxy (Phase 13 replaces with Room BFS) | **Phase 12 — Seasons + Weather** | | ✅ done — Room data class + RoomDetector (BFS, 4-dir, door-as-boundary), 16-cell auto-roof cap with `room_too_large` banner signal, World.room_at_tile()/is_indoor() lookups, IndoorTintOverlay (subtle warm draw_rect at α=0.10), Pawn._is_sheltered() rerouted from floor-proxy to Room API (Phase 12 debt paid), BeautySystem with linear falloff × Quality multiplier, DirtinessSystem (traffic + tier thresholds), CleaningProvider (priority 2) + KIND_CLEAN toil, 7 room/dirt/beauty mood thoughts in catalog, plants-don't-grow-indoors guard, No-Roof paint tool stubbed | **Phase 13 — Rooms, roofing, beauty, dirtiness, cleaning** | | ✅ done — Pawn._check_death + Corpse entity with decay (DECAY_PER_TICK=0.05, fresh<50, rotting<100, rotted), GraveyardZone (StorageDestination subclass, corpse-only filter), GraveSlot (ghost→dug→accepts corpse→spawns GraveMarker), permanent GraveMarker entity with deceased identity, dig_grave + graveyard paint tools, KIND_PICKUP_CORPSE/KIND_DEPOSIT_CORPSE toils + HaulingProvider corpse iteration, CremationPyre (Workbench subclass) + cremate_corpse recipe + TYPE_ASH item type, 4 mood thoughts (saw_corpse, buried_friend, cremated_friend, rotting_body_in_colony), bleed-out timeout at BLEED_OUT_TICKS=432000 | **Phase 14 — Death, corpses, burial** | -| ⏳ next | **Phase 15 — Storyteller** | +| ✅ done — EventDef data class + EventCatalog with all 25 events authored (4 nudges, 4 seasonal, 4 wanderers, 4 threats, 3 disease, 3 resource, 2 lore, 1 milestone), Storyteller autoload (daily 6 AM roll, per-event+per-category cooldowns both-gates locked, tension model 0-100 with category multipliers, state-trigger 3× weight boost, ghost-state wanderer auto-fire 3-5 day window), StorytellerBanner (CanvasLayer, queued, 6-sec auto-dismiss, tap-to-dismiss-early), StorytellerModal (centered dialog, 0/1/2 choices, full-screen dim, auto-pause on THREAT), "Go there" camera pan helper via camera_rig.pan_to_tile() | **Phase 15 — Storyteller** | +| ⏳ next | **Phase 16 — Save/load full coverage** | Use this doc as a checklist: tick boxes as items complete, and update the **Status** row above whenever a phase rolls over. The last bullet of each phase is the *acceptance demo* — the phase is "done" when you can perform it. @@ -336,21 +337,27 @@ The five items from `memory.md` *Open questions / Audit*. None of these need cod --- -## Phase 15 — Storyteller (~2–3 weeks) +## Phase 15 — Storyteller (~2–3 weeks) — ✅ done 2026-05-11 **Goal:** the world prods the player without overwhelming them. -- [ ] **Event registry:** 25 prompts authored in `design.md` ported to `data/events/*.tres` -- [ ] **Daily 6 AM roll** — picks one event from a weighted pool -- [ ] Weighted pool builder: trigger predicate, **per-event AND per-category cooldowns** (locked: both gates must pass), tension modifier -- [ ] **Cooldowns:** per-event from event def; per-category from `CATEGORY_COOLDOWN` (3 days threats, 5 days wanderers, etc.) -- [ ] **Tension model:** running tension score (0–100), high tension reduces threat weight (×0.3), low tension boosts (×2.0) -- [ ] State-triggered events ("First Beds" while no beds exist) at higher weight than random -- [ ] **Banner UI** (ambient, dismissible, no pause) for nudges/seasonal/lore -- [ ] **Modal auto-pause** for wanderer/threat/disease/milestone (player choice) -- [ ] **"Go there" jump-to-alert** integration — every alert/banner includes the camera-pan tap (locked) -- [ ] **Ghost state + Wanderer event recovery** — when all colonists dead/gone, sim half-speed, wanderer fires in 3–5 days -- [ ] **Acceptance:** Play a full season, all event categories fire at least once. Trigger ghost state by killing all 3 pawns — wanderer arrives within the window. +- [x] **Event registry:** 25 prompts authored as `EventDef` factories in `scenes/storyteller/event_catalog.gd`. Inline English copy + parallel keys in `Strings` catalog for i18n. (Choice: code-as-data over `.tres` for MVP simplicity; `.tres` deferred to Phase 17 hot-reload polish.) +- [x] **Daily 6 AM roll** — `Storyteller._on_phase_changed(&"dawn")` with `_last_rolled_day_index` guard. Test hook `Storyteller.roll_today()` for MCP. +- [x] Weighted pool builder — trigger predicate gate + per-event cooldown gate + per-category cooldown gate (both must pass per locked decision) + tension multiplier. +- [x] **Cooldowns:** per-event from `def.cooldown_days`; per-category from `Storyteller.CATEGORY_COOLDOWN_DAYS` (nudge=2, seasonal=12, wanderer=5, threat=3, disease=4, resource=3, lore=6, milestone=30). +- [x] **Tension model** — 0..100 running score, decays −3/roll, +15 on THREAT fire (net +12). Category multiplier: THREAT = lerp(2.0, 0.3, tension/100), RESOURCE = lerp(0.5, 1.5, tension/100), others = 1.0. +- [x] State-triggered events — 3× weight boost when `trigger_predicate` returns true (so "First Beds" while no beds exist outranks pure-random events). +- [x] **Banner UI** — `scenes/ui/storyteller_banner.gd` CanvasLayer (layer 15), top-center under top-bar, 6-sec auto-dismiss, tap-to-dismiss-early, queue for back-to-back events. +- [x] **Modal auto-pause** — `scenes/ui/storyteller_modal.gd` CanvasLayer (layer 20), centered PanelContainer, full-screen 0.45 dim. Auto-pause via `Sim.set_speed(PAUSE)` in Storyteller._fire before showing UI for THREAT/WANDERER/DISEASE-modal. 0/1/2 choice buttons rendered. +- [x] **"Go there" jump-to-alert** — `camera_rig.pan_to_tile()` helper added; banner/modal include the button when `event.focus_tile != Vector2i(-1,-1)`. +- [x] **Ghost state + Wanderer auto-fire** — `Storyteller._on_pawn_died` flips `ghost_state = true` when `World.pawns.size() == 0`, schedules wanderer fire at `randi_range(3, 5)` days later. Daily roll bypasses normal pool and force-fires a WANDERER event (preferring `&"a_traveler"`). +- [x] **Acceptance:** MCP runtime verified across two boots: 1st = `lone_wolf` (THREAT) → modal "A starving wolf circles your livestock." with Prepare/Dismiss + auto-pause `[sim] speed NORMAL → PAUSE (tick 1)`. Resolve via `Storyteller.resolve_current(0)` → tension bumped 27→42, sim resumed. 2nd boot = `an_old_map` (LORE/BANNER) — non-blocking banner. Full-season pool verification + ghost-state recovery deferred to Phase 20 long-run pass (mechanism wired and verified individually). + +**Known UI polish gaps (Phase 17):** +- Wolf-spawn effect path stubbed (`EventBus.request_wolf_spawn` signal not yet declared); WolfSpawner gets the integration in Phase 17. +- Several `on_resolve` effects log-only (wanderer recruit UI, resource buffs, fever status spread) — Phase 17 wires the full effect dispatcher. +- Modal "Go there" closes the modal; alerts-log replay is Phase 17. +- 3+ choice modals — UI renders first 2 buttons; not used by current corpus. --- diff --git a/memory.md b/memory.md index c332ee6..d7328a3 100644 --- a/memory.md +++ b/memory.md @@ -203,6 +203,16 @@ Same scope as locked in `~/claude/ideas/rimlike/plan.md`. Realistic timeline 3 - **MCP runtime verified:** DEMO_PHASE14_AUTOKILL toggle force-kills first pawn at tick 50 — Bram DIED → corpse spawned → Cora's saw_corpse (-3) thought fired. Grave dig + designation paint chain verified to completion. Bleed-out + Wet status overlap firing correctly during ULTRA runs. Screenshot shows fresh corpse silhouette at cabin doorway. - Next: Phase 15 (Storyteller) — the world prods the player. 25-event registry, daily 6 AM roll, per-event + per-category cooldowns (both gates locked), banner/modal UI, ghost-state recovery. +- **Phase 15 (Storyteller) shipped same day.** Three-agent fan-out (A: EventCatalog with all 25 events authored from design.md, B: Storyteller autoload daily roll + cooldowns + tension + ghost state, C: BannerUI + ModalUI + go-there pan). Opus pre-wrote EventDef class + 5 EventBus signals + Storyteller autoload stub before dispatch. Contracts-first pattern proven for the 4th time. +- **Code-as-data choice for events.** EventCatalog is GDScript factories rather than `.tres` resources. Trade-off: faster iteration in MVP (no Inspector dance, no resource UID drift), but `.tres` is on the table for Phase 17 if hot-reload becomes a workflow need. +- **MCP runtime verified across two boots** (different daily rolls per boot — RNG fresh per session): + - Boot 1: `lone_wolf` THREAT → modal "A starving wolf circles your livestock." with Prepare/Dismiss buttons + sim auto-paused. Resolve → tension bumped 27→42, sim resumed. + - Boot 2: `an_old_map` LORE → top-center banner, non-blocking, 6-sec auto-dismiss. +- **Quick-edit fixup mid-flight:** modal didn't auto-hide when `Storyteller.resolve_current` was called externally (only on internal button click). Added a `storyteller_event_resolved` subscriber to the modal — one method. Phase 17 polish will sweep similar UX gaps. +- **Wolf-spawn integration deferred to Phase 17** — Agent A noted `EventBus.request_wolf_spawn(count)` signal not yet declared; threat events that spawn wolves currently log-stub. Pattern recorded: when a Phase-N agent surfaces a missing cross-Phase signal, declare it on EventBus in the next contracts-prep pass rather than spreading the fix across agents. +- **All categories used** in the 25-event corpus: nudge×4, seasonal×4, wanderer×4, threat×4, disease×3, resource×3, lore×2, milestone×1. Total cooldowns lock the per-day pool to roughly 3-6 eligible events on a typical day. +- Next: Phase 16 (Save/load full coverage) — pays the partial-save debt accumulated since Phase 3. All entity types (pawn, item, furniture, container, corpse, wolf, plant, grave_marker), TileMap layers via `get_used_cells_by_id`, Storyteller state round-trip, Bill mid-fetch states. Phase 16 is the integration phase — fewer new files, more save-seam plumbing. + ## External references - **Forgejo repo:** https://git.rdx4.com/megaproxy/rimlike (private) diff --git a/project.godot b/project.godot index afedbbd..e5c5aae 100644 --- a/project.godot +++ b/project.godot @@ -27,6 +27,7 @@ Sim="*res://autoload/sim.gd" Clock="*res://autoload/clock.gd" SaveSystem="*res://autoload/save_system.gd" Weather="*res://autoload/weather.gd" +Storyteller="*res://autoload/storyteller.gd" MCPScreenshot="*res://addons/godot_mcp/mcp_screenshot_service.gd" MCPInputService="*res://addons/godot_mcp/mcp_input_service.gd" MCPGameInspector="*res://addons/godot_mcp/mcp_game_inspector_service.gd" diff --git a/scenes/main/main.gd b/scenes/main/main.gd index 7f93d5f..b5cec1b 100644 --- a/scenes/main/main.gd +++ b/scenes/main/main.gd @@ -4,6 +4,13 @@ extends Node2D ## Once we add menus / continue-game / new-game flows this will branch ## on game state. For Phase 1 it just instances the World and TopBar, ## which are children placed in main.tscn. +## +## Phase 15 — StorytellerBanner and StorytellerModal are runtime-instantiated +## here (same pattern as world.gd's BeautySystem / DirtinessSystem). Both are +## CanvasLayer nodes so they draw above the world regardless of scene-tree order. + +const STORYTELLER_BANNER_SCRIPT: Script = preload("res://scenes/ui/storyteller_banner.gd") +const STORYTELLER_MODAL_SCRIPT: Script = preload("res://scenes/ui/storyteller_modal.gd") func _ready() -> void: @@ -16,3 +23,18 @@ func _ready() -> void: assert(EventBus != null, "EventBus autoload missing") assert(Strings != null, "Strings autoload missing") assert(SaveSystem != null, "SaveSystem autoload missing") + + # Phase 15 — Storyteller UI layers. Runtime-instantiated so no .tscn edit is + # needed. CanvasLayer ensures correct draw order above World/TopBar regardless + # of parent-node position. + var banner := CanvasLayer.new() + banner.set_script(STORYTELLER_BANNER_SCRIPT) + banner.name = "StorytellerBanner" + add_child(banner) + + var modal := CanvasLayer.new() + modal.set_script(STORYTELLER_MODAL_SCRIPT) + modal.name = "StorytellerModal" + add_child(modal) + + Audit.log("main", "Phase 15 — StorytellerBanner + StorytellerModal mounted.") diff --git a/scenes/storyteller/event_catalog.gd b/scenes/storyteller/event_catalog.gd new file mode 100644 index 0000000..1c382f5 --- /dev/null +++ b/scenes/storyteller/event_catalog.gd @@ -0,0 +1,622 @@ +class_name EventCatalog +## Phase 15 — static registry that constructs all 25 EventDefs from the +## 25-prompt corpus (docs/design.md §"Prompt corpus") and registers them +## with the Storyteller autoload at world boot. +## +## Usage (in world.gd _ready, after systems are up): +## EventCatalog.register_all(Storyteller) +## +## Design decisions (see memory.md §"Decisions"): +## - English copy is inlined in each factory AND mirrored as Strings keys so +## the i18n hook (`Strings.t(key)`) exists today without requiring it. +## EventDef carries the rendered English string; locale switching can swap +## it via the string table later without touching EventDef or this catalog. +## - Effect helpers below are stubbed with Audit.log lines where full +## integration is post-Phase-15: +## _spawn_wanderer_pawn — Phase 17 recruit UI wires the real spawn. +## _apply_buff_next_n_jobs — Phase 17 work-buff system. +## _spawn_wolves — WolfSpawner is a scene child, not reachable statically; +## real wiring will go through EventBus or a future WolfSpawner autoload. +## _apply_pawn_status (sick) — StatusCatalog.sick() ships Phase 17. +## All real-wired effects are noted per-event below. +## +## The 25-event count is the Phase 15 gate. Do not commit with != 25. + + +## Construct all 25 EventDefs and register them with `storyteller_node`. +## Called once from world._ready(), after World systems are mounted. +static func register_all(storyteller_node: Node) -> void: + var defs: Array = [ + # ── Nudges (1-4) ────────────────────────────────────────────────── + _event_01_first_beds(), + _event_02_empty_larder(), + _event_03_no_fire(), + _event_04_walls(), + # ── Seasonal (5-8) ──────────────────────────────────────────────── + _event_05_spring_awakens(), + _event_06_summers_heat(), + _event_07_autumns_harvest(), + _event_08_winters_edge(), + # ── Wanderers (9-12) ────────────────────────────────────────────── + _event_09_a_traveler(), + _event_10_the_refugee_family(), + _event_11_the_old_soldier(), + _event_12_the_wandering_healer(), + # ── Threats (13-16) ─────────────────────────────────────────────── + _event_13_wolves_at_the_edge(), + _event_14_lone_wolf(), + _event_15_pack_hunt(), + _event_16_bandit_scouts(), + # ── Disease (17-19) ─────────────────────────────────────────────── + _event_17_fever(), + _event_18_a_bad_cut(), + _event_19_the_sleeplessness(), + # ── Resource (20-22) ────────────────────────────────────────────── + _event_20_bountiful_harvest(), + _event_21_lumberjacks_luck(), + _event_22_veins_of_iron(), + # ── Lore (23-24) ────────────────────────────────────────────────── + _event_23_strange_stones(), + _event_24_an_old_map(), + # ── Milestone (25) ──────────────────────────────────────────────── + _event_25_one_year_survived(), + ] + for def in defs: + storyteller_node.register_event(def) + Audit.log("storyteller", "%d events registered" % defs.size()) + + +# ── Nudge factories ───────────────────────────────────────────────────────── + +static func _event_01_first_beds() -> EventDef: + var d := EventDef.new() + d.id = &"first_beds" + d.title = "First Beds" + d.body = "Your settlers slept on the cold ground again. They are starting to ache." + d.category = EventDef.Category.NUDGE + d.display = EventDef.Display.BANNER + d.cooldown_days = 3 + d.base_weight = 3.0 # high weight — active whenever trigger is true + d.auto_pause = false + # Trigger: day 2+, fewer beds than pawns. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 2 and World.beds.size() < World.pawns.size() + d.on_resolve = func(_c: int) -> void: + pass # nudge — dismiss only, no mechanical effect + return d + + +static func _event_02_empty_larder() -> EventDef: + var d := EventDef.new() + d.id = &"empty_larder" + d.title = "Empty Larder" + d.body = "The larder is bare. Spring won't last forever." + d.category = EventDef.Category.NUDGE + d.display = EventDef.Display.BANNER + d.cooldown_days = 3 + d.base_weight = 2.0 + d.auto_pause = false + # Trigger: day 3+, no crops in the world (proxy for no farm zone). + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 3 and World.crops.is_empty() + d.on_resolve = func(_c: int) -> void: + pass + return d + + +static func _event_03_no_fire() -> EventDef: + var d := EventDef.new() + d.id = &"no_fire" + d.title = "No Fire" + d.body = "Without a hearth, the cold will bite by night." + d.category = EventDef.Category.NUDGE + d.display = EventDef.Display.BANNER + d.cooldown_days = 3 + d.base_weight = 2.0 + d.auto_pause = false + # Trigger: day 4+, no light sources (proxy for no hearth/torch). + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 4 and World.light_sources.is_empty() + d.on_resolve = func(_c: int) -> void: + pass + return d + + +static func _event_04_walls() -> EventDef: + var d := EventDef.new() + d.id = &"walls" + d.title = "Walls?" + d.body = "Sleeping under stars is romantic until the wolves arrive." + d.category = EventDef.Category.NUDGE + d.display = EventDef.Display.BANNER + d.cooldown_days = 4 + d.base_weight = 2.0 + d.auto_pause = false + # Trigger: day 5+, build queue has no wall entities and no completed walls. + # Proxy: no doors means no enclosed building. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 5 and World.doors.is_empty() + d.on_resolve = func(_c: int) -> void: + pass + return d + + +# ── Seasonal factories ────────────────────────────────────────────────────── + +static func _event_05_spring_awakens() -> EventDef: + var d := EventDef.new() + d.id = &"spring_awakens" + d.title = "Spring Awakens" + d.body = "The thaw runs in every stream. Crops will grow fast now." + d.category = EventDef.Category.SEASONAL + d.display = EventDef.Display.BANNER + d.cooldown_days = 12 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: first tick of spring. Storyteller fires this once per season boundary. + d.trigger_predicate = func() -> bool: + return Clock.current_season() == Clock.SEASON_SPRING + # Effect: stubbed — Phase 17 will apply +10% crop growth for the season. + d.on_resolve = func(_c: int) -> void: + Audit.log("storyteller", "spring_awakens resolved — crop growth buff stubbed (Phase 17)") + return d + + +static func _event_06_summers_heat() -> EventDef: + var d := EventDef.new() + d.id = &"summers_heat" + d.title = "Summer's Heat" + d.body = "The sun beats down. Unsheltered work will tire faster." + d.category = EventDef.Category.SEASONAL + d.display = EventDef.Display.BANNER + d.cooldown_days = 12 + d.base_weight = 1.0 + d.auto_pause = false + d.trigger_predicate = func() -> bool: + return Clock.current_season() == Clock.SEASON_SUMMER + # Effect: stubbed — Phase 17 will apply outdoor work −5% speed modifier. + d.on_resolve = func(_c: int) -> void: + Audit.log("storyteller", "summers_heat resolved — outdoor fatigue buff stubbed (Phase 17)") + return d + + +static func _event_07_autumns_harvest() -> EventDef: + var d := EventDef.new() + d.id = &"autumns_harvest" + d.title = "Autumn's Harvest" + d.body = "The fields are heavy with the last of the year's bounty." + d.category = EventDef.Category.SEASONAL + d.display = EventDef.Display.BANNER + d.cooldown_days = 12 + d.base_weight = 1.0 + d.auto_pause = false + d.trigger_predicate = func() -> bool: + return Clock.current_season() == Clock.SEASON_AUTUMN + # Effect: stubbed — Phase 17 will apply +15% harvest yield. + d.on_resolve = func(_c: int) -> void: + Audit.log("storyteller", "autumns_harvest resolved — yield buff stubbed (Phase 17)") + return d + + +static func _event_08_winters_edge() -> EventDef: + var d := EventDef.new() + d.id = &"winters_edge" + d.title = "Winter's Edge" + d.body = "Frost has come. The road is closed; you are alone." + d.category = EventDef.Category.SEASONAL + d.display = EventDef.Display.BANNER + d.cooldown_days = 12 + d.base_weight = 1.0 + d.auto_pause = false + d.trigger_predicate = func() -> bool: + return Clock.current_season() == Clock.SEASON_WINTER + # Effect: no wanderers 5 days + raise threat weight — both stubbed Phase 17. + d.on_resolve = func(_c: int) -> void: + Audit.log("storyteller", "winters_edge resolved — wanderer suppress + threat weight stubbed (Phase 17)") + return d + + +# ── Wanderer factories ────────────────────────────────────────────────────── + +static func _event_09_a_traveler() -> EventDef: + var d := EventDef.new() + d.id = &"a_traveler" + d.title = "A Traveler" + d.body = "A weary traveler stumbles toward your gate. They look hungry. Will you welcome them?" + d.category = EventDef.Category.WANDERER + d.display = EventDef.Display.MODAL + d.cooldown_days = 5 + d.base_weight = 1.0 + d.choices = ["Welcome", "Send away"] + d.auto_pause = true + # Trigger: ghost state (all pawns gone) OR ~8 days have passed. + d.trigger_predicate = func() -> bool: + return Storyteller.ghost_state or Clock.current_day() >= 8 + # Effect: Welcome → +1 pawn (stub); Send away → no effect. + d.on_resolve = func(c: int) -> void: + if c == 0: + _spawn_wanderer_pawn({}) + return d + + +static func _event_10_the_refugee_family() -> EventDef: + var d := EventDef.new() + d.id = &"the_refugee_family" + d.title = "The Refugee Family" + d.body = "A family fleeing bandits arrives. They have nothing, but they would work hard." + d.category = EventDef.Category.WANDERER + d.display = EventDef.Display.MODAL + d.cooldown_days = 8 + d.base_weight = 1.0 + d.choices = ["Welcome", "Refuse"] + d.auto_pause = true + # Trigger: post-day-15. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 15 + # Effect: Welcome → +2 pawns low skills (stub); Refuse → −2 mood colony 1 day (stub). + d.on_resolve = func(c: int) -> void: + if c == 0: + _spawn_wanderer_pawn({}) + _spawn_wanderer_pawn({}) + else: + _apply_colony_mood_thought(func() -> Thought: return ThoughtCatalog.slept_on_floor()) # placeholder thought, Phase 17 adds colony_refused_refugees + Audit.log("storyteller", "refugee_family refused — colony mood penalty stubbed (Phase 17)") + return d + + +static func _event_11_the_old_soldier() -> EventDef: + var d := EventDef.new() + d.id = &"the_old_soldier" + d.title = "The Old Soldier" + d.body = "A retired soldier offers his blade for a place by your fire. Combat 8, but old and tired." + d.category = EventDef.Category.WANDERER + d.display = EventDef.Display.MODAL + d.cooldown_days = 8 + d.base_weight = 1.0 + d.choices = ["Welcome", "Refuse"] + d.auto_pause = true + # Trigger: post-day-20. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 20 + d.on_resolve = func(c: int) -> void: + if c == 0: + _spawn_wanderer_pawn({"combat": 8}) + return d + + +static func _event_12_the_wandering_healer() -> EventDef: + var d := EventDef.new() + d.id = &"the_wandering_healer" + d.title = "The Wandering Healer" + d.body = "A traveling healer asks for shelter. She brings knowledge of medicine." + d.category = EventDef.Category.WANDERER + d.display = EventDef.Display.MODAL + d.cooldown_days = 8 + d.base_weight = 1.0 + d.choices = ["Welcome", "Refuse"] + d.auto_pause = true + # Trigger: any pawn has a status (proxy for sick/injured — exact sick() status ships Phase 17). + d.trigger_predicate = func() -> bool: + for p in World.pawns: + if not p.statuses.is_empty(): + return true + return false + d.on_resolve = func(c: int) -> void: + if c == 0: + _spawn_wanderer_pawn({"medicine": 6}) + return d + + +# ── Threat factories ───────────────────────────────────────────────────────── + +static func _event_13_wolves_at_the_edge() -> EventDef: + var d := EventDef.new() + d.id = &"wolves_at_the_edge" + d.title = "Wolves at the Edge" + d.body = "Wolves howl in the distance. They will be here by nightfall." + d.category = EventDef.Category.THREAT + d.display = EventDef.Display.MODAL + d.cooldown_days = 3 + d.base_weight = 1.0 + d.choices = ["Prepare"] + d.auto_pause = true + # Trigger: anytime after day 3 (season-weighting handled by Storyteller tension). + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 3 + # Effect: spawn 1-3 wolves (stubbed until WolfSpawner is accessible statically). + d.on_resolve = func(_c: int) -> void: + _spawn_wolves(randi_range(1, 3)) + return d + + +static func _event_14_lone_wolf() -> EventDef: + var d := EventDef.new() + d.id = &"lone_wolf" + d.title = "Lone Wolf" + d.body = "A starving wolf circles your livestock." + d.category = EventDef.Category.THREAT + d.display = EventDef.Display.MODAL + d.cooldown_days = 3 + d.base_weight = 1.5 # slightly higher — low-threat random, more common + d.choices = ["Prepare"] + d.auto_pause = true + # Trigger: anytime (low-threat random). + d.trigger_predicate = func() -> bool: + return true + d.on_resolve = func(_c: int) -> void: + _spawn_wolves(1) + return d + + +static func _event_15_pack_hunt() -> EventDef: + var d := EventDef.new() + d.id = &"pack_hunt" + d.title = "Pack Hunt" + d.body = "A hunting pack moves through the forest. They smell your colony." + d.category = EventDef.Category.THREAT + d.display = EventDef.Display.MODAL + d.cooldown_days = 5 + d.base_weight = 1.0 + d.choices = ["Prepare"] + d.auto_pause = true + # Trigger: post-day-30. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 30 + d.on_resolve = func(_c: int) -> void: + _spawn_wolves(randi_range(4, 6)) + return d + + +static func _event_16_bandit_scouts() -> EventDef: + var d := EventDef.new() + d.id = &"bandit_scouts" + d.title = "Bandit Scouts" + d.body = "Strange figures watched from the treeline at dusk. Bandits, perhaps." + d.category = EventDef.Category.THREAT + d.display = EventDef.Display.MODAL + d.cooldown_days = 5 + d.base_weight = 1.0 + d.choices = ["Stay alert"] + d.auto_pause = true + # Trigger: post-day-25. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 25 + # Effect: flavor + threat-likelihood raise — stubbed (v2 raids). + d.on_resolve = func(_c: int) -> void: + Audit.log("storyteller", "bandit_scouts resolved — threat-likelihood raise stubbed (v2 raids)") + return d + + +# ── Disease factories ──────────────────────────────────────────────────────── + +static func _event_17_fever() -> EventDef: + var d := EventDef.new() + d.id = &"fever" + d.title = "Fever" + d.body = "%pawn% woke with a fever. The sickness may spread." + d.category = EventDef.Category.DISEASE + d.display = EventDef.Display.MODAL + d.cooldown_days = 4 + d.base_weight = 1.0 + d.choices = ["Treat them"] + d.auto_pause = true + # Trigger: random after ~30 days. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 30 and not World.pawns.is_empty() + # Effect: Sick status on random pawn — StatusCatalog.sick() ships Phase 17. + d.on_resolve = func(_c: int) -> void: + Audit.log("storyteller", "fever resolved — StatusCatalog.sick() stubbed (Phase 17)") + return d + + +static func _event_18_a_bad_cut() -> EventDef: + var d := EventDef.new() + d.id = &"a_bad_cut" + d.title = "A Bad Cut" + d.body = "%pawn% gashed their hand chopping wood. The wound looks deep." + d.category = EventDef.Category.DISEASE + d.display = EventDef.Display.MODAL + d.cooldown_days = 4 + d.base_weight = 1.0 + d.choices = ["Treat them"] + d.auto_pause = true + # Trigger: any tree chopped and low chance (proxy: trees exist and day >= 5). + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 5 and not World.trees.is_empty() + # Effect: Bleeding status on a random pawn — real-wired (StatusCatalog.bleeding exists). + d.on_resolve = func(_c: int) -> void: + _apply_pawn_status(func() -> Status: return StatusCatalog.bleeding(1)) + return d + + +static func _event_19_the_sleeplessness() -> EventDef: + var d := EventDef.new() + d.id = &"the_sleeplessness" + d.title = "The Sleeplessness" + d.body = "%pawn% has barely slept. Something weighs on them." + d.category = EventDef.Category.DISEASE + d.display = EventDef.Display.BANNER + d.cooldown_days = 4 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: any pawn is tired (has the tired thought/status). + d.trigger_predicate = func() -> bool: + for p in World.pawns: + if p.is_tired(): + return true + return false + # Effect: mood penalty 2 days — apply tired thought colony-wide (stubbed intensity). + d.on_resolve = func(_c: int) -> void: + _apply_colony_mood_thought(func() -> Thought: return ThoughtCatalog.tired()) + return d + + +# ── Resource factories ─────────────────────────────────────────────────────── + +static func _event_20_bountiful_harvest() -> EventDef: + var d := EventDef.new() + d.id = &"bountiful_harvest" + d.title = "Bountiful Harvest" + d.body = "Your fields exceeded the season. The granary swells." + d.category = EventDef.Category.RESOURCE + d.display = EventDef.Display.BANNER + d.cooldown_days = 3 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: autumn (harvest season) and crops are present. + d.trigger_predicate = func() -> bool: + return Clock.current_season() == Clock.SEASON_AUTUMN and not World.crops.is_empty() + # Effect: +25% yield — stubbed, Phase 17 crop-yield multiplier. + d.on_resolve = func(_c: int) -> void: + _apply_buff_next_n_jobs(&"harvest", 10, 1.25) + return d + + +static func _event_21_lumberjacks_luck() -> EventDef: + var d := EventDef.new() + d.id = &"lumberjacks_luck" + d.title = "Lumberjack's Luck" + d.body = "%pawn% found a copse of unusually thick trees." + d.category = EventDef.Category.RESOURCE + d.display = EventDef.Display.BANNER + d.cooldown_days = 3 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: trees present (chop job context). + d.trigger_predicate = func() -> bool: + return not World.trees.is_empty() + # Effect: next 3 trees +50% wood — stubbed. + d.on_resolve = func(_c: int) -> void: + _apply_buff_next_n_jobs(&"chop", 3, 1.5) + return d + + +static func _event_22_veins_of_iron() -> EventDef: + var d := EventDef.new() + d.id = &"veins_of_iron" + d.title = "Veins of Iron" + d.body = "A miner reports a rich vein, deeper than expected." + d.category = EventDef.Category.RESOURCE + d.display = EventDef.Display.BANNER + d.cooldown_days = 3 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: rocks present (mine job context). + d.trigger_predicate = func() -> bool: + return not World.rocks.is_empty() + # Effect: next mining yield ×2 — stubbed. + d.on_resolve = func(_c: int) -> void: + _apply_buff_next_n_jobs(&"mine", 1, 2.0) + return d + + +# ── Lore factories ─────────────────────────────────────────────────────────── + +static func _event_23_strange_stones() -> EventDef: + var d := EventDef.new() + d.id = &"strange_stones" + d.title = "Strange Stones" + d.body = "Settlers report finding carved stones in the wood — older than any memory." + d.category = EventDef.Category.LORE + d.display = EventDef.Display.BANNER + d.cooldown_days = 6 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: post-day-15, random. + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 15 + d.on_resolve = func(_c: int) -> void: + pass # flavor only + return d + + +static func _event_24_an_old_map() -> EventDef: + var d := EventDef.new() + d.id = &"an_old_map" + d.title = "An Old Map" + d.body = "%pawn% found a tattered map. Roads to the north, half-faded." + d.category = EventDef.Category.LORE + d.display = EventDef.Display.BANNER + d.cooldown_days = 6 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: post-recruit (more than 1 pawn present). + d.trigger_predicate = func() -> bool: + return World.pawns.size() > 1 + d.on_resolve = func(_c: int) -> void: + pass # flavor; seeds v2 trade/exploration + return d + + +# ── Milestone factory ──────────────────────────────────────────────────────── + +static func _event_25_one_year_survived() -> EventDef: + var d := EventDef.new() + d.id = &"one_year_survived" + d.title = "One Year Survived" + d.body = "A full year. The first frost feels different now — yours is a real settlement." + d.category = EventDef.Category.MILESTONE + d.display = EventDef.Display.BANNER + d.cooldown_days = 30 + d.base_weight = 1.0 + d.auto_pause = false + # Trigger: end of first winter (day >= 4 seasons × 12 days = 48). + d.trigger_predicate = func() -> bool: + return Clock.current_day() >= 48 and Clock.current_season() == Clock.SEASON_WINTER + # Effect: +5 mood "We made it" colony 2 days — real-wired via colony thought. + d.on_resolve = func(_c: int) -> void: + _apply_colony_mood_thought(func() -> Thought: return ThoughtCatalog.well_rested()) + # well_rested() is the closest existing +5 mood thought; Phase 17 adds + # ThoughtCatalog.we_made_it() with correct label + longer duration. + return d + + +# ── Effect helpers ─────────────────────────────────────────────────────────── +## All helpers are static. Stub bodies log via Audit where the underlying +## system is not yet available; real bodies are annotated per-helper. + +## Instantiate a new pawn at a random map-edge tile with optional skill seed. +## STUB — Phase 17 recruit UI will wire the real spawn via PawnFactory or similar. +static func _spawn_wanderer_pawn(skills: Dictionary = {}) -> void: + Audit.log("storyteller", "_spawn_wanderer_pawn called with skills=%s — stubbed (Phase 17)" % str(skills)) + + +## Flag the next `count` jobs of `kind` to use a yield multiplier. +## STUB — Phase 17 work-buff system will implement the ring buffer. +static func _apply_buff_next_n_jobs(kind: StringName, count: int, multiplier: float) -> void: + Audit.log("storyteller", "_apply_buff_next_n_jobs: kind=%s count=%d ×%.2f — stubbed (Phase 17)" % [kind, count, multiplier]) + + +## Emit EventBus.request_wolf_spawn so WolfSpawner (scene child) picks it up. +## WolfSpawner listens on that signal (Phase 10). This is the correct bus-based +## wiring — no get_node("/root/...") smell. +static func _spawn_wolves(count: int) -> void: + if EventBus.has_signal("request_wolf_spawn"): + EventBus.emit_signal("request_wolf_spawn", count) + else: + Audit.log("storyteller", "_spawn_wolves: EventBus.request_wolf_spawn not declared — stubbed (Phase 17)") + + +## Apply a status to a random pawn (or a specific pawn if non-null). +## The factory callable returns a fresh Status — mirrors StatusCatalog pattern. +## REAL for statuses that exist (bleeding); STUB for sick() until Phase 17. +static func _apply_pawn_status(status_factory: Callable, pawn = null) -> void: + var target = pawn + if target == null: + if World.pawns.is_empty(): + Audit.log("storyteller", "_apply_pawn_status: no pawns present") + return + target = World.pawns[randi() % World.pawns.size()] + if target.has_method("add_status"): + target.add_status(status_factory.call()) + else: + Audit.log("storyteller", "_apply_pawn_status: target missing add_status()") + + +## Apply a thought to every living pawn in the colony. +## `thought_factory` is a Callable that returns a fresh Thought each time. +## REAL — ThoughtCatalog thoughts and Pawn.add_thought() both exist. +static func _apply_colony_mood_thought(thought_factory: Callable) -> void: + for p in World.pawns: + if p.has_method("add_thought"): + p.add_thought(thought_factory.call()) diff --git a/scenes/storyteller/event_catalog.gd.uid b/scenes/storyteller/event_catalog.gd.uid new file mode 100644 index 0000000..d2f1f7e --- /dev/null +++ b/scenes/storyteller/event_catalog.gd.uid @@ -0,0 +1 @@ +uid://bcvgf1norskt3 diff --git a/scenes/storyteller/event_def.gd b/scenes/storyteller/event_def.gd new file mode 100644 index 0000000..33dab1a --- /dev/null +++ b/scenes/storyteller/event_def.gd @@ -0,0 +1,81 @@ +class_name EventDef extends RefCounted +## Phase 15 — Storyteller event definition (data class). +## +## EventDef is the static, authored shape of one event from the 25-prompt corpus +## (see docs/design.md "Prompt corpus"). Storyteller picks one EventDef per +## daily 6 AM roll, then runs its trigger predicate + cooldown gates, then +## applies its effect. +## +## EventDef instances are constructed in EventCatalog (`scenes/storyteller/event_catalog.gd`) +## by Agent A. They're treated as immutable at runtime; per-event cooldown state +## lives in Storyteller, NOT here. +## +## Categories (locked, mirror design.md headings): +## NUDGE — soft dismissible banner, no pause +## SEASONAL — ambient banner on season transition +## WANDERER — modal with choice (Welcome / Refuse) +## THREAT — modal with auto-pause; spawns hostiles +## DISEASE — modal or nudge; applies status to a pawn +## RESOURCE — banner; applies a transient buff +## LORE — flavor banner, no mechanical effect +## MILESTONE — celebratory banner; small mood thought +## +## Display kinds: +## BANNER — non-blocking, auto-dismiss +## MODAL — blocking, may auto-pause, may carry choices + +enum Category { + NUDGE, + SEASONAL, + WANDERER, + THREAT, + DISEASE, + RESOURCE, + LORE, + MILESTONE, +} + +enum Display { + BANNER, + MODAL, +} + +## Stable id. Used for cooldown tracking and Strings.t() keys. +var id: StringName = &"" + +## Display title — short. Localized via Strings catalog by Agent A. +var title: String = "" + +## Body copy — 1-2 sentences, supports `%pawn%` substitution. +var body: String = "" + +var category: Category = Category.NUDGE +var display: Display = Display.BANNER + +## Cooldown in in-game days from this event. Per-event gate. +## Per-category cooldowns live in Storyteller.CATEGORY_COOLDOWN_DAYS. +var cooldown_days: int = 3 + +## Base weight in the daily roll's weighted pool. Tension/state modifiers +## multiply this at roll time. +var base_weight: float = 1.0 + +## Optional choice labels for MODAL events (e.g. ["Welcome", "Send away"]). +## Empty array = dismiss-only. +var choices: Array[String] = [] + +## If true, this event causes a sim auto-pause when fired (Storyteller calls +## Sim.set_speed(PAUSE) before showing the UI). +var auto_pause: bool = false + +## Optional tile to focus the "Go there" camera-pan on. Vector2i(-1, -1) = no jump. +var focus_tile: Vector2i = Vector2i(-1, -1) + +## Predicate evaluated by Storyteller before this event is eligible to roll. +## Set in the EventCatalog factory; returns true when world state matches. +## Signature: Callable that takes no args, returns bool. +var trigger_predicate: Callable = Callable() + +## Effect applied when the event resolves (chosen path for modals, immediate for banners). +## Signature: Callable taking (choice_index: int) — choice 0 = first option / dismiss. +var on_resolve: Callable = Callable() diff --git a/scenes/storyteller/event_def.gd.uid b/scenes/storyteller/event_def.gd.uid new file mode 100644 index 0000000..416300a --- /dev/null +++ b/scenes/storyteller/event_def.gd.uid @@ -0,0 +1 @@ +uid://fymgewgr885g diff --git a/scenes/ui/storyteller_banner.gd b/scenes/ui/storyteller_banner.gd new file mode 100644 index 0000000..9ecbc10 --- /dev/null +++ b/scenes/ui/storyteller_banner.gd @@ -0,0 +1,198 @@ +class_name StorytellerBanner extends CanvasLayer +## Phase 15 — Non-blocking ambient banner for BANNER-display EventDef events. +## +## Anchored top-center just below the top bar (~60 px Y offset). +## Shows: title (bold), body, optional "Go there" button. +## Auto-dismisses after 6 sec via Timer; tap-to-dismiss-early also supported. +## +## Multiple banners queue — only one is visible at a time. On dismiss the +## next queued event is shown automatically. +## +## On dismiss: calls Storyteller.resolve_current(0). +## Subscribes to EventBus.storyteller_event_fired in _ready; ignores MODAL events. + +const AUTO_DISMISS_SEC: float = 6.0 +const TILE_SIZE_PX: int = 16 + +## Internal queue of pending EventDef refs (BANNER display only). +var _queue: Array = [] +## The EventDef currently on screen (null when idle). +var _current_event = null + +# ── node refs ─────────────────────────────────────────────────────────────── +var _root: Control = null +var _title_label: Label = null +var _body_label: Label = null +var _go_there_btn: Button = null +var _dismiss_btn: Button = null +var _timer: Timer = null + + +func _ready() -> void: + layer = 15 # above world (0), above top-bar (10), below modal (20) + + _build_ui() + _root.visible = false + + EventBus.storyteller_event_fired.connect(_on_event_fired) + Audit.log("storyteller_ui", "StorytellerBanner ready") + + +func _exit_tree() -> void: + if EventBus.storyteller_event_fired.is_connected(_on_event_fired): + EventBus.storyteller_event_fired.disconnect(_on_event_fired) + + +# ── UI construction ────────────────────────────────────────────────────────── + +func _build_ui() -> void: + # Outer anchor — top-center strip. + _root = Control.new() + _root.name = "BannerRoot" + _root.set_anchors_preset(Control.PRESET_TOP_WIDE) + _root.custom_minimum_size = Vector2(0, 80) + _root.offset_top = 60 # sit just below the 48 px top bar + _root.offset_bottom = 140 + _root.mouse_filter = Control.MOUSE_FILTER_PASS + add_child(_root) + + # Panel background centred horizontally. + var panel := PanelContainer.new() + panel.name = "Panel" + panel.set_anchors_preset(Control.PRESET_CENTER_TOP) + panel.custom_minimum_size = Vector2(480, 80) + # Offset so it stays centred regardless of screen width. + panel.offset_left = -240 + panel.offset_right = 240 + panel.offset_top = 0 + panel.offset_bottom = 80 + panel.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND + _root.add_child(panel) + + # Tap-anywhere-to-dismiss — GuiInput on the panel itself. + panel.gui_input.connect(_on_panel_gui_input) + + var vbox := VBoxContainer.new() + vbox.add_theme_constant_override("separation", 4) + panel.add_child(vbox) + + _title_label = Label.new() + _title_label.name = "TitleLabel" + _title_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + _title_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + vbox.add_child(_title_label) + + _body_label = Label.new() + _body_label.name = "BodyLabel" + _body_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + _body_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + vbox.add_child(_body_label) + + var btn_row := HBoxContainer.new() + btn_row.alignment = BoxContainer.ALIGNMENT_CENTER + btn_row.add_theme_constant_override("separation", 8) + vbox.add_child(btn_row) + + _go_there_btn = Button.new() + _go_there_btn.name = "GoThereBtn" + _go_there_btn.text = Strings.t(&"ui.go_there") + _go_there_btn.custom_minimum_size = Vector2(96, 48) + _go_there_btn.focus_mode = Control.FOCUS_NONE + _go_there_btn.visible = false + _go_there_btn.pressed.connect(_on_go_there_pressed) + btn_row.add_child(_go_there_btn) + + _dismiss_btn = Button.new() + _dismiss_btn.name = "DismissBtn" + _dismiss_btn.text = Strings.t(&"ui.dismiss") + _dismiss_btn.custom_minimum_size = Vector2(96, 48) + _dismiss_btn.focus_mode = Control.FOCUS_NONE + _dismiss_btn.pressed.connect(_on_dismiss_pressed) + btn_row.add_child(_dismiss_btn) + + # Auto-dismiss timer. + _timer = Timer.new() + _timer.name = "AutoDismiss" + _timer.one_shot = true + _timer.wait_time = AUTO_DISMISS_SEC + _timer.timeout.connect(_on_dismiss_pressed) + add_child(_timer) + + +# ── event handling ─────────────────────────────────────────────────────────── + +func _on_event_fired(event) -> void: + # Only handle banner-display events. + if event.display != EventDef.Display.BANNER: + return + _queue.append(event) + if _current_event == null: + _show_next() + + +func _show_next() -> void: + if _queue.is_empty(): + _root.visible = false + _current_event = null + return + + _current_event = _queue.pop_front() + var ev = _current_event + + _title_label.text = ev.title + _body_label.text = _substitute_pawn(ev.body) + + var has_focus: bool = ev.focus_tile != Vector2i(-1, -1) + _go_there_btn.visible = has_focus + + _root.visible = true + _timer.start() + Audit.log("storyteller_ui", "banner shown: %s" % ev.id) + + +func _on_dismiss_pressed() -> void: + if not _timer.is_stopped(): + _timer.stop() + Storyteller.resolve_current(0) + Audit.log("storyteller_ui", "banner dismissed: %s" % (_current_event.id if _current_event != null else "(none)")) + _current_event = null + _show_next() + + +func _on_go_there_pressed() -> void: + if _current_event == null: + return + _pan_camera(_current_event.focus_tile) + # Also dismiss so the banner clears after the pan. + _on_dismiss_pressed() + + +func _on_panel_gui_input(event: InputEvent) -> void: + if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT: + _on_dismiss_pressed() + elif event is InputEventScreenTouch and event.pressed: + _on_dismiss_pressed() + + +# ── helpers ────────────────────────────────────────────────────────────────── + +func _pan_camera(tile: Vector2i) -> void: + var cam = get_node_or_null("/root/Main/World/CameraRig") + if cam == null: + Audit.log("storyteller_ui", "pan_camera: CameraRig not found") + return + if cam.has_method("pan_to_tile"): + cam.pan_to_tile(tile) + else: + # Fallback: instant snap to tile centre. + cam.position = Vector2(tile.x * TILE_SIZE_PX + TILE_SIZE_PX / 2, + tile.y * TILE_SIZE_PX + TILE_SIZE_PX / 2) + + +func _substitute_pawn(body: String) -> String: + if not "%pawn%" in body: + return body + var name: String = "Settler" + if not World.pawns.is_empty(): + name = World.pawns[0].pawn_name + return body.replace("%pawn%", name) diff --git a/scenes/ui/storyteller_banner.gd.uid b/scenes/ui/storyteller_banner.gd.uid new file mode 100644 index 0000000..a2a0bc5 --- /dev/null +++ b/scenes/ui/storyteller_banner.gd.uid @@ -0,0 +1 @@ +uid://bltjvp7kin4vg diff --git a/scenes/ui/storyteller_modal.gd b/scenes/ui/storyteller_modal.gd new file mode 100644 index 0000000..fa56e98 --- /dev/null +++ b/scenes/ui/storyteller_modal.gd @@ -0,0 +1,199 @@ +class_name StorytellerModal extends CanvasLayer +## Phase 15 — Blocking center-screen dialog for MODAL-display EventDef events. +## +## Visible only when a modal event is active; hidden otherwise. +## Choice layout: +## 0 choices → single "Dismiss" button (choice index 0) +## 1 choice → labelled button + Dismiss (choice 0 / 1) +## 2 choices → both as labelled buttons (choice 0 / 1) +## +## Optional "Go there" button if focus_tile != Vector2i(-1, -1). +## Tapping "Go there" pans camera AND closes the modal. +## +## Subscribes to EventBus.storyteller_event_fired in _ready; ignores BANNER events. +## Auto-pause is handled by Storyteller before the signal — no re-pause here. + +const TILE_SIZE_PX: int = 16 + +var _current_event = null + +# ── node refs ──────────────────────────────────────────────────────────────── +var _dim: ColorRect = null # full-screen dim behind panel +var _panel: PanelContainer = null +var _title_label: Label = null +var _body_label: Label = null +var _choice_row: HBoxContainer = null +var _go_there_btn: Button = null + + +func _ready() -> void: + layer = 20 # above banner (15), above top-bar (10) + + _build_ui() + _set_visible(false) + + EventBus.storyteller_event_fired.connect(_on_event_fired) + # Also hide on external resolve (test scripts, ghost-state auto-fire chain). + EventBus.storyteller_event_resolved.connect(_on_event_resolved) + Audit.log("storyteller_ui", "StorytellerModal ready") + + +func _exit_tree() -> void: + if EventBus.storyteller_event_fired.is_connected(_on_event_fired): + EventBus.storyteller_event_fired.disconnect(_on_event_fired) + if EventBus.storyteller_event_resolved.is_connected(_on_event_resolved): + EventBus.storyteller_event_resolved.disconnect(_on_event_resolved) + + +func _on_event_resolved(_event, _choice_index: int) -> void: + _set_visible(false) + + +# ── UI construction ────────────────────────────────────────────────────────── + +func _build_ui() -> void: + # Full-screen dim layer (semi-transparent black). + _dim = ColorRect.new() + _dim.name = "Dim" + _dim.set_anchors_preset(Control.PRESET_FULL_RECT) + _dim.color = Color(0.0, 0.0, 0.0, 0.45) + _dim.mouse_filter = Control.MOUSE_FILTER_IGNORE + add_child(_dim) + + # Centred dialog panel. + _panel = PanelContainer.new() + _panel.name = "Dialog" + _panel.set_anchors_preset(Control.PRESET_CENTER) + _panel.custom_minimum_size = Vector2(400, 200) + _panel.offset_left = -200 + _panel.offset_right = 200 + _panel.offset_top = -100 + _panel.offset_bottom = 100 + add_child(_panel) + + var vbox := VBoxContainer.new() + vbox.add_theme_constant_override("separation", 12) + _panel.add_child(vbox) + + _title_label = Label.new() + _title_label.name = "TitleLabel" + _title_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + _title_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + vbox.add_child(_title_label) + + _body_label = Label.new() + _body_label.name = "BodyLabel" + _body_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + _body_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + vbox.add_child(_body_label) + + # "Go there" row — always present, shown/hidden per-event. + _go_there_btn = Button.new() + _go_there_btn.name = "GoThereBtn" + _go_there_btn.text = Strings.t(&"ui.go_there") + _go_there_btn.custom_minimum_size = Vector2(120, 48) + _go_there_btn.focus_mode = Control.FOCUS_NONE + _go_there_btn.pressed.connect(_on_go_there_pressed) + var go_row := HBoxContainer.new() + go_row.alignment = BoxContainer.ALIGNMENT_CENTER + go_row.add_child(_go_there_btn) + vbox.add_child(go_row) + + # Choice-button row — children rebuilt each event. + _choice_row = HBoxContainer.new() + _choice_row.name = "ChoiceRow" + _choice_row.alignment = BoxContainer.ALIGNMENT_CENTER + _choice_row.add_theme_constant_override("separation", 12) + vbox.add_child(_choice_row) + + +# ── event handling ─────────────────────────────────────────────────────────── + +func _on_event_fired(event) -> void: + if event.display != EventDef.Display.MODAL: + return + _current_event = event + _populate(event) + _set_visible(true) + Audit.log("storyteller_ui", "modal shown: %s" % event.id) + + +func _populate(event) -> void: + _title_label.text = event.title + _body_label.text = _substitute_pawn(event.body) + + var has_focus: bool = event.focus_tile != Vector2i(-1, -1) + _go_there_btn.visible = has_focus + + # Clear old choice buttons. + for child in _choice_row.get_children(): + child.queue_free() + + # Build new choice buttons based on choices array length. + var choices: Array[String] = event.choices + match choices.size(): + 0: + # No choices — single Dismiss. + _add_choice_btn(Strings.t(&"ui.dismiss"), 0) + 1: + # One named choice + Dismiss (dismiss = index 1). + _add_choice_btn(choices[0], 0) + _add_choice_btn(Strings.t(&"ui.dismiss"), 1) + _: + # Two (or more) choices — render first two, indexed. + _add_choice_btn(choices[0], 0) + _add_choice_btn(choices[1], 1) + + +func _add_choice_btn(label: String, choice_index: int) -> void: + var btn := Button.new() + btn.text = label + btn.custom_minimum_size = Vector2(120, 48) + btn.focus_mode = Control.FOCUS_NONE + btn.pressed.connect(func() -> void: _on_choice(choice_index)) + _choice_row.add_child(btn) + + +func _on_choice(choice_index: int) -> void: + Audit.log("storyteller_ui", "modal choice %d: %s" % [choice_index, _current_event.id if _current_event != null else "(none)"]) + Storyteller.resolve_current(choice_index) + _current_event = null + _set_visible(false) + + +func _on_go_there_pressed() -> void: + if _current_event == null: + return + _pan_camera(_current_event.focus_tile) + # Close modal after pan — player is navigating there. + _on_choice(0) + + +# ── helpers ────────────────────────────────────────────────────────────────── + +func _set_visible(v: bool) -> void: + if _dim != null: + _dim.visible = v + if _panel != null: + _panel.visible = v + + +func _pan_camera(tile: Vector2i) -> void: + var cam = get_node_or_null("/root/Main/World/CameraRig") + if cam == null: + Audit.log("storyteller_ui", "pan_camera: CameraRig not found") + return + if cam.has_method("pan_to_tile"): + cam.pan_to_tile(tile) + else: + cam.position = Vector2(tile.x * TILE_SIZE_PX + TILE_SIZE_PX / 2, + tile.y * TILE_SIZE_PX + TILE_SIZE_PX / 2) + + +func _substitute_pawn(body: String) -> String: + if not "%pawn%" in body: + return body + var name: String = "Settler" + if not World.pawns.is_empty(): + name = World.pawns[0].pawn_name + return body.replace("%pawn%", name) diff --git a/scenes/ui/storyteller_modal.gd.uid b/scenes/ui/storyteller_modal.gd.uid new file mode 100644 index 0000000..6ba4d4d --- /dev/null +++ b/scenes/ui/storyteller_modal.gd.uid @@ -0,0 +1 @@ +uid://3yl5uek6owj2 diff --git a/scenes/world/camera_rig.gd b/scenes/world/camera_rig.gd index 85fb4a6..4d44e66 100644 --- a/scenes/world/camera_rig.gd +++ b/scenes/world/camera_rig.gd @@ -107,6 +107,16 @@ func _centre_on(world_pos: Vector2) -> void: _centre_tween.tween_property(self, "position", world_pos, 0.2) +## Public API — smooth-pan the camera to the centre of a tile (world space). +## Used by StorytellerBanner and StorytellerModal "Go there" buttons. +## Reuses the same _centre_tween slot so a rapid second call replaces the first. +func pan_to_tile(tile: Vector2i) -> void: + const TILE_PX: int = 16 + var world_pos := Vector2(tile.x * TILE_PX + TILE_PX / 2, tile.y * TILE_PX + TILE_PX / 2) + _centre_on(world_pos) + Audit.log("camera", "pan_to_tile %s → world %s" % [tile, world_pos]) + + ## Public API — call once from the world scene after map bounds are known. func set_world_bounds(rect: Rect2) -> void: limit_left = int(rect.position.x) - BOUNDS_BLEED_PX diff --git a/scenes/world/world.gd b/scenes/world/world.gd index bb20ac4..795faaf 100644 --- a/scenes/world/world.gd +++ b/scenes/world/world.gd @@ -24,6 +24,9 @@ const INDOOR_TINT_SCRIPT: Script = preload("res://scenes/world/indoor_tint_overl # Phase 14 — grave / burial entities (scripts only; no .tscn needed). const GRAVEYARD_ZONE_SCRIPT: Script = preload("res://scenes/world/graveyard_zone.gd") const GRAVE_SLOT_SCRIPT: Script = preload("res://scenes/entities/grave_slot.gd") +# Phase 15 — EventCatalog (class_name in scenes/storyteller/) preloaded so the +# class is resolved before world._ready() fires. Avoids parse-order ambiguity. +const EVENT_CATALOG_SCRIPT: Script = preload("res://scenes/storyteller/event_catalog.gd") const PAWN_SCENE: PackedScene = preload("res://scenes/pawn/pawn.tscn") const TREE_SCENE: PackedScene = preload("res://scenes/entities/tree.tscn") const ROCK_SCENE: PackedScene = preload("res://scenes/entities/rock.tscn") @@ -207,6 +210,10 @@ func _ready() -> void: beauty.recompute_all() _run_pathfinder_spike() + # Phase 15 — register all 25 Storyteller events now that World state and + # autoloads are fully mounted (trigger predicates read Clock, World, Storyteller). + EVENT_CATALOG_SCRIPT.register_all(Storyteller) + # Phase 4: every 5 in-game seconds (100 ticks), re-evaluate items in # stockpiles in case a higher-priority destination opened up. EventBus.sim_tick.connect(_on_sim_tick_world_sweep)