Implementation:4thfever Cultivation world simulator save game
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Persistence, Save_Load |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Concrete tool for saving game state to a JSON file with section-based serialization and SQLite event database copy, provided by the Cultivation World Simulator save system.
Description
The save_game function is the top-level save entry point. It:
- Resolves the save file path (custom name or timestamp-based)
- Copies the SQLite events database to the save location
- Creates a `SaveContext` with world, simulator, sects, and paths
- Calls `dump_save_data(context)` to iterate save sections and build the JSON dict
- Writes the JSON to the resolved path
Supporting functions: sanitize_save_name (strips unsafe characters, limits to 50 chars), get_save_info (reads meta section), list_saves (lists all saves sorted by time descending).
Usage
Import and call this function when:
- Manual save: called by `save_current_game` in the control service when the user requests a save
- Auto-save: called with `is_auto_save=True` by the auto-save timer
Code Reference
Source Location
- Repository: cultivation-world-simulator
- File: src/sim/save/save_game.py
Signature
def save_game(
world: "World",
simulator: "Simulator",
existed_sects: List["Sect"],
save_path: Optional[Path] = None,
custom_name: Optional[str] = None,
is_auto_save: bool = False,
) -> tuple[bool, Optional[str]]:
"""Save game state to JSON + SQLite events DB."""
def sanitize_save_name(name: str) -> str:
"""Strip unsafe characters, keep alphanumeric+CJK, max 50 chars."""
def get_save_info(save_path: Path) -> Optional[dict]:
"""Read the meta section from a save file."""
def list_saves(saves_dir: Optional[Path] = None) -> List[tuple[Path, dict]]:
"""List all save files with metadata, sorted by save_time descending."""
Import
from src.sim.save.save_game import save_game, get_save_info, list_saves, sanitize_save_name
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| world | World | Yes | World instance to save |
| simulator | Simulator | Yes | Simulator instance to save |
| existed_sects | list[Sect] | Yes | Active sects for this playthrough |
| save_path | None | No | Explicit path (default: auto-generated) |
| custom_name | None | No | Custom name for filename |
| is_auto_save | bool | No | Flag for auto-save (default False) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None] | (success, filename) — filename is the JSON file name |
Usage Examples
Save Game with Custom Name
from src.sim.save.save_game import save_game
success, filename = save_game(
world=world,
simulator=sim,
existed_sects=world.existed_sects,
custom_name="My Epic Playthrough",
)
if success:
print(f"Saved as: {filename}")
List Available Saves
from src.sim.save.save_game import list_saves
saves = list_saves()
for path, meta in saves:
print(f"{path.name}: {meta.get('game_time', '?')} | alive={meta.get('alive_count', 0)}")
Related Pages
Implements Principle
Required Environments
Uses Heuristics
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment