Merge
authorDavid Sharpe <decoydavid@gmail.com>
Thu, 3 Mar 2016 21:09:36 +0000 (23:09 +0200)
committerDavid Sharpe <decoydavid@gmail.com>
Thu, 3 Mar 2016 21:09:36 +0000 (23:09 +0200)
koperkapel/generators/maps.py

index 4f917c68169ea454c9feb34d63ee4a2284ff70e7..bbb6d1384b8e59c4ee35c7b4a48576c58e2f3f38 100644 (file)
@@ -5,15 +5,22 @@ import math
 import json
 import os
 
-i = random.randint(0,100)
+ATTRIBUTE_MAP = {
+    '#': {'base': 'cwall',
+          'behaviour': [],
+         },
+    ' ': {'base': 'floor',
+          'behaviour': ['walk', 'fly'],
+         },
+}
 
 
-<<<<<<< HEAD
 class Room:
-    def __init__(self, region):
+    def __init__(self, coordinates, region):
         """
         """
-        self.regions = [region]
+        self.coordinates = [coordinates]
+        self.region = region
         self.max_connections = 1
         self.passages = []
         self.tunnels = []
@@ -25,21 +32,13 @@ class Room:
         """
         return len(self.passages) + len(self.tunnels) > 0
 
-    def add_passage(self, room_region, new_region):
-        """ Add a passage link between rooms
-        :param room_region: coordinate of region from which we are linking
-        :param new_region: coordinate of region to which we are linking
-        :return:
-        """
-        pass
-
-    def add_region(self, region):
+    def add_region(self, coordinates):
         """
         Add a new region into an existing room
-        :param region: region coordinates to be added to room
+        :param coordinates: region coordinates to be added to room
         :return:
         """
-        self.regions.append(region)
+        self.coordinates.append(coordinates)
 
     def connect_rooms(self, other_rooms):
         """ Find the nearest rooms to this room
@@ -50,16 +49,16 @@ class Room:
         other_tile = []
         this_tile = []
         target_rooms = []
-        for local_region in self.regions:
+        for coord in self.coordinates:
             for room in other_rooms:
                 if self == room:
                     continue
-                for new_region in room.regions:
+                for new_coord in room.coordinates:
                     distance.append(
-                        math.sqrt((local_region[0] - new_region[0]) ** 2 +
-                                  (local_region[1] - new_region[1]) ** 2))
-                    other_tile.append(new_region)
-                    this_tile.append(local_region)
+                        math.sqrt((coord[0] - new_coord[0]) ** 2 +
+                                  (coord[1] - new_coord[1]) ** 2))
+                    other_tile.append(new_coord)
+                    this_tile.append(coord)
                     target_rooms.append(room)
 
         sorted_indices = [i[0] for i in sorted(enumerate(distance),
@@ -79,16 +78,54 @@ class Room:
         :return:
         """
         self.passages.append([local_tile, foriegn_tile])
-=======
-ATTRIBUTE_MAP = {
-    '#': {'base': 'cwall', 
-          'behaviour': [],
-         },
-    ' ': {'base': 'floor',
-          'behaviour': ['walk', 'fly'],
-         },
-}
->>>>>>> 0b2149fff63b346192148b7eb334e00ad3b9283d
+
+    def render_region(self, region, 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 room_dist: Tile separation distance from other rooms
+        :param region_size: Region size in tiles
+        :return:
+        """
+        if region in self.coordinates:
+            print(region)
+            print(self.region)
+            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 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] = ' '
 
 
 class LevelGenerator:
@@ -97,26 +134,22 @@ class LevelGenerator:
     no_rooms = 0
     rooms = []
     map = None
-    min_room_size = 0
-    max_room_size = 0
-    dist_from_edge = 0
+    map2 = None
     dist_from_other_rooms = 0
     region_map = None
     regions = 0
     region_size = 0
 
-    def __init__(self, width, height, no_rooms, min_room_size, max_room_size,
-                 dist_from_edge, dist_from_other_rooms, region_size):
+    def __init__(self, width, height, no_rooms, dist_from_other_rooms,
+                 region_size):
         """ Initialize the level parameters
         """
         self.width = width
         self.height = height
         self.no_rooms = no_rooms
-        self.min_room_size = min_room_size
-        self.max_room_size = max_room_size
-        self.dist_from_edge = dist_from_edge
         self.dist_from_other_rooms = dist_from_other_rooms
         self.region_size = region_size
+        self.region_coordinates = []
 
     def generate(self):
         """ Generate a random level map
@@ -126,11 +159,21 @@ class LevelGenerator:
                                          min(self.regions, self.no_rooms))
         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_tiles(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)
 
     def generate_rooms(self):
         """ Generate a random level region map
@@ -142,35 +185,37 @@ class LevelGenerator:
                 random_number = random.randint(0, 2)
                 increment_region = False
                 if w == h == 0:
-                    self.region_map[h][w] = self.regions
+                    update_value = self.regions
                     increment_region = True
                 elif h == 0:
                     if random_number > 1:
-                        self.region_map[h][w] = self.region_map[h][w - 1]
+                        update_value = self.region_map[h][w - 1]
                     else:
-                        self.region_map[h][w] = self.regions
+                        update_value = self.regions
                         increment_region = True
                 elif w == 0:
                     if random_number > 1:
-                        self.region_map[h][w] = self.region_map[h - 1][w]
+                        update_value = self.region_map[h - 1][w]
                     else:
-                        self.region_map[h][w] = self.regions
+                        update_value = self.regions
                         increment_region = True
                 else:
                     if random_number > 1:
-                        self.region_map[h][w] = self.region_map[h - 1][w]
-
+                        update_value = self.region_map[h - 1][w]
                     elif random_number > 0:
-                        self.region_map[h][w] = self.region_map[h][w - 1]
+                        update_value = self.region_map[h][w - 1]
                     else:
-                        self.region_map[h][w] = self.regions
+                        update_value = self.regions
                         increment_region = True
+                self.region_map[h][w] = update_value
                 if increment_region:
-                    self.regions += 1
-                    r = Room([h, w])
+                    r = Room([h, w], self.regions)
                     self.rooms.append(r)
+                    self.region_coordinates.append([update_value, [h, w]])
+                    self.regions += 1
                 else:
-                    self.rooms[-1].add_region([h,w])
+                    self.rooms[-1].add_region([h, w])
+                    self.region_coordinates.append([update_value, [h, w]])
 
     def generate_tiles(self, region_selected):
         """
@@ -211,28 +256,24 @@ class LevelGenerator:
                     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 generate_passage_tiles(self):
-        """ Generate the tiles that form the passages between the rooms
-        :return:
-        """
-
-
+                                [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))
+            file.write('\n')
+        print('-----------------')
         file.close()
-<<<<<<< HEAD
         for l in self.region_map:
-=======
-        self._to_json()
-        for l in self.regions:
->>>>>>> 0b2149fff63b346192148b7eb334e00ad3b9283d
+            # self._to_json()
             print(l)
 
     def _to_json(self):
@@ -254,9 +295,7 @@ class LevelGenerator:
 if __name__ == '__main__':
     while True:
         level = LevelGenerator(width=4, height=3, no_rooms=4,
-                               min_room_size=5, max_room_size=20,
-                               dist_from_edge=1, dist_from_other_rooms=1,
-                               region_size=5)
+                               dist_from_other_rooms=0, region_size=3)
         level.generate()
         level.display()
         input("Press Enter to continue...")