Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def generate_dungeon(
- max_rooms: int, # Number of rooms allowed in the dungeon.
- room_min_size: int, # Min size of one room.
- room_max_size: int, # Max size of one room. Pick a rand size between this and the min size in order to carve out
- map_width: int,
- map_height: int,
- player: Entity, # This is here so we know where to place the player.
- ) -> GameMap:
- """Generate a new dungeon map."""
- dungeon = GameMap(map_width, map_height)
- map_width = dungeon.width # 80
- map_height = dungeon.height # 45
- # dungeon.tiles[0:1, 0] = tile_types.floor
- # [10:15, 1] Refers to first row, 10th position until 15th.
- # [75:79, 44] = Last five spaces on the last row
- """
- Our first goal is to create the cellular automata solution for randomly generating rooms.
- Step 1: Iterate through every position on the map, and randomly choose between floor or wall.
- Step 2: Differentiate between inner and outer positions (to be able to count 3x3 squares). Fill outer pos with wall
- Step 3: Count all walls in a 3x3 position, at each position with that position being center, and if there are at least
- 5 walls around it, that tile becomes a wall. Else, that tile is a floor
- """
- def do_simulation_step(dungeon):
- new_dungeon = GameMap(map_width, map_height)
- birth_limit = 4
- death_limit = 3
- row = 0
- for height in range(map_height):
- pos1 = 0
- pos2 = 1
- for width in range(map_width):
- count_alive = count_alive_neighbors(pos1, pos2, row, dungeon)
- if (dungeon.tiles[pos1:pos2, row][0][0]):
- if count_alive < death_limit:
- new_dungeon.tiles[pos1:pos2, row] = tile_types.floor
- else:
- new_dungeon.tiles[pos1:pos2, row] = tile_types.wall
- elif (dungeon.tiles[pos1:pos2, row][0][0] == False):
- if count_alive > birth_limit:
- new_dungeon.tiles[pos1:pos2, row] = tile_types.wall
- else:
- new_dungeon.tiles[pos1:pos2, row] = tile_types.floor
- pos1 += 1
- pos2 += 1
- row += 1
- return new_dungeon
- def count_alive_neighbors(pos1, pos2, row, dungeon):
- count = 0
- if (row == 0) or (row == map_height - 1) or (pos1 == 0) or (pos1 == map_width - 1):
- pass
- else:
- # Fix up this ugly ass code sometime.
- north = dungeon.tiles[pos1:pos2, row - 1][0][0]
- if north == True:
- count += 1
- north_east = dungeon.tiles[pos1 + 1:pos2 + 1, row - 1][0][0]
- if north_east == True:
- count += 1
- east = dungeon.tiles[pos1 + 1:pos2 + 1, row][0][0]
- if east == True:
- count += 1
- south_east = dungeon.tiles[pos1 + 1:pos2 + 1, row + 1][0][0]
- if south_east == True:
- count += 1
- south = dungeon.tiles[pos1:pos2, row + 1][0][0]
- if south == True:
- count += 1
- south_west = dungeon.tiles[pos1 - 1:pos2 - 1, row + 1][0][0]
- if south_west == True:
- count += 1
- west = dungeon.tiles[pos1 - 1:pos2 - 1, row][0][0]
- if west == True:
- count += 1
- north_west = dungeon.tiles[pos1 - 1:pos2 - 1, row - 1][0][0]
- if north_west == True:
- count += 1
- return count
- def starting_dungeon():
- row = 0
- for height in range(map_height):
- pos1 = 0
- pos2 = 1
- for width in range(map_width):
- if (row == 0) or (row == map_height-1) or (pos1 == 0) or (pos1 == map_width-1):
- dungeon.tiles[pos1:pos2, row] = tile_types.wall
- else:
- choice = random.randint(0,100)
- if choice >= 45:
- tile = tile_types.floor
- elif choice < 45:
- tile = tile_types.wall
- dungeon.tiles[pos1:pos2, row] = tile
- pos1 += 1
- pos2 += 1
- row += 1
- return dungeon
- for i in range(1):
- dungeon = starting_dungeon()
- dungeon = do_simulation_step(dungeon)
- row = 0
- for height in range(map_height):
- pos1 = 0
- pos2 = 1
- for width in range(map_width):
- if dungeon.tiles[pos1:pos2, row] == tile_types.wall:
- dungeon.tiles[pos1:pos2, row] = tile_types.floor
- else:
- dungeon.tiles[pos1:pos2, row] = tile_types.wall
- pos1+=1
- pos2+=1
- row+=1
- return dungeon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement