155 lines
4.6 KiB
Python
155 lines
4.6 KiB
Python
"""GameSession: single source of truth for all mutable state.
|
|
|
|
Owns the player, all 10 dungeon floors, the message log, turn counter, and the
|
|
high-level state tag. new_game() generates a fresh dungeon and populates it per
|
|
plan.md's per-floor tables; recompute_fov() refreshes the current floor's
|
|
visibility from the player's position.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
import random
|
|
from dataclasses import dataclass
|
|
|
|
from data.constants import FLOORS, BOSS_FLOOR, SIGHT_RADIUS
|
|
from engine.fov import compute_fov
|
|
from engine.map_gen import GameMap, generate_dungeon
|
|
from entities.player import Player
|
|
from data.enemies import enemies_for_floor
|
|
from entities.enemy import make_enemy
|
|
from entities.item import make_item
|
|
from data.items import roll_item
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class GameSession:
|
|
player: Player
|
|
dungeon: list[GameMap]
|
|
current_floor: int
|
|
log: list[str]
|
|
turn: int = 0
|
|
state: str = "playing"
|
|
|
|
@property
|
|
def current_map(self) -> GameMap:
|
|
return self.dungeon[self.current_floor]
|
|
|
|
def recompute_fov(self) -> None:
|
|
cmap = self.current_map
|
|
for y in range(cmap.height):
|
|
vrow = cmap.visible[y]
|
|
for x in range(cmap.width):
|
|
vrow[x] = False
|
|
seen = compute_fov(cmap, self.player.x, self.player.y, SIGHT_RADIUS)
|
|
for (x, y) in seen:
|
|
cmap.visible[y][x] = True
|
|
cmap.explored[y][x] = True
|
|
|
|
|
|
# --- per-floor spawn counts (plan.md spawning table) --------------------------
|
|
# (floor_lo, floor_hi, min, max) for enemies and items.
|
|
_ENEMY_TABLE: tuple[tuple[int, int, int, int], ...] = (
|
|
(0, 1, 3, 4), (2, 3, 4, 6), (4, 5, 5, 7), (6, 7, 6, 8), (8, 8, 7, 9),
|
|
)
|
|
_ITEM_TABLE: tuple[tuple[int, int, int, int], ...] = (
|
|
(0, 1, 2, 3), (2, 3, 2, 3), (4, 5, 3, 4), (6, 7, 3, 4), (8, 8, 4, 5),
|
|
)
|
|
|
|
|
|
def _count_for(floor_index: int, table: tuple[tuple[int, int, int, int], ...]) -> tuple[int, int]:
|
|
for lo, hi, mn, mx in table:
|
|
if lo <= floor_index <= hi:
|
|
return (mn, mx)
|
|
return (0, 0)
|
|
|
|
|
|
def _floor_tiles(gmap: GameMap) -> list[tuple[int, int]]:
|
|
return [
|
|
(x, y)
|
|
for y in range(gmap.height)
|
|
for x in range(gmap.width)
|
|
if gmap.tiles[y][x] == 1
|
|
]
|
|
|
|
|
|
def _populate_boss(gmap: GameMap) -> None:
|
|
room = gmap.rooms[0]
|
|
cx, cy = room.center()
|
|
dragon_x = room.x + room.w - 2
|
|
dragon_y = room.y + room.h - 2
|
|
if not (0 <= dragon_x < gmap.width and 0 <= dragon_y < gmap.height):
|
|
dragon_x, dragon_y = cx, cy
|
|
gmap.entities.append(make_enemy("Ancient Dragon", dragon_x, dragon_y))
|
|
placed = 0
|
|
for ox, oy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
|
|
if placed >= 2:
|
|
break
|
|
px, py = cx + ox, cy + oy
|
|
if 0 <= px < gmap.width and 0 <= py < gmap.height and gmap.tiles[py][px] == 1:
|
|
pot = make_item("Healing Potion")
|
|
pot.x, pot.y = px, py
|
|
gmap.entities.append(pot)
|
|
placed += 1
|
|
|
|
|
|
def _populate(gmap: GameMap, floor_index: int, rng: random.Random) -> None:
|
|
if floor_index == BOSS_FLOOR:
|
|
_populate_boss(gmap)
|
|
return
|
|
|
|
occupied: set[tuple[int, int]] = set()
|
|
if gmap.rooms:
|
|
occupied.add(gmap.rooms[0].center())
|
|
if gmap.stairs_down is not None:
|
|
occupied.add(gmap.stairs_down)
|
|
|
|
tiles = _floor_tiles(gmap)
|
|
rng.shuffle(tiles)
|
|
|
|
n_enemies = rng.randint(*_count_for(floor_index, _ENEMY_TABLE))
|
|
roster = enemies_for_floor(floor_index)
|
|
placed = 0
|
|
if roster:
|
|
for (x, y) in tiles:
|
|
if placed >= n_enemies:
|
|
break
|
|
if (x, y) in occupied:
|
|
continue
|
|
gmap.entities.append(make_enemy(rng.choice(roster), x, y))
|
|
occupied.add((x, y))
|
|
placed += 1
|
|
|
|
n_items = rng.randint(*_count_for(floor_index, _ITEM_TABLE))
|
|
placed = 0
|
|
for (x, y) in tiles:
|
|
if placed >= n_items:
|
|
break
|
|
if (x, y) in occupied:
|
|
continue
|
|
item = roll_item(floor_index, rng)
|
|
item.x, item.y = x, y
|
|
gmap.entities.append(item)
|
|
occupied.add((x, y))
|
|
placed += 1
|
|
|
|
|
|
def new_game(seed: int | None = None) -> GameSession:
|
|
rng = random.Random(seed)
|
|
dungeon = [generate_dungeon(fi, rng) for fi in range(FLOORS)]
|
|
for fi, gmap in enumerate(dungeon):
|
|
_populate(gmap, fi, rng)
|
|
|
|
spawn = dungeon[0].rooms[0].center()
|
|
player = Player(x=spawn[0], y=spawn[1], name="You")
|
|
|
|
sess = GameSession(
|
|
player=player,
|
|
dungeon=dungeon,
|
|
current_floor=0,
|
|
log=[],
|
|
turn=0,
|
|
state="playing",
|
|
)
|
|
sess.log.append("Welcome to the Underdepths. Slay the Ancient Dragon on floor 10!")
|
|
sess.recompute_fov()
|
|
return sess
|