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