drunkard.rs 8.1 KB

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