-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_generator.py
565 lines (372 loc) · 16 KB
/
map_generator.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import random
from os.path import exists
import os
import numpy as np
import math
from collections import defaultdict
"""
Idea: make a map generator based on the concept of the turtle drawing library.
We have a map generator that goes along the map, making corridors as it goes and sometimes expanding them into rooms of different sizes.
It also places coins and enemies along the way
"""
class MapGenerator(object):
floor_char = '.'
empty_char = '_'
wall_char = 'x'
player_char = 'p'
enemy_char = 'f'
health_char = 'r'
coin_char = 'm'
objective_char = '0'
save_folder = "./Maps/Generated_Maps/"
def print_map(self):
print("--------MAP--------")
for line in self.map:
for char in line:
print(char)
print()
def initialize_empty_map(self):
for _ in range(self.canvas_size):
line = ['_']*self.canvas_size
self.map.append(line)
class TurtleGenerator(MapGenerator):
directions = [(-1,0), (1,0), (0,1), (0,-1)]
def __init__(self, canvas_size, total_lenght, rotation_frequency, room_frequency, num_rooms, corridor_min_width, corridor_max_width, room_min_size, room_max_size):
self.map = []
self.canvas_size = canvas_size
self.total_lenght = total_lenght
self.rotation_frequency = rotation_frequency
self.room_frequency = room_frequency
self.num_rooms = num_rooms
self.corridor_min_width = corridor_min_width
self.corridor_max_width = corridor_max_width
self.room_min_size = room_min_size
self.room_max_size = room_max_size
self.initialize_empty_map()
def make_a_map(self):
turtle = [random.randint(2, self.canvas_size - 2 - self.corridor_max_width), random.randint(2, self.canvas_size - 2 - self.corridor_max_width)]
new_direction = random.choice(self.directions)
direction = new_direction
corridor_width = random.randint(self.corridor_min_width, self.corridor_max_width)
rooms_made = 0
current_lenght = 0
while((rooms_made < self.num_rooms) and current_lenght < self.total_lenght):
if random.uniform(0.0, 1.0) < self.rotation_frequency:
new_direction = random.choice(self.directions)
corridor_width = random.randint(self.corridor_min_width, self.corridor_max_width)
# if direction[0] == new_direction[0] and direction[1] == new_direction[1]:
# pass
# else:
direction = new_direction
new_turtle_x = turtle[0] + direction[0]
new_turtle_y = turtle[1] + direction[1]
change_counter = 0
while((new_turtle_x < 2) or (new_turtle_x >= self.canvas_size - 2 - self.corridor_max_width) or (new_turtle_y < 2) or (new_turtle_y >= self.canvas_size - 2 - self.corridor_max_width)):
direction = random.choice(self.directions)
new_turtle_x = turtle[0] + direction[0]
new_turtle_y = turtle[1] + direction[1]
change_counter +=1
if change_counter > 100:
print("Couldn't find a viable directional option!")
exit()
turtle[0] = new_turtle_x
turtle[1] = new_turtle_y
self.map[turtle[0]][turtle[1]] = self.floor_char
if direction[0] == 0:
for i in range(corridor_width):
self.map[turtle[0] + 1 + i][turtle[1]] = self.floor_char
if self.map[turtle[0] + corridor_width + 1][turtle[1]] == self.floor_char:
pass
else:
self.map[turtle[0] + corridor_width + 1][turtle[1]] = self.wall_char
if self.map[turtle[0] - 1][turtle[1]] == self.floor_char:
pass
else:
self.map[turtle[0] - 1][turtle[1]] = self.wall_char
elif direction[1] == 0:
for i in range(corridor_width):
self.map[turtle[0]][turtle[1] + 1 + i] = self.floor_char
if self.map[turtle[0]][turtle[1] + corridor_width + 1] == self.floor_char:
pass
else:
self.map[turtle[0]][turtle[1] + corridor_width + 1] = self.wall_char
if self.map[turtle[0]][turtle[1] - 1] == self.floor_char:
pass
else:
self.map[turtle[0]][turtle[1] - 1] = self.wall_char
current_lenght += 1
# Wall it up
for i in range(1, len(self.map) - 1):
for j in range(1, len(self.map[0]) - 1):
if self.map[i][j] == self.empty_char:
if self.map[i+1][j] == self.floor_char or self.map[i+1][j+1] == self.floor_char or self.map[i+1][j-1] == self.floor_char or self.map[i-1][j+1] == self.floor_char or self.map[i-1][j-1] == self.floor_char or self.map[i-1][j] == self.floor_char or self.map[i][j-1] == self.floor_char or self.map[i][j+1] == self.floor_char:
self.map[i][j] = self.wall_char
class TopDownGenerator(MapGenerator):
pass
class BottomUpGenerator(MapGenerator):
floor_char = '.'
empty_char = '_'
wall_char = 'x'
player_char = 'p'
enemy_char = 'f'
health_char = 'r'
coin_char = 'm'
objective_char = '0'
save_folder = "./Maps/Generated_Maps/"
def __init__(self, canvas_size, dungeon_density, num_loops, neighbour_depth, neighbour_number_threshold, coin_density, health_density, enemy_density, minimum_tile_distance_player_flower):
self.map = []
self.canvas_size = canvas_size
self.dungeon_density = dungeon_density
self.num_loops = num_loops
self.neighbour_depth = neighbour_depth
self.neighbour_number_threshold = neighbour_number_threshold
self.coin_density = coin_density
self.health_density = health_density
self.enemy_density = enemy_density
self.minimum_distance_player_flower = minimum_tile_distance_player_flower
self.small_col_matrix = None
def make_a_map(self, verbose = False, name = None):
path_between_player_and_flower = []
tries = 0
while len(path_between_player_and_flower) < self.minimum_distance_player_flower/2:
tries += 1
if tries > 500:
print("Cannot find viable map for given parameters after 500 tries. Giving up...")
return False
self.map = []
self.initialize_empty_map()
if verbose:
self.print_map()
self.randomly_add_floor_tiles()
if verbose:
self.print_map()
self.aglutinate_floor()
self.wall_it_up()
if verbose:
self.print_map()
np.set_printoptions(threshold=np.inf)
self.makeCollisionMatrix()
player_pos = self.place_player_randomly()
if verbose:
self.print_map()
flower_pos = self.place_flower_randomly()
if verbose:
self.print_map()
path_between_player_and_flower = self.AStar((int(player_pos[0]/2), int(player_pos[1]/2)), (int(flower_pos[0]/2), int(flower_pos[1]/2)))
self.place_enemies_randomly()
if verbose:
self.print_map()
self.place_coins_randomly()
if verbose:
self.print_map()
self.place_health_randomly()
if verbose:
self.print_map()
self.save_as_csv(name = name)
def randomly_add_floor_tiles(self):
for i in range(self.canvas_size):
for j in range(self.canvas_size):
if random.randint(0,100) < self.dungeon_density:
self.map[i][j] = self.floor_char
def get_position_empty_neighbours(self, i, j):
neighbours = []
if self.neighbour_depth <= 0:
print("Depth variable needs to be greater than 0")
exit()
for n_i in range(i - 1, i + 1 + 1):
for n_j in range(j - 1, j + 1 + 1):
if n_i == i and n_j == j:
continue
if self.map[n_i][n_j] == self.empty_char:
neighbours.append([n_i, n_j])
return neighbours
def get_position_floored_neighbours(self, i, j):
neighbours = []
bordering_edges = False
if self.neighbour_depth <= 0:
print("Depth variable needs to be greater than 0")
exit()
for n_i in range(i - self.neighbour_depth, i + self.neighbour_depth + 1):
for n_j in range(j - self.neighbour_depth, j + self.neighbour_depth + 1):
if n_i < 0 or n_i >= self.canvas_size or n_j < 0 or n_j >= self.canvas_size:
bordering_edges = True
continue
if n_i == i and n_j == j:
continue
if self.map[n_i][n_j] == self.floor_char:
neighbours.append([n_i, n_j])
return neighbours, bordering_edges
def aglutinate_floor(self):
for run in range(self.num_loops):
to_add = []
to_remove = []
for i in range(self.canvas_size):
for j in range(self.canvas_size):
neighbors, bordering = self.get_position_floored_neighbours(i, j)
if (len(neighbors) >= self.neighbour_number_threshold) and not bordering:
to_add.append([i, j])
else:
to_remove.append([i,j])
for cord in to_add:
self.map[cord[0]][cord[1]] = self.floor_char
for cord in to_remove:
self.map[cord[0]][cord[1]] = self.empty_char
def wall_it_up(self):
for i in range(self.canvas_size):
for j in range(self.canvas_size):
if self.map[i][j] == self.floor_char:
empty_neighbours = self.get_position_empty_neighbours(i,j)
for neighbour in empty_neighbours:
self.map[neighbour[0]][neighbour[1]] = self.wall_char
def place_player_randomly(self):
for _ in range(10000):
i = random.randint(1, self.canvas_size-2)
j = random.randint(1, self.canvas_size-2)
if self.map[i][j] == self.floor_char and len(self.get_position_floored_neighbours(i,j)[0]) == 8:
self.map[i][j] = self.player_char
return (i,j)
print("Too many attempts at placing player. The map generator quit...")
return False
def place_flower_randomly(self):
for _ in range(10000):
i = random.randint(1, self.canvas_size-2)
j = random.randint(1, self.canvas_size-2)
if self.map[i][j] == self.floor_char and len(self.get_position_floored_neighbours(i,j)[0]) == 8:
self.map[i][j] = self.objective_char
return (i,j)
print("Too many attempts at placing flower. The map generator quit...")
return False
def place_coins_randomly(self):
for _ in range(self.canvas_size*self.canvas_size):
i = random.randint(1, self.canvas_size-2)
j = random.randint(1, self.canvas_size-2)
if self.map[i][j] == self.floor_char and len(self.get_position_floored_neighbours(i,j)[0]) == 8:
if random.uniform(0.0, 100.0) < self.coin_density:
self.map[i][j] = self.coin_char
def place_health_randomly(self):
for _ in range(self.canvas_size*self.canvas_size):
i = random.randint(1, self.canvas_size-2)
j = random.randint(1, self.canvas_size-2)
if self.map[i][j] == self.floor_char and len(self.get_position_floored_neighbours(i,j)[0]) == 8:
if random.uniform(0.0, 100.0) < self.health_density:
self.map[i][j] = self.health_char
def place_enemies_randomly(self):
for _ in range(self.canvas_size*self.canvas_size):
i = random.randint(1, self.canvas_size-2)
j = random.randint(1, self.canvas_size-2)
if self.map[i][j] == self.floor_char and len(self.get_position_floored_neighbours(i,j)[0]) == 8:
if random.uniform(0.0, 100.0) < self.enemy_density:
self.map[i][j] = self.enemy_char
def makeCollisionMatrix(self):
self.small_col_matrix = np.zeros((int(self.canvas_size/2), int(self.canvas_size/2)))
for i in range(int(self.canvas_size/2)):
for j in range(int(self.canvas_size/2)):
if self.map[i*2][j*2] == self.wall_char or self.map[i*2 + 1][j*2] == self.wall_char or self.map[i*2][j*2 + 1] == self.wall_char or self.map[i*2 + 1][j*2 + 1] == self.wall_char:
self.small_col_matrix[i][j] = 1
def reconstruct_path(self, cameFrom, current, start):
total_path = [current]
while current != start:
current = cameFrom[current]
total_path.insert(0, current)
return total_path
def AStarHeuristic(self, a, b):
#Euclidian Distance
return ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5
#Only returns diagonals when both adjacent horizontal and vertical tiles are also free as to avoid collisions.
def getMixedNeighbors(self, node):
neighbors = []
if node[0]-1 > 0 and self.small_col_matrix[node[0]-1][node[1]] == 0:
neighbors.append((node[0]-1, node[1]))
if node[1]-1 > 0 and self.small_col_matrix[node[0]][node[1]-1] == 0:
if self.small_col_matrix[node[0]-1][node[1]-1] == 0:
neighbors.append((node[0]-1, node[1]-1))
if node[1]+1 < len(self.small_col_matrix[0]) and self.small_col_matrix[node[0]][node[1]+1] == 0:
if self.small_col_matrix[node[0]-1][node[1]+1] == 0:
neighbors.append((node[0]-1, node[1]+1))
if node[0]+1 < len(self.small_col_matrix) and self.small_col_matrix[node[0]+1][node[1]] == 0:
neighbors.append((node[0] + 1, node[1]))
if node[1]-1 > 0 and self.small_col_matrix[node[0]][node[1]-1] == 0:
if self.small_col_matrix[node[0]+1][node[1]-1] == 0:
neighbors.append((node[0]+1, node[1]-1))
if node[1]+1 < len(self.small_col_matrix[0]) and self.small_col_matrix[node[0]][node[1]+1] == 0:
if self.small_col_matrix[node[0]+1][node[1]+1] == 0:
neighbors.append((node[0]+1, node[1]+1))
if node[1]-1 > 0 and self.small_col_matrix[node[0]][node[1]-1] == 0:
neighbors.append((node[0], node[1]-1))
if node[1]+1 < len(self.small_col_matrix[0]) and self.small_col_matrix[node[0]][node[1]+1] == 0:
neighbors.append((node[0], node[1] + 1))
return neighbors
# A* finds a path from start to goal.
# h is the heuristic function. h(n) estimates the cost to reach goal from node n.
def AStar(self, start, goal):
# The set of discovered nodes that may need to be (re-)expanded.
# Initially, only the start node is known.
# This is usually implemented as a min-heap or priority queue rather than a hash-set.
openSet = [start]
# For node n, cameFrom[n] is the node immediately preceding it on the cheapest path from start
# to n currently known.
cameFrom = {}
# For node n, gScore[n] is the cost of the cheapest path from start to n currently known.
# I initialize every value as Infinite
gScore = {}
gScore = defaultdict(lambda: math.inf, gScore)
gScore[start] = 0
# For node n, fScore[n] := gScore[n] + h(n). fScore[n] represents our current best guess as to
# how short a path from start to finish can be if it goes through n.
fScore = {}
fScore = defaultdict(lambda: math.inf, fScore)
fScore[start] = self.AStarHeuristic(start, goal)
while len(openSet) > 0:
# This operation can occur in O(Log(N)) time if openSet is a min-heap or a priority queue
min_val = math.inf
for node in openSet:
if fScore[node] < min_val:
min_val = fScore[node]
current = node
if current == goal:
return self.reconstruct_path(cameFrom, current, start)
openSet.remove(current)
for neighbor in self.getMixedNeighbors(current):
# d(current,neighbor) is the weight of the edge from current to neighbor
# tentative_gScore is the distance from start to the neighbor through current
tentative_gScore = gScore[current] + 1 #d(current, neighbor) --> for now, lets say all weights are 1
if tentative_gScore < gScore[neighbor]:
# This path to neighbor is better than any previous one. Record it!
cameFrom[neighbor] = current
gScore[neighbor] = tentative_gScore
fScore[neighbor] = tentative_gScore + self.AStarHeuristic(neighbor, goal)
if neighbor not in openSet:
openSet.append(neighbor)
# Open set is empty but goal was never reached
return []
def save_as_csv(self, name):
rand_id = random.randint(1000000000, 9999999999)
if name == None:
file_path = self.save_folder + "BottomUpMap_" + str(self.canvas_size) + "_" + str(self.dungeon_density) + "_" + str(self.num_loops) + "_" + str(self.neighbour_depth) + "_" + str(self.neighbour_number_threshold) + "_" + str(self.coin_density) + "_" + str(self.health_density) + "_" + str(rand_id) +".csv"
else:
file_path = self.save_folder + "Map_" + str(name) +".csv"
while exists(file_path):
if name == None:
print("File already exists")
return
else:
rand_id = random.randint(1000000000, 9999999999)
file_path = self.save_folder + "BottomUpMap_" + str(self.canvas_size) + "_" + str(self.dungeon_density) + "_" + str(self.num_loops) + "_" + str(self.neighbour_depth) + "_" + str(self.neighbour_number_threshold) + "_" + str(self.coin_density) + "_" + str(self.health_density) + "_" + str(rand_id) +".csv"
f = open(file_path, 'w+')
for line in self.map:
for char in line:
f.write(char)
f.write(';')
f.write("\n")
##############################################
### Code to make celular automata maps
##############################################
# turly = BottomUpGenerator(32, 30, 3, 1, 3, 0.5, 0.5, 0.4, 10)
# for i in range(1):
# turly.make_a_map(name = i, verbose = True)
##############################################
### Code to make room and corridors maps
##############################################
mappy_maker = TurtleGenerator(150, 800, 0.1, 0.1, 5, 3, 4, 6, 20)
mappy_maker.make_a_map()
mappy_maker.print_map()