rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
branchtransitional_engine
changeset 15915 8f093b1b18bc
parent 15913 c5684cc62de8
child 15916 e82de0410da5
equal deleted inserted replaced
15914:c571d4b8879c 15915:8f093b1b18bc
     1 use integral_geometry::Size;
     1 use integral_geometry::Size;
       
     2 use std::collections::HashMap;
     2 use vec2d::Vec2D;
     3 use vec2d::Vec2D;
     3 use std::collections::HashMap;
       
     4 
     4 
     5 #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
     5 #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
     6 pub enum Tile {
     6 pub enum Tile {
     7     Empty,
     7     Empty,
     8     Outside,
     8     Outside,
    58     pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>(
    58     pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>(
    59         &mut self,
    59         &mut self,
    60         map_size: &Size,
    60         map_size: &Size,
    61         seed_fn: F,
    61         seed_fn: F,
    62         random_numbers: &mut I,
    62         random_numbers: &mut I,
    63         ) -> Vec2D<Tile> {
    63     ) -> Vec2D<Tile> {
    64         let mut land = Vec2D::new(&map_size, Tile::Empty);
    64         let mut land = Vec2D::new(&map_size, Tile::Empty);
    65 
    65 
    66         seed_fn(&mut land);
    66         seed_fn(&mut land);
    67 
    67 
    68         while self.collapse_step(&mut land, random_numbers) {}
    68         while self.collapse_step(&mut land, random_numbers) {}
   114 
   114 
   115     fn get_neighbors(&self, land: &Vec2D<Tile>, x: usize, y: usize) -> [Tile; 4] {
   115     fn get_neighbors(&self, land: &Vec2D<Tile>, x: usize, y: usize) -> [Tile; 4] {
   116         [
   116         [
   117             land.get(y, x + 1).map(|p| *p).unwrap_or_default(),
   117             land.get(y, x + 1).map(|p| *p).unwrap_or_default(),
   118             land.get(y + 1, x).map(|p| *p).unwrap_or_default(),
   118             land.get(y + 1, x).map(|p| *p).unwrap_or_default(),
   119             land.get(y, x.wrapping_sub(1)).map(|p| *p).unwrap_or_default(),
   119             land.get(y, x.wrapping_sub(1))
   120             land.get(y.wrapping_sub(1), x).map(|p| *p).unwrap_or_default(),
   120                 .map(|p| *p)
       
   121                 .unwrap_or_default(),
       
   122             land.get(y.wrapping_sub(1), x)
       
   123                 .map(|p| *p)
       
   124                 .unwrap_or_default(),
   121         ]
   125         ]
   122     }
   126     }
   123 }
   127 }
   124 
   128 
   125 #[cfg(test)]
   129 #[cfg(test)]