rust/landgen/src/maze.rs
branchtransitional_engine
changeset 16064 0caa3dfb3ba2
parent 16063 09beeec033ba
equal deleted inserted replaced
16063:09beeec033ba 16064:0caa3dfb3ba2
     1 use crate::outline_template_based::outline::OutlinePoints;
     1 use crate::outline_template_based::outline::OutlinePoints;
     2 use crate::{LandGenerationParameters, LandGenerator};
     2 use crate::{LandGenerationParameters, LandGenerator};
     3 use integral_geometry::{Point, Polygon, Rect, Size};
     3 use integral_geometry::{Point, Polygon, Rect, Size};
     4 use land2d::Land2D;
     4 use land2d::Land2D;
     5 
     5 
       
     6 #[derive(Clone)]
     6 pub struct MazeTemplate {
     7 pub struct MazeTemplate {
     7     pub width: usize,
     8     pub width: usize,
     8     pub height: usize,
     9     pub height: usize,
     9     pub cell_size: usize,
    10     pub cell_size: usize,
    10     pub inverted: bool,
    11     pub inverted: bool,
    13 }
    14 }
    14 
    15 
    15 struct Maze {
    16 struct Maze {
    16     inverted: bool,
    17     inverted: bool,
    17     braidness: u32,
    18     braidness: u32,
    18     off_y: i32,
    19     off: Point,
    19     num_cells: Size,
    20     num_cells: Size,
    20     num_edges: Size,
    21     num_edges: Size,
    21     seen_cells: Size,
    22     seen_cells: Size,
    22     cell_size: usize,
    23     cell_size: usize,
    23     seen_list: Vec<Vec<Option<usize>>>,
    24     seen_list: Vec<Vec<Option<usize>>>,
   107                 (x + current_step * seen_cells.width / num_steps) as i32,
   108                 (x + current_step * seen_cells.width / num_steps) as i32,
   108                 random_numbers.next().unwrap_or_default() as i32 % seen_cells.height as i32,
   109                 random_numbers.next().unwrap_or_default() as i32 % seen_cells.height as i32,
   109             );
   110             );
   110         }
   111         }
   111 
   112 
       
   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 
   112         Self {
   116         Self {
   113             inverted,
   117             inverted,
   114             braidness,
   118             braidness,
   115             off_y: ((size.height - num_cells.height * cell_size) / 2) as i32,
   119             off: Point::new(off_x, off_y),
   116             num_cells,
   120             num_cells,
   117             num_edges,
   121             num_edges,
   118             seen_cells,
   122             seen_cells,
   119             cell_size,
   123             cell_size,
   120             seen_list,
   124             seen_list,
   212             } else {
   216             } else {
   213                 self.cell_size * 2 / 3
   217                 self.cell_size * 2 / 3
   214             }
   218             }
   215         });
   219         });
   216         let new_point = Point::new(
   220         let new_point = Point::new(
   217             (p.x - 1) * self.cell_size as i32 + x as i32,
   221             (p.x - 1) * self.cell_size as i32 + x as i32 + self.off.x,
   218             (p.y - 1) * self.cell_size as i32 + y as i32 + self.off_y,
   222             (p.y - 1) * self.cell_size as i32 + y as i32 + self.off.y,
   219         );
   223         );
   220 
   224 
   221         let nv = polygon.len();
   225         let nv = polygon.len();
   222         if nv > 2 {
   226         if nv > 2 {
   223             if in_line(&polygon[nv - 2], &polygon[nv - 1], &new_point) {
   227             if in_line(&polygon[nv - 2], &polygon[nv - 1], &new_point) {
   294                 self.edge_list[1][y][x] = maze[y][x] != maze[y + 1][x];
   298                 self.edge_list[1][y][x] = maze[y][x] != maze[y + 1][x];
   295             }
   299             }
   296         }
   300         }
   297 
   301 
   298         let mut fill_points = vec![];
   302         let mut fill_points = vec![];
   299 
       
   300         for y in 0..self.num_cells.height {
       
   301             for x in 0..self.num_cells.width {
       
   302                 if maze[y][x] {
       
   303                     let half_cell = self.cell_size / 2;
       
   304                     let fill_point = Point::new(
       
   305                         (x * self.cell_size + half_cell) as i32,
       
   306                         (y * self.cell_size + half_cell) as i32 + self.off_y,
       
   307                     );
       
   308                     islands.push(Polygon::new(&[fill_point]));
       
   309                     fill_points.push(fill_point);
       
   310                 }
       
   311             }
       
   312         }
       
   313 
   303 
   314         for x in 0..self.num_edges.width {
   304         for x in 0..self.num_edges.width {
   315             for y in 0..self.num_cells.height {
   305             for y in 0..self.num_cells.height {
   316                 if self.edge_list[0][y][x] {
   306                 if self.edge_list[0][y][x] {
   317                     self.edge_list[0][y][x] = false;
   307                     self.edge_list[0][y][x] = false;
   326                     if polygon.len() > 4 {
   316                     if polygon.len() > 4 {
   327                         if in_line(polygon.last().unwrap(), &polygon[0], &polygon[1]) {
   317                         if in_line(polygon.last().unwrap(), &polygon[0], &polygon[1]) {
   328                             polygon.pop();
   318                             polygon.pop();
   329                         }
   319                         }
   330 
   320 
   331                         /*
   321                         for p in &polygon {
   332                                                 for p in &polygon {
   322                             println!("{} {}", p.x, p.y);
   333                                                     println!("{} {}", p.x, p.y);
   323                         }
   334                                                 }
   324                         println!("\ne\n");
   335                                                 println!("\ne\n");
       
   336                         */
       
   337 
   325 
   338                         islands.push(Polygon::new(&polygon));
   326                         islands.push(Polygon::new(&polygon));
   339                     }
   327                     }
   340                     polygon.clear();
   328                     polygon.clear();
       
   329                 }
       
   330             }
       
   331         }
       
   332 
       
   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                     }
   341                 }
   364                 }
   342             }
   365             }
   343         }
   366         }
   344 
   367 
   345         (islands, fill_points)
   368         (islands, fill_points)
   429             land.play_box(),
   452             land.play_box(),
   430             random_numbers,
   453             random_numbers,
   431         );
   454         );
   432 
   455 
   433         if !parameters.skip_distort {
   456         if !parameters.skip_distort {
   434             points.distort(parameters.distance_divisor, random_numbers);
   457             points.distort(parameters.distance_divisor, self.maze_template.distortion_limiting_factor, random_numbers);
   435         }
   458         }
   436 
   459 
   437         if !parameters.skip_bezier {
   460         if !parameters.skip_bezier {
   438             points.bezierize(5);
   461             points.bezierize(5);
   439         }
   462         }