drunkard.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #[derive(PartialEq, Copy, Clone)]
  8. pub enum DrunkSpawnMode { StartingPoint, Random }
  9. pub struct DrunkardSettings {
  10. pub spawn_mode : DrunkSpawnMode,
  11. pub drunken_lifetime : i32,
  12. pub floor_percent: f32
  13. }
  14. pub struct DrunkardsWalkBuilder {
  15. map : Map,
  16. starting_position : Position,
  17. depth: i32,
  18. history: Vec<Map>,
  19. noise_areas : HashMap<i32, Vec<usize>>,
  20. settings : DrunkardSettings
  21. }
  22. impl MapBuilder for DrunkardsWalkBuilder {
  23. fn get_map(&self) -> Map {
  24. self.map.clone()
  25. }
  26. fn get_starting_position(&self) -> Position {
  27. self.starting_position.clone()
  28. }
  29. fn get_snapshot_history(&self) -> Vec<Map> {
  30. self.history.clone()
  31. }
  32. fn build_map(&mut self) {
  33. self.build();
  34. }
  35. fn spawn_entities(&mut self, ecs : &mut World) {
  36. for area in self.noise_areas.iter() {
  37. spawner::spawn_region(ecs, area.1, self.depth);
  38. }
  39. }
  40. fn take_snapshot(&mut self) {
  41. if SHOW_MAPGEN_VISUALIZER {
  42. let mut snapshot = self.map.clone();
  43. for v in snapshot.revealed_tiles.iter_mut() {
  44. *v = true;
  45. }
  46. self.history.push(snapshot);
  47. }
  48. }
  49. }
  50. impl DrunkardsWalkBuilder {
  51. pub fn new(new_depth : i32, settings: DrunkardSettings) -> DrunkardsWalkBuilder {
  52. DrunkardsWalkBuilder{
  53. map : Map::new(new_depth),
  54. starting_position : Position{ x: 0, y : 0 },
  55. depth : new_depth,
  56. history: Vec::new(),
  57. noise_areas : HashMap::new(),
  58. settings
  59. }
  60. }
  61. pub fn open_area(new_depth : i32) -> DrunkardsWalkBuilder {
  62. DrunkardsWalkBuilder{
  63. map : Map::new(new_depth),
  64. starting_position : Position{ x: 0, y : 0 },
  65. depth : new_depth,
  66. history: Vec::new(),
  67. noise_areas : HashMap::new(),
  68. settings : DrunkardSettings{
  69. spawn_mode: DrunkSpawnMode::StartingPoint,
  70. drunken_lifetime: 400,
  71. floor_percent: 0.5
  72. }
  73. }
  74. }
  75. pub fn open_halls(new_depth : i32) -> DrunkardsWalkBuilder {
  76. DrunkardsWalkBuilder{
  77. map : Map::new(new_depth),
  78. starting_position : Position{ x: 0, y : 0 },
  79. depth : new_depth,
  80. history: Vec::new(),
  81. noise_areas : HashMap::new(),
  82. settings : DrunkardSettings{
  83. spawn_mode: DrunkSpawnMode::Random,
  84. drunken_lifetime: 400,
  85. floor_percent: 0.5
  86. }
  87. }
  88. }
  89. pub fn winding_passages(new_depth : i32) -> DrunkardsWalkBuilder {
  90. DrunkardsWalkBuilder{
  91. map : Map::new(new_depth),
  92. starting_position : Position{ x: 0, y : 0 },
  93. depth : new_depth,
  94. history: Vec::new(),
  95. noise_areas : HashMap::new(),
  96. settings : DrunkardSettings{
  97. spawn_mode: DrunkSpawnMode::Random,
  98. drunken_lifetime: 100,
  99. floor_percent: 0.4
  100. }
  101. }
  102. }
  103. fn build(&mut self) {
  104. let mut rng = RandomNumberGenerator::new();
  105. // Set a central starting point
  106. self.starting_position = Position{ x: self.map.width / 2, y: self.map.height / 2 };
  107. let start_idx = self.map.xy_idx(self.starting_position.x, self.starting_position.y);
  108. self.map.tiles[start_idx] = TileType::Floor;
  109. let total_tiles = self.map.width * self.map.height;
  110. let desired_floor_tiles = (self.settings.floor_percent * total_tiles as f32) as usize;
  111. let mut floor_tile_count = self.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
  112. let mut digger_count = 0;
  113. let mut active_digger_count = 0;
  114. while floor_tile_count < desired_floor_tiles {
  115. let mut did_something = false;
  116. let mut drunk_x;
  117. let mut drunk_y;
  118. match self.settings.spawn_mode {
  119. DrunkSpawnMode::StartingPoint => {
  120. drunk_x = self.starting_position.x;
  121. drunk_y = self.starting_position.y;
  122. }
  123. DrunkSpawnMode::Random => {
  124. if digger_count == 0 {
  125. drunk_x = self.starting_position.x;
  126. drunk_y = self.starting_position.y;
  127. } else {
  128. drunk_x = rng.roll_dice(1, self.map.width - 3) + 1;
  129. drunk_y = rng.roll_dice(1, self.map.height - 3) + 1;
  130. }
  131. }
  132. }
  133. let mut drunk_life = self.settings.drunken_lifetime;
  134. while drunk_life > 0 {
  135. let drunk_idx = self.map.xy_idx(drunk_x, drunk_y);
  136. if self.map.tiles[drunk_idx] == TileType::Wall {
  137. did_something = true;
  138. }
  139. self.map.tiles[drunk_idx] = TileType::DownStairs;
  140. let stagger_direction = rng.roll_dice(1, 4);
  141. match stagger_direction {
  142. 1 => { if drunk_x > 2 { drunk_x -= 1; } }
  143. 2 => { if drunk_x < self.map.width-2 { drunk_x += 1; } }
  144. 3 => { if drunk_y > 2 { drunk_y -=1; } }
  145. _ => { if drunk_y < self.map.height-2 { drunk_y += 1; } }
  146. }
  147. drunk_life -= 1;
  148. }
  149. if did_something {
  150. self.take_snapshot();
  151. active_digger_count += 1;
  152. }
  153. digger_count += 1;
  154. for t in self.map.tiles.iter_mut() {
  155. if *t == TileType::DownStairs {
  156. *t = TileType::Floor;
  157. }
  158. }
  159. floor_tile_count = self.map.tiles.iter().filter(|a| **a == TileType::Floor).count();
  160. }
  161. println!("{} dwarves gave up their sobriety, of whom {} actually found a wall.", digger_count, active_digger_count);
  162. // Find all tiles we can reach from the starting point
  163. let exit_tile = remove_unreachable_areas_returning_most_distant(&mut self.map, start_idx);
  164. self.take_snapshot();
  165. // Place the stairs
  166. self.map.tiles[exit_tile] = TileType::DownStairs;
  167. self.take_snapshot();
  168. // Now we build a noise map for use in spawning entities later
  169. self.noise_areas = generate_voronoi_spawn_regions(&self.map, &mut rng);
  170. }
  171. }