Exemple #1
0
def generate_rooms(size_x, size_y, num_rooms):
    grid = [None] * size_y
    width = size_x
    height = size_y
    for i in range(len(grid)):
        grid[i] = [None] * size_x
    x = -1
    y = 0
    room_count = 0
    direction = 1
    while room_count < num_rooms:
        if direction > 0 and x < size_x - 1:
            x += 1
        else:
            y += 1
            x = 0
        room = Room(room_id=room_count,
                    title="A Generic Room",
                    description="This is a generic room.",
                    x=x,
                    y=y)
        # Save the room in the World grid
        grid[y][x] = room
        room.save()
        room_count += 1
    for row in grid:
        for room in row:
            direction_list = ['n', 's', 'e', 'w']
            new_y = room.y
            new_x = room.x
            num_loops = 0
            while num_loops <= 1:
                direction = random.choice(direction_list)
                if direction == 'n' and room.x <= 9 and room.y < 9:
                    new_y = room.y + 1
                    room.connect_rooms(grid[new_y][new_x], direction)
                elif direction == 's' and room.x <= 9 and room.y > 0:
                    new_y = room.y - 1
                    room.connect_rooms(grid[new_y][new_x], direction)
                elif direction == 'e' and room.x >= 0 and room.x < 9 and room.y >= 0 and room.y < 10:
                    new_x = room.x + 1
                    room.connect_rooms(grid[new_y][new_x], direction)
                elif direction == 'w' and room.x >= 1 and room.x < 10 and room.y >= 0 and room.y < 10:
                    new_x = room.x - 1
                    room.connect_rooms(grid[new_y][new_x], direction)
                num_loops += 1
    return grid
    def generate_rooms(self, size_x, size_y, num_rooms):
        '''
        Fill up the grid, bottom to top, in a zig-zag pattern
        '''

        # Initialize the grid
        self.grid = [None] * size_y
        self.width = size_x
        self.height = size_y
        for i in range( len(self.grid) ):
            self.grid[i] = [None] * size_x

        x = (size_x//2) 
        y = (size_y//2)
        room_count = 0

        # Start generating rooms to the east
        direction = [-1, 1]  # -1 = south or west, 1 = north or east
        vertical_or_horizontal = ['x', 'y']

        firstroom = Room(room_count, "Dark Atrium", "You begin your journey in a wide open atrium, covered in the shadow of night. Choose your path carefully going forward.", x, y)
        self.grid[y][x] = firstroom
        room_count += 1

        firstroom.save()

        # While the room count is less than the current number of rooms
        # Navigate randomly (within the set grid boundary) and create rooms/connections accordingly
        previous_room = firstroom
        
        while room_count < num_rooms:
            ## Pick a direction (x or y) for movement
            ## Generate -1 or 1 randomly
            hit_a_wall = False
            one_or_negative_one = random.choice(direction)
            x_or_y = random.choice(vertical_or_horizontal)

            current_title = f"{random.choice(title_adj)} {random.choice(title_noun)}"
            current_description = f"{random.choice(description_start)} {random.choice(description_end)}"
        
            if x_or_y == 'x':    #we are moving east or west

                if one_or_negative_one == 1: #moving east
                    if x < (size_x - 1):
                        room_direction = "e"
                        x += 1
                    else:
                        hit_a_wall = True

                else:    #moving west
                    if x > 0:
                        room_direction = "w"
                        x -= 1
                    else:
                        hit_a_wall = True

            elif x_or_y == 'y':  #we are moving north or south

                if one_or_negative_one == 1: #moving north
                    if y < (size_y - 1):
                        room_direction = "n"
                        y += 1
                    else:
                        hit_a_wall = True

                else: #moving south
                    if y > 0:
                        room_direction = "s"
                        y -= 1
                    else:
                        hit_a_wall = True

            #check to see if there is a room after moving
            if hit_a_wall == False and self.grid[y][x] is not None:
                #connect the room to the previous one
                if previous_room is not None:
                    previous_room.connect_rooms(self.grid[y][x], room_direction)
                previous_room = self.grid[y][x]

            #if there is no room after moving
            elif hit_a_wall == False and self.grid[y][x] is None:
                #create a room
                room = Room(title=current_title, description=current_description, x=x, y=y)
                room.save()

                #save the room in the grid
                self.grid[y][x] = room

                #connect the room to the previous one
                if previous_room is not None:
                    previous_room.connect_rooms(room, room_direction)
                
                #because we created a room, increment room_count and update previous_room
                previous_room = room
                room_count += 1
            else:
                #if we hit this, we hit a wall. So try choosing another direction
                pass

        if self.grid[start_y + 1][start_x] is not None:
          firstroom.connect_rooms(self.grid[start_y + 1][start_x], 'n')
        if self.grid[start_y - 1][start_x] is not None:
          firstroom.connect_rooms(self.grid[start_y - 1][start_x], 's')
        if self.grid[start_y][start_x + 1] is not None:
          firstroom.connect_rooms(self.grid[start_y][start_x + 1], 'e')
        if self.grid[start_y][start_x - 1] is not None:
          firstroom.connect_rooms(self.grid[start_y][start_x - 1], 'w')

        players = Player.objects.all()
        for p in players:
            p.currentRoom=firstroom.id
            p.save()
Exemple #3
0
    def generate_rooms(self, size_x, size_y, num_rooms):
        """
        Fill up the grid, bottom to top, in a zig-zag pattern
        """

        # Initialize the grid
        self.grid = [None] * size_y
        self.width = size_x
        self.height = size_y
        for i in range(len(self.grid)):
            self.grid[i] = [None] * size_x

        # Start from lower-left corner (0,0)
        x = -1  # (this will become 0 on the first step)
        y = 0
        room_count = 0

        # Start generating rooms to the east
        direction = 1  # 1: east, -1: west

        p_adj = ["Frozen", "Frostbit", "Shivering", "Glacial"]
        h_c_adj = [
            "Desolate",
            "Bleak",
            "Dreary",
            "Bare",
            "Deserted",
            "Forlorn",
            "Gloomy",
            "Barren",
            "Bereft",
        ]
        r_adj = ["Warm", "Cozy"]
        cryptic_adj = ["Cryptic", "Dark", "Dim", "Mysterious"]

        h = [
            f"{random.choice(h_c_adj+cryptic_adj)} {i}"
            for i in ["Hallway"] * 40
        ]
        p = [f"{random.choice(p_adj+cryptic_adj)} {i}" for i in ["Pass"] * 15]
        c = [
            f"{random.choice(h_c_adj+cryptic_adj)} {i}"
            for i in ["Chamber"] * 15
        ]
        cell = [
            f"{random.choice(h_c_adj+cryptic_adj)} {i}" for i in ["Cell"] * 15
        ]
        room = [f"{random.choice(cryptic_adj)} {i}" for i in ["Room"] * 15]
        names = h + p + c + cell + room

        # While there are rooms to be created...
        previous_room = None
        while room_count < num_rooms:

            # Calculate the direction of the room to be created
            if direction > 0 and x < size_x - 1:
                room_direction = "e"
                x += 1
            elif direction < 0 and x > 0:
                room_direction = "w"
                x -= 1
            else:
                # If we hit a wall, turn north and reverse direction
                room_direction = "n"
                y += 1
                direction *= -1

            # Create a room in the given direction

            name = random.choice(names)
            names.pop(names.index(name))
            room = Room(title=name,
                        description="This is a generic room.",
                        x=x,
                        y=y)
            room.save()
            item = items.pop()
            if item is not None:
                room.inventory.set([item])
            # Note that in Django, you'll need to save the room after you create it

            # Save the room in the World grid
            self.grid[y][x] = room

            # Connect the new room to the previous room
            opposite_dictionary = {"w": "e", "e": "w", "s": "n", "n": "s"}
            if previous_room is not None:
                previous_room.connect_rooms(room, room_direction)
                room.connect_rooms(previous_room,
                                   opposite_dictionary[room_direction])

            # Update iteration variables
            previous_room = room
            room_count += 1
Exemple #4
0
                description="""The narrow passage bends here from west
to north. The smell of gold permeates the air.""")

r_treasure = Room(title="Treasure Chamber",
                  description="""You've found the long-lost treasure
chamber! Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south.""")

r_outside.save()
r_foyer.save()
r_overlook.save()
r_narrow.save()
r_treasure.save()

# Link rooms together
r_outside.connect_rooms(r_foyer, "n")
r_foyer.connect_rooms(r_outside, "s")

r_foyer.connect_rooms(r_overlook, "n")
r_overlook.connect_rooms(r_foyer, "s")

r_foyer.connect_rooms(r_narrow, "e")
r_narrow.connect_rooms(r_foyer, "w")

r_narrow.connect_rooms(r_treasure, "n")
r_treasure.connect_rooms(r_narrow, "s")

players = Player.objects.all()

for p in players:
    p.current_room = r_outside.id
Exemple #5
0
 def generate_rooms(self, size_x, size_y, num_rooms):
     # Initialize the grid
     grid = [None] * size_y
     self.width = size_x
     self.height = size_y
     for i in range(len(grid)):
         grid[i] = [None] * size_x
     # Start from lower-left corner (0,0)
     x = -1  # (this will become 0 on the first step)
     y = 0
     room_count = 0
     # Start generating rooms to the east
     direction = 1  # 1: east, -1: west
     horDirection = 1  # 1: up, -1: down
     # Generated Room Names
     roomAdj = [
         "Dark", "Old", "Old, Intact", "Loathsome", "Horrid", "Empty",
         "Moist, Murky", "Suspicous", "Damp", "Gloomy", "Secret", "Filthy",
         "Misty", "Moldy", "Pestilent", "Cozy Little", "Dim", "Smokey",
         "Vacant", "Ancient"
     ]
     roomNames = [
         "Cellar", "Cave", "Cabin", "Hideout", "Pathway", "Corridor",
         "Cave", "Hideout", "Pathway", "Corridor", "Treasure Room"
     ]
     # Generated Room Descriptions
     descStarters = [
         "You approach a(n)", "You step forward and see a(n)",
         "You walk into a(n)", "This is a", "You creep into a(n)"
     ]
     descInfo = [
         "It seems to give off an errie mood",
         "Its a pretty dark room, better watch you step!",
         "You see a sparkling light deeper in the dungeon, is it your imagination?",
         "A terrible smell creeps up your nose.", "Seems like a cozy place",
         "Its a very quiet room. Too quiet.", "Its very dark in here.",
         "Dont be fooled.",
         "You hear some rocks hit the ground, better watch your step."
     ]
     # While there are rooms to be created...
     previous_room = None
     while room_count < num_rooms:
         # Calculate the direction of the room to be created
         nextDi = random.randint(0, 18)
         canDown = horDirection <= 0
         canUp = horDirection >= 0
         if nextDi > 11 and canDown and not grid[
                 y - 1][x] and y > 1 and x < size_x - 2 and x > 1:
             room_direction = "s"
             horDirection = -1
             y -= 1
         elif x > 1 and nextDi > 16 and canUp:
             room_direction = "n"
             horDirection = 1
             y += 1
         elif direction > 0 and x < size_x - 1 and not grid[y][x + 1]:
             room_direction = "e"
             horDirection = 0
             x += 1
         elif direction < 0 and x > 0 and not grid[y][x - 1]:
             horDirection = 0
             room_direction = "w"
             x -= 1
         else:
             # If we hit a wall, turn north and reverse direction
             # If theres a room above it, go to the room above that
             if grid[y + 1][x]:
                 while grid[y + 1][x]:
                     y += 1
                     previous_room = grid[y][x]
             y += 1
             room_direction = "n"
             horDirection = 1
             direction *= -1
         currName = random.choice(roomAdj) + " " + random.choice(roomNames)
         roomDescription = random.choice(
             descStarters) + " " + currName.lower() + ". " + random.choice(
                 descInfo)
         # Create a room in the given direction
         room = Room(title=currName, description=roomDescription, x=x, y=y)
         # Note that in Django, you'll need to save the room after you create it
         # Save the room in the World grid
         grid[y][x] = room
         room.save()
         # Connect the new room to the previous room
         if previous_room is not None:
             reverse_dirs = {"n": "s", "s": "n", "e": "w", "w": "e"}
             reverse_dir = reverse_dirs[room_direction]
             previous_room.connect_rooms(room, room_direction)
             room.connect_rooms(previous_room, reverse_dir)
         if nextDi < 10 and y > 0 and grid[y - 1][x]:
             room.connect_rooms(grid[y - 1][x], "s")
             grid[y - 1][x].connect_rooms(room, "n")
         # Update iteration variables
         if previous_room:
             previous_room.save()
         room.save()
         previous_room = room
         room_count += 1
     self.grid = f"{grid}"
     return
def world_generation(size_x, size_y, num_rooms):

    # Initialize the grid
    world_array = [None] * num_rooms
    width = size_x
    height = size_y
    # for i in range( len(self.grid) ):
    #   self.grid[i] = [None] * size_x
    # print(self.grid)

    # Start from lower-left corner (0,0)
    x = 0
    y = 0
    room_count = 0
    indexed_height = height - 1
    indexed_width = width - 1

    # # Start generating rooms by line until max height/width reached

    ###### Creating All Rooms
    while room_count < num_rooms:
        ## start left to right, incrementing x by 1, until x = width
        ## reset x, y+1
        ## need to create first room:
        if x == 0 and y == 0:
            # print('first if')
            # print(x, y)
            room = Room(room_count, "A Generic Room",
                        "This is a generic room.", x, y)
            world_array[room_count] = room
            x += 1
            room_count = room_count + 1

            # print(x, y)
        ## Create exit case of last room
        elif y < indexed_height and x == indexed_width:
            # print('first elif')
            # print(x, y)
            room = Room(room_count, "A Generic Room",
                        "This is a generic room.", x, y)
            world_array[room_count] = room
            y += 1
            x = 0
            room_count = room_count + 1
            # print(x, y)
        elif x < indexed_width:
            # print('second elif')
            # print(x, y)
            room = Room(room_count, "A Generic Room",
                        "This is a generic room.", x, y)
            world_array[room_count] = room
            x += 1
            room_count = room_count + 1
            # print(x, y)
        elif y == indexed_height and x == indexed_width:
            # print('LAST ONE')
            # print(x, y)
            room = Room(room_count, "A Generic Room",
                        "This is a generic room.", x, y)
            world_array[room_count] = room
            # print(room_count, num_rooms)
            room_count = room_count + 1
        else:
            print('Ive f****d something')

    ## reset variables
    # room_count = 0
    # x = 0
    # y = 0

    # print('rooms created')
    # print(self.grid)
    # print(self.grid[3][4].x)


### helper for directions
## can_n: y-1, x == 0
## can_e: x+1, x == width
## can_s: y+1, y == height
## can_w: x-1, y == 0

#### Now that rooms are created, we can connect them randomly
    for rooms in world_array:
        print('starting room connections')
        print(room.x, room.y)
        # Initalizing variables for examining room connections and blockers
        can_n = 'open'
        can_e = 'open'
        can_s = 'open'
        can_w = 'open'
        curr_connected = 0
        curr_x = room.x
        curr_y = room.y
        indexed_height = height - 1
        indexed_width = width - 1

        ## Checking current room connections
        if room.check_connections('n'):
            can_n = 'connected'
            curr_connected += 1
        if room.check_connections('e'):
            can_e = 'connected'
            curr_connected += 1
        if room.check_connections('s'):
            can_s = 'connected'
            curr_connected += 1
        if room.check_connections('w'):
            can_w = 'connected'
            curr_connected += 1

        ## Generating chance for how many connections ---- I DON'T KNOW HOW RANDOM WORKS
        ## 100% to get first connection
        ## 80% to get second connection
        ## 60% to get third
        ## 10% to get 4th

        connection_roll = random.randint(0, 10)
        # print('connection_roll')
        # print(connection_roll)
        ###### TO-DO: THIS SHIT DOESN'T WORK. I DON'T UNDERSTAND PERCENT CHANCES WTF???????
        connection_attempts = 0
        if connection_roll <= 1:
            connection_attempts = 4
        elif connection_roll > 1 and connection_roll < 5:
            connection_attempts = 3
        elif connection_roll >= 5 and connection_roll < 9:
            connection_attempts = 2
        else:
            connection_attempts = 1

        ### If the amount of connections rolled == the current amount of connections, nothing left to do!
        if connection_attempts == curr_connected:
            print('were in the connect = curr check')
            pass

        ### Now to check which directions are block
        blocked = 0
        print('checking blocks')
        if curr_x == 0:
            can_s = 'blocked'
            blocked += 1
        if curr_x == indexed_width:
            can_e = 'blocked'
            blocked += 1
        if curr_y == 0:
            can_w = 'blocked'
            blocked += 1
        if curr_y == indexed_height:
            can_n = 'blocked'
            blocked += 1

        #### TO-DO: SPECIAL CASE
        ### If connection_attempts is 4 and blocked are 2, need to only check 2. if connection attempts are 2 and blocked are 2, still need to check 2
        ## how the f**k do u math that

        ### NOW IT'S TIME TO MAKE SOME CONNECTIONS, LADIES AND GENTS
        print('starting connection while loop')
        print(connection_attempts)
        while connection_attempts > 0:
            connection_complete = False
            # print('we in first while')
            while connection_complete is False:
                # print('start of second while')
                #Roll for direction
                direction_roll = random.randint(0, 3)
                # print(direction_roll)
                # set directions to array
                directions = [can_n, can_e, can_s, can_w]
                direction_array = ['n', 'e', 's', 'w']
                # print(directions[direction_roll])
                print('this is the directions roll')
                print(directions[direction_roll])
                if directions[direction_roll] == 'open':
                    # print('success')
                    # directions[direction_roll] = 'connected'
                    ####### UAOLDFKS;LADSF HOW DOES SOME F*****G IDIOT CONNECT ROOMS
                    new_x = curr_x
                    new_y = curr_y
                    ## Creating new direction
                    if direction_roll == 0:  # N
                        new_y = curr_y + 1
                    if direction_roll == 1:  # E
                        new_x = curr_x + 1
                    if direction_roll == 2:  # S
                        new_y = curr_y - 1
                    if direction_roll == 3:  # W
                        new_x = curr_x - 1
                    print(
                        'AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH'
                    )
                    print(curr_x, curr_y)
                    print(new_x, new_y)
                    new_direction = direction_array[direction_roll]
                    new_room = self.grid[new_y][new_x]
                    print(new_direction)
                    room.connect_rooms(new_room, new_direction)
                connection_attempts = connection_attempts - 1
                connection_complete = True

    for room in world_array:
        room.save()