rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
branchtransitional_engine
changeset 15954 9502611bffc1
parent 15953 d46ad15c6dec
child 15955 b0e8cc72bfef
equal deleted inserted replaced
15953:d46ad15c6dec 15954:9502611bffc1
    25 }
    25 }
    26 
    26 
    27 pub struct WavefrontCollapse {
    27 pub struct WavefrontCollapse {
    28     rules: Vec<CollapseRule>,
    28     rules: Vec<CollapseRule>,
    29     grid: Vec2D<Tile>,
    29     grid: Vec2D<Tile>,
       
    30     wrap: bool,
    30 }
    31 }
    31 
    32 
    32 impl Default for WavefrontCollapse {
    33 impl Default for WavefrontCollapse {
    33     fn default() -> Self {
    34     fn default() -> Self {
    34         Self {
    35         Self {
    35             rules: Vec::new(),
    36             rules: Vec::new(),
    36             grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
    37             grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
       
    38             wrap: false,
    37         }
    39         }
    38     }
    40     }
    39 }
    41 }
    40 
    42 
    41 impl WavefrontCollapse {
    43 impl WavefrontCollapse {
       
    44     pub fn new(wrap: bool) -> Self {
       
    45         Self {
       
    46             rules: Vec::new(),
       
    47             grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
       
    48             wrap,
       
    49         }
       
    50     }
       
    51 
    42     pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>(
    52     pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>(
    43         &mut self,
    53         &mut self,
    44         map_size: &Size,
    54         map_size: &Size,
    45         seed_fn: F,
    55         seed_fn: F,
    46         random_numbers: &mut I,
    56         random_numbers: &mut I,
    55     pub fn set_rules(&mut self, rules: Vec<CollapseRule>) {
    65     pub fn set_rules(&mut self, rules: Vec<CollapseRule>) {
    56         self.rules = rules;
    66         self.rules = rules;
    57     }
    67     }
    58 
    68 
    59     fn get_tile(&self, y: usize, x: usize) -> Tile {
    69     fn get_tile(&self, y: usize, x: usize) -> Tile {
       
    70         let x = if self.wrap {
       
    71             if x == usize::MAX {
       
    72                 self.grid.width() - 1
       
    73             } else {
       
    74                 if x == self.grid.width() {
       
    75                     0
       
    76                 } else {
       
    77                     x
       
    78                 }
       
    79             }
       
    80         } else {
       
    81             x
       
    82         };
       
    83         
    60         self.grid.get(y, x).copied().unwrap_or_default()
    84         self.grid.get(y, x).copied().unwrap_or_default()
    61     }
    85     }
    62 
    86 
    63     fn collapse_step<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) -> bool {
    87     fn collapse_step<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) -> bool {
    64         let mut tiles_to_collapse = (usize::max_value(), Vec::new());
    88         let mut tiles_to_collapse = (usize::max_value(), Vec::new());