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