"""JSON save/load for a GameSession. Serialization is lossless and invertible: dict_to_session(session_to_dict(s)) rebuilds a session whose session_to_dict is byte-identical, which is what the round-trip gate asserts. Tuples (colors, positions, stairs) are normalized to JSON lists on the way out and back to tuples on the way in, so sess and its loaded copy serialize identically. The per-turn visible grid is NEVER serialized -- it is recomputed from FOV on load. """ from __future__ import annotations import json from engine.game_state import GameSession from engine.map_gen import GameMap, Rect from entities.enemy import Enemy from entities.item import Item from entities.player import Player def _item_to_dict(it: Item) -> dict: return { "type": "item", "x": it.x, "y": it.y, "name": it.name, "color": list(it.color), "kind": it.kind, "sub": it.sub, "power": it.power, "armor": it.armor, "evasion_delta": it.evasion_delta, "crit_delta": it.crit_delta, "heal": it.heal, "duration": it.duration, "radius": it.radius, "damage": it.damage, "value": it.value, } def _enemy_to_dict(e: Enemy) -> dict: return { "type": "enemy", "x": e.x, "y": e.y, "name": e.name, "color": list(e.color), "hp": e.hp, "max_hp": e.max_hp, "power": e.power, "armor": e.armor, "accuracy": e.accuracy, "evasion": e.evasion, "crit_chance": e.crit_chance, "speed": e.speed, "behavior": e.behavior, "xp_value": e.xp_value, "ranged_power": e.ranged_power, "last_known_pos": list(e.last_known_pos) if e.last_known_pos is not None else None, "act_counter": e.act_counter, "can_split": e.can_split, } def _entity_to_dict(e: object) -> dict | None: if isinstance(e, Enemy): return _enemy_to_dict(e) if isinstance(e, Item): return _item_to_dict(e) return None def _item_from_dict(d: dict) -> Item: fields = dict(d) fields.pop("type", None) fields["color"] = tuple(fields["color"]) return Item(**fields) def _enemy_from_dict(d: dict) -> Enemy: fields = dict(d) fields.pop("type", None) fields["color"] = tuple(fields["color"]) lkp = fields["last_known_pos"] fields["last_known_pos"] = tuple(lkp) if lkp is not None else None return Enemy(**fields) def _entity_from_dict(d: dict) -> object: if d["type"] == "enemy": return _enemy_from_dict(d) return _item_from_dict(d) def _player_to_dict(p: Player) -> dict: return { "name": p.name, "x": p.x, "y": p.y, "hp": p.hp, "max_hp": p.max_hp, "power": p.power, "armor": p.armor, "accuracy": p.accuracy, "evasion": p.evasion, "crit_chance": p.crit_chance, "speed": p.speed, "level": p.level, "xp": p.xp, "timers": dict(p.timers), "inventory": [_item_to_dict(i) for i in p.inventory], "weapon": _item_to_dict(p.weapon) if p.weapon is not None else None, "armor_item": _item_to_dict(p.armor_item) if p.armor_item is not None else None, } def _player_from_dict(d: dict) -> Player: return Player( name=d["name"], x=d["x"], y=d["y"], hp=d["hp"], max_hp=d["max_hp"], power=d["power"], armor=d["armor"], accuracy=d["accuracy"], evasion=d["evasion"], crit_chance=d["crit_chance"], speed=d["speed"], level=d["level"], xp=d["xp"], timers=dict(d["timers"]), inventory=[_item_from_dict(i) for i in d["inventory"]], weapon=_entity_from_dict(d["weapon"]) if d["weapon"] is not None else None, armor_item=_entity_from_dict(d["armor_item"]) if d["armor_item"] is not None else None, ) def _map_to_dict(gmap: GameMap) -> dict: return { "width": gmap.width, "height": gmap.height, "tiles": gmap.tiles, "rooms": [[r.x, r.y, r.w, r.h] for r in gmap.rooms], "explored": gmap.explored, "entities": [d for d in (_entity_to_dict(e) for e in gmap.entities) if d is not None], "stairs_down": list(gmap.stairs_down) if gmap.stairs_down is not None else None, "floor_index": gmap.floor_index, } def _map_from_dict(d: dict) -> GameMap: rooms = [Rect(r[0], r[1], r[2], r[3]) for r in d["rooms"]] entities = [_entity_from_dict(e) for e in d["entities"]] width, height = d["width"], d["height"] visible = [[False] * width for _ in range(height)] stairs = tuple(d["stairs_down"]) if d["stairs_down"] is not None else None return GameMap( width=width, height=height, tiles=d["tiles"], rooms=rooms, explored=d["explored"], visible=visible, entities=entities, stairs_down=stairs, floor_index=d["floor_index"], ) def session_to_dict(sess: GameSession) -> dict: return { "player": _player_to_dict(sess.player), "dungeon": [_map_to_dict(m) for m in sess.dungeon], "current_floor": sess.current_floor, "log": list(sess.log), "turn": sess.turn, "state": sess.state, } def dict_to_session(d: dict) -> GameSession: return GameSession( player=_player_from_dict(d["player"]), dungeon=[_map_from_dict(m) for m in d["dungeon"]], current_floor=d["current_floor"], log=list(d["log"]), turn=d["turn"], state=d["state"], ) def save_session(sess: GameSession, path: str) -> None: with open(path, "w", encoding="utf-8") as f: json.dump(session_to_dict(sess), f) def load_session(path: str) -> GameSession: with open(path, "r", encoding="utf-8") as f: return dict_to_session(json.load(f))