Implementation:4thfever Cultivation world simulator SimulationPhase Registry
| Knowledge Sources | |
|---|---|
| Domains | Simulation, Game_Engine |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Concrete tool for defining and ordering the 29 simulation phases, including the phase runner orchestrator and shared step context, provided by the Cultivation World Simulator engine.
Description
The SimulationPhase Registry consists of three components:
- SimulationPhase: a frozen dataclass with `name`, `index`, `handler_name`, `handler` callable, and `reset_check_after` flag
- SIMULATION_PHASES: an ordered tuple of 29 `SimulationPhase` instances, defining the execution order
- SimulationPhaseRunner: the orchestrator that creates a `SimulationStepContext`, iterates phases, awaits async handlers, checks for reset requests, and returns finalized events
- SimulationStepContext: a dataclass holding `world`, `living_avatars` snapshot, accumulated `events`, `processed_event_ids`, and `month_stamp`
The 29 phases span: perception, objective thinking, gatherings, AI decisions, plan commitment, action execution, opportunities, world secrets, interactions (twice), relation evolution, death resolution, treasure lifecycle, POI discovery, age/birth, backstory, passive effects, autonomous creation, random minor events, background NPC events, sect random events, sect wars, nicknames, celestial phenomena, city population, dynasty/officials, calculated relations, annual maintenance, and step finalization.
Usage
Import these components when:
- Running a simulation step: `SimulationPhaseRunner` is used by `Simulator.step()`
- Adding a new phase: define a handler function, add a `SimulationPhase` entry to the tuple
- Testing phases in isolation: call individual phase handlers with a mock `SimulationStepContext`
Code Reference
Source Location
- Repository: cultivation-world-simulator
- File: src/sim/simulator_engine/phase_registry.py, src/sim/simulator_engine/phase_runner.py, src/sim/simulator_engine/context.py
Signature
@dataclass(frozen=True, slots=True)
class SimulationPhase:
name: str
index: int
handler_name: str
handler: PhaseHandler
reset_check_after: bool = True
SIMULATION_PHASES: tuple[SimulationPhase, ...] = ( # 29 phases
SimulationPhase("update_perception_and_knowledge", 1, ...),
SimulationPhase("long_term_objective_thinking", 2, ...),
# ... 27 more phases ...
SimulationPhase("finalize_step", 29, "finalize_step", finalize_step_phase, reset_check_after=False),
)
def get_simulation_phases() -> tuple[SimulationPhase, ...]:
return SIMULATION_PHASES
class SimulationPhaseRunner:
def __init__(self, simulator: Any, phases: tuple[SimulationPhase, ...] | None = None):
self.simulator = simulator
self.world = simulator.world
self.phases = phases or get_simulation_phases()
self.handlers = {phase.handler_name: phase.handler for phase in self.phases}
def raise_if_reset_requested(self) -> None:
"""Abort the step if a reset was requested by an external command."""
async def run(self) -> list[Any]:
"""Execute all phases in order, returning finalized events."""
@dataclass(slots=True)
class SimulationStepContext:
world: World
living_avatars: list[Avatar]
events: list[Event] = field(default_factory=list)
processed_event_ids: set[str] = field(default_factory=set)
month_stamp: MonthStamp | None = None
@classmethod
def create(cls, world: World) -> "SimulationStepContext":
return cls(
world=world,
living_avatars=world.avatar_manager.get_living_avatars(),
month_stamp=world.month_stamp,
)
@property
def is_january(self) -> bool:
return self.month_stamp is not None and self.month_stamp.get_month() == Month.JANUARY
def add_events(self, new_events: list[Event] | None) -> None:
if new_events:
self.events.extend(new_events)
Import
from src.sim.simulator_engine.phase_registry import SimulationPhase, SIMULATION_PHASES, get_simulation_phases
from src.sim.simulator_engine.phase_runner import SimulationPhaseRunner
from src.sim.simulator_engine.context import SimulationStepContext
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| simulator | Simulator | Simulator with world and sect_manager | |
| phases | None | Custom phase tuple (default: all 29 phases) |
Outputs
| Name | Type | Description |
|---|---|---|
| run() return | Finalized, deduplicated events from all phases |
Usage Examples
Run All Phases (via Simulator)
sim = Simulator(world)
events = await sim.step() # internally calls SimulationPhaseRunner(self).run()
Run Custom Phase Subset
from src.sim.simulator_engine.phase_runner import SimulationPhaseRunner
from src.sim.simulator_engine.phase_registry import SIMULATION_PHASES
# Run only the first 5 phases
runner = SimulationPhaseRunner(sim, phases=SIMULATION_PHASES[:5])
events = await runner.run()