author | unC0Rr |
Wed, 18 Sep 2024 13:42:26 +0200 | |
branch | transitional_engine |
changeset 16035 | 0caa3dfb3ba2 |
parent 16034 | 09beeec033ba |
permissions | -rw-r--r-- |
16032 | 1 |
use crate::outline_template_based::outline::OutlinePoints; |
2 |
use crate::{LandGenerationParameters, LandGenerator}; |
|
3 |
use integral_geometry::{Point, Polygon, Rect, Size}; |
|
4 |
use land2d::Land2D; |
|
5 |
||
16035 | 6 |
#[derive(Clone)] |
16032 | 7 |
pub struct MazeTemplate { |
16033
1860852892c0
Use rust implementation of maze generator in engine
unC0Rr
parents:
16032
diff
changeset
|
8 |
pub width: usize, |
1860852892c0
Use rust implementation of maze generator in engine
unC0Rr
parents:
16032
diff
changeset
|
9 |
pub height: usize, |
1860852892c0
Use rust implementation of maze generator in engine
unC0Rr
parents:
16032
diff
changeset
|
10 |
pub cell_size: usize, |
1860852892c0
Use rust implementation of maze generator in engine
unC0Rr
parents:
16032
diff
changeset
|
11 |
pub inverted: bool, |
1860852892c0
Use rust implementation of maze generator in engine
unC0Rr
parents:
16032
diff
changeset
|
12 |
pub distortion_limiting_factor: u32, |
16034 | 13 |
pub braidness: u32, |
14 |
} |
|
15 |
||
16 |
struct Maze { |
|
17 |
inverted: bool, |
|
18 |
braidness: u32, |
|
16035 | 19 |
off: Point, |
16034 | 20 |
num_cells: Size, |
21 |
num_edges: Size, |
|
22 |
seen_cells: Size, |
|
23 |
cell_size: usize, |
|
24 |
seen_list: Vec<Vec<Option<usize>>>, |
|
25 |
walls: Vec<Vec<Vec<bool>>>, |
|
26 |
edge_list: Vec<Vec<Vec<bool>>>, |
|
27 |
last_cell: Vec<Point>, |
|
28 |
came_from: Vec<Vec<Point>>, |
|
29 |
came_from_pos: Vec<i32>, |
|
30 |
} |
|
31 |
||
32 |
fn in_line(p1: &Point, p2: &Point, p3: &Point) -> bool { |
|
33 |
p1.x == p2.x && p2.x == p3.x || p1.y == p2.y && p2.y == p3.y |
|
34 |
} |
|
35 |
||
36 |
#[derive(Clone, Copy)] |
|
37 |
struct Direction(Point); |
|
38 |
||
39 |
impl Direction { |
|
40 |
#[inline] |
|
41 |
pub fn new(direction: usize) -> Self { |
|
42 |
match direction % 4 { |
|
43 |
0 => Self(Point::new(0, -1)), |
|
44 |
1 => Self(Point::new(1, 0)), |
|
45 |
2 => Self(Point::new(0, 1)), |
|
46 |
3 => Self(Point::new(-1, 0)), |
|
47 |
_ => panic!("Impossible"), |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
#[inline] |
|
52 |
pub fn rotate_right(self) -> Self { |
|
53 |
Self(self.0.rotate90()) |
|
54 |
} |
|
55 |
||
56 |
#[inline] |
|
57 |
pub fn rotate_left(self) -> Self { |
|
58 |
Self(self.0.rotate270()) |
|
59 |
} |
|
60 |
||
61 |
#[inline] |
|
62 |
pub fn to_edge(self) -> Self { |
|
63 |
Self(Point::new( |
|
64 |
if self.0.x < 0 { 0 } else { self.0.x }, |
|
65 |
if self.0.y < 0 { 0 } else { self.0.y }, |
|
66 |
)) |
|
67 |
} |
|
68 |
||
69 |
#[inline] |
|
70 |
pub fn orientation(&self) -> usize { |
|
71 |
if self.0.x == 0 { |
|
72 |
0 |
|
73 |
} else { |
|
74 |
1 |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
impl Maze { |
|
80 |
pub fn new<I: Iterator<Item = u32>>( |
|
81 |
size: &Size, |
|
82 |
cell_size: usize, |
|
83 |
num_steps: usize, |
|
84 |
inverted: bool, |
|
85 |
braidness: u32, |
|
86 |
random_numbers: &mut I, |
|
87 |
) -> Self { |
|
88 |
let num_cells = Size::new( |
|
89 |
prev_odd(size.width / cell_size), |
|
90 |
prev_odd(size.height / cell_size), |
|
91 |
); |
|
92 |
||
93 |
let num_edges = Size::new(num_cells.width - 1, num_cells.height - 1); |
|
94 |
let seen_cells = Size::new(num_cells.width / 2, num_cells.height / 2); |
|
95 |
||
96 |
let mut last_cell = vec![Point::diag(0); num_steps]; |
|
97 |
let came_from_pos = vec![0; num_steps]; |
|
98 |
let came_from = vec![vec![Point::diag(0); num_steps]; num_cells.area()]; |
|
99 |
||
100 |
let seen_list = vec![vec![None as Option<usize>; seen_cells.width]; seen_cells.height]; |
|
101 |
let walls = vec![vec![vec![true; seen_cells.width]; seen_cells.height]; 2]; |
|
102 |
let edge_list = vec![vec![vec![false; num_cells.width]; num_cells.height]; 2]; |
|
103 |
||
104 |
for current_step in 0..num_steps { |
|
105 |
let x = random_numbers.next().unwrap_or_default() as usize % (seen_cells.width - 1) |
|
106 |
/ num_steps; |
|
107 |
last_cell[current_step] = Point::new( |
|
108 |
(x + current_step * seen_cells.width / num_steps) as i32, |
|
109 |
random_numbers.next().unwrap_or_default() as i32 % seen_cells.height as i32, |
|
110 |
); |
|
111 |
} |
|
112 |
||
16035 | 113 |
let off_x = ((size.width - num_cells.width * cell_size) / 2) as i32; |
114 |
let off_y = ((size.height - num_cells.height * cell_size) / 2) as i32; |
|
115 |
||
16034 | 116 |
Self { |
117 |
inverted, |
|
118 |
braidness, |
|
16035 | 119 |
off: Point::new(off_x, off_y), |
16034 | 120 |
num_cells, |
121 |
num_edges, |
|
122 |
seen_cells, |
|
123 |
cell_size, |
|
124 |
seen_list, |
|
125 |
walls, |
|
126 |
edge_list, |
|
127 |
last_cell, |
|
128 |
came_from, |
|
129 |
came_from_pos, |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
fn see_cell<I: Iterator<Item = u32>>( |
|
134 |
&mut self, |
|
135 |
current_step: usize, |
|
136 |
start_dir: Direction, |
|
137 |
random_numbers: &mut I, |
|
138 |
) -> bool { |
|
139 |
let mut dir = start_dir; |
|
140 |
loop { |
|
141 |
let p = self.last_cell[current_step]; |
|
142 |
self.seen_list[p.y as usize][p.x as usize] = Some(current_step); |
|
143 |
||
144 |
let next_dir_clockwise = random_numbers.next().unwrap_or_default() % 2 == 0; |
|
145 |
||
146 |
for _ in 0..5 { |
|
147 |
let sp = p + dir.0; |
|
148 |
let when_seen = if sp.x < 0 |
|
149 |
|| sp.x >= self.seen_cells.width as i32 |
|
150 |
|| sp.y < 0 |
|
151 |
|| sp.y >= self.seen_cells.height as i32 |
|
152 |
{ |
|
153 |
Some(current_step) |
|
154 |
} else { |
|
155 |
self.seen_list[sp.y as usize][sp.x as usize] |
|
156 |
}; |
|
157 |
||
158 |
match when_seen { |
|
159 |
Some(a) if a == current_step => { |
|
160 |
// try another direction |
|
161 |
if !self.inverted |
|
162 |
&& random_numbers.next().unwrap_or_default() % self.braidness == 0 |
|
163 |
{ |
|
164 |
if dir.0.x == -1 && p.x > 0 { |
|
165 |
self.walls[dir.orientation()][p.y as usize][p.x as usize - 1] = |
|
166 |
false; |
|
167 |
} |
|
168 |
if dir.0.x == 1 && p.x < self.seen_cells.width as i32 - 1 { |
|
169 |
self.walls[dir.orientation()][p.y as usize][p.x as usize] = false; |
|
170 |
} |
|
171 |
if dir.0.y == -1 && p.y > 0 { |
|
172 |
self.walls[dir.orientation()][p.y as usize - 1][p.x as usize] = |
|
173 |
false; |
|
174 |
} |
|
175 |
if dir.0.y == 1 && p.y < self.seen_cells.height as i32 - 1 { |
|
176 |
self.walls[dir.orientation()][p.y as usize][p.x as usize] = false; |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
if next_dir_clockwise { |
|
181 |
dir = dir.rotate_right(); |
|
182 |
} else { |
|
183 |
dir = dir.rotate_left(); |
|
184 |
} |
|
185 |
} |
|
186 |
None => { |
|
187 |
// cell was not seen yet, go there |
|
188 |
let o_dir = dir.rotate_right().rotate_right(); |
|
189 |
let op = p - o_dir.to_edge().0; |
|
190 |
self.walls[o_dir.orientation()][op.y as usize][op.x as usize] = false; |
|
191 |
self.last_cell[current_step] = sp; |
|
192 |
self.came_from_pos[current_step] += 1; |
|
193 |
self.came_from[self.came_from_pos[current_step] as usize][current_step] = p; |
|
194 |
return false; |
|
195 |
} |
|
196 |
_ => { |
|
197 |
return true; |
|
198 |
} |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
self.last_cell[current_step] = |
|
203 |
self.came_from[self.came_from_pos[current_step] as usize][current_step]; |
|
204 |
self.came_from_pos[current_step] -= 1; |
|
205 |
||
206 |
if self.came_from_pos[current_step] < 0 { |
|
207 |
return true; |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
fn add_vertex(&mut self, p: Point, polygon: &mut Vec<Point>) { |
|
213 |
let [x, y] = [p.x, p.y].map(|i| { |
|
214 |
if self.inverted || i & 1 == 0 { |
|
215 |
self.cell_size |
|
216 |
} else { |
|
217 |
self.cell_size * 2 / 3 |
|
218 |
} |
|
219 |
}); |
|
220 |
let new_point = Point::new( |
|
16035 | 221 |
(p.x - 1) * self.cell_size as i32 + x as i32 + self.off.x, |
222 |
(p.y - 1) * self.cell_size as i32 + y as i32 + self.off.y, |
|
16034 | 223 |
); |
224 |
||
225 |
let nv = polygon.len(); |
|
226 |
if nv > 2 { |
|
227 |
if in_line(&polygon[nv - 2], &polygon[nv - 1], &new_point) { |
|
228 |
polygon.pop(); |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
polygon.push(new_point); |
|
233 |
} |
|
234 |
||
235 |
fn add_edge(&mut self, p: Point, mut dir: Direction, polygon: &mut Vec<Point>) { |
|
236 |
let mut next_p = Some(p); |
|
237 |
||
238 |
while let Some(p) = next_p { |
|
239 |
next_p = None; |
|
240 |
||
241 |
for _ in 0..4 { |
|
242 |
let cdir = dir.to_edge(); |
|
243 |
||
244 |
let np = p + cdir.0; |
|
245 |
||
246 |
if np.x >= 0 |
|
247 |
&& np.y >= 0 |
|
248 |
&& np.x < self.num_cells.width as i32 |
|
249 |
&& np.y < self.num_cells.height as i32 |
|
250 |
&& self.edge_list[dir.orientation()][np.y as usize][np.x as usize] |
|
251 |
{ |
|
252 |
self.edge_list[dir.orientation()][np.y as usize][np.x as usize] = false; |
|
253 |
self.add_vertex(p + dir.0 + Point::new(1, 1), polygon); |
|
254 |
next_p = Some(p + dir.0); |
|
255 |
break; |
|
256 |
} |
|
257 |
||
258 |
dir = dir.rotate_right(); |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
pub fn to_islands(mut self) -> (Vec<Polygon>, Vec<Point>) { |
|
264 |
let mut islands: Vec<Polygon> = vec![]; |
|
265 |
let mut polygon: Vec<Point> = vec![]; |
|
266 |
let mut maze = vec![vec![false; self.num_cells.width]; self.num_cells.height]; |
|
267 |
||
268 |
for x in 0..self.seen_cells.width { |
|
269 |
for y in 0..self.seen_cells.height { |
|
270 |
if self.seen_list[y][x].is_some() { |
|
271 |
maze[y * 2 + 1][x * 2 + 1] = true; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
for y in 0..self.seen_cells.height - 1 { |
|
276 |
if !self.walls[0][y][x] { |
|
277 |
maze[y * 2 + 2][x * 2 + 1] = true; |
|
278 |
} |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
for x in 0..self.seen_cells.width - 1 { |
|
283 |
for y in 0..self.seen_cells.height { |
|
284 |
if !self.walls[1][y][x] { |
|
285 |
maze[y * 2 + 1][x * 2 + 2] = true; |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
||
290 |
for x in 0..self.num_edges.width { |
|
291 |
for y in 0..self.num_cells.height { |
|
292 |
self.edge_list[0][y][x] = maze[y][x] != maze[y][x + 1]; |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
for x in 0..self.num_cells.width { |
|
297 |
for y in 0..self.num_edges.height { |
|
298 |
self.edge_list[1][y][x] = maze[y][x] != maze[y + 1][x]; |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
let mut fill_points = vec![]; |
|
303 |
||
304 |
for x in 0..self.num_edges.width { |
|
305 |
for y in 0..self.num_cells.height { |
|
306 |
if self.edge_list[0][y][x] { |
|
307 |
self.edge_list[0][y][x] = false; |
|
308 |
self.add_vertex(Point::new(x as i32 + 1, y as i32 + 1), &mut polygon); |
|
309 |
self.add_vertex(Point::new(x as i32 + 1, y as i32), &mut polygon); |
|
310 |
self.add_edge( |
|
311 |
Point::new(x as i32, y as i32 - 1), |
|
312 |
Direction::new(0), |
|
313 |
&mut polygon, |
|
314 |
); |
|
315 |
||
316 |
if polygon.len() > 4 { |
|
317 |
if in_line(polygon.last().unwrap(), &polygon[0], &polygon[1]) { |
|
318 |
polygon.pop(); |
|
319 |
} |
|
320 |
||
16035 | 321 |
for p in &polygon { |
322 |
println!("{} {}", p.x, p.y); |
|
323 |
} |
|
324 |
println!("\ne\n"); |
|
16034 | 325 |
|
326 |
islands.push(Polygon::new(&polygon)); |
|
327 |
} |
|
328 |
polygon.clear(); |
|
329 |
} |
|
330 |
} |
|
331 |
} |
|
332 |
||
16035 | 333 |
for x in 0..self.num_cells.width { |
334 |
for y in 0..self.num_cells.height { |
|
335 |
if maze[y][x] { |
|
336 |
let half_cell = self.cell_size / 2; |
|
337 |
let fill_point = Point::new( |
|
338 |
(x * self.cell_size + half_cell) as i32 + self.off.x, |
|
339 |
(y * self.cell_size + half_cell) as i32 + self.off.y, |
|
340 |
); |
|
341 |
islands.push(Polygon::new(&[fill_point])); |
|
342 |
fill_points.push(fill_point); |
|
343 |
||
344 |
let mut points = vec![(x, y)]; |
|
345 |
||
346 |
while let Some((x, y)) = points.pop() { |
|
347 |
if maze[y][x] { |
|
348 |
maze[y][x] = false; |
|
349 |
||
350 |
if x > 0 { |
|
351 |
points.push((x - 1, y)); |
|
352 |
} |
|
353 |
if x < self.num_cells.width - 1 { |
|
354 |
points.push((x + 1, y)); |
|
355 |
} |
|
356 |
if y > 0 { |
|
357 |
points.push((x, y - 1)); |
|
358 |
} |
|
359 |
if y < self.num_cells.height - 1 { |
|
360 |
points.push((x, y + 1)); |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
366 |
} |
|
367 |
||
16034 | 368 |
(islands, fill_points) |
369 |
} |
|
16032 | 370 |
} |
371 |
||
372 |
pub struct MazeLandGenerator { |
|
373 |
maze_template: MazeTemplate, |
|
374 |
} |
|
375 |
||
376 |
fn prev_odd(num: usize) -> usize { |
|
377 |
if num & 1 == 0 { |
|
378 |
num - 1 |
|
379 |
} else { |
|
380 |
num |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
impl MazeLandGenerator { |
|
385 |
pub fn new(maze_template: MazeTemplate) -> Self { |
|
386 |
Self { maze_template } |
|
387 |
} |
|
388 |
||
389 |
fn generate_outline<I: Iterator<Item = u32>>( |
|
390 |
&self, |
|
391 |
size: &Size, |
|
392 |
play_box: Rect, |
|
393 |
intersections_box: Rect, |
|
394 |
random_numbers: &mut I, |
|
395 |
) -> OutlinePoints { |
|
396 |
let num_steps = if self.maze_template.inverted { 3 } else { 1 }; |
|
397 |
let mut step_done = vec![false; num_steps]; |
|
398 |
let mut done = false; |
|
399 |
||
16034 | 400 |
let mut maze = Maze::new( |
401 |
&size, |
|
402 |
self.maze_template.cell_size, |
|
403 |
num_steps, |
|
404 |
self.maze_template.inverted, |
|
405 |
self.maze_template.braidness, |
|
406 |
random_numbers, |
|
407 |
); |
|
16032 | 408 |
|
409 |
while !done { |
|
410 |
done = true; |
|
411 |
||
412 |
for current_step in 0..num_steps { |
|
413 |
if !step_done[current_step] { |
|
16034 | 414 |
let dir = Direction::new(random_numbers.next().unwrap_or_default() as usize); |
415 |
step_done[current_step] = maze.see_cell(current_step, dir, random_numbers); |
|
16032 | 416 |
done = false; |
417 |
} |
|
418 |
} |
|
419 |
} |
|
420 |
||
16034 | 421 |
let (islands, fill_points) = maze.to_islands(); |
16032 | 422 |
|
423 |
OutlinePoints { |
|
424 |
islands, |
|
16034 | 425 |
fill_points, |
16032 | 426 |
size: *size, |
427 |
play_box, |
|
428 |
intersections_box, |
|
429 |
} |
|
430 |
} |
|
431 |
} |
|
432 |
||
433 |
impl LandGenerator for MazeLandGenerator { |
|
434 |
fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>( |
|
435 |
&self, |
|
436 |
parameters: &LandGenerationParameters<T>, |
|
437 |
random_numbers: &mut I, |
|
438 |
) -> Land2D<T> { |
|
16034 | 439 |
let do_invert = self.maze_template.inverted; |
16032 | 440 |
let (basic, zero) = if do_invert { |
441 |
(parameters.zero, parameters.basic) |
|
442 |
} else { |
|
443 |
(parameters.basic, parameters.zero) |
|
444 |
}; |
|
445 |
||
446 |
let land_size = Size::new(self.maze_template.width, self.maze_template.height); |
|
447 |
let mut land = Land2D::new(&land_size, basic); |
|
448 |
||
449 |
let mut points = self.generate_outline( |
|
450 |
&land.size().size(), |
|
16034 | 451 |
land.play_box(), //??? Rect::at_origin(land_size).with_margin(land_size.to_square().width as i32 * -2), |
16032 | 452 |
land.play_box(), |
453 |
random_numbers, |
|
454 |
); |
|
455 |
||
456 |
if !parameters.skip_distort { |
|
16035 | 457 |
points.distort(parameters.distance_divisor, self.maze_template.distortion_limiting_factor, random_numbers); |
16032 | 458 |
} |
459 |
||
460 |
if !parameters.skip_bezier { |
|
461 |
points.bezierize(5); |
|
462 |
} |
|
463 |
||
464 |
points.draw(&mut land, zero); |
|
465 |
||
466 |
for p in &points.fill_points { |
|
467 |
land.fill(*p, zero, zero) |
|
468 |
} |
|
469 |
||
470 |
land |
|
471 |
} |
|
472 |
} |