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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
//! The `wfc` module contains the implementation of the
//! Wave Function Collapse algorithm for procedural generation of the `Floor`.
// TODO: Add pattern size input
// Reference map's map size and tile count constants
use crate::map::TILE_COUNT;
use crate::map::Tile;
use crate::pos::Direction;
use crate::pos::Pos;
use rand::prelude::IndexedRandom;
/// The `State` struct represents each possible state of a tile in the WFC algorithm.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct State {
/// Position of the tile
#[expect(dead_code)]
pos: Pos,
/// Possible tiles for this state
possible_tiles: Vec<Tile>,
/// Entropy of the state
entropy: usize,
}
impl State {
/// Creates a new State instance
pub fn new(pos: Pos, possible_tiles: Vec<Tile>) -> Self {
let entropy = possible_tiles.len();
Self {
pos,
possible_tiles,
entropy,
}
}
/// Updates the possible tiles and recalculates entropy
pub fn set_possible_tiles(&mut self, possible_tiles: Vec<Tile>) {
self.possible_tiles = possible_tiles;
self.entropy = self.entropy();
}
/// Calculates the entropy of the state
/// TODO: Implement shannon entropy calculation
pub fn entropy(&self) -> usize {
self.possible_tiles.len()
}
/// Checks if the state has been collapsed to a single tile
pub fn is_collapsed(&self) -> bool {
self.possible_tiles.len() == 1
}
/// Returns a reference to the possible tiles
pub fn possible_tiles(&self) -> &Vec<Tile> {
&self.possible_tiles
}
/// Collapses the state to a single tile
pub fn collapse(&mut self, tile: Tile) {
self.possible_tiles = vec![tile];
self.entropy = 1;
}
}
/// The `Compatability` struct represents the compatibility rules between tiles.
pub(crate) struct Compatability {
/// The first tile in the compatibility pair
tile1: Tile,
/// The second tile in the compatibility pair
tile2: Tile,
/// The direction from tile1 -> tile2
direction: Direction,
}
impl Compatability {
/// Creates a new Compatability instance
pub fn new(tile1: Tile, tile2: Tile, direction: Direction) -> Self {
Self {
tile1,
tile2,
direction,
}
}
}
/// The `CompatabilityChecker` struct manages compatibility rules between tiles.
pub(crate) struct CompatabilityChecker {
compatabilities: Vec<Compatability>,
}
impl CompatabilityChecker {
/// Creates a new CompatabilityChecker instance
pub fn new(input: &[Vec<Tile>]) -> Self {
let mut compatabilities = Vec::new();
// Which pairs of tiles can be placed next to each other and in which directions
for row in input {
for window in row.windows(2) {
if let [tile1, tile2] = window {
compatabilities.push(Compatability::new(
*tile1,
*tile2,
Direction::East,
));
compatabilities.push(Compatability::new(
*tile2,
*tile1,
Direction::West,
));
}
}
}
for col in 0..input[0].len() {
for row in 0..input.len() - 1 {
let tile1 = input[row][col];
let tile2 = input[row + 1][col];
compatabilities.push(Compatability::new(tile1, tile2, Direction::North));
compatabilities.push(Compatability::new(tile2, tile1, Direction::South));
}
}
Self { compatabilities }
}
/// Checks if two tiles are compatible in the given direction
pub fn is_valid(&self, tile1: Tile, tile2: Tile, direction: Direction) -> bool {
self.compatabilities
.iter()
.any(|c| c.tile1 == tile1 && c.tile2 == tile2 && c.direction == direction)
}
}
/// The `Wfc` struct encapsulates the Wave Function Collapse algorithm.
pub struct Wfc {
/// The compatibility checker for tile constraints
compatabilities: CompatabilityChecker,
/// The random number generator
rng: rand::rngs::StdRng,
/// The current state of the WFC algorithm (smart pointer for interior mutability)
states: Vec<std::cell::RefCell<State>>,
/// The States that have been collapsed (as positions)
collapsed_states: Vec<Pos>,
// Note: uses map's map size and tile count constants
}
impl Wfc {
/// Creates a new Wfc instance with the given seed and map size
pub fn new(input: &[Vec<Tile>], seed: u64) -> Self {
let rng = rand::SeedableRng::seed_from_u64(seed);
Self {
compatabilities: CompatabilityChecker::new(input),
rng,
states: Vec::with_capacity(TILE_COUNT),
collapsed_states: Vec::new(),
}
}
/// Initializes the states vector with default states
fn initialize_states(&mut self) {
// Store a superposition of all possible tiles for each position
for pos in Pos::values() {
let possible_tiles = Tile::values().collect();
let state = State::new(pos, possible_tiles);
self.states.push(std::cell::RefCell::new(state));
}
}
/// Find the state with the lowest entropy that has not been collapsed yet
fn find_lowest_entropy_state(&self) -> Option<State> {
self.states
.iter()
.filter_map(|state| {
let borrowed_state = state.borrow();
if !borrowed_state.is_collapsed() {
Some(borrowed_state.clone())
} else {
None
}
})
.min_by_key(State::entropy)
}
/// Collapses the given state by selecting one of its possible tiles (using the rng)
fn collapse_state(&mut self, state: &State) {
// Select a random tile from the possible tiles
if let Some(&selected_tile) = state.possible_tiles().choose(&mut self.rng) {
// Update the state to be collapsed with the selected tile
if let Some(state_ref) = self.states.get(state.pos.idx()) {
let mut state_mut = state_ref.borrow_mut();
state_mut.collapse(selected_tile);
self.collapsed_states.push(state.pos);
}
}
}
/// Propagates constraints after a state has been collapsed, starting from the given state
fn propagate(&mut self, state: &State) {
// Keep a queue of states to propagate constraints from
let mut queue = vec![state.pos];
while let Some(current_pos) = queue.pop() {
// For each direction, check neighboring states
for direction in Direction::values() {
if let Some(neighbor_pos) = current_pos.step(direction)
&& let Some(neighbor_state_ref) = self.states.get(neighbor_pos.idx())
{
let mut neighbor_state = neighbor_state_ref.borrow_mut();
let valid_tiles: Vec<Tile> = neighbor_state
.possible_tiles()
.iter()
.cloned()
.filter(|&tile| {
self.compatabilities.is_valid(
state.possible_tiles()[0],
tile,
direction,
)
})
.collect();
if valid_tiles.len() < neighbor_state.possible_tiles().len() {
neighbor_state.set_possible_tiles(valid_tiles);
queue.push(neighbor_pos);
}
}
}
}
}
/// Runs the Wave Function Collapse algorithm to generate the map
#[expect(clippy::unused_self)]
pub(crate) fn run(&mut self) {
// ----- Step 1: Read in input sample and define constraints -----
// (This step is currently handled in the CompatabilityChecker::new method)
// ----- Step 2: Initialize the grid of possible states -----
self.initialize_states();
// Keep running until all states are collapsed
while self.collapsed_states.len() != TILE_COUNT {
// ----- Step 3: Collapse the superpositions -----
// Find the shannon entropy of all superpositions
if let Some(state) = self.find_lowest_entropy_state() {
// Collapse the tile with the lowest entropy
// (Assign a seeded random superposition)
self.collapse_state(&state);
// ----- Step 4: Propagate -----
// Update neighboring superpositions based on the collapsed tile
// (Remove any that violate constraints)
self.propagate(&state);
}
// ----- Step 5: Repeat until all superpositions are collapsed -----
// If any tiles are left with no possible states, restart
// TODO: backtrack
}
}
/// Returns the generated tiles as a boxed array
pub fn to_tiles(&self) -> Box<[Tile; TILE_COUNT]> {
let mut arr = [Tile::Air; TILE_COUNT];
for (i, state_ref) in self.states.iter().enumerate() {
let state = state_ref.borrow();
if state.is_collapsed() {
arr[i] = state.possible_tiles()[0];
}
}
Box::new(arr)
}
}
/// Tests
#[cfg(test)]
mod tests {
use super::*;
use crate::map::MAP_SIZE_USIZE;
#[test]
fn test_wfc_generation() {
// 16x16 input
// 0 is wall, 1 is air
let input = vec![
vec![0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
vec![0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
vec![0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
vec![0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
vec![0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
vec![1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
vec![0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
vec![0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
vec![0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
vec![0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
vec![0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0],
vec![1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
vec![0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
vec![0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0],
vec![0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0],
vec![0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
];
// Convert input to Tiles
let input_tiles: Vec<Vec<Tile>> = input
.iter()
.map(|row| {
row.iter()
.map(|&cell| if cell == 0 { Tile::Wall } else { Tile::Air })
.collect()
})
.collect();
let seed = 12345;
let mut wfc = Wfc::new(&input_tiles, seed);
wfc.run();
let tiles = wfc.to_tiles();
// Print the generated tiles
for y in 0..MAP_SIZE_USIZE {
for x in 0..MAP_SIZE_USIZE {
// Print air as "██", walls as " " and stairs as ">"
let tile = tiles[y * MAP_SIZE_USIZE + x];
let symbol = match tile {
Tile::Air => "██",
Tile::Wall => " ",
Tile::Stairs => ">",
};
print!("{symbol}");
}
println!();
}
// Check that the tiles array has the correct length
assert_eq!(tiles.len(), TILE_COUNT);
// Check that there is at least one Air tile
assert!(tiles.contains(&Tile::Air));
}
}
|