author | unC0Rr |
Wed, 28 Aug 2024 17:16:23 +0200 | |
branch | transitional_engine |
changeset 16023 | 0fd23fc57947 |
parent 15925 | b0e8cc72bfef |
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 Default for Tile { |
|
13 |
fn default() -> Self { |
|
14 |
Tile::Outside |
|
15 |
} |
|
16 |
} |
|
17 |
||
15917 | 18 |
#[derive(Debug)] |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
19 |
pub struct CollapseRule { |
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 |
||
15913 | 52 |
pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>( |
15912 | 53 |
&mut self, |
54 |
map_size: &Size, |
|
55 |
seed_fn: F, |
|
56 |
random_numbers: &mut I, |
|
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 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
82 |
self.grid.get(y, x).copied().unwrap_or_default() |
15912 | 83 |
} |
84 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
85 |
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
|
86 |
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
|
87 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
88 |
// Iterate through the tiles in the land |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
89 |
for x in 0..self.grid.width() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
90 |
for y in 0..self.grid.height() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
91 |
let current_tile = self.get_tile(y, x); |
15912 | 92 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
93 |
if let Tile::Empty = current_tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
94 |
// calc entropy |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
95 |
let right_tile = self.get_tile(y, x + 1); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
96 |
let bottom_tile = self.get_tile(y + 1, x); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
100 |
let possibilities: Vec<Tile> = self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
101 |
.rules |
15912 | 102 |
.iter() |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
103 |
.filter_map(|rule| { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
104 |
if rule.right.contains(&right_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
105 |
&& rule.bottom.contains(&bottom_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
106 |
&& rule.left.contains(&left_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
107 |
&& rule.top.contains(&top_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
108 |
{ |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
109 |
Some(rule.tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
110 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
111 |
None |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
112 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
113 |
}) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
114 |
.collect(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
115 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
116 |
let entropy = possibilities.len(); |
15917 | 117 |
if entropy > 0 { |
118 |
if entropy <= tiles_to_collapse.0 { |
|
119 |
let entry = ( |
|
120 |
y, |
|
121 |
x, |
|
122 |
possibilities |
|
123 |
[random_numbers.next().unwrap_or_default() as usize % entropy], |
|
124 |
); |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
125 |
|
15917 | 126 |
if entropy < tiles_to_collapse.0 { |
127 |
tiles_to_collapse = (entropy, vec![entry]) |
|
128 |
} else { |
|
129 |
tiles_to_collapse.1.push(entry) |
|
130 |
} |
|
15912 | 131 |
} |
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
132 |
} else { |
15919 | 133 |
/*println!("We're here: {}, {}", x, y); |
15917 | 134 |
println!( |
135 |
"Neighbour tiles are: {:?} {:?} {:?} {:?}", |
|
136 |
right_tile, bottom_tile, left_tile, top_tile |
|
137 |
); |
|
15919 | 138 |
println!("Rules are: {:?}", self.rules);*/ |
15917 | 139 |
|
15923
d46ad15c6dec
Get wavefront collapse generator to work in engine
unC0Rr
parents:
15922
diff
changeset
|
140 |
//todo!("no collapse possible - what to do?") |
15912 | 141 |
} |
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
||
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
146 |
let tiles_to_collapse = tiles_to_collapse.1; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
147 |
let possibilities_number = tiles_to_collapse.len(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
148 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
149 |
if possibilities_number > 0 { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
150 |
let (y, x, tile) = tiles_to_collapse |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
151 |
[random_numbers.next().unwrap_or_default() as usize % possibilities_number]; |
15912 | 152 |
|
15916
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
153 |
*self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
154 |
.grid |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
155 |
.get_mut(y, x) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
156 |
.expect("correct iteration over grid") = tile; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
157 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
158 |
true |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
159 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
160 |
false |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15915
diff
changeset
|
161 |
} |
15912 | 162 |
} |
15917 | 163 |
|
164 |
pub fn grid(&self) -> &Vec2D<Tile> { |
|
165 |
&self.grid |
|
166 |
} |
|
15912 | 167 |
} |
168 |
||
169 |
#[cfg(test)] |
|
170 |
mod tests { |
|
15913 | 171 |
use super::{Tile, WavefrontCollapse}; |
15912 | 172 |
use integral_geometry::Size; |
15913 | 173 |
use vec2d::Vec2D; |
15912 | 174 |
|
175 |
#[test] |
|
176 |
fn test_wavefront_collapse() { |
|
177 |
let size = Size::new(4, 4); |
|
15917 | 178 |
let mut rnd = [0u32; 64].into_iter().cycle(); |
15912 | 179 |
let mut wfc = WavefrontCollapse::default(); |
180 |
||
15917 | 181 |
wfc.generate_map(&size, |_| {}, &mut rnd); |
182 |
||
15913 | 183 |
let empty_land = Vec2D::new(&size, Tile::Empty); |
15912 | 184 |
|
15917 | 185 |
assert_eq!(empty_land.as_slice(), wfc.grid().as_slice()); |
15912 | 186 |
} |
187 |
} |