Fixed rooms in maps again and added passages(broken)
authorDavid Sharpe <decoydavid@gmail.com>
Fri, 4 Mar 2016 05:45:25 +0000 (07:45 +0200)
committerDavid Sharpe <decoydavid@gmail.com>
Fri, 4 Mar 2016 05:45:25 +0000 (07:45 +0200)
koperkapel/generators/maps.py

index 6e6bdbdc1915f61627fb1c5872979bfe75fa5d54..164d27fb90bcbae767aa576996368f85884c80da 100644 (file)
@@ -38,7 +38,7 @@ class Room:
         """
         return len(self.passages) + len(self.tunnels) > 0
 
-    def add_region(self, coordinates):
+    def add_coords(self, coordinates):
         """
         Add a new region into an existing room
         :param coordinates: region coordinates to be added to room
@@ -85,9 +85,9 @@ class Room:
         """
         self.passages.append([local_tile, foreign_tile])
 
-    def render_region(self, region, room_dist, region_size, tile_map, x, y):
+    def render_region(self, coords, room_dist, region_size, tile_map, x, y):
         """ Check if a region is in this room and return the required tiles
-        :param region: Region that we wish to render
+        :param coords: Coordinates of the region that we wish to render
         :param room_dist: Tile separation distance from other rooms
         :param region_size: Region size in tiles
         :param tile_map: Tile map to update
@@ -95,44 +95,47 @@ class Room:
         :param y: Y coordinate
         :return:
         """
-        if region in self.coordinates:
-            print(self.coordinates)
-            for ht in range(room_dist, region_size - room_dist):
-                for wt in range(room_dist, region_size - room_dist):
-                    tile_map[x + ht][y + wt] = ' '
+        if coords in self.coordinates:
+            x_pre_room_dist = room_dist
+            x_post_room_dist = region_size - room_dist
+            y_pre_room_dist = room_dist
+            y_post_room_dist = region_size - room_dist
+            if [x - 1, y] in self.coordinates:
+                y_pre_room_dist = 0
+            if [x + 1, y] in self.coordinates:
+                y_post_room_dist = region_size
+            if [x, y - 1] in self.coordinates:
+                x_pre_room_dist = 0
+            if [x, y + 1] in self.coordinates:
+                x_post_room_dist = region_size
+
+            for ht in range(y_pre_room_dist, y_post_room_dist):
+                for wt in range(x_pre_room_dist, x_post_room_dist):
+                    tile_map[(x * region_size) + ht][(y * region_size) + wt] =\
+                                             str(self.region)
+            for p in self.passages:
+                print(p)
+                x_regions = p[0][0] - p[1][0]
+                y_regions = p[0][1] - p[1][1]
+                if p[0][0] < p[1][0]:
+                    x_direction = -1
+                elif p[0][0] < p[1][0]:
+                    x_direction = 0
+                else:
+                    x_direction = 1
+                if p[0][1] < p[1][1]:
+                    y_direction = -1
+                elif p[0][1] < p[1][1]:
+                    y_direction = 0
+                else:
+                    y_direction = 1
+                for ht in range(0, region_size, x_direction):
+                    tile_map[(p[0][0] * region_size) + int(region_size / 2)]\
+                        [(p[0][1] * region_size) + int(region_size / 2) + ht] = 'p'
+                for wt in range(0, y_regions, y_direction):
+                    tile_map[(p[0][0] * region_size) + int(region_size / 2) + wt]\
+                        [(p[0][1] * region_size) + int(region_size / 2)] = 'p'
 
-                    # if w == 0:
-                    #     w_dist = self.dist_from_other_rooms
-                    # elif self.region_map[h][w-1] == region_selected:
-                    #     w_dist = 0
-                    # else:
-                    #     w_dist = self.dist_from_other_rooms
-                    #
-                    # if w + 1 == self.width:
-                    #     e_dist = self.region_size - self.dist_from_other_rooms
-                    # elif self.region_map[h][w+1] == region_selected:
-                    #     e_dist = self.region_size
-                    # else:
-                    #     e_dist = self.region_size - self.dist_from_other_rooms
-                    #
-                    # if h == 0:
-                    #     n_dist = self.dist_from_other_rooms
-                    # elif self.region_map[h-1][w] == region_selected:
-                    #     n_dist = 0
-                    # else:
-                    #     n_dist = self.dist_from_other_rooms
-                    #
-                    # if h + 1 == self.height:
-                    #     s_dist = self.region_size - self.dist_from_other_rooms
-                    # elif self.region_map[h+1][w] == region_selected:
-                    #     s_dist = self.region_size
-                    # else:
-                    #     s_dist = self.region_size - self.dist_from_other_rooms
-                    #
-                    # for wt in range(w_dist, e_dist):
-                    #     for ht in range(n_dist, s_dist):
-                    #         self.map[h * self.region_size + ht]\
-                    #             [w * self.region_size + wt] = ' '
 
 def random_cardinal():
     """Return a random cardinal direction for random walks."""
@@ -171,20 +174,15 @@ class LevelGenerator:
         row = ['#' for x in range(self.width * self.region_size)]
         self.map = [row[:] for x in range(self.height * self.region_size)]
         self.map2 = [row[:] for x in range(self.height * self.region_size)]
-        print('Regions: %s' % str(regions_selected))
         for region in regions_selected:
             self.rooms[region].connect_rooms(
                 [self.rooms[i] for i in regions_selected])
-            self.generate_room(region)
         region_coordinates_selected = [p for p in self.region_coordinates if
                                        p[0] in regions_selected]
-        print('Coords: %s' % str(region_coordinates_selected))
         for coord in region_coordinates_selected:
-            print(str(coord))
             self.rooms[coord[0]].render_region(
                 coord[1], self.dist_from_other_rooms, self.region_size,
-                self.map2, coord[1][0] * self.region_size,
-                coord[1][1] * self.region_size)
+                self.map2, coord[1][0], coord[1][1])
         # self.generate_underlayer()
 
     def generate_rooms(self):
@@ -228,7 +226,7 @@ class LevelGenerator:
                 else:
                     for r in self.rooms:
                         if r.region == update_value:
-                            r.add_region([h, w])
+                            r.add_coords([h, w])
                     self.region_coordinates.append([update_value, [h, w]])
 
     def generate_underlayer(self):
@@ -273,55 +271,9 @@ class LevelGenerator:
                 x += dir_x
                 y += dir_y
 
-    def generate_room(self, region_selected):
-        """
-        :param region_selected:
-        :return:
-        """
-        for h in range(self.height):
-            for w in range(self.width):
-                if self.region_map[h][w] == region_selected:
-                    if w == 0:
-                        w_dist = self.dist_from_other_rooms
-                    elif self.region_map[h][w-1] == region_selected:
-                        w_dist = 0
-                    else:
-                        w_dist = self.dist_from_other_rooms
-
-                    if w + 1 == self.width:
-                        e_dist = self.region_size - self.dist_from_other_rooms
-                    elif self.region_map[h][w+1] == region_selected:
-                        e_dist = self.region_size
-                    else:
-                        e_dist = self.region_size - self.dist_from_other_rooms
-
-                    if h == 0:
-                        n_dist = self.dist_from_other_rooms
-                    elif self.region_map[h-1][w] == region_selected:
-                        n_dist = 0
-                    else:
-                        n_dist = self.dist_from_other_rooms
-
-                    if h + 1 == self.height:
-                        s_dist = self.region_size - self.dist_from_other_rooms
-                    elif self.region_map[h+1][w] == region_selected:
-                        s_dist = self.region_size
-                    else:
-                        s_dist = self.region_size - self.dist_from_other_rooms
-
-                    for wt in range(w_dist, e_dist):
-                        for ht in range(n_dist, s_dist):
-                            self.map[h * self.region_size + ht]\
-                                [w * self.region_size + wt] = str(region_selected)
-
     def display(self):
         file = open('map.txt', 'w')
         print('-----------------')
-        for l in self.map:
-            print(''.join(l))
-            file.write(''.join(l))
-            file.write('\n')
-        print('-----------------')
         for l in self.map2:
             print(''.join(l))
             file.write(''.join(l))
@@ -359,9 +311,7 @@ class LevelGenerator:
 
 
 if __name__ == '__main__':
-    while True:
-        level = LevelGenerator(width=4, height=3, no_rooms=4,
-                               dist_from_other_rooms=0, region_size=3)
-        level.generate()
-        level.display()
-        input("Press Enter to continue...")
+    level = LevelGenerator(width=4, height=3, no_rooms=4,
+                           dist_from_other_rooms=1, region_size=5)
+    level.generate()
+    level.display()