fn random_tiles(width: usize, height: usize, max_attempts: i32, buffer: &mut [Tile]) {
    let mut positions: Vec<(usize, usize)> = Vec::with_capacity(width * height);
    // We use these bounds to make sure that rooms don't touch the edges of walls
    for r in 1..(height - 3) {
        for c in 1..(width - 3) {
            positions.push((r, c));
        }
    }
    let mut attempts = max_attempts;
    let mut rooms: Vec<Room> = Vec::new();
    let mut rng = thread_rng();
    let sizes: Vec<usize> = (0..7).collect();
    while attempts > 0 {
        let &(r, c) = rng.choose(&positions).unwrap();
        // Chooses room sizes between 3 and 7, but makes sure not to hit the edge
        let w = *rng.choose(&sizes[3..min(7, width - c)]).unwrap();
        let h = *rng.choose(&sizes[3..min(7, height - r)]).unwrap();
        let room = Room::new((r, c), w, h);
        let larger = Room::new((r - 1, c - 1), w + 2, h + 2);
        let collided = rooms.iter().any(|r| r.collides_with(&larger));
        if collided {
            attempts -= 1;
        } else {
            rooms.push(room);
        }
    }
    // Now we place the tiles
    for room in &rooms {
        let (r0, c0) = room.top_left;
        let (rmax, cmax) = room.down_right;
        for r in r0..rmax {
            for c in c0..cmax {
                buffer[r * width + c] = Tile::Floor;
            }
        }
    }
}
/// Generate random tiles
/// Max attempts controls density, more or less.
/// It specifies how many times an overlap can fail before generation ends.
Generate random tiles
Max attempts controls density, more or less.
It specifies how many times an overlap can fail before generation ends.
Generate random tiles Max attempts controls density more or less . It specifies how many times an overlap can fail before generation ends .
fn random_tiles(width: usize, height: usize, max_attempts: i32, buffer: &mut [Tile]) {
    let mut positions: Vec<(usize, usize)> = Vec::with_capacity(width * height);
    // We use these bounds to make sure that rooms don't touch the edges of walls
    for r in 1..(height - 3) {
        for c in 1..(width - 3) {
            positions.push((r, c));
        }
    }
    let mut attempts = max_attempts;
    let mut rooms: Vec<Room> = Vec::new();
    let mut rng = thread_rng();
    let sizes: Vec<usize> = (0..7).collect();
    while attempts > 0 {
        let &(r, c) = rng.choose(&positions).unwrap();
        // Chooses room sizes between 3 and 7, but makes sure not to hit the edge
        let w = *rng.choose(&sizes[3..min(7, width - c)]).unwrap();
        let h = *rng.choose(&sizes[3..min(7, height - r)]).unwrap();
        let room = Room::new((r, c), w, h);
        let larger = Room::new((r - 1, c - 1), w + 2, h + 2);
        let collided = rooms.iter().any(|r| r.collides_with(&larger));
        if collided {
            attempts -= 1;
        } else {
            rooms.push(room);
        }
    }
    // Now we place the tiles
    for room in &rooms {
        let (r0, c0) = room.top_left;
        let (rmax, cmax) = room.down_right;
        for r in r0..rmax {
            for c in c0..cmax {
                buffer[r * width + c] = Tile::Floor;
            }
        }
    }
}
fn random_tiles ( width : usize , height : usize , max_attempts : i32 , buffer : & mut [ Tile ] ) { let mut positions : Vec < ( usize , usize ) > = Vec :: with_capacity ( width * height ) ; // We use these bounds to make sure that rooms don't touch the edges of walls for r in 1 .. ( height - 3 ) { for c in 1 .. ( width - 3 ) { positions . push ( ( r , c ) ) ; } } let mut attempts = max_attempts ; let mut rooms : Vec < Room > = Vec :: new ( ) ; let mut rng = thread_rng ( ) ; let sizes : Vec < usize > = ( 0 .. 7 ) . collect ( ) ; while attempts > 0 { let & ( r , c ) = rng . choose ( & positions ) . unwrap ( ) ; // Chooses room sizes between 3 and 7, but makes sure not to hit the edge let w = * rng . choose ( & sizes [ 3 .. min ( 7 , width - c ) ] ) . unwrap ( ) ; let h = * rng . choose ( & sizes [ 3 .. min ( 7 , height - r ) ] ) . unwrap ( ) ; let room = Room :: new ( ( r , c ) , w , h ) ; let larger = Room :: new ( ( r - 1 , c - 1 ) , w + 2 , h + 2 ) ; let collided = rooms . iter ( ) . any ( | r | r . collides_with ( & larger ) ) ; if collided { attempts -= 1 ; } else { rooms . push ( room ) ; } } // Now we place the tiles for room in & rooms { let ( r0 , c0 ) = room . top_left ; let ( rmax , cmax ) = room . down_right ; for r in r0 .. rmax { for c in c0 .. cmax { buffer [ r * width + c ] = Tile :: Floor ; } } } }
Generate random tiles
Max attempts controls density, more or less.
Generate random tiles Max attempts controls density more or less .
width
usize
height
usize
max_attempts
i32
buffer
&mut [Tile]
width
usize
height
usize
max_attempts
i32
buffer
&mut [Tile]