rust/landgen/src/template_based.rs
author alfadur
Fri, 02 Nov 2018 02:31:01 +0300
changeset 14067 3f21f27c6564
parent 14066 649ccb9f8cfd
child 14069 abb42ba345b6
permissions -rw-r--r--
wrestle with borrow checker
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
     1
use itertools::Itertools;
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
     2
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
     3
use integral_geometry::{Point, Rect, Size};
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     4
use land2d::Land2D;
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     5
use LandGenerationParameters;
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     6
use LandGenerator;
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     7
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
     8
struct OutlinePoints {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
     9
    islands: Vec<Vec<Point>>,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    10
    fill_points: Vec<Point>,
14052
alfadur
parents: 14051 14032
diff changeset
    11
    size: Size,
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    12
}
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    13
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    14
impl OutlinePoints {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    15
    fn from_outline_template<I: Iterator<Item = u32>>(
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    16
        outline_template: &OutlineTemplate,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    17
        random_numbers: &mut I,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    18
    ) -> Self {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    19
        Self {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    20
            islands: outline_template
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    21
                .islands
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    22
                .iter()
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    23
                .map(|i| {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    24
                    i.iter()
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    25
                        .zip(random_numbers.tuples())
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    26
                        .map(|(rect, (rnd_a, rnd_b))| {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    27
                            Point::new(
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    28
                                rect.x + (rnd_a % rect.width) as i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    29
                                rect.y + (rnd_b % rect.height) as i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    30
                            )
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    31
                        }).collect()
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    32
                }).collect(),
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    33
            fill_points: outline_template.fill_points.clone(),
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    34
            size: outline_template.size,
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    35
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    36
    }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    37
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    38
    fn total_len(&self) -> usize {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    39
        self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    40
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    41
14052
alfadur
parents: 14051 14032
diff changeset
    42
    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    43
        self.islands
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    44
            .iter_mut()
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    45
            .flat_map(|i| i.iter_mut())
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    46
            .chain(self.fill_points.iter_mut())
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    47
    }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    48
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    49
    fn divide_edge<I: Iterator<Item = u32>>(
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    50
        &self,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    51
        start_point: Point,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    52
        end_point: Point,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    53
        random_numbers: &mut I,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    54
    ) -> Option<Point> {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    55
        None
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    56
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    57
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    58
    fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    59
        for is in 0..self.islands.len() {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    60
            let mut i = 0;
14067
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    61
            let mut start_point = Point::zero();
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    62
            let mut end_point = Point::zero();
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    63
14067
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    64
            loop {
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    65
                {
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    66
                    let island = &self.islands[is];
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    67
                    if i < island.len() {
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    68
                        start_point = island[i];
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    69
                        end_point = if i + 1 < island.len() {
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    70
                            island[i + 1]
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    71
                        } else {
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    72
                            island[0]
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    73
                        };
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    74
                    } else {
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    75
                        break
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    76
                    }
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    77
                }
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    78
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    79
                if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) {
14067
3f21f27c6564 wrestle with borrow checker
alfadur
parents: 14066
diff changeset
    80
                    self.islands[is].insert(i + 1, new_point);
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    81
                    i += 2;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    82
                } else {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    83
                    i += 1;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    84
                }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    85
            }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    86
        }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    87
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    88
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    89
    fn bezierize(&mut self) {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    90
        unimplemented!()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    91
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    92
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    93
    fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    94
        loop {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    95
            let old_len = self.total_len();
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    96
            self.divide_edges(random_numbers);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    97
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    98
            if self.total_len() != old_len {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    99
                break;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   100
            }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   101
        }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   102
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   103
        self.bezierize();
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   104
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   105
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   106
    fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   107
        for island in &self.islands {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   108
            if island.len() > 1 {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   109
                for i in 0..island.len() - 1 {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   110
                    land.draw_line(island[i], island[i + 1], value);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   111
                }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   112
                land.draw_line(island[island.len() - 1], island[0], value);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   113
            }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   114
        }
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   115
    }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   116
}
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   117
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   118
pub struct OutlineTemplate {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   119
    islands: Vec<Vec<Rect>>,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   120
    fill_points: Vec<Point>,
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14026
diff changeset
   121
    size: Size,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   122
    can_flip: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   123
    can_invert: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   124
    can_mirror: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   125
    is_negative: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   126
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   127
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   128
impl OutlineTemplate {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   129
    pub fn new(size: Size) -> Self {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   130
        OutlineTemplate {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   131
            size,
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   132
            islands: Vec::new(),
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   133
            fill_points: Vec::new(),
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   134
            can_flip: false,
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   135
            can_invert: false,
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   136
            can_mirror: false,
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   137
            is_negative: false,
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   138
        }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   139
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   140
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   141
    pub fn flippable(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   142
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   143
            can_flip: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   144
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   145
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   146
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   147
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   148
    pub fn mirrorable(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   149
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   150
            can_mirror: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   151
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   152
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   153
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   154
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   155
    pub fn invertable(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   156
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   157
            can_invert: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   158
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   159
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   160
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   161
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   162
    pub fn negative(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   163
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   164
            is_negative: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   165
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   166
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   167
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   168
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   169
    pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   170
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   171
            fill_points,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   172
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   173
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   174
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   175
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   176
    pub fn with_islands(self, islands: Vec<Vec<Rect>>) -> Self {
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   177
        Self { islands, ..self }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   178
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   179
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   180
    pub fn add_fill_points(mut self, points: &[Point]) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   181
        self.fill_points.extend_from_slice(points);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   182
        self
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   183
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   184
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   185
    pub fn add_island(mut self, island: &[Rect]) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   186
        self.islands.push(island.into());
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   187
        self
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   188
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   189
}
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   190
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   191
pub struct TemplatedLandGenerator {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   192
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   193
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   194
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   195
impl TemplatedLandGenerator {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   196
    pub fn new(outline_template: OutlineTemplate) -> Self {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   197
        Self { outline_template }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   198
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   199
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   200
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   201
impl LandGenerator for TemplatedLandGenerator {
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   202
    fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   203
        &self,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   204
        parameters: LandGenerationParameters<T>,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   205
        random_numbers: &mut I,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   206
    ) -> Land2D<T> {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   207
        let mut points =
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   208
            OutlinePoints::from_outline_template(&self.outline_template, random_numbers);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   209
14052
alfadur
parents: 14051 14032
diff changeset
   210
        let mut land = Land2D::new(points.size, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   211
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   212
        let top_left = Point::new(
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   213
            (land.width() - land.play_width() / 2) as i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   214
            (land.height() - land.play_height()) as i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   215
        );
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   216
14052
alfadur
parents: 14051 14032
diff changeset
   217
        points.size = land.size();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   218
14052
alfadur
parents: 14051 14032
diff changeset
   219
        points.iter_mut().for_each(|p| *p += top_left);
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   220
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   221
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   222
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   223
            if let Some(b) = random_numbers.next() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   224
                if b & 1 != 0 {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   225
                    points
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   226
                        .iter_mut()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   227
                        .for_each(|p| p.x = land.width() as i32 - 1 - p.x);
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   228
                }
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   229
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   230
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   231
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   232
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   233
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   234
            if let Some(b) = random_numbers.next() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   235
                if b & 1 != 0 {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   236
                    points
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   237
                        .iter_mut()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   238
                        .for_each(|p| p.y = land.height() as i32 - 1 - p.y);
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   239
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   240
            }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   241
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   242
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   243
        points.distort(random_numbers);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   244
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   245
        points.draw(&mut land, parameters.zero);
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   246
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   247
        for p in &points.fill_points {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   248
            land.fill(*p, parameters.zero, parameters.zero)
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   249
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   250
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   251
        points.draw(&mut land, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   252
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   253
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   254
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   255
}
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   256
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   257
#[test()]
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   258
fn points_test() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   259
    let mut points = OutlinePoints {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   260
        islands: vec![vec![]],
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   261
        fill_points: vec![Point::new(1, 1)],
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   262
        size: Size::square(100),
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   263
    };
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   264
14052
alfadur
parents: 14051 14032
diff changeset
   265
    points.iter_mut().for_each(|p| p.x = 2);
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   266
    assert_eq!(points.fill_points[0].x, 2);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   267
}