rust/mapgen/src/template/wavefront_collapse.rs
author unC0Rr
Sun, 02 Feb 2025 17:47:54 +0100
changeset 16079 65c017453e83
parent 16077 aba25f4e4645
permissions -rw-r--r--
Add anti_match feature, log some info when cannot satisfy rules
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
     1
use integral_geometry::Size;
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
     2
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
     3
use landgen::wavefront_collapse::generator::*;
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
     4
use serde_derive::Deserialize;
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
     5
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
     6
use std::collections::hash_map::HashMap;
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
     7
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
     8
#[derive(Debug, Deserialize)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
     9
pub struct TileDesc {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    10
    pub name: String,
16077
aba25f4e4645 Implement configurable weight for tiles
unC0Rr
parents: 16073
diff changeset
    11
    pub weight: Option<u8>,
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    12
    pub edges: [String; 4],
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
    13
    pub anti_match: Option<[u64; 4]>,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    14
    pub is_negative: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    15
    pub can_flip: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    16
    pub can_mirror: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    17
    pub can_rotate90: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    18
    pub can_rotate180: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    19
    pub can_rotate270: Option<bool>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    20
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    21
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    22
#[derive(Debug, Deserialize)]
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    23
pub struct ComplexEdgeDesc {
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    24
    pub begin: Option<String>,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    25
    pub fill: Option<String>,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    26
    pub end: Option<String>,
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    27
}
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    28
#[derive(Debug, Deserialize)]
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    29
pub struct NonStrictComplexEdgesDesc {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    30
    pub top: Option<ComplexEdgeDesc>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    31
    pub right: Option<ComplexEdgeDesc>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    32
    pub bottom: Option<ComplexEdgeDesc>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    33
    pub left: Option<ComplexEdgeDesc>,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    34
}
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    35
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    36
#[derive(Debug, Deserialize)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    37
pub struct TemplateDesc {
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    38
    pub width: u32,
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    39
    pub height: u32,
16065
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    40
    pub can_invert: Option<bool>,
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    41
    pub is_negative: Option<bool>,
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    42
    pub put_girders: Option<bool>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    43
    pub max_hedgehogs: u8,
16065
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    44
    pub wrap: Option<bool>,
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    45
    pub edges: Option<String>,
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    46
    pub tiles: Vec<String>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    47
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    48
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    49
#[derive(Debug, Deserialize)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    50
pub struct TemplateCollectionDesc {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    51
    pub templates: Vec<TemplateDesc>,
16065
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    52
    pub tiles: HashMap<String, Vec<TileDesc>>,
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    53
    pub edges: HashMap<String, NonStrictComplexEdgesDesc>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    54
    pub template_types: HashMap<String, Vec<usize>>,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    55
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    56
16065
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    57
impl TemplateDesc {
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    58
    pub fn to_template(
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    59
        &self,
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    60
        tiles: &HashMap<String, Vec<TileDesc>>,
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    61
        edges: &HashMap<String, NonStrictComplexEdgesDesc>,
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    62
    ) -> TemplateDescription {
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    63
        let [top, right, bottom, left]: [Option<ComplexEdgeDescription>; 4] =
16065
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    64
            if let Some(edges_name) = &self.edges {
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    65
                let edges = edges.get(edges_name).expect("missing template edges");
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    66
                [&edges.top, &edges.right, &edges.bottom, &edges.left]
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    67
                    .map(|e| e.as_ref().map(Into::into))
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    68
            } else {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    69
                [None, None, None, None]
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    70
            };
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    71
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    72
        let tiles = self
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    73
            .tiles
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    74
            .iter()
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    75
            .flat_map(|t| tiles.get(t).expect("missing template tiles"))
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16065
diff changeset
    76
            .collect::<Vec<_>>();
16065
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    77
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    78
        TemplateDescription {
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    79
            size: Size::new(self.width, self.height),
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    80
            tiles: tiles.into_iter().map(|t| t.into()).collect(),
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    81
            wrap: self.wrap.unwrap_or(false),
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    82
            can_invert: self.can_invert.unwrap_or(false),
33f09636018b Switch to toml for WFC templates
unC0Rr
parents: 16064
diff changeset
    83
            is_negative: self.is_negative.unwrap_or(false),
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    84
            edges: NonStrictComplexEdgesDescription {
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    85
                top,
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    86
                right,
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    87
                bottom,
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    88
                left,
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    89
            },
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    90
        }
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    91
    }
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents:
diff changeset
    92
}
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
    93
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    94
impl From<&TileDesc> for TileDescription {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    95
    fn from(desc: &TileDesc) -> Self {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    96
        let [top, right, bottom, left]: [EdgeDescription; 4] = desc.edges.clone().map(|e| e.into());
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    97
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    98
        Self {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
    99
            name: desc.name.clone(),
16077
aba25f4e4645 Implement configurable weight for tiles
unC0Rr
parents: 16073
diff changeset
   100
            weight: desc.weight.unwrap_or(10),
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   101
            edges: EdgesDescription {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   102
                top,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   103
                right,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   104
                bottom,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   105
                left,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   106
            },
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   107
            anti_match: desc.anti_match,
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   108
            is_negative: desc.is_negative,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   109
            can_flip: desc.can_flip,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   110
            can_mirror: desc.can_mirror,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   111
            can_rotate90: desc.can_rotate90,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   112
            can_rotate180: desc.can_rotate180,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   113
            can_rotate270: desc.can_rotate270,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   114
        }
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   115
    }
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   116
}
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   117
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
   118
impl From<&ComplexEdgeDesc> for ComplexEdgeDescription {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
   119
    fn from(value: &ComplexEdgeDesc) -> Self {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
   120
        Self {
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   121
            begin: value.begin.as_ref().map(|e| e.into()),
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   122
            fill: value.fill.as_ref().map(|e| e.into()),
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   123
            end: value.end.as_ref().map(|e| e.into()),
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
   124
        }
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
   125
    }
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 15925
diff changeset
   126
}