author | unC0Rr |
Tue, 28 Jan 2025 15:49:45 +0100 | |
changeset 16073 | 5d302b12d837 |
parent 16059 | 2acea266d297 |
child 16075 | 2c2b094e6bbe |
permissions | -rw-r--r-- |
15912 | 1 |
use integral_geometry::Size; |
16073 | 2 |
use rand::distr::{weighted::WeightedIndex, Distribution}; |
3 |
use rand::prelude::IndexedRandom; |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
4 |
use rand::Rng; |
15917 | 5 |
use std::collections::HashSet; |
15913 | 6 |
use vec2d::Vec2D; |
15912 | 7 |
|
8 |
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] |
|
15913 | 9 |
pub enum Tile { |
15912 | 10 |
Empty, |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
11 |
OutsideBegin, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
12 |
OutsideFill, |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
13 |
OutsideEnd, |
15917 | 14 |
Numbered(usize), |
15912 | 15 |
} |
16 |
||
15917 | 17 |
#[derive(Debug)] |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
18 |
pub struct CollapseRule { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
19 |
pub weight: u32, |
15917 | 20 |
pub tile: Tile, |
21 |
pub right: HashSet<Tile>, |
|
22 |
pub bottom: HashSet<Tile>, |
|
23 |
pub left: HashSet<Tile>, |
|
24 |
pub top: HashSet<Tile>, |
|
15912 | 25 |
} |
26 |
||
15913 | 27 |
pub struct WavefrontCollapse { |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
28 |
rules: Vec<CollapseRule>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
29 |
grid: Vec2D<Tile>, |
15924 | 30 |
wrap: bool, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
31 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
32 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
33 |
impl Default for WavefrontCollapse { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
34 |
fn default() -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
35 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
36 |
rules: Vec::new(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
37 |
grid: Vec2D::new(&Size::new(1, 1), Tile::Empty), |
15924 | 38 |
wrap: false, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
39 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
40 |
} |
15912 | 41 |
} |
42 |
||
43 |
impl WavefrontCollapse { |
|
15924 | 44 |
pub fn new(wrap: bool) -> Self { |
45 |
Self { |
|
46 |
rules: Vec::new(), |
|
47 |
grid: Vec2D::new(&Size::new(1, 1), Tile::Empty), |
|
48 |
wrap, |
|
49 |
} |
|
50 |
} |
|
51 |
||
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
52 |
pub fn generate_map<F: FnOnce(&mut Vec2D<Tile>)>( |
15912 | 53 |
&mut self, |
54 |
map_size: &Size, |
|
55 |
seed_fn: F, |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
56 |
random_numbers: &mut impl Rng, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
57 |
) { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
58 |
self.grid = Vec2D::new(map_size, Tile::Empty); |
15912 | 59 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
60 |
seed_fn(&mut self.grid); |
15912 | 61 |
|
16073 | 62 |
let mut backtracks = 0usize; |
63 |
while let Some(b) = self.collapse_step(random_numbers) { |
|
64 |
backtracks += b; |
|
65 |
||
66 |
if backtracks >= 1000 { |
|
67 |
println!("[WFC] Too much backtracking, stopping generation!"); |
|
68 |
break; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
if backtracks > 0 { |
|
73 |
println!("[WFC] Had to backtrack {} times...", backtracks); |
|
74 |
} |
|
15912 | 75 |
} |
76 |
||
15917 | 77 |
pub fn set_rules(&mut self, rules: Vec<CollapseRule>) { |
78 |
self.rules = rules; |
|
79 |
} |
|
80 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
81 |
fn get_tile(&self, y: usize, x: usize) -> Tile { |
15924 | 82 |
let x = if self.wrap { |
83 |
if x == usize::MAX { |
|
84 |
self.grid.width() - 1 |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
85 |
} else if x == self.grid.width() { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
86 |
0 |
15924 | 87 |
} else { |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
88 |
x |
15924 | 89 |
} |
90 |
} else { |
|
91 |
x |
|
92 |
}; |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
93 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
94 |
self.grid.get(y, x).copied().unwrap_or_else(|| { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
95 |
let x_out = x >= self.grid.width(); |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
96 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
97 |
if x_out { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
98 |
let y_at_begin = y == 0; |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
99 |
let y_at_end = y.wrapping_add(1) == self.grid.height(); |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
100 |
if y_at_begin { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
101 |
Tile::OutsideBegin |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
102 |
} else if y_at_end { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
103 |
Tile::OutsideEnd |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
104 |
} else { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
105 |
Tile::OutsideFill |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
106 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
107 |
} else { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
108 |
// if not x, then it is y |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
109 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
110 |
let x_at_begin = x == 0; |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
111 |
let x_at_end = x.wrapping_add(1) == self.grid.width(); |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
112 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
113 |
if x_at_begin { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
114 |
Tile::OutsideBegin |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
115 |
} else if x_at_end { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
116 |
Tile::OutsideEnd |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
117 |
} else { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
118 |
Tile::OutsideFill |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
119 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
120 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
121 |
}) |
15912 | 122 |
} |
123 |
||
16073 | 124 |
fn collapse_step(&mut self, random_numbers: &mut impl Rng) -> Option<usize> { |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
125 |
let mut tiles_to_collapse = (usize::MAX, Vec::new()); |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
126 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
127 |
// Iterate through the tiles in the land |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
128 |
for x in 0..self.grid.width() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
129 |
for y in 0..self.grid.height() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
130 |
let current_tile = self.get_tile(y, x); |
15912 | 131 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
132 |
if let Tile::Empty = current_tile { |
16073 | 133 |
let neighbors = [ |
134 |
(y, x.wrapping_add(1)), |
|
135 |
(y.wrapping_add(1), x), |
|
136 |
(y, x.wrapping_sub(1)), |
|
137 |
(y.wrapping_sub(1), x), |
|
138 |
]; |
|
139 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
140 |
// calc entropy |
16073 | 141 |
let [right_tile, bottom_tile, left_tile, top_tile] = |
142 |
neighbors.map(|(y, x)| self.get_tile(y, x)); |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
143 |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
144 |
let possibilities: Vec<(u32, Tile)> = self |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
145 |
.rules |
15912 | 146 |
.iter() |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
147 |
.filter_map(|rule| { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
148 |
if rule.right.contains(&right_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
149 |
&& rule.bottom.contains(&bottom_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
150 |
&& rule.left.contains(&left_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
151 |
&& rule.top.contains(&top_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
152 |
{ |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
153 |
Some((rule.weight, rule.tile)) |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
154 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
155 |
None |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
156 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
157 |
}) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
158 |
.collect(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
159 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
160 |
let entropy = possibilities.len(); |
15917 | 161 |
if entropy > 0 { |
162 |
if entropy <= tiles_to_collapse.0 { |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
163 |
let weights = possibilities.iter().map(|(weight, _)| *weight); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
164 |
let distribution = WeightedIndex::new(weights).unwrap(); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
165 |
|
16073 | 166 |
let entry = |
167 |
(y, x, possibilities[distribution.sample(random_numbers)].1); |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
168 |
|
15917 | 169 |
if entropy < tiles_to_collapse.0 { |
170 |
tiles_to_collapse = (entropy, vec![entry]) |
|
171 |
} else { |
|
172 |
tiles_to_collapse.1.push(entry) |
|
173 |
} |
|
15912 | 174 |
} |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
175 |
} else { |
16073 | 176 |
/* |
177 |
println!("We're here: {}, {}", x, y); |
|
15917 | 178 |
println!( |
179 |
"Neighbour tiles are: {:?} {:?} {:?} {:?}", |
|
180 |
right_tile, bottom_tile, left_tile, top_tile |
|
181 |
); |
|
16073 | 182 |
println!("Rules are: {:?}", self.rules); |
183 |
*/ |
|
184 |
||
185 |
let entries = neighbors |
|
186 |
.iter() |
|
187 |
.filter(|(y, x)| self.grid.get(*y, *x).is_some()) |
|
188 |
.map(|(y, x)| (*y, *x, Tile::Empty)) |
|
189 |
.collect::<Vec<_>>(); |
|
190 |
||
191 |
if entropy < tiles_to_collapse.0 { |
|
192 |
tiles_to_collapse = (entropy, entries); |
|
193 |
} else { |
|
194 |
tiles_to_collapse.1.extend(entries); |
|
195 |
} |
|
15917 | 196 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
197 |
//todo!("no collapse possible - what to do?") |
15912 | 198 |
} |
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
||
16073 | 203 |
if tiles_to_collapse.0 == 0 { |
204 |
// cannot collapse, we're clearing some tiles |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
205 |
|
16073 | 206 |
for (y, x, tile) in tiles_to_collapse.1 { |
207 |
*self |
|
208 |
.grid |
|
209 |
.get_mut(y, x) |
|
210 |
.expect("correct iteration over grid") = tile; |
|
211 |
} |
|
15912 | 212 |
|
16073 | 213 |
Some(1) |
214 |
} else { |
|
215 |
if let Some(&(y, x, tile)) = tiles_to_collapse.1.as_slice().choose(random_numbers) { |
|
216 |
*self |
|
217 |
.grid |
|
218 |
.get_mut(y, x) |
|
219 |
.expect("correct iteration over grid") = tile; |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
220 |
|
16073 | 221 |
Some(0) |
222 |
} else { |
|
223 |
None |
|
224 |
} |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
225 |
} |
15912 | 226 |
} |
15917 | 227 |
|
228 |
pub fn grid(&self) -> &Vec2D<Tile> { |
|
229 |
&self.grid |
|
230 |
} |
|
15912 | 231 |
} |
232 |
||
233 |
#[cfg(test)] |
|
234 |
mod tests { |
|
15913 | 235 |
use super::{Tile, WavefrontCollapse}; |
15912 | 236 |
use integral_geometry::Size; |
15913 | 237 |
use vec2d::Vec2D; |
15912 | 238 |
|
239 |
#[test] |
|
240 |
fn test_wavefront_collapse() { |
|
241 |
let size = Size::new(4, 4); |
|
15917 | 242 |
let mut rnd = [0u32; 64].into_iter().cycle(); |
15912 | 243 |
let mut wfc = WavefrontCollapse::default(); |
244 |
||
15917 | 245 |
wfc.generate_map(&size, |_| {}, &mut rnd); |
246 |
||
15913 | 247 |
let empty_land = Vec2D::new(&size, Tile::Empty); |
15912 | 248 |
|
15917 | 249 |
assert_eq!(empty_land.as_slice(), wfc.grid().as_slice()); |
15912 | 250 |
} |
251 |
} |