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