rust/landgen/src/template_based.rs
author alfadur
Thu, 01 Nov 2018 02:55:22 +0300
changeset 14052 9c817b2eedae
parent 14051 8a0d69c16cad
parent 14032 2869c2ccb1b8
child 14054 3185fb34f3b5
permissions -rw-r--r--
merge
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
14052
alfadur
parents: 14051 14032
diff changeset
     3
use integral_geometry::{Point, Size, Rect};
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(),
14052
alfadur
parents: 14051 14032
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
14052
alfadur
parents: 14051 14032
diff changeset
    38
    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
alfadur
parents: 14051 14032
diff changeset
    39
        self.islands.iter_mut()
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    40
            .flat_map(|i| i.iter_mut())
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    41
            .chain(self.fill_points.iter_mut())
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    42
    }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    43
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    44
    fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    45
        unimplemented!()
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    46
    }
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
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    49
struct OutlineTemplate {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    50
    islands: Vec<Vec<Rect>>,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    51
    fill_points: Vec<Point>,
14032
2869c2ccb1b8 extract size struct for common usage
alfadur
parents: 14026
diff changeset
    52
    size: Size,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    53
    can_flip: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    54
    can_invert: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    55
    can_mirror: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    56
    is_negative: bool,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    57
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    58
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    59
struct TemplatedLandGenerator {
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    60
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    61
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    62
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    63
impl TemplatedLandGenerator {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    64
    pub fn new(outline_template: OutlineTemplate) -> Self {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    65
        Self { outline_template }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    66
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    67
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    68
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    69
impl LandGenerator for TemplatedLandGenerator {
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    70
    fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    71
        &self,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    72
        parameters: LandGenerationParameters<T>,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    73
        random_numbers: &mut I,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    74
    ) -> Land2D<T> {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    75
        let mut points =
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    76
            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
    77
14052
alfadur
parents: 14051 14032
diff changeset
    78
        let mut land = Land2D::new(points.size, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    79
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    80
        let top_left = Point::new(
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    81
            (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
    82
            (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
    83
        );
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    84
14052
alfadur
parents: 14051 14032
diff changeset
    85
        points.size = land.size();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    86
14052
alfadur
parents: 14051 14032
diff changeset
    87
        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
    88
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    89
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    90
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    91
            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
    92
                if b & 1 != 0 {
14052
alfadur
parents: 14051 14032
diff changeset
    93
                    points.iter_mut().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
    94
                }
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    95
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    96
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    97
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    98
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    99
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   100
            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
   101
                if b & 1 != 0 {
14052
alfadur
parents: 14051 14032
diff changeset
   102
                    points.iter_mut().for_each(|p|
alfadur
parents: 14051 14032
diff changeset
   103
                        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
   104
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   105
            }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   106
        }
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
        points.distort(random_numbers);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   109
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   110
        // draw_edge(points, land, parameters.zero)
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   111
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   112
        for p in points.fill_points {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   113
            land.fill(p, parameters.zero, parameters.zero)
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   114
        }
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
        // draw_edge(points, land, parameters.basic)
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   117
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   118
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   119
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
   120
}
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   121
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   122
#[test()]
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   123
fn points_test() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   124
    let mut points = OutlinePoints {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   125
        islands: vec![vec![]],
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   126
        fill_points: vec![Point::new(1, 1)],
14052
alfadur
parents: 14051 14032
diff changeset
   127
        size: Size::square(100)
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   128
    };
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
   129
14052
alfadur
parents: 14051 14032
diff changeset
   130
    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
   131
    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
   132
}