def generate_room(self, section): """ Generate room :param section: section for generator to draw to :type section: Section """ middle_height = section_height(section) // 2 middle_width = section_width(section) // 2 if len([x for x in section_connections(section) if x.direction == 'right']) > 0: room_left_edge = self.rng.randint(2, middle_width - 2) else: room_left_edge = 1 if len([x for x in section_connections(section) if x.direction == 'left']) > 0: room_right_edge = self.rng.randint(middle_width + 2, section_width(section) - 2) else: room_right_edge = section_width(section) - 1 if len([x for x in section_connections(section) if x.direction == 'down']) > 0: room_top_edge = self.rng.randint(2, middle_height - 2) else: room_top_edge = 1 if len([x for x in section_connections(section) if x.direction == 'up']) > 0: room_bottom_edge = self.rng.randint(middle_height + 2, section_height(section) - 2) else: room_bottom_edge = section_height(section) - 1 for loc_y in range(room_top_edge + 1, room_bottom_edge): for loc_x in range(room_left_edge + 1, room_right_edge): section_floor(section, (loc_x, loc_y), self.floor_tile, 'room') section_wall(section, (loc_x, loc_y), self.empty_tile, None) center_x = (room_right_edge - room_left_edge) // 2 + room_left_edge center_y = (room_bottom_edge - room_top_edge) // 2 + room_top_edge add_room_connection(section, (center_x, room_top_edge), "up") add_room_connection(section, (center_x, room_bottom_edge), "down") add_room_connection(section, (room_left_edge, center_y), "left") add_room_connection(section, (room_right_edge, center_y), "right") self.add_corridors(section) self.room_corners = [] self.room_corners.append((room_left_edge + 1, room_top_edge + 1)) self.room_corners.append((room_right_edge - 1, room_top_edge + 1)) self.room_corners.append((room_right_edge - 1, room_bottom_edge - 1)) self.room_corners.append((room_left_edge + 1, room_bottom_edge - 1)) self.add_rows()
def test_connect_row_of_sections(self): """ Test special case where connections have to branch Row of Sections is connected, starting from the middle RandomConnector can not connect this in one path, but has to branch """ section0 = new_section((0, 0), (10, 10), self.level, self.rng) section1 = new_section((11, 0), (20, 10), self.level, self.rng) section2 = new_section((21, 0), (30, 10), self.level, self.rng) section3 = new_section((31, 0), (40, 10), self.level, self.rng) section4 = new_section((41, 0), (50, 10), self.level, self.rng) mark_neighbours(section0, section1) mark_neighbours(section1, section2) mark_neighbours(section2, section3) mark_neighbours(section3, section4) sections = [section0, section1, section2, section3, section4] connected_sections = self.connector.connect_sections(sections, section2) # noqa for section in connected_sections: assert_that(list(section_connections(section)), has_length(greater_than(0))) assert_that(is_connected(section))
def test_connect_row_of_sections(self): """ Test special case where connections have to branch Row of Sections is connected, starting from the middle RandomConnector can not connect this in one path, but has to branch """ section0 = new_section((0, 0), (10, 10), self.level, self.rng) section1 = new_section((11, 0), (20, 10), self.level, self.rng) section2 = new_section((21, 0), (30, 10), self.level, self.rng) section3 = new_section((31, 0), (40, 10), self.level, self.rng) section4 = new_section((41, 0), (50, 10), self.level, self.rng) mark_neighbours(section0, section1) mark_neighbours(section1, section2) mark_neighbours(section2, section3) mark_neighbours(section3, section4) sections = [section0, section1, section2, section3, section4] connected_sections = self.connector.connect_sections( sections, section2) # noqa for section in connected_sections: assert_that(list(section_connections(section)), has_length(greater_than(0))) assert_that(is_connected(section))
def corridor(section, trap_generator=None): "carve corridors" for it in section_connections(section): room_connection = match_section_to_room(section, it) corridor = CorridorGenerator(room_connection, it.translate_to_section(), None, floor_tile) corridor.generate()
def add_corridors(self, section): """ Add corridors leading from room connection to section connections :param section: section to add corridors :type section: Section """ for section_connection in section_connections(section): room_connection = match_section_to_room(section, section_connection) corridor = CorridorGenerator( room_connection, section_connection.translate_to_section(), self.empty_tile, self.corridor_tile) corridor.generate()
def test_connect_two_sections(self): """ Test that two adjacent sections can be connected """ section1 = new_section((0, 0), (10, 5), self.level, self.rng) section2 = new_section((0, 6), (10, 10), self.level, self.rng) mark_neighbours(section1, section2) sections = [section1, section2] connected_sections = self.connector.connect_sections(sections) assert_that(connected_sections[1], is_(equal_to( list(section_connections(connected_sections[0]))[0].connection)))
def test_connect_two_sections(self): """ Test that two adjacent sections can be connected """ section1 = new_section((0, 0), (10, 5), self.level, self.rng) section2 = new_section((0, 6), (10, 10), self.level, self.rng) mark_neighbours(section1, section2) sections = [section1, section2] connected_sections = self.connector.connect_sections(sections) assert_that( connected_sections[1], is_( equal_to( list(section_connections( connected_sections[0]))[0].connection)))
def test_connecting_2x2_grid(self): """ Test that 2x2 grid is fully connected """ section00 = new_section((0, 0), (5, 5), self.level, self.rng) section10 = new_section((6, 0), (10, 5), self.level, self.rng) section01 = new_section((0, 6), (5, 10), self.level, self.rng) section11 = new_section((6, 6), (10, 10), self.level, self.rng) mark_neighbours(section00, section10) mark_neighbours(section00, section01) mark_neighbours(section10, section11) mark_neighbours(section01, section11) sections = [section00, section10, section01, section11] connected_sections = self.connector.connect_sections(sections) assert_that(connected_sections, has_length(4)) for section in connected_sections: assert_that(list(section_connections(section)), has_length(greater_than(0))) assert_that(is_connected(section))
def generate_room(self, section): """ Generate room :param section: section for generator to draw to :type section: Section """ middle_height = section_height(section) // 2 middle_width = section_width(section) // 2 if len([ x for x in section_connections(section) if x.direction == 'right' ]) > 0: room_left_edge = self.rng.randint(2, middle_width - 2) else: room_left_edge = 1 if len( [x for x in section_connections(section) if x.direction == 'left' ]) > 0: room_right_edge = self.rng.randint(middle_width + 2, section_width(section) - 2) else: room_right_edge = section_width(section) - 1 if len( [x for x in section_connections(section) if x.direction == 'down' ]) > 0: room_top_edge = self.rng.randint(2, middle_height - 2) else: room_top_edge = 1 if len([ x for x in section_connections(section) if x.direction == 'up' ]) > 0: room_bottom_edge = self.rng.randint(middle_height + 2, section_height(section) - 2) else: room_bottom_edge = section_height(section) - 1 for loc_y in range(room_top_edge + 1, room_bottom_edge): for loc_x in range(room_left_edge + 1, room_right_edge): section_floor(section, (loc_x, loc_y), self.floor_tile, 'room') section_wall(section, (loc_x, loc_y), self.empty_tile, None) center_x = (room_right_edge - room_left_edge) // 2 + room_left_edge center_y = (room_bottom_edge - room_top_edge) // 2 + room_top_edge add_room_connection(section, (center_x, room_top_edge), "up") add_room_connection(section, (center_x, room_bottom_edge), "down") add_room_connection(section, (room_left_edge, center_y), "left") add_room_connection(section, (room_right_edge, center_y), "right") self.add_corridors(section) self.room_corners = [] self.room_corners.append((room_left_edge + 1, room_top_edge + 1)) self.room_corners.append((room_right_edge - 1, room_top_edge + 1)) self.room_corners.append((room_right_edge - 1, room_bottom_edge - 1)) self.room_corners.append((room_left_edge + 1, room_bottom_edge - 1)) self.add_rows()