1 """ Procedural map generation for levels """
8 i = random.randint(0,100)
12 '#': {'base': 'cwall',
15 ' ': {'base': 'floor',
16 'behaviour': ['walk', 'fly'],
29 dist_from_other_rooms = 0
32 region_size_in_tiles = 0
34 def __init__(self, width, height, rooms, min_room_size, max_room_size, dist_from_edge,
35 dist_from_other_rooms, region_size_in_tiles):
36 """ Initialize the level parameters
41 self.min_room_size = min_room_size
42 self.max_room_size = max_room_size
43 self.dist_from_edge = dist_from_edge
44 self.dist_from_other_rooms = dist_from_other_rooms
45 self.region_size_in_tiles = region_size_in_tiles
48 """ Generate a random level map
50 self.generate_regions()
51 row = ['#' for x in range(self.width * self.region_size_in_tiles)]
52 self.map = [row[:] for x in range(self.height * self.region_size_in_tiles)]
53 regions_selected = random.sample(range(self.region), min(self.region, self.rooms))
54 print('Regions: %s' % str(regions_selected))
55 for region in regions_selected:
56 self.generate_room(region)
58 def generate_regions(self):
59 """ Generate a random level region map
61 row = ['#' for x in range(self.width)]
62 self.regions = [row[:] for x in range(self.height)]
63 for h in range(self.height):
64 for w in range(self.width):
65 random_number = random.randint(0, 2)
67 self.regions[h][w] = self.region
72 self.regions[h][w] = self.regions[h][w - 1]
77 self.regions[h][w] = self.region
81 self.regions[h][w] = self.regions[h - 1][w]
83 self.regions[h][w] = self.region
87 self.regions[h][w] = self.regions[h - 1][w]
88 elif random_number > 0:
89 self.regions[h][w] = self.regions[h][w - 1]
91 self.regions[h][w] = self.region
94 def generate_room(self, region_selected):
97 for h in range(self.height):
98 for w in range(self.width):
99 if self.regions[h][w] == region_selected:
101 w_dist = self.dist_from_other_rooms
102 elif self.regions[h][w-1] == region_selected:
105 w_dist = self.dist_from_other_rooms
107 if w + 1 == self.width:
108 e_dist = self.region_size_in_tiles - self.dist_from_other_rooms
109 elif self.regions[h][w+1] == region_selected:
110 e_dist = self.region_size_in_tiles
112 e_dist = self.region_size_in_tiles - self.dist_from_other_rooms
115 n_dist = self.dist_from_other_rooms
116 elif self.regions[h-1][w] == region_selected:
119 n_dist = self.dist_from_other_rooms
121 if h + 1 == self.height:
122 s_dist = self.region_size_in_tiles - self.dist_from_other_rooms
123 elif self.regions[h+1][w] == region_selected:
124 s_dist = self.region_size_in_tiles
126 s_dist = self.region_size_in_tiles - self.dist_from_other_rooms
128 for wt in range(w_dist, e_dist):
129 for ht in range(n_dist, s_dist):
130 self.map[h * self.region_size_in_tiles + ht][w * self.region_size_in_tiles + wt] = ' '
133 file = open('map.txt', 'w')
136 file.write(''.join(l))
140 for l in self.regions:
149 row.append(ATTRIBUTE_MAP[t])
150 level['tiles'].append(row)
151 name = os.path.join(os.path.dirname(__file__), '..', 'levels', 'map.json')
152 # FIXME: Do a lot better here
153 # Crude hack so the level is written into the levels folder
159 if __name__ == '__main__':
161 level = LevelGenerator(width=8, height=5, rooms=12, min_room_size=5, max_room_size=20,
162 dist_from_edge=2, dist_from_other_rooms=1, region_size_in_tiles=6)
165 input("Press Enter to continue...")