Principle:4thfever Cultivation world simulator Probability Gated Narrative Expansion
| Knowledge Sources | |
|---|---|
| Domains | Narrative_Generation, LLM_Applications, Game_Design |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
A narrative generation pattern where factual gameplay events are selectively expanded into rich story text by an LLM, gated by per-event-kind probability to control narrative density and LLM cost.
Description
Probability-Gated Narrative Expansion addresses two competing needs in simulation games: factual events ("X defeated Y") are too dry for engaging narrative, but generating LLM story text for every event is prohibitively expensive and drowns the player in text.
The pattern:
- Base factual event production: gameplay systems (combat, cultivation, fortune, social) produce factual `Event` instances with `is_story=False`
- Probability check: `StoryEventService.should_trigger(kind)` rolls against a per-kind probability from config; gathering events always trigger (100%), others are configurable
- Actor filtering and context enrichment: None-valued actors are filtered; the base prompt is enriched with each actor's "goldfinger" story hints
- LLM narrative generation: `StoryTeller.tell_story` selects a template (single-actor vs. dual-actor vs. gathering), fills it with world/actor context, and calls the LLM
- Story event creation: the LLM-generated text becomes a new `Event` with `is_story=True`, persisted alongside factual events
This allows the game to produce a mix of dry factual logs and rich narrative passages, with the ratio controlled by configuration.
Usage
Use this principle when:
- A simulation produces many factual events, but only some should be narrated
- LLM cost is a concern: you can't afford to narrate every event
- Different event kinds warrant different narrative frequencies (combat is exciting, daily social is not)
- Gathering/group events should always be narrated (100% rate)
- Fallback text should exist when LLM fails or returns empty
Theoretical Basis
The core algorithm is a probability gate followed by template-based LLM expansion:
# Abstract algorithm description (NOT real implementation)
async def maybe_create_story(event_kind, start_text, result_text, actors):
# Gate 1: probability check
if not should_trigger(event_kind):
return None
# Gate 2: filter actors
filtered = [a for a in actors if a is not None]
if not filtered:
return None
# Enrich prompt with goldfinger hints
prompt = merge_with_goldfinger(base_prompt, *filtered)
# Select template based on actor count and kind
if event_kind == GATHERING:
story = await tell_gathering_story(...)
elif len(filtered) >= 2 and allow_relation_changes:
story = await tell_story_dual(...)
else:
story = await tell_story_single(...)
# Fallback if LLM returns empty
if not story:
story = f"{start_text}. {result_text}."
return Event(content=story, is_story=True)
def should_trigger(kind):
if kind == GATHERING:
return True # always
return random.random() < get_probability(kind)
Key design properties:
- Per-kind probability: each `StoryEventKind` has its own probability in `config.yml`, allowing fine-grained control
- Gathering exception: gathering events bypass probability and always trigger (100%)
- Goldfinger enrichment: each actor may have "goldfinger" story hints that are appended to the prompt
- Template selection: dual-actor template for relationship events, single-actor otherwise, gathering template for group events
- Fallback text: if the LLM returns empty or fails, the original factual text is used
- is_story flag: story events are marked `is_story=True` and excluded from memory indexing; they don't use `is_major=True`
Practical Guide
When implementing probability-gated narrative expansion:
- Produce factual events first: always create the base `Event` with factual content before deciding whether to add a story
- Centralize the probability gate: use a single `should_trigger` method that reads from config, so probabilities can be tuned without code changes
- Use locale-specific templates: story templates should be in the game's configured language
- Handle empty LLM responses: always have a fallback that concatenates the factual text
- Mark story events distinctly: use `is_story=True`, never `is_major=True`, for LLM-expanded narrative text
- Include random story styles: pick a random narrative style from a pool to add variety