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