1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Wave Function Collapse: constraint-based procedural generation
use std::collections::VecDeque;
/// Adjacency rules: which tiles can appear next to each other
pub struct TileRules {
/// Number of distinct tiles (ids are `0..n_tiles`).
pub n_tiles: usize,
/// `allowed_neighbors[tile][direction]`: the set of tile ids allowed next to `tile` in that direction.
/// Directions: 0=North, 1=East, 2=South, 3=West
pub allowed_neighbors: Vec<Vec<Vec<usize>>>,
/// Relative probability of choosing each tile when a cell collapses.
pub tile_weights: Vec<f64>,
}
impl TileRules {
/// Create rules for `n_tiles` tiles with no allowed neighbours and equal weights.
pub fn new(n_tiles: usize) -> Self {
TileRules {
n_tiles,
allowed_neighbors: vec![vec![vec![]; 4]; n_tiles],
tile_weights: vec![1.0; n_tiles],
}
}
/// Allow `tile_b` next to `tile_a` in direction `dir` (and the reverse in the opposite direction).
pub fn allow(mut self, tile_a: usize, dir: usize, tile_b: usize) -> Self {
let opposite = [2, 3, 0, 1][dir]; // N<->S, E<->W
self.allowed_neighbors[tile_a][dir].push(tile_b);
self.allowed_neighbors[tile_b][opposite].push(tile_a);
self
}
/// Set the selection weight of `tile`.
pub fn set_weight(mut self, tile: usize, weight: f64) -> Self {
self.tile_weights[tile] = weight;
self
}
}
/// Create simple checkerboard-compatible rules (3 tiles: empty, wall, floor)
pub fn default_rules() -> TileRules {
TileRules::new(3)
.allow(0, 0, 0)
.allow(0, 1, 0)
.allow(0, 2, 0)
.allow(0, 3, 0)
.allow(1, 0, 1)
.allow(1, 1, 1)
.allow(1, 2, 1)
.allow(1, 3, 1)
.allow(2, 0, 2)
.allow(2, 1, 2)
.allow(2, 2, 2)
.allow(2, 3, 2)
.allow(0, 0, 2)
.allow(0, 1, 2)
.allow(0, 2, 2)
.allow(0, 3, 2)
.allow(1, 1, 2)
.allow(1, 3, 2)
}
/// A grid being solved by wave function collapse.
pub struct WfcGrid {
/// Number of columns.
pub width: usize,
/// Number of rows.
pub height: usize,
cells: Vec<Vec<usize>>, // possible tiles at each cell
collapsed: Vec<bool>,
rules: TileRules,
}
impl WfcGrid {
/// Create a grid where every cell can still be any tile.
pub fn new(width: usize, height: usize, rules: TileRules) -> Self {
let all_tiles: Vec<usize> = (0..rules.n_tiles).collect();
WfcGrid {
width,
height,
cells: vec![all_tiles; width * height],
collapsed: vec![false; width * height],
rules,
}
}
fn idx(&self, x: usize, y: usize) -> usize {
y * self.width + x
}
/// Shannon entropy for a cell
fn entropy(&self, x: usize, y: usize) -> f64 {
let possibilities = &self.cells[self.idx(x, y)];
if possibilities.len() <= 1 {
return 0.0;
}
let weights: Vec<f64> = possibilities
.iter()
.map(|&t| self.rules.tile_weights[t])
.collect();
let total: f64 = weights.iter().sum();
-weights
.iter()
.map(|&w| {
let p = w / total;
if p > 0.0 {
p * p.ln()
} else {
0.0
}
})
.sum::<f64>()
}
/// Find cell with lowest non-zero entropy
fn min_entropy_cell(&self, seed: u64) -> Option<(usize, usize)> {
let mut best = f64::MAX;
let mut best_cells = Vec::new();
for y in 0..self.height {
for x in 0..self.width {
if !self.collapsed[self.idx(x, y)] {
let e = self.entropy(x, y);
if e > 0.0 {
if e < best - 1e-10 {
best = e;
best_cells.clear();
}
if (e - best).abs() < 1e-10 {
best_cells.push((x, y));
}
}
}
}
}
if best_cells.is_empty() {
return None;
}
Some(best_cells[seed as usize % best_cells.len()])
}
/// Collapse a cell to one tile (weighted random selection)
fn collapse_cell(&mut self, x: usize, y: usize, seed: u64) {
let idx = self.idx(x, y);
let possibilities = &self.cells[idx];
let weights: Vec<f64> = possibilities
.iter()
.map(|&t| self.rules.tile_weights[t])
.collect();
let total: f64 = weights.iter().sum();
let mut r = (seed as f64 / u64::MAX as f64) * total;
let chosen = possibilities
.iter()
.zip(weights.iter())
.find(|(_, &w)| {
r -= w;
r <= 0.0
})
.map(|(&t, _)| t)
.unwrap_or(possibilities[0]);
self.cells[idx] = vec![chosen];
self.collapsed[idx] = true;
}
/// Propagate constraints via AC-3
fn propagate(&mut self, start_x: usize, start_y: usize) -> bool {
let mut queue: VecDeque<(usize, usize)> = VecDeque::new();
queue.push_back((start_x, start_y));
while let Some((cx, cy)) = queue.pop_front() {
let neighbors: Vec<(usize, usize, usize)> = [
(cy.wrapping_sub(1), cx, 0), // North: (y-1, x, dir=0)
(cy, cx + 1, 1), // East
(cy + 1, cx, 2), // South
(cy, cx.wrapping_sub(1), 3), // West
]
.iter()
.filter_map(|&(ny, nx, dir)| {
if nx < self.width && ny < self.height {
Some((nx, ny, dir))
} else {
None
}
})
.collect();
for (nx, ny, dir) in neighbors {
let ni = self.idx(nx, ny);
let ci = self.idx(cx, cy);
let current_tiles = self.cells[ci].clone();
let before_len = self.cells[ni].len();
self.cells[ni].retain(|&neighbor_tile| {
current_tiles
.iter()
.any(|&ct| self.rules.allowed_neighbors[ct][dir].contains(&neighbor_tile))
});
if self.cells[ni].is_empty() {
return false;
} // contradiction
if self.cells[ni].len() < before_len {
queue.push_back((nx, ny));
}
}
}
true
}
/// Run WFC until fully collapsed or contradiction
pub fn run(&mut self, seed: u64) -> bool {
let mut state = seed;
loop {
state = state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
match self.min_entropy_cell(state) {
None => return true, // all collapsed
Some((x, y)) => {
self.collapse_cell(x, y, state >> 11);
if !self.propagate(x, y) {
return false;
} // contradiction
}
}
}
}
/// Get final tile map (returns 0 if cell has no possibilities)
pub fn result(&self) -> Vec<usize> {
self.cells
.iter()
.map(|c| c.first().copied().unwrap_or(0))
.collect()
}
/// Render as RGBA pixels with tile colors
pub fn render(&self, tile_size: u32, colors: &[(u8, u8, u8)]) -> Vec<u8> {
let pw = self.width as u32 * tile_size;
let ph = self.height as u32 * tile_size;
let tiles = self.result();
let mut pixels = vec![0u8; (pw * ph * 4) as usize];
for cy in 0..self.height {
for cx in 0..self.width {
let tile = tiles[self.idx(cx, cy)];
let (r, g, b) = colors
.get(tile % colors.len())
.copied()
.unwrap_or((128, 128, 128));
for ty in 0..tile_size {
for tx in 0..tile_size {
let px = cx as u32 * tile_size + tx;
let py = cy as u32 * tile_size + ty;
let pi = ((py * pw + px) * 4) as usize;
if pi + 3 < pixels.len() {
pixels[pi] = r;
pixels[pi + 1] = g;
pixels[pi + 2] = b;
pixels[pi + 3] = 255;
}
}
}
}
}
pixels
}
}