author | unC0Rr |
Tue, 21 Jan 2025 22:10:55 +0100 | |
changeset 16059 | 2acea266d297 |
parent 16058 | de01be16df95 |
child 16073 | 5d302b12d837 |
permissions | -rw-r--r-- |
15912 | 1 |
use integral_geometry::Size; |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
2 |
use rand::distributions::Distribution; |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
3 |
use rand::distributions::WeightedIndex; |
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 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
62 |
while self.collapse_step(random_numbers) {} |
15912 | 63 |
} |
64 |
||
15917 | 65 |
pub fn set_rules(&mut self, rules: Vec<CollapseRule>) { |
66 |
self.rules = rules; |
|
67 |
} |
|
68 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
69 |
fn get_tile(&self, y: usize, x: usize) -> Tile { |
15924 | 70 |
let x = if self.wrap { |
71 |
if x == usize::MAX { |
|
72 |
self.grid.width() - 1 |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
73 |
} else if x == self.grid.width() { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
74 |
0 |
15924 | 75 |
} else { |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
76 |
x |
15924 | 77 |
} |
78 |
} else { |
|
79 |
x |
|
80 |
}; |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
81 |
|
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
82 |
self.grid.get(y, x).copied().unwrap_or_else(|| { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
83 |
let x_out = x >= self.grid.width(); |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
84 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
85 |
if x_out { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
86 |
let y_at_begin = y == 0; |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
87 |
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
|
88 |
if y_at_begin { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
89 |
Tile::OutsideBegin |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
90 |
} else if y_at_end { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
91 |
Tile::OutsideEnd |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
92 |
} else { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
93 |
Tile::OutsideFill |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
94 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
95 |
} else { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
96 |
// if not x, then it is y |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
97 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
98 |
let x_at_begin = x == 0; |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
99 |
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
|
100 |
|
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
101 |
if x_at_begin { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
102 |
Tile::OutsideBegin |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
103 |
} else if x_at_end { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
104 |
Tile::OutsideEnd |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
105 |
} else { |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
106 |
Tile::OutsideFill |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
107 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
108 |
} |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
109 |
}) |
15912 | 110 |
} |
111 |
||
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
112 |
fn collapse_step(&mut self, random_numbers: &mut impl Rng) -> bool { |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
113 |
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
|
114 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
115 |
// Iterate through the tiles in the land |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
116 |
for x in 0..self.grid.width() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
117 |
for y in 0..self.grid.height() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
118 |
let current_tile = self.get_tile(y, x); |
15912 | 119 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
120 |
if let Tile::Empty = current_tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
121 |
// calc entropy |
16059
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
122 |
let right_tile = self.get_tile(y, x.wrapping_add(1)); |
2acea266d297
Fix generation in corners by extending outline edge definitions
unC0Rr
parents:
16058
diff
changeset
|
123 |
let bottom_tile = self.get_tile(y.wrapping_add(1), x); |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
124 |
let left_tile = self.get_tile(y, x.wrapping_sub(1)); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
125 |
let top_tile = self.get_tile(y.wrapping_sub(1), x); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
126 |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
127 |
let possibilities: Vec<(u32, Tile)> = self |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
128 |
.rules |
15912 | 129 |
.iter() |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
130 |
.filter_map(|rule| { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
131 |
if rule.right.contains(&right_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
132 |
&& rule.bottom.contains(&bottom_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
133 |
&& rule.left.contains(&left_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
134 |
&& rule.top.contains(&top_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
135 |
{ |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
136 |
Some((rule.weight, rule.tile)) |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
137 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
138 |
None |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
139 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
140 |
}) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
141 |
.collect(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
142 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
143 |
let entropy = possibilities.len(); |
15917 | 144 |
if entropy > 0 { |
145 |
if entropy <= tiles_to_collapse.0 { |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
146 |
let weights = possibilities.iter().map(|(weight, _)| *weight); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
147 |
let distribution = WeightedIndex::new(weights).unwrap(); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
148 |
|
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
149 |
let entry = (y, x, possibilities[distribution.sample(random_numbers)]); |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
150 |
|
15917 | 151 |
if entropy < tiles_to_collapse.0 { |
152 |
tiles_to_collapse = (entropy, vec![entry]) |
|
153 |
} else { |
|
154 |
tiles_to_collapse.1.push(entry) |
|
155 |
} |
|
15912 | 156 |
} |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
157 |
} else { |
15919 | 158 |
/*println!("We're here: {}, {}", x, y); |
15917 | 159 |
println!( |
160 |
"Neighbour tiles are: {:?} {:?} {:?} {:?}", |
|
161 |
right_tile, bottom_tile, left_tile, top_tile |
|
162 |
); |
|
15919 | 163 |
println!("Rules are: {:?}", self.rules);*/ |
15917 | 164 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
165 |
//todo!("no collapse possible - what to do?") |
15912 | 166 |
} |
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
171 |
let tiles_to_collapse = tiles_to_collapse.1; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
172 |
let possibilities_number = tiles_to_collapse.len(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
173 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
174 |
if possibilities_number > 0 { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
175 |
let weights = tiles_to_collapse.iter().map(|(_, _, (weight, _))| *weight); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
176 |
let distribution = WeightedIndex::new(weights).unwrap(); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
177 |
|
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
178 |
let (y, x, (_, tile)) = tiles_to_collapse[distribution.sample(random_numbers)]; |
15912 | 179 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
180 |
*self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
181 |
.grid |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
182 |
.get_mut(y, x) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
183 |
.expect("correct iteration over grid") = tile; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
184 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
185 |
true |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
186 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
187 |
false |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
188 |
} |
15912 | 189 |
} |
15917 | 190 |
|
191 |
pub fn grid(&self) -> &Vec2D<Tile> { |
|
192 |
&self.grid |
|
193 |
} |
|
15912 | 194 |
} |
195 |
||
196 |
#[cfg(test)] |
|
197 |
mod tests { |
|
15913 | 198 |
use super::{Tile, WavefrontCollapse}; |
15912 | 199 |
use integral_geometry::Size; |
15913 | 200 |
use vec2d::Vec2D; |
15912 | 201 |
|
202 |
#[test] |
|
203 |
fn test_wavefront_collapse() { |
|
204 |
let size = Size::new(4, 4); |
|
15917 | 205 |
let mut rnd = [0u32; 64].into_iter().cycle(); |
15912 | 206 |
let mut wfc = WavefrontCollapse::default(); |
207 |
||
15917 | 208 |
wfc.generate_map(&size, |_| {}, &mut rnd); |
209 |
||
15913 | 210 |
let empty_land = Vec2D::new(&size, Tile::Empty); |
15912 | 211 |
|
15917 | 212 |
assert_eq!(empty_land.as_slice(), wfc.grid().as_slice()); |
15912 | 213 |
} |
214 |
} |