-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdungeon.py
More file actions
263 lines (235 loc) · 8.39 KB
/
dungeon.py
File metadata and controls
263 lines (235 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import random
import _dungeon_helper
#DOORS = _dungeon_helper.DOORS
DUNGEONS = _dungeon_helper.DUNGEONS
MONSTERS = _dungeon_helper.MONSTERS
MONSTER_CLUSTERS = _dungeon_helper.MONSTER_CLUSTERS
ITEMS = _dungeon_helper.ITEMS
FOODS = _dungeon_helper.FOODS
MONUMENTS = _dungeon_helper.MONUMENTS
PLAYERS = _dungeon_helper.PLAYERS
PETS = _dungeon_helper.PETS
floor = _dungeon_helper.floor
wall = _dungeon_helper.wall
room_tries = 40
action_tries = 16
width, height = (13,10)
def gen_dungeon_border(rooms):
out = []
for y in range(height):
for x in range(width):
if y == 0 or y == height-1:
out.append((x,y))
elif x == 0 or x == width -1:
out.append((x,y))
out = set(out).difference(rooms)
return list(out)
def get_width_height(dungeon):
return (len(dungeon[0]), len(dungeon))
def get_random_x_y(dungeon, rooms):
return random.choice(list(rooms))
def draw(places, tile, dungeon):
for space in places:
x, y = space
dungeon[y][x] = tile
def gen_dungeon():
# TEMPLATE_DUNGEON = random.choice(DUNGEONS)
dungeon, empty_spaces = make_dungeon_outline()
rooms, room_list = gen_rooms(empty_spaces)
paths = gen_paths(room_list)
draw(list(rooms), _dungeon_helper.floor, dungeon)
draw(list(paths), _dungeon_helper.path, dungeon)
rooms = rooms.union(paths)
decorate_room(dungeon, rooms)
# dungeon = _dungeon_helper.test_dungeon
printable_dungeon = format_dungeon(dungeon)
return(printable_dungeon)
def gen_rooms(empty_spaces):
rooms = set()
borders = set()
room_list = []
for i in range(room_tries):
potential_room, border, empty_spaces = make_room(empty_spaces)
if rooms.isdisjoint(potential_room) and borders.isdisjoint(potential_room):
rooms = rooms.union(potential_room)
borders = borders.union(border)
room_list.append(list(potential_room))
return rooms, room_list
def gen_paths(room_list):
paths = set()
coords = sorted([[min(room), max(room)] for room in room_list])
for idx,room1 in enumerate(coords):
for room2 in coords[idx+1:]:
x_overlap = sorted(list(set(range(room1[0][0],room1[1][0]+1)).intersection(set(range(room2[0][0], room2[1][0]+1)))))
y_overlap = sorted(list(set(range(room1[0][1],room1[1][1]+1)).intersection(set(range(room2[0][1], room2[1][1]+1)))))
if len(x_overlap):
ys = sorted([room1[0][1], room1[1][1], room2[0][1], room2[1][1]])
y_difference = sorted(list(set(range(ys[1]+1, ys[2]))))
#y_difference = sorted(list(set(range(min([room1[1][1], room2[0][1]])+1,
# max([room1[1][1], room2[0][1]])))))
path = make_v_path(x_overlap, y_difference, room_list)
paths = paths.union(path)
elif len(y_overlap):
xs = sorted([room1[0][0], room1[1][0], room2[0][0], room2[1][0]])
x_difference = sorted(list(set(range(xs[1]+1, xs[2]))))
#x_difference = sorted(list(set(range(min([room1[1][0], room2[0][0]])+1,
# max([room1[1][0], room2[0][0]])))))
path = make_h_path(x_difference, y_overlap, room_list)
paths = paths.union(path)
continue
return paths
def expand_room_list(room_list):
out = set()
for room in room_list:
out = out.union(set(room))
return out
def make_v_path(x, y, room_list):
room_spaces = expand_room_list(room_list)
path = set()
if len(y) == 1:
path.add((random.choice(x), y[0]))
return path
x1 = random.choice(x)
y1 = y[0]
path.add((x1, y1))
y1 += 1
if (x1, y1) not in room_spaces:
path.add((x1, y1))
xendpoint = random.choice([x[0], x[-1]])
if xendpoint > x1:
direction = 1
else:
direction = -1
while y1 != y[-1] and (x1, y1+1) not in room_spaces:
if x1 != xendpoint and random.choice([0,1]):
x1 += direction
path.add((x1, y1))
else:
y1 += 1
path.add((x1, y1))
return path
def make_h_path(x, y, room_list):
room_spaces = expand_room_list(room_list)
path = set()
if len(x) == 1:
path.add((x[0], random.choice(y)))
return path
x1 = x[0]
y1 = random.choice(y)
path.add((x1, y1))
x1 += 1
if (x1, y1) not in room_spaces:
path.add((x1,y1))
yendpoint = random.choice([y[0], y[-1]])
if yendpoint > y1:
direction = 1
else:
direction = -1
while x1 != x[-1] and (x1+1, y1) not in room_spaces:
if y1 != yendpoint and random.choice([0,1]):
y1 += direction
path.add((x1, y1))
else:
x1 += 1
path.add((x1, y1))
return path
return path
def make_dungeon_outline():
empty_spaces = set()
for i in range(10):
for j in range(13):
empty_spaces.add((j,i))
return [[_dungeon_helper.wall]*13 for i in range(10)], empty_spaces
def make_room(empty_spaces):
startx = random.randint(0,11)
starty = random.randint(0,8)
x = random.randint(2,8)
y = random.randint(2,8)
room = set()
border = set()
for i in range(y):
for j in range(x):
if i == 0:
if 0 < (starty-1):
border.add((min([startx+j, width-1]), starty-1))
elif i == y-1:
if i+starty < height:
border.add((min([startx+j, width-1]), starty+i+1))
if j == 0:
if 0 < (startx-1):
border.add((startx-1, min([starty+i, height-1])))
elif j == x-1:
if j+startx < width:
border.add((startx+j+1, min([starty+i, height-1])))
room.add((min([startx+j, width-1]),min([starty+i,height-1])))
empty_spaces = empty_spaces.difference(room)
return room, border, empty_spaces
def decorate_room(dungeon, rooms):
x, y = add_player(dungeon, rooms)
for i in range(action_tries):
action = ACTIONS.pop(random.randint(0, len(ACTIONS)-1))
action(dungeon, rooms)
def add_player(dungeon, rooms):
player = random.choice(PLAYERS)
x, y = get_random_x_y(dungeon, rooms)
dungeon[y][x] = player
return (x, y)
def add_pet(dungeon, x, y):
x = x + random.randint(-1,1)
y = y + random.randint(-1,1)
if dungeon[y][x] == floor:
dungeon[y][x] = random.choice(PETS)
#def add_door(dungeon):
# door = random.choice(DOORS)
# width, height = get_width_height(dungeon)
# pos_doors = (width*2) + ((height-2)*2)
# placement = random.randint(0, pos_doors)
# if placement < width:
# dungeon[0][placement] = door
# elif placement > pos_doors-width:
# dungeon[-1][placement-(pos_doors - width)] = door
# else:
# dungeon[int((placement-13)/2)][-(placement%2)] = door #ewww
def place_monster(dungeon, rooms):
x, y = get_random_x_y(dungeon, rooms)
if dungeon[y][x] == floor:
monster = random.choice(MONSTERS)
if monster:
dungeon[y][x] = monster
def place_monster_cluster(dungeon, rooms):
width, height = get_width_height(dungeon)
x, y = get_random_x_y(dungeon, rooms)
monster = random.choice(list(MONSTER_CLUSTERS.keys()))
for i in range(MONSTER_CLUSTERS[monster]):
if 0 < x < width and 0 < y < height and dungeon[y][x] == floor:
dungeon[y][x] = monster
y += random.randint(-1,1)
x += random.randint(-1,1)
def place_food(dungeon, rooms):
x, y = get_random_x_y(dungeon, rooms)
if dungeon[y][x] == floor:
food = random.choice(FOODS)
if food:
dungeon[y][x] = food
def place_item(dungeon, rooms):
x, y = get_random_x_y(dungeon, rooms)
if dungeon[y][x] == floor:
item = random.choice(ITEMS)
if item:
dungeon[y][x] = item
def place_monument(dungeon, rooms):
x, y = get_random_x_y(dungeon, rooms)
if dungeon[y][x] == floor:
monument = random.choice(MONUMENTS)
if monument:
dungeon[y][x] = monument
def format_dungeon(dungeon):
return "\n".join(["".join(row) for row in dungeon])
def do_nothing(a, b):
return
ACTIONS = ([place_monster]*10+
[place_monster_cluster]*2+
[place_item]*5+
[place_food]*3+
[place_monument]*2+
[do_nothing]*5)