rust/landgen/src/wavefront_collapse/generator.rs
author unC0Rr
Sun, 26 Jan 2025 21:29:54 +0100
changeset 16062 288df7b85efc
parent 16059 2acea266d297
child 16064 07cb6dbc8444
permissions -rw-r--r--
Fix inversion parameters being disregarded
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
     1
use super::tile_image::{Edge, TileImage};
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     2
use super::wavefront_collapse::{CollapseRule, Tile, WavefrontCollapse};
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     3
use crate::{LandGenerationParameters, LandGenerator};
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     4
use integral_geometry::Size;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     5
use png::Decoder;
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
     6
use rand::Rng;
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     7
use std::collections::HashSet;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     8
use std::fs::File;
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
     9
use std::io::{BufReader, Result};
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    10
use std::path::{Path, PathBuf};
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    11
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    12
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    13
pub struct EdgeDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    14
    pub name: String,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    15
    pub reversed: Option<bool>,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    16
    pub symmetrical: Option<bool>,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    17
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    18
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    19
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    20
pub struct EdgesDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    21
    pub top: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    22
    pub right: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    23
    pub bottom: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    24
    pub left: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    25
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    26
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    27
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    28
pub struct TileDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    29
    pub name: String,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    30
    pub edges: EdgesDescription,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    31
    pub is_negative: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    32
    pub can_flip: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    33
    pub can_mirror: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    34
    pub can_rotate90: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    35
    pub can_rotate180: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    36
    pub can_rotate270: Option<bool>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    37
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    38
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    39
#[derive(Debug, Clone)]
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    40
pub struct ComplexEdgeDescription {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    41
    pub begin: Option<EdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    42
    pub fill: Option<EdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    43
    pub end: Option<EdgeDescription>,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    44
}
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    45
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    46
#[derive(Debug, Clone)]
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    47
pub struct NonStrictComplexEdgesDescription {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    48
    pub top: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    49
    pub right: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    50
    pub bottom: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    51
    pub left: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    52
}
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    53
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    54
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    55
pub struct TemplateDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    56
    pub size: Size,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    57
    pub tiles: Vec<TileDescription>,
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    58
    pub edges: NonStrictComplexEdgesDescription,
16062
288df7b85efc Fix inversion parameters being disregarded
unC0Rr
parents: 16059
diff changeset
    59
    pub can_invert: bool,
288df7b85efc Fix inversion parameters being disregarded
unC0Rr
parents: 16059
diff changeset
    60
    pub is_negative: bool,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    61
    pub wrap: bool,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    62
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    63
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    64
pub struct WavefrontCollapseLandGenerator {
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    65
    pub template: TemplateDescription,
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    66
    data_path: PathBuf,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    67
}
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    68
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    69
impl WavefrontCollapseLandGenerator {
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    70
    pub fn new(template: TemplateDescription, data_path: &Path) -> Self {
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    71
        Self {
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    72
            template,
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    73
            data_path: data_path.to_owned(),
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    74
        }
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    75
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    76
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    77
    fn load_image_tiles<T: Copy + PartialEq + Default>(
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    78
        &self,
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    79
        parameters: &LandGenerationParameters<T>,
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    80
        tile_description: &TileDescription,
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    81
    ) -> Result<Vec<TileImage<T, String>>> {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    82
        let mut result = Vec::new();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    83
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    84
        let file = File::open(
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    85
            self.data_path
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    86
                .join("Tiles")
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    87
                .join(&tile_description.name)
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    88
                .as_path(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    89
        )?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    90
        let decoder = Decoder::new(BufReader::new(file));
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    91
        let mut reader = decoder.read_info()?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    92
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    93
        let info = reader.info();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    94
        let mut tiles_image = vec2d::Vec2D::new(
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    95
            &Size::new(info.width as usize, info.height as usize),
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    96
            parameters.zero,
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    97
        );
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    98
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    99
        let mut buf = vec![0; reader.output_buffer_size()];
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   100
        let info = reader.next_frame(&mut buf)?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   101
        let bytes = &buf[..info.buffer_size()];
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   102
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   103
        let mut tiles_image_pixels = tiles_image.as_mut_slice().iter_mut();
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   104
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   105
        let (zero, basic) = if tile_description.is_negative.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   106
            (parameters.basic(), parameters.zero())
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   107
        } else {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   108
            (parameters.zero(), parameters.basic())
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   109
        };
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   110
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   111
        match info.color_type.samples() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   112
            1 => {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   113
                for line in bytes.chunks_exact(info.line_size) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   114
                    for value in line.iter() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   115
                        *tiles_image_pixels
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   116
                            .next()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   117
                            .expect("vec2d size matching image dimensions") =
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   118
                            if *value == 0 { zero } else { basic };
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   119
                    }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   120
                }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   121
            }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   122
            a => {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   123
                for line in bytes.chunks_exact(info.line_size) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   124
                    for value in line.chunks_exact(a) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   125
                        print!("{:?},", value);
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   126
                        *tiles_image_pixels
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   127
                            .next()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   128
                            .expect("vec2d size matching image dimensions") =
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   129
                            if value[0] == 0u8 { zero } else { basic };
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   130
                    }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   131
                }
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   132
            }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   133
        }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   134
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   135
        let [top_edge, right_edge, bottom_edge, left_edge]: [Edge<String>; 4] = [
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   136
            (&tile_description.edges.top).into(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   137
            (&tile_description.edges.right).into(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   138
            (&tile_description.edges.bottom).into(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   139
            (&tile_description.edges.left).into(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   140
        ];
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   141
15926
c273908218f3 Format the code, apply clippy fixes
unC0Rr
parents: 15925
diff changeset
   142
        let tile =
c273908218f3 Format the code, apply clippy fixes
unC0Rr
parents: 15925
diff changeset
   143
            TileImage::<T, String>::new(tiles_image, top_edge, right_edge, bottom_edge, left_edge);
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   144
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   145
        result.push(tile.clone());
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   146
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   147
        if tile_description.can_flip.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   148
            result.push(tile.flipped());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   149
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   150
        if tile_description.can_mirror.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   151
            result.push(tile.mirrored());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   152
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   153
        if tile_description.can_flip.unwrap_or_default()
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   154
            && tile_description.can_mirror.unwrap_or_default()
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   155
        {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   156
            result.push(tile.mirrored().flipped());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   157
        }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   158
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   159
        if tile_description.can_rotate90.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   160
            result.push(tile.rotated90());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   161
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   162
        if tile_description.can_rotate180.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   163
            result.push(tile.rotated180());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   164
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   165
        if tile_description.can_rotate270.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   166
            result.push(tile.rotated270());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   167
        }
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   168
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   169
        Ok(result)
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   170
    }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   171
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   172
    pub fn load_template<T: Copy + PartialEq + Default>(
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   173
        &self,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   174
        parameters: &LandGenerationParameters<T>,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   175
    ) -> Vec<TileImage<T, String>> {
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   176
        let mut result = Vec::new();
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   177
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   178
        for tile_description in self.template.tiles.iter() {
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
   179
            if let Ok(mut tiles) = self.load_image_tiles(parameters, tile_description) {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   180
                result.append(&mut tiles);
16023
0fd23fc57947 Make pascal engine link to hwengine-future and use WFC generator
unC0Rr
parents: 15926
diff changeset
   181
            } else {
0fd23fc57947 Make pascal engine link to hwengine-future and use WFC generator
unC0Rr
parents: 15926
diff changeset
   182
                eprintln!("Failed to load a tile!");
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   183
            }
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   184
        }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   185
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   186
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   187
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   188
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   189
    pub fn build_rules<T: Copy + PartialEq + Default>(
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   190
        &self,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   191
        tiles: &[TileImage<T, String>],
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   192
        probability_distribution_factor: i32,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   193
    ) -> Vec<CollapseRule> {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   194
        let [grid_top_edge, grid_right_edge, grid_bottom_edge, grid_left_edge]: [Option<
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   195
            [Option<Edge<String>>; 3],
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   196
        >; 4] = [
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   197
            self.template.edges.top.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   198
            self.template.edges.right.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   199
            self.template.edges.bottom.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   200
            self.template.edges.left.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   201
        ]
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   202
        .map(|opt| opt.map(|d| [&d.begin, &d.fill, &d.end].map(|e| e.as_ref().map(Into::into))));
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   203
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   204
        let mut rules = Vec::<CollapseRule>::new();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   205
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   206
        let default_connection = HashSet::from_iter(vec![Tile::Empty].into_iter());
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   207
        for (i, tile) in tiles.iter().enumerate() {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   208
            let mut right = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   209
            let mut bottom = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   210
            let mut left = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   211
            let mut top = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   212
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   213
            let iteration = [
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   214
                (&grid_top_edge, tile.top_edge(), &mut top),
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   215
                (&grid_right_edge, tile.right_edge(), &mut right),
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   216
                (&grid_bottom_edge, tile.bottom_edge(), &mut bottom),
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   217
                (&grid_left_edge, tile.left_edge(), &mut left),
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   218
            ];
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   219
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   220
            // compatibility with grid edges
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   221
            for (edge, tile_edge, set) in iteration {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   222
                for (is_compatible, tile) in edge
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   223
                    .as_ref()
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   224
                    .map(|e| {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   225
                        e.clone().map(|ed| {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   226
                            ed.as_ref()
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   227
                                .map(|e| e.is_compatible(tile_edge))
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   228
                                .unwrap_or(true)
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   229
                        })
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   230
                    })
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   231
                    .unwrap_or([true, true, true])
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   232
                    .into_iter()
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   233
                    .zip([Tile::OutsideBegin, Tile::OutsideFill, Tile::OutsideEnd].into_iter())
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   234
                {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   235
                    if is_compatible {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   236
                        set.insert(tile);
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   237
                    }
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   238
                }
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   239
            }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   240
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   241
            // compatibility with itself
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   242
            if tile.left_edge().is_compatible(tile.right_edge()) {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   243
                left.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   244
                right.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   245
            }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   246
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   247
            if tile.top_edge().is_compatible(tile.bottom_edge()) {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   248
                top.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   249
                bottom.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   250
            }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   251
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   252
            // compatibility with previously defined tiles
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   253
            for p in 0..i {
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   254
                // Check left edge
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   255
                if tiles[p].left_edge().is_compatible(tile.right_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   256
                    rules[p].left.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   257
                    right.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   258
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   259
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   260
                // Check right edge
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   261
                if tiles[p].right_edge().is_compatible(tile.left_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   262
                    rules[p].right.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   263
                    left.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   264
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   265
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   266
                // Check top edge
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   267
                if tiles[p].top_edge().is_compatible(tile.bottom_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   268
                    rules[p].top.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   269
                    bottom.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   270
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   271
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   272
                // Check bottom edge
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   273
                if tiles[p].bottom_edge().is_compatible(tile.top_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   274
                    rules[p].bottom.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   275
                    top.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   276
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   277
            }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   278
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   279
            let weight = (probability_distribution_factor * 2 * i as i32 / (tiles.len() - 1) as i32
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   280
                + 100
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   281
                - probability_distribution_factor) as u32;
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   282
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   283
            rules.push(CollapseRule {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   284
                weight,
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   285
                tile: Tile::Numbered(i),
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   286
                top,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   287
                right,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   288
                bottom,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   289
                left,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   290
            });
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   291
        }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   292
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   293
        rules
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   294
    }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   295
}
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   296
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   297
impl LandGenerator for WavefrontCollapseLandGenerator {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   298
    fn generate_land<T: Copy + PartialEq + Default>(
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   299
        &self,
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   300
        parameters: &LandGenerationParameters<T>,
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   301
        random_numbers: &mut impl Rng,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   302
    ) -> land2d::Land2D<T> {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   303
        assert!(parameters.distance_divisor >= 1);
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   304
        assert!(parameters.distance_divisor <= 25);
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   305
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   306
        let tiles = self.load_template(parameters);
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   307
        let distribution_factor = (parameters.distance_divisor - 1) as i32 * 8 - 96;
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   308
        let rules = self.build_rules(&tiles, distribution_factor);
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   309
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   310
        let mut wfc = WavefrontCollapse::new(self.template.wrap);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   311
        wfc.set_rules(rules);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   312
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   313
        let wfc_size = if let Some(first_tile) = tiles.first() {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   314
            let tile_size = first_tile.size();
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   315
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   316
            Size::new(
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   317
                self.template.size.width / tile_size.width,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   318
                self.template.size.height / tile_size.height,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   319
            )
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   320
        } else {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   321
            Size::new(1, 1)
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   322
        };
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   323
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   324
        wfc.generate_map(&wfc_size, |_| {}, random_numbers);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   325
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   326
        // render tiles into resulting land array
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   327
        let mut result = land2d::Land2D::new(&self.template.size, parameters.zero);
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   328
        let offset_y = result.height() - result.play_height();
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   329
        let offset_x = (result.width() - result.play_width()) / 2;
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   330
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   331
        for row in 0..wfc_size.height {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   332
            for column in 0..wfc_size.width {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   333
                if let Some(Tile::Numbered(tile_index)) = wfc.grid().get(row, column) {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   334
                    let tile = &tiles[*tile_index];
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   335
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   336
                    for tile_row in 0..tile.size().height {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   337
                        for tile_column in 0..tile.size().width {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   338
                            result.map(
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   339
                                (row * tile.size().height + tile_row + offset_y) as i32,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   340
                                (column * tile.size().width + tile_column + offset_x) as i32,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   341
                                |p| {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   342
                                    *p =
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   343
                                        *tile.get(tile_row, tile_column).unwrap_or(&parameters.zero)
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   344
                                },
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   345
                            );
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   346
                        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   347
                    }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   348
                }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   349
            }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   350
        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   351
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   352
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   353
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   354
}
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   355
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   356
impl From<&EdgeDescription> for Edge<String> {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   357
    fn from(val: &EdgeDescription) -> Self {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   358
        let edge = Edge::new(val.name.clone(), val.symmetrical.unwrap_or_default());
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   359
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   360
        if val.reversed.unwrap_or_default() {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   361
            edge.reversed()
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   362
        } else {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   363
            edge
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   364
        }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   365
    }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   366
}