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