rust/landgen/src/template_based.rs
changeset 14057 9c817b2eedae
parent 14056 8a0d69c16cad
parent 14037 2869c2ccb1b8
child 14059 3185fb34f3b5
equal deleted inserted replaced
14056:8a0d69c16cad 14057:9c817b2eedae
     1 use itertools::Itertools;
     1 use itertools::Itertools;
     2 
     2 
     3 use integral_geometry::Point;
     3 use integral_geometry::{Point, Size, Rect};
     4 use integral_geometry::Rect;
       
     5 use land2d::Land2D;
     4 use land2d::Land2D;
     6 use LandGenerationParameters;
     5 use LandGenerationParameters;
     7 use LandGenerator;
     6 use LandGenerator;
     8 
     7 
     9 struct OutlinePoints {
     8 struct OutlinePoints {
    10     islands: Vec<Vec<Point>>,
     9     islands: Vec<Vec<Point>>,
    11     fill_points: Vec<Point>,
    10     fill_points: Vec<Point>,
    12     width: usize,
    11     size: Size,
    13     height: usize,
       
    14 }
    12 }
    15 
    13 
    16 impl OutlinePoints {
    14 impl OutlinePoints {
    17     fn from_outline_template<I: Iterator<Item = u32>>(
    15     fn from_outline_template<I: Iterator<Item = u32>>(
    18         outline_template: &OutlineTemplate,
    16         outline_template: &OutlineTemplate,
    31                                 rect.y + (rnd_b % rect.height) as i32,
    29                                 rect.y + (rnd_b % rect.height) as i32,
    32                             )
    30                             )
    33                         }).collect()
    31                         }).collect()
    34                 }).collect(),
    32                 }).collect(),
    35             fill_points: outline_template.fill_points.clone(),
    33             fill_points: outline_template.fill_points.clone(),
    36             width: outline_template.width,
    34             size: outline_template.size
    37             height: outline_template.height,
       
    38         }
    35         }
    39     }
    36     }
    40 
    37 
    41     fn for_each<F: Fn(&mut Point)>(&mut self, f: F) {
    38     fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
    42         self.islands
    39         self.islands.iter_mut()
    43             .iter_mut()
       
    44             .flat_map(|i| i.iter_mut())
    40             .flat_map(|i| i.iter_mut())
    45             .chain(self.fill_points.iter_mut())
    41             .chain(self.fill_points.iter_mut())
    46             .into_iter()
       
    47             .for_each(f);
       
    48     }
    42     }
    49 
    43 
    50     fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
    44     fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
    51         unimplemented!()
    45         unimplemented!()
    52     }
    46     }
    53 }
    47 }
    54 
    48 
    55 struct OutlineTemplate {
    49 struct OutlineTemplate {
    56     islands: Vec<Vec<Rect>>,
    50     islands: Vec<Vec<Rect>>,
    57     fill_points: Vec<Point>,
    51     fill_points: Vec<Point>,
    58     width: usize,
    52     size: Size,
    59     height: usize,
       
    60     can_flip: bool,
    53     can_flip: bool,
    61     can_invert: bool,
    54     can_invert: bool,
    62     can_mirror: bool,
    55     can_mirror: bool,
    63     is_negative: bool,
    56     is_negative: bool,
    64 }
    57 }
    80         random_numbers: &mut I,
    73         random_numbers: &mut I,
    81     ) -> Land2D<T> {
    74     ) -> Land2D<T> {
    82         let mut points =
    75         let mut points =
    83             OutlinePoints::from_outline_template(&self.outline_template, random_numbers);
    76             OutlinePoints::from_outline_template(&self.outline_template, random_numbers);
    84 
    77 
    85         let mut land = Land2D::new(points.width, points.height, parameters.basic);
    78         let mut land = Land2D::new(points.size, parameters.basic);
    86 
    79 
    87         let top_left = Point::new(
    80         let top_left = Point::new(
    88             (land.width() - land.play_width() / 2) as i32,
    81             (land.width() - land.play_width() / 2) as i32,
    89             (land.height() - land.play_height()) as i32,
    82             (land.height() - land.play_height()) as i32,
    90         );
    83         );
    91 
    84 
    92         points.width = land.width();
    85         points.size = land.size();
    93         points.height = land.height();
       
    94 
    86 
    95         points.for_each(|p| *p += top_left);
    87         points.iter_mut().for_each(|p| *p += top_left);
    96 
    88 
    97         // mirror
    89         // mirror
    98         if self.outline_template.can_mirror {
    90         if self.outline_template.can_mirror {
    99             if let Some(b) = random_numbers.next() {
    91             if let Some(b) = random_numbers.next() {
   100                 if b & 1 != 0 {
    92                 if b & 1 != 0 {
   101                     points.for_each(|p| p.x = land.width() as i32 - 1 - p.x);
    93                     points.iter_mut().for_each(|p| p.x = land.width() as i32 - 1 - p.x);
   102                 }
    94                 }
   103             }
    95             }
   104         }
    96         }
   105 
    97 
   106         // flip
    98         // flip
   107         if self.outline_template.can_flip {
    99         if self.outline_template.can_flip {
   108             if let Some(b) = random_numbers.next() {
   100             if let Some(b) = random_numbers.next() {
   109                 if b & 1 != 0 {
   101                 if b & 1 != 0 {
   110                     points.for_each(|p| p.y = land.height() as i32 - 1 - p.y);
   102                     points.iter_mut().for_each(|p|
       
   103                         p.y = land.height() as i32 - 1 - p.y);
   111                 }
   104                 }
   112             }
   105             }
   113         }
   106         }
   114 
   107 
   115         points.distort(random_numbers);
   108         points.distort(random_numbers);
   129 #[test()]
   122 #[test()]
   130 fn points_test() {
   123 fn points_test() {
   131     let mut points = OutlinePoints {
   124     let mut points = OutlinePoints {
   132         islands: vec![vec![]],
   125         islands: vec![vec![]],
   133         fill_points: vec![Point::new(1, 1)],
   126         fill_points: vec![Point::new(1, 1)],
   134         width: 100,
   127         size: Size::square(100)
   135         height: 100,
       
   136     };
   128     };
   137 
   129 
   138     points.for_each(|p| p.x = 2);
   130     points.iter_mut().for_each(|p| p.x = 2);
   139     assert_eq!(points.fill_points[0].x, 2);
   131     assert_eq!(points.fill_points[0].x, 2);
   140 }
   132 }