rust/landgen/src/wavefront_collapse/generator.rs
author unC0Rr
Fri, 03 Feb 2023 14:44:33 +0100
branchtransitional_engine
changeset 15916 e82de0410da5
parent 15915 8f093b1b18bc
child 15917 60b5639cc3a5
permissions -rw-r--r--
Rework how rules are defined, add transformations for tiles
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};
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     2
use super::wavefront_collapse::WavefrontCollapse;
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;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     6
use std::fs::File;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     7
use std::io::BufReader;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     8
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     9
pub struct WavefrontCollapseLandGenerator {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    10
    wfc: WavefrontCollapse,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    11
}
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    12
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    13
impl WavefrontCollapseLandGenerator {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    14
    pub fn new() -> Self {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    15
        Self {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    16
            wfc: WavefrontCollapse::default(),
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    17
        }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    18
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    19
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    20
    pub fn load_template<T: Copy + PartialEq + Default>(
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    21
        &self,
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    22
        parameters: &LandGenerationParameters<T>,
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    23
    ) -> Vec<TileImage<T, String>> {
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
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    26
        let file = File::open("sample.png").expect("file exists");
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
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    55
        let top_edge = Edge::new("edge".to_owned(), false);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    56
        let right_edge = Edge::new("edge".to_owned(), false);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    57
        let bottom_edge = Edge::new("edge".to_owned(), false);
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    58
        let left_edge = Edge::new("edge".to_owned(), false);
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());
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    64
        result.push(tile.mirrored());
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    65
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    66
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    67
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    68
}
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    69
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    70
impl LandGenerator for WavefrontCollapseLandGenerator {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    71
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    72
        &self,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    73
        parameters: &LandGenerationParameters<T>,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    74
        random_numbers: &mut I,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    75
    ) -> land2d::Land2D<T> {
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    76
        let tiles = self.load_template(parameters);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    77
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    78
        todo!()
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    79
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    80
}
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    81
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    82
#[cfg(test)]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    83
mod tests {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    84
    use super::WavefrontCollapseLandGenerator;
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    85
    use crate::{LandGenerationParameters, LandGenerator};
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    86
    use integral_geometry::Size;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    87
    use vec2d::Vec2D;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    88
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    89
    #[test]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    90
    fn test_generation() {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    91
        let wfc_gen = WavefrontCollapseLandGenerator::new();
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    92
        let landgen_params = LandGenerationParameters::new(0u8, 255u8, 0, true, true);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    93
        wfc_gen.generate_land(&landgen_params, &mut std::iter::repeat(1u32));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    94
    }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    95
}