rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
author unC0Rr
Sat, 18 Jan 2025 16:57:26 +0100
changeset 16058 de01be16df95
parent 15925 b0e8cc72bfef
child 16059 2acea266d297
permissions -rw-r--r--
Make slider below preview affect WFC generator by skewing tile probabilities
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
     1
use integral_geometry::Size;
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
     2
use rand::distributions::Distribution;
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
     3
use rand::distributions::WeightedIndex;
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
     4
use rand::Rng;
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     5
use std::collections::HashSet;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
     6
use vec2d::Vec2D;
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
     7
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
     8
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
     9
pub enum Tile {
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    10
    Empty,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    11
    Outside,
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    12
    Numbered(usize),
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    13
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    14
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    15
impl Default for Tile {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    16
    fn default() -> Self {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    17
        Tile::Outside
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    18
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    19
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    20
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    21
#[derive(Debug)]
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    22
pub struct CollapseRule {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
    23
    pub weight: u32,
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    24
    pub tile: Tile,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    25
    pub right: HashSet<Tile>,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    26
    pub bottom: HashSet<Tile>,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    27
    pub left: HashSet<Tile>,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    28
    pub top: HashSet<Tile>,
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    29
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    30
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
    31
pub struct WavefrontCollapse {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    32
    rules: Vec<CollapseRule>,
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    33
    grid: Vec2D<Tile>,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    34
    wrap: bool,
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    35
}
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    36
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    37
impl Default for WavefrontCollapse {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    38
    fn default() -> Self {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    39
        Self {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    40
            rules: Vec::new(),
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    41
            grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    42
            wrap: false,
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    43
        }
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    44
    }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    45
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    46
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    47
impl WavefrontCollapse {
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    48
    pub fn new(wrap: bool) -> Self {
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    49
        Self {
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    50
            rules: Vec::new(),
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    51
            grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    52
            wrap,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    53
        }
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    54
    }
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    55
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
    56
    pub fn generate_map<F: FnOnce(&mut Vec2D<Tile>)>(
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    57
        &mut self,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    58
        map_size: &Size,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    59
        seed_fn: F,
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
    60
        random_numbers: &mut impl Rng,
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    61
    ) {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    62
        self.grid = Vec2D::new(map_size, Tile::Empty);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    63
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    64
        seed_fn(&mut self.grid);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    65
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    66
        while self.collapse_step(random_numbers) {}
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    67
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    68
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    69
    pub fn set_rules(&mut self, rules: Vec<CollapseRule>) {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    70
        self.rules = rules;
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    71
    }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    72
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    73
    fn get_tile(&self, y: usize, x: usize) -> Tile {
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    74
        let x = if self.wrap {
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    75
            if x == usize::MAX {
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    76
                self.grid.width() - 1
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    77
            } else if x == self.grid.width() {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    78
                0
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    79
            } else {
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    80
                x
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    81
            }
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    82
        } else {
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    83
            x
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    84
        };
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    85
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    86
        self.grid.get(y, x).copied().unwrap_or_default()
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    87
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    88
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
    89
    fn collapse_step(&mut self, random_numbers: &mut impl Rng) -> bool {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    90
        let mut tiles_to_collapse = (usize::max_value(), Vec::new());
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    91
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    92
        // Iterate through the tiles in the land
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    93
        for x in 0..self.grid.width() {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    94
            for y in 0..self.grid.height() {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    95
                let current_tile = self.get_tile(y, x);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    96
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    97
                if let Tile::Empty = current_tile {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    98
                    // calc entropy
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    99
                    let right_tile = self.get_tile(y, x + 1);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   100
                    let bottom_tile = self.get_tile(y + 1, x);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   101
                    let left_tile = self.get_tile(y, x.wrapping_sub(1));
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   102
                    let top_tile = self.get_tile(y.wrapping_sub(1), x);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   103
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   104
                    let possibilities: Vec<(u32, Tile)> = self
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   105
                        .rules
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   106
                        .iter()
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   107
                        .filter_map(|rule| {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   108
                            if rule.right.contains(&right_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   109
                                && rule.bottom.contains(&bottom_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   110
                                && rule.left.contains(&left_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   111
                                && rule.top.contains(&top_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   112
                            {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   113
                                Some((rule.weight, rule.tile))
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   114
                            } else {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   115
                                None
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   116
                            }
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   117
                        })
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   118
                        .collect();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   119
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   120
                    let entropy = possibilities.len();
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   121
                    if entropy > 0 {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   122
                        if entropy <= tiles_to_collapse.0 {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   123
                            let weights = possibilities.iter().map(|(weight, _)| *weight);
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   124
                            let distribution = WeightedIndex::new(weights).unwrap();
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   125
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   126
                            let entry = (y, x, possibilities[distribution.sample(random_numbers)]);
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   127
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   128
                            if entropy < tiles_to_collapse.0 {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   129
                                tiles_to_collapse = (entropy, vec![entry])
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   130
                            } else {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   131
                                tiles_to_collapse.1.push(entry)
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   132
                            }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   133
                        }
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   134
                    } else {
15919
659c92124c26 Fix some bugs
unC0Rr
parents: 15918
diff changeset
   135
                        /*println!("We're here: {}, {}", x, y);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   136
                        println!(
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   137
                            "Neighbour tiles are: {:?} {:?} {:?} {:?}",
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   138
                            right_tile, bottom_tile, left_tile, top_tile
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   139
                        );
15919
659c92124c26 Fix some bugs
unC0Rr
parents: 15918
diff changeset
   140
                        println!("Rules are: {:?}", self.rules);*/
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   141
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   142
                        //todo!("no collapse possible - what to do?")
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   143
                    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   144
                }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   145
            }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   146
        }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   147
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   148
        let tiles_to_collapse = tiles_to_collapse.1;
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   149
        let possibilities_number = tiles_to_collapse.len();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   150
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   151
        if possibilities_number > 0 {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   152
            let weights = tiles_to_collapse.iter().map(|(_, _, (weight, _))| *weight);
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   153
            let distribution = WeightedIndex::new(weights).unwrap();
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   154
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 15925
diff changeset
   155
            let (y, x, (_, tile)) = tiles_to_collapse[distribution.sample(random_numbers)];
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   156
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   157
            *self
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   158
                .grid
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   159
                .get_mut(y, x)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   160
                .expect("correct iteration over grid") = tile;
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   161
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   162
            true
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   163
        } else {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   164
            false
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   165
        }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   166
    }
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   167
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   168
    pub fn grid(&self) -> &Vec2D<Tile> {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   169
        &self.grid
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   170
    }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   171
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   172
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   173
#[cfg(test)]
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   174
mod tests {
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
   175
    use super::{Tile, WavefrontCollapse};
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   176
    use integral_geometry::Size;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
   177
    use vec2d::Vec2D;
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   178
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   179
    #[test]
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   180
    fn test_wavefront_collapse() {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   181
        let size = Size::new(4, 4);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   182
        let mut rnd = [0u32; 64].into_iter().cycle();
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   183
        let mut wfc = WavefrontCollapse::default();
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   184
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   185
        wfc.generate_map(&size, |_| {}, &mut rnd);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   186
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
   187
        let empty_land = Vec2D::new(&size, Tile::Empty);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   188
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   189
        assert_eq!(empty_land.as_slice(), wfc.grid().as_slice());
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   190
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   191
}