bsp_dungeon.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. use super::{MapBuilder, Map, Rect, apply_room_to_map,
  2. TileType, Position, spawner, SHOW_MAPGEN_VISUALIZER, draw_corridor};
  3. use rltk::RandomNumberGenerator;
  4. use specs::prelude::*;
  5. pub struct BspDungeonBuilder {
  6. map : Map,
  7. starting_position : Position,
  8. depth: i32,
  9. rooms: Vec<Rect>,
  10. history: Vec<Map>,
  11. rects: Vec<Rect>
  12. }
  13. impl MapBuilder for BspDungeonBuilder {
  14. fn get_map(&self) -> Map {
  15. self.map.clone()
  16. }
  17. fn get_starting_position(&self) -> Position {
  18. self.starting_position.clone()
  19. }
  20. fn get_snapshot_history(&self) -> Vec<Map> {
  21. self.history.clone()
  22. }
  23. fn build_map(&mut self) {
  24. self.build();
  25. }
  26. fn spawn_entities(&mut self, ecs : &mut World) {
  27. for room in self.rooms.iter().skip(1) {
  28. spawner::spawn_room(ecs, room, self.depth);
  29. }
  30. }
  31. fn take_snapshot(&mut self) {
  32. if SHOW_MAPGEN_VISUALIZER {
  33. let mut snapshot = self.map.clone();
  34. for v in snapshot.revealed_tiles.iter_mut() {
  35. *v = true;
  36. }
  37. self.history.push(snapshot);
  38. }
  39. }
  40. }
  41. impl BspDungeonBuilder {
  42. pub fn new(new_depth : i32) -> BspDungeonBuilder {
  43. BspDungeonBuilder{
  44. map : Map::new(new_depth),
  45. starting_position : Position{ x: 0, y : 0 },
  46. depth : new_depth,
  47. rooms: Vec::new(),
  48. history: Vec::new(),
  49. rects: Vec::new()
  50. }
  51. }
  52. fn build(&mut self) {
  53. let mut rng = RandomNumberGenerator::new();
  54. self.rects.clear();
  55. self.rects.push( Rect::new(2, 2, self.map.width-5, self.map.height-5) ); // Start with a single map-sized rectangle
  56. let first_room = self.rects[0];
  57. self.add_subrects(first_room); // Divide the first room
  58. // Up to 240 times, we get a random rectangle and divide it. If its possible to squeeze a
  59. // room in there, we place it and add it to the rooms list.
  60. let mut n_rooms = 0;
  61. while n_rooms < 240 {
  62. let rect = self.get_random_rect(&mut rng);
  63. let candidate = self.get_random_sub_rect(rect, &mut rng);
  64. if self.is_possible(candidate) {
  65. apply_room_to_map(&mut self.map, &candidate);
  66. self.rooms.push(candidate);
  67. self.add_subrects(rect);
  68. self.take_snapshot();
  69. }
  70. n_rooms += 1;
  71. }
  72. // Now we sort the rooms
  73. self.rooms.sort_by(|a,b| a.x1.cmp(&b.x1) );
  74. // Now we want corridors
  75. for i in 0..self.rooms.len()-1 {
  76. let room = self.rooms[i];
  77. let next_room = self.rooms[i+1];
  78. let start_x = room.x1 + (rng.roll_dice(1, i32::abs(room.x1 - room.x2))-1);
  79. let start_y = room.y1 + (rng.roll_dice(1, i32::abs(room.y1 - room.y2))-1);
  80. let end_x = next_room.x1 + (rng.roll_dice(1, i32::abs(next_room.x1 - next_room.x2))-1);
  81. let end_y = next_room.y1 + (rng.roll_dice(1, i32::abs(next_room.y1 - next_room.y2))-1);
  82. draw_corridor(&mut self.map, start_x, start_y, end_x, end_y);
  83. self.take_snapshot();
  84. }
  85. // Don't forget the stairs
  86. let stairs = self.rooms[self.rooms.len()-1].center();
  87. let stairs_idx = self.map.xy_idx(stairs.0, stairs.1);
  88. self.map.tiles[stairs_idx] = TileType::DownStairs;
  89. self.take_snapshot();
  90. // Set player start
  91. let start = self.rooms[0].center();
  92. self.starting_position = Position{ x: start.0, y: start.1 };
  93. }
  94. fn add_subrects(&mut self, rect : Rect) {
  95. let width = i32::abs(rect.x1 - rect.x2);
  96. let height = i32::abs(rect.y1 - rect.y2);
  97. let half_width = i32::max(width / 2, 1);
  98. let half_height = i32::max(height / 2, 1);
  99. self.rects.push(Rect::new( rect.x1, rect.y1, half_width, half_height ));
  100. self.rects.push(Rect::new( rect.x1, rect.y1 + half_height, half_width, half_height ));
  101. self.rects.push(Rect::new( rect.x1 + half_width, rect.y1, half_width, half_height ));
  102. self.rects.push(Rect::new( rect.x1 + half_width, rect.y1 + half_height, half_width, half_height ));
  103. }
  104. fn get_random_rect(&mut self, rng : &mut RandomNumberGenerator) -> Rect {
  105. if self.rects.len() == 1 { return self.rects[0]; }
  106. let idx = (rng.roll_dice(1, self.rects.len() as i32)-1) as usize;
  107. self.rects[idx]
  108. }
  109. fn get_random_sub_rect(&self, rect : Rect, rng : &mut RandomNumberGenerator) -> Rect {
  110. let mut result = rect;
  111. let rect_width = i32::abs(rect.x1 - rect.x2);
  112. let rect_height = i32::abs(rect.y1 - rect.y2);
  113. let w = i32::max(3, rng.roll_dice(1, i32::min(rect_width, 10))-1) + 1;
  114. let h = i32::max(3, rng.roll_dice(1, i32::min(rect_height, 10))-1) + 1;
  115. result.x1 += rng.roll_dice(1, 6)-1;
  116. result.y1 += rng.roll_dice(1, 6)-1;
  117. result.x2 = result.x1 + w;
  118. result.y2 = result.y1 + h;
  119. result
  120. }
  121. fn is_possible(&self, rect : Rect) -> bool {
  122. let mut expanded = rect;
  123. expanded.x1 -= 2;
  124. expanded.x2 += 2;
  125. expanded.y1 -= 2;
  126. expanded.y2 += 2;
  127. let mut can_build = true;
  128. for y in expanded.y1 ..= expanded.y2 {
  129. for x in expanded.x1 ..= expanded.x2 {
  130. if x > self.map.width-2 { can_build = false; }
  131. if y > self.map.height-2 { can_build = false; }
  132. if x < 1 { can_build = false; }
  133. if y < 1 { can_build = false; }
  134. if can_build {
  135. let idx = self.map.xy_idx(x, y);
  136. if self.map.tiles[idx] != TileType::Wall {
  137. can_build = false;
  138. }
  139. }
  140. }
  141. }
  142. can_build
  143. }
  144. }