140 lines
4.8 KiB
Python
140 lines
4.8 KiB
Python
"""GameSession: the single source of truth for all mutable state.
|
|
|
|
``new_session`` generates all 10 floors AND populates them with enemies/items
|
|
(keep generation in map_gen, content spawning here). Floor 9 always carries the
|
|
Ancient Dragon so the boss is killable/victory-triggerable on a real playthrough.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import random
|
|
from dataclasses import dataclass, field
|
|
from typing import Any
|
|
|
|
from entities.player import Player
|
|
from engine import map_gen
|
|
from data import enemies as ENEMIES
|
|
from data import items as ITEMS
|
|
from data.constants import BOSS_ROOM_W, BOSS_ROOM_H, MAP_W, MAP_H
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class GameSession:
|
|
player: Any = None
|
|
dungeon: list = field(default_factory=list)
|
|
current_floor: int = 0
|
|
log: list = field(default_factory=list)
|
|
turn: int = 0
|
|
state: str = "playing"
|
|
|
|
|
|
# Enemy roster gated by depth (plan.md Enemy Roster "Floors" column).
|
|
_FLOOR_ENEMIES: dict[int, list[str]] = {
|
|
0: ["rat", "goblin"],
|
|
1: ["rat", "goblin"],
|
|
2: ["rat", "goblin", "skeleton_archer"],
|
|
3: ["rat", "goblin", "skeleton_archer", "orc_warrior"],
|
|
4: ["goblin", "skeleton_archer", "orc_warrior", "green_slime"],
|
|
5: ["goblin", "skeleton_archer", "orc_warrior", "wraith", "green_slime"],
|
|
6: ["skeleton_archer", "orc_warrior", "wraith", "green_slime"],
|
|
7: ["skeleton_archer", "orc_warrior", "wraith", "green_slime"],
|
|
8: ["orc_warrior", "wraith", "green_slime", "skeleton_archer"],
|
|
}
|
|
|
|
# (min_enemies, max_enemies, min_items, max_items) per depth band (plan.md table).
|
|
_FLOOR_COUNTS: dict[int, tuple[int, int, int, int]] = {
|
|
0: (3, 4, 2, 3), 1: (3, 4, 2, 3),
|
|
2: (4, 6, 2, 3), 3: (4, 6, 2, 3),
|
|
4: (5, 7, 3, 4), 5: (5, 7, 3, 4),
|
|
6: (6, 8, 3, 4), 7: (6, 8, 3, 4),
|
|
8: (7, 9, 4, 5),
|
|
}
|
|
|
|
# Item pool split by depth (better weapons/armor appear deeper; consumables uniform).
|
|
_DEEP_WEAPONS = ["battle_axe", "war_hammer"]
|
|
_MID_WEAPONS = ["short_sword", "battle_axe"]
|
|
_SHALLOW_WEAPONS = ["rusty_dagger", "short_sword"]
|
|
_DEEP_ARMOR = ["chain_mail", "plate_armor"]
|
|
_SHALLOW_ARMOR = ["leather_armor", "chain_mail"]
|
|
_CONSUMABLES = ["healing_potion", "strength_potion", "invisibility_potion",
|
|
"fireball_scroll", "teleport_scroll", "mapping_scroll"]
|
|
|
|
|
|
def _walkable_tiles(gm: Any) -> list[tuple[int, int]]:
|
|
return [(x, y) for y in range(gm.height) for x in range(gm.width)
|
|
if gm.walkable(x, y)]
|
|
|
|
|
|
def _populate_standard(gm: Any, rng: random.Random) -> None:
|
|
pool = _FLOOR_ENEMIES.get(gm.floor_index, ["goblin"])
|
|
lo_e, hi_e, lo_i, hi_i = _FLOOR_COUNTS.get(gm.floor_index, (3, 4, 2, 3))
|
|
spawn = gm.spawn
|
|
tiles = [t for t in _walkable_tiles(gm)
|
|
if spawn is None or max(abs(t[0] - spawn[0]), abs(t[1] - spawn[1])) >= 4]
|
|
rng.shuffle(tiles)
|
|
|
|
def take() -> tuple[int, int] | None:
|
|
return tiles.pop() if tiles else None
|
|
|
|
for _ in range(rng.randint(lo_e, hi_e)):
|
|
pos = take()
|
|
if pos is None:
|
|
break
|
|
kind = rng.choice(pool)
|
|
gm.entities.append(ENEMIES.make_enemy(kind, pos[0], pos[1]))
|
|
|
|
fi = gm.floor_index
|
|
if fi <= 1:
|
|
weapons, armors = _SHALLOW_WEAPONS, _SHALLOW_ARMOR
|
|
elif fi <= 4:
|
|
weapons, armors = _MID_WEAPONS, _SHALLOW_ARMOR
|
|
else:
|
|
weapons, armors = _DEEP_WEAPONS, _DEEP_ARMOR
|
|
for _ in range(rng.randint(lo_i, hi_i)):
|
|
pos = take()
|
|
if pos is None:
|
|
break
|
|
roll = rng.random()
|
|
if roll < 0.35:
|
|
kind = rng.choice(weapons)
|
|
elif roll < 0.55:
|
|
kind = rng.choice(armors)
|
|
else:
|
|
kind = rng.choice(_CONSUMABLES)
|
|
gm.entities.append(ITEMS.make_item(kind, pos[0], pos[1]))
|
|
|
|
|
|
def _populate_boss(gm: Any) -> None:
|
|
# Boss arena: dragon on the far side of the room, two potions near the entrance.
|
|
rx = (MAP_W - BOSS_ROOM_W) // 2
|
|
ry = (MAP_H - BOSS_ROOM_H) // 2
|
|
mid_y = ry + BOSS_ROOM_H // 2
|
|
dragon = ENEMIES.make_enemy("ancient_dragon", rx + BOSS_ROOM_W - 2, mid_y)
|
|
gm.entities.append(dragon)
|
|
gm.entities.append(ITEMS.make_item("healing_potion", rx + 2, mid_y))
|
|
gm.entities.append(ITEMS.make_item("healing_potion", rx + 2, mid_y + 1))
|
|
|
|
|
|
def populate_floor(gm: Any, rng: random.Random) -> None:
|
|
"""Fill a generated floor's entity list with enemies + items (in place)."""
|
|
if gm.floor_index == 9:
|
|
_populate_boss(gm)
|
|
else:
|
|
_populate_standard(gm, rng)
|
|
|
|
|
|
def new_session(seed: int = 0) -> GameSession:
|
|
rng = random.Random(seed)
|
|
dungeon = map_gen.generate_dungeon(seed=seed)
|
|
for gm in dungeon:
|
|
populate_floor(gm, rng)
|
|
spawn = dungeon[0].spawn or (1, 1)
|
|
player = Player(x=spawn[0], y=spawn[1])
|
|
return GameSession(
|
|
player=player,
|
|
dungeon=dungeon,
|
|
current_floor=0,
|
|
log=["You descend into the Underdepths."],
|
|
turn=0,
|
|
state="playing",
|
|
)
|