rust/landgen/src/template_based.rs
author unC0Rr
Fri, 02 Nov 2018 09:45:54 +0100
changeset 14090 abb42ba345b6
parent 14088 3f21f27c6564
child 14099 bf40b5f938b0
permissions -rw-r--r--
Rework lib structure, no code changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents: 14088
diff changeset
     1
use integral_geometry::{Point, Size};
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     2
use land2d::Land2D;
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     3
use LandGenerationParameters;
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     4
use LandGenerator;
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     5
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents: 14088
diff changeset
     6
use outline::OutlinePoints;
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents: 14088
diff changeset
     7
use outline_template::OutlineTemplate;
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
     8
14075
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14073
diff changeset
     9
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14073
diff changeset
    10
pub struct TemplatedLandGenerator {
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    11
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    12
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    13
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    14
impl TemplatedLandGenerator {
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    15
    pub fn new(outline_template: OutlineTemplate) -> Self {
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    16
        Self { outline_template }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    17
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    18
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    19
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    20
impl LandGenerator for TemplatedLandGenerator {
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    21
    fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    22
        &self,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    23
        parameters: LandGenerationParameters<T>,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    24
        random_numbers: &mut I,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    25
    ) -> Land2D<T> {
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    26
        let mut points =
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    27
            OutlinePoints::from_outline_template(&self.outline_template, random_numbers);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    28
14073
alfadur
parents: 14072 14053
diff changeset
    29
        let mut land = Land2D::new(points.size, parameters.basic);
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    30
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    31
        let top_left = Point::new(
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    32
            (land.width() - land.play_width() / 2) as i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    33
            (land.height() - land.play_height()) as i32,
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    34
        );
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    35
14073
alfadur
parents: 14072 14053
diff changeset
    36
        points.size = land.size();
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    37
14073
alfadur
parents: 14072 14053
diff changeset
    38
        points.iter_mut().for_each(|p| *p += top_left);
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    39
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    40
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    41
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    42
            if let Some(b) = random_numbers.next() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    43
                if b & 1 != 0 {
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    44
                    points
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    45
                        .iter_mut()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    46
                        .for_each(|p| p.x = land.width() as i32 - 1 - p.x);
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    47
                }
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    48
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    49
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    50
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    51
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    52
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    53
            if let Some(b) = random_numbers.next() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    54
                if b & 1 != 0 {
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    55
                    points
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    56
                        .iter_mut()
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    57
                        .for_each(|p| p.y = land.height() as i32 - 1 - p.y);
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    58
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    59
            }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    60
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    61
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    62
        points.distort(random_numbers);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    63
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    64
        points.draw(&mut land, parameters.zero);
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    65
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    66
        for p in &points.fill_points {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    67
            land.fill(*p, parameters.zero, parameters.zero)
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    68
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    69
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    70
        points.draw(&mut land, parameters.basic);
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    71
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    72
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    73
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    74
}
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    75
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    76
#[test()]
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    77
fn points_test() {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    78
    let mut points = OutlinePoints {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    79
        islands: vec![vec![]],
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    80
        fill_points: vec![Point::new(1, 1)],
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    81
        size: Size::square(100),
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    82
    };
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    83
14073
alfadur
parents: 14072 14053
diff changeset
    84
    points.iter_mut().for_each(|p| p.x = 2);
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    85
    assert_eq!(points.fill_points[0].x, 2);
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    86
}