Ejemplo n.º 1
0
 def generate_collider_grid(cls, size_vector=Vector2.zero()):
     pass
Ejemplo n.º 2
0
    async def generate_map(cls,
                           map_size=None,
                           connectivity=None,
                           biome=None,
                           level=None,
                           level_interval=1,
                           current_map=[],
                           special_rooms=[],
                           special_room_occurance=0.01,
                           client=None,
                           progress_message=None):
        """
        Generates a series of linked rooms
        :param map_size: number of rooms in the map
        :param connectivity: how many hallways in the map
        :param biome: map biome
        :param level: map starting level, goes up by level_interval for each room
        :param current_map: The map that this map will build upon
        :param special_rooms: list of premade rooms
        :param special_room_occurance: how often premade rooms appear as percent (0, 1)
        :return: Map instance
        """

        start_time = datetime.datetime.now()

        if not map_size:
            map_size = random.randint(5, 30)
            logger.debug(map_size)

        if not connectivity:
            connectivity = random.uniform(0, 1)

        if not biome:
            biome = Room.biome_list[random.randint(0,
                                                   len(Room.biome_list) - 1)]

        if not level:
            level = 1

        if current_map:
            room_list = current_map
        else:
            room_list = [
                Room(items=None,
                     enemies=None,
                     doors={
                         'north': True,
                         'south': False,
                         'east': False,
                         'west': False
                     },
                     position=Vector2.zero(),
                     size_vector=Vector2(random.randint(2, 8),
                                         random.randint(2, 8)),
                     biome=biome,
                     level=level,
                     entrance_direction=None,
                     exit_directions=['north'])
            ]

        map_size = int(map_size)
        level = int(level)

        message_content = progress_message.content
        progress_message = await client.edit_message(
            progress_message, message_content + f'\nProgress: []')

        list_index = 0
        while list_index <= map_size - 2:
            last_room = room_list[list_index]
            room_succesful = False
            level += level_interval

            entrance_direction = GameObject.opposite_direction(
                last_room.exit_directions[random.randint(
                    0,
                    len(last_room.exit_directions) - 1)])

            room_tries = 0
            while room_tries < 100:

                doors = {
                    'north': random.randint(0, 1) == 1,
                    'south': random.randint(0, 1) == 1,
                    'east': random.randint(0, 1) == 1,
                    'west': random.randint(0, 1) == 1
                }
                pass  # get rid of pycharm formatting bug
                doors[entrance_direction] = True

                exit_directions = []

                for direction in doors:
                    if direction != entrance_direction and doors[direction]:
                        exit_directions.append(direction)

                if len(exit_directions) == 0:
                    # room_tries += 1
                    # logger.debug(f'No exit {room_tries}')
                    continue

                if random.uniform(
                        0,
                        1) < special_room_occurance and len(special_rooms) > 0:

                    room = special_rooms[random.randint(
                        0,
                        len(special_rooms) - 1)]

                else:

                    room = Room(items=None,
                                enemies=None,
                                doors=doors,
                                biome=biome,
                                level=level,
                                position=Vector2.zero(),
                                size_vector=Vector2(random.randint(2, 20),
                                                    random.randint(2, 20)),
                                entrance_direction=entrance_direction,
                                exit_directions=exit_directions)

                # if room is above last_room
                if entrance_direction == 'north':
                    room.position.x = random.randint(
                        last_room.position.x - room.size_vector.x + 1,
                        last_room.position.x + last_room.size_vector.x - 1)

                    room.position.y = int(last_room.position.y +
                                          last_room.size_vector.y)

                # if room is below last_room
                elif entrance_direction == 'south':
                    room.position.x = random.randint(
                        last_room.position.x - room.size_vector.x + 1,
                        last_room.position.x + last_room.size_vector.x - 1)

                    room.position.y = int(last_room.position.y -
                                          room.size_vector.y)

                # if room to the right of last_room
                elif entrance_direction == 'east':
                    room.position.x = int(last_room.position.x +
                                          last_room.size_vector.x)

                    room.position.y = room.position.y = random.randint(
                        last_room.position.y - room.size_vector.y + 1,
                        last_room.position.y + last_room.size_vector.y - 1)

                # if room to the left of last_room
                elif entrance_direction == 'west':
                    room.position.x = int(last_room.position.x -
                                          room.size_vector.x)

                    room.position.y = random.randint(
                        last_room.position.y - room.size_vector.y + 1,
                        last_room.position.y + last_room.size_vector.y - 1)

                if room.check_all_collision(room_list):
                    room_tries += 1
                    # logger.debug(f'room tries: {room_tries}')
                    continue

                room_list.append(room)
                logger.info(f'Generated level {level} room')
                # last_room = room
                room_tries = 100000
                room_succesful = True
                wip_map = Map(level, biome, room_list)
                wip_map.print_map(f'{level}.txt')
                list_index += 1

            if not room_succesful:
                logger.info(f'level {level} room unsuccesful')
                level -= 1 * level_interval
                for _ in range(0, 5):
                    if list_index > 0:
                        level -= int(1 * level_interval)
                        list_index -= 1
                        del (room_list[-1])

            progress_string = GameObject.progress_bar(
                int((len(room_list) / map_size) * 100), map_size)

            progress_message = await client.edit_message(
                progress_message,
                message_content + f'\nProgress: {progress_string}')

        # for room in room_list:
        #     for other_room in room_list:
        #
        #         hallway = None
        #
        #         # if room is underneath or above other_room
        #         if cls.check_horizontal_alignment(room, other_room) and \
        #                 random.uniform(0, 1) < connectivity:
        #
        #             hallway_pos = Vector2.zero()
        #             hallway_size = Vector2(2, 0)
        #
        #             # if room is below other_room
        #             if room.position.y - other_room.position.y < 0:
        #                 if other_room.position.y - room.position.y < 15 > 2 and \
        #                         room.doors['north'] and other_room.doors['south']:
        #
        #                     hallway_pos.y = room.position.y + room.size_vector.y
        #                     hallway_size.y = other_room.position.y - room.position.y + room.size_vector.y
        #
        #                     if hallway_size.y < 2:
        #                         continue
        #                 else:
        #                     continue
        #
        #             # if room is above other_room
        #             elif room.position.y - other_room.position.y > 0:
        #                 if room.position.y - other_room.position.y < 15 > 2 and \
        #                         room.doors['south'] and other_room.doors['north']:
        #
        #                     hallway_pos.y = other_room.position.y + other_room.size_vector.y
        #                     hallway_size.y = room.position.y - other_room.position.y + other_room.size_vector.y
        #
        #                     if hallway_size.y < 2:
        #                         continue
        #                 else:
        #                     continue
        #
        #             hallway_pos.x = ((room.position.x + room.size_vector.x / 2) +
        #                              (other_room.position.x + other_room.size_vector.x / 2)) // 2
        #
        #             hallway = Room.generate_room(doors={
        #                 'north': True,
        #                 'south': True,
        #                 'east': False,
        #                 'west': False},
        #                 level=int(level/2),
        #                 size_vector=hallway_size,
        #                 position=hallway_pos
        #             )
        #
        #             if hallway.check_all_collision(room_list):
        #                 hallway = None
        #             else:
        #                 logger.debug(f'Created vertical hallway between {hallway.position.y} and {hallway.position.y + hallway.size_vector.y}')
        #
        #         # if room is to the left or right of other_room
        #         if cls.check_vertical_alignment(room, other_room) and \
        #                 random.uniform(0, 1) < connectivity:
        #
        #             hallway_pos = Vector2.zero()
        #             hallway_size = Vector2(0, 2)
        #
        #             # if room is to the left of other room
        #             if room.position.x - other_room.position.x < 0:
        #                 if other_room.position.x - room.position.x < 15 and \
        #                         room.doors['east'] and other_room.doors['west']:
        #
        #                     hallway_pos.x = room.position.x + room.size_vector.x
        #                     hallway_size.x = other_room.position.x - room.position.x + room.size_vector.x
        #
        #                     if hallway_size.x < 2:
        #                         continue
        #                 else:
        #                     continue
        #
        #             # if room is to the right of other_room
        #             else:
        #                 if room.position.x - other_room.position.x < 15 and \
        #                         room.doors['west'] and other_room.doors['east']:
        #
        #                     hallway_pos.x = other_room.position.x + other_room.size_vector.x
        #                     hallway_size.x = room.position.x - other_room.position.x + other_room.size_vector.x
        #
        #                     if hallway_size.x < 2:
        #                         continue
        #                 else:
        #                     continue
        #
        #             hallway_pos.y = ((room.position.y + room.size_vector.y / 2) +
        #                              (other_room.position.y + other_room.size_vector.y / 2)) // 2
        #
        #             hallway = Room.generate_room(doors={
        #                 'north': False,
        #                 'south': False,
        #                 'east': True,
        #                 'west': True},
        #                 level=int(level / 2),
        #                 size_vector=hallway_size,
        #                 position=hallway_pos
        #             )
        #
        #             if hallway.check_all_collision(room_list):
        #                 hallway = None
        #             else:
        #                 logger.debug(f'Created horizontal hallway between {hallway.position.x} and {hallway.position.x + hallway.size_vector.x}')
        #
        #         if hallway:
        #             room_list.append(hallway)
        #             await asyncio.sleep(0.1)

        room_list.sort(key=Map.room_level_sort_key)

        for room in room_list:
            populated_room = Room.generate_room(
                room.doors,
                room.size_vector,
                room.level,
                biome=biome,
                position=room.position,
                entrance_direction=room.entrance_direction,
                exit_directions=room.exit_directions)
            room_list.remove(room)
            room_list.append(populated_room)

        generation_time = datetime.datetime.now() - start_time
        logger.info(f'Map of size {map_size} generated in {generation_time}')

        return cls(level, biome, room_list)
Ejemplo n.º 3
0
 def empty(cls, size_vector=Vector2.zero(), position=Vector2.zero()):
     return cls(None, size_vector, None, None, None, None, position, None,
                None)