rust/landgen/src/template_based.rs
author unC0Rr
Thu, 02 Feb 2023 08:41:31 +0100
branchtransitional_engine
changeset 15945 8f093b1b18bc
parent 15942 6e22f4390b7e
child 15946 e82de0410da5
permissions -rw-r--r--
Add loading of tiles from png
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14228
diff changeset
     1
use crate::{
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14228
diff changeset
     2
    outline::OutlinePoints, outline_template::OutlineTemplate, LandGenerationParameters,
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14228
diff changeset
     3
    LandGenerator,
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14228
diff changeset
     4
};
14090
abb42ba345b6 Rework lib structure, no code changes
unC0Rr
parents: 14088
diff changeset
     5
use integral_geometry::{Point, Size};
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     6
use land2d::Land2D;
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
     7
14075
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14073
diff changeset
     8
pub struct TemplatedLandGenerator {
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     9
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    10
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    11
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    12
impl TemplatedLandGenerator {
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    13
    pub fn new(outline_template: OutlineTemplate) -> Self {
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    14
        Self { outline_template }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    15
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    16
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    17
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    18
impl LandGenerator for TemplatedLandGenerator {
15942
6e22f4390b7e Add basics of wavefront collapse algorithm
unC0Rr
parents: 15934
diff changeset
    19
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    20
        &self,
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    21
        parameters: &LandGenerationParameters<T>,
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    22
        random_numbers: &mut I,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    23
    ) -> Land2D<T> {
15934
022ec6b916b7 Split generation and painting phases, paint by old engine, use template filters
unC0Rr
parents: 15850
diff changeset
    24
        let mut land = Land2D::new(&self.outline_template.size, parameters.basic);
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    25
14121
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14116
diff changeset
    26
        let mut points = OutlinePoints::from_outline_template(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14116
diff changeset
    27
            &self.outline_template,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14116
diff changeset
    28
            land.play_box(),
15850
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14228
diff changeset
    29
            land.size().size(),
14121
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14116
diff changeset
    30
            random_numbers,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14116
diff changeset
    31
        );
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    32
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    33
        // mirror
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    34
        if self.outline_template.can_mirror {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    35
            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
    36
                if b & 1 != 0 {
14116
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14099
diff changeset
    37
                    points.mirror();
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    38
                }
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    39
            }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    40
        }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    41
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    42
        // flip
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    43
        if self.outline_template.can_flip {
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    44
            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
    45
                if b & 1 != 0 {
14116
b2feb190e4bc Move flip and mirror to outline methods
unc0rr
parents: 14099
diff changeset
    46
                    points.flip();
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    47
                }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    48
            }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    49
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    50
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    51
        if !parameters.skip_distort {
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    52
            points.distort(parameters.distance_divisor, random_numbers);
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    53
        }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    54
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    55
        if !parameters.skip_bezier {
14161
3078123e84ea Bezierize land outline
unc0rr
parents: 14142
diff changeset
    56
            points.bezierize(5);
14142
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14121
diff changeset
    57
        }
14072
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    58
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    59
        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
    60
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    61
        for p in &points.fill_points {
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    62
            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
    63
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14047
diff changeset
    64
14087
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14075
diff changeset
    65
        points.draw(&mut land, parameters.basic);
14047
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    66
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    67
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    68
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    69
}