-
-
Save twobob/cd2e1a9e2c822ea2aca549d5ae0ebf86 to your computer and use it in GitHub Desktop.
Simple dungeon maker. Guaranteed connectedness.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
@author AlexHuleatt | |
Generate a simple, connected dungeon. Focus is on rooms, not maze-like features. | |
Output of get_dungeon is two sets: | |
1. A set of (y,x) tuples representing the position of walls | |
2. A set of (y,x) tuples representing the walls removed to make doors | |
Guaranteed connectedness between all open cells. | |
''' | |
import random | |
def random_rooms(height, width): | |
rooms = [] | |
def helper(rooms, lowx, lowy, highx, highy): | |
if highx-lowx <= 3 or highy - lowy <= 3: #space too small to fit room | |
return | |
width,height = random.randint(3, highx-lowx-1), random.randint(3, highy-lowy-1) #minimum width and height is 3 so we have empty space in the middle | |
px, py = random.randint(lowx, highx-width-1), random.randint(lowy, highy-height-1) #upper left coordinate | |
rooms.append( ((py, px),(py+height, px+width)) ) | |
helper(rooms, lowx, lowy, highx, py-2) #above | |
helper(rooms, lowx, py, px-2, highy) #left | |
helper(rooms, px+width+2, py, highx, highy) #right | |
helper(rooms, px, py+height+2, px+width, highy) #below | |
helper(rooms, px + 2, py + 2, px+width - 2, py+height - 2) #inside | |
helper(rooms, 2, 2, width-2, height-2) | |
return rooms | |
def get_dungeon(height, width, doors_per_room=1): | |
''' Produces the actual dungeon, as a set of walls and a set of doors in the walls.''' | |
rooms = random_rooms(height, width) | |
walls = set() | |
doors = set() | |
for r in rooms: | |
upper_left, lower_right = r | |
ul_y, ul_x = upper_left | |
lr_y, lr_x = lower_right | |
room_walls = set() | |
for i in range(ul_y, lr_y+1): | |
room_walls.add((i, ul_x)) | |
room_walls.add((i, lr_x)) | |
for i in range(ul_x, lr_x+1): | |
room_walls.add((ul_y, i)) | |
room_walls.add((lr_y, i)) | |
for i in range(doors_per_room): | |
w = random.sample(room_walls, 1)[0] | |
while (w[0] in [ul_y, lr_y] and w[1] in [ul_x, lr_x]): #avoid removing corners | |
w = random.sample(room_walls, 1)[0] | |
room_walls.remove(w) | |
doors.add(w) | |
walls.update(room_walls) | |
return walls, doors | |
k = 45 | |
walls, doors = get_dungeon(k, k) | |
for i in range(k): | |
for j in range(k): | |
if (i,j) in walls: | |
print '.', | |
else: | |
print ' ', | |
#example output | |
''' | |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |
. . | |
. . | |
. . | |
. . | |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |
. . . . | |
. . . . . . . . . . . . . . . . . . . . . . | |
. . . . . . . . | |
. . . . . . . | |
. . . . . . . . . . . . . . . . . . . | |
. . . . . . . . . . | |
. . . . | |
. . . . | |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |
. . | |
. | |
. . | |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | |
. . . . . . . . . . . . . . | |
. . | |
. . . . . . . . | |
. . . . | |
. . . . . . . . . . . . . . | |
. . . . . . . . . | |
. . . . | |
. . . . | |
. . . . . . . . . . . . . . | |
. . . . . . | |
. . . . . . . . | |
. . . . . . . . . . . . . . . . . . | |
. . . . . . . | |
. . | |
. . . . . . . . . . | |
. . . . | |
. . . . . . . . . . . . . . . | |
. . . . . . . | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment