common.rs 5.8 KB

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