(world: &mut World, map_width: usize, map_height: usize)
| 73 | |
| 74 | impl Map { |
| 75 | pub fn generate(world: &mut World, map_width: usize, map_height: usize) -> Map { |
| 76 | const MAX_ROOMS: usize = 20; |
| 77 | const MAX_ATTEMPTS: usize = 200; |
| 78 | const MIN_SIZE: usize = 5; |
| 79 | const MAX_SIZE: usize = 15; |
| 80 | |
| 81 | let mut map = Map { |
| 82 | tiles: vec![MapTile::WALL; map_width * map_height], |
| 83 | width: map_width, |
| 84 | }; |
| 85 | |
| 86 | let mut rng = rand::thread_rng(); |
| 87 | |
| 88 | let mut rooms = vec![]; |
| 89 | |
| 90 | 'attempt: for _ in 0..MAX_ATTEMPTS { |
| 91 | let x = rng.gen_range(1..map_width - 1); |
| 92 | let y = rng.gen_range(1..map_height - 1); |
| 93 | |
| 94 | let width = usize::min(rng.gen_range(MIN_SIZE..MAX_SIZE), map_width - 1 - x); |
| 95 | let height = usize::min(rng.gen_range(MIN_SIZE..MAX_SIZE), map_height - 1 - y); |
| 96 | |
| 97 | if width < MIN_SIZE || height < MIN_SIZE { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | let room = Room { |
| 102 | x, |
| 103 | y, |
| 104 | width, |
| 105 | height, |
| 106 | }; |
| 107 | |
| 108 | for existing in &rooms { |
| 109 | if room.intersects(existing) { |
| 110 | continue 'attempt; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | map.carve_room(&room); |
| 115 | |
| 116 | rooms.push(room); |
| 117 | |
| 118 | if rooms.len() == MAX_ROOMS { |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | for pair in rooms.windows(2) { |
| 124 | let (ax, ay) = pair[0].centre(); |
| 125 | let (bx, by) = pair[1].centre(); |
| 126 | |
| 127 | if rng.gen_bool(0.5) { |
| 128 | map.carve_h_corridor(ax, bx, ay); |
| 129 | map.carve_v_corridor(bx, ay, by); |
| 130 | } else { |
| 131 | map.carve_v_corridor(ax, ay, by); |
| 132 | map.carve_h_corridor(ax, bx, by); |
nothing calls this directly
no test coverage detected