drunkard.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 DrunkardsWalkBuilder {
  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 DrunkardsWalkBuilder {
  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 DrunkardsWalkBuilder {
  42. pub fn new(new_depth : i32) -> DrunkardsWalkBuilder {
  43. DrunkardsWalkBuilder{
  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. // Set a central starting point
  55. self.starting_position = Position{ x: self.map.width / 2, y: self.map.height / 2 };
  56. let start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
  57. self.map.tiles[start_idx] = TileType::Floor;
  58. let total_tiles = self.map.width * self.map.height;
  59. let desired_floor_tiles = (total_tiles / 2) as usize;
  60. let mut floor_tile_count = self.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
  61. let mut digger_count = 0;
  62. let mut active_digger_count = 0;
  63. while floor_tile_count < desired_floor_tiles {
  64. let mut did_something = false;
  65. let mut drunk_x = self.starting_position.x;
  66. let mut drunk_y = self.starting_position.y;
  67. let mut drunk_life = 400;
  68. while drunk_life > 0 {
  69. let drunk_idx = self.map.xy_idx(drunk_x, drunk_y);
  70. if self.map.tiles[drunk_idx] == TileType::Wall {
  71. did_something = true;
  72. }
  73. self.map.tiles[drunk_idx] = TileType::DownStairs;
  74. let stagger_direction = rng.roll_dice(1, 4);
  75. match stagger_direction {
  76. 1 => { if drunk_x > 1 { drunk_x -= 1; } }
  77. 2 => { if drunk_x < self.map.width-1 { drunk_x += 1; } }
  78. 3 => { if drunk_y > 1 { drunk_y -=1; } }
  79. _ => { if drunk_y < self.map.height-1 { drunk_y += 1; } }
  80. }
  81. drunk_life -= 1;
  82. }
  83. if did_something {
  84. self.take_snapshot();
  85. active_digger_count += 1;
  86. }
  87. digger_count += 1;
  88. for t in self.map.tiles.iter_mut() {
  89. if *t == TileType::DownStairs {
  90. *t = TileType::Floor;
  91. }
  92. }
  93. floor_tile_count = self.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
  94. }
  95. println!("{} dwarves gave up their sobriety, of whom {} actually found a wall.", digger_count, active_digger_count);
  96. // Find all tiles we can reach from the starting point
  97. let map_starts : Vec<i32> = vec![start_idx as i32];
  98. let dijkstra_map = rltk::DijkstraMap::new(self.map.width, self.map.height, &map_starts , &self.map, 200.0);
  99. let mut exit_tile = (0, 0.0f32);
  100. for (i, tile) in self.map.tiles.iter_mut().enumerate() {
  101. if *tile == TileType::Floor {
  102. let distance_to_start = dijkstra_map.map[i];
  103. // We can't get to this tile - so we'll make it a wall
  104. if distance_to_start == std::f32::MAX {
  105. *tile = TileType::Wall;
  106. } else {
  107. // If it is further away than our current exit candidate, move the exit
  108. if distance_to_start > exit_tile.1 {
  109. exit_tile.0 = i;
  110. exit_tile.1 = distance_to_start;
  111. }
  112. }
  113. }
  114. }
  115. self.take_snapshot();
  116. // Place the stairs
  117. self.map.tiles[exit_tile.0] = TileType::DownStairs;
  118. self.take_snapshot();
  119. // Now we build a noise map for use in spawning entities later
  120. let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64);
  121. noise.set_noise_type(rltk::NoiseType::Cellular);
  122. noise.set_frequency(0.08);
  123. noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan);
  124. for y in 1 .. self.map.height-1 {
  125. for x in 1 .. self.map.width-1 {
  126. let idx = self.map.xy_idx(x, y);
  127. if self.map.tiles[idx] == TileType::Floor {
  128. let cell_value_f = noise.get_noise(x as f32, y as f32) * 10240.0;
  129. let cell_value = cell_value_f as i32;
  130. if self.noise_areas.contains_key(&cell_value) {
  131. self.noise_areas.get_mut(&cell_value).unwrap().push(idx);
  132. } else {
  133. self.noise_areas.insert(cell_value, vec![idx]);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }