common.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. use super::{Map, Rect, TileType};
  2. use std::cmp::{max, min};
  3. use std::collections::HashMap;
  4. pub fn apply_room_to_map(map : &mut Map, room : &Rect) {
  5. for y in room.y1 +1 ..= room.y2 {
  6. for x in room.x1 + 1 ..= room.x2 {
  7. let idx = map.xy_idx(x, y);
  8. if idx > 0 && idx < ((map.width * map.height)-1) as usize {
  9. map.tiles[idx] = TileType::Floor;
  10. }
  11. }
  12. }
  13. }
  14. pub fn apply_horizontal_tunnel(map : &mut Map, x1:i32, x2:i32, y:i32) {
  15. for x in min(x1,x2) ..= max(x1,x2) {
  16. let idx = map.xy_idx(x, y);
  17. if idx > 0 && idx < map.width as usize * map.height as usize {
  18. map.tiles[idx as usize] = TileType::Floor;
  19. }
  20. }
  21. }
  22. pub fn apply_vertical_tunnel(map : &mut Map, y1:i32, y2:i32, x:i32) {
  23. for y in min(y1,y2) ..= max(y1,y2) {
  24. let idx = map.xy_idx(x, y);
  25. if idx > 0 && idx < map.width as usize * map.height as usize {
  26. map.tiles[idx as usize] = TileType::Floor;
  27. }
  28. }
  29. }
  30. /// Searches a map, removes unreachable areas and returns the most distant tile.
  31. pub fn remove_unreachable_areas_returning_most_distant(map : &mut Map, start_idx : usize) -> usize {
  32. map.populate_blocked();
  33. let map_starts : Vec<i32> = vec![start_idx as i32];
  34. let dijkstra_map = rltk::DijkstraMap::new(map.width, map.height, &map_starts , map, 200.0);
  35. let mut exit_tile = (0, 0.0f32);
  36. for (i, tile) in map.tiles.iter_mut().enumerate() {
  37. if *tile == TileType::Floor {
  38. let distance_to_start = dijkstra_map.map[i];
  39. // We can't get to this tile - so we'll make it a wall
  40. if distance_to_start == std::f32::MAX {
  41. *tile = TileType::Wall;
  42. } else {
  43. // If it is further away than our current exit candidate, move the exit
  44. if distance_to_start > exit_tile.1 {
  45. exit_tile.0 = i;
  46. exit_tile.1 = distance_to_start;
  47. }
  48. }
  49. }
  50. }
  51. exit_tile.0
  52. }
  53. /// Generates a Voronoi/cellular noise map of a region, and divides it into spawn regions.
  54. #[allow(clippy::map_entry)]
  55. pub fn generate_voronoi_spawn_regions(map: &Map, rng : &mut rltk::RandomNumberGenerator) -> HashMap<i32, Vec<usize>> {
  56. let mut noise_areas : HashMap<i32, Vec<usize>> = HashMap::new();
  57. let mut noise = rltk::FastNoise::seeded(rng.roll_dice(1, 65536) as u64);
  58. noise.set_noise_type(rltk::NoiseType::Cellular);
  59. noise.set_frequency(0.08);
  60. noise.set_cellular_distance_function(rltk::CellularDistanceFunction::Manhattan);
  61. for y in 1 .. map.height-1 {
  62. for x in 1 .. map.width-1 {
  63. let idx = map.xy_idx(x, y);
  64. if map.tiles[idx] == TileType::Floor {
  65. let cell_value_f = noise.get_noise(x as f32, y as f32) * 10240.0;
  66. let cell_value = cell_value_f as i32;
  67. if noise_areas.contains_key(&cell_value) {
  68. noise_areas.get_mut(&cell_value).unwrap().push(idx);
  69. } else {
  70. noise_areas.insert(cell_value, vec![idx]);
  71. }
  72. }
  73. }
  74. }
  75. noise_areas
  76. }