author | unC0Rr |
Fri, 03 Feb 2023 14:44:33 +0100 | |
branch | transitional_engine |
changeset 15916 | e82de0410da5 |
parent 15915 | 8f093b1b18bc |
child 15917 | 60b5639cc3a5 |
permissions | -rw-r--r-- |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
1 |
use super::tile_image::{Edge, TileImage}; |
15913 | 2 |
use super::wavefront_collapse::WavefrontCollapse; |
3 |
use crate::{LandGenerationParameters, LandGenerator}; |
|
15915 | 4 |
use integral_geometry::Size; |
5 |
use png::Decoder; |
|
6 |
use std::fs::File; |
|
7 |
use std::io::BufReader; |
|
15913 | 8 |
|
9 |
pub struct WavefrontCollapseLandGenerator { |
|
10 |
wfc: WavefrontCollapse, |
|
11 |
} |
|
12 |
||
13 |
impl WavefrontCollapseLandGenerator { |
|
14 |
pub fn new() -> Self { |
|
15 |
Self { |
|
16 |
wfc: WavefrontCollapse::default(), |
|
17 |
} |
|
18 |
} |
|
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 | 26 |
let file = File::open("sample.png").expect("file exists"); |
27 |
let decoder = Decoder::new(BufReader::new(file)); |
|
28 |
let mut reader = decoder.read_info().unwrap(); |
|
29 |
||
30 |
let info = reader.info(); |
|
31 |
let mut tiles_image = vec2d::Vec2D::new( |
|
32 |
&Size::new(info.width as usize, info.height as usize), |
|
33 |
parameters.zero, |
|
34 |
); |
|
35 |
||
36 |
let mut buf = vec![0; reader.output_buffer_size()]; |
|
37 |
let info = reader.next_frame(&mut buf).unwrap(); |
|
38 |
let bytes = &buf[..info.buffer_size()]; |
|
15913 | 39 |
|
15915 | 40 |
let mut tiles_image_pixels = tiles_image.as_mut_slice().into_iter(); |
41 |
||
42 |
for line in bytes.chunks_exact(info.line_size) { |
|
43 |
for value in line.chunks_exact(info.color_type.samples()) { |
|
44 |
*tiles_image_pixels |
|
45 |
.next() |
|
46 |
.expect("vec2d size matching image dimensions") = |
|
47 |
if value.into_iter().all(|p| *p == 0) { |
|
48 |
parameters.zero |
|
49 |
} else { |
|
50 |
parameters.basic |
|
51 |
}; |
|
52 |
} |
|
53 |
} |
|
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 | 67 |
} |
68 |
} |
|
69 |
||
70 |
impl LandGenerator for WavefrontCollapseLandGenerator { |
|
71 |
fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>( |
|
72 |
&self, |
|
73 |
parameters: &LandGenerationParameters<T>, |
|
74 |
random_numbers: &mut I, |
|
75 |
) -> land2d::Land2D<T> { |
|
15915 | 76 |
let tiles = self.load_template(parameters); |
77 |
||
15913 | 78 |
todo!() |
79 |
} |
|
80 |
} |
|
15915 | 81 |
|
82 |
#[cfg(test)] |
|
83 |
mod tests { |
|
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 | 86 |
use integral_geometry::Size; |
87 |
use vec2d::Vec2D; |
|
88 |
||
89 |
#[test] |
|
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 | 92 |
let landgen_params = LandGenerationParameters::new(0u8, 255u8, 0, true, true); |
93 |
wfc_gen.generate_land(&landgen_params, &mut std::iter::repeat(1u32)); |
|
94 |
} |
|
95 |
} |