author | unC0Rr |
Fri, 03 Feb 2023 15:59:18 +0100 | |
branch | transitional_engine |
changeset 15917 | 60b5639cc3a5 |
parent 15916 | e82de0410da5 |
child 15918 | 9bd828451d77 |
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}; |
15917 | 2 |
use super::wavefront_collapse::{CollapseRule, Tile, WavefrontCollapse}; |
15913 | 3 |
use crate::{LandGenerationParameters, LandGenerator}; |
15915 | 4 |
use integral_geometry::Size; |
5 |
use png::Decoder; |
|
15917 | 6 |
use std::collections::HashSet; |
15915 | 7 |
use std::fs::File; |
8 |
use std::io::BufReader; |
|
15913 | 9 |
|
15917 | 10 |
pub struct WavefrontCollapseLandGenerator {} |
15913 | 11 |
|
12 |
impl WavefrontCollapseLandGenerator { |
|
13 |
pub fn new() -> Self { |
|
15917 | 14 |
Self {} |
15913 | 15 |
} |
16 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
17 |
pub fn load_template<T: Copy + PartialEq + Default>( |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
18 |
&self, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
19 |
parameters: &LandGenerationParameters<T>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
20 |
) -> Vec<TileImage<T, String>> { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
21 |
let mut result = Vec::new(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
22 |
|
15915 | 23 |
let file = File::open("sample.png").expect("file exists"); |
24 |
let decoder = Decoder::new(BufReader::new(file)); |
|
25 |
let mut reader = decoder.read_info().unwrap(); |
|
26 |
||
27 |
let info = reader.info(); |
|
28 |
let mut tiles_image = vec2d::Vec2D::new( |
|
29 |
&Size::new(info.width as usize, info.height as usize), |
|
30 |
parameters.zero, |
|
31 |
); |
|
32 |
||
33 |
let mut buf = vec![0; reader.output_buffer_size()]; |
|
34 |
let info = reader.next_frame(&mut buf).unwrap(); |
|
35 |
let bytes = &buf[..info.buffer_size()]; |
|
15913 | 36 |
|
15915 | 37 |
let mut tiles_image_pixels = tiles_image.as_mut_slice().into_iter(); |
38 |
||
39 |
for line in bytes.chunks_exact(info.line_size) { |
|
40 |
for value in line.chunks_exact(info.color_type.samples()) { |
|
41 |
*tiles_image_pixels |
|
42 |
.next() |
|
43 |
.expect("vec2d size matching image dimensions") = |
|
44 |
if value.into_iter().all(|p| *p == 0) { |
|
45 |
parameters.zero |
|
46 |
} else { |
|
47 |
parameters.basic |
|
48 |
}; |
|
49 |
} |
|
50 |
} |
|
51 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
52 |
let top_edge = Edge::new("edge".to_owned(), false); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
53 |
let right_edge = Edge::new("edge".to_owned(), false); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
54 |
let bottom_edge = Edge::new("edge".to_owned(), false); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
55 |
let left_edge = Edge::new("edge".to_owned(), false); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
56 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
57 |
let tile = |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
58 |
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
|
59 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
60 |
result.push(tile.clone()); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
61 |
result.push(tile.mirrored()); |
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 |
15913 | 64 |
} |
65 |
} |
|
66 |
||
67 |
impl LandGenerator for WavefrontCollapseLandGenerator { |
|
68 |
fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>( |
|
69 |
&self, |
|
70 |
parameters: &LandGenerationParameters<T>, |
|
71 |
random_numbers: &mut I, |
|
72 |
) -> land2d::Land2D<T> { |
|
15915 | 73 |
let tiles = self.load_template(parameters); |
74 |
||
15917 | 75 |
let mut rules = Vec::<CollapseRule>::new(); |
76 |
||
77 |
let default_connection = HashSet::from_iter(vec![Tile::Outside, Tile::Empty].into_iter()); |
|
78 |
for (i, tile) in tiles.iter().enumerate() { |
|
79 |
let mut right = default_connection.clone(); |
|
80 |
let mut bottom = default_connection.clone(); |
|
81 |
let mut left = default_connection.clone(); |
|
82 |
let mut top = default_connection.clone(); |
|
83 |
||
84 |
for p in 0..i { |
|
85 |
if tiles[p].left_edge() == tile.right_edge() { |
|
86 |
rules[p].left.insert(Tile::Numbered(i)); |
|
87 |
right.insert(Tile::Numbered(p)); |
|
88 |
} |
|
89 |
||
90 |
if tiles[p].right_edge() == tile.left_edge() { |
|
91 |
rules[p].right.insert(Tile::Numbered(i)); |
|
92 |
left.insert(Tile::Numbered(p)); |
|
93 |
} |
|
94 |
||
95 |
if tiles[p].top_edge() == tile.bottom_edge() { |
|
96 |
rules[p].top.insert(Tile::Numbered(i)); |
|
97 |
bottom.insert(Tile::Numbered(p)); |
|
98 |
} |
|
99 |
||
100 |
if tiles[p].bottom_edge() == tile.top_edge() { |
|
101 |
rules[p].bottom.insert(Tile::Numbered(i)); |
|
102 |
top.insert(Tile::Numbered(p)); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
rules.push(CollapseRule { |
|
107 |
tile: Tile::Numbered(i), |
|
108 |
top, |
|
109 |
right, |
|
110 |
bottom, |
|
111 |
left, |
|
112 |
}); |
|
113 |
} |
|
114 |
||
115 |
let mut wfc = WavefrontCollapse::default(); |
|
116 |
wfc.set_rules(rules); |
|
117 |
||
118 |
wfc.generate_map(&Size::new(40, 20), |_| {}, random_numbers); |
|
119 |
||
120 |
let grid = wfc.grid(); |
|
121 |
||
122 |
for r in 0..grid.height() { |
|
123 |
for c in 0..grid.width() { |
|
124 |
print!("{:?}", grid.get(r, c)); |
|
125 |
} |
|
126 |
||
127 |
println!(); |
|
128 |
} |
|
129 |
||
130 |
todo!("build result") |
|
15913 | 131 |
} |
132 |
} |
|
15915 | 133 |
|
134 |
#[cfg(test)] |
|
135 |
mod tests { |
|
136 |
use super::WavefrontCollapseLandGenerator; |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
137 |
use crate::{LandGenerationParameters, LandGenerator}; |
15915 | 138 |
use integral_geometry::Size; |
139 |
use vec2d::Vec2D; |
|
140 |
||
141 |
#[test] |
|
142 |
fn test_generation() { |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
143 |
let wfc_gen = WavefrontCollapseLandGenerator::new(); |
15915 | 144 |
let landgen_params = LandGenerationParameters::new(0u8, 255u8, 0, true, true); |
145 |
wfc_gen.generate_land(&landgen_params, &mut std::iter::repeat(1u32)); |
|
146 |
} |
|
147 |
} |