rust/landgen/src/wavefront_collapse/generator.rs
author unC0Rr
Thu, 02 Feb 2023 08:41:31 +0100
branchtransitional_engine
changeset 15915 8f093b1b18bc
parent 15913 c5684cc62de8
child 15916 e82de0410da5
permissions -rw-r--r--
Add loading of tiles from png
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     1
use super::tile_image::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
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    20
    pub fn load_template<T: Copy + PartialEq + Default>(&self, parameters: &LandGenerationParameters<T>) -> Vec<TileImage<T>> {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    21
        let file = File::open("sample.png").expect("file exists");
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    22
        let decoder = Decoder::new(BufReader::new(file));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    23
        let mut reader = decoder.read_info().unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    24
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    25
        let info = reader.info();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    26
        let mut tiles_image = vec2d::Vec2D::new(
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    27
            &Size::new(info.width as usize, info.height as usize),
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    28
            parameters.zero,
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
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    31
        let mut buf = vec![0; reader.output_buffer_size()];
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    32
        let info = reader.next_frame(&mut buf).unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    33
        let bytes = &buf[..info.buffer_size()];
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    34
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    35
        let mut tiles_image_pixels = tiles_image.as_mut_slice().into_iter();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    36
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    37
        for line in bytes.chunks_exact(info.line_size) {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    38
            for value in line.chunks_exact(info.color_type.samples()) {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    39
                *tiles_image_pixels
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    40
                    .next()
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    41
                    .expect("vec2d size matching image dimensions") =
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    42
                    if value.into_iter().all(|p| *p == 0) {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    43
                        parameters.zero
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    44
                    } else {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    45
                        parameters.basic
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    46
                    };
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    47
            }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    48
        }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    49
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    50
        TileImage::<T>::new(tiles_image).split(3, 3)
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    51
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    52
}
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    53
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    54
impl LandGenerator for WavefrontCollapseLandGenerator {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    55
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    56
        &self,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    57
        parameters: &LandGenerationParameters<T>,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    58
        random_numbers: &mut I,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    59
    ) -> land2d::Land2D<T> {
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    60
        let tiles = self.load_template(parameters);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    61
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    62
        todo!()
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    63
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    64
}
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    65
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    66
#[cfg(test)]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    67
mod tests {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    68
    use super::WavefrontCollapseLandGenerator;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    69
    use crate::{LandGenerator, LandGenerationParameters};
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    70
    use integral_geometry::Size;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    71
    use vec2d::Vec2D;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    72
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    73
    #[test]
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    74
    fn test_generation() {
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    75
        let wfc_gen =WavefrontCollapseLandGenerator::new();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    76
        let landgen_params = LandGenerationParameters::new(0u8, 255u8, 0, true, true);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    77
        wfc_gen.generate_land(&landgen_params, &mut std::iter::repeat(1u32));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    78
    }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    79
}