World: one city among many under cities, no gameplay change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rope 2026-11-04 15:02:19 +00:00
parent 7dc780352c
commit 11e4d98d49
3 changed files with 100 additions and 7 deletions

63
game.py
View file

@ -10,6 +10,7 @@ from __future__ import annotations
import math
import random
from collections.abc import MutableMapping
from datetime import date
import combat
@ -1683,11 +1684,57 @@ RAID_TRAITS = ["lightly armoured, heavy on archers", "mounted and fast, few bows
# --- world ---------------------------------------------------------------
WORLD_VERSION = 38
WORLD_VERSION = 39
HOME_CITY = "guildhall"
CITY_KEYS = ("events", "next_event_at", "next_minor_at", "next_hardship_at", "next_fortune_at", "caravan")
def new_world(now: float, speed: float = 1.0) -> dict:
return {
class World(MutableMapping):
"""The saved doc as seen from one city. `world["city"]` and CITY_KEYS are that city's own;
everything else is shared by the whole kingdom. `doc` is what gets saved."""
def __init__(self, doc: dict, here: str = HOME_CITY):
self.doc = doc
self.here = here
def at(self, here: str) -> World:
return World(self.doc, here)
def _holder(self, key: str) -> dict:
return self.doc["cities"][self.here] if key in CITY_KEYS else self.doc
def __getitem__(self, key: str):
return self.doc["cities"][self.here] if key == "city" else self._holder(key)[key]
def __setitem__(self, key: str, value) -> None:
if key == "city":
self.doc["cities"][self.here] = value
else:
self._holder(key)[key] = value
def __delitem__(self, key: str) -> None:
del self._holder(key)[key]
def __iter__(self):
city = self.doc["cities"][self.here]
yield "city"
yield from (key for key in CITY_KEYS if key in city)
yield from self.doc
def __len__(self) -> int:
return sum(1 for _key in self)
def _nested(doc: dict) -> World:
"""Move a flat doc's one city, and what belongs to it, under `cities`."""
city = doc.pop("city")
city.update({key: doc.pop(key) for key in CITY_KEYS if key in doc})
doc["cities"] = {HOME_CITY: city}
return World(doc)
def new_world(now: float, speed: float = 1.0) -> World:
return _nested({
"version": WORLD_VERSION,
"created": now,
"config": {"speed": speed, "clock_offset": 0},
@ -1722,11 +1769,13 @@ def new_world(now: float, speed: float = 1.0) -> dict:
"orders": [], "next_order_id": 1,
"parties": {}, "next_party_id": 1,
"caravan": None, "next_caravan_id": 1,
}
})
def migrate(world: dict, now: float | None = None) -> dict:
def migrate(world: dict, now: float | None = None) -> World:
"""Bring a saved world up to the current shape. Never loses anything."""
if "cities" in world and not isinstance(world, World):
world = World(world)
now = now or world["city"]["last_tick"]
if world.get("version", 1) < 2:
city = world["city"]
@ -1961,7 +2010,9 @@ def migrate(world: dict, now: float | None = None) -> dict:
# The crucible: a fifth fire, for the jeweller's silver and gold.
world["city"]["fires"].setdefault("crucible", new_fire(now))
world["version"] = 38
return world
if world["version"] < 39:
world["version"] = 39
return world if isinstance(world, World) else _nested(world)
def _planks_to_frames(world: dict) -> None: