Advertisement
xFazz

proc gen. cave

Feb 14th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. def generate_dungeon(
  2. max_rooms: int, # Number of rooms allowed in the dungeon.
  3. room_min_size: int, # Min size of one room.
  4. room_max_size: int, # Max size of one room. Pick a rand size between this and the min size in order to carve out
  5. map_width: int,
  6. map_height: int,
  7. player: Entity, # This is here so we know where to place the player.
  8. ) -> GameMap:
  9. """Generate a new dungeon map."""
  10. dungeon = GameMap(map_width, map_height)
  11.  
  12. map_width = dungeon.width # 80
  13. map_height = dungeon.height # 45
  14.  
  15. # dungeon.tiles[0:1, 0] = tile_types.floor
  16. # [10:15, 1] Refers to first row, 10th position until 15th.
  17. # [75:79, 44] = Last five spaces on the last row
  18.  
  19. """
  20. Our first goal is to create the cellular automata solution for randomly generating rooms.
  21.  
  22. Step 1: Iterate through every position on the map, and randomly choose between floor or wall.
  23. Step 2: Differentiate between inner and outer positions (to be able to count 3x3 squares). Fill outer pos with wall
  24. Step 3: Count all walls in a 3x3 position, at each position with that position being center, and if there are at least
  25. 5 walls around it, that tile becomes a wall. Else, that tile is a floor
  26. """
  27.  
  28. def do_simulation_step(dungeon):
  29. new_dungeon = GameMap(map_width, map_height)
  30. birth_limit = 4
  31. death_limit = 3
  32.  
  33. row = 0
  34. for height in range(map_height):
  35. pos1 = 0
  36. pos2 = 1
  37. for width in range(map_width):
  38. count_alive = count_alive_neighbors(pos1, pos2, row, dungeon)
  39.  
  40. if (dungeon.tiles[pos1:pos2, row][0][0]):
  41. if count_alive < death_limit:
  42. new_dungeon.tiles[pos1:pos2, row] = tile_types.floor
  43. else:
  44. new_dungeon.tiles[pos1:pos2, row] = tile_types.wall
  45. elif (dungeon.tiles[pos1:pos2, row][0][0] == False):
  46. if count_alive > birth_limit:
  47. new_dungeon.tiles[pos1:pos2, row] = tile_types.wall
  48. else:
  49. new_dungeon.tiles[pos1:pos2, row] = tile_types.floor
  50.  
  51. pos1 += 1
  52. pos2 += 1
  53. row += 1
  54. return new_dungeon
  55.  
  56. def count_alive_neighbors(pos1, pos2, row, dungeon):
  57. count = 0
  58. if (row == 0) or (row == map_height - 1) or (pos1 == 0) or (pos1 == map_width - 1):
  59. pass
  60. else:
  61. # Fix up this ugly ass code sometime.
  62. north = dungeon.tiles[pos1:pos2, row - 1][0][0]
  63. if north == True:
  64. count += 1
  65. north_east = dungeon.tiles[pos1 + 1:pos2 + 1, row - 1][0][0]
  66. if north_east == True:
  67. count += 1
  68. east = dungeon.tiles[pos1 + 1:pos2 + 1, row][0][0]
  69. if east == True:
  70. count += 1
  71. south_east = dungeon.tiles[pos1 + 1:pos2 + 1, row + 1][0][0]
  72. if south_east == True:
  73. count += 1
  74. south = dungeon.tiles[pos1:pos2, row + 1][0][0]
  75. if south == True:
  76. count += 1
  77. south_west = dungeon.tiles[pos1 - 1:pos2 - 1, row + 1][0][0]
  78. if south_west == True:
  79. count += 1
  80. west = dungeon.tiles[pos1 - 1:pos2 - 1, row][0][0]
  81. if west == True:
  82. count += 1
  83. north_west = dungeon.tiles[pos1 - 1:pos2 - 1, row - 1][0][0]
  84. if north_west == True:
  85. count += 1
  86.  
  87. return count
  88.  
  89. def starting_dungeon():
  90. row = 0
  91. for height in range(map_height):
  92. pos1 = 0
  93. pos2 = 1
  94. for width in range(map_width):
  95. if (row == 0) or (row == map_height-1) or (pos1 == 0) or (pos1 == map_width-1):
  96. dungeon.tiles[pos1:pos2, row] = tile_types.wall
  97. else:
  98. choice = random.randint(0,100)
  99. if choice >= 45:
  100. tile = tile_types.floor
  101. elif choice < 45:
  102. tile = tile_types.wall
  103.  
  104. dungeon.tiles[pos1:pos2, row] = tile
  105. pos1 += 1
  106. pos2 += 1
  107. row += 1
  108. return dungeon
  109.  
  110. for i in range(1):
  111. dungeon = starting_dungeon()
  112. dungeon = do_simulation_step(dungeon)
  113. row = 0
  114. for height in range(map_height):
  115. pos1 = 0
  116. pos2 = 1
  117. for width in range(map_width):
  118. if dungeon.tiles[pos1:pos2, row] == tile_types.wall:
  119. dungeon.tiles[pos1:pos2, row] = tile_types.floor
  120. else:
  121. dungeon.tiles[pos1:pos2, row] = tile_types.wall
  122. pos1+=1
  123. pos2+=1
  124. row+=1
  125.  
  126. return dungeon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement