author | unC0Rr |
Fri, 03 Feb 2023 15:59:18 +0100 | |
branch | transitional_engine |
changeset 15917 | 60b5639cc3a5 |
parent 15916 | e82de0410da5 |
child 15918 | 9bd828451d77 |
permissions | -rw-r--r-- |
15912 | 1 |
use integral_geometry::Size; |
15917 | 2 |
use std::collections::HashSet; |
15913 | 3 |
use vec2d::Vec2D; |
15912 | 4 |
|
5 |
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] |
|
15913 | 6 |
pub enum Tile { |
15912 | 7 |
Empty, |
8 |
Outside, |
|
15917 | 9 |
Numbered(usize), |
15912 | 10 |
} |
11 |
||
12 |
impl Tile { |
|
15917 | 13 |
fn is(&self, i: usize) -> bool { |
15912 | 14 |
*self == Tile::Numbered(i) |
15 |
} |
|
16 |
||
17 |
fn is_empty(&self) -> bool { |
|
18 |
match self { |
|
19 |
Tile::Empty => true, |
|
20 |
Tile::Outside => true, |
|
21 |
_ => false, |
|
22 |
} |
|
23 |
} |
|
24 |
||
15917 | 25 |
fn is_empty_or(&self, i: usize) -> bool { |
15912 | 26 |
match self { |
27 |
Tile::Numbered(n) => *n == i, |
|
28 |
Tile::Empty => true, |
|
29 |
_ => false, |
|
30 |
} |
|
31 |
} |
|
32 |
||
15917 | 33 |
fn is_void_or(&self, i: usize) -> bool { |
15912 | 34 |
match self { |
35 |
Tile::Numbered(n) => *n == i, |
|
36 |
_ => true, |
|
37 |
} |
|
38 |
} |
|
39 |
} |
|
40 |
||
41 |
impl Default for Tile { |
|
42 |
fn default() -> Self { |
|
43 |
Tile::Outside |
|
44 |
} |
|
45 |
} |
|
46 |
||
15917 | 47 |
#[derive(Debug)] |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
48 |
pub struct CollapseRule { |
15917 | 49 |
pub tile: Tile, |
50 |
pub right: HashSet<Tile>, |
|
51 |
pub bottom: HashSet<Tile>, |
|
52 |
pub left: HashSet<Tile>, |
|
53 |
pub top: HashSet<Tile>, |
|
15912 | 54 |
} |
55 |
||
15913 | 56 |
pub struct WavefrontCollapse { |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
57 |
rules: Vec<CollapseRule>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
58 |
grid: Vec2D<Tile>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
59 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
60 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
61 |
impl Default for WavefrontCollapse { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
62 |
fn default() -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
63 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
64 |
rules: Vec::new(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
65 |
grid: Vec2D::new(&Size::new(1, 1), Tile::Empty), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
66 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
67 |
} |
15912 | 68 |
} |
69 |
||
70 |
impl WavefrontCollapse { |
|
15913 | 71 |
pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>( |
15912 | 72 |
&mut self, |
73 |
map_size: &Size, |
|
74 |
seed_fn: F, |
|
75 |
random_numbers: &mut I, |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
76 |
) { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
77 |
self.grid = Vec2D::new(&map_size, Tile::Empty); |
15912 | 78 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
79 |
seed_fn(&mut self.grid); |
15912 | 80 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
81 |
while self.collapse_step(random_numbers) {} |
15912 | 82 |
} |
83 |
||
15917 | 84 |
pub fn set_rules(&mut self, rules: Vec<CollapseRule>) { |
85 |
self.rules = rules; |
|
86 |
} |
|
87 |
||
88 |
pub fn add_rule(&mut self, rule: CollapseRule) { |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
89 |
self.rules.push(rule); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
90 |
} |
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 |
fn get_tile(&self, y: usize, x: usize) -> Tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
93 |
self.grid.get(y, x).map(|p| *p).unwrap_or_default() |
15912 | 94 |
} |
95 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
96 |
fn collapse_step<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) -> bool { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
97 |
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
|
98 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
99 |
// Iterate through the tiles in the land |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
100 |
for x in 0..self.grid.width() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
101 |
for y in 0..self.grid.height() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
102 |
let current_tile = self.get_tile(y, x); |
15912 | 103 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
104 |
if let Tile::Empty = current_tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
105 |
// calc entropy |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
106 |
let right_tile = self.get_tile(y, x + 1); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
107 |
let bottom_tile = self.get_tile(y + 1, x); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
111 |
let possibilities: Vec<Tile> = self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
112 |
.rules |
15912 | 113 |
.iter() |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
114 |
.filter_map(|rule| { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
115 |
if rule.right.contains(&right_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
116 |
&& rule.bottom.contains(&bottom_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
117 |
&& rule.left.contains(&left_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
118 |
&& rule.top.contains(&top_tile) |
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 |
Some(rule.tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
121 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
122 |
None |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
123 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
124 |
}) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
125 |
.collect(); |
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 |
let entropy = possibilities.len(); |
15917 | 128 |
if entropy > 0 { |
129 |
if entropy <= tiles_to_collapse.0 { |
|
130 |
let entry = ( |
|
131 |
y, |
|
132 |
x, |
|
133 |
possibilities |
|
134 |
[random_numbers.next().unwrap_or_default() as usize % entropy], |
|
135 |
); |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
136 |
|
15917 | 137 |
if entropy < tiles_to_collapse.0 { |
138 |
tiles_to_collapse = (entropy, vec![entry]) |
|
139 |
} else { |
|
140 |
tiles_to_collapse.1.push(entry) |
|
141 |
} |
|
15912 | 142 |
} |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
143 |
} else { |
15917 | 144 |
println!("We're here: {}, {}", x, y); |
145 |
println!( |
|
146 |
"Neighbour tiles are: {:?} {:?} {:?} {:?}", |
|
147 |
right_tile, bottom_tile, left_tile, top_tile |
|
148 |
); |
|
149 |
println!("Rules are: {:?}", self.rules); |
|
150 |
||
151 |
todo!("no collapse possible - what to do?") |
|
15912 | 152 |
} |
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
157 |
let tiles_to_collapse = tiles_to_collapse.1; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
158 |
let possibilities_number = tiles_to_collapse.len(); |
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 |
if possibilities_number > 0 { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
161 |
let (y, x, tile) = tiles_to_collapse |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
162 |
[random_numbers.next().unwrap_or_default() as usize % possibilities_number]; |
15912 | 163 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
164 |
*self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
165 |
.grid |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
166 |
.get_mut(y, x) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
167 |
.expect("correct iteration over grid") = tile; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
168 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
169 |
true |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
170 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
171 |
false |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
172 |
} |
15912 | 173 |
} |
15917 | 174 |
|
175 |
pub fn grid(&self) -> &Vec2D<Tile> { |
|
176 |
&self.grid |
|
177 |
} |
|
15912 | 178 |
} |
179 |
||
180 |
#[cfg(test)] |
|
181 |
mod tests { |
|
15913 | 182 |
use super::{Tile, WavefrontCollapse}; |
15912 | 183 |
use integral_geometry::Size; |
15913 | 184 |
use vec2d::Vec2D; |
15912 | 185 |
|
186 |
#[test] |
|
187 |
fn test_wavefront_collapse() { |
|
188 |
let size = Size::new(4, 4); |
|
15917 | 189 |
let mut rnd = [0u32; 64].into_iter().cycle(); |
15912 | 190 |
let mut wfc = WavefrontCollapse::default(); |
191 |
||
15917 | 192 |
wfc.generate_map(&size, |_| {}, &mut rnd); |
193 |
||
15913 | 194 |
let empty_land = Vec2D::new(&size, Tile::Empty); |
15912 | 195 |
|
15917 | 196 |
assert_eq!(empty_land.as_slice(), wfc.grid().as_slice()); |
15912 | 197 |
} |
198 |
} |