Ejemplo n.º 1
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities, colors):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)

            # random position without going out of boundaries
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # 'rect' class makes rectangles easy
            new_room = Rect(x, y, w, h)

            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # no intersections, it's a good room
                self.create_room(new_room)

                # center coordinates of new room
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # this means it is first room
                    player.x = new_x
                    player.y = new_y
                else:
                    # all rooms after first
                    # connec to previous room

                    # center coords of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # flip a coin (random 0, or 1)
                    if randint(0, 1) == 1:
                        # make horizontal tunnel first
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # make vertical tunnel first
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # add monsters to the room
                self.place_entities(new_room, entities, colors)

                # append new room to rooms
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             tcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 2
0
def make_map(game_map,
             max_rooms,
             room_min_size,
             room_max_size,
             map_width,
             map_height,
             player,
             entities,
             colors,
             lexicon,
             direction=None):
    rooms = []
    num_rooms = 0

    center_of_last_room_x = None
    center_of_last_room_y = None

    for r in range(max_rooms):
        # random width and height
        w = randint(room_min_size, room_max_size)
        h = randint(room_min_size, room_max_size)
        # random position without going out of the boundaries of the map
        x = randint(0, map_width - w - 1)
        y = randint(0, map_height - h - 1)

        # "Rect" class makes rectangles easier to work with
        new_room = Rect(x, y, w, h)

        # Run through the other rooms and see if they intersect  with this one
        for other_room in rooms:
            if new_room.intersect(other_room):
                break
        else:
            # this means there are no intersections, so this room is valid

            # paint it to the map's tiles
            create_room(game_map, new_room)

            # center coordinates of new room, will be useful later
            (new_x, new_y) = new_room.center()

            center_of_last_room_x = new_x
            center_of_last_room_y = new_y

            if num_rooms == 0:
                # this is the first room, where the player starts at
                player.x = new_x
                player.y = new_y
            else:
                # all rooms after the first:
                # connect it to the previous room with a tunnel

                # center coordinates of previous room
                (prev_x, prev_y) = rooms[num_rooms - 1].center()

                # flip a coin (random number that is either 0 or 1)
                if randint(0, 1):
                    # first move horizontally, then vertically
                    create_h_tunnel(game_map, prev_x, new_x, prev_y)
                    create_v_tunnel(game_map, prev_y, new_y, new_x)
                else:
                    # first move vertically, then horizontally
                    create_v_tunnel(game_map, prev_y, new_y, prev_x)
                    create_h_tunnel(game_map, prev_x, new_x, new_y)

            place_entities(new_room, entities, game_map.dungeon_level, colors,
                           lexicon)

            # finally, append the new room to the list
            rooms.append(new_room)
            num_rooms += 1

    if direction == 'down':
        down_stairs_component = Stairs(game_map.dungeon_level + 1, 'down')
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>', (255, 255, 255),
                             'Down Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=down_stairs_component)
        entities.append(down_stairs)

        up_stairs_component = Stairs(game_map.dungeon_level - 1, 'up')
        up_stairs = Entity(player.x,
                           player.y,
                           '<', (255, 255, 255),
                           'Up Stairs',
                           render_order=RenderOrder.STAIRS,
                           stairs=up_stairs_component)
        entities.append(up_stairs)

    elif direction == 'up':
        down_stairs_component = Stairs(game_map.dungeon_level + 1, 'down')
        down_stairs = Entity(player.x,
                             player.y,
                             '>', (255, 255, 255),
                             'Down Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=down_stairs_component)
        entities.append(down_stairs)

        up_stairs_component = Stairs(game_map.dungeon_level - 1, 'up')
        up_stairs = Entity(center_of_last_room_x,
                           center_of_last_room_y,
                           '<', (255, 255, 255),
                           'Up Stairs',
                           render_order=RenderOrder.STAIRS,
                           stairs=up_stairs_component)
        entities.append(up_stairs)

    else:
        down_stairs_component = Stairs(game_map.dungeon_level + 1, 'down')
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>', (255, 255, 255),
                             'Down Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=down_stairs_component)
        entities.append(down_stairs)

    # choose a random room from all rooms except the first and the last, to put a fountain of health
    fountain_location = rooms[randint(1, len(rooms) - 2)]
    (fountain_x, fountain_y) = fountain_location.center()
    fountain_component = Fountain(20 * game_map.dungeon_level)
    fountain = Entity(fountain_x,
                      fountain_y,
                      '+',
                      colors.get('blue'),
                      'Fountain',
                      render_order=RenderOrder.ITEM,
                      fountain=fountain_component)
    entities.append(fountain)
Ejemplo n.º 3
0
    def make_map(self, max_rooms: int, room_min_size: int, room_max_size: int, map_width: int,
                 map_height: int, player: Entity):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)

            x = randint(0, map_width - w - 2)
            y = randint(0, map_height - h - 2)

            new_room = Rect(x, y, w, h)

            for other_room in rooms:
                if new_room.intersect(other_room):
                    break

            else:
                self.dig_room(new_room)

                new_x, new_y = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y
                    self.start_x = new_x
                    self.start_y = new_y
                else:
                    other = find_nearest_room(new_room, rooms)

                    prev_x, prev_y = other.center()
                    # new_x, new_y = new_room.center()

                    # print(f"\t\tNearest room to {(new_x, new_y)} is {(prev_x, prev_y)}")

                    if randint(0, 1) == 1:
                        self.dig_h_tunnel(new_x, prev_x, new_y)
                        self.dig_v_tunnel(prev_x, prev_y, new_y)
                    else:
                        self.dig_v_tunnel(new_x, new_y, prev_y)
                        self.dig_h_tunnel(new_x, prev_x, prev_y)

                if num_rooms > 0:
                    self.place_entities(new_room)

                rooms.append(new_room)
                num_rooms += 1

        self.end_x = center_of_last_room_x
        self.end_y = center_of_last_room_y

        down_stairs_component = Stairs(1)
        down_stairs = Entity("Stairs", EntityType.STAIRS, center_of_last_room_x, center_of_last_room_y, ord('>'),
                             (255, 255, 255), stairs=down_stairs_component)
        self.entities.append(down_stairs)

        if self.dungeon_level > 1:
            up_stairs_component = Stairs(-1)
            up_stairs = Entity("Stairs", EntityType.STAIRS, player.x, player.y, ord('<'),
                               (255, 255, 255), stairs=up_stairs_component)
            self.entities.append(up_stairs)
Ejemplo n.º 4
0
def make_map(game_map, max_rooms, room_min_size, room_max_size, map_width,
             map_height, player, entities, colors):
    rooms = []
    num_rooms = 0

    center_of_last_room_x = None
    center_of_last_room_y = None

    for r in range(max_rooms):
        # random width and height
        w = randint(room_min_size, room_max_size)
        h = randint(room_min_size, room_max_size)
        # random position withut going outside bounds of the map
        x = randint(0, map_width - w - 1)
        y = randint(0, map_height - h - 1)

        # "Rect" class makes rectangles easier to work with
        new_room = Rect(x, y, w, h)

        # run through other rooms and see if intersection occurs
        for other_room in rooms:
            if new_room.intersect(other_room):
                break
        else:
            # no intersections

            # 'paint' to map's tiles
            create_room(game_map, new_room)

            # center coordinates of new room
            (new_x, new_y) = new_room.center()

            center_of_last_room_x = new_x
            center_of_last_room_y = new_y

            if num_rooms == 0:
                #this is the first room, where the player starts
                player.x = new_x
                player.y = new_y
            else:
                # all rooms after first
                # connect to previous room with tunnel

                # center coordinates of previous room
                (prev_x, prev_y) = rooms[num_rooms - 1].center()

                # flip a coin (random number that is either 0 or 1)
                if randint(0, 1) == 1:
                    # first move horizontally, then vertically
                    create_h_tunnel(game_map, prev_x, new_x, prev_y)
                    create_v_tunnel(game_map, prev_y, new_y, new_x)
                else:
                    # first move vertically, then horizontally
                    create_v_tunnel(game_map, prev_y, new_y, prev_x)
                    create_h_tunnel(game_map, prev_x, new_x, new_y)

            place_entities(new_room, entities, game_map.dungeon_level, colors)

            #finally, append new room to the list
            rooms.append(new_room)
            num_rooms += 1
    stairs_component = Stairs(game_map.dungeon_level + 1)
    down_stairs = Entity(center_of_last_room_x,
                         center_of_last_room_y,
                         '>', (255, 255, 255),
                         'Stairs',
                         render_order=RenderOrder.STAIRS,
                         stairs=stairs_component)
    entities.append(down_stairs)
Ejemplo n.º 5
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        """
        Creates the map. Chisels out all of the rooms and paths. Places all entities.
        Places a stairway in the middle of the last room.
        :param max_rooms: Integer.
        :param room_min_size: Integer.
        :param room_max_size: Integer.
        :param map_width: Integer.
        :param map_height: Integer.
        :param player: The player's entity object.
        :param entities: The list of entities
        :param max_monsters_per_room: Integer of max monsters per room.
        :return: None
        """

        # Stores the rooms in a list.
        rooms = []
        # Counts the number of rooms currently created.
        num_rooms = 0

        # Stores the centre of the last room to place the stairs here.
        centre_of_last_room_x = None
        centre_of_last_room_y = None

        ####### SPAWNS ROOMS #######
        for r in range(max_rooms):
            # Generates a random room width and height.
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # Generates a random position without going outside the map boundaries.
            # Subtracts 1 because the top corner is a wall tile not the inside of the room -
            # Without subtracting 1 the outer right/bottom wall could be destroyed.
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # Makes a new rectangular room using the newly generated values.
            new_room = Rect(x, y, w, h)

            # Runs through the other rooms and checks if they intersects with this one.
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # NICE USE OF THIS FOR/ELSE! If the for loop doesn't break else is ran.
                # If the new room did not intersect with any other room it will be built.

                # Creates the room.
                self.create_room(new_room)

                # Stores the centre coordinates of the new room.
                (new_x, new_y) = new_room.center()

                # Will end up storing the centre of the last room for stair placement.
                centre_of_last_room_x = new_x
                centre_of_last_room_y = new_y

                # Puts the player in the centre of the first room.
                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y
                # If the number of rooms in the list is NOT 0.
                else:
                    # All rooms after the first.
                    # Connect it to the previous room with a tunnel.

                    # Centre coordinates of the previous room.
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # Flip a coin (Random number that is either 0 or 1)
                    if randint(0, 1) == 1:
                        # First move horizontally, then vertically.
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # First move vertically, then horizontally.
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # Spawns entities (the player was already spawned earlier).
                self.place_entities(new_room, entities)

                # Finally, append the new room to the room list.
                rooms.append(new_room)
                num_rooms += 1

        # Creates the stairs component - sets its level to the next dungeon level directly below.
        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(centre_of_last_room_x,
                             centre_of_last_room_y,
                             ">",
                             libtcod.white,
                             "Stairs",
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 6
0
    def make_map(self, max_rooms, room_max_size, room_min_size, map_width,
                 map_height, player, entities):

        rooms = []
        num_rooms = 0

        center_of_last_room_x = 0
        center_of_last_room_y = 0

        for r in range(max_rooms):
            # generates random room size
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)

            # generates random room location in the map and not overlapping
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            new_room = Rect(x, y, w, h)

            # checks for room intersections
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # if no intersections
                self.create_room(new_room)
                (new_x, new_y) = new_room.center()

                if num_rooms == 0:
                    # the first room contains the player in its center
                    player.x = new_x
                    player.y = new_y
                else:
                    # finds center of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    center_of_last_room_x = new_x
                    center_of_last_room_y = new_y

                    # flips a coin to find order of tunnel generation
                    if randint(0, 1) == 0:
                        # moves horizontally then vertically
                        self.create_horizontal_tunnel(prev_x, new_x, prev_y)
                        self.create_vertical_tunnel(prev_y, new_y, new_x)
                    else:
                        # moves vertically then horizontally
                        self.create_horizontal_tunnel(prev_x, new_x, new_y)
                        self.create_vertical_tunnel(prev_y, new_y, prev_x)

                self.place_entities(new_room, entities)
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             ">",
                             libtcod.white,
                             "Stairs",
                             False,
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 7
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities,):
        """
        Create rooms for dungeon map
        """
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)

            # random position without going outside the boundaries of the map
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # 'Rect' class to make rooms
            new_room = Rect(x, y, w, h)

            # Run through other rooms to make sure it doesnt intersect
            for other_rooms in rooms:
                if new_room.intersect(other_rooms):
                    break
            else:
                # This will action if there are no intersections

                # unblock map tiles
                self.create_room(new_room)

                # center coordinates for new room
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # Upon creation of first room
                    player.x = new_x
                    player.y = new_y

                else:
                    """
                    for all rooms after the first one
                    connect the current room to the previous room
                    """
                    # Coordinates of the previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()
                    # Randomise the direction of the tunnel 1 == horizontal
                    if randint(0, 1) == 1:
                        # Move horizontal then vertical
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # move vertical then horizontal
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities, )

                # Finally add the new room to the room list
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', tcod.white, 'stairs',
                             render_order=RenderOrder.STAIRS, stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 8
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # Random height and width of room
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # Random position within boundaries
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            new_room = Rect(x, y, w, h)
            for other_room in rooms:
                if new_room.intersects(other_room):
                    break
            else:
                self.create_room(new_room)
                (new_x, new_y) = new_room.center()
                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if len(rooms) == 0:
                    # Start player in the first room
                    player.x = new_x
                    player.y = new_y
                    print("player starting position set")
                else:
                    # All rooms after the first:
                    # connect it to the prev one with a tunnel

                    # Center coords of prev room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # Flip a coin whether to go vert or hor first
                    if randint(0, 1) == 1:
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(new_x, prev_y, new_y)
                    else:
                        self.create_v_tunnel(prev_x, prev_y, new_y)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # Append the new room to the list of rooms
                rooms.append(new_room)
                self.place_entities(new_room, entities)

                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             tcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 9
0
    def make_map(self, player, entities):
        start_time = time.time()
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        # Essentially, this algorithm generates a random room, and checks to see if it intesects with the generated map (if it does, it breaks)
        # If not, it generates the room, and using the center of that room, connects that room with a tunnel to the previously generated room
        # if it's the first room, it plops the player in the middle of it
        # Then fill it with stuff and keep going
        for r in range(game_constants.max_rooms):
            w = randint(game_constants.room_min_size,
                        game_constants.room_max_size)
            h = randint(game_constants.room_min_size,
                        game_constants.room_max_size)

            x = randint(0, game_constants.map_width - w - 1)
            y = randint(0, game_constants.map_height - h - 1)
            new_room = Rect(x, y, w, h)

            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                self.create_room(new_room)
                (new_x, new_y) = new_room.center()
                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y
                    libtcod.mouse_move(player.x, player.y)
                else:
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    if randint(0, 1) == 1:
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)
                self.populate_room(new_room, entities)
                rooms.append(new_room)
                num_rooms += 1

        # Once all of the rooms have been generated, plot the stairs in the middle of the last room created
        stairs_components = {"Stairs": Stairs(self.dungeon_level + 1)}
        stairs_entity = Entity(center_of_last_room_x,
                               center_of_last_room_y,
                               ">",
                               libtcod.white,
                               "Stairs Down",
                               blocks=True,
                               render_order=RenderOrder.ACTOR,
                               message_log=self.log,
                               state=AIStates.INANIMATE,
                               components=stairs_components)
        self.tiles[center_of_last_room_x][center_of_last_room_y].add_entity(
            stairs_entity)
        entities.insert_entity(stairs_entity)
        self.compute_dijkstra_map([stairs_entity], "stairs")

        print("Map generated in {} seconds".format(time.time() - start_time))
Ejemplo n.º 10
0
    def make_map(self, max_rooms: int, room_min_size: int, room_max_size: int,
                 map_width: int, map_height: int, player, entities):
        rooms: List[Rect] = []
        num_rooms: int = 0

        item_components = get_item_classes()
        center_of_last_room_x: int
        center_of_last_room_y: int

        for r in range(max_rooms):
            w: int = randint(room_min_size, room_max_size)
            h: int = randint(room_min_size, room_max_size)
            x: int = randint(0, map_width - w - 1)
            y: int = randint(0, map_height - h - 1)

            new_room: Rect = Rect(x, y, w, h)

            # for-else loop
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # this means there are no intersections, so this room is valid
                # "paint" it to the map's tiles
                self.create_room(new_room)

                # center coordinates of new room, will be useful later
                (new_x, new_y) = new_room.center()
                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # this is the first room, where the player starts at
                    player.pos.x = new_x
                    player.pos.y = new_y
                else:
                    # all rooms after the first:
                    # connect it to the previous room with a tunnel

                    # center coordinates of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # flip a coin (random number that is either 0 or 1)
                    if randint(0, 1) == 1:
                        # first move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # first move vertically, then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                    self.place_entities(new_room, entities, item_components)

                # finally, append the new room to the list
                rooms.append(new_room)
                num_rooms += 1
        stairs_component = Stairs(self.dungeon_level + 1)
        pos_comp = Position(center_of_last_room_x, center_of_last_room_y)
        down_stairs = Entity('>',
                             tcod.white,
                             'Stairs',
                             position=pos_comp,
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 11
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities, max_monsters_per_room,
                 max_items_per_room):

        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random room size
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # random room location within boundaries
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            new_room = Rect(x, y, w, h)

            # make sure rooms don't intersect
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # no intersections with any other rooms - place it
                self.create_room(new_room)

                # place player in center of first valid room

                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y
                else:
                    # we'll need to connect rooms with tunnels

                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    if randint(0, 1) == 1:
                        # hor -> vert
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # vert -> hor
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities, max_monsters_per_room,
                                    max_items_per_room)

                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             libtcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 12
0
    def make_map(
        self,
        max_rooms,
        room_min_size,
        room_max_size,
        map_width,
        map_height,
        player,
        entities,
        max_monsters_per_room,
        max_items_per_room,
    ):
        """ Given max num of rooms: create them + connect with tunnels

        """

        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for _ in range(max_rooms):
            # random width and height
            width = randint(room_min_size, room_max_size)
            height = randint(room_min_size, room_max_size)
            # random position without going out of the bounds of the map
            x_pos = randint(0, map_width - width - 1)
            y_pos = randint(0, map_height - height - 1)

            new_room = Rect(x_pos, y_pos, width, height)

            # run through other rooms to see if they intersect with this one
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # valid room
                self.create_room(new_room)

                # center the coordinates of the new room
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # this is the first room, where the player starts
                    player.x_pos = new_x
                    player.y_pos = new_y
                else:
                    # all rooms after the first
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # flip a coin
                    if randint(0, 1) == 1:
                        # move horizontally, then vertically
                        self.create_horizontal_tunnel(prev_x, new_x, prev_y)
                        self.create_vertical_tunnel(prev_y, new_y, new_x)
                    else:
                        # move vertically, then horizontally
                        self.create_vertical_tunnel(prev_y, new_y, prev_x)
                        self.create_horizontal_tunnel(prev_x, new_x, new_y)

                # add monsters
                self.place_entities(new_room, entities, max_monsters_per_room,
                                    max_items_per_room)

                # append new room to the list
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             tcod.white,
                             'Stairs',
                             RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 13
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities, max_monsters_per_room,
                 max_items_per_room):
        # Create rooms
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # Random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # Random position without going out of the boundaries of the map
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # Make the rectangle to work with
            new_room = Rect(x, y, w, h)

            # Check to see if new room intersects with any existing rooms
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break

            # No intersections, so we can make the room
            else:
                # "Paint" the room to the map's tiles
                self.create_room(new_room)

                new_x, new_y = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                # If we are making the first room, put the player in it
                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y

                # All rooms after the first one
                else:
                    prev_x, prev_y = rooms[num_rooms - 1].center()

                    # 50/50 chance at which order we connect the new room to the previous
                    if randint(0, 1) == 1:
                        # Make tunnel horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # Make tunnel vertically then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # Add monsters to the room
                self.place_entities(new_room, entities, max_monsters_per_room,
                                    max_items_per_room)
                # Append the new room to the list
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             libtcod.white,
                             "Stairs",
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 14
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        """Randomly generates rooms and tunnels between them based on the map dimensions,
        and also sets the player's position to be the center of the first room.
        """
        rooms = []  # List of room objects
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # random position without leaving the map boundaries
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # 'Rect' class makes rectangles easier to work with
            new_room = Rect(x, y, w, h)

            # go through other rooms and check if they intersect
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:  # Python for-else: the else runs if for doesn't break!
                # No intersections: room is valid - so actually create it!
                self.create_room(new_room)
                # center coordinates of new room, for later
                new_x, new_y = new_room.center()
                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # This is the first room, where the player starts
                    player.x = new_x
                    player.y = new_y
                else:
                    # all rooms after the first: connect to previous with a tunnel
                    prev_x, prev_y = rooms[num_rooms - 1].center()
                    # flip a coin on moving horiz then vert, or vert then horiz
                    if randint(0, 1) == 1:
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # Place entities into the room
                self.place_entities(new_room, entities)

                # finally, append the new room to the list
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             libtcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 15
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, pet, direction):
        rooms = []
        num_rooms = 0

        if self.dungeon_level < 6:
            self.biome = 'The Dungeon'
        elif self.dungeon_level < 11:
            self.biome = 'The Icebleak Cavern'
        elif self.dungeon_level < 16:
            self.biome = 'The Underglade'
        elif self.dungeon_level < 21:
            self.biome = 'The Hadalrealm'
        elif self.dungeon_level < 24:
            self.biome = 'Dragonroost'
        elif self.dungeon_level == 24:
            self.biome = 'Oblivion\'s Gate'
        elif self.dungeon_level == 25:
            self.biome = 'The Vault'

        center_of_last_room_x = None
        center_of_last_room_y = None
        if self.dungeon_level < 24:
            for r in range(max_rooms):
                # random width and height
                w = libtcod.random_get_int(0, room_min_size, room_max_size)
                h = libtcod.random_get_int(0, room_min_size, room_max_size)
                # random position without going out of the boundaries of the map
                x = libtcod.random_get_int(0, 0, map_width - w - 1)
                y = libtcod.random_get_int(0, 0, map_height - h - 1)

                # "Rect" class makes rectangles easier to work with
                new_room = Rect(x, y, w, h)

                # run through the other rooms and see if they intersect with this one
                for other_room in rooms:
                    if new_room.intersect(other_room):
                        break
                else:
                    # this means there are no intersections, so this room is valid

                    # "paint" it to the map's tiles
                    self.create_room(new_room)

                    # center coordinates of new room, will be useful later
                    (new_x, new_y) = new_room.center()

                    center_of_last_room_x = new_x
                    center_of_last_room_y = new_y

                    if num_rooms == 0:
                        # this is the first room, where the player starts at
                        player.x = new_x
                        player.y = new_y
                        player_room = new_room
                    else:
                        # all rooms after the first:
                        # connect it to the previous room with a tunnel

                        # center coordinates of previous room
                        (prev_x, prev_y) = rooms[num_rooms - 1].center()

                        self.create_tunnel(prev_x, new_x, prev_y, new_y)

                    if new_room != player_room:
                        place_entities(self, new_room, entities)

                    # finally, append the new room to the list
                    rooms.append(new_room)
                    num_rooms += 1
        else:
            for i in range(0, 3):
                room = Rect(int(i*(map_width/4)), 0, int(map_width/4), map_height - 1)
                room2 = Rect(0, int(i*(map_height/3)), map_width - 1, int(map_height/3))
                self.create_room(room)
                self.create_room(room2)
                player.x, player.y = 10, 10
                num_rooms += 2
                rooms.append(room)
                rooms.append(room2)
            room = Rect(int(3 * (map_width / 4)), 0, int(map_width / 4) - 1, map_height - 1)
            self.create_room(room)
            num_rooms += 1
            rooms.append(room)
            center_of_last_room_x, center_of_last_room_y = 40, 21
        if direction == 'down':
            stairs_component = Stairs(self.dungeon_level + 1)
            down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', libtcod.white, 'Down Stairs',
                                 render_order=RenderOrder.STAIRS, stairs=stairs_component)
            stairs_component = Stairs(self.dungeon_level - 1)
            up_stairs = Entity(player.x, player.y, '<', libtcod.white, 'Up Stairs',
                                 render_order=RenderOrder.STAIRS, stairs=stairs_component)
        if direction == 'up':
            stairs_component = Stairs(self.dungeon_level + 1)
            down_stairs = Entity(player.x, player.y, '>', libtcod.white, 'Down Stairs',
                                 render_order=RenderOrder.STAIRS, stairs=stairs_component)
            stairs_component = Stairs(self.dungeon_level - 1)
            up_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '<', libtcod.white, 'Up Stairs',
                               render_order=RenderOrder.STAIRS, stairs=stairs_component)
        if self.dungeon_level != 25:
            entities.append(down_stairs)
        if self.dungeon_level != 1:
            entities.append(up_stairs)
        if self.dungeon_level == 24:
            fighter_component = Fighter(hp=300, defence=50, power=30)
            ai_component = BasicMonster()
            monster = Entity(40, 21, 'W', libtcod.white, 'DragonLord', blocks=True,
                             render_order=RenderOrder.ACTOR, fighter=fighter_component, ai=ai_component,
                             poisons_chance=100, burns_chance=100, freezes_chance=25, monster_class='(Dragon)')
            entities.append(monster)

        if pet:

            ai_component = BasicPet()
            fighter_component = Fighter(hp=pet.fighter.hp + 50, power=self.dungeon_level, defence=int(self.dungeon_level/2))
            pet = Entity(player.x - 1, player.y, 'P', libtcod.blue, 'Pet', render_order=RenderOrder.ACTOR, fighter=fighter_component,
                         ai=ai_component, blocks=True, monster_class='(Pet)')
            entities.append(pet)
            return pet
Ejemplo n.º 16
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):

        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # random position without going out of the boundarties of the map
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            new_room = Rect(x, y, w, h)

            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                self.create_room(new_room)
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:  # First room, player starts here.
                    player.x = new_x
                    player.y = new_y
                else:
                    # all rooms after the first:
                    # connect it to the previous room with tunnel.
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # flip a coin
                    if randint(0, 1) == 1:
                        # first move horizontal and then vertical
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # do the opposite
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities)

                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             tcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 17
0
    def make_map(self,
                 max_rooms,
                 room_min_size,
                 room_max_size,
                 map_width,
                 map_height,
                 player,
                 entities):
        rooms = []

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            width = randint(room_min_size, room_max_size)
            height = randint(room_min_size, room_max_size)

            # random position without going out of bounds
            x = randint(0, map_width - width - 1)
            y = randint(0, map_height - height - 1)

            # create rectangle from specs
            new_room = Rect(x, y, width, height)

            # run through other rooms and check for intersections
            for other_room in rooms:
                if new_room.is_intersecting(other_room):
                    break
            else:
                # no intersections
                # make room walkable
                self.create_room(new_room)

                # get center coordinates of new room
                (new_center_x, new_center_y) = new_room.get_center_coords()

                center_of_last_room_x = new_center_x
                center_of_last_room_y = new_center_y

                if len(rooms) == 0:
                    # starting room for player
                    player.x = new_center_x
                    player.y = new_center_y
                else:
                    # connect new room to previous room
                    (prev_center_x, prev_center_y) = rooms[-1].get_center_coords()

                    # create random tunnel
                    if randint(0, 1):
                        # move horizontally then vertically
                        self.create_horiz_tunnel(prev_center_x, new_center_x, prev_center_y)
                        self.create_vert_tunnel(prev_center_y, new_center_y, new_center_x)
                    else:
                        # move vertically then horizontally
                        self.create_vert_tunnel(prev_center_y, new_center_y, prev_center_x)
                        self.create_horiz_tunnel(prev_center_x, new_center_x, new_center_y)

                # fill with monsters
                self.place_entities_in_room(new_room, entities)

                # append new room to list
                rooms.append(new_room)

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(
            center_of_last_room_x,
            center_of_last_room_y,
            '>',
            libtcod.white,
            "Stairs Down",
            render_order=RenderOrder.STAIRS,
            stairs=stairs_component
        )
        entities.append(down_stairs)
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        #Creates up to 'max_rooms' and connects them all together

        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            #random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            #random position with map boundaries
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            #Make our rooms Rect objects to handle them better
            new_room = Rect(x, y, w, h)

            #Make sure our new room does not intersect with our other rooms
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                #We did not intersect so we can create our room

                #first carve it out from our tile map
                self.create_room(new_room)

                #then get its center coords
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y
                else:
                    #All rooms besides first need to connect to
                    #the last created room

                    #get previous rooms center coords
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    if randint(0, 1) == 1:
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities)
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             libtcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 19
0
    def from_json(json_data):
        x = json_data.get('x')
        y = json_data.get('y')
        char = json_data.get('char')
        color_o = json_data.get('color')
        color = libtcod.Color(color_o[0], color_o[1], color_o[2])
        name = json_data.get('name')
        blocks = json_data.get('blocks', False)
        render_order = RenderOrder(json_data.get('render_order'))
        fighter_json = json_data.get('fighter')
        ai_json = json_data.get('ai')
        item_json = json_data.get('item')
        inventory_json = json_data.get('inventory')
        stairs_json = json_data.get('stairs')
        level_json = json_data.get('level')
        equipment_json = json_data.get('equipment')
        equippable_json = json_data.get('equippable')
        uID_data = json_data.get('uID')
        quickuse_json = json_data.get('quick_use')

        entity = Entity(x,
                        y,
                        char,
                        color,
                        name,
                        blocks,
                        render_order,
                        uID=uID_data)

        if fighter_json:
            entity.fighter = Fighter.from_json(fighter_json)
            entity.fighter.owner = entity

        if ai_json:
            name = ai_json.get('name')

            if name == BasicMonster.__name__:
                ai = BasicMonster.from_json()
            elif name == ConfusedMonster.__name__:
                ai = ConfusedMonster.from_json(ai_json, entity)
            else:
                ai = None

            if ai:
                entity.ai = ai
                entity.ai.owner = entity

        if item_json:
            entity.item = Item.from_json(item_json)
            entity.item.owner = entity

        if inventory_json:
            entity.inventory = Inventory.from_json(inventory_json)
            entity.inventory.owner = entity

        if stairs_json:
            entity.stairs = Stairs.from_json(stairs_json)
            entity.stairs.owner = entity

        if level_json:
            entity.level = Level.from_json(level_json)
            entity.level.owner = entity

        if equipment_json:
            entity.equipment = Equipment.from_json(equipment_json)
            entity.equipment.owner = entity

        if equippable_json:
            entity.equippable = Equippable.from_json(equippable_json)
            entity.equippable.owner = entity

        if quickuse_json:
            entity.quick_use = Quickuse.from_json(quickuse_json)
            entity.quick_use.owner = entity

        return entity
Ejemplo n.º 20
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for _ in range(max_rooms):
            # ランダムで幅と高さを決める
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # マップの範囲内でランダムな位置の指定
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)
            # "Rect" クラスで部屋を生成する
            new_room = Rect(x, y, w, h)

            # 部屋と部屋が重複するかの判定
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break

            else:
                # 重複が無ければ部屋を作成
                self.create_room(new_room)

                # 作成した部屋の中心座標を変数に格納
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # 初回はとりあえずplayerが居る最初の部屋の作成だけしてroomsリストに追加しnum_roomsに+1する
                    player.x = new_x
                    player.y = new_y

                else:
                    # 二回目以降に前の部屋の中心座標を変数prev_x,yに入れる(当然新しい部屋の座標はnew_x,yに入っている)
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # 前の部屋の中心座標と現在の部屋の中心座標からトンネル関数で部屋を彫っていく
                    if randint(0, 1) == 1:
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                # ここでモンスターたちを部屋に配置する
                self.place_entities(new_room, entities)

                # 新しい部屋をリストに追加する
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             ">",
                             libtcod.white,
                             "Stairs",
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)

        entities.append(down_stairs)
Ejemplo n.º 21
0
    def make_map(self, entities, moving_down=True):
        if self.encounter:
            self.encounter.setup_map_boundaries(0, self.owner.width - 1, 0, self.owner.height - 1)
            self.encounter.make_rect()
            rooms = [self.encounter.rect]
            self.create_room(rooms[0])
            num_rooms = 1
        else:
            rooms = []
            num_rooms = 0

        for _ in range(self.max_rooms):
            # random width and height
            w = randint(self.room_min_size, self.room_max_size)
            h = randint(self.room_min_size, self.room_max_size)
            # random position without going out of the boundaries of the map
            x = randint(0, self.owner.width - w - 1)
            y = randint(0, self.owner.height - h - 1)

            new_room = Rect(x, y, w, h)
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # this means there are no intersections, so this room is valid
                self.create_room(new_room)
                new_x, new_y = new_room.center()

                if num_rooms == 0 or (self.encounter and num_rooms == 1):
                    self.player_start = (new_x, new_y)
                    self.place_player(entities.player)
                else:
                    # all rooms after the first:
                    # connect it to the previous room with a tunnel
                    self.connect_room(new_room, rooms[num_rooms - 1])

                self.fauna.populate_room(new_room, self.owner.dungeon_level, entities)
                self.loot.fill_room(new_room, self.owner.dungeon_level, entities)
                self.flora.grow_fungi_in_room(self.owner, new_room)

                rooms.append(new_room)
                num_rooms += 1

        if moving_down:
            down_stairs_x = new_x
            down_stairs_y = new_y

            up_stairs_x = entities.player.x
            up_stairs_y = entities.player.y
        else:
            down_stairs_x = entities.player.x
            down_stairs_y = entities.player.y

            up_stairs_x = new_x
            up_stairs_y = new_y

        if not self.encounter:
            stairs_component = Stairs(self.owner.dungeon_level + 1)
            down_stairs = Entity(down_stairs_x, down_stairs_y, char='>', color=tcod.white,
                                    name='Stairs down', render_order=RenderOrder.STAIRS,
                                    stairs=stairs_component)
            entities.append(down_stairs)

        if self.owner.dungeon_level == 1:
            direction = StairsDirections.WORLD
            stairs_name = 'Stairs outside'
        else:
            direction = StairsDirections.UP
            stairs_name = 'Stairs up'

        up_stairs_component = Stairs(self.owner.dungeon_level, direction=direction)
        up_stairs = Entity(up_stairs_x, up_stairs_y, char='<', color=tcod.white,
                            name=stairs_name, render_order=RenderOrder.STAIRS,
                            stairs=up_stairs_component)
        entities.append(up_stairs)

        if self.encounter:
            self.connect_room(rooms[0], rooms[randint(1, len(rooms) - 1)])
            self.encounter.create_on(self.owner, entities)

        for x in range(self.owner.width):
            for y in range(self.owner.height):
                tile = self.owner.tiles[x][y]
                if 'blocked' in tile.regulatory_flags:
                    tile.set_bg_color(self.material['wall'])
                else:
                    tile.set_bg_color(self.material['floor'])
Ejemplo n.º 22
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities, graphics):
        """
        Cree un nombre de salles inferieur a un nombre fixe (s'arrete lorsque la generation aleatoire cree
        deux pieces qui s'intersectent. Place le joueur dans la premiere d'entre elles, puis connecte la nouvelle
        salle avec la precedente et enfin ajoute les escaliers a la liste des entites du jeu

        Parametres:
        ----------
        max_rooms : int

        room_min_size : int

        room_max_size : int

        map_width : int

        map_height : int

        player : Entity

        entities : list

        graphics : dict


        Renvoi:
        -------
        entities : list
            retourne la liste des entités avec les monstres et les objets placés

        """
        rooms = []
        num_rooms = 0
        center_of_last_room_x = None
        center_of_last_room_y = None
        for r in range(max_rooms):
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)
            new_room = Rect(x, y, w, h)
            # Coupe la boucle si la pièce créée intersecte d'autres déjà créées
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            # Si la pièce créée est valide, la relie à la précédente et place les entités
            else:
                self.create_room(new_room)
                (new_x, new_y) = new_room.center()
                center_of_last_room_x = new_x
                center_of_last_room_y = new_y
                if num_rooms == 0:
                    player.x = new_x
                    player.y = new_y
                else:
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()
                    if randint(0, 1) == 1:
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)
                entities = self.place_entities(new_room, entities, graphics)
                rooms.append(new_room)
                num_rooms += 1
        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             graphics.get('stairs'),
                             libtcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
        return entities
Ejemplo n.º 23
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        '''
        Creates map of current dungeon floor
        '''
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # random pos in map boundaries
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # use rectangle room class
            new_room = Rect(x, y, w, h)

            # check for intersecting rooms
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break

            else:
                self.create_room(new_room)

                # center coordinates of new room
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # starting room
                    player.x = new_x
                    player.y = new_y

                else:
                    # all rooms after the first
                    # connect to previous room with tunnels

                    if num_rooms == 1:  #and (randint(0, 2) == 1):
                        #33% chance of generating a shop on second room
                        (prev_x, prev_y) = rooms[num_rooms - 1].center()

                        if randint(0, 1) == 1:
                            # first move horizontally, then vertically
                            self.create_h_tunnel(prev_x, new_x, prev_y)
                            self.create_v_tunnel(prev_y, new_y, new_x)
                        else:
                            # first move vertically, then horizontally
                            self.create_v_tunnel(prev_y, new_y, prev_x)
                            self.create_h_tunnel(prev_x, new_x, new_y)

                        shopkeep_component = Shopkeep(True)
                        inventory_component = Inventory(25)
                        shopkeeper = Entity(new_x,
                                            new_y,
                                            '$',
                                            libtcod.Color(5, 5, 5),
                                            'Shopkeeper',
                                            blocks=True,
                                            render_order=RenderOrder.SHOPKEEP,
                                            shopkeep=shopkeep_component,
                                            inventory=inventory_component)

                        shopkeep_inventory = [
                            's_healing_potion', 'm_healing_potion',
                            'l_healing_potion', 'full_healing_salve',
                            'valkyrie_helm', 'hulidshjalmr', 'falcon_cloak',
                            'golden_coat', 'dwarven_leggings',
                            'leggings_of_odin', 'dwarven_boots',
                            'shoes_of_vidarr', 'megingjord', 'shield_of_hel',
                            'svallin', 'hrotti', 'dainsleif', 'tyrfing',
                            'gungnir', 'mjolnir', 'confusion_scroll',
                            'lightning_scroll', 'fireball_scroll', 'mistletoe',
                            'ragnarok'
                        ]

                        for item in shopkeep_inventory:
                            item = get_item(item, 0, 0)
                            shopkeeper.inventory.add_item(item)

                        entities.append(shopkeeper)

                    else:
                        # center coordinates of previous room
                        (prev_x, prev_y) = rooms[num_rooms - 1].center()

                        # random choice of tunnel
                        if randint(0, 1) == 1:
                            # first move horizontally, then vertically
                            self.create_h_tunnel(prev_x, new_x, prev_y)
                            self.create_v_tunnel(prev_y, new_y, new_x)
                        else:
                            # first move vertically, then horizontally
                            self.create_v_tunnel(prev_y, new_y, prev_x)
                            self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities)
                # append the new room to the list
                rooms.append(new_room)
                num_rooms += 1

        # create stairs entity in center of last room
        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             libtcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 24
0
    def make_map(self, player, entities):
        rooms = []
        num_rooms = 0
        up_stairs = None
        down_stairs = None

        # Create the marketplace on the topmost level
        if self.dungeon_level == 0:
            self.make_market(player, entities)

        elif self.dungeon_level == MAX_LEVELS:
            w = randint(ROOM_MAX_SIZE + 4, ROOM_MAX_SIZE + 4)
            h = randint(ROOM_MAX_SIZE + 4, ROOM_MAX_SIZE + 4)
            x = MAP_WIDTH // 2
            y = MAP_HEIGHT // 2

            # "Rect" class makes rectangles easier to work with
            new_room = Rect(x, y, w, h)

            # "paint" it to the map's tiles
            self.create_room(new_room)

            (new_x, new_y) = new_room.center()
            player.x = new_x
            player.y = new_y

            self.place_entities(new_room, entities)
            rooms.append(new_room)
            num_rooms += 1

        else:

            for r in range(MAX_ROOMS):
                # random width and height
                w = randint(ROOM_MIN_SIZE, ROOM_MAX_SIZE)
                h = randint(ROOM_MIN_SIZE, ROOM_MAX_SIZE)
                # random position without going out of the boundaries of the map
                x = randint(0, MAP_WIDTH - w - 1)
                y = randint(0, MAP_HEIGHT - h - 1)

                # "Rect" class makes rectangles easier to work with
                new_room = Rect(x, y, w, h)

                intersects = False
                # run through the other rooms and see if they intersect with this one
                for other_room in rooms:
                    if new_room.intersect(other_room):
                        intersects = True
                        break

                if not intersects:
                    # this means there are no intersections, so this room is valid

                    # "paint" it to the map's tiles
                    self.create_room(new_room)

                    # center coordinates of new room, will be useful later
                    (new_x, new_y) = new_room.center()

                    centre_of_last_room_x = new_x
                    centre_of_last_room_y = new_y

                    if num_rooms == 0:
                        # this is the first room, where the player starts at
                        player.x = new_x
                        player.y = new_y
                    else:
                        # all rooms after the first:
                        # connect it to the previous room with a tunnel

                        # center coordinates of previous room
                        (prev_x, prev_y) = rooms[num_rooms - 1].center()

                        # flip a coin (random number that is either 0 or 1)
                        if randint(0, 1) == 1:
                            # first move horizontally, then vertically
                            self.create_h_tunnel(prev_x, new_x, prev_y)
                            self.create_v_tunnel(prev_y, new_y, new_x)
                        else:
                            # first move vertically, then horizontally
                            self.create_v_tunnel(prev_y, new_y, prev_x)
                            self.create_h_tunnel(prev_x, new_x, new_y)

                        # finally, append the new room to the list
                    # Put stairs in the world ordering based on where the player just came from
                    # i.e. if the player came from the floor below then the player should start with stairs going down
                    if self.previous_level < self.dungeon_level:
                        if up_stairs is None:
                            stairs_component = Stairs(self.dungeon_level - 1)
                            up_stairs = Entity(centre_of_last_room_x,
                                               centre_of_last_room_y,
                                               UP_STAIRS_CHAR,
                                               libtcod.white,
                                               'Stairs',
                                               render_order=RenderOrder.STAIRS,
                                               stairs=stairs_component)
                            entities.append(up_stairs)

                        elif down_stairs is None:
                            stairs_component = Stairs(self.dungeon_level + 1)
                            down_stairs = Entity(
                                centre_of_last_room_x,
                                centre_of_last_room_y,
                                DOWN_STAIRS_CHAR,
                                libtcod.white,
                                'Stairs',
                                render_order=RenderOrder.STAIRS,
                                stairs=stairs_component)
                            entities.append(down_stairs)
                    else:
                        if down_stairs is None:
                            stairs_component = Stairs(self.dungeon_level + 1)
                            down_stairs = Entity(
                                centre_of_last_room_x,
                                centre_of_last_room_y,
                                DOWN_STAIRS_CHAR,
                                libtcod.white,
                                'Stairs',
                                render_order=RenderOrder.STAIRS,
                                stairs=stairs_component)
                            entities.append(down_stairs)
                        elif up_stairs is None:
                            stairs_component = Stairs(self.dungeon_level - 1)
                            up_stairs = Entity(centre_of_last_room_x,
                                               centre_of_last_room_y,
                                               UP_STAIRS_CHAR,
                                               libtcod.white,
                                               'Stairs',
                                               render_order=RenderOrder.STAIRS,
                                               stairs=stairs_component)
                            entities.append(up_stairs)

                    self.place_entities(new_room, entities)
                    rooms.append(new_room)
                    num_rooms += 1
Ejemplo n.º 25
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)

            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            new_room = Rect(x, y, w, h)

            for other_room in rooms:
                if new_room.intersect(other_room):
                    break

            else:
                # There are no intersections, so the room is valid

                # Paint map tile
                self.create_room(new_room)

                # Center room coords

                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # First room aka player rom
                    player.x = new_x
                    player.y = new_y
                else:
                    # all rooms after the first:
                    # Connect to the previous room with a tunnel

                    # Center coords of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    if randint(0, 1) == 1:
                        # First move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # First move vertically, then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities)

                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x, center_of_last_room_y, '>', libtcod.white, 'Stairs',
                             render_order=RenderOrder.STAIRS, stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 26
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):

        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height of room
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)

            # random position within map bounds
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            new_room = Rect(x, y, w, h)
            # Check if new room intersects with others
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            else:
                # if room check didnt break, add new room to map tiles
                self.create_room(new_room)

                # center coord of room
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # if this is first room, player starts here
                    player.x = new_x
                    player.y = new_y

                else:
                    # for all rooms after first, connect to previous room with tunnel
                    # center coords of prev room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # Flip a coin
                    if randint(0, 1) == 1:
                        # First move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # or vice versa
                        self.create_h_tunnel(prev_x, new_x, new_y)
                        self.create_v_tunnel(prev_y, new_y, prev_x)

                self.place_entities(new_room, entities)

                # Finally, append new room to list
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             tcod.white,
                             'Stairs Down',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 27
0
class GameMap:
    def __init__(self, width, height, dungeon_level=1):
        self.width = width
        self.height = height
        self.tiles = self.initialize_tiles()

        self.dungeon_level = dungeon_level

    def initialize_tiles(self):
        tiles = [[Tile(True) for y in range(self.height)]
                 for x in range(self.width)]

        return tiles

    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities, max_monster_per_room,
                 max_items_per_room):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # random position without going out of the boundaries of the map
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # 'Rect' class makes rectangles easier to work with
            new_room = Rect(x, y, w, h)

            # run through the other rooms and see if they intersect with this one
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break
            # 这个else的缩进错误,花费了1小时去修正
            else:
                # this means there are no intersections, so this rooms is valid

                # 'paint' it to the map's tiles
                self.create_room(new_room)

                # center coordinates of new room, will be useful later

                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # this is the first room, where the player starts at
                    player.x = new_x
                    player.y = new_y
                else:
                    # all rooms after the first
                    # connect it to the previous room with a tunnel
                    # center coordinates of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # flip a coin(random number that is either 0 or 1)
                    if randint(0, 1) == 1:
                        # first move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # first move vertically, then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)
                self.place_entities(new_room, entities, max_monster_per_room,
                                    max_items_per_room)
                # finally, append the new room to the list
                rooms.append(new_room)
                num_rooms += 1

    stairs_component = Stairs(self.dungeon_level + 1)
    down_stairs = Entity(center_of_last_room_x,
                         center_of_last_room_y,
                         '>',
                         tcod.white,
                         'Stairs',
                         render_order=RenderOrder.STAIRS,
                         stairs=stairs_component)
    entities.append(down_stairs)

    def create_room(self, room):
        # go throught the tiles in the rectangele and make them passable
        for x in range(room.x1 + 1, room.x2):
            for y in range(room.y1 + 1, room.y2):
                self.tiles[x][y].blocked = False
                self.tiles[x][y].block_sight = False

    def create_h_tunnel(self, x1, x2, y):
        for x in range(min(x1, x2), max(x1, x2) + 1):
            self.tiles[x][y].blocked = False
            self.tiles[x][y].block_sight = False

    def create_v_tunnel(self, y1, y2, x):
        for y in range(min(y1, y2), max(y1, y2) + 1):
            self.tiles[x][y].blocked = False
            self.tiles[x][y].block_sight = False

    def place_entities(self, room, entities, max_monsters_per_room,
                       max_items_per_room):
        # Get a random number of monsters
        number_of_monster = randint(0, max_monsters_per_room)
        number_of_items = randint(0, max_items_per_room)

        for i in range(number_of_monster):
            # Choose a random location in the room
            x = randint(room.x1 + 1, room.x2 - 1)
            y = randint(room.y1 + 1, room.y2 - 1)

            if not any([
                    entity
                    for entity in entities if entity.x == x and entity.y == y
            ]):
                if randint(0, 100) < 80:
                    fighter_component = Fighter(hp=10, defense=0, power=3)
                    ai_component = BasicMonster()
                    monster = Entity(x,
                                     y,
                                     'o',
                                     tcod.desaturated_green,
                                     'Orc',
                                     blocks=True,
                                     render_order=RenderOrder.ACTOR,
                                     fighter=fighter_component,
                                     ai=ai_component)

                else:
                    fighter_component = Fighter(hp=16, defense=1, power=4)
                    ai_component = BasicMonster()

                    monster = Entity(x,
                                     y,
                                     'T',
                                     tcod.darker_green,
                                     'Troll',
                                     blocks=True,
                                     fighter=fighter_component,
                                     render_order=RenderOrder.ACTOR,
                                     ai=ai_component)

                entities.append(monster)
        for i in range(number_of_monster):
            x = randint(room.x1 + 1, room.x2 - 1)
            y = randint(room.y1 + 1, room.y2 - 1)

            if not any([
                    entity
                    for entity in entities if entity.x == x and entity.y == y
            ]):
                item_chance = randint(0, 100)

                if item_chance < 70:
                    item_component = Item(use_function=heal, amount=4)
                    item = Entity(x,
                                  y,
                                  '!',
                                  tcod.violet,
                                  'Healing Potion',
                                  render_order=RenderOrder.ITEM,
                                  item=item_component)

                elif item_chance < 80:
                    item_component = Item(
                        use_function=cast_fireball,
                        targeting=True,
                        targeting_message=Message(
                            'Left-click a target tile for the fireball, or right-click to cancel.',
                            tcod.light_cyan),
                        damage=12,
                        radius=3)
                    item = Entity(x,
                                  y,
                                  "#",
                                  tcod.red,
                                  'Fireball Scroll',
                                  render_order=RenderOrder.ITEM,
                                  item=item_component)

                elif item_chance < 90:
                    item_component = Item(
                        use_function=cast_confuse,
                        targeting=True,
                        targeting_message=Message(
                            'Left-click an enemy to confuse it, or right-click to cancel.',
                            tcod.light_cyan))
                    item = Entity(x,
                                  y,
                                  '#',
                                  tcod.light_pink,
                                  'Confusion Scroll',
                                  render_order=RenderOrder.ITEM,
                                  item=item_component)

                else:
                    item_component = Item(use_function=cast_lightning,
                                          damage=20,
                                          maximum_range=5)
                    item = Entity(x,
                                  y,
                                  '#',
                                  tcod.yellow,
                                  'Lightning Scroll',
                                  render_order=RenderOrder.ITEM,
                                  item=item_component)

                entities.append(item)

    def is_blocked(self, x, y):
        if self.tiles[x][y].blocked:
            return True

        return False
Ejemplo n.º 28
0
def make_map(game_map, max_rooms, room_min_size, room_max_size, map_width,
             map_height, player, entities, colors):
    rooms = []
    num_rooms = 0

    center_of_last_room_x = None
    center_of_last_room_y = None

    for r in range(max_rooms):
        # random width and height
        w = randint(room_min_size, room_max_size)
        h = randint(room_min_size, room_max_size)
        # random position without going out of bounds
        x = randint(0, map_width - w - 1)
        y = randint(0, map_height - h - 1)

        new_room = Rect(x, y, w, h)
        # run through the other rooms and see if intersect
        for other_room in rooms:
            if new_room.intersect(other_room):
                break
        else:
            # no intersection, valid room

            # paint room to tiles
            create_room(game_map, new_room)

            # center coords of new room
            (new_x, new_y) = new_room.center()

            center_of_last_room_x = new_x
            center_of_last_room_y = new_y

            if num_rooms == 0:
                # first room/player start
                player.x = new_x
                player.y = new_y
            else:
                # rooms after first
                # connect with tunnels

                # center coords of prev room
                (prev_x, prev_y) = rooms[num_rooms - 1].center()

                # coin flip
                if randint(0, 1) == 1:
                    # first move horz then vert
                    create_h_tunnel(game_map, prev_x, new_x, prev_y)
                    create_v_tunnel(game_map, prev_y, new_y, new_x)
                else:
                    # first ver then horz
                    create_v_tunnel(game_map, prev_y, new_y, prev_x)
                    create_h_tunnel(game_map, prev_x, new_x, new_y)

            place_entities(new_room, entities, game_map.dungeon_level, colors)

            # append room to list
            rooms.append(new_room)
            num_rooms += 1

    stairs_component = Stairs(game_map.dungeon_level + 1)
    down_stairs = Entity(center_of_last_room_x,
                         center_of_last_room_y,
                         '>', (255, 255, 255),
                         'Stairs',
                         render_order=RenderOrder.STAIRS,
                         stairs=stairs_component)
    entities.append(down_stairs)
Ejemplo n.º 29
0
    def make_map(self, max_rooms, room_min_size, room_max_size, map_width,
                 map_height, player, entities):
        rooms = []
        num_rooms = 0

        center_of_last_room_x = None
        center_of_last_room_y = None

        for r in range(max_rooms):
            # random width and height
            w = randint(room_min_size, room_max_size)
            h = randint(room_min_size, room_max_size)
            # random position without going out of the boundaries of the map
            x = randint(0, map_width - w - 1)
            y = randint(0, map_height - h - 1)

            # "Rect" class makes rectangles easier to work with
            new_room = Rect(x, y, w, h)

            # run through the other rooms and see if they intersect with this one
            for other_room in rooms:
                if new_room.intersect(other_room):
                    break

            else:
                # this means there are no intersections, so this room is valid

                # "paint" it to the map's tiles
                self.create_room(new_room)

                # center coordinates of new room, will be useful later
                (new_x, new_y) = new_room.center()

                center_of_last_room_x = new_x
                center_of_last_room_y = new_y

                if num_rooms == 0:
                    # this is the first room, where the player starts at
                    player.x = new_x
                    player.y = new_y

                else:
                    # all rooms after the first:
                    # connect it to the previous room with a tunnel

                    # center coordinates of previous room
                    (prev_x, prev_y) = rooms[num_rooms - 1].center()

                    # flip a coin (random number that is either 0 or 1)
                    if randint(0, 1) == 1:
                        # first move horizontally, then vertically
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        # first move vertically, then horizontally
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)

                self.place_entities(new_room, entities)

                # finally, append the new room to the list
                rooms.append(new_room)
                num_rooms += 1

        stairs_component = Stairs(self.dungeon_level + 1)
        down_stairs = Entity(center_of_last_room_x,
                             center_of_last_room_y,
                             '>',
                             libtcod.white,
                             'Stairs',
                             render_order=RenderOrder.STAIRS,
                             stairs=stairs_component)
        entities.append(down_stairs)
Ejemplo n.º 30
0
def make_map(game_map, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities,
             max_monsters_per_room, max_items_per_room, colours):
    rooms = []
    num_rooms = 0

    centre_of_last_room_x = None
    centre_of_last_room_y = None

    for r in range(max_rooms):
        # Random width and height
        w = randint(room_min_size, room_max_size)
        h = randint(room_min_size, room_max_size)

        # Random position without going out of the boundaries of the map
        x = randint(0, map_width - w - 1)
        y = randint(0, map_height - h - 1)

        new_room = Rect(x, y, w, h)

        # Run through the other rooms to see if they intersect with this one
        for other_room in rooms:
            if new_room.intersect(other_room):
                break
        else:
            # This means there are no intersections so this room is valid
            # Paint it to the maps tiles
            create_room(game_map, new_room)

            # Centre coords of the room will be useful later!
            (new_x, new_y) = new_room.center()

            centre_of_last_room_x = new_x
            centre_of_last_room_y = new_y

            if num_rooms == 0:
                # This is the first room, where the player starts at
                player.x = new_x
                player.y = new_y
            else:
                # All rooms after the first
                # connect it to the previous room with a tunnel
                # center coords of the previous room
                (prev_x, prev_y) = rooms[num_rooms - 1].center()

                # Flip a coin
                if randint(0, 1) == 1:
                    # First move horizontally then vertically
                    create_h_tunnel(game_map, prev_x, new_x, prev_y)
                    create_v_tunnel(game_map, prev_y, new_y, new_x)
                else:
                    # First move vertically then horizontally
                    create_v_tunnel(game_map, prev_y, new_y, prev_x)
                    create_h_tunnel(game_map, prev_x, new_x, new_y)

            place_entities(new_room, entities, max_monsters_per_room, max_items_per_room, colours)

            # Finally append the new room to the list
            rooms.append(new_room)
            num_rooms += 1

    stairs_component = Stairs(game_map.dungeon_level + 1)
    down_stairs = Entity(centre_of_last_room_x, centre_of_last_room_y, '>', (255, 255, 255), 'Stairs',
                         render_order=RenderOrder.STAIRS, stairs=stairs_component)
    entities.append(down_stairs)