Linear Dungeon Generator
Here's a Python script that generates a linear dungeon from a start room to a finish room. The dungeon will have a straightforward path with optional side rooms or obstacles.
import random
from enum import Enum
class Direction(Enum):
NORTH = (0, -1)
SOUTH = (0, 1)
EAST = (1, 0)
WEST = (-1, 0)
class RoomType(Enum):
START = "Start"
END = "End"
NORMAL = "Normal"
TREASURE = "Treasure"
TRAP = "Trap"
MONSTER = "Monster"
PUZZLE = "Puzzle"
class Room:
def __init__(self, x, y, room_type=RoomType.NORMAL):
self.x = x
self.y = y
self.room_type = room_type
self.connected_rooms = []
self.visited = False
self.description = self._generate_description()
def _generate_description(self):
descriptions = {
RoomType.START: "The entrance to the dungeon. Torches flicker on the walls.",
RoomType.END: "A grand chamber with a glowing artifact at its center.",
RoomType.NORMAL: [
"A plain stone room with damp walls.",
"A circular chamber with strange carvings.",
"A rectangular room with a low ceiling.",
"A cavernous space with stalactites hanging from above.",
"A room with a small pool of water in the center."
],
RoomType.TREASURE: [
"A glittering hoard of gold and jewels fills this room.",
"Ancient artifacts line the shelves of this chamber.",
"A chest sits in the center of the room, overflowing with treasure."
],
RoomType.TRAP: [
"The floor looks suspiciously clean...",
"Dart traps line the walls of this dangerous room.",
"A pit yawns open in the center of the floor."
],
RoomType.MONSTER: [
"Growls echo from the shadows of this chamber.",
"Bones litter the floor of this monster's lair.",
"A foul stench fills this room - something lives here."
],
RoomType.PUZZLE: [
"Strange symbols cover the walls of this puzzle room.",
"A locked door stands at the far end, with no obvious key.",
"Mechanical devices whir quietly in this chamber."
]
}
if self.room_type in [RoomType.START, RoomType.END]:
return descriptions[self.room_type]
if self.room_type == RoomType.NORMAL:
return random.choice(descriptions[RoomType.NORMAL])
else:
return random.choice(descriptions[self.room_type])
def connect(self, other_room):
if other_room not in self.connected_rooms:
self.connected_rooms.append(other_room)
if self not in other_room.connected_rooms:
other_room.connected_rooms.append(self)
def __str__(self):
return f"Room at ({self.x}, {self.y}) - {self.room_type.value}: {self.description}"
class DungeonGenerator:
def __init__(self, length=10, width=5, height=5, branch_chance=0.2):
self.length = length # Minimum number of rooms in the main path
self.width = width
self.height = height
self.branch_chance = branch_chance
self.rooms = []
self.start_room = None
self.end_room = None
def generate(self):
# Create start room
start_x, start_y = random.randint(0, self.width-1), random.randint(0, self.height-1)
self.start_room = Room(start_x, start_y, RoomType.START)
self.rooms.append(self.start_room)
current_room = self.start_room
previous_direction = None
# Generate main path
for i in range(self.length):
# Determine next direction (try to avoid going back)
possible_directions = [d for d in Direction if d != previous_direction]
# Try to find a valid position
next_room = None
attempts = 0
max_attempts = 10
while attempts < max_attempts:
direction = random.choice(possible_directions)
dx, dy = direction.value
next_x, next_y = current_room.x + dx, current_room.y + dy
# Check if position is within bounds
if 0 <= next_x < self.width and 0 <= next_y < self.height:
# Check if position is already occupied
if not any(room.x == next_x and room.y == next_y for room in self.rooms):
# Determine room type
if i == self.length - 1:
room_type = RoomType.END
else:
# Random chance for special rooms
roll = random.random()
if roll < 0.1:
room_type = RoomType.TREASURE
elif roll < 0.2:
room_type = RoomType.TRAP
elif roll < 0.3:
room_type = RoomType.MONSTER
elif roll < 0.4:
room_type = RoomType.PUZZLE
else:
room_type = RoomType.NORMAL
next_room = Room(next_x, next_y, room_type)
self.rooms.append(next_room)
break
attempts += 1
if next_room is None:
# Couldn't find a valid position, try again with any direction
direction = random.choice(list(Direction))
dx, dy = direction.value
next_x, next_y = current_room.x + dx, current_room.y + dy
# Determine room type
if i == self.length - 1:
room_type = RoomType.END
else:
room_type = RoomType.NORMAL
next_room = Room(next_x, next_y, room_type)
self.rooms.append(next_room)
# Connect rooms
current_room.connect(next_room)
previous_direction = self._get_opposite_direction(direction)
current_room = next_room
# Random chance to add a branch
if random.random() < self.branch_chance and i < self.length - 1:
self._add_branch(current_room)
self.end_room = current_room
return self.rooms
def _add_branch(self, from_room):
# Try to add a side room or short branch
possible_directions = [d for d in Direction if not any(
(from_room.x + d.value[0] == room.x and from_room.y + d.value[1] == room.y)
for room in from_room.connected_rooms
)]
if not possible_directions:
return
direction = random.choice(possible_directions)
dx, dy = direction.value
branch_x, branch_y = from_room.x + dx, from_room.y + dy
# Check if position is within bounds and not occupied
if (0 <= branch_x < self.width and 0 <= branch_y < self.height and
not any(room.x == branch_x and room.y == branch_y for room in self.rooms)):
# Determine branch length (1-3 rooms)
branch_length = random.randint(1, 3)
for i in range(branch_length):
# Determine room type
if i == branch_length - 1:
# End of branch - more likely to be special
roll = random.random()
if roll < 0.2:
room_type = RoomType.TREASURE
elif roll < 0.4:
room_type = RoomType.TRAP
elif roll < 0.6:
room_type = RoomType.MONSTER
elif roll < 0.8:
room_type = RoomType.PUZZLE
else:
room_type = RoomType.NORMAL
else:
room_type = RoomType.NORMAL
branch_room = Room(branch_x, branch_y, room_type)
self.rooms.append(branch_room)
# Connect to previous room in branch
if i == 0:
from_room.connect(branch_room)
else:
# Find the previous room in the branch
prev_x, prev_y = branch_x - dx, branch_y - dy
prev_room = next(room for room in self.rooms
if room.x == prev_x and room.y == prev_y)
prev_room.connect(branch_room)
# Move to next position in branch
branch_x += dx
branch_y += dy
# Check if we're still in bounds
if not (0 <= branch_x < self.width and 0 <= branch_y < self.height):
break
def _get_opposite_direction(self, direction):
opposite = {
Direction.NORTH: Direction.SOUTH,
Direction.SOUTH: Direction.NORTH,
Direction.EAST: Direction.WEST,
Direction.WEST: Direction.EAST
}
return opposite[direction]
def print_dungeon(self):
# Create a grid representation
grid = [[' ' for _ in range(self.width)] for _ in range(self.height)]
# Mark rooms on the grid
for room in self.rooms:
if room.room_type == RoomType.START:
grid[room.y][room.x] = 'S'
elif room.room_type == RoomType.END:
grid[room.y][room.x] = 'E'
elif room.room_type == RoomType.TREASURE:
grid[room.y][room.x] = 'T'
elif room.room_type == RoomType.TRAP:
grid[room.y][room.x] = 'X'
elif room.room_type == RoomType.MONSTER:
grid[room.y][room.x] = 'M'
elif room.room_type == RoomType.PUZZLE:
grid[room.y][room.x] = 'P'
else:
grid[room.y][room.x] = 'R'
# Print the grid
print("Dungeon Map:")
print("S = Start, E = End, T = Treasure, X = Trap, M = Monster, P = Puzzle, R = Normal Room")
print("+" + "-" * self.width + "+")
for row in grid:
print("|" + "".join(row) + "|")
print("+" + "-" * self.width + "+")
# Print room details
print("\nRoom Details:")
for i, room in enumerate(self.rooms, 1):
print(f"\nRoom {i}:")
print(room)
print(f"Connected to rooms: {[f'({r.x}, {r.y})' for r in room.connected_rooms]}")
# Example usage
if __name__ == "__main__":
dungeon = DungeonGenerator(length=8, width=10, height=10, branch_chance=0.3)
rooms = dungeon.generate()
dungeon.print_dungeon()
How This Dungeon Generator Works:
-
Room Types: The generator creates different types of rooms:
- Start room (entrance)
- End room (goal)
- Normal rooms (basic passageways)
- Special rooms (treasure, traps, monsters, puzzles)
-
Linear Path: The generator creates a main path from start to finish with the specified length.
-
Branches: With a configurable chance, it adds side branches to make the dungeon more interesting.
-
Grid System: Rooms are placed on a grid with configurable width and height.
-
Visualization: The generator can print a simple map of the dungeon layout.
Customization Options:
length: Minimum number of rooms in the main path
width and height: Dimensions of the dungeon grid
branch_chance: Probability of adding side branches (0.0 to 1.0)
Example Output:
The script will generate a dungeon map showing room types and their connections, along with descriptions for each room.
You can modify the parameters to create different sized dungeons or adjust the probability of special rooms appearing.