rust/landgen/src/template_based.rs
changeset 14069 abb42ba345b6
parent 14067 3f21f27c6564
child 14078 bf40b5f938b0
equal deleted inserted replaced
14068:859a41b137d4 14069:abb42ba345b6
     1 use itertools::Itertools;
     1 use integral_geometry::{Point, Size};
     2 
       
     3 use integral_geometry::{Point, Rect, Size};
       
     4 use land2d::Land2D;
     2 use land2d::Land2D;
     5 use LandGenerationParameters;
     3 use LandGenerationParameters;
     6 use LandGenerator;
     4 use LandGenerator;
     7 
     5 
     8 struct OutlinePoints {
     6 use outline::OutlinePoints;
     9     islands: Vec<Vec<Point>>,
     7 use outline_template::OutlineTemplate;
    10     fill_points: Vec<Point>,
       
    11     size: Size,
       
    12 }
       
    13 
     8 
    14 impl OutlinePoints {
       
    15     fn from_outline_template<I: Iterator<Item = u32>>(
       
    16         outline_template: &OutlineTemplate,
       
    17         random_numbers: &mut I,
       
    18     ) -> Self {
       
    19         Self {
       
    20             islands: outline_template
       
    21                 .islands
       
    22                 .iter()
       
    23                 .map(|i| {
       
    24                     i.iter()
       
    25                         .zip(random_numbers.tuples())
       
    26                         .map(|(rect, (rnd_a, rnd_b))| {
       
    27                             Point::new(
       
    28                                 rect.x + (rnd_a % rect.width) as i32,
       
    29                                 rect.y + (rnd_b % rect.height) as i32,
       
    30                             )
       
    31                         }).collect()
       
    32                 }).collect(),
       
    33             fill_points: outline_template.fill_points.clone(),
       
    34             size: outline_template.size,
       
    35         }
       
    36     }
       
    37 
       
    38     fn total_len(&self) -> usize {
       
    39         self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len()
       
    40     }
       
    41 
       
    42     fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
       
    43         self.islands
       
    44             .iter_mut()
       
    45             .flat_map(|i| i.iter_mut())
       
    46             .chain(self.fill_points.iter_mut())
       
    47     }
       
    48 
       
    49     fn divide_edge<I: Iterator<Item = u32>>(
       
    50         &self,
       
    51         start_point: Point,
       
    52         end_point: Point,
       
    53         random_numbers: &mut I,
       
    54     ) -> Option<Point> {
       
    55         None
       
    56     }
       
    57 
       
    58     fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
       
    59         for is in 0..self.islands.len() {
       
    60             let mut i = 0;
       
    61             let mut start_point = Point::zero();
       
    62             let mut end_point = Point::zero();
       
    63 
       
    64             loop {
       
    65                 {
       
    66                     let island = &self.islands[is];
       
    67                     if i < island.len() {
       
    68                         start_point = island[i];
       
    69                         end_point = if i + 1 < island.len() {
       
    70                             island[i + 1]
       
    71                         } else {
       
    72                             island[0]
       
    73                         };
       
    74                     } else {
       
    75                         break
       
    76                     }
       
    77                 }
       
    78 
       
    79                 if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) {
       
    80                     self.islands[is].insert(i + 1, new_point);
       
    81                     i += 2;
       
    82                 } else {
       
    83                     i += 1;
       
    84                 }
       
    85             }
       
    86         }
       
    87     }
       
    88 
       
    89     fn bezierize(&mut self) {
       
    90         unimplemented!()
       
    91     }
       
    92 
       
    93     fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
       
    94         loop {
       
    95             let old_len = self.total_len();
       
    96             self.divide_edges(random_numbers);
       
    97 
       
    98             if self.total_len() != old_len {
       
    99                 break;
       
   100             }
       
   101         }
       
   102 
       
   103         self.bezierize();
       
   104     }
       
   105 
       
   106     fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
       
   107         for island in &self.islands {
       
   108             if island.len() > 1 {
       
   109                 for i in 0..island.len() - 1 {
       
   110                     land.draw_line(island[i], island[i + 1], value);
       
   111                 }
       
   112                 land.draw_line(island[island.len() - 1], island[0], value);
       
   113             }
       
   114         }
       
   115     }
       
   116 }
       
   117 
       
   118 pub struct OutlineTemplate {
       
   119     islands: Vec<Vec<Rect>>,
       
   120     fill_points: Vec<Point>,
       
   121     size: Size,
       
   122     can_flip: bool,
       
   123     can_invert: bool,
       
   124     can_mirror: bool,
       
   125     is_negative: bool,
       
   126 }
       
   127 
       
   128 impl OutlineTemplate {
       
   129     pub fn new(size: Size) -> Self {
       
   130         OutlineTemplate {
       
   131             size,
       
   132             islands: Vec::new(),
       
   133             fill_points: Vec::new(),
       
   134             can_flip: false,
       
   135             can_invert: false,
       
   136             can_mirror: false,
       
   137             is_negative: false,
       
   138         }
       
   139     }
       
   140 
       
   141     pub fn flippable(self) -> Self {
       
   142         Self {
       
   143             can_flip: true,
       
   144             ..self
       
   145         }
       
   146     }
       
   147 
       
   148     pub fn mirrorable(self) -> Self {
       
   149         Self {
       
   150             can_mirror: true,
       
   151             ..self
       
   152         }
       
   153     }
       
   154 
       
   155     pub fn invertable(self) -> Self {
       
   156         Self {
       
   157             can_invert: true,
       
   158             ..self
       
   159         }
       
   160     }
       
   161 
       
   162     pub fn negative(self) -> Self {
       
   163         Self {
       
   164             is_negative: true,
       
   165             ..self
       
   166         }
       
   167     }
       
   168 
       
   169     pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self {
       
   170         Self {
       
   171             fill_points,
       
   172             ..self
       
   173         }
       
   174     }
       
   175 
       
   176     pub fn with_islands(self, islands: Vec<Vec<Rect>>) -> Self {
       
   177         Self { islands, ..self }
       
   178     }
       
   179 
       
   180     pub fn add_fill_points(mut self, points: &[Point]) -> Self {
       
   181         self.fill_points.extend_from_slice(points);
       
   182         self
       
   183     }
       
   184 
       
   185     pub fn add_island(mut self, island: &[Rect]) -> Self {
       
   186         self.islands.push(island.into());
       
   187         self
       
   188     }
       
   189 }
       
   190 
     9 
   191 pub struct TemplatedLandGenerator {
    10 pub struct TemplatedLandGenerator {
   192     outline_template: OutlineTemplate,
    11     outline_template: OutlineTemplate,
   193 }
    12 }
   194 
    13