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