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