author | unC0Rr |
Fri, 03 Feb 2023 15:59:18 +0100 | |
branch | transitional_engine |
changeset 15947 | 60b5639cc3a5 |
parent 15946 | e82de0410da5 |
child 15948 | 9bd828451d77 |
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 Tile { |
|
15947 | 13 |
fn is(&self, i: usize) -> bool { |
15942 | 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 |
||
15947 | 25 |
fn is_empty_or(&self, i: usize) -> bool { |
15942 | 26 |
match self { |
27 |
Tile::Numbered(n) => *n == i, |
|
28 |
Tile::Empty => true, |
|
29 |
_ => false, |
|
30 |
} |
|
31 |
} |
|
32 |
||
15947 | 33 |
fn is_void_or(&self, i: usize) -> bool { |
15942 | 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 |
||
15947 | 47 |
#[derive(Debug)] |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
48 |
pub struct CollapseRule { |
15947 | 49 |
pub tile: Tile, |
50 |
pub right: HashSet<Tile>, |
|
51 |
pub bottom: HashSet<Tile>, |
|
52 |
pub left: HashSet<Tile>, |
|
53 |
pub top: HashSet<Tile>, |
|
15942 | 54 |
} |
55 |
||
15943 | 56 |
pub struct WavefrontCollapse { |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
57 |
rules: Vec<CollapseRule>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
58 |
grid: Vec2D<Tile>, |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
59 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
60 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
61 |
impl Default for WavefrontCollapse { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
62 |
fn default() -> Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
63 |
Self { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
64 |
rules: Vec::new(), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
65 |
grid: Vec2D::new(&Size::new(1, 1), Tile::Empty), |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
66 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
67 |
} |
15942 | 68 |
} |
69 |
||
70 |
impl WavefrontCollapse { |
|
15943 | 71 |
pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>( |
15942 | 72 |
&mut self, |
73 |
map_size: &Size, |
|
74 |
seed_fn: F, |
|
75 |
random_numbers: &mut I, |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
76 |
) { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
77 |
self.grid = Vec2D::new(&map_size, Tile::Empty); |
15942 | 78 |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
79 |
seed_fn(&mut self.grid); |
15942 | 80 |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
81 |
while self.collapse_step(random_numbers) {} |
15942 | 82 |
} |
83 |
||
15947 | 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) { |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
89 |
self.rules.push(rule); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
90 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
91 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
92 |
fn get_tile(&self, y: usize, x: usize) -> Tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
93 |
self.grid.get(y, x).map(|p| *p).unwrap_or_default() |
15942 | 94 |
} |
95 |
||
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
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:
15945
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:
15945
diff
changeset
|
98 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
99 |
// Iterate through the tiles in the land |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
100 |
for x in 0..self.grid.width() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
101 |
for y in 0..self.grid.height() { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
102 |
let current_tile = self.get_tile(y, x); |
15942 | 103 |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
104 |
if let Tile::Empty = current_tile { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
105 |
// calc entropy |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
106 |
let right_tile = self.get_tile(y, x + 1); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
107 |
let bottom_tile = self.get_tile(y + 1, x); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
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:
15945
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:
15945
diff
changeset
|
110 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
111 |
let possibilities: Vec<Tile> = self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
112 |
.rules |
15942 | 113 |
.iter() |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
114 |
.filter_map(|rule| { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
115 |
if rule.right.contains(&right_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
116 |
&& rule.bottom.contains(&bottom_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
117 |
&& rule.left.contains(&left_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
118 |
&& rule.top.contains(&top_tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
119 |
{ |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
120 |
Some(rule.tile) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
121 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
122 |
None |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
123 |
} |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
124 |
}) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
125 |
.collect(); |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
126 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
127 |
let entropy = possibilities.len(); |
15947 | 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 |
); |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
136 |
|
15947 | 137 |
if entropy < tiles_to_collapse.0 { |
138 |
tiles_to_collapse = (entropy, vec![entry]) |
|
139 |
} else { |
|
140 |
tiles_to_collapse.1.push(entry) |
|
141 |
} |
|
15942 | 142 |
} |
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
143 |
} else { |
15947 | 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?") |
|
15942 | 152 |
} |
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
||
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
157 |
let tiles_to_collapse = tiles_to_collapse.1; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
158 |
let possibilities_number = tiles_to_collapse.len(); |
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 |
if possibilities_number > 0 { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
161 |
let (y, x, tile) = tiles_to_collapse |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
162 |
[random_numbers.next().unwrap_or_default() as usize % possibilities_number]; |
15942 | 163 |
|
15946
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
164 |
*self |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
165 |
.grid |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
166 |
.get_mut(y, x) |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
167 |
.expect("correct iteration over grid") = tile; |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
168 |
|
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
169 |
true |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
170 |
} else { |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
171 |
false |
e82de0410da5
Rework how rules are defined, add transformations for tiles
unC0Rr
parents:
15945
diff
changeset
|
172 |
} |
15942 | 173 |
} |
15947 | 174 |
|
175 |
pub fn grid(&self) -> &Vec2D<Tile> { |
|
176 |
&self.grid |
|
177 |
} |
|
15942 | 178 |
} |
179 |
||
180 |
#[cfg(test)] |
|
181 |
mod tests { |
|
15943 | 182 |
use super::{Tile, WavefrontCollapse}; |
15942 | 183 |
use integral_geometry::Size; |
15943 | 184 |
use vec2d::Vec2D; |
15942 | 185 |
|
186 |
#[test] |
|
187 |
fn test_wavefront_collapse() { |
|
188 |
let size = Size::new(4, 4); |
|
15947 | 189 |
let mut rnd = [0u32; 64].into_iter().cycle(); |
15942 | 190 |
let mut wfc = WavefrontCollapse::default(); |
191 |
||
15947 | 192 |
wfc.generate_map(&size, |_| {}, &mut rnd); |
193 |
||
15943 | 194 |
let empty_land = Vec2D::new(&size, Tile::Empty); |
15942 | 195 |
|
15947 | 196 |
assert_eq!(empty_land.as_slice(), wfc.grid().as_slice()); |
15942 | 197 |
} |
198 |
} |