+ATTRIBUTE_MAP = {
+ '#': {'base': 'cwall',
+ 'behaviour': [],
+ },
+ ' ': {'base': 'floor',
+ 'behaviour': ['walk', 'fly'],
+ },
+}
+
+
+class Room:
+ def __init__(self, coordinates, region):
+ """
+ """
+ self.coordinates = [coordinates]
+ self.region = region
+ self.max_connections = 1
+ self.passages = []
+ self.tunnels = []
+
+ def is_linked(self):
+ """
+ Check if the room is linked to another room
+ :return: Whether the room has any links or not
+ """
+ return len(self.passages) + len(self.tunnels) > 0
+
+ def add_region(self, coordinates):
+ """
+ Add a new region into an existing room
+ :param coordinates: region coordinates to be added to room
+ :return:
+ """
+ self.coordinates.append(coordinates)
+
+ def connect_rooms(self, other_rooms):
+ """ Find the nearest rooms to this room
+ :param other_rooms: list of Rooms objects that we are searching
+ :return:
+ """
+ distance = []
+ other_tile = []
+ this_tile = []
+ target_rooms = []
+ for coord in self.coordinates:
+ for room in other_rooms:
+ if self == room:
+ continue
+ for new_coord in room.coordinates:
+ distance.append(
+ 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),
+ key=lambda x:x[0])]
+ for index in sorted_indices:
+ if len(self.passages) + len(self.tunnels) >= self.max_connections:
+ break
+ if not target_rooms[index].is_linked():
+ self.link_passage(this_tile[index], other_tile[index])
+ target_rooms[index].link_passage(
+ other_tile[index], this_tile[index])
+
+ def link_passage(self, local_tile, foriegn_tile):
+ """ Link a passage between two rooms
+ :param local_tile: tile in this room to which we wish to link
+ :param foriegn_tile: tile in another room to which we wish to link
+ :return:
+ """
+ self.passages.append([local_tile, foriegn_tile])
+
+ 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] = ' '