rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
author unC0Rr
Sun, 12 Feb 2023 14:19:02 +0100
branchtransitional_engine
changeset 15918 9bd828451d77
parent 15917 60b5639cc3a5
child 15919 659c92124c26
permissions -rw-r--r--
Fix several issues with transformations, more work on getting generated image
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;
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     2
use std::collections::HashSet;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
     3
use vec2d::Vec2D;
15912
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)]
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
     6
pub enum Tile {
15912
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,
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     9
    Numbered(usize),
15912
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 Tile {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    13
    fn is(&self, i: usize) -> bool {
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    14
        *self == Tile::Numbered(i)
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
    fn is_empty(&self) -> bool {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    18
        match self {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    19
            Tile::Empty => true,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    20
            Tile::Outside => true,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    21
            _ => false,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    22
        }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    23
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    24
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    25
    fn is_empty_or(&self, i: usize) -> bool {
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    26
        match self {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    27
            Tile::Numbered(n) => *n == i,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    28
            Tile::Empty => true,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    29
            _ => false,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    30
        }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    31
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    32
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    33
    fn is_void_or(&self, i: usize) -> bool {
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    34
        match self {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    35
            Tile::Numbered(n) => *n == i,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    36
            _ => true,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    37
        }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    38
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    39
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    40
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    41
impl Default for Tile {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    42
    fn default() -> Self {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    43
        Tile::Outside
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    44
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    45
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    46
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    47
#[derive(Debug)]
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    48
pub struct CollapseRule {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    49
    pub tile: Tile,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    50
    pub right: HashSet<Tile>,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    51
    pub bottom: HashSet<Tile>,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    52
    pub left: HashSet<Tile>,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    53
    pub top: HashSet<Tile>,
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    54
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    55
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
    56
pub struct WavefrontCollapse {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    57
    rules: Vec<CollapseRule>,
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    58
    grid: Vec2D<Tile>,
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    59
}
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    60
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    61
impl Default for WavefrontCollapse {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    62
    fn default() -> Self {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    63
        Self {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    64
            rules: Vec::new(),
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    65
            grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    66
        }
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    67
    }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    68
}
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    69
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    70
impl WavefrontCollapse {
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
    71
    pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>(
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    72
        &mut self,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    73
        map_size: &Size,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    74
        seed_fn: F,
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    75
        random_numbers: &mut I,
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    76
    ) {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    77
        self.grid = Vec2D::new(&map_size, Tile::Empty);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    78
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    79
        seed_fn(&mut self.grid);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    80
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    81
        while self.collapse_step(random_numbers) {}
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    82
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    83
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    84
    pub fn set_rules(&mut self, rules: Vec<CollapseRule>) {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    85
        self.rules = rules;
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    86
    }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    87
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    88
    pub fn add_rule(&mut self, rule: CollapseRule) {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    89
        self.rules.push(rule);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    90
    }
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
    fn get_tile(&self, y: usize, x: usize) -> Tile {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    93
        self.grid.get(y, x).map(|p| *p).unwrap_or_default()
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    94
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
    95
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    96
    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: 15915
diff changeset
    97
        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
    98
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    99
        // Iterate through the tiles in the land
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   100
        for x in 0..self.grid.width() {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   101
            for y in 0..self.grid.height() {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   102
                let current_tile = self.get_tile(y, x);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   103
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   104
                if let Tile::Empty = current_tile {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   105
                    // calc entropy
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   106
                    let right_tile = self.get_tile(y, x + 1);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   107
                    let bottom_tile = self.get_tile(y + 1, x);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   108
                    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
   109
                    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
   110
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   111
                    let possibilities: Vec<Tile> = self
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   112
                        .rules
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   113
                        .iter()
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   114
                        .filter_map(|rule| {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   115
                            if rule.right.contains(&right_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   116
                                && rule.bottom.contains(&bottom_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   117
                                && rule.left.contains(&left_tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   118
                                && rule.top.contains(&top_tile)
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
                                Some(rule.tile)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   121
                            } else {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   122
                                None
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   123
                            }
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   124
                        })
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   125
                        .collect();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   126
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   127
                    let entropy = possibilities.len();
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   128
                    if entropy > 0 {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   129
                        if entropy <= tiles_to_collapse.0 {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   130
                            let entry = (
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   131
                                y,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   132
                                x,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   133
                                possibilities
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   134
                                    [random_numbers.next().unwrap_or_default() as usize % entropy],
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   135
                            );
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   136
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   137
                            if entropy < tiles_to_collapse.0 {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   138
                                tiles_to_collapse = (entropy, vec![entry])
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   139
                            } else {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   140
                                tiles_to_collapse.1.push(entry)
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   141
                            }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   142
                        }
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   143
                    } else {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   144
                        println!("We're here: {}, {}", x, y);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   145
                        println!(
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   146
                            "Neighbour tiles are: {:?} {:?} {:?} {:?}",
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   147
                            right_tile, bottom_tile, left_tile, top_tile
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   148
                        );
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   149
                        println!("Rules are: {:?}", self.rules);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   150
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   151
                        //todo!("no collapse possible - what to do?")
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   152
                    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   153
                }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   154
            }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   155
        }
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
        let tiles_to_collapse = tiles_to_collapse.1;
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   158
        let possibilities_number = tiles_to_collapse.len();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   159
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   160
        if possibilities_number > 0 {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   161
            let (y, x, tile) = tiles_to_collapse
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   162
                [random_numbers.next().unwrap_or_default() as usize % possibilities_number];
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   163
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   164
            *self
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   165
                .grid
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   166
                .get_mut(y, x)
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   167
                .expect("correct iteration over grid") = tile;
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   168
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   169
            true
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   170
        } else {
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   171
            false
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   172
        }
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   173
    }
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   174
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   175
    pub fn grid(&self) -> &Vec2D<Tile> {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   176
        &self.grid
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   177
    }
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
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   180
#[cfg(test)]
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   181
mod tests {
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
   182
    use super::{Tile, WavefrontCollapse};
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   183
    use integral_geometry::Size;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
   184
    use vec2d::Vec2D;
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   185
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   186
    #[test]
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   187
    fn test_wavefront_collapse() {
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   188
        let size = Size::new(4, 4);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   189
        let mut rnd = [0u32; 64].into_iter().cycle();
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   190
        let mut wfc = WavefrontCollapse::default();
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   191
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   192
        wfc.generate_map(&size, |_| {}, &mut rnd);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   193
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents: 15912
diff changeset
   194
        let empty_land = Vec2D::new(&size, Tile::Empty);
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   195
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   196
        assert_eq!(empty_land.as_slice(), wfc.grid().as_slice());
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   197
    }
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents:
diff changeset
   198
}