Principle:4thfever Cultivation world simulator Phase Based Simulation Pipeline
| Knowledge Sources | |
|---|---|
| Domains | Game_Architecture, Simulation |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
A simulation architecture where each simulation step (one in-game time unit) is decomposed into an ordered sequence of independent phases that share a common context, enabling modular, extensible, and testable game logic.
Description
Phase-Based Simulation Pipeline addresses the complexity of a rich game simulation step that must perform dozens of distinct operations—perception updates, AI decisions, action execution, combat resolution, relationship evolution, death handling, birth, storytelling, and world maintenance—in a predictable, maintainable order.
Instead of a monolithic step function, the pattern introduces:
- SimulationPhase: an immutable descriptor (name, index, handler function, reset-check flag)
- Phase Registry: an ordered tuple of all phases, serving as the single source of truth for execution order
- Shared Context: a dataclass passed to every phase, accumulating events and holding the living-avatar snapshot
- Phase Runner: an orchestrator that iterates phases, awaits async handlers, checks for reset requests, and returns the finalized event list
This design allows:
- New phases to be added by registering a handler function and inserting a `SimulationPhase` entry
- Each phase to be tested in isolation by calling its handler with a mock context
- Reset/interruption to be checked between phases
- Mix of sync and async phases (runner auto-detects awaitables)
Usage
Use this principle when:
- A simulation step involves 10+ distinct sub-operations with complex ordering dependencies
- You need to add, remove, or reorder sub-operations without modifying a monolithic function
- Sub-operations have varying async requirements (some need LLM calls, others are pure computation)
- The simulation must support mid-step interruption (e.g., reset requests from external commands)
- Testing individual sub-operations in isolation is important
Theoretical Basis
The core structure is a phased pipeline with shared mutable context:
# Abstract algorithm description (NOT real implementation)
@dataclass
class PhaseContext:
world: World
living_avatars: list[Avatar]
events: list[Event] = []
processed_ids: set[str] = set()
@dataclass(frozen=True)
class Phase:
name: str
index: int
handler: Callable[[Simulator, Context], Any]
reset_check_after: bool = True
class PhaseRunner:
def __init__(self, simulator, phases):
self.simulator = simulator
self.phases = phases
async def run(self) -> list[Event]:
ctx = Context.create(self.simulator.world)
for phase in self.phases:
self.check_reset() # honor mid-step interruption
result = phase.handler(self.simulator, ctx)
if isawaitable(result):
result = await result
ctx.add_events(result)
if phase.name == "finalize_step":
return result
return []
Key design properties:
- Ordered execution: phases run in a fixed sequence defined by the registry tuple
- Context accumulation: events from all phases collect in `ctx.events`; finalization deduplicates and persists them
- Living-avatar snapshot: taken once at step start; death resolution phases modify it in-place
- Reset awareness: after each phase (except finalize), the runner checks if a reset was requested and aborts cleanly
- Async flexibility: handlers may be sync or async; the runner auto-detects via `inspect.isawaitable`
Practical Guide
When implementing a phased simulation pipeline:
- Take the living-avatar snapshot once: at step start, copy `world.avatar_manager.get_living_avatars()` into the context; death phases remove from this list in-place
- Register phases as a tuple: the ordered tuple is the single source of truth for execution order
- Keep handlers focused: each handler does one thing (e.g., "decide actions", "resolve death") and returns events
- Finalize at the end: the last phase deduplicates events, persists them to the event manager, logs, and increments the time stamp
- Check for reset between phases: allows external commands to interrupt the simulation cleanly