rust/landgen/src/wavefront_collapse/generator.rs
author unC0Rr
Sun, 02 Feb 2025 22:30:49 +0100
changeset 16080 2fc37552b587
parent 16079 65c017453e83
permissions -rw-r--r--
Introduce hard match option for edges to reduce backtracking
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
     1
use super::tile_image::{Edge, EdgeSet, MatchSide, 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>,
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
    17
    pub hard_match: Option<bool>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    18
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    19
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    20
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    21
pub struct EdgesDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    22
    pub top: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    23
    pub right: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    24
    pub bottom: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    25
    pub left: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    26
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    27
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    28
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    29
pub struct TileDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    30
    pub name: String,
16077
aba25f4e4645 Implement configurable weight for tiles
unC0Rr
parents: 16075
diff changeset
    31
    pub weight: u8,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    32
    pub edges: EdgesDescription,
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
    33
    pub anti_match: Option<[u64; 4]>,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    34
    pub is_negative: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    35
    pub can_flip: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    36
    pub can_mirror: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    37
    pub can_rotate90: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    38
    pub can_rotate180: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    39
    pub can_rotate270: Option<bool>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    40
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    41
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    42
#[derive(Debug, Clone)]
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    43
pub struct ComplexEdgeDescription {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    44
    pub begin: Option<EdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    45
    pub fill: Option<EdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    46
    pub end: Option<EdgeDescription>,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    47
}
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
    48
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    49
#[derive(Debug, Clone)]
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    50
pub struct NonStrictComplexEdgesDescription {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    51
    pub top: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    52
    pub right: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    53
    pub bottom: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    54
    pub left: Option<ComplexEdgeDescription>,
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    55
}
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    56
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    57
#[derive(Debug, Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    58
pub struct TemplateDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    59
    pub size: Size,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    60
    pub tiles: Vec<TileDescription>,
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    61
    pub edges: NonStrictComplexEdgesDescription,
16062
288df7b85efc Fix inversion parameters being disregarded
unC0Rr
parents: 16059
diff changeset
    62
    pub can_invert: bool,
288df7b85efc Fix inversion parameters being disregarded
unC0Rr
parents: 16059
diff changeset
    63
    pub is_negative: bool,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    64
    pub wrap: bool,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    65
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    66
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    67
pub struct WavefrontCollapseLandGenerator {
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    68
    pub template: TemplateDescription,
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    69
    data_path: PathBuf,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    70
}
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    71
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    72
impl WavefrontCollapseLandGenerator {
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    73
    pub fn new(template: TemplateDescription, data_path: &Path) -> Self {
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    74
        Self {
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    75
            template,
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    76
            data_path: data_path.to_owned(),
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    77
        }
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    78
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    79
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    80
    fn load_image_tiles<T: Copy + PartialEq + Default>(
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
    81
        &self,
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    82
        parameters: &LandGenerationParameters<T>,
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    83
        tile_description: &TileDescription,
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    84
    ) -> Result<Vec<TileImage<T, String>>> {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    85
        let mut result = Vec::new();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    86
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    87
        let file = File::open(
16029
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    88
            self.data_path
9cbd18220eb7 Fix cavern templates
unC0Rr
parents: 16024
diff changeset
    89
                .join("Tiles")
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    90
                .join(&tile_description.name)
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    91
                .as_path(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    92
        )?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    93
        let decoder = Decoder::new(BufReader::new(file));
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
    94
        let mut reader = decoder.read_info()?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    95
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    96
        let info = reader.info();
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
    97
        let mut tiles_image =
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
    98
            vec2d::Vec2D::new(&Size::new(info.width, info.height), parameters.zero);
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    99
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   100
        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
   101
        let info = reader.next_frame(&mut buf)?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   102
        let bytes = &buf[..info.buffer_size()];
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   103
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   104
        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
   105
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   106
        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
   107
            (parameters.basic(), parameters.zero())
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   108
        } else {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   109
            (parameters.zero(), parameters.basic())
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   110
        };
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   111
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   112
        match info.color_type.samples() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   113
            1 => {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   114
                for line in bytes.chunks_exact(info.line_size) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   115
                    for value in line.iter() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   116
                        *tiles_image_pixels
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   117
                            .next()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   118
                            .expect("vec2d size matching image dimensions") =
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   119
                            if *value == 0 { zero } else { basic };
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
            }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   123
            a => {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   124
                for line in bytes.chunks_exact(info.line_size) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   125
                    for value in line.chunks_exact(a) {
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
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   135
        let edge_set: EdgeSet<String> = EdgeSet::new([
15925
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(),
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   140
        ]);
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   141
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   142
        let tile = TileImage::<T, String>::new(
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   143
            tiles_image,
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   144
            tile_description.weight,
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   145
            edge_set,
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   146
            tile_description.anti_match.unwrap_or_default(),
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   147
        );
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   148
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   149
        result.push(tile.clone());
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   150
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   151
        if tile_description.can_flip.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   152
            result.push(tile.flipped());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   153
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   154
        if tile_description.can_mirror.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   155
            result.push(tile.mirrored());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   156
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   157
        if tile_description.can_flip.unwrap_or_default()
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   158
            && tile_description.can_mirror.unwrap_or_default()
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   159
        {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   160
            result.push(tile.mirrored().flipped());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   161
        }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   162
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   163
        if tile_description.can_rotate90.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   164
            result.push(tile.rotated90());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   165
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   166
        if tile_description.can_rotate180.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   167
            result.push(tile.rotated180());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   168
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   169
        if tile_description.can_rotate270.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   170
            result.push(tile.rotated270());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   171
        }
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   172
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   173
        Ok(result)
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   174
    }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   175
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   176
    pub fn load_template<T: Copy + PartialEq + Default>(
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   177
        &self,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   178
        parameters: &LandGenerationParameters<T>,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   179
    ) -> Vec<TileImage<T, String>> {
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   180
        let mut result = Vec::new();
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   181
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   182
        for tile_description in self.template.tiles.iter() {
16024
3402b2185698 Pass full path to WFC for tile loading
unC0Rr
parents: 16023
diff changeset
   183
            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
   184
                result.append(&mut tiles);
16023
0fd23fc57947 Make pascal engine link to hwengine-future and use WFC generator
unC0Rr
parents: 15926
diff changeset
   185
            } else {
0fd23fc57947 Make pascal engine link to hwengine-future and use WFC generator
unC0Rr
parents: 15926
diff changeset
   186
                eprintln!("Failed to load a tile!");
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   187
            }
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   188
        }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   189
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   190
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   191
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   192
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   193
    pub fn build_rules<T: Copy + PartialEq + Default>(
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   194
        &self,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   195
        tiles: &[TileImage<T, String>],
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   196
        probability_distribution_factor: i32,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   197
    ) -> Vec<CollapseRule> {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   198
        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
   199
            [Option<Edge<String>>; 3],
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   200
        >; 4] = [
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   201
            self.template.edges.top.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   202
            self.template.edges.right.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   203
            self.template.edges.bottom.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   204
            self.template.edges.left.as_ref(),
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   205
        ]
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   206
        .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
   207
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   208
        let mut rules = Vec::<CollapseRule>::new();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   209
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   210
        for (i, tile) in tiles.iter().enumerate() {
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   211
            let mut top = HashSet::new();
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   212
            let mut right = HashSet::new();
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   213
            let mut bottom = HashSet::new();
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   214
            let mut left = HashSet::new();
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   215
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   216
            let iteration = [
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   217
                (&grid_top_edge, tile.edge_set().top(), &mut top),
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   218
                (&grid_right_edge, tile.edge_set().right(), &mut right),
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   219
                (&grid_bottom_edge, tile.edge_set().bottom(), &mut bottom),
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   220
                (&grid_left_edge, tile.edge_set().left(), &mut left),
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   221
            ];
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   222
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   223
            // compatibility with grid edges
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   224
            for (edge, tile_edge, set) in iteration {
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   225
                if !tile_edge.hard_match() {
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   226
                    set.insert(Tile::Empty);
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   227
                }
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   228
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   229
                for (is_compatible, tile) in edge
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   230
                    .as_ref()
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   231
                    .map(|e| {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   232
                        e.clone().map(|ed| {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   233
                            ed.as_ref()
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   234
                                .map(|e| e.is_compatible(tile_edge))
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   235
                                .unwrap_or(true)
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   236
                        })
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
                    .unwrap_or([true, true, true])
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   239
                    .into_iter()
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   240
                    .zip([Tile::OutsideBegin, Tile::OutsideFill, Tile::OutsideEnd].into_iter())
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   241
                {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   242
                    if is_compatible {
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   243
                        set.insert(tile);
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   244
                    }
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   245
                }
15925
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
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   248
            // compatibility with itself
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   249
            if tile.is_compatible(&tile, MatchSide::OnLeft) {
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   250
                left.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   251
                right.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   252
            }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   253
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   254
            if tile.is_compatible(&tile, MatchSide::OnTop) {
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   255
                top.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   256
                bottom.insert(Tile::Numbered(i));
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   257
            }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   258
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   259
            // compatibility with previously defined tiles
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   260
            for p in 0..i {
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   261
                // Check left edge
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   262
                if tiles[p].is_compatible(&tile, MatchSide::OnLeft) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   263
                    rules[p].left.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   264
                    right.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   265
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   266
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   267
                // Check right edge
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   268
                if tiles[p].is_compatible(&tile, MatchSide::OnRight) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   269
                    rules[p].right.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   270
                    left.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   271
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   272
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   273
                // Check top edge
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   274
                if tiles[p].is_compatible(&tile, MatchSide::OnTop) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   275
                    rules[p].top.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   276
                    bottom.insert(Tile::Numbered(p));
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
16059
2acea266d297 Fix generation in corners by extending outline edge definitions
unC0Rr
parents: 16058
diff changeset
   279
                // Check bottom edge
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   280
                if tiles[p].is_compatible(&tile, MatchSide::OnBottom) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   281
                    rules[p].bottom.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   282
                    top.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   283
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   284
            }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   285
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   286
            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
   287
                + 100
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   288
                - probability_distribution_factor) as u32;
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   289
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   290
            rules.push(CollapseRule {
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   291
                weight: weight * tile.weight as u32,
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   292
                tile: Tile::Numbered(i),
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   293
                top,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   294
                right,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   295
                bottom,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   296
                left,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   297
            });
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   298
        }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   299
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   300
        rules
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   301
    }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   302
}
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   303
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   304
impl LandGenerator for WavefrontCollapseLandGenerator {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   305
    fn generate_land<T: Copy + PartialEq + Default>(
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   306
        &self,
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   307
        parameters: &LandGenerationParameters<T>,
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   308
        random_numbers: &mut impl Rng,
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   309
    ) -> land2d::Land2D<T> {
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   310
        assert!(parameters.distance_divisor >= 1);
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   311
        assert!(parameters.distance_divisor <= 25);
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   312
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   313
        let tiles = self.load_template(parameters);
16058
de01be16df95 Make slider below preview affect WFC generator by skewing tile probabilities
unC0Rr
parents: 16029
diff changeset
   314
        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
   315
        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
   316
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   317
        let mut wfc = WavefrontCollapse::new(self.template.wrap);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   318
        wfc.set_rules(rules);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   319
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   320
        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
   321
            let tile_size = first_tile.size();
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
            Size::new(
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   324
                self.template.size.width / tile_size.width,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   325
                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
   326
            )
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   327
        } else {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   328
            Size::new(1, 1)
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   329
        };
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
        wfc.generate_map(&wfc_size, |_| {}, random_numbers);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   332
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   333
        // render tiles into resulting land array
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   334
        let mut result = land2d::Land2D::new(&self.template.size, parameters.zero);
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   335
        let offset_y = result.height() - result.play_height() as usize;
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   336
        let offset_x = (result.width() - result.play_width() as usize) / 2;
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   337
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   338
        for row in 0..wfc_size.height as usize {
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   339
            for column in 0..wfc_size.width as usize {
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   340
                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
   341
                    let tile = &tiles[*tile_index];
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   342
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   343
                    for tile_row in 0..tile.size().height as usize {
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   344
                        for tile_column in 0..tile.size().width as usize {
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   345
                            result.map(
16073
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   346
                                (row * tile.size().height as usize + tile_row + offset_y) as i32,
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   347
                                (column * tile.size().width as usize + tile_column + offset_x)
5d302b12d837 - Update landgen to use the latest rand crate
unC0Rr
parents: 16064
diff changeset
   348
                                    as i32,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   349
                                |p| {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   350
                                    *p =
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   351
                                        *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
   352
                                },
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   353
                            );
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   354
                        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   355
                    }
16079
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   356
                } else {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   357
                    // couldn't find a tile to place here, dump some debug info for tile set maker
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   358
                    let mut edges = ["-", "|", "-", "|"].map(|s| s.to_owned());
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   359
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   360
                    if row > 0 {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   361
                        let tile = wfc.grid().get(row - 1, column);
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   362
                        edges[0] = if let Some(Tile::Numbered(tile_index)) = tile {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   363
                            tiles[*tile_index].edge_set().bottom().name()
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   364
                        } else {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   365
                            format!("{:?}", tile.unwrap())
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   366
                        }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   367
                    }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   368
                    if column < wfc_size.width as usize - 1 {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   369
                        let tile = wfc.grid().get(row, column + 1);
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   370
                        edges[1] = if let Some(Tile::Numbered(tile_index)) = tile {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   371
                            tiles[*tile_index].edge_set().left().name()
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   372
                        } else {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   373
                            format!("{:?}", tile.unwrap())
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   374
                        }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   375
                    }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   376
                    if row < wfc_size.height as usize - 1 {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   377
                        let tile = wfc.grid().get(row + 1, column);
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   378
                        edges[2] = if let Some(Tile::Numbered(tile_index)) = tile {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   379
                            tiles[*tile_index].edge_set().top().name()
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   380
                        } else {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   381
                            format!("{:?}", tile.unwrap())
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   382
                        }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   383
                    }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   384
                    if column > 0 {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   385
                        let tile = wfc.grid().get(row, column - 1);
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   386
                        edges[3] = if let Some(Tile::Numbered(tile_index)) = tile {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   387
                            tiles[*tile_index].edge_set().right().name()
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   388
                        } else {
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   389
                            format!("{:?}", tile.unwrap())
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   390
                        }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   391
                    }
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   392
                    eprintln!(
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   393
                        "Couldn't find a tile to place here (row, column): ({}, {}), edges are: [{}]",
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   394
                        row, column, edges.join(", "),
65c017453e83 Add anti_match feature, log some info when cannot satisfy rules
unC0Rr
parents: 16077
diff changeset
   395
                    );
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   396
                }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   397
            }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   398
        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   399
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   400
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   401
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   402
}
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   403
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   404
impl From<&EdgeDescription> for Edge<String> {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   405
    fn from(val: &EdgeDescription) -> Self {
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   406
        let edge = Edge::new(
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   407
            val.name.clone(),
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   408
            val.symmetrical.unwrap_or_default(),
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   409
            val.hard_match.unwrap_or_default(),
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   410
        );
15925
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   411
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   412
        if val.reversed.unwrap_or_default() {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   413
            edge.reversed()
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   414
        } else {
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   415
            edge
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   416
        }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   417
    }
b0e8cc72bfef Allow defining compatible edges for grid, add few more templates
unC0Rr
parents: 15924
diff changeset
   418
}
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   419
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   420
impl<T: AsRef<str>> From<T> for EdgeDescription {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   421
    fn from(val: T) -> Self {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   422
        use std::cmp::Ordering;
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   423
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   424
        let mut chars = val.as_ref().chars();
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   425
        let hard_match = chars.next() == Some('!');
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   426
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   427
        let (name, reversed): (String, String) = if hard_match {
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   428
            (chars.clone().collect(), chars.rev().collect())
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   429
        } else {
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   430
            (
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   431
                val.as_ref().chars().collect(),
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   432
                val.as_ref().chars().rev().collect(),
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   433
            )
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   434
        };
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   435
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   436
        match name.cmp(&reversed) {
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   437
            Ordering::Less => EdgeDescription {
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   438
                name,
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   439
                symmetrical: Some(false),
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   440
                reversed: Some(false),
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   441
                hard_match: Some(hard_match),
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   442
            },
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   443
            Ordering::Equal => EdgeDescription {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   444
                name: reversed,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   445
                symmetrical: Some(true),
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   446
                reversed: Some(false),
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   447
                hard_match: Some(hard_match),
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   448
            },
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   449
            Ordering::Greater => EdgeDescription {
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   450
                name: reversed,
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   451
                symmetrical: Some(false),
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   452
                reversed: Some(true),
16080
2fc37552b587 Introduce hard match option for edges to reduce backtracking
unC0Rr
parents: 16079
diff changeset
   453
                hard_match: Some(hard_match),
16064
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   454
            },
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   455
        }
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   456
    }
07cb6dbc8444 Implement simplified format for edges in config
unC0Rr
parents: 16062
diff changeset
   457
}