World Events And Scenario Director
What This System Is
The World Event Director composes existing conditions, quest flags, runtime events, dialogue, music, NPC states, scene spawners, and named encounters into durable multi-phase scenarios. Definitions are data. The shared reducer is deterministic and independent of Bevy or server transport, so previews and authority use the same phase rules.
The catalog supports player, party, zone, and realm scopes. The first authoritative adapter reconciles player-scoped events whenever gameplay state and the quest snapshot refresh. Its state is embedded in the existing persisted gameplay JSON, so active phases, completion history, cooldowns, and pending action edges survive reconnects without a database migration.
How Authors Use It
Edit assets/gameplay/world_event_catalog.json. Each event declares start conditions, an initial phase, and an acyclic phase graph. A phase may use minimum and maximum durations, completion conditions, activation conditions for branches, map markers, and enter or exit actions.
Conditions use the shared authored syntax, including quest_flag:, scene_var:, faction_rep:, runtime_event_seen:, and inventory checks. Actions can apply safe shared state effects, spawn a named encounter through a scene spawner, start dialogue, change music, or request an NPC state.
The runtime catalog hot-reload lane parses world events atomically with abilities, status effects, passives, and enemy content. Cross-catalog validation rejects unknown named encounters before replacing the live catalog.
State And Delivery
Every phase edge receives a stable dispatch id containing event, scope, run, phase, edge, and action index. State-only effects are applied and acknowledged by authority immediately. External actions remain in the director's durable outbox until the unified server dispatcher acknowledges them. Re-evaluating the same phase never emits the same edge twice.
The dispatcher claims work with a persisted lease before execution. A failed action records its error and retries with exponential backoff; after five attempts it becomes a terminal failure rather than looping forever. Music and dialogue reuse the authoritative trigger stream, encounter actions validate that the hosted scene spawner is bound to the named encounter, and NPC state writes npc_state.<npc_id> into replicated scene variables. Dispatch state is drained on session activation and after accepted gameplay or combat actions.
The shared projection reports active phase, elapsed time, markers, status, scope, and pending and terminal-failure action counts for UI and editor consumers. It travels in the gameplay-condition snapshot. The Event Console creates in-progress or error entries from that projection, so dispatch health is inspectable without reading server logs. Completed non-repeatable events stay completed. Repeatable events can restart only after their authored cooldown and start gates pass again.
Validation And Debugging
Validate phase graph references, named encounters, dialogue actions, NPC state requests, and spawner bindings before testing runtime. If an event stalls, inspect the active phase, elapsed time gate, completion rule, pending dispatch count, terminal failure count, and Event Console entry.
Rules And Invariants
- Event and phase ids are stable and case-insensitive; phase graphs are acyclic.
- Every event has a valid initial phase and every transition references a phase in that event.
- A minimum duration must not exceed the optional maximum duration.
- An active phase advances only after its time gate and completion rule pass, or its maximum duration expires.
- If no authored branch is currently eligible, the current phase remains active.
- Automatic effects cannot mutate inventory or currency. Rewards stay with transactional economy/progression owners.
- Encounter actions must reference a named enemy encounter and a non-empty scene spawner id.
- Pending non-state actions are persisted and deduplicated across refresh and reconnect.
Examples
world_event.slime_infestation demonstrates warning, road defense, boss, and resolution phases. Setting world_events.slime_infestation.started starts it. Finishing the authored guide warning automatically acknowledges the warning phase. The dispatcher starts durable road and core encounter instances in scene_2; the Encounter Director schedules their authored waves and records the configured completion event only after the final wave. Each phase owns its markers and presentation/action edges, while resolution adds a journal unlock and the final completion flag.