cellular_automota.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use super::{MapBuilder, Map,
  2. TileType, Position, spawner, SHOW_MAPGEN_VISUALIZER};
  3. use rltk::RandomNumberGenerator;
  4. use specs::prelude::*;
  5. use std::collections::HashMap;
  6. pub struct CellularAutomotaBuilder {
  7. map : Map,
  8. starting_position : Position,
  9. depth: i32,
  10. history: Vec<Map>,
  11. noise_areas : HashMap<i32, Vec<usize>>
  12. }
  13. impl MapBuilder for CellularAutomotaBuilder {
  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 area in self.noise_areas.iter() {
  28. spawner::spawn_region(ecs, area.1, 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 CellularAutomotaBuilder {
  42. pub fn new(new_depth : i32) -> CellularAutomotaBuilder {
  43. CellularAutomotaBuilder{
  44. map : Map::new(new_depth),
  45. starting_position : Position{ x: 0, y : 0 },
  46. depth : new_depth,
  47. history: Vec::new(),
  48. noise_areas : HashMap::new()
  49. }
  50. }
  51. #[allow(clippy::map_entry)]
  52. fn build(&mut self) {
  53. let mut rng = RandomNumberGenerator::new();
  54. // First we completely randomize the map, setting 55% of it to be floor.
  55. for y in 1..self.map.height-1 {
  56. for x in 1..self.map.width-1 {
  57. let roll = rng.roll_dice(1, 100);
  58. let idx = self.map.xy_idx(x, y);
  59. if roll > 55 { self.map.tiles[idx] = TileType::Floor }
  60. else { self.map.tiles[idx] = TileType::Wall }
  61. }
  62. }
  63. self.take_snapshot();
  64. // Now we iteratively apply cellular automota rules
  65. for _i in 0..15 {
  66. let mut newtiles = self.map.tiles.clone();
  67. for y in 1..self.map.height-1 {
  68. for x in 1..self.map.width-1 {
  69. let idx = self.map.xy_idx(x, y);
  70. let mut neighbors = 0;
  71. if self.map.tiles[idx - 1] == TileType::Wall { neighbors += 1; }
  72. if self.map.tiles[idx + 1] == TileType::Wall { neighbors += 1; }
  73. if self.map.tiles[idx - self.map.width as usize] == TileType::Wall { neighbors += 1; }
  74. if self.map.tiles[idx + self.map.width as usize] == TileType::Wall { neighbors += 1; }
  75. if self.map.tiles[idx - (self.map.width as usize - 1)] == TileType::Wall { neighbors += 1; }
  76. if self.map.tiles[idx - (self.map.width as usize + 1)] == TileType::Wall { neighbors += 1; }
  77. if self.map.tiles[idx + (self.map.width as usize - 1)] == TileType::Wall { neighbors += 1; }
  78. if self.map.tiles[idx + (self.map.width as usize + 1)] == TileType::Wall { neighbors += 1; }
  79. if neighbors > 4 || neighbors == 0 {
  80. newtiles[idx] = TileType::Wall;
  81. }
  82. else {
  83. newtiles[idx] = TileType::Floor;
  84. }
  85. }
  86. }
  87. self.map.tiles = newtiles.clone();
  88. self.take_snapshot();
  89. }
  90. // Find a starting point; start at the middle and walk left until we find an open tile
  91. self.starting_position = Position{ x: self.map.width / 2, y : self.map.height / 2 };
  92. let mut start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
  93. while self.map.tiles[start_idx] != TileType::Floor {
  94. self.starting_position.x -= 1;
  95. start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
  96. }
  97. self.take_snapshot();
  98. // Find all tiles we can reach from the starting point
  99. let exit_tile = remove_unreachable_areas_returning_most_distant(&mut self.map, start_idx);
  100. self.take_snapshot();
  101. // Place the stairs
  102. self.map.tiles[exit_tile] = TileType::DownStairs;
  103. self.take_snapshot();
  104. // Now we build a noise map for use in spawning entities later
  105. self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
  106. }
  107. }