rust/landgen/src/outline_template_based/template_based.rs
author unC0Rr
Tue, 28 Jan 2025 15:49:45 +0100
changeset 16073 5d302b12d837
parent 16058 de01be16df95
permissions -rw-r--r--
- Update landgen to use the latest rand crate - Change Size width and height from usize to u32 for portability - Implement backtracking in wfc generator
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;
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16044
diff changeset
     4
use rand::Rng;
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
     5
14054
3185fb34f3b5 update theme editor to use new land generator implementation
alfadur
parents: 14052
diff changeset
     6
pub struct TemplatedLandGenerator {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     7
    outline_template: OutlineTemplate,
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     8
}
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
     9
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    10
impl TemplatedLandGenerator {
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    11
    pub fn new(outline_template: OutlineTemplate) -> Self {
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    12
        Self { outline_template }
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
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    16
impl LandGenerator for TemplatedLandGenerator {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16044
diff changeset
    17
    fn generate_land<T: Copy + PartialEq + Default>(
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    18
        &self,
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    19
        parameters: &LandGenerationParameters<T>,
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16044
diff changeset
    20
        random_numbers: &mut impl Rng,
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    21
    ) -> Land2D<T> {
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    22
        let do_invert = self.outline_template.is_negative
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16058
diff changeset
    23
            && (!self.outline_template.can_invert || random_numbers.random());
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    24
        let (basic, zero) = if do_invert {
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    25
            (parameters.zero, parameters.basic)
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    26
        } else {
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    27
            (parameters.basic, parameters.zero)
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    28
        };
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    29
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    30
        let mut land = Land2D::new(&self.outline_template.size, basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    31
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    32
        let mut points = OutlinePoints::from_outline_template(
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    33
            &self.outline_template,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    34
            land.play_box(),
15828
44b49f255e31 add type safe power of two sizes
alfadur
parents: 14207
diff changeset
    35
            land.size().size(),
14100
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    36
            random_numbers,
4d22be35cfa2 Finish porting FindPoint()
unc0rr
parents: 14095
diff changeset
    37
        );
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    38
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    39
        // mirror
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16058
diff changeset
    40
        if self.outline_template.can_mirror && random_numbers.random() {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16044
diff changeset
    41
            points.mirror();
14026
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
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16058
diff changeset
    45
        if self.outline_template.can_flip && random_numbers.random() {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16044
diff changeset
    46
            points.flip();
14051
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
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    49
        if !parameters.skip_distort {
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16058
diff changeset
    50
            let distortion_limiting_factor = 100 + random_numbers.random_range(0..8) * 10;
16035
0caa3dfb3ba2 Add templates for different maze styles
unC0Rr
parents: 16029
diff changeset
    51
16044
5c941f5deeec * Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents: 16035
diff changeset
    52
            points.distort(
5c941f5deeec * Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents: 16035
diff changeset
    53
                parameters.distance_divisor,
5c941f5deeec * Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents: 16035
diff changeset
    54
                distortion_limiting_factor,
5c941f5deeec * Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents: 16035
diff changeset
    55
                random_numbers,
5c941f5deeec * Introduce concept of invizible walls to constrain outline map generation
unC0Rr
parents: 16035
diff changeset
    56
            );
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    57
        }
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    58
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    59
        if !parameters.skip_bezier {
14140
3078123e84ea Bezierize land outline
unc0rr
parents: 14121
diff changeset
    60
            points.bezierize(5);
14121
69db1d2e4cec land_dump app for testing templated landgen
unc0rr
parents: 14100
diff changeset
    61
        }
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    62
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    63
        points.draw(&mut land, zero);
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    64
14066
649ccb9f8cfd Commit broken code for divide_edges in hope for salvation
unc0rr
parents: 14054
diff changeset
    65
        for p in &points.fill_points {
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    66
            land.fill(*p, zero, zero)
14051
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    67
        }
8a0d69c16cad Implement OutlinePoints for land generators, some ground work for template based landgen
unc0rr
parents: 14026
diff changeset
    68
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 15922
diff changeset
    69
        points.draw(&mut land, basic);
14026
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    70
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    71
        land
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    72
    }
3b3d97ed2286 Start land generators implementation
unc0rr
parents:
diff changeset
    73
}