cellular_automota.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 map_starts : Vec<i32> = vec![start_idx as i32];
  100. let dijkstra_map = rltk::DijkstraMap::new(self.map.width, self.map.height, &map_starts , &self.map, 200.0);
  101. let mut exit_tile = (0, 0.0f32);
  102. for (i, tile) in self.map.tiles.iter_mut().enumerate() {
  103. if *tile == TileType::Floor {
  104. let distance_to_start = dijkstra_map.map[i];
  105. // We can't get to this tile - so we'll make it a wall
  106. if distance_to_start == std::f32::MAX {
  107. *tile = TileType::Wall;
  108. } else {
  109. // If it is further away than our current exit candidate, move the exit
  110. if distance_to_start > exit_tile.1 {
  111. exit_tile.0 = i;
  112. exit_tile.1 = distance_to_start;
  113. }
  114. }
  115. }
  116. }
  117. self.take_snapshot();
  118. // Place the stairs
  119. self.map.tiles[exit_tile.0] = TileType::DownStairs;
  120. self.take_snapshot();
  121. // Now we build a noise map for use in spawning entities later
  122. let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64);
  123. noise.set_noise_type(rltk::NoiseType::Cellular);
  124. noise.set_frequency(0.08);
  125. noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan);
  126. for y in 1 .. self.map.height-1 {
  127. for x in 1 .. self.map.width-1 {
  128. let idx = self.map.xy_idx(x, y);
  129. if self.map.tiles[idx] == TileType::Floor {
  130. let cell_value_f = noise.get_noise(x as f32, y as f32) * 10240.0;
  131. let cell_value = cell_value_f as i32;
  132. if self.noise_areas.contains_key(&cell_value) {
  133. self.noise_areas.get_mut(&cell_value).unwrap().push(idx);
  134. } else {
  135. self.noise_areas.insert(cell_value, vec![idx]);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }