author | unC0Rr |
Sat, 18 Jan 2025 16:57:26 +0100 | |
changeset 16058 | de01be16df95 |
parent 15925 | b0e8cc72bfef |
child 16059 | 2acea266d297 |
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, |
11 |
Outside, |
|
15917 | 12 |
Numbered(usize), |
15912 | 13 |
} |
14 |
||
15 |
impl Default for Tile { |
|
16 |
fn default() -> Self { |
|
17 |
Tile::Outside |
|
18 |
} |
|
19 |
} |
|
20 |
||
15917 | 21 |
#[derive(Debug)] |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
22 |
pub struct CollapseRule { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
23 |
pub weight: u32, |
15917 | 24 |
pub tile: Tile, |
25 |
pub right: HashSet<Tile>, |
|
26 |
pub bottom: HashSet<Tile>, |
|
27 |
pub left: HashSet<Tile>, |
|
28 |
pub top: HashSet<Tile>, |
|
15912 | 29 |
} |
30 |
||
15913 | 31 |
pub struct WavefrontCollapse { |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
32 |
rules: Vec<CollapseRule>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
33 |
grid: Vec2D<Tile>, |
15924 | 34 |
wrap: bool, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
35 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
36 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
37 |
impl Default for WavefrontCollapse { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
38 |
fn default() -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
39 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
40 |
rules: Vec::new(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
41 |
grid: Vec2D::new(&Size::new(1, 1), Tile::Empty), |
15924 | 42 |
wrap: false, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
43 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
44 |
} |
15912 | 45 |
} |
46 |
||
47 |
impl WavefrontCollapse { |
|
15924 | 48 |
pub fn new(wrap: bool) -> Self { |
49 |
Self { |
|
50 |
rules: Vec::new(), |
|
51 |
grid: Vec2D::new(&Size::new(1, 1), Tile::Empty), |
|
52 |
wrap, |
|
53 |
} |
|
54 |
} |
|
55 |
||
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
56 |
pub fn generate_map<F: FnOnce(&mut Vec2D<Tile>)>( |
15912 | 57 |
&mut self, |
58 |
map_size: &Size, |
|
59 |
seed_fn: F, |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
60 |
random_numbers: &mut impl Rng, |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
61 |
) { |
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
62 |
self.grid = Vec2D::new(map_size, Tile::Empty); |
15912 | 63 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
64 |
seed_fn(&mut self.grid); |
15912 | 65 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
66 |
while self.collapse_step(random_numbers) {} |
15912 | 67 |
} |
68 |
||
15917 | 69 |
pub fn set_rules(&mut self, rules: Vec<CollapseRule>) { |
70 |
self.rules = rules; |
|
71 |
} |
|
72 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
73 |
fn get_tile(&self, y: usize, x: usize) -> Tile { |
15924 | 74 |
let x = if self.wrap { |
75 |
if x == usize::MAX { |
|
76 |
self.grid.width() - 1 |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
77 |
} else if x == self.grid.width() { |
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
78 |
0 |
15924 | 79 |
} else { |
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
80 |
x |
15924 | 81 |
} |
82 |
} else { |
|
83 |
x |
|
84 |
}; |
|
15925
b0e8cc72bfef
Allow defining compatible edges for grid, add few more templates
unC0Rr
parents:
15924
diff
changeset
|
85 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
86 |
self.grid.get(y, x).copied().unwrap_or_default() |
15912 | 87 |
} |
88 |
||
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
89 |
fn collapse_step(&mut self, random_numbers: &mut impl Rng) -> bool { |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
90 |
let mut tiles_to_collapse = (usize::max_value(), Vec::new()); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
91 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
92 |
// Iterate through the tiles in the land |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
93 |
for x in 0..self.grid.width() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
94 |
for y in 0..self.grid.height() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
95 |
let current_tile = self.get_tile(y, x); |
15912 | 96 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
97 |
if let Tile::Empty = current_tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
98 |
// calc entropy |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
99 |
let right_tile = self.get_tile(y, x + 1); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
100 |
let bottom_tile = self.get_tile(y + 1, x); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
104 |
let possibilities: Vec<(u32, Tile)> = self |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
105 |
.rules |
15912 | 106 |
.iter() |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
107 |
.filter_map(|rule| { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
108 |
if rule.right.contains(&right_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
109 |
&& rule.bottom.contains(&bottom_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
110 |
&& rule.left.contains(&left_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
111 |
&& rule.top.contains(&top_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
112 |
{ |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
113 |
Some((rule.weight, rule.tile)) |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
114 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
115 |
None |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
116 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
117 |
}) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
118 |
.collect(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
119 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
120 |
let entropy = possibilities.len(); |
15917 | 121 |
if entropy > 0 { |
122 |
if entropy <= tiles_to_collapse.0 { |
|
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
123 |
let weights = possibilities.iter().map(|(weight, _)| *weight); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
124 |
let distribution = WeightedIndex::new(weights).unwrap(); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
125 |
|
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
126 |
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
|
127 |
|
15917 | 128 |
if entropy < tiles_to_collapse.0 { |
129 |
tiles_to_collapse = (entropy, vec![entry]) |
|
130 |
} else { |
|
131 |
tiles_to_collapse.1.push(entry) |
|
132 |
} |
|
15912 | 133 |
} |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
134 |
} else { |
15919 | 135 |
/*println!("We're here: {}, {}", x, y); |
15917 | 136 |
println!( |
137 |
"Neighbour tiles are: {:?} {:?} {:?} {:?}", |
|
138 |
right_tile, bottom_tile, left_tile, top_tile |
|
139 |
); |
|
15919 | 140 |
println!("Rules are: {:?}", self.rules);*/ |
15917 | 141 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
142 |
//todo!("no collapse possible - what to do?") |
15912 | 143 |
} |
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
148 |
let tiles_to_collapse = tiles_to_collapse.1; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
149 |
let possibilities_number = tiles_to_collapse.len(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
150 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
151 |
if possibilities_number > 0 { |
16058
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
152 |
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
|
153 |
let distribution = WeightedIndex::new(weights).unwrap(); |
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
154 |
|
de01be16df95
Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents:
15925
diff
changeset
|
155 |
let (y, x, (_, tile)) = tiles_to_collapse[distribution.sample(random_numbers)]; |
15912 | 156 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
157 |
*self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
158 |
.grid |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
159 |
.get_mut(y, x) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
160 |
.expect("correct iteration over grid") = tile; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
161 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
162 |
true |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
163 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
164 |
false |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
165 |
} |
15912 | 166 |
} |
15917 | 167 |
|
168 |
pub fn grid(&self) -> &Vec2D<Tile> { |
|
169 |
&self.grid |
|
170 |
} |
|
15912 | 171 |
} |
172 |
||
173 |
#[cfg(test)] |
|
174 |
mod tests { |
|
15913 | 175 |
use super::{Tile, WavefrontCollapse}; |
15912 | 176 |
use integral_geometry::Size; |
15913 | 177 |
use vec2d::Vec2D; |
15912 | 178 |
|
179 |
#[test] |
|
180 |
fn test_wavefront_collapse() { |
|
181 |
let size = Size::new(4, 4); |
|
15917 | 182 |
let mut rnd = [0u32; 64].into_iter().cycle(); |
15912 | 183 |
let mut wfc = WavefrontCollapse::default(); |
184 |
||
15917 | 185 |
wfc.generate_map(&size, |_| {}, &mut rnd); |
186 |
||
15913 | 187 |
let empty_land = Vec2D::new(&size, Tile::Empty); |
15912 | 188 |
|
15917 | 189 |
assert_eq!(empty_land.as_slice(), wfc.grid().as_slice()); |
15912 | 190 |
} |
191 |
} |