rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
changeset 16059 2acea266d297
parent 16058 de01be16df95
child 16073 5d302b12d837
equal deleted inserted replaced
16058:de01be16df95 16059:2acea266d297
     6 use vec2d::Vec2D;
     6 use vec2d::Vec2D;
     7 
     7 
     8 #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
     8 #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
     9 pub enum Tile {
     9 pub enum Tile {
    10     Empty,
    10     Empty,
    11     Outside,
    11     OutsideBegin,
       
    12     OutsideFill,
       
    13     OutsideEnd,
    12     Numbered(usize),
    14     Numbered(usize),
    13 }
       
    14 
       
    15 impl Default for Tile {
       
    16     fn default() -> Self {
       
    17         Tile::Outside
       
    18     }
       
    19 }
    15 }
    20 
    16 
    21 #[derive(Debug)]
    17 #[derive(Debug)]
    22 pub struct CollapseRule {
    18 pub struct CollapseRule {
    23     pub weight: u32,
    19     pub weight: u32,
    81             }
    77             }
    82         } else {
    78         } else {
    83             x
    79             x
    84         };
    80         };
    85 
    81 
    86         self.grid.get(y, x).copied().unwrap_or_default()
    82         self.grid.get(y, x).copied().unwrap_or_else(|| {
       
    83             let x_out = x >= self.grid.width();
       
    84 
       
    85             if x_out {
       
    86                 let y_at_begin = y == 0;
       
    87                 let y_at_end = y.wrapping_add(1) == self.grid.height();
       
    88                 if y_at_begin {
       
    89                     Tile::OutsideBegin
       
    90                 } else if y_at_end {
       
    91                     Tile::OutsideEnd
       
    92                 } else {
       
    93                     Tile::OutsideFill
       
    94                 }
       
    95             } else {
       
    96                 // if not x, then it is y
       
    97 
       
    98                 let x_at_begin = x == 0;
       
    99                 let x_at_end = x.wrapping_add(1) == self.grid.width();
       
   100 
       
   101                 if x_at_begin {
       
   102                     Tile::OutsideBegin
       
   103                 } else if x_at_end {
       
   104                     Tile::OutsideEnd
       
   105                 } else {
       
   106                     Tile::OutsideFill
       
   107                 }
       
   108             }
       
   109         })
    87     }
   110     }
    88 
   111 
    89     fn collapse_step(&mut self, random_numbers: &mut impl Rng) -> bool {
   112     fn collapse_step(&mut self, random_numbers: &mut impl Rng) -> bool {
    90         let mut tiles_to_collapse = (usize::max_value(), Vec::new());
   113         let mut tiles_to_collapse = (usize::MAX, Vec::new());
    91 
   114 
    92         // Iterate through the tiles in the land
   115         // Iterate through the tiles in the land
    93         for x in 0..self.grid.width() {
   116         for x in 0..self.grid.width() {
    94             for y in 0..self.grid.height() {
   117             for y in 0..self.grid.height() {
    95                 let current_tile = self.get_tile(y, x);
   118                 let current_tile = self.get_tile(y, x);
    96 
   119 
    97                 if let Tile::Empty = current_tile {
   120                 if let Tile::Empty = current_tile {
    98                     // calc entropy
   121                     // calc entropy
    99                     let right_tile = self.get_tile(y, x + 1);
   122                     let right_tile = self.get_tile(y, x.wrapping_add(1));
   100                     let bottom_tile = self.get_tile(y + 1, x);
   123                     let bottom_tile = self.get_tile(y.wrapping_add(1), x);
   101                     let left_tile = self.get_tile(y, x.wrapping_sub(1));
   124                     let left_tile = self.get_tile(y, x.wrapping_sub(1));
   102                     let top_tile = self.get_tile(y.wrapping_sub(1), x);
   125                     let top_tile = self.get_tile(y.wrapping_sub(1), x);
   103 
   126 
   104                     let possibilities: Vec<(u32, Tile)> = self
   127                     let possibilities: Vec<(u32, Tile)> = self
   105                         .rules
   128                         .rules