rust/landgen/src/outline_template_based/template_based.rs
author unC0Rr
Mon, 13 Feb 2023 12:31:30 +0100
branchtransitional_engine
changeset 15922 da6b67f13c12
parent 15921 5f00829c55ec
child 16029 9cbd18220eb7
permissions -rw-r--r--
Refactor mapgen to allow for easy switching between generators
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15921
diff changeset
     1
use super::{outline::OutlinePoints, outline_template::OutlineTemplate};
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15921
diff changeset
     2
use crate::{LandGenerationParameters, LandGenerator};
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     3
use land2d::Land2D;
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
     4
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
     5
pub struct TemplatedLandGenerator {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     6
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     7
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     8
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     9
impl TemplatedLandGenerator {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    10
    pub fn new(outline_template: OutlineTemplate) -> Self {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    11
        Self { outline_template }
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
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    15
impl LandGenerator for TemplatedLandGenerator {
15912
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents: 15905
diff changeset
    16
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    17
        &self,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    18
        parameters: &LandGenerationParameters<T>,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    19
        random_numbers: &mut I,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    20
    ) -> Land2D<T> {
15905
022ec6b916b7 Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents: 15828
diff changeset
    21
        let mut land = Land2D::new(&self.outline_template.size, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    22
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    23
        let mut points = OutlinePoints::from_outline_template(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    24
            &self.outline_template,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    25
            land.play_box(),
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14207
diff changeset
    26
            land.size().size(),
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    27
            random_numbers,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    28
        );
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    29
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    30
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    31
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    32
            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
    33
                if b & 1 != 0 {
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14078
diff changeset
    34
                    points.mirror();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    35
                }
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    36
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    37
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    38
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    39
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    40
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    41
            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
    42
                if b & 1 != 0 {
14095
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14078
diff changeset
    43
                    points.flip();
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    44
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    45
            }
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
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    48
        if !parameters.skip_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    49
            points.distort(parameters.distance_divisor, random_numbers);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    50
        }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    51
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    52
        if !parameters.skip_bezier {
14140
3078123e84ea Bezierize land outline
unc0rr
parents: 14121
diff changeset
    53
            points.bezierize(5);
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    54
        }
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    55
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    56
        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
    57
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    58
        for p in &points.fill_points {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    59
            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
    60
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    61
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    62
        points.draw(&mut land, parameters.basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    63
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    64
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    65
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    66
}