def chunkify(m): direction = Dir.horizontal start_chunk = Rect(0, 0, m.width, m.height) start_chunk.room = -1 chunks = [start_chunk] minsize = 13 minlength = 6 while m.free_area < m.total_size * 0.2: curr = pop_largest(chunks) if direction == Dir.vertical and curr.width > minsize: start = curr.x + max(minlength, int(curr.width * 0.2)) end = curr.x + curr.width - max(minlength, int(curr.width * 0.2)) hallway = random.randint(start, end) first = Rect(curr.x, curr.y, hallway - curr.x, curr.height) second = Rect(hallway + 1, curr.y, curr.x + curr.width - hallway - 1, curr.height) new_direction = Dir.horizontal elif direction == Dir.horizontal and curr.height > minsize: # horizontal start = curr.y + max(minlength, int(curr.height * 0.2)) end = curr.y + curr.height - max(minlength, int(curr.height * 0.2)) hallway = random.randint(start, end) first = Rect(curr.x, curr.y, curr.width, hallway - curr.y) second = Rect(curr.x, hallway + 1, curr.width, curr.y + curr.height - hallway - 1) new_direction = Dir.vertical else: # chunk too small, done. just re-add the chunk chunks.append(curr) break mark_room(m, first, m.first_unused_room_id) chunks.append(first) mark_room(m, second, m.first_unused_room_id) chunks.append(second) mark_hallway(m, curr, hallway, direction) direction = new_direction # print_map(m) return chunks