(Underdepths) Pass 4
This commit is contained in:
1 parent
a219ad17d5
commit
6147ab3121
29 files changed
+4888
No files matched your search
@@ -0,0 +1,224 @@
|
||||
"""JSON save/load: exact round-trip serialization of a GameSession.
|
||||
|
||||
The `visible` grids are intentionally NOT serialized — they are recomputed via
|
||||
`recompute_fov` on load. Every other field of Player / Enemy / Item is preserved
|
||||
verbatim, including per-instance enemy state (damaged HP, last-known position,
|
||||
small-slime `can_split=False`, act counters) so a loaded dive matches the saved
|
||||
dive exactly. Entities are reconstructed directly from their serialized fields
|
||||
rather than via `make_enemy`/`make_item`, which would reset mutable state to the
|
||||
roster/catalog defaults.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from engine.game_state import GameSession, recompute_fov
|
||||
from engine.map_gen import GameMap, Rect
|
||||
from entities.enemy import Enemy
|
||||
from entities.entity import Entity
|
||||
from entities.item import Item
|
||||
from entities.player import Effect, Player
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Top level
|
||||
# --------------------------------------------------------------------------- #
|
||||
def session_to_dict(session: GameSession) -> dict:
|
||||
return {
|
||||
"player": _player_to_dict(session.player),
|
||||
"dungeon": [_map_to_dict(m) for m in session.dungeon],
|
||||
"current_floor": session.current_floor,
|
||||
"turn": session.turn,
|
||||
"log": list(session.log),
|
||||
"state": session.state,
|
||||
}
|
||||
|
||||
|
||||
def dict_to_session(d: dict) -> GameSession:
|
||||
session = GameSession(
|
||||
player=_dict_to_player(d["player"]),
|
||||
dungeon=[_dict_to_map(m) for m in d["dungeon"]],
|
||||
current_floor=d["current_floor"],
|
||||
log=list(d["log"]),
|
||||
turn=d["turn"],
|
||||
state=d["state"],
|
||||
)
|
||||
recompute_fov(session)
|
||||
return session
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Player
|
||||
# --------------------------------------------------------------------------- #
|
||||
def _player_to_dict(p: Player) -> dict:
|
||||
return {
|
||||
"type": "player",
|
||||
"x": p.x, "y": p.y,
|
||||
"name": p.name,
|
||||
"sprite_key": p.sprite_key,
|
||||
"blocks": p.blocks,
|
||||
"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,
|
||||
"inventory": [_item_to_dict(i) for i in p.inventory],
|
||||
"equipped": {
|
||||
slot: (_item_to_dict(it) if it is not None else None)
|
||||
for slot, it in p.equipped.items()
|
||||
},
|
||||
"effects": [_effect_to_dict(e) for e in p.effects],
|
||||
"facing": p.facing,
|
||||
"invisible": p.invisible,
|
||||
"pending_log": list(p.pending_log),
|
||||
}
|
||||
|
||||
|
||||
def _dict_to_player(d: dict) -> Player:
|
||||
p = Player(d["x"], d["y"])
|
||||
p.name = d["name"]
|
||||
p.sprite_key = d["sprite_key"]
|
||||
p.blocks = d["blocks"]
|
||||
p.hp = d["hp"]
|
||||
p.max_hp = d["max_hp"]
|
||||
p.power = d["power"]
|
||||
p.armor = d["armor"]
|
||||
p.accuracy = d["accuracy"]
|
||||
p.evasion = d["evasion"]
|
||||
p.crit_chance = d["crit_chance"]
|
||||
p.speed = d["speed"]
|
||||
p.level = d["level"]
|
||||
p.xp = d["xp"]
|
||||
p.inventory = [_dict_to_item(i) for i in d["inventory"]]
|
||||
p.equipped = {
|
||||
slot: (_dict_to_item(it) if it is not None else None)
|
||||
for slot, it in d["equipped"].items()
|
||||
}
|
||||
p.effects = [_dict_to_effect(e) for e in d["effects"]]
|
||||
p.facing = d["facing"]
|
||||
p.invisible = d["invisible"]
|
||||
p.pending_log = list(d["pending_log"])
|
||||
return p
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Effects
|
||||
# --------------------------------------------------------------------------- #
|
||||
def _effect_to_dict(e: Effect) -> dict:
|
||||
return {"kind": e.kind, "power": e.power, "duration": e.duration}
|
||||
|
||||
|
||||
def _dict_to_effect(d: dict) -> Effect:
|
||||
return Effect(kind=d["kind"], power=d["power"], duration=d["duration"])
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Floor entities (enemies + items) — discriminated by "type"
|
||||
# --------------------------------------------------------------------------- #
|
||||
def _entity_to_dict(e) -> dict:
|
||||
if isinstance(e, Enemy):
|
||||
return _enemy_to_dict(e)
|
||||
if isinstance(e, Item):
|
||||
return _item_to_dict(e)
|
||||
return {"type": "entity", "x": e.x, "y": e.y,
|
||||
"name": getattr(e, "name", ""), "sprite_key": getattr(e, "sprite_key", ""),
|
||||
"blocks": getattr(e, "blocks", True)}
|
||||
|
||||
|
||||
def _dict_to_entity(d):
|
||||
t = d.get("type")
|
||||
if t == "enemy":
|
||||
return _dict_to_enemy(d)
|
||||
if t == "item":
|
||||
return _dict_to_item(d)
|
||||
return Entity(d["x"], d["y"], d.get("name", ""),
|
||||
d.get("sprite_key", ""), d.get("blocks", True))
|
||||
|
||||
|
||||
def _enemy_to_dict(e: Enemy) -> dict:
|
||||
return {
|
||||
"type": "enemy",
|
||||
"x": e.x, "y": e.y,
|
||||
"name": e.name,
|
||||
"sprite_key": e.sprite_key,
|
||||
"blocks": e.blocks,
|
||||
"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,
|
||||
"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,
|
||||
"ranged_power": e.ranged_power,
|
||||
}
|
||||
|
||||
|
||||
def _dict_to_enemy(d: dict) -> Enemy:
|
||||
lkp = d["last_known_pos"]
|
||||
return Enemy(
|
||||
x=d["x"], y=d["y"], name=d["name"], sprite_key=d["sprite_key"],
|
||||
blocks=d["blocks"],
|
||||
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"],
|
||||
behavior=d["behavior"], xp_value=d["xp_value"],
|
||||
last_known_pos=(tuple(lkp) if lkp is not None else None),
|
||||
act_counter=d["act_counter"], can_split=d["can_split"],
|
||||
ranged_power=d["ranged_power"],
|
||||
)
|
||||
|
||||
|
||||
def _item_to_dict(it: Item) -> dict:
|
||||
return {
|
||||
"type": "item",
|
||||
"x": it.x, "y": it.y,
|
||||
"name": it.name,
|
||||
"sprite_key": it.sprite_key,
|
||||
"blocks": it.blocks,
|
||||
"item_type": it.item_type,
|
||||
"subtype": it.subtype,
|
||||
"stats": dict(it.stats),
|
||||
}
|
||||
|
||||
|
||||
def _dict_to_item(d: dict) -> Item:
|
||||
return Item(
|
||||
x=d["x"], y=d["y"], name=d["name"], sprite_key=d["sprite_key"],
|
||||
blocks=d["blocks"], item_type=d["item_type"],
|
||||
subtype=d["subtype"], stats=dict(d["stats"]),
|
||||
)
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Map
|
||||
# --------------------------------------------------------------------------- #
|
||||
def _rect_to_dict(r: Rect) -> dict:
|
||||
return {"x": r.x, "y": r.y, "w": r.w, "h": r.h}
|
||||
|
||||
|
||||
def _map_to_dict(m: GameMap) -> dict:
|
||||
return {
|
||||
"width": m.width,
|
||||
"height": m.height,
|
||||
"tiles": [list(row) for row in m.tiles],
|
||||
"rooms": [_rect_to_dict(r) for r in m.rooms],
|
||||
"explored": [list(row) for row in m.explored],
|
||||
"entities": [_entity_to_dict(e) for e in m.entities],
|
||||
"stairs_down": list(m.stairs_down),
|
||||
"floor_index": m.floor_index,
|
||||
}
|
||||
|
||||
|
||||
def _dict_to_map(d: dict) -> GameMap:
|
||||
width = d["width"]
|
||||
height = d["height"]
|
||||
return GameMap(
|
||||
width=width, height=height,
|
||||
tiles=[list(row) for row in d["tiles"]],
|
||||
rooms=[Rect(r["x"], r["y"], r["w"], r["h"]) for r in d["rooms"]],
|
||||
explored=[list(row) for row in d["explored"]],
|
||||
visible=[[False] * width for _ in range(height)],
|
||||
entities=[_dict_to_entity(e) for e in d["entities"]],
|
||||
stairs_down=tuple(d["stairs_down"]),
|
||||
floor_index=d["floor_index"],
|
||||
)
|
||||
Reference in new issue
Block a user