rust/landgen/src/wavefront_collapse/generator.rs
author unC0Rr
Mon, 13 Feb 2023 10:39:37 +0100
branchtransitional_engine
changeset 15920 168f44ef9b67
parent 15919 659c92124c26
child 15922 da6b67f13c12
permissions -rw-r--r--
Extract tile loading into separate method
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
     1
use super::tile_image::{Edge, TileImage};
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     2
use super::wavefront_collapse::{CollapseRule, Tile, WavefrontCollapse};
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     3
use crate::{LandGenerationParameters, LandGenerator};
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     4
use integral_geometry::Size;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     5
use png::Decoder;
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     6
use std::collections::HashSet;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     7
use std::fs::File;
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
     8
use std::io::{BufReader, Result};
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
     9
use std::path::Path;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    10
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    11
pub struct WavefrontCollapseLandGenerator {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    12
    pub size: Size,
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    13
}
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    14
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    15
impl WavefrontCollapseLandGenerator {
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    16
    pub fn new(size: &Size) -> Self {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    17
        Self { size: *size }
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    18
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    19
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    20
    fn load_image_tiles<T: Copy + PartialEq + Default>(
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    21
        parameters: &LandGenerationParameters<T>,
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    22
        path: &Path,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    23
    ) -> Result<Vec<TileImage<T, String>>> {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    24
        let mut result = Vec::new();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    25
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    26
        let file = File::open(path)?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    27
        let decoder = Decoder::new(BufReader::new(file));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    28
        let mut reader = decoder.read_info().unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    29
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    30
        let info = reader.info();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    31
        let mut tiles_image = vec2d::Vec2D::new(
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    32
            &Size::new(info.width as usize, info.height as usize),
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    33
            parameters.zero,
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    34
        );
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    35
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    36
        let mut buf = vec![0; reader.output_buffer_size()];
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    37
        let info = reader.next_frame(&mut buf).unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    38
        let bytes = &buf[..info.buffer_size()];
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    39
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    40
        let mut tiles_image_pixels = tiles_image.as_mut_slice().into_iter();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    41
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    42
        for line in bytes.chunks_exact(info.line_size) {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    43
            for value in line.chunks_exact(info.color_type.samples()) {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    44
                *tiles_image_pixels
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    45
                    .next()
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    46
                    .expect("vec2d size matching image dimensions") =
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    47
                    if value.into_iter().all(|p| *p == 0) {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    48
                        parameters.zero
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    49
                    } else {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    50
                        parameters.basic
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    51
                    };
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    52
            }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    53
        }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    54
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    55
        let top_edge = Edge::new("ef".to_owned(), false);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    56
        let right_edge = top_edge.reversed();
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    57
        let bottom_edge = Edge::new("ee".to_owned(), true);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    58
        let left_edge = bottom_edge.clone();
15916
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
        let tile =
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    61
            TileImage::<T, String>::new(tiles_image, top_edge, right_edge, bottom_edge, left_edge);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    62
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    63
        result.push(tile.clone());
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    64
        result.push(tile.flipped());
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    65
        result.push(tile.mirrored());
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    66
        result.push(tile.mirrored().flipped());
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    67
        result.push(tile.rotated90());
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    68
        result.push(tile.rotated180());
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    69
        result.push(tile.rotated270());
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    70
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    71
        Ok(result)
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    72
    }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    73
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    74
    pub fn load_template<T: Copy + PartialEq + Default>(
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    75
        &self,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    76
        parameters: &LandGenerationParameters<T>,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    77
    ) -> Vec<TileImage<T, String>> {
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    78
        let mut result = Vec::new();
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    79
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    80
        if let Ok(mut tiles) = Self::load_image_tiles(parameters, Path::new("sample.png")) {
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    81
            result.append(&mut tiles);
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    82
        }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    83
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    84
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    85
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    86
}
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    87
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    88
impl LandGenerator for WavefrontCollapseLandGenerator {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    89
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    90
        &self,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    91
        parameters: &LandGenerationParameters<T>,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    92
        random_numbers: &mut I,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    93
    ) -> land2d::Land2D<T> {
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    94
        let tiles = self.load_template(parameters);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    95
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    96
        let mut rules = Vec::<CollapseRule>::new();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    97
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    98
        let default_connection = HashSet::from_iter(vec![Tile::Outside, Tile::Empty].into_iter());
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
    99
        for (i, tile) in tiles.iter().enumerate() {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   100
            let mut right = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   101
            let mut bottom = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   102
            let mut left = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   103
            let mut top = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   104
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   105
            for p in 0..i {
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   106
                if tiles[p].left_edge().is_compatible(tile.right_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   107
                    rules[p].left.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   108
                    right.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   109
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   110
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   111
                if tiles[p].right_edge().is_compatible(tile.left_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   112
                    rules[p].right.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   113
                    left.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   114
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   115
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   116
                if tiles[p].top_edge().is_compatible(tile.bottom_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   117
                    rules[p].top.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   118
                    bottom.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   119
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   120
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   121
                if tiles[p].bottom_edge().is_compatible(tile.top_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   122
                    rules[p].bottom.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   123
                    top.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   124
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   125
            }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   126
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   127
            rules.push(CollapseRule {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   128
                tile: Tile::Numbered(i),
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   129
                top,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   130
                right,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   131
                bottom,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   132
                left,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   133
            });
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   134
        }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   135
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   136
        let mut wfc = WavefrontCollapse::default();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   137
        wfc.set_rules(rules);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   138
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   139
        let wfc_size = if let Some(first_tile) = tiles.first() {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   140
            let tile_size = first_tile.size();
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   141
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   142
            Size::new(
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   143
                self.size.width / tile_size.width,
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   144
                self.size.height / tile_size.height,
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   145
            )
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   146
        } else {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   147
            Size::new(1, 1)
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   148
        };
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   149
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   150
        wfc.generate_map(&wfc_size, |_| {}, random_numbers);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   151
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   152
        let grid = wfc.grid();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   153
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   154
        for r in 0..grid.height() {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   155
            for c in 0..grid.width() {
15919
659c92124c26 Fix some bugs
unC0Rr
parents: 15918
diff changeset
   156
                print!("{:?} ", grid.get(r, c));
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   157
            }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   158
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   159
            println!();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   160
        }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   161
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   162
        let mut result = land2d::Land2D::new(&self.size, parameters.zero);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   163
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   164
        for row in 0..wfc_size.height {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   165
            for column in 0..wfc_size.width {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   166
                if let Some(Tile::Numbered(tile_index)) = wfc.grid().get(row, column) {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   167
                    let tile = &tiles[*tile_index];
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   168
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   169
                    for tile_row in 0..tile.size().height {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   170
                        for tile_column in 0..tile.size().width {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   171
                            result.map(
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   172
                                (row * tile.size().height + tile_row) as i32,
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   173
                                (column * tile.size().width + tile_column) as i32,
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   174
                                |p| {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   175
                                    *p =
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   176
                                        *tile.get(tile_row, tile_column).unwrap_or(&parameters.zero)
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   177
                                },
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   178
                            );
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   179
                        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   180
                    }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   181
                }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   182
            }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   183
        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   184
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   185
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   186
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   187
}
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   188
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   189
#[cfg(test)]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   190
mod tests {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   191
    use super::WavefrontCollapseLandGenerator;
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   192
    use crate::{LandGenerationParameters, LandGenerator};
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   193
    use integral_geometry::Size;
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   194
    use std::fs::File;
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   195
    use std::io::BufWriter;
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   196
    use std::path::Path;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   197
    use vec2d::Vec2D;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   198
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   199
    #[test]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   200
    fn test_generation() {
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   201
        let wfc_gen = WavefrontCollapseLandGenerator::new(&Size::new(2048, 1024));
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   202
        let landgen_params = LandGenerationParameters::new(0u32, 0xff000000u32, 0, true, true);
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   203
        let land = wfc_gen.generate_land(
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   204
            &landgen_params,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   205
            &mut [0u32, 1u32, 3u32, 5u32, 7u32, 11u32].into_iter().cycle(),
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   206
        );
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   207
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   208
        let path = Path::new(r"output.png");
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   209
        let file = File::create(path).unwrap();
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   210
        let ref mut w = BufWriter::new(file);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   211
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   212
        let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   213
        encoder.set_color(png::ColorType::Rgba);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   214
        encoder.set_depth(png::BitDepth::Eight);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   215
        encoder.set_source_gamma(png::ScaledFloat::from_scaled(45455)); // 1.0 / 2.2, scaled by 100000
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   216
        encoder.set_source_gamma(png::ScaledFloat::new(1.0 / 2.2)); // 1.0 / 2.2, unscaled, but rounded
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   217
        let source_chromaticities = png::SourceChromaticities::new(
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   218
            // Using unscaled instantiation here
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   219
            (0.31270, 0.32900),
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   220
            (0.64000, 0.33000),
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   221
            (0.30000, 0.60000),
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   222
            (0.15000, 0.06000),
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   223
        );
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   224
        encoder.set_source_chromaticities(source_chromaticities);
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   225
        let mut writer = encoder.write_header().unwrap();
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   226
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   227
        writer.write_image_data(land.raw_pixel_bytes()).unwrap(); // Save
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   228
    }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   229
}