def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, ) -> GameMap: """Generate a new dungeon map.""" player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) #rooms = naiveGenerate(dungeon, max_rooms, room_min_size, room_max_size) rooms = bspGenerate(dungeon, player, engine, max_rooms, room_min_size, room_max_size, 1) player.place(*rooms[0].center, dungeon) print_dungeon(dungeon) for room in rooms: place_entities(room, dungeon, engine.game_world.current_floor) last_room = rooms[len(rooms) - 1] dungeon.tiles[last_room.center] = tile_types.down_stairs dungeon.downstairs_location = last_room.center return dungeon
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, max_monsters_per_room: int, max_items_per_room: int, engine: Engine, ) -> GameMap: """Genererer et Dungeon Map Args: max_rooms (int): Maks antal af rum i dungeon room_min_size (int): Mindste størrelse af et rum room_max_size (int): Største størrelse af et rum map_width (int): Hele Dungeons bredde map_height (int): Hele Dungeons højde player ([type]): Player entity Returns: GameMap: Området hvor PCen er. """ player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player, ]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for room in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) pos_x = random.randint(0, dungeon.width - room_width - 1) pos_y = random.randint(0, dungeon.height - room_height - 1) new_room = RectangularRoom(pos_x, pos_y, room_width, room_height) if any(new_room.intersects(other_room) for other_room in rooms): continue dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: # Første rum, hvor spilleren starter player.place(*new_room.center, dungeon) else: for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, max_monsters_per_room, max_items_per_room) dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room rooms.append(new_room) return dungeon
def _place_downstairs( self, rooms: List[RectangularRoom], dungeon: GameMap ): field = self._get_random_room(rooms).random_field dungeon.tiles[field] = tile_types.down_stairs dungeon.downstairs_location = field
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, max_monsters_per_room: int, max_items_per_room: int, engine: Engine, ) -> GameMap: """Generate a new dungeon map.""" player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, dungeon.width - room_width - 1) y = random.randint(0, dungeon.height - room_height - 1) # "RectangularRoom" class makes rectangles easier to work with new_room = RectangularRoom(x, y, room_width, room_height) # Run through the other rooms and see if they intersect with this one. if any(new_room.intersects(other_room) for other_room in rooms): continue # This room intersects, go to the next attempt. # If there are no intersections then the room is valid. # Dig out this rooms inner area. dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: # The first room, where the player starts. player.place(*new_room.center, dungeon) else: # Dig out a tunnel between this room and the previous one. for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, max_monsters_per_room, max_items_per_room) dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room rooms.append(new_room) return dungeon
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, ) -> GameMap: """Generate a new dungeon map""" player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) #keep a running list of all of the rooms rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): #use given minimum and maximum room sizes to set the room's width and height room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, dungeon.width - room_width - 1) y = random.randint(0, dungeon.height - room_height - 1) #"RectangularRoom" class makes rectangles easier to work with new_room = RectangularRoom(x, y, room_width, room_height) #Run through the other rooms to see if they intersect with this one if any(new_room.intersects(other_room) for other_room in rooms): continue #This room intersects, so go on to next attempt #If there are no intersections then the room is valid. #dig out this room's inner area. dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: #The first room, where the player starts player.place(*new_room.center, dungeon) else: #All rooms that come after the first #Dig out a tunnel between this room and the previous room for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, engine.game_world.current_floor) dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room #Finally, append the new room to the list. rooms.append(new_room) return dungeon
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, ) -> GameMap: '''Generate a new dungeon map.''' player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, dungeon.width - room_width - 1) y = random.randint(0, dungeon.height - room_height - 1) # RectangularRoom class makes rectangles a bit easier to work w new_room = RectangularRoom(x, y, room_width, room_height) # spot some intersections if any(new_room.intersects(other_room) for other_room in rooms): continue # room intersects, so go to the next one # dig out room inner area dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: # first room, where player starts player.place(*new_room.center, dungeon) else: # dig a tunnel between this and the previous for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, engine.game_world.current_floor) # the very last room added will get the `tile_types.down_stairs` tile dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room # append room to list rooms.append(new_room) return dungeon
def generate_dungeon( level, map_width: int, map_height: int, max_monsters_per_room: int, max_items_per_room: int, engine: Engine, ) -> GameMap: rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) player = engine.player dungeon = GameMap(level, engine, map_width, map_height, entities=[player]) # New root node bsp = tcod.bsp.BSP(0, 0, map_width - 3, map_height - 3) # Split into nodes bsp.split_recursive(DEPTH, MIN_SIZE + 1, MIN_SIZE + 1, 1.5, 1.5) # Traverse the nodes and create rooms order = bsp.inverted_level_order() for node in order: traverse_node(node, dungeon, rooms) player_room = random.choice(rooms) player.place(*player_room.center, dungeon) last_room = random.choice(rooms) max_distance = abs(last_room.center[0] - player.x) + abs(last_room.center[1] - player.y) for room in rooms: if room != player_room: place_entities(room, dungeon, max_monsters_per_room, max_items_per_room) distance = abs(room.center[0] - player.x) + abs(room.center[1] - player.y) if distance > max_distance: last_room = room max_distance = distance center_of_last_room = last_room.center dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room return dungeon
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, ) -> GameMap: player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, dungeon.width - room_width - 1) y = random.randint(0, dungeon.height - room_height - 1) new_room = RectangularRoom(x, y, room_width, room_height) if any(new_room.intersects(other_room) for other_room in rooms): continue # this room intersects an existing room; try again # this room is valid, so dig it out dungeon.tiles[new_room.inner] = tile_types.FLOOR if len(rooms) == 0: player.place(*new_room.center, dungeon) else: for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.FLOOR center_of_last_room = new_room.center place_entities(new_room, dungeon, engine.game_world.current_floor) rooms.append(new_room) dungeon.tiles[center_of_last_room] = tile_types.STAIRS_DOWN dungeon.downstairs_location = center_of_last_room return dungeon
def generate_dungeon(max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine) -> GameMap: """Generate a new dungeon map""" player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for _ in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, dungeon.width - room_width - 1) y = random.randint(0, dungeon.height - room_height - 1) new_room = RectangularRoom(x, y, room_width, room_height) if any(new_room.intersects(other_room) for other_room in rooms): continue dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: player.place(*new_room.center, dungeon) else: for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, engine.game_world.current_floor) dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room rooms.append(new_room) return dungeon
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, ) -> GameMap: """Generate a new dungeon map.""" player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(1, dungeon.width - room_width - 2) y = random.randint(1, dungeon.height - room_height - 2) # "RectangularRoom" class makes rectangles easier to work with new_room = RectangularRoom(x, y, room_width, room_height) # Run through the other rooms and see if they intersect with this one. if any(new_room.intersects(other_room) for other_room in rooms): continue # This room intersects, so go to the next attempt. # If there are no intersections then the room is valid. # Dig out this rooms inner area. dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: # The first room, where the player starts. player.place(*new_room.center, dungeon) else: # All rooms after the first. # Dig out a tunnel between this room and the previous one. for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, engine.game_world.current_floor) dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room # Finally, append the new room to the list. rooms.append(new_room) # print(dungeon.tiles) # Bit Masking for Walls Prior to Placing Any Map "Objects" dungeon.walkable_bitmasking = Bitmasking.generate( dungeon.tiles['walkable']) # Reset all Values in a Perimeter around Map(weird bit masking issues) BLANK_TILE = 255 for x in range(dungeon.width): dungeon.walkable_bitmasking[x][dungeon.height - 1] = BLANK_TILE dungeon.walkable_bitmasking[x][0] = BLANK_TILE for y in range(dungeon.height): dungeon.walkable_bitmasking[dungeon.width - 1][y] = BLANK_TILE dungeon.walkable_bitmasking[0][y] = BLANK_TILE # Weird Bottom Left Corner Bug? BOTTOM_LEFT_CORNER = 5 dungeon.walkable_bitmasking[1][1] = BLANK_TILE if dungeon.walkable_bitmasking[1][ 2] != 255 and dungeon.walkable_bitmasking[2][1] != 255: dungeon.walkable_bitmasking[1][1] = BOTTOM_LEFT_CORNER # Printing for Reference # wall_space_print = np.rot90(dungeon.walkable_bitmasking, k=1, axes=(0, 1)) # np.set_printoptions(threshold=sys.maxsize, linewidth=sys.maxsize) # print("dungeon.walkalble_bitmasking:") # print(wall_space_print) return dungeon
def generate_spaceship( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, floor: int, lvl: int, ) -> GameMap: """создает новую карту""" player = engine.player spaceship = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, spaceship.width - room_width - 1) y = random.randint(0, spaceship.height - room_height - 1) new_room = RectangularRoom(x, y, room_width, room_height) # Проверяет, не накладываются комнаты друг на друга. if any(new_room.intersects(other_room) for other_room in rooms): continue # Комната накладывается, поэтому она не появляется на карте. # Если комната не накладывается на другую, то она подходит. # Внутренность комнаты становится полом. spaceship.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: # первая комната, в которой появится персонаж player.place(*new_room.center, spaceship) else: # все комнаты после первой # создает туннель между этой комнатой и предыдущей for x, y in tunnel_between(rooms[-1].center, new_room.center): spaceship.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, spaceship, floor) if floor < lvl: spaceship.tiles[center_of_last_room] = tile_types.down_stairs spaceship.downstairs_location = center_of_last_room # добавляет новую комнату в список rooms.append(new_room) if floor == lvl: room = rooms[-1] x = random.randint(room.x1 + 1, room.x2 - 1) y = random.randint(room.y1 + 1, room.y2 - 1) if not any(entity.x == x and entity.y == y for entity in spaceship.entities): entity_factories.key.spawn(spaceship, x, y) else: room = rooms[-2] entity_factories.key.spawn(spaceship, x, y) return spaceship
def generate_dungeon( max_rooms: int, room_min_size: int, room_max_size: int, map_width: int, map_height: int, engine: Engine, ) -> GameMap: """generate a new map""" player = engine.player dungeon = GameMap(engine, map_width, map_height, entities=[player]) rooms: List[RectangularRoom] = [] center_of_last_room = (0, 0) for r in range(max_rooms): room_width = random.randint(room_min_size, room_max_size) room_height = random.randint(room_min_size, room_max_size) x = random.randint(0, dungeon.width - room_width - 1) y = random.randint(0, dungeon.height - room_height - 1) # RR class makes rectangles easier to work with this one new_room = RectangularRoom(x, y, room_width, room_height) # run thru the other rooms and see if they intersect with this one if any(new_room.intersects(other_room) for other_room in rooms): continue # this room intersects, go to next attempt, else valid # dig out rooms in this area dungeon.tiles[new_room.inner] = tile_types.floor if len(rooms) == 0: # starting room player.place(*new_room.center, dungeon) else: # all rooms after the first # dig out a tunnel for x, y in tunnel_between(rooms[-1].center, new_room.center): dungeon.tiles[x, y] = tile_types.floor center_of_last_room = new_room.center place_entities(new_room, dungeon, engine.game_world.current_floor) dungeon.tiles[center_of_last_room] = tile_types.down_stairs dungeon.downstairs_location = center_of_last_room # append the new room rooms.append(new_room) return dungeon room_1 = RectangularRoom(x=20, y=15, width=10, height=15) room_2 = RectangularRoom(x=35, y=15, width=10, height=15) dungeon.tiles[room_1.inner] = tile_types.floor dungeon.tiles[room_2.inner] = tile_types.floor for x, y in tunnel_between(room_2.center, room_1.center): dungeon.tiles[x, y] = tile_types.floor return dungeon