rust/landgen/src/wavefront_collapse/generator.rs
author unC0Rr
Tue, 14 Feb 2023 08:52:20 +0100
branchtransitional_engine
changeset 15924 9502611bffc1
parent 15923 d46ad15c6dec
child 15925 b0e8cc72bfef
permissions -rw-r--r--
Some bug fixes, build fixes and code formatting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
     1
use super::tile_image::{Edge, TileImage};
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     2
use super::wavefront_collapse::{CollapseRule, Tile, WavefrontCollapse};
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
     3
use crate::{LandGenerationParameters, LandGenerator};
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     4
use integral_geometry::Size;
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     5
use png::Decoder;
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
     6
use std::collections::HashSet;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
     7
use std::fs::File;
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
     8
use std::io::{BufReader, Result};
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
     9
use std::path::Path;
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    10
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    11
#[derive(Clone)]
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    12
pub struct EdgeDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    13
    pub name: String,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    14
    pub reversed: Option<bool>,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    15
    pub symmetrical: Option<bool>,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    16
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    17
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    18
#[derive(Clone)]
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    19
pub struct EdgesDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    20
    pub top: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    21
    pub right: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    22
    pub bottom: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    23
    pub left: EdgeDescription,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    24
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    25
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    26
#[derive(Clone)]
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    27
pub struct TileDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    28
    pub name: String,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    29
    pub edges: EdgesDescription,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    30
    pub is_negative: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    31
    pub can_flip: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    32
    pub can_mirror: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    33
    pub can_rotate90: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    34
    pub can_rotate180: Option<bool>,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    35
    pub can_rotate270: Option<bool>,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    36
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    37
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    38
#[derive(Clone)]
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    39
pub struct TemplateDescription {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    40
    pub size: Size,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    41
    pub tiles: Vec<TileDescription>,
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    42
    pub wrap: bool,
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    43
}
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    44
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    45
pub struct WavefrontCollapseLandGenerator {
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    46
    pub template: TemplateDescription,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
    47
}
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    48
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    49
impl WavefrontCollapseLandGenerator {
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    50
    pub fn new(template: TemplateDescription) -> Self {
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
    51
        Self { template }
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    52
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    53
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    54
    fn load_image_tiles<T: Copy + PartialEq + Default>(
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    55
        parameters: &LandGenerationParameters<T>,
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    56
        tile_description: &TileDescription,
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
    57
    ) -> Result<Vec<TileImage<T, String>>> {
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    58
        let mut result = Vec::new();
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
    59
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    60
        let file = File::open(
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    61
            Path::new("../share/hedgewars/Data/Tiles")
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    62
                .join(&tile_description.name)
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    63
                .as_path(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    64
        )?;
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    65
        let decoder = Decoder::new(BufReader::new(file));
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    66
        let mut reader = decoder.read_info().unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    67
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    68
        let info = reader.info();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    69
        let mut tiles_image = vec2d::Vec2D::new(
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    70
            &Size::new(info.width as usize, info.height as usize),
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    71
            parameters.zero,
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    72
        );
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    73
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    74
        let mut buf = vec![0; reader.output_buffer_size()];
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    75
        let info = reader.next_frame(&mut buf).unwrap();
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    76
        let bytes = &buf[..info.buffer_size()];
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
    77
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    78
        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
    79
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
    80
        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
    81
            (parameters.basic(), parameters.zero())
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    82
        } else {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    83
            (parameters.zero(), parameters.basic())
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    84
        };
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
    85
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    86
        match info.color_type.samples() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    87
            1 => {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    88
                for line in bytes.chunks_exact(info.line_size) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    89
                    for value in line.iter() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    90
                        *tiles_image_pixels
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    91
                            .next()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    92
                            .expect("vec2d size matching image dimensions") =
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    93
                            if *value == 0 { zero } else { basic };
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    94
                    }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    95
                }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    96
            }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    97
            a => {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    98
                for line in bytes.chunks_exact(info.line_size) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
    99
                    for value in line.chunks_exact(a) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   100
                        print!("{:?},", value);
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   101
                        *tiles_image_pixels
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   102
                            .next()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   103
                            .expect("vec2d size matching image dimensions") =
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   104
                            if value[0] == 0u8 { zero } else { basic };
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   105
                    }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   106
                }
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   107
            }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   108
        }
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   109
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   110
        let edges: Vec<Edge<String>> = [
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   111
            &tile_description.edges.top,
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   112
            &tile_description.edges.right,
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   113
            &tile_description.edges.bottom,
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   114
            &tile_description.edges.left,
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   115
        ]
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   116
        .iter()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   117
        .map(|descr| {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   118
            let edge = Edge::new(descr.name.clone(), descr.symmetrical.unwrap_or_default());
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   119
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   120
            if descr.reversed.unwrap_or_default() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   121
                edge.reversed()
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   122
            } else {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   123
                edge
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   124
            }
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   125
        })
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   126
        .collect();
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   127
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   128
        let [top_edge, right_edge, bottom_edge, left_edge] = edges.as_slice() else {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   129
            unreachable!()
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
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   132
        let tile = TileImage::<T, String>::new(
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   133
            tiles_image,
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   134
            top_edge.clone(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   135
            right_edge.clone(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   136
            bottom_edge.clone(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   137
            left_edge.clone(),
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   138
        );
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   139
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   140
        result.push(tile.clone());
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   141
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   142
        if tile_description.can_flip.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   143
            result.push(tile.flipped());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   144
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   145
        if tile_description.can_mirror.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   146
            result.push(tile.mirrored());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   147
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   148
        if tile_description.can_flip.unwrap_or_default()
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   149
            && tile_description.can_mirror.unwrap_or_default()
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   150
        {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   151
            result.push(tile.mirrored().flipped());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   152
        }
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_rotate90.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   155
            result.push(tile.rotated90());
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_rotate180.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   158
            result.push(tile.rotated180());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   159
        }
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   160
        if tile_description.can_rotate270.unwrap_or_default() {
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   161
            result.push(tile.rotated270());
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   162
        }
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   163
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   164
        Ok(result)
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   165
    }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   166
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   167
    pub fn load_template<T: Copy + PartialEq + Default>(
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   168
        &self,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   169
        parameters: &LandGenerationParameters<T>,
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   170
    ) -> Vec<TileImage<T, String>> {
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   171
        let mut result = Vec::new();
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   172
15923
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   173
        for tile_description in self.template.tiles.iter() {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   174
            if let Ok(mut tiles) = Self::load_image_tiles(parameters, tile_description) {
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   175
                result.append(&mut tiles);
d46ad15c6dec Get wavefront collapse generator to work in engine
unC0Rr
parents: 15922
diff changeset
   176
            }
15920
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   177
        }
168f44ef9b67 Extract tile loading into separate method
unC0Rr
parents: 15919
diff changeset
   178
15916
e82de0410da5 Rework how rules are defined, add transformations for tiles
unC0Rr
parents: 15915
diff changeset
   179
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   180
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   181
}
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   182
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   183
impl LandGenerator for WavefrontCollapseLandGenerator {
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   184
    fn generate_land<T: Copy + PartialEq + Default, I: Iterator<Item = u32>>(
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   185
        &self,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   186
        parameters: &LandGenerationParameters<T>,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   187
        random_numbers: &mut I,
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   188
    ) -> land2d::Land2D<T> {
15915
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   189
        let tiles = self.load_template(parameters);
8f093b1b18bc Add loading of tiles from png
unC0Rr
parents: 15913
diff changeset
   190
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   191
        let mut rules = Vec::<CollapseRule>::new();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   192
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   193
        let default_connection = HashSet::from_iter(vec![Tile::Outside, Tile::Empty].into_iter());
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   194
        for (i, tile) in tiles.iter().enumerate() {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   195
            let mut right = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   196
            let mut bottom = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   197
            let mut left = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   198
            let mut top = default_connection.clone();
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   199
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   200
            for p in 0..i {
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   201
                if tiles[p].left_edge().is_compatible(tile.right_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   202
                    rules[p].left.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   203
                    right.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   204
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   205
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   206
                if tiles[p].right_edge().is_compatible(tile.left_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   207
                    rules[p].right.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   208
                    left.insert(Tile::Numbered(p));
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
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   211
                if tiles[p].top_edge().is_compatible(tile.bottom_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   212
                    rules[p].top.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   213
                    bottom.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   214
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   215
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   216
                if tiles[p].bottom_edge().is_compatible(tile.top_edge()) {
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   217
                    rules[p].bottom.insert(Tile::Numbered(i));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   218
                    top.insert(Tile::Numbered(p));
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   219
                }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   220
            }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   221
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   222
            rules.push(CollapseRule {
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   223
                tile: Tile::Numbered(i),
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   224
                top,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   225
                right,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   226
                bottom,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   227
                left,
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   228
            });
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   229
        }
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   230
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   231
        let mut wfc = WavefrontCollapse::new(self.template.wrap);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   232
        wfc.set_rules(rules);
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   233
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   234
        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
   235
            let tile_size = first_tile.size();
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   236
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   237
            Size::new(
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   238
                self.template.size.width / tile_size.width,
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   239
                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
   240
            )
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   241
        } else {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   242
            Size::new(1, 1)
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   243
        };
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   244
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   245
        wfc.generate_map(&wfc_size, |_| {}, random_numbers);
15917
60b5639cc3a5 Add WIP for generation of rules
unC0Rr
parents: 15916
diff changeset
   246
15922
da6b67f13c12 Refactor mapgen to allow for easy switching between generators
unC0Rr
parents: 15920
diff changeset
   247
        let mut result = land2d::Land2D::new(&self.template.size, parameters.zero);
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   248
        let offset_y = result.height() - result.play_height();
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   249
        let offset_x = (result.width() - result.play_width()) / 2;
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   250
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   251
        for row in 0..wfc_size.height {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   252
            for column in 0..wfc_size.width {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   253
                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
   254
                    let tile = &tiles[*tile_index];
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   255
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   256
                    for tile_row in 0..tile.size().height {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   257
                        for tile_column in 0..tile.size().width {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   258
                            result.map(
15924
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   259
                                (row * tile.size().height + tile_row + offset_y) as i32,
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15923
diff changeset
   260
                                (column * tile.size().width + tile_column + offset_x) as i32,
15918
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   261
                                |p| {
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   262
                                    *p =
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   263
                                        *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
   264
                                },
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   265
                            );
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   266
                        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   267
                    }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   268
                }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   269
            }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   270
        }
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   271
9bd828451d77 Fix several issues with transformations, more work on getting generated image
unC0Rr
parents: 15917
diff changeset
   272
        result
15913
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   273
    }
c5684cc62de8 Switch to Vec2D in wavefront algorithm
unC0Rr
parents:
diff changeset
   274
}