rust/landgen/src/template_based.rs
author alfadur
Thu, 29 Nov 2018 01:23:14 +0300
changeset 14336 9e8e64b2bb9e
parent 14207 bb2f301d4fe0
child 15828 44b49f255e31
permissions -rw-r--r--
make blowtorch hit moving gears
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14069
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents: 14067
diff changeset
     1
use integral_geometry::{Point, Size};
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     2
use land2d::Land2D;
14207
bb2f301d4fe0 2018ize everything
alfadur
parents: 14140
diff changeset
     3
use crate::{
bb2f301d4fe0 2018ize everything
alfadur
parents: 14140
diff changeset
     4
    LandGenerationParameters,
bb2f301d4fe0 2018ize everything
alfadur
parents: 14140
diff changeset
     5
    LandGenerator,
bb2f301d4fe0 2018ize everything
alfadur
parents: 14140
diff changeset
     6
    outline::OutlinePoints,
bb2f301d4fe0 2018ize everything
alfadur
parents: 14140
diff changeset
     7
    outline_template::OutlineTemplate
bb2f301d4fe0 2018ize everything
alfadur
parents: 14140
diff changeset
     8
};
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
     9
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
    10
pub struct TemplatedLandGenerator {
14026
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 {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    15
    pub fn new(outline_template: OutlineTemplate) -> Self {
14026
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,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    23
        parameters: &LandGenerationParameters<T>,
14026
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> {
14078
bf40b5f938b0 - Add methods to work with Rect as box
unC0Rr
parents: 14069
diff changeset
    26
        let mut land = Land2D::new(self.outline_template.size, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    27
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    28
        let mut points = OutlinePoints::from_outline_template(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    29
            &self.outline_template,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    30
            land.play_box(),
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    31
            land.size(),
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    32
            random_numbers,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    33
        );
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    34
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    35
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    36
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    37
            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
    38
                if b & 1 != 0 {
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14078
diff changeset
    39
                    points.mirror();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    40
                }
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    41
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    42
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    43
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    44
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    45
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    46
            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
    47
                if b & 1 != 0 {
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14078
diff changeset
    48
                    points.flip();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    49
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    50
            }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    51
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    52
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    53
        if !parameters.skip_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    54
            points.distort(parameters.distance_divisor, random_numbers);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    55
        }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    56
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    57
        if !parameters.skip_bezier {
14140
3078123e84ea Bezierize land outline
unc0rr
parents: 14121
diff changeset
    58
            points.bezierize(5);
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    59
        }
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    60
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    61
        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
    62
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    63
        for p in &points.fill_points {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    64
            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
    65
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    66
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    67
        points.draw(&mut land, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    68
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    69
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    70
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    71
}