common.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. use super::{Map, Rect, TileType};
  2. use std::cmp::{max, min};
  3. use std::collections::HashMap;
  4. #[derive(PartialEq, Copy, Clone)]
  5. pub enum Symmetry { None, Horizontal, Vertical, Both }
  6. pub fn apply_room_to_map(map : &mut Map, room : &Rect) {
  7. for y in room.y1 +1 ..= room.y2 {
  8. for x in room.x1 + 1 ..= room.x2 {
  9. let idx = map.xy_idx(x, y);
  10. if idx > 0 && idx < ((map.width * map.height)-1) as usize {
  11. map.tiles[idx] = TileType::Floor;
  12. }
  13. }
  14. }
  15. }
  16. pub fn apply_horizontal_tunnel(map : &mut Map, x1:i32, x2:i32, y:i32) {
  17. for x in min(x1,x2) ..= max(x1,x2) {
  18. let idx = map.xy_idx(x, y);
  19. if idx > 0 && idx < map.width as usize * map.height as usize {
  20. map.tiles[idx as usize] = TileType::Floor;
  21. }
  22. }
  23. }
  24. pub fn apply_vertical_tunnel(map : &mut Map, y1:i32, y2:i32, x:i32) {
  25. for y in min(y1,y2) ..= max(y1,y2) {
  26. let idx = map.xy_idx(x, y);
  27. if idx > 0 && idx < map.width as usize * map.height as usize {
  28. map.tiles[idx as usize] = TileType::Floor;
  29. }
  30. }
  31. }
  32. /// Searches a map, removes unreachable areas and returns the most distant tile.
  33. pub fn remove_unreachable_areas_returning_most_distant(map : &mut Map, start_idx : usize) -> usize {
  34. map.populate_blocked();
  35. let map_starts : Vec<i32> = vec![start_idx as i32];
  36. let dijkstra_map = rltk::DijkstraMap::new(map.width, map.height, &map_starts , map, 400.0);
  37. let mut exit_tile = (0, 0.0f32);
  38. for (i, tile) in map.tiles.iter_mut().enumerate() {
  39. if *tile == TileType::Floor {
  40. let distance_to_start = dijkstra_map.map[i];
  41. // We can't get to this tile - so we'll make it a wall
  42. if distance_to_start == std::f32::MAX {
  43. *tile = TileType::Wall;
  44. } else {
  45. // If it is further away than our current exit candidate, move the exit
  46. if distance_to_start > exit_tile.1 {
  47. exit_tile.0 = i;
  48. exit_tile.1 = distance_to_start;
  49. }
  50. }
  51. }
  52. }
  53. exit_tile.0
  54. }
  55. /// Generates a Voronoi/cellular noise map of a region, and divides it into spawn regions.
  56. #[allow(clippy::map_entry)]
  57. pub fn generate_voronoi_spawn_regions(map: &Map, rng : &mut rltk::RandomNumberGenerator) -> HashMap<i32, Vec<usize>> {
  58. let mut noise_areas : HashMap<i32, Vec<usize>> = HashMap::new();
  59. let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64);
  60. noise.set_noise_type(rltk::NoiseType::Cellular);
  61. noise.set_frequency(0.08);
  62. noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan);
  63. for y in 1 .. map.height-1 {
  64. for x in 1 .. map.width-1 {
  65. let idx = map.xy_idx(x, y);
  66. if map.tiles[idx] == TileType::Floor {
  67. let cell_value_f = noise.get_noise(x as f32, y as f32) * 10240.0;
  68. let cell_value = cell_value_f as i32;
  69. if noise_areas.contains_key(&cell_value) {
  70. noise_areas.get_mut(&cell_value).unwrap().push(idx);
  71. } else {
  72. noise_areas.insert(cell_value, vec![idx]);
  73. }
  74. }
  75. }
  76. }
  77. noise_areas
  78. }
  79. pub fn draw_corridor(map: &mut Map, x1:i32, y1:i32, x2:i32, y2:i32) {
  80. let mut x = x1;
  81. let mut y = y1;
  82. while x != x2 || y != y2 {
  83. if x < x2 {
  84. x += 1;
  85. } else if x > x2 {
  86. x -= 1;
  87. } else if y < y2 {
  88. y += 1;
  89. } else if y > y2 {
  90. y -= 1;
  91. }
  92. let idx = map.xy_idx(x, y);
  93. map.tiles[idx] = TileType::Floor;
  94. }
  95. }
  96. pub fn paint(map: &mut Map, mode: Symmetry, brush_size: i32, x: i32, y:i32) {
  97. match mode {
  98. Symmetry::None => apply_paint(map, brush_size, x, y),
  99. Symmetry::Horizontal => {
  100. let center_x = map.width / 2;
  101. if x == center_x {
  102. apply_paint(map, brush_size, x, y);
  103. } else {
  104. let dist_x = i32::abs(center_x - x);
  105. apply_paint(map, brush_size, center_x + dist_x, y);
  106. apply_paint(map, brush_size, center_x - dist_x, y);
  107. }
  108. }
  109. Symmetry::Vertical => {
  110. let center_y = map.height / 2;
  111. if y == center_y {
  112. apply_paint(map, brush_size, x, y);
  113. } else {
  114. let dist_y = i32::abs(center_y - y);
  115. apply_paint(map, brush_size, x, center_y + dist_y);
  116. apply_paint(map, brush_size, x, center_y - dist_y);
  117. }
  118. }
  119. Symmetry::Both => {
  120. let center_x = map.width / 2;
  121. let center_y = map.height / 2;
  122. if x == center_x && y == center_y {
  123. apply_paint(map, brush_size, x, y);
  124. } else {
  125. let dist_x = i32::abs(center_x - x);
  126. apply_paint(map, brush_size, center_x + dist_x, y);
  127. apply_paint(map, brush_size, center_x - dist_x, y);
  128. let dist_y = i32::abs(center_y - y);
  129. apply_paint(map, brush_size, x, center_y + dist_y);
  130. apply_paint(map, brush_size, x, center_y - dist_y);
  131. }
  132. }
  133. }
  134. }
  135. fn apply_paint(map: &mut Map, brush_size: i32, x: i32, y: i32) {
  136. match brush_size {
  137. 1 => {
  138. let digger_idx = map.xy_idx(x, y);
  139. map.tiles[digger_idx] = TileType::Floor;
  140. }
  141. _ => {
  142. let half_brush_size = brush_size / 2;
  143. for brush_y in y-half_brush_size .. y+half_brush_size {
  144. for brush_x in x-half_brush_size .. x+half_brush_size {
  145. if brush_x > 1 && brush_x < map.width-1 && brush_y > 1 && brush_y < map.height-1 {
  146. let idx = map.xy_idx(brush_x, brush_y);
  147. map.tiles[idx] = TileType::Floor;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }