cellular_automota.rs 4.6 KB

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