author | unC0Rr |
Sun, 26 Jan 2025 21:29:54 +0100 | |
changeset 16062 | 288df7b85efc |
parent 16059 | 2acea266d297 |
child 16064 | 07cb6dbc8444 |
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; |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
6 |
use rand::Rng; |
15917 | 7 |
use std::collections::HashSet; |
15915 | 8 |
use std::fs::File; |
15920 | 9 |
use std::io::{BufReader, Result}; |
16024 | 10 |
use std::path::{Path, PathBuf}; |
15913 | 11 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
12 |
#[derive(Debug, Clone)] |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
13 |
pub struct EdgeDescription { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
14 |
pub name: String, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
15 |
pub reversed: Option<bool>, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
16 |
pub symmetrical: Option<bool>, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
17 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
18 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
19 |
#[derive(Debug, Clone)] |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
20 |
pub struct EdgesDescription { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
21 |
pub top: EdgeDescription, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
22 |
pub right: EdgeDescription, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
23 |
pub bottom: EdgeDescription, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
24 |
pub left: EdgeDescription, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
25 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
26 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
27 |
#[derive(Debug, Clone)] |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
28 |
pub struct TileDescription { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
29 |
pub name: String, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
30 |
pub edges: EdgesDescription, |
15924 | 31 |
pub is_negative: Option<bool>, |
32 |
pub can_flip: Option<bool>, |
|
33 |
pub can_mirror: Option<bool>, |
|
34 |
pub can_rotate90: Option<bool>, |
|
35 |
pub can_rotate180: Option<bool>, |
|
36 |
pub can_rotate270: Option<bool>, |
|
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
37 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
38 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
39 |
#[derive(Debug, Clone)] |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
40 |
pub struct ComplexEdgeDescription { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
41 |
pub begin: Option<EdgeDescription>, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
42 |
pub fill: Option<EdgeDescription>, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
43 |
pub end: Option<EdgeDescription>, |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
44 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
45 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
46 |
#[derive(Debug, Clone)] |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
47 |
pub struct NonStrictComplexEdgesDescription { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
48 |
pub top: Option<ComplexEdgeDescription>, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
49 |
pub right: Option<ComplexEdgeDescription>, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
50 |
pub bottom: Option<ComplexEdgeDescription>, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
51 |
pub left: Option<ComplexEdgeDescription>, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
52 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
53 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
54 |
#[derive(Debug, Clone)] |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
55 |
pub struct TemplateDescription { |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
56 |
pub size: Size, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
57 |
pub tiles: Vec<TileDescription>, |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
58 |
pub edges: NonStrictComplexEdgesDescription, |
16062 | 59 |
pub can_invert: bool, |
60 |
pub is_negative: bool, |
|
15924 | 61 |
pub wrap: bool, |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
62 |
} |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
63 |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
64 |
pub struct WavefrontCollapseLandGenerator { |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
65 |
pub template: TemplateDescription, |
16024 | 66 |
data_path: PathBuf, |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
67 |
} |
15913 | 68 |
|
69 |
impl WavefrontCollapseLandGenerator { |
|
16024 | 70 |
pub fn new(template: TemplateDescription, data_path: &Path) -> Self { |
16029 | 71 |
Self { |
72 |
template, |
|
73 |
data_path: data_path.to_owned(), |
|
74 |
} |
|
15913 | 75 |
} |
76 |
||
15920 | 77 |
fn load_image_tiles<T: Copy + PartialEq + Default>( |
16024 | 78 |
&self, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
79 |
parameters: &LandGenerationParameters<T>, |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
80 |
tile_description: &TileDescription, |
15920 | 81 |
) -> Result<Vec<TileImage<T, String>>> { |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
82 |
let mut result = Vec::new(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
83 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
84 |
let file = File::open( |
16029 | 85 |
self.data_path |
86 |
.join("Tiles") |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
87 |
.join(&tile_description.name) |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
88 |
.as_path(), |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
89 |
)?; |
15915 | 90 |
let decoder = Decoder::new(BufReader::new(file)); |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
91 |
let mut reader = decoder.read_info()?; |
15915 | 92 |
|
93 |
let info = reader.info(); |
|
94 |
let mut tiles_image = vec2d::Vec2D::new( |
|
95 |
&Size::new(info.width as usize, info.height as usize), |
|
96 |
parameters.zero, |
|
97 |
); |
|
98 |
||
99 |
let mut buf = vec![0; reader.output_buffer_size()]; |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
100 |
let info = reader.next_frame(&mut buf)?; |
15915 | 101 |
let bytes = &buf[..info.buffer_size()]; |
15913 | 102 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
103 |
let mut tiles_image_pixels = tiles_image.as_mut_slice().iter_mut(); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
104 |
|
15924 | 105 |
let (zero, basic) = if tile_description.is_negative.unwrap_or_default() { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
106 |
(parameters.basic(), parameters.zero()) |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
107 |
} else { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
108 |
(parameters.zero(), parameters.basic()) |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
109 |
}; |
15915 | 110 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
111 |
match info.color_type.samples() { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
112 |
1 => { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
113 |
for line in bytes.chunks_exact(info.line_size) { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
114 |
for value in line.iter() { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
115 |
*tiles_image_pixels |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
116 |
.next() |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
117 |
.expect("vec2d size matching image dimensions") = |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
118 |
if *value == 0 { zero } else { basic }; |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
119 |
} |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
120 |
} |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
121 |
} |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
122 |
a => { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
123 |
for line in bytes.chunks_exact(info.line_size) { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
124 |
for value in line.chunks_exact(a) { |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
125 |
print!("{:?},", value); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
126 |
*tiles_image_pixels |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
127 |
.next() |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
128 |
.expect("vec2d size matching image dimensions") = |
15924 | 129 |
if value[0] == 0u8 { zero } else { basic }; |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
130 |
} |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
131 |
} |
15915 | 132 |
} |
133 |
} |
|
134 |
||
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
135 |
let [top_edge, right_edge, bottom_edge, left_edge]: [Edge<String>; 4] = [ |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
136 |
(&tile_description.edges.top).into(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
137 |
(&tile_description.edges.right).into(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
138 |
(&tile_description.edges.bottom).into(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
139 |
(&tile_description.edges.left).into(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
140 |
]; |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
141 |
|
15926 | 142 |
let tile = |
143 |
TileImage::<T, String>::new(tiles_image, top_edge, right_edge, bottom_edge, left_edge); |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
144 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
145 |
result.push(tile.clone()); |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
146 |
|
15924 | 147 |
if tile_description.can_flip.unwrap_or_default() { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
148 |
result.push(tile.flipped()); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
149 |
} |
15924 | 150 |
if tile_description.can_mirror.unwrap_or_default() { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
151 |
result.push(tile.mirrored()); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
152 |
} |
15924 | 153 |
if tile_description.can_flip.unwrap_or_default() |
154 |
&& tile_description.can_mirror.unwrap_or_default() |
|
155 |
{ |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
156 |
result.push(tile.mirrored().flipped()); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
157 |
} |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
158 |
|
15924 | 159 |
if tile_description.can_rotate90.unwrap_or_default() { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
160 |
result.push(tile.rotated90()); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
161 |
} |
15924 | 162 |
if tile_description.can_rotate180.unwrap_or_default() { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
163 |
result.push(tile.rotated180()); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
164 |
} |
15924 | 165 |
if tile_description.can_rotate270.unwrap_or_default() { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
166 |
result.push(tile.rotated270()); |
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
167 |
} |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
168 |
|
15920 | 169 |
Ok(result) |
170 |
} |
|
171 |
||
172 |
pub fn load_template<T: Copy + PartialEq + Default>( |
|
173 |
&self, |
|
174 |
parameters: &LandGenerationParameters<T>, |
|
175 |
) -> Vec<TileImage<T, String>> { |
|
176 |
let mut result = Vec::new(); |
|
177 |
||
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
178 |
for tile_description in self.template.tiles.iter() { |
16024 | 179 |
if let Ok(mut tiles) = self.load_image_tiles(parameters, tile_description) { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
180 |
result.append(&mut tiles); |
16023
0fd23fc57947
Make pascal engine link to hwengine-future and use WFC generator
unC0Rr
parents:
15926
diff
changeset
|
181 |
} else { |
0fd23fc57947
Make pascal engine link to hwengine-future and use WFC generator
unC0Rr
parents:
15926
diff
changeset
|
182 |
eprintln!("Failed to load a tile!"); |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
183 |
} |
15920 | 184 |
} |
185 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
186 |
result |
15913 | 187 |
} |
188 |
||
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
189 |
pub fn build_rules<T: Copy + PartialEq + Default>( |
15913 | 190 |
&self, |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
191 |
tiles: &[TileImage<T, String>], |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
192 |
probability_distribution_factor: i32, |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
193 |
) -> Vec<CollapseRule> { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
194 |
let [grid_top_edge, grid_right_edge, grid_bottom_edge, grid_left_edge]: [Option< |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
195 |
[Option<Edge<String>>; 3], |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
196 |
>; 4] = [ |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
197 |
self.template.edges.top.as_ref(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
198 |
self.template.edges.right.as_ref(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
199 |
self.template.edges.bottom.as_ref(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
200 |
self.template.edges.left.as_ref(), |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
201 |
] |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
202 |
.map(|opt| opt.map(|d| [&d.begin, &d.fill, &d.end].map(|e| e.as_ref().map(Into::into)))); |
15915 | 203 |
|
15917 | 204 |
let mut rules = Vec::<CollapseRule>::new(); |
205 |
||
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
206 |
let default_connection = HashSet::from_iter(vec![Tile::Empty].into_iter()); |
15917 | 207 |
for (i, tile) in tiles.iter().enumerate() { |
208 |
let mut right = default_connection.clone(); |
|
209 |
let mut bottom = default_connection.clone(); |
|
210 |
let mut left = default_connection.clone(); |
|
211 |
let mut top = default_connection.clone(); |
|
212 |
||
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
213 |
let iteration = [ |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
214 |
(&grid_top_edge, tile.top_edge(), &mut top), |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
215 |
(&grid_right_edge, tile.right_edge(), &mut right), |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
216 |
(&grid_bottom_edge, tile.bottom_edge(), &mut bottom), |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
217 |
(&grid_left_edge, tile.left_edge(), &mut left), |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
218 |
]; |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
219 |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
220 |
// compatibility with grid edges |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
221 |
for (edge, tile_edge, set) in iteration { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
222 |
for (is_compatible, tile) in edge |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
223 |
.as_ref() |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
224 |
.map(|e| { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
225 |
e.clone().map(|ed| { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
226 |
ed.as_ref() |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
227 |
.map(|e| e.is_compatible(tile_edge)) |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
228 |
.unwrap_or(true) |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
229 |
}) |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
230 |
}) |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
231 |
.unwrap_or([true, true, true]) |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
232 |
.into_iter() |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
233 |
.zip([Tile::OutsideBegin, Tile::OutsideFill, Tile::OutsideEnd].into_iter()) |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
234 |
{ |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
235 |
if is_compatible { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
236 |
set.insert(tile); |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
237 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
238 |
} |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
239 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
240 |
|
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
241 |
// compatibility with itself |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
242 |
if tile.left_edge().is_compatible(tile.right_edge()) { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
243 |
left.insert(Tile::Numbered(i)); |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
244 |
right.insert(Tile::Numbered(i)); |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
245 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
246 |
|
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
247 |
if tile.top_edge().is_compatible(tile.bottom_edge()) { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
248 |
top.insert(Tile::Numbered(i)); |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
249 |
bottom.insert(Tile::Numbered(i)); |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
250 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
251 |
|
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
252 |
// compatibility with previously defined tiles |
15917 | 253 |
for p in 0..i { |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
254 |
// Check left edge |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
255 |
if tiles[p].left_edge().is_compatible(tile.right_edge()) { |
15917 | 256 |
rules[p].left.insert(Tile::Numbered(i)); |
257 |
right.insert(Tile::Numbered(p)); |
|
258 |
} |
|
259 |
||
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
260 |
// Check right edge |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
261 |
if tiles[p].right_edge().is_compatible(tile.left_edge()) { |
15917 | 262 |
rules[p].right.insert(Tile::Numbered(i)); |
263 |
left.insert(Tile::Numbered(p)); |
|
264 |
} |
|
265 |
||
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
266 |
// Check top edge |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
267 |
if tiles[p].top_edge().is_compatible(tile.bottom_edge()) { |
15917 | 268 |
rules[p].top.insert(Tile::Numbered(i)); |
269 |
bottom.insert(Tile::Numbered(p)); |
|
270 |
} |
|
271 |
||
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
272 |
// Check bottom edge |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
273 |
if tiles[p].bottom_edge().is_compatible(tile.top_edge()) { |
15917 | 274 |
rules[p].bottom.insert(Tile::Numbered(i)); |
275 |
top.insert(Tile::Numbered(p)); |
|
276 |
} |
|
277 |
} |
|
278 |
||
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
279 |
let weight = (probability_distribution_factor * 2 * i as i32 / (tiles.len() - 1) as i32 |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
280 |
+ 100 |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
281 |
- probability_distribution_factor) as u32; |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
282 |
|
15917 | 283 |
rules.push(CollapseRule { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
284 |
weight, |
15917 | 285 |
tile: Tile::Numbered(i), |
286 |
top, |
|
287 |
right, |
|
288 |
bottom, |
|
289 |
left, |
|
290 |
}); |
|
291 |
} |
|
292 |
||
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
293 |
rules |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
294 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
295 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
296 |
|
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
297 |
impl LandGenerator for WavefrontCollapseLandGenerator { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
298 |
fn generate_land<T: Copy + PartialEq + Default>( |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
299 |
&self, |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
300 |
parameters: &LandGenerationParameters<T>, |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
301 |
random_numbers: &mut impl Rng, |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
302 |
) -> land2d::Land2D<T> { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
303 |
assert!(parameters.distance_divisor >= 1); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
304 |
assert!(parameters.distance_divisor <= 25); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
305 |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
306 |
let tiles = self.load_template(parameters); |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
307 |
let distribution_factor = (parameters.distance_divisor - 1) as i32 * 8 - 96; |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
16029
diff
changeset
|
308 |
let rules = self.build_rules(&tiles, distribution_factor); |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
309 |
|
15924 | 310 |
let mut wfc = WavefrontCollapse::new(self.template.wrap); |
15917 | 311 |
wfc.set_rules(rules); |
312 |
||
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
313 |
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
|
314 |
let tile_size = first_tile.size(); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
315 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
316 |
Size::new( |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
317 |
self.template.size.width / tile_size.width, |
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
318 |
self.template.size.height / tile_size.height, |
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
319 |
) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
320 |
} else { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
321 |
Size::new(1, 1) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
322 |
}; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
323 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
324 |
wfc.generate_map(&wfc_size, |_| {}, random_numbers); |
15917 | 325 |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
326 |
// render tiles into resulting land array |
15922
da6b67f13c12
Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
15920
diff
changeset
|
327 |
let mut result = land2d::Land2D::new(&self.template.size, parameters.zero); |
15924 | 328 |
let offset_y = result.height() - result.play_height(); |
329 |
let offset_x = (result.width() - result.play_width()) / 2; |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
330 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
331 |
for row in 0..wfc_size.height { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
332 |
for column in 0..wfc_size.width { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
333 |
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
|
334 |
let tile = &tiles[*tile_index]; |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
335 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
336 |
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
|
337 |
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
|
338 |
result.map( |
15924 | 339 |
(row * tile.size().height + tile_row + offset_y) as i32, |
340 |
(column * tile.size().width + tile_column + offset_x) as i32, |
|
15918
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
341 |
|p| { |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
342 |
*p = |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
343 |
*tile.get(tile_row, tile_column).unwrap_or(¶meters.zero) |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
344 |
}, |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
345 |
); |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
346 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
347 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
348 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
349 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
350 |
} |
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
351 |
|
9bd828451d77
Fix several issues with transformations, more work on getting generated image
unC0Rr
parents:
15917
diff
changeset
|
352 |
result |
15913 | 353 |
} |
354 |
} |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
355 |
|
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
356 |
impl From<&EdgeDescription> for Edge<String> { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
357 |
fn from(val: &EdgeDescription) -> Self { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
358 |
let edge = Edge::new(val.name.clone(), val.symmetrical.unwrap_or_default()); |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
359 |
|
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
360 |
if val.reversed.unwrap_or_default() { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
361 |
edge.reversed() |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
362 |
} else { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
363 |
edge |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
364 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
365 |
} |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
366 |
} |