rust/landgen/src/template_based.rs
author unc0rr
Fri, 02 Nov 2018 00:09:05 +0100
changeset 14066 649ccb9f8cfd
parent 14054 3185fb34f3b5
child 14067 3f21f27c6564
permissions -rw-r--r--
Commit broken code for divide_edges in hope for salvation
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 island = &mut self.islands[is];
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    61
            let mut i = 0;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    62
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    63
            while i < island.len() {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    64
                let start_point = island[i];
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    65
                let end_point = if i + 1 < island.len() {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    66
                    island[i + 1]
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    67
                } else {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    68
                    island[0]
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    69
                };
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    70
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    71
                if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    72
                    (*island).insert(i + 1, new_point);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    73
                    i += 2;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    74
                } else {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    75
                    i += 1;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    76
                }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    77
            }
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
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    80
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    81
    fn bezierize(&mut self) {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    82
        unimplemented!()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    83
    }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    84
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    85
    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
    86
        loop {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    87
            let old_len = self.total_len();
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    88
            self.divide_edges(random_numbers);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    89
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    90
            if self.total_len() != old_len {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    91
                break;
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    92
            }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    93
        }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    94
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    95
        self.bezierize();
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    96
    }
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
    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
    99
        for island in &self.islands {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   100
            if island.len() > 1 {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   101
                for i in 0..island.len() - 1 {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   102
                    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
   103
                }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   104
                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
   105
            }
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   106
        }
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   107
    }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   108
}
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   109
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   110
pub struct OutlineTemplate {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   111
    islands: Vec<Vec<Rect>>,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   112
    fill_points: Vec<Point>,
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14026
diff changeset
   113
    size: Size,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   114
    can_flip: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   115
    can_invert: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   116
    can_mirror: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   117
    is_negative: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   118
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   119
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   120
impl OutlineTemplate {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   121
    pub fn new(size: Size) -> Self {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   122
        OutlineTemplate {
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   123
            size,
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   124
            islands: Vec::new(),
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   125
            fill_points: Vec::new(),
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   126
            can_flip: false,
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   127
            can_invert: false,
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   128
            can_mirror: false,
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   129
            is_negative: false,
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   130
        }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   131
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   132
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   133
    pub fn flippable(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   134
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   135
            can_flip: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   136
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   137
        }
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
    pub fn mirrorable(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   141
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   142
            can_mirror: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   143
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   144
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   145
    }
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
    pub fn invertable(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   148
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   149
            can_invert: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   150
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   151
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   152
    }
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
    pub fn negative(self) -> Self {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   155
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   156
            is_negative: true,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   157
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   158
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   159
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   160
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   161
    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
   162
        Self {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   163
            fill_points,
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   164
            ..self
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   165
        }
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   166
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   167
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   168
    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
   169
        Self { islands, ..self }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   170
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   171
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   172
    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
   173
        self.fill_points.extend_from_slice(points);
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   174
        self
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   175
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   176
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   177
    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
   178
        self.islands.push(island.into());
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   179
        self
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   180
    }
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   181
}
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   182
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
   183
pub struct TemplatedLandGenerator {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   184
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   185
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   186
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   187
impl TemplatedLandGenerator {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   188
    pub fn new(outline_template: OutlineTemplate) -> Self {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   189
        Self { outline_template }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   190
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   191
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   192
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   193
impl LandGenerator for TemplatedLandGenerator {
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   194
    fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   195
        &self,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   196
        parameters: LandGenerationParameters<T>,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   197
        random_numbers: &mut I,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   198
    ) -> Land2D<T> {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   199
        let mut points =
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   200
            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
   201
14052
alfadur
parents: 14051 14032
diff changeset
   202
        let mut land = Land2D::new(points.size, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   203
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   204
        let top_left = Point::new(
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   205
            (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
   206
            (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
   207
        );
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   208
14052
alfadur
parents: 14051 14032
diff changeset
   209
        points.size = land.size();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   210
14052
alfadur
parents: 14051 14032
diff changeset
   211
        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
   212
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   213
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   214
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   215
            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
   216
                if b & 1 != 0 {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   217
                    points
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   218
                        .iter_mut()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   219
                        .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
   220
                }
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   221
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   222
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   223
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   224
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   225
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   226
            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
   227
                if b & 1 != 0 {
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   228
                    points
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   229
                        .iter_mut()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   230
                        .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
   231
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   232
            }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   233
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   234
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   235
        points.distort(random_numbers);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   236
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   237
        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
   238
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   239
        for p in &points.fill_points {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   240
            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
   241
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   242
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   243
        points.draw(&mut land, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   244
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   245
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   246
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   247
}
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   248
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   249
#[test()]
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   250
fn points_test() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   251
    let mut points = OutlinePoints {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   252
        islands: vec![vec![]],
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   253
        fill_points: vec![Point::new(1, 1)],
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
   254
        size: Size::square(100),
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   255
    };
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   256
14052
alfadur
parents: 14051 14032
diff changeset
   257
    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
   258
    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
   259
}